Skip to content

Commit

Permalink
The big refactor — part 2 (#193)
Browse files Browse the repository at this point in the history
* ButtonStyle refactor

* CheckboxStyle refactor

* ChipStyle refactor

* CircularProgressStyle refactor

* DividerStyle refactor

* DropdownStyle refactor

* GroupHeaderStyle refactor, simplify other styling

* HorizontalProgressBarStyle refactor

* IconButtonStyle refactor

* LazyTreeStyle refactor

* LinkStyle refactor

* MenuStyle refactor

* RadioButtonStyle refactor

* ScrollbarStyle refactor

* Fix IntUiButtonStyling

* TabStyle refactor

* InputFieldStyle & co refactor

* TooltipStyle refactor

* Update API dump

* Fix dropdown menu and fallback rule (#194)

* Fix dropdown menu and fallback rule

* Fix tooltip

* Fix tooltip

* 🔀 Apply suggestions from code review

Co-authored-by: Sebastiano Poggi <[email protected]>

* 🔀 Apply suggestions from code review

---------

Co-authored-by: Sebastiano Poggi <[email protected]>

* Fix issues

---------

Co-authored-by: Kanro <[email protected]>
  • Loading branch information
rock3r and devkanro authored Oct 20, 2023
1 parent 1b863e8 commit 3b59123
Show file tree
Hide file tree
Showing 55 changed files with 4,470 additions and 5,497 deletions.
1,538 changes: 1,081 additions & 457 deletions core/api/core.api

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion core/src/main/kotlin/org/jetbrains/jewel/Chip.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,16 @@ private fun ChipImpl(
val colors = style.colors
val borderColor by colors.borderFor(chipState)

val borderWidth = if (chipState.isSelected) {
style.metrics.borderWidthSelected
} else {
style.metrics.borderWidth
}

Row(
modifier = modifier
.background(colors.backgroundFor(chipState).value, shape)
.border(Stroke.Alignment.Center, style.metrics.borderWidth, borderColor, shape)
.border(Stroke.Alignment.Center, borderWidth, borderColor, shape)
.focusOutline(chipState, shape)
.padding(style.metrics.padding),
verticalAlignment = Alignment.CenterVertically,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import org.jetbrains.jewel.styling.LocalCheckboxStyle
import org.jetbrains.jewel.styling.LocalChipStyle
import org.jetbrains.jewel.styling.LocalCircularProgressStyle
import org.jetbrains.jewel.styling.LocalDefaultButtonStyle
import org.jetbrains.jewel.styling.LocalDefaultDropdownStyle
import org.jetbrains.jewel.styling.LocalDefaultTabStyle
import org.jetbrains.jewel.styling.LocalDividerStyle
import org.jetbrains.jewel.styling.LocalDropdownStyle
import org.jetbrains.jewel.styling.LocalEditorTabStyle
import org.jetbrains.jewel.styling.LocalGroupHeaderStyle
import org.jetbrains.jewel.styling.LocalHorizontalProgressBarStyle
Expand All @@ -37,6 +37,7 @@ import org.jetbrains.jewel.styling.LocalScrollbarStyle
import org.jetbrains.jewel.styling.LocalTextAreaStyle
import org.jetbrains.jewel.styling.LocalTextFieldStyle
import org.jetbrains.jewel.styling.LocalTooltipStyle
import org.jetbrains.jewel.styling.LocalUndecoratedDropdownStyle
import org.jetbrains.jewel.styling.MenuStyle
import org.jetbrains.jewel.styling.RadioButtonStyle
import org.jetbrains.jewel.styling.ScrollbarStyle
Expand All @@ -46,16 +47,19 @@ import org.jetbrains.jewel.styling.TextFieldStyle
import org.jetbrains.jewel.styling.TooltipStyle

@Stable
@GenerateDataFunctions
class IntelliJComponentStyling(
val checkboxStyle: CheckboxStyle,
val chipStyle: ChipStyle,
val circularProgressStyle: CircularProgressStyle,
val defaultButtonStyle: ButtonStyle,
val defaultDropdownStyle: DropdownStyle,
val defaultTabStyle: TabStyle,
val dividerStyle: DividerStyle,
val dropdownStyle: DropdownStyle,
val editorTabStyle: TabStyle,
val groupHeaderStyle: GroupHeaderStyle,
val horizontalProgressBarStyle: HorizontalProgressBarStyle,
val iconButtonStyle: IconButtonStyle,
val labelledTextFieldStyle: LabelledTextFieldStyle,
val lazyTreeStyle: LazyTreeStyle,
val linkStyle: LinkStyle,
Expand All @@ -65,21 +69,24 @@ class IntelliJComponentStyling(
val scrollbarStyle: ScrollbarStyle,
val textAreaStyle: TextAreaStyle,
val textFieldStyle: TextFieldStyle,
val circularProgressStyle: CircularProgressStyle,
val tooltipStyle: TooltipStyle,
val iconButtonStyle: IconButtonStyle,
val undecoratedDropdownStyle: DropdownStyle,
) {

@Composable
fun providedStyles(): Array<ProvidedValue<*>> = arrayOf(
LocalCheckboxStyle provides checkboxStyle,
LocalChipStyle provides chipStyle,
LocalCircularProgressStyle provides circularProgressStyle,
LocalContextMenuRepresentation provides IntelliJContextMenuRepresentation,
LocalDefaultButtonStyle provides defaultButtonStyle,
LocalDefaultDropdownStyle provides defaultDropdownStyle,
LocalDefaultTabStyle provides defaultTabStyle,
LocalDividerStyle provides dividerStyle,
LocalDropdownStyle provides dropdownStyle,
LocalEditorTabStyle provides editorTabStyle,
LocalGroupHeaderStyle provides groupHeaderStyle,
LocalHorizontalProgressBarStyle provides horizontalProgressBarStyle,
LocalIconButtonStyle provides iconButtonStyle,
LocalLabelledTextFieldStyle provides labelledTextFieldStyle,
LocalLazyTreeStyle provides lazyTreeStyle,
LocalLinkStyle provides linkStyle,
Expand All @@ -89,76 +96,7 @@ class IntelliJComponentStyling(
LocalScrollbarStyle provides scrollbarStyle,
LocalTextAreaStyle provides textAreaStyle,
LocalTextFieldStyle provides textFieldStyle,
LocalDefaultTabStyle provides defaultTabStyle,
LocalEditorTabStyle provides editorTabStyle,
LocalCircularProgressStyle provides circularProgressStyle,
LocalTooltipStyle provides tooltipStyle,
LocalIconButtonStyle provides iconButtonStyle,
LocalUndecoratedDropdownStyle provides undecoratedDropdownStyle,
)

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as IntelliJComponentStyling

if (checkboxStyle != other.checkboxStyle) return false
if (chipStyle != other.chipStyle) return false
if (defaultButtonStyle != other.defaultButtonStyle) return false
if (defaultTabStyle != other.defaultTabStyle) return false
if (dividerStyle != other.dividerStyle) return false
if (dropdownStyle != other.dropdownStyle) return false
if (editorTabStyle != other.editorTabStyle) return false
if (groupHeaderStyle != other.groupHeaderStyle) return false
if (horizontalProgressBarStyle != other.horizontalProgressBarStyle) return false
if (labelledTextFieldStyle != other.labelledTextFieldStyle) return false
if (lazyTreeStyle != other.lazyTreeStyle) return false
if (linkStyle != other.linkStyle) return false
if (menuStyle != other.menuStyle) return false
if (outlinedButtonStyle != other.outlinedButtonStyle) return false
if (radioButtonStyle != other.radioButtonStyle) return false
if (scrollbarStyle != other.scrollbarStyle) return false
if (textAreaStyle != other.textAreaStyle) return false
if (textFieldStyle != other.textFieldStyle) return false
if (circularProgressStyle != other.circularProgressStyle) return false
if (tooltipStyle != other.tooltipStyle) return false
if (iconButtonStyle != other.iconButtonStyle) return false

return true
}

override fun hashCode(): Int {
var result = checkboxStyle.hashCode()
result = 31 * result + chipStyle.hashCode()
result = 31 * result + defaultButtonStyle.hashCode()
result = 31 * result + defaultTabStyle.hashCode()
result = 31 * result + dividerStyle.hashCode()
result = 31 * result + dropdownStyle.hashCode()
result = 31 * result + editorTabStyle.hashCode()
result = 31 * result + groupHeaderStyle.hashCode()
result = 31 * result + horizontalProgressBarStyle.hashCode()
result = 31 * result + labelledTextFieldStyle.hashCode()
result = 31 * result + lazyTreeStyle.hashCode()
result = 31 * result + linkStyle.hashCode()
result = 31 * result + menuStyle.hashCode()
result = 31 * result + outlinedButtonStyle.hashCode()
result = 31 * result + radioButtonStyle.hashCode()
result = 31 * result + scrollbarStyle.hashCode()
result = 31 * result + textAreaStyle.hashCode()
result = 31 * result + textFieldStyle.hashCode()
result = 31 * result + circularProgressStyle.hashCode()
result = 31 * result + tooltipStyle.hashCode()
result = 31 * result + iconButtonStyle.hashCode()
return result
}

override fun toString() =
"IntelliJComponentStyling(checkboxStyle=$checkboxStyle, chipStyle=$chipStyle, " +
"defaultButtonStyle=$defaultButtonStyle, defaultTabStyle=$defaultTabStyle, " +
"dividerStyle=$dividerStyle, dropdownStyle=$dropdownStyle, editorTabStyle=$editorTabStyle, " +
"groupHeaderStyle=$groupHeaderStyle, horizontalProgressBarStyle=$horizontalProgressBarStyle, " +
"labelledTextFieldStyle=$labelledTextFieldStyle, lazyTreeStyle=$lazyTreeStyle, linkStyle=$linkStyle, " +
"menuStyle=$menuStyle, outlinedButtonStyle=$outlinedButtonStyle, radioButtonStyle=$radioButtonStyle, " +
"scrollbarStyle=$scrollbarStyle, textAreaStyle=$textAreaStyle, textFieldStyle=$textFieldStyle, " +
"circularProgressStyle=$circularProgressStyle, tooltipStyle=$tooltipStyle, iconButtonStyle=$iconButtonStyle)"
}
4 changes: 2 additions & 2 deletions core/src/main/kotlin/org/jetbrains/jewel/IntelliJTheme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import org.jetbrains.jewel.styling.LocalCheckboxStyle
import org.jetbrains.jewel.styling.LocalChipStyle
import org.jetbrains.jewel.styling.LocalCircularProgressStyle
import org.jetbrains.jewel.styling.LocalDefaultButtonStyle
import org.jetbrains.jewel.styling.LocalDefaultDropdownStyle
import org.jetbrains.jewel.styling.LocalDefaultTabStyle
import org.jetbrains.jewel.styling.LocalDividerStyle
import org.jetbrains.jewel.styling.LocalDropdownStyle
import org.jetbrains.jewel.styling.LocalEditorTabStyle
import org.jetbrains.jewel.styling.LocalGroupHeaderStyle
import org.jetbrains.jewel.styling.LocalHorizontalProgressBarStyle
Expand Down Expand Up @@ -127,7 +127,7 @@ interface IntelliJTheme {
val dropdownStyle: DropdownStyle
@Composable
@ReadOnlyComposable
get() = LocalDropdownStyle.current
get() = LocalDefaultDropdownStyle.current

val groupHeaderStyle: GroupHeaderStyle
@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ fun LabelledTextField(
keyboardOptions = keyboardOptions,
keyboardActions = keyboardActions,
onTextLayout = onTextLayout,
style = style,
style = style.asTextFieldStyle(),
textStyle = textStyle,
interactionSource = interactionSource,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ import org.jetbrains.jewel.painter.hints.HiDpi
@Immutable
interface PainterHintsProvider {

/**
* These hints will be consumed before the hints which are passed as a parameter to [PainterProvider.getPainter].
*/
@Composable
fun priorityHints(path: String): List<PainterHint> = emptyList()

/**
* These hints will be consumed after the hints which are passed as a parameter to [PainterProvider.getPainter].
*/
@Composable
fun hints(path: String): List<PainterHint>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ class ResourcePainterProvider(

@Composable
override fun getPainter(vararg hints: PainterHint): State<Painter> {
val resolvedHints = (hints.toList() + LocalPainterHintsProvider.current.hints(basePath))
.filter { it.canApplyTo(basePath) }
val currentHintsProvider = LocalPainterHintsProvider.current
val providedHints =
currentHintsProvider.priorityHints(basePath) + hints.toList() + currentHintsProvider.hints(basePath)
val resolvedHints = providedHints.filter { it.canApplyTo(basePath) }

val cacheKey = resolvedHints.hashCode()

Expand All @@ -81,22 +83,22 @@ class ResourcePainterProvider(

@Composable
private fun loadPainter(hints: List<PainterHint>): Painter {
val pathStack = buildSet {
var path = basePath

add(path)

for (hint in hints) {
path = when (hint) {
is PainterResourcePathHint -> hint.patch(path, contextClassLoaders)
is PainterPathHint -> hint.patch(path)
else -> continue
var paths = setOf(basePath)

for (hint in hints) {
if (hint !is PainterResourcePathHint && hint !is PainterPathHint) continue
paths = paths.flatMap {
val patched = when (hint) {
is PainterResourcePathHint -> hint.patch(it, contextClassLoaders)
is PainterPathHint -> hint.patch(it)
else -> return@flatMap listOf(it)
}
add(path)
}
}.reversed()

val url = pathStack.firstNotNullOfOrNull { resolveResource(it) }
setOf(patched, it)
}.toSet()
}

val url = paths.firstNotNullOfOrNull { resolveResource(it) }

@Suppress("UrlHashCode") // It's ok when comparing a URL to null
if (url == null) {
Expand Down
62 changes: 35 additions & 27 deletions core/src/main/kotlin/org/jetbrains/jewel/styling/ButtonStyling.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,37 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.DpSize
import org.jetbrains.jewel.ButtonState
import org.jetbrains.jewel.GenerateDataFunctions

@Stable
interface ButtonStyle {
@GenerateDataFunctions
class ButtonStyle(
val colors: ButtonColors,
val metrics: ButtonMetrics,
) {

val colors: ButtonColors
val metrics: ButtonMetrics
companion object
}

@Immutable
interface ButtonColors {

val background: Brush
val backgroundDisabled: Brush
val backgroundFocused: Brush
val backgroundPressed: Brush
val backgroundHovered: Brush
@GenerateDataFunctions
class ButtonColors(
val background: Brush,
val backgroundDisabled: Brush,
val backgroundFocused: Brush,
val backgroundPressed: Brush,
val backgroundHovered: Brush,
val content: Color,
val contentDisabled: Color,
val contentFocused: Color,
val contentPressed: Color,
val contentHovered: Color,
val border: Brush,
val borderDisabled: Brush,
val borderFocused: Brush,
val borderPressed: Brush,
val borderHovered: Brush,
) {

@Composable
fun backgroundFor(state: ButtonState) = rememberUpdatedState(
Expand All @@ -41,12 +56,6 @@ interface ButtonColors {
),
)

val content: Color
val contentDisabled: Color
val contentFocused: Color
val contentPressed: Color
val contentHovered: Color

@Composable
fun contentFor(state: ButtonState) = rememberUpdatedState(
state.chooseValue(
Expand All @@ -59,12 +68,6 @@ interface ButtonColors {
),
)

val border: Brush
val borderDisabled: Brush
val borderFocused: Brush
val borderPressed: Brush
val borderHovered: Brush

@Composable
fun borderFor(state: ButtonState) = rememberUpdatedState(
state.chooseValue(
Expand All @@ -76,15 +79,20 @@ interface ButtonColors {
active = border,
),
)

companion object
}

@Stable
interface ButtonMetrics {
@GenerateDataFunctions
class ButtonMetrics(
val cornerSize: CornerSize,
val padding: PaddingValues,
val minSize: DpSize,
val borderWidth: Dp,
) {

val cornerSize: CornerSize
val padding: PaddingValues
val minSize: DpSize
val borderWidth: Dp
companion object
}

val LocalDefaultButtonStyle = staticCompositionLocalOf<ButtonStyle> {
Expand Down
Loading

0 comments on commit 3b59123

Please sign in to comment.