Browse Source

Intellij compose tool window panel (#1994)

pull/2014/head
dima-avdeev-jb 3 years ago committed by GitHub
parent
commit
dd205b1955
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 28
      examples/intellij-plugin-with-experimental-shared-base/src/main/kotlin/com/jetbrains/compose/IntellijTheme.kt
  2. 30
      examples/intellij-plugin-with-experimental-shared-base/src/main/kotlin/com/jetbrains/compose/color/ColorLineMarkerProvider.kt
  3. 44
      examples/intellij-plugin-with-experimental-shared-base/src/main/kotlin/com/jetbrains/compose/panel/ComposeToolWindow.kt
  4. 36
      examples/intellij-plugin-with-experimental-shared-base/src/main/kotlin/com/jetbrains/compose/panel/CounterPanel.kt
  5. 10
      examples/intellij-plugin-with-experimental-shared-base/src/main/kotlin/com/jetbrains/compose/panel/CounterState.kt
  6. 2
      examples/intellij-plugin-with-experimental-shared-base/src/main/resources/META-INF/plugin.xml
  7. 1
      examples/intellij-plugin-with-experimental-shared-base/src/main/resources/icons/compose.svg

28
examples/intellij-plugin-with-experimental-shared-base/src/main/kotlin/com/jetbrains/compose/IntellijTheme.kt

@ -0,0 +1,28 @@
/*
* Copyright 2020-2022 JetBrains s.r.o. and respective authors and developers.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
*/
package com.jetbrains.compose
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.intellij.openapi.project.Project
import com.jetbrains.compose.theme.WidgetTheme
import org.intellij.datavis.r.inlays.components.GraphicsManager
@Composable
fun IntellijTheme(project: Project, content: @Composable () -> Unit) {
val isDarkMode = try {
GraphicsManager.getInstance(project)?.isDarkModeEnabled ?: false
} catch (t: Throwable) {
false
}
WidgetTheme(darkTheme = isDarkMode) {
Surface(modifier = Modifier.fillMaxSize()) {
content()
}
}
}

30
examples/intellij-plugin-with-experimental-shared-base/src/main/kotlin/com/jetbrains/compose/color/ColorLineMarkerProvider.kt

@ -5,30 +5,22 @@
package com.jetbrains.compose.color package com.jetbrains.compose.color
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Surface
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.awt.ComposePanel import androidx.compose.ui.awt.ComposePanel
import androidx.compose.ui.graphics.Color
import com.intellij.codeInsight.daemon.LineMarkerInfo import com.intellij.codeInsight.daemon.LineMarkerInfo
import com.intellij.codeInsight.daemon.LineMarkerProvider import com.intellij.codeInsight.daemon.LineMarkerProvider
import com.intellij.icons.AllIcons import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.editor.markup.GutterIconRenderer import com.intellij.openapi.editor.markup.GutterIconRenderer
import com.intellij.openapi.ui.DialogWrapper import com.intellij.openapi.ui.DialogWrapper
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import com.jetbrains.compose.IntellijTheme
import org.jetbrains.kotlin.psi.KtPsiFactory import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.uast.* import org.jetbrains.uast.*
import javax.swing.JComponent
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.painter.ColorPainter
import androidx.compose.ui.graphics.toArgb
import com.intellij.openapi.application.ApplicationManager
import com.jetbrains.compose.theme.WidgetTheme
import org.intellij.datavis.r.inlays.components.GraphicsManager
import java.awt.Component import java.awt.Component
import java.awt.Graphics import java.awt.Graphics
import javax.swing.Icon import javax.swing.Icon
import javax.swing.JComponent
class ColorLineMarkerProvider : LineMarkerProvider { class ColorLineMarkerProvider : LineMarkerProvider {
@ -59,16 +51,14 @@ class ColorLineMarkerProvider : LineMarkerProvider {
) )
g?.fillRect(0, 0, iconSize, iconSize) g?.fillRect(0, 0, iconSize, iconSize)
} }
override fun getIconWidth(): Int = iconSize override fun getIconWidth(): Int = iconSize
override fun getIconHeight(): Int = iconSize override fun getIconHeight(): Int = iconSize
}, },
null, null,
{ _, psiElement: PsiElement -> { _, psiElement: PsiElement ->
val isDarkMode = try {
GraphicsManager.getInstance(project)?.isDarkModeEnabled ?: false
} catch (t: Throwable) {
false
}
class ChooseColorDialog() : DialogWrapper(project) { class ChooseColorDialog() : DialogWrapper(project) {
val colorState = mutableStateOf(previousColor) val colorState = mutableStateOf(previousColor)
@ -81,10 +71,8 @@ class ColorLineMarkerProvider : LineMarkerProvider {
ComposePanel().apply { ComposePanel().apply {
setBounds(0, 0, 400, 400) setBounds(0, 0, 400, 400)
setContent { setContent {
WidgetTheme(darkTheme = isDarkMode) { IntellijTheme(project) {
Surface(modifier = Modifier.fillMaxSize()) { ColorPicker(colorState)
ColorPicker(colorState)
}
} }
} }
} }

44
examples/intellij-plugin-with-experimental-shared-base/src/main/kotlin/com/jetbrains/compose/panel/ComposeToolWindow.kt

@ -0,0 +1,44 @@
/*
* Copyright 2020-2022 JetBrains s.r.o. and respective authors and developers.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
*/
package com.jetbrains.compose.panel
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.awt.ComposePanel
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.ToolWindow
import com.intellij.openapi.wm.ToolWindowFactory
import com.intellij.ui.content.ContentFactory
import com.jetbrains.compose.IntellijTheme
import java.awt.Dimension
class ComposeToolWindow : ToolWindowFactory, DumbAware {
override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
ApplicationManager.getApplication().invokeLater {
toolWindow.contentManager.addContent(
ContentFactory.SERVICE.getInstance().createContent(
ComposePanel().apply {
size = Dimension(300, 300)
setContent {
IntellijTheme(project) {
CounterPanel(stateWithIdeLifecycle)
}
}
},
"Compose tool window",
false
)
)
}
}
companion object {
val stateWithIdeLifecycle = mutableStateOf(CounterState())
}
}

36
examples/intellij-plugin-with-experimental-shared-base/src/main/kotlin/com/jetbrains/compose/panel/CounterPanel.kt

@ -0,0 +1,36 @@
/*
* Copyright 2020-2022 JetBrains s.r.o. and respective authors and developers.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
*/
package com.jetbrains.compose.panel
import androidx.compose.foundation.layout.Column
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.*
@Composable
fun CounterPanel(stateWithIdeLifecycle: MutableState<CounterState>) {
var stateInline by remember { mutableStateOf(CounterState()) }
Column {
Text("Counter with IDE lifecycle: ${stateWithIdeLifecycle.value.counter}")
Button(onClick = {
stateWithIdeLifecycle.value = stateWithIdeLifecycle.value.copy(
counter = stateWithIdeLifecycle.value.counter + 1
)
}) {
Text("Increment state with IDE lifecycle")
}
Text("Counter with @Composable lifecycle: ${stateInline.counter}")
Button(onClick = {
stateInline = stateInline.copy(
counter = stateInline.counter + 1
)
}) {
Text("Increment state with @Composable lifecycle")
}
}
}

10
examples/intellij-plugin-with-experimental-shared-base/src/main/kotlin/com/jetbrains/compose/panel/CounterState.kt

@ -0,0 +1,10 @@
/*
* Copyright 2020-2022 JetBrains s.r.o. and respective authors and developers.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
*/
package com.jetbrains.compose.panel
data class CounterState(
val counter: Int = 0
)

2
examples/intellij-plugin-with-experimental-shared-base/src/main/resources/META-INF/plugin.xml

@ -22,6 +22,8 @@ A plugin demonstrates Jetpack compose capabilities on IntelliJ Platform
</actions> </actions>
<extensions defaultExtensionNs="com.intellij"> <extensions defaultExtensionNs="com.intellij">
<codeInsight.lineMarkerProvider language="kotlin" implementationClass="com.jetbrains.compose.color.ColorLineMarkerProvider" /> <codeInsight.lineMarkerProvider language="kotlin" implementationClass="com.jetbrains.compose.color.ColorLineMarkerProvider" />
<toolWindow id="Compose" anchor="right" secondary="false" factoryClass="com.jetbrains.compose.panel.ComposeToolWindow"
icon="/icons/compose.svg"/>
</extensions> </extensions>
</idea-plugin> </idea-plugin>

1
examples/intellij-plugin-with-experimental-shared-base/src/main/resources/icons/compose.svg

@ -0,0 +1 @@
<svg width="16" height="16" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><rect id="frame" width="16" height="16" fill="none"/><g fill="none" fill-rule="evenodd"><path d="M8.5 1L14 4l-3.317 1.81L8.5 4.5 6.317 5.81 3 4l5.5-3z" fill="#3DDC84"/><path d="M3 5l3 1.589V9l2 1.2V14l-5-3V5z" fill="#073042"/><path d="M14 5v6l-5 3v-3.8L11 9V6.589L14 5z" fill="#4285F4"/><path fill="#D6F0FF" d="M8.5 4.5L11 6v3l-2.5 1.5L6 9V6z"/></g></svg>

After

Width:  |  Height:  |  Size: 438 B

Loading…
Cancel
Save