From cc2ddac2e2d83d2e04dba96e33ae87efd8e5a921 Mon Sep 17 00:00:00 2001 From: Gabriel Souza Date: Mon, 15 Feb 2021 03:31:44 -0300 Subject: [PATCH] Updates the intelliJPlugin example with currently IDEA Theme colors (#351) --- .../com/jetbrains/compose/theme/Theme.kt | 17 +++--- .../compose/theme/intellij/SwingColor.kt | 61 +++++++++++++++++++ .../theme/intellij/ThemeChangeListener.kt | 13 ++++ 3 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 examples/intelliJPlugin/src/main/kotlin/com/jetbrains/compose/theme/intellij/SwingColor.kt create mode 100644 examples/intelliJPlugin/src/main/kotlin/com/jetbrains/compose/theme/intellij/ThemeChangeListener.kt diff --git a/examples/intelliJPlugin/src/main/kotlin/com/jetbrains/compose/theme/Theme.kt b/examples/intelliJPlugin/src/main/kotlin/com/jetbrains/compose/theme/Theme.kt index 150352193a..78a3ec5181 100644 --- a/examples/intelliJPlugin/src/main/kotlin/com/jetbrains/compose/theme/Theme.kt +++ b/examples/intelliJPlugin/src/main/kotlin/com/jetbrains/compose/theme/Theme.kt @@ -5,17 +5,14 @@ import androidx.compose.material.darkColors import androidx.compose.material.lightColors import androidx.compose.runtime.Composable import androidx.compose.ui.graphics.Color +import com.jetbrains.compose.theme.intellij.SwingColor private val DarkGreenColorPalette = darkColors( primary = green200, primaryVariant = green700, secondary = teal200, - background = Color.Black, - surface = Color(74, 74, 74), onPrimary = Color.Black, onSecondary = Color.White, - onBackground = Color.White, - onSurface = Color.White, error = Color.Red, ) @@ -23,11 +20,7 @@ private val LightGreenColorPalette = lightColors( primary = green500, primaryVariant = green700, secondary = teal200, - background = Color.White, - surface = Color.White, onPrimary = Color.White, - onSecondary = Color.Black, - onBackground = Color.Black, onSurface = Color.Black ) @@ -37,9 +30,15 @@ fun WidgetTheme( content: @Composable() () -> Unit, ) { val colors = if (darkTheme) DarkGreenColorPalette else LightGreenColorPalette + val swingColor = SwingColor() MaterialTheme( - colors = colors, + colors = colors.copy( + background = swingColor.background, + onBackground = swingColor.onBackground, + surface = swingColor.background, + onSurface = swingColor.onBackground, + ), typography = typography, shapes = shapes, content = content diff --git a/examples/intelliJPlugin/src/main/kotlin/com/jetbrains/compose/theme/intellij/SwingColor.kt b/examples/intelliJPlugin/src/main/kotlin/com/jetbrains/compose/theme/intellij/SwingColor.kt new file mode 100644 index 0000000000..0aac1aaf50 --- /dev/null +++ b/examples/intelliJPlugin/src/main/kotlin/com/jetbrains/compose/theme/intellij/SwingColor.kt @@ -0,0 +1,61 @@ +package com.jetbrains.compose.theme.intellij + +import androidx.compose.runtime.* +import androidx.compose.ui.graphics.Color +import com.intellij.ide.ui.LafManagerListener +import com.intellij.openapi.application.ApplicationManager +import javax.swing.UIManager +import java.awt.Color as AWTColor + +interface SwingColor { + val background: Color + val onBackground: Color +} + +@Composable +fun SwingColor(): SwingColor { + val swingColor = remember { SwingColorImpl() } + + val messageBus = remember { + ApplicationManager.getApplication().messageBus.connect() + } + + remember(messageBus) { + messageBus.subscribe( + LafManagerListener.TOPIC, + ThemeChangeListener(swingColor::updateCurrentColors) + ) + } + + DisposableEffect(messageBus) { + onDispose { + messageBus.disconnect() + } + } + + return swingColor +} + +private class SwingColorImpl : SwingColor { + private val _backgroundState: MutableState = mutableStateOf(getBackgroundColor) + private val _onBackgroundState: MutableState = mutableStateOf(getOnBackgroundColor) + + override val background: Color get() = _backgroundState.value + override val onBackground: Color get() = _onBackgroundState.value + + private val getBackgroundColor get() = getColor(BACKGROUND_KEY) + private val getOnBackgroundColor get() = getColor(ON_BACKGROUND_KEY) + + fun updateCurrentColors() { + _backgroundState.value = getBackgroundColor + _onBackgroundState.value = getOnBackgroundColor + } + + private val AWTColor.asComposeColor: Color get() = Color(red, green, blue, alpha) + private fun getColor(key: String): Color = UIManager.getColor(key).asComposeColor + + companion object { + private const val BACKGROUND_KEY = "Panel.background" + private const val ON_BACKGROUND_KEY = "Panel.foreground" + } +} \ No newline at end of file diff --git a/examples/intelliJPlugin/src/main/kotlin/com/jetbrains/compose/theme/intellij/ThemeChangeListener.kt b/examples/intelliJPlugin/src/main/kotlin/com/jetbrains/compose/theme/intellij/ThemeChangeListener.kt new file mode 100644 index 0000000000..95a8324114 --- /dev/null +++ b/examples/intelliJPlugin/src/main/kotlin/com/jetbrains/compose/theme/intellij/ThemeChangeListener.kt @@ -0,0 +1,13 @@ +package com.jetbrains.compose.theme.intellij + +import com.intellij.ide.ui.LafManager +import com.intellij.ide.ui.LafManagerListener + +internal class ThemeChangeListener( + val updateColors: () -> Unit +) : LafManagerListener { + override fun lookAndFeelChanged(source: LafManager) { + updateColors() + } +} +