From dd205b1955bb37dafd29f263b331e6f3b5dd109a Mon Sep 17 00:00:00 2001 From: dima-avdeev-jb <99798741+dima-avdeev-jb@users.noreply.github.com> Date: Mon, 11 Apr 2022 12:48:36 +0400 Subject: [PATCH] Intellij compose tool window panel (#1994) --- .../com/jetbrains/compose/IntellijTheme.kt | 28 ++++++++++++ .../compose/color/ColorLineMarkerProvider.kt | 30 ++++--------- .../compose/panel/ComposeToolWindow.kt | 44 +++++++++++++++++++ .../jetbrains/compose/panel/CounterPanel.kt | 36 +++++++++++++++ .../jetbrains/compose/panel/CounterState.kt | 10 +++++ .../src/main/resources/META-INF/plugin.xml | 2 + .../src/main/resources/icons/compose.svg | 1 + 7 files changed, 130 insertions(+), 21 deletions(-) create mode 100644 examples/intellij-plugin-with-experimental-shared-base/src/main/kotlin/com/jetbrains/compose/IntellijTheme.kt create mode 100644 examples/intellij-plugin-with-experimental-shared-base/src/main/kotlin/com/jetbrains/compose/panel/ComposeToolWindow.kt create mode 100644 examples/intellij-plugin-with-experimental-shared-base/src/main/kotlin/com/jetbrains/compose/panel/CounterPanel.kt create mode 100644 examples/intellij-plugin-with-experimental-shared-base/src/main/kotlin/com/jetbrains/compose/panel/CounterState.kt create mode 100644 examples/intellij-plugin-with-experimental-shared-base/src/main/resources/icons/compose.svg diff --git a/examples/intellij-plugin-with-experimental-shared-base/src/main/kotlin/com/jetbrains/compose/IntellijTheme.kt b/examples/intellij-plugin-with-experimental-shared-base/src/main/kotlin/com/jetbrains/compose/IntellijTheme.kt new file mode 100644 index 0000000000..8969581b72 --- /dev/null +++ b/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() + } + } +} diff --git a/examples/intellij-plugin-with-experimental-shared-base/src/main/kotlin/com/jetbrains/compose/color/ColorLineMarkerProvider.kt b/examples/intellij-plugin-with-experimental-shared-base/src/main/kotlin/com/jetbrains/compose/color/ColorLineMarkerProvider.kt index a9cc554ec0..1ab8977c62 100644 --- a/examples/intellij-plugin-with-experimental-shared-base/src/main/kotlin/com/jetbrains/compose/color/ColorLineMarkerProvider.kt +++ b/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 -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.material.Surface import androidx.compose.runtime.mutableStateOf -import androidx.compose.ui.Modifier import androidx.compose.ui.awt.ComposePanel +import androidx.compose.ui.graphics.Color import com.intellij.codeInsight.daemon.LineMarkerInfo 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.ui.DialogWrapper import com.intellij.psi.PsiElement +import com.jetbrains.compose.IntellijTheme import org.jetbrains.kotlin.psi.KtPsiFactory 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.Graphics import javax.swing.Icon +import javax.swing.JComponent class ColorLineMarkerProvider : LineMarkerProvider { @@ -59,16 +51,14 @@ class ColorLineMarkerProvider : LineMarkerProvider { ) g?.fillRect(0, 0, iconSize, iconSize) } + override fun getIconWidth(): Int = iconSize override fun getIconHeight(): Int = iconSize }, null, { _, psiElement: PsiElement -> - val isDarkMode = try { - GraphicsManager.getInstance(project)?.isDarkModeEnabled ?: false - } catch (t: Throwable) { - false - } + + class ChooseColorDialog() : DialogWrapper(project) { val colorState = mutableStateOf(previousColor) @@ -81,10 +71,8 @@ class ColorLineMarkerProvider : LineMarkerProvider { ComposePanel().apply { setBounds(0, 0, 400, 400) setContent { - WidgetTheme(darkTheme = isDarkMode) { - Surface(modifier = Modifier.fillMaxSize()) { - ColorPicker(colorState) - } + IntellijTheme(project) { + ColorPicker(colorState) } } } diff --git a/examples/intellij-plugin-with-experimental-shared-base/src/main/kotlin/com/jetbrains/compose/panel/ComposeToolWindow.kt b/examples/intellij-plugin-with-experimental-shared-base/src/main/kotlin/com/jetbrains/compose/panel/ComposeToolWindow.kt new file mode 100644 index 0000000000..c7e66f37c9 --- /dev/null +++ b/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()) + } + +} diff --git a/examples/intellij-plugin-with-experimental-shared-base/src/main/kotlin/com/jetbrains/compose/panel/CounterPanel.kt b/examples/intellij-plugin-with-experimental-shared-base/src/main/kotlin/com/jetbrains/compose/panel/CounterPanel.kt new file mode 100644 index 0000000000..24be64a9f9 --- /dev/null +++ b/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) { + 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") + } + } +} diff --git a/examples/intellij-plugin-with-experimental-shared-base/src/main/kotlin/com/jetbrains/compose/panel/CounterState.kt b/examples/intellij-plugin-with-experimental-shared-base/src/main/kotlin/com/jetbrains/compose/panel/CounterState.kt new file mode 100644 index 0000000000..5466e579f9 --- /dev/null +++ b/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 +) diff --git a/examples/intellij-plugin-with-experimental-shared-base/src/main/resources/META-INF/plugin.xml b/examples/intellij-plugin-with-experimental-shared-base/src/main/resources/META-INF/plugin.xml index 9aaa65acf2..5932048466 100644 --- a/examples/intellij-plugin-with-experimental-shared-base/src/main/resources/META-INF/plugin.xml +++ b/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 + \ No newline at end of file diff --git a/examples/intellij-plugin-with-experimental-shared-base/src/main/resources/icons/compose.svg b/examples/intellij-plugin-with-experimental-shared-base/src/main/resources/icons/compose.svg new file mode 100644 index 0000000000..2154d497bd --- /dev/null +++ b/examples/intellij-plugin-with-experimental-shared-base/src/main/resources/icons/compose.svg @@ -0,0 +1 @@ + \ No newline at end of file