Skip to content

Commit

Permalink
ScrollbarStyle refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rock3r committed Oct 20, 2023
1 parent 57a8457 commit a7668d0
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,40 @@ import androidx.compose.runtime.Stable
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import org.jetbrains.jewel.GenerateDataFunctions
import kotlin.time.Duration

@Stable
interface ScrollbarStyle {

val colors: ScrollbarColors
val metrics: ScrollbarMetrics
val hoverDuration: Duration
@GenerateDataFunctions
class ScrollbarStyle(
val colors: ScrollbarColors,
val metrics: ScrollbarMetrics,
val hoverDuration: Duration,
) {

companion object
}

@Immutable
interface ScrollbarColors {
@GenerateDataFunctions
class ScrollbarColors(
val thumbBackground: Color,
val thumbBackgroundHovered: Color,
) {

val thumbBackground: Color
val thumbBackgroundHovered: Color
companion object
}

@Stable
interface ScrollbarMetrics {

val thumbCornerSize: CornerSize
val thumbThickness: Dp
val minThumbLength: Dp
val trackPadding: PaddingValues
@GenerateDataFunctions
class ScrollbarMetrics(
val thumbCornerSize: CornerSize,
val thumbThickness: Dp,
val minThumbLength: Dp,
val trackPadding: PaddingValues,
) {

companion object
}

val LocalScrollbarStyle = staticCompositionLocalOf<ScrollbarStyle> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ import org.jetbrains.jewel.intui.standalone.styling.IntUiLabelledTextFieldColors
import org.jetbrains.jewel.intui.standalone.styling.IntUiLabelledTextFieldMetrics
import org.jetbrains.jewel.intui.standalone.styling.IntUiLabelledTextFieldStyle
import org.jetbrains.jewel.intui.standalone.styling.IntUiLabelledTextFieldTextStyles
import org.jetbrains.jewel.intui.standalone.styling.IntUiScrollbarColors
import org.jetbrains.jewel.intui.standalone.styling.IntUiScrollbarMetrics
import org.jetbrains.jewel.intui.standalone.styling.IntUiScrollbarStyle
import org.jetbrains.jewel.intui.standalone.styling.IntUiTabColors
import org.jetbrains.jewel.intui.standalone.styling.IntUiTabContentAlpha
import org.jetbrains.jewel.intui.standalone.styling.IntUiTabIcons
Expand Down Expand Up @@ -91,6 +88,9 @@ import org.jetbrains.jewel.styling.RadioButtonColors
import org.jetbrains.jewel.styling.RadioButtonIcons
import org.jetbrains.jewel.styling.RadioButtonMetrics
import org.jetbrains.jewel.styling.RadioButtonStyle
import org.jetbrains.jewel.styling.ScrollbarColors
import org.jetbrains.jewel.styling.ScrollbarMetrics
import org.jetbrains.jewel.styling.ScrollbarStyle
import org.jetbrains.jewel.styling.SubmenuMetrics
import org.jetbrains.skiko.DependsOnJBR
import javax.swing.UIManager
Expand All @@ -101,6 +101,9 @@ private val logger = Logger.getInstance("JewelIntUiBridge")
internal val uiDefaults
get() = UIManager.getDefaults()

private val iconsBasePath
get() = DirProvider().dir()

@OptIn(DependsOnJBR::class)
internal suspend fun createBridgeIntUiDefinition(): IntUiThemeDefinition {
val textStyle = retrieveTextStyle("Label.font", "Label.foreground")
Expand Down Expand Up @@ -252,9 +255,6 @@ private fun readOutlinedButtonStyle(): ButtonStyle {
)
}

private val iconsBasePath
get() = DirProvider().dir()

private fun readCheckboxStyle(): CheckboxStyle {
val textColor = retrieveColorOrUnspecified("CheckBox.foreground")
val colors = CheckboxColors(
Expand Down Expand Up @@ -447,15 +447,16 @@ private fun readUndecoratedDropdownStyle(
)
}

private fun readGroupHeaderStyle() = GroupHeaderStyle(
colors = GroupHeaderColors(
divider = retrieveColorOrUnspecified("Separator.separatorColor"),
),
metrics = GroupHeaderMetrics(
dividerThickness = 1.dp, // see DarculaSeparatorUI
indent = 1.dp, // see DarculaSeparatorUI
),
)
private fun readGroupHeaderStyle() =
GroupHeaderStyle(
colors = GroupHeaderColors(
divider = retrieveColorOrUnspecified("Separator.separatorColor"),
),
metrics = GroupHeaderMetrics(
dividerThickness = 1.dp, // see DarculaSeparatorUI
indent = 1.dp, // see DarculaSeparatorUI
),
)

private fun readHorizontalProgressBarStyle() = HorizontalProgressBarStyle(
colors = HorizontalProgressBarColors(
Expand Down Expand Up @@ -644,23 +645,24 @@ private fun readRadioButtonStyle(): RadioButtonStyle {
)
}

private fun readScrollbarStyle(isDark: Boolean) = IntUiScrollbarStyle(
colors = IntUiScrollbarColors(
// See ScrollBarPainter.THUMB_OPAQUE_BACKGROUND
thumbBackground = retrieveColorOrUnspecified("ScrollBar.Mac.Transparent.thumbColor")
.takeOrElse { if (isDark) Color(0x59808080) else Color(0x33000000) },
// See ScrollBarPainter.THUMB_OPAQUE_HOVERED_BACKGROUND
thumbBackgroundHovered = retrieveColorOrUnspecified("ScrollBar.Mac.Transparent.hoverThumbColor")
.takeOrElse { if (isDark) Color(0x8C808080) else Color(0x80000000) },
),
metrics = IntUiScrollbarMetrics(
thumbCornerSize = CornerSize(100),
thumbThickness = 8.dp,
minThumbLength = 16.dp,
trackPadding = PaddingValues(start = 7.dp, end = 3.dp),
),
hoverDuration = 300.milliseconds,
)
private fun readScrollbarStyle(isDark: Boolean) =
ScrollbarStyle(
colors = ScrollbarColors(
// See ScrollBarPainter.THUMB_OPAQUE_BACKGROUND
thumbBackground = retrieveColorOrUnspecified("ScrollBar.Mac.Transparent.thumbColor")
.takeOrElse { if (isDark) Color(0x59808080) else Color(0x33000000) },
// See ScrollBarPainter.THUMB_OPAQUE_HOVERED_BACKGROUND
thumbBackgroundHovered = retrieveColorOrUnspecified("ScrollBar.Mac.Transparent.hoverThumbColor")
.takeOrElse { if (isDark) Color(0x8C808080) else Color(0x80000000) },
),
metrics = ScrollbarMetrics(
thumbCornerSize = CornerSize(100),
thumbThickness = 8.dp,
minThumbLength = 16.dp,
trackPadding = PaddingValues(start = 7.dp, end = 3.dp),
),
hoverDuration = 300.milliseconds,
)

private fun readTextAreaStyle(textStyle: TextStyle, metrics: IntUiTextFieldMetrics): IntUiTextAreaStyle {
val normalBackground = retrieveColorOrUnspecified("TextArea.background")
Expand Down Expand Up @@ -919,23 +921,24 @@ private fun readTooltipStyle() =
),
)

private fun readIconButtonStyle(): IconButtonStyle = IconButtonStyle(
metrics = IconButtonMetrics(
cornerSize = CornerSize(DarculaUIUtil.BUTTON_ARC.dp / 2),
borderWidth = 1.dp,
padding = PaddingValues(0.dp),
minSize = DpSize(16.dp, 16.dp),
),
colors = IconButtonColors(
background = Color.Unspecified,
backgroundDisabled = Color.Unspecified,
backgroundFocused = Color.Unspecified,
backgroundPressed = retrieveColorOrUnspecified("ActionButton.pressedBackground"),
backgroundHovered = retrieveColorOrUnspecified("ActionButton.hoverBackground"),
border = Color.Unspecified,
borderDisabled = Color.Unspecified,
borderFocused = retrieveColorOrUnspecified("ActionButton.focusedBorderColor"),
borderPressed = retrieveColorOrUnspecified("ActionButton.pressedBorderColor"),
borderHovered = retrieveColorOrUnspecified("ActionButton.hoverBorderColor"),
),
)
private fun readIconButtonStyle(): IconButtonStyle =
IconButtonStyle(
metrics = IconButtonMetrics(
cornerSize = CornerSize(DarculaUIUtil.BUTTON_ARC.dp / 2),
borderWidth = 1.dp,
padding = PaddingValues(0.dp),
minSize = DpSize(16.dp, 16.dp),
),
colors = IconButtonColors(
background = Color.Unspecified,
backgroundDisabled = Color.Unspecified,
backgroundFocused = Color.Unspecified,
backgroundPressed = retrieveColorOrUnspecified("ActionButton.pressedBackground"),
backgroundHovered = retrieveColorOrUnspecified("ActionButton.hoverBackground"),
border = Color.Unspecified,
borderDisabled = Color.Unspecified,
borderFocused = retrieveColorOrUnspecified("ActionButton.focusedBorderColor"),
borderPressed = retrieveColorOrUnspecified("ActionButton.pressedBorderColor"),
borderHovered = retrieveColorOrUnspecified("ActionButton.hoverBorderColor"),
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import org.jetbrains.jewel.intui.core.theme.IntUiLightTheme
import org.jetbrains.jewel.intui.standalone.IntUiTheme.defaultComponentStyling
import org.jetbrains.jewel.intui.standalone.styling.Default
import org.jetbrains.jewel.intui.standalone.styling.IntUiLabelledTextFieldStyle
import org.jetbrains.jewel.intui.standalone.styling.IntUiScrollbarStyle
import org.jetbrains.jewel.intui.standalone.styling.IntUiTabStyle
import org.jetbrains.jewel.intui.standalone.styling.IntUiTextAreaStyle
import org.jetbrains.jewel.intui.standalone.styling.IntUiTextFieldStyle
Expand Down Expand Up @@ -101,7 +100,7 @@ object IntUiTheme : BaseIntUiTheme {
menuStyle: MenuStyle = MenuStyle.dark(),
outlinedButtonStyle: ButtonStyle = ButtonStyle.Outlined.dark(),
radioButtonStyle: RadioButtonStyle = RadioButtonStyle.dark(),
scrollbarStyle: ScrollbarStyle = IntUiScrollbarStyle.dark(),
scrollbarStyle: ScrollbarStyle = ScrollbarStyle.dark(),
textAreaStyle: IntUiTextAreaStyle = IntUiTextAreaStyle.dark(),
textFieldStyle: TextFieldStyle = IntUiTextFieldStyle.dark(),
tooltipStyle: IntUiTooltipStyle = IntUiTooltipStyle.dark(),
Expand Down Expand Up @@ -150,7 +149,7 @@ object IntUiTheme : BaseIntUiTheme {
menuStyle: MenuStyle = MenuStyle.light(),
outlinedButtonStyle: ButtonStyle = ButtonStyle.Outlined.light(),
radioButtonStyle: RadioButtonStyle = RadioButtonStyle.light(),
scrollbarStyle: ScrollbarStyle = IntUiScrollbarStyle.light(),
scrollbarStyle: ScrollbarStyle = ScrollbarStyle.light(),
textAreaStyle: IntUiTextAreaStyle = IntUiTextAreaStyle.light(),
textFieldStyle: TextFieldStyle = IntUiTextFieldStyle.light(),
tooltipStyle: IntUiTooltipStyle = IntUiTooltipStyle.light(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,72 +3,44 @@ package org.jetbrains.jewel.intui.standalone.styling
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.shape.CornerSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import org.jetbrains.jewel.GenerateDataFunctions
import org.jetbrains.jewel.styling.ScrollbarColors
import org.jetbrains.jewel.styling.ScrollbarMetrics
import org.jetbrains.jewel.styling.ScrollbarStyle
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds

@Stable
@GenerateDataFunctions
class IntUiScrollbarStyle(
override val colors: IntUiScrollbarColors,
override val metrics: IntUiScrollbarMetrics,
override val hoverDuration: Duration,
) : ScrollbarStyle {

companion object {

@Composable
fun light(
colors: IntUiScrollbarColors = IntUiScrollbarColors.light(),
metrics: IntUiScrollbarMetrics = IntUiScrollbarMetrics(),
hoverDuration: Duration = 300.milliseconds,
) = IntUiScrollbarStyle(colors, metrics, hoverDuration)

@Composable
fun dark(
colors: IntUiScrollbarColors = IntUiScrollbarColors.dark(),
metrics: IntUiScrollbarMetrics = IntUiScrollbarMetrics(),
hoverDuration: Duration = 300.milliseconds,
) = IntUiScrollbarStyle(colors, metrics, hoverDuration)
}
}

@Immutable
@GenerateDataFunctions
class IntUiScrollbarColors(
override val thumbBackground: Color,
override val thumbBackgroundHovered: Color,
) : ScrollbarColors {

companion object {

@Composable
fun light(
thumbBackground: Color = Color(0x33000000),
thumbBackgroundHovered: Color = Color(0x80000000),
) = IntUiScrollbarColors(thumbBackground, thumbBackgroundHovered)

@Composable
fun dark(
thumbBackground: Color = Color(0x59808080),
thumbBackgroundHovered: Color = Color(0x8C808080),
) = IntUiScrollbarColors(thumbBackground, thumbBackgroundHovered)
}
}

@Stable
@GenerateDataFunctions
class IntUiScrollbarMetrics(
override val thumbCornerSize: CornerSize = CornerSize(100),
override val thumbThickness: Dp = 8.dp,
override val minThumbLength: Dp = 16.dp,
override val trackPadding: PaddingValues = PaddingValues(start = 7.dp, end = 3.dp),
) : ScrollbarMetrics
@Composable
fun ScrollbarStyle.Companion.light(
colors: ScrollbarColors = ScrollbarColors.light(),
metrics: ScrollbarMetrics = ScrollbarMetrics.defaults(),
hoverDuration: Duration = 300.milliseconds,
) = ScrollbarStyle(colors, metrics, hoverDuration)

@Composable
fun ScrollbarStyle.Companion.dark(
colors: ScrollbarColors = ScrollbarColors.dark(),
metrics: ScrollbarMetrics = ScrollbarMetrics.defaults(),
hoverDuration: Duration = 300.milliseconds,
) = ScrollbarStyle(colors, metrics, hoverDuration)

@Composable
fun ScrollbarColors.Companion.light(
thumbBackground: Color = Color(0x33000000),
thumbBackgroundHovered: Color = Color(0x80000000),
) = ScrollbarColors(thumbBackground, thumbBackgroundHovered)

@Composable
fun ScrollbarColors.Companion.dark(
thumbBackground: Color = Color(0x59808080),
thumbBackgroundHovered: Color = Color(0x8C808080),
) = ScrollbarColors(thumbBackground, thumbBackgroundHovered)

fun ScrollbarMetrics.Companion.defaults(
thumbCornerSize: CornerSize = CornerSize(100),
thumbThickness: Dp = 8.dp,
minThumbLength: Dp = 16.dp,
trackPadding: PaddingValues = PaddingValues(start = 7.dp, end = 3.dp),
) = ScrollbarMetrics(thumbCornerSize, thumbThickness, minThumbLength, trackPadding)

0 comments on commit a7668d0

Please sign in to comment.