Gabriel Souza
4 years ago
committed by
GitHub
3 changed files with 82 additions and 9 deletions
@ -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" |
||||
} |
||||
} |
@ -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…
Reference in new issue