Skip to content

Commit

Permalink
Remove OnBackground
Browse files Browse the repository at this point in the history
  • Loading branch information
devkanro committed Oct 17, 2023
1 parent 98b58b2 commit c53d7ce
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 39 deletions.
10 changes: 6 additions & 4 deletions core/src/main/kotlin/org/jetbrains/jewel/Checkbox.kt
Original file line number Diff line number Diff line change
Expand Up @@ -249,24 +249,26 @@ 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)
}
}
}

if (LocalSwingCompatMode.current) {
checkboxState = checkboxState.copy(hovered = false, pressed = false)
}

val wrapperModifier = modifier.triStateToggleable(
state = state,
onClick = onClick,
Expand Down
4 changes: 1 addition & 3 deletions core/src/main/kotlin/org/jetbrains/jewel/IconButton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ fun IconButton(
.border(style.metrics.borderWidth, border, shape),
contentAlignment = Alignment.Center,
content = {
onBackground(background) {
content(buttonState)
}
content(buttonState)
},
)
}
22 changes: 3 additions & 19 deletions core/src/main/kotlin/org/jetbrains/jewel/IntelliJTheme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -235,10 +233,6 @@ internal val LocalIsDarkTheme = staticCompositionLocalOf<Boolean> {
error("No InDarkTheme provided")
}

val LocalOnDarkBackground = staticCompositionLocalOf<Boolean> {
error("No OnDarkBackground provided")
}

internal val LocalSwingCompatMode = staticCompositionLocalOf {
// By default, Swing compat is not enabled
false
Expand All @@ -253,19 +247,9 @@ val LocalIconData = staticCompositionLocalOf<IntelliJThemeIconData> {
}

/**
* 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)
}
10 changes: 6 additions & 4 deletions core/src/main/kotlin/org/jetbrains/jewel/RadioButton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -132,24 +132,26 @@ 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)
}
}
}

if (LocalSwingCompatMode.current) {
radioButtonState = radioButtonState.copy(hovered = false, pressed = false)
}

val wrapperModifier = modifier.selectable(
selected = selected,
onClick = onClick,
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/kotlin/org/jetbrains/jewel/Tooltip.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -63,7 +64,7 @@ fun Tooltip(
)
.padding(style.metrics.contentPadding),
) {
onBackground(style.colors.background) {
OverrideDarkMode(style.colors.background.isDark()) {
tooltip()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand All @@ -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<PainterHint> = listOf(Dark(LocalOnDarkBackground.current))
override fun hints(path: String): List<PainterHint> = listOf(Dark(IntelliJTheme.isDark))
}

val LocalPainterHintsProvider = staticCompositionLocalOf<PainterHintsProvider> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -31,7 +31,7 @@ class StandalonePainterHintsProvider(
override fun hints(path: String): List<PainterHint> = buildList {
add(getPaletteHint(path))
add(overrideHint)
add(Dark(LocalOnDarkBackground.current))
add(Dark(IntelliJTheme.isDark))
}

companion object {
Expand Down

0 comments on commit c53d7ce

Please sign in to comment.