mirror of
https://github.com/jlengrand/compose-multiplatform.git
synced 2026-03-10 08:11:20 +00:00
Intellij compose tool window panel (#1994)
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
)
|
||||
@@ -22,6 +22,8 @@ A plugin demonstrates Jetpack compose capabilities on IntelliJ Platform
|
||||
</actions>
|
||||
<extensions defaultExtensionNs="com.intellij">
|
||||
<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>
|
||||
|
||||
</idea-plugin>
|
||||
@@ -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 |
Reference in New Issue
Block a user