From c53d7cecd25566ef0af5a3ab9b5d1512f4fd3ee4 Mon Sep 17 00:00:00 2001 From: higan Date: Wed, 18 Oct 2023 01:17:51 +0800 Subject: [PATCH] Remove OnBackground --- .../kotlin/org/jetbrains/jewel/Checkbox.kt | 10 +++++---- .../kotlin/org/jetbrains/jewel/IconButton.kt | 4 +--- .../org/jetbrains/jewel/IntelliJTheme.kt | 22 +++---------------- .../kotlin/org/jetbrains/jewel/RadioButton.kt | 10 +++++---- .../kotlin/org/jetbrains/jewel/Tooltip.kt | 3 ++- .../jewel/painter/PainterHintsProvider.kt | 6 ++--- .../org/jetbrains/jewel/window/TitleBar.kt | 6 ++--- .../StandalonePainterHintsProvider.kt | 4 ++-- 8 files changed, 26 insertions(+), 39 deletions(-) diff --git a/core/src/main/kotlin/org/jetbrains/jewel/Checkbox.kt b/core/src/main/kotlin/org/jetbrains/jewel/Checkbox.kt index 5a6eb2cad2..2f42e4d093 100644 --- a/core/src/main/kotlin/org/jetbrains/jewel/Checkbox.kt +++ b/core/src/main/kotlin/org/jetbrains/jewel/Checkbox.kt @@ -249,17 +249,15 @@ private fun CheckboxImpl( checkboxState = checkboxState.copy(toggleableState = state, enabled = enabled) } - val swingCompat = LocalSwingCompatMode.current - LaunchedEffect(interactionSource) { interactionSource.interactions.collect { interaction -> when (interaction) { - is PressInteraction.Press -> checkboxState = checkboxState.copy(pressed = !swingCompat) + is PressInteraction.Press -> checkboxState = checkboxState.copy(pressed = true) is PressInteraction.Cancel, is PressInteraction.Release -> checkboxState = checkboxState.copy(pressed = false) - is HoverInteraction.Enter -> checkboxState = checkboxState.copy(hovered = !swingCompat) + is HoverInteraction.Enter -> checkboxState = checkboxState.copy(hovered = true) is HoverInteraction.Exit -> checkboxState = checkboxState.copy(hovered = false) is FocusInteraction.Focus -> checkboxState = checkboxState.copy(focused = true) is FocusInteraction.Unfocus -> checkboxState = checkboxState.copy(focused = false) @@ -267,6 +265,10 @@ private fun CheckboxImpl( } } + if (LocalSwingCompatMode.current) { + checkboxState = checkboxState.copy(hovered = false, pressed = false) + } + val wrapperModifier = modifier.triStateToggleable( state = state, onClick = onClick, diff --git a/core/src/main/kotlin/org/jetbrains/jewel/IconButton.kt b/core/src/main/kotlin/org/jetbrains/jewel/IconButton.kt index f610e44ace..61b4e4a2f2 100644 --- a/core/src/main/kotlin/org/jetbrains/jewel/IconButton.kt +++ b/core/src/main/kotlin/org/jetbrains/jewel/IconButton.kt @@ -74,9 +74,7 @@ fun IconButton( .border(style.metrics.borderWidth, border, shape), contentAlignment = Alignment.Center, content = { - onBackground(background) { - content(buttonState) - } + content(buttonState) }, ) } diff --git a/core/src/main/kotlin/org/jetbrains/jewel/IntelliJTheme.kt b/core/src/main/kotlin/org/jetbrains/jewel/IntelliJTheme.kt index e8b6b0608d..1de19e3dfb 100644 --- a/core/src/main/kotlin/org/jetbrains/jewel/IntelliJTheme.kt +++ b/core/src/main/kotlin/org/jetbrains/jewel/IntelliJTheme.kt @@ -5,7 +5,6 @@ import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.ReadOnlyComposable import androidx.compose.runtime.staticCompositionLocalOf import androidx.compose.ui.graphics.Color -import androidx.compose.ui.graphics.isSpecified import androidx.compose.ui.text.TextStyle import org.jetbrains.jewel.styling.ButtonStyle import org.jetbrains.jewel.styling.CheckboxStyle @@ -222,7 +221,6 @@ fun IntelliJTheme( fun IntelliJTheme(theme: IntelliJThemeDefinition, content: @Composable () -> Unit) { CompositionLocalProvider( LocalIsDarkTheme provides theme.isDark, - LocalOnDarkBackground provides theme.isDark, LocalContentColor provides theme.contentColor, LocalTextStyle provides theme.defaultTextStyle, LocalGlobalColors provides theme.globalColors, @@ -235,10 +233,6 @@ internal val LocalIsDarkTheme = staticCompositionLocalOf { error("No InDarkTheme provided") } -val LocalOnDarkBackground = staticCompositionLocalOf { - error("No OnDarkBackground provided") -} - internal val LocalSwingCompatMode = staticCompositionLocalOf { // By default, Swing compat is not enabled false @@ -253,19 +247,9 @@ val LocalIconData = staticCompositionLocalOf { } /** - * Sets the background color of the current area, - * which affects the style(light or dark) of the icon rendered above it, - * by calculating the luminance. - * If the color is not specified, the style will follow the current theme style. - * Transparent color will be ignored. + * Overrides the dark mode of the current area. */ @Composable -fun onBackground(color: Color, content: @Composable () -> Unit) { - val locals = if (color.isSpecified && color.alpha > 0) { - arrayOf(LocalOnDarkBackground provides color.isDark()) - } else { - emptyArray() - } - - CompositionLocalProvider(values = locals, content) +fun OverrideDarkMode(isDark: Boolean, content: @Composable () -> Unit) { + CompositionLocalProvider(LocalIsDarkTheme provides isDark, content = content) } diff --git a/core/src/main/kotlin/org/jetbrains/jewel/RadioButton.kt b/core/src/main/kotlin/org/jetbrains/jewel/RadioButton.kt index b6c4337491..5b3bdb34a5 100644 --- a/core/src/main/kotlin/org/jetbrains/jewel/RadioButton.kt +++ b/core/src/main/kotlin/org/jetbrains/jewel/RadioButton.kt @@ -132,17 +132,15 @@ private fun RadioButtonImpl( radioButtonState = radioButtonState.copy(selected = selected, enabled = enabled) } - val swingCompat = LocalSwingCompatMode.current - LaunchedEffect(interactionSource) { interactionSource.interactions.collect { interaction -> when (interaction) { - is PressInteraction.Press -> radioButtonState = radioButtonState.copy(pressed = !swingCompat) + is PressInteraction.Press -> radioButtonState = radioButtonState.copy(pressed = true) is PressInteraction.Cancel, is PressInteraction.Release -> radioButtonState = radioButtonState.copy(pressed = false) - is HoverInteraction.Enter -> radioButtonState = radioButtonState.copy(hovered = !swingCompat) + is HoverInteraction.Enter -> radioButtonState = radioButtonState.copy(hovered = true) is HoverInteraction.Exit -> radioButtonState = radioButtonState.copy(hovered = false) is FocusInteraction.Focus -> radioButtonState = radioButtonState.copy(focused = true) is FocusInteraction.Unfocus -> radioButtonState = radioButtonState.copy(focused = false) @@ -150,6 +148,10 @@ private fun RadioButtonImpl( } } + if (LocalSwingCompatMode.current) { + radioButtonState = radioButtonState.copy(hovered = false, pressed = false) + } + val wrapperModifier = modifier.selectable( selected = selected, onClick = onClick, diff --git a/core/src/main/kotlin/org/jetbrains/jewel/Tooltip.kt b/core/src/main/kotlin/org/jetbrains/jewel/Tooltip.kt index f504a5e63a..90565aa4fb 100644 --- a/core/src/main/kotlin/org/jetbrains/jewel/Tooltip.kt +++ b/core/src/main/kotlin/org/jetbrains/jewel/Tooltip.kt @@ -26,6 +26,7 @@ import androidx.compose.ui.unit.LayoutDirection import androidx.compose.ui.unit.dp import androidx.compose.ui.window.PopupPositionProvider import org.jetbrains.jewel.styling.TooltipStyle +import org.jetbrains.jewel.util.isDark @Composable fun Tooltip( @@ -63,7 +64,7 @@ fun Tooltip( ) .padding(style.metrics.contentPadding), ) { - onBackground(style.colors.background) { + OverrideDarkMode(style.colors.background.isDark()) { tooltip() } } diff --git a/core/src/main/kotlin/org/jetbrains/jewel/painter/PainterHintsProvider.kt b/core/src/main/kotlin/org/jetbrains/jewel/painter/PainterHintsProvider.kt index 536bfc381f..4f1d42a407 100644 --- a/core/src/main/kotlin/org/jetbrains/jewel/painter/PainterHintsProvider.kt +++ b/core/src/main/kotlin/org/jetbrains/jewel/painter/PainterHintsProvider.kt @@ -3,7 +3,7 @@ package org.jetbrains.jewel.painter import androidx.compose.runtime.Composable import androidx.compose.runtime.Immutable import androidx.compose.runtime.staticCompositionLocalOf -import org.jetbrains.jewel.LocalOnDarkBackground +import org.jetbrains.jewel.IntelliJTheme import org.jetbrains.jewel.painter.hints.Dark /** @@ -22,12 +22,12 @@ interface PainterHintsProvider { /** * The default [PainterHintsProvider] to load dark theme icon variants. - * It will provide the [Dark] hint when [LocalOnDarkBackground] is true. + * It will provide the [Dark] hint when [LocalIsDarkTheme][org.jetbrains.jewel.LocalIsDarkTheme] is true. */ object DarkPainterHintsProvider : PainterHintsProvider { @Composable - override fun hints(path: String): List = listOf(Dark(LocalOnDarkBackground.current)) + override fun hints(path: String): List = listOf(Dark(IntelliJTheme.isDark)) } val LocalPainterHintsProvider = staticCompositionLocalOf { diff --git a/decorated-window/src/main/kotlin/org/jetbrains/jewel/window/TitleBar.kt b/decorated-window/src/main/kotlin/org/jetbrains/jewel/window/TitleBar.kt index a3ebffa25c..4db0f8ab59 100644 --- a/decorated-window/src/main/kotlin/org/jetbrains/jewel/window/TitleBar.kt +++ b/decorated-window/src/main/kotlin/org/jetbrains/jewel/window/TitleBar.kt @@ -10,7 +10,6 @@ import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.Stable import androidx.compose.runtime.getValue import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.focus.focusProperties @@ -41,9 +40,10 @@ import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.offset import org.jetbrains.jewel.IntelliJTheme import org.jetbrains.jewel.LocalContentColor -import org.jetbrains.jewel.onBackground +import org.jetbrains.jewel.OverrideDarkMode import org.jetbrains.jewel.styling.LocalDropdownStyle import org.jetbrains.jewel.styling.LocalIconButtonStyle +import org.jetbrains.jewel.util.isDark import org.jetbrains.jewel.window.styling.TitleBarStyle import org.jetbrains.jewel.window.utils.DesktopPlatform import org.jetbrains.jewel.window.utils.macos.MacUtil @@ -106,7 +106,7 @@ internal const val TITLE_BAR_BORDER_LAYOUT_ID = "__TITLE_BAR_BORDER__" LocalIconButtonStyle provides style.iconButtonStyle, LocalDropdownStyle provides style.dropdownStyle, ) { - onBackground(background) { + OverrideDarkMode(background.isDark()) { val scope = TitleBarScopeImpl(titleBarInfo.title, titleBarInfo.icon) scope.content(state) } diff --git a/int-ui/int-ui-standalone/src/main/kotlin/org/jetbrains/jewel/intui/standalone/StandalonePainterHintsProvider.kt b/int-ui/int-ui-standalone/src/main/kotlin/org/jetbrains/jewel/intui/standalone/StandalonePainterHintsProvider.kt index 9c57af2f94..53ca57477c 100644 --- a/int-ui/int-ui-standalone/src/main/kotlin/org/jetbrains/jewel/intui/standalone/StandalonePainterHintsProvider.kt +++ b/int-ui/int-ui-standalone/src/main/kotlin/org/jetbrains/jewel/intui/standalone/StandalonePainterHintsProvider.kt @@ -1,7 +1,7 @@ package org.jetbrains.jewel.intui.standalone import androidx.compose.runtime.Composable -import org.jetbrains.jewel.LocalOnDarkBackground +import org.jetbrains.jewel.IntelliJTheme import org.jetbrains.jewel.intui.core.IntUiPainterHintsProvider import org.jetbrains.jewel.intui.core.IntUiThemeDefinition import org.jetbrains.jewel.painter.PainterHint @@ -31,7 +31,7 @@ class StandalonePainterHintsProvider( override fun hints(path: String): List = buildList { add(getPaletteHint(path)) add(overrideHint) - add(Dark(LocalOnDarkBackground.current)) + add(Dark(IntelliJTheme.isDark)) } companion object {