Browse Source

Updates the intelliJPlugin example with currently IDEA Theme colors (#351)

pull/384/head
Gabriel Souza 4 years ago committed by GitHub
parent
commit
cc2ddac2e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      examples/intelliJPlugin/src/main/kotlin/com/jetbrains/compose/theme/Theme.kt
  2. 61
      examples/intelliJPlugin/src/main/kotlin/com/jetbrains/compose/theme/intellij/SwingColor.kt
  3. 13
      examples/intelliJPlugin/src/main/kotlin/com/jetbrains/compose/theme/intellij/ThemeChangeListener.kt

17
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

61
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<Color> = mutableStateOf(getBackgroundColor)
private val _onBackgroundState: MutableState<Color> = 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"
}
}

13
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()
}
}
Loading…
Cancel
Save