Skip to content

Commit

Permalink
Clean up painter provider usage
Browse files Browse the repository at this point in the history
  • Loading branch information
rock3r committed Sep 12, 2023
1 parent d1619bf commit 0258458
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ open class ResourcePainterProvider<T> @InternalJewelApi constructor(
override fun toString(): String =
"ResourcePainterProvider(basePath='$basePath', svgLoader=$svgLoader, pathPatcher=$pathPatcher)"

companion object {
@OptIn(InternalJewelApi::class) // These are the public constructors
companion object Factory {

fun stateless(basePath: String, svgLoader: SvgLoader) =
ResourcePainterProvider<Unit>(basePath, svgLoader, SimpleResourcePathPatcher())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.jetbrains.jewel.bridge

import androidx.compose.runtime.Composable
import androidx.compose.ui.res.ResourceLoader
import org.jetbrains.jewel.IntelliJThemeIconData
import org.jetbrains.jewel.InteractiveComponentState
import org.jetbrains.jewel.InternalJewelApi
import org.jetbrains.jewel.SvgLoader
Expand All @@ -11,34 +12,46 @@ import org.jetbrains.jewel.styling.SimpleResourcePathPatcher
import org.jetbrains.jewel.styling.StatefulResourcePathPatcher

@OptIn(InternalJewelApi::class)
class IntelliJResourcePainterProvider<T> @InternalJewelApi constructor(
internal class BridgeResourcePainterProvider<T> @InternalJewelApi constructor(
basePath: String,
svgLoader: SvgLoader,
pathPatcher: ResourcePathPatcher<T>,
private val iconMapper: IconMapper,
private val iconData: IntelliJThemeIconData,
) : ResourcePainterProvider<T>(basePath, svgLoader, pathPatcher) {

@Composable
override fun patchPath(basePath: String, resourceLoader: ResourceLoader, extraData: T?): String {
override fun patchPath(
basePath: String,
resourceLoader: ResourceLoader,
extraData: T?,
): String {
val patchedPath = super.patchPath(basePath, resourceLoader, extraData)
return iconMapper.mapPath(patchedPath, resourceLoader)
return iconMapper.mapPath(patchedPath, iconData, resourceLoader)
}

companion object {
companion object Factory {

fun stateless(basePath: String, svgLoader: SvgLoader) =
IntelliJResourcePainterProvider<Unit>(basePath, svgLoader, SimpleResourcePathPatcher(), BridgeIconMapper)
fun stateless(basePath: String, svgLoader: SvgLoader, iconData: IntelliJThemeIconData) =
BridgeResourcePainterProvider<Unit>(
basePath,
svgLoader,
SimpleResourcePathPatcher(),
BridgeIconMapper,
iconData,
)

fun <T : InteractiveComponentState> stateful(
basePath: String,
iconPath: String,
svgLoader: SvgLoader,
iconData: IntelliJThemeIconData,
prefixTokensProvider: (state: T) -> String = { "" },
suffixTokensProvider: (state: T) -> String = { "" },
pathPatcher: ResourcePathPatcher<T> = StatefulResourcePathPatcher(
prefixTokensProvider,
suffixTokensProvider,
),
) =
IntelliJResourcePainterProvider(basePath, svgLoader, pathPatcher, BridgeIconMapper)
BridgeResourcePainterProvider(iconPath, svgLoader, pathPatcher, BridgeIconMapper, iconData)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,24 @@ internal operator fun TextUnit.plus(delta: Float) =
else -> this
}

internal fun <T : InteractiveComponentState> retrieveIcon(
baseIconPath: String,
iconData: IntelliJThemeIconData,
internal fun <T : InteractiveComponentState> retrieveStatefulIcon(
iconPath: String,
svgLoader: SvgLoader,
iconData: IntelliJThemeIconData,
prefixTokensProvider: (state: T) -> String = { "" },
suffixTokensProvider: (state: T) -> String = { "" },
): PainterProvider<T> =
IntelliJResourcePainterProvider.stateful(
basePath = iconData.iconOverrides[baseIconPath] ?: baseIconPath,
BridgeResourcePainterProvider.stateful(
iconPath = iconPath,
svgLoader,
iconData,
prefixTokensProvider,
suffixTokensProvider,
)

internal fun retrieveStatelessIcon(
iconPath: String,
svgLoader: SvgLoader,
iconData: IntelliJThemeIconData,
): PainterProvider<Unit> =
BridgeResourcePainterProvider.stateless(iconPath, svgLoader, iconData)
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,10 @@ private fun readCheckboxStyle(iconData: IntelliJThemeIconData, svgLoader: SvgLoa
iconContentGap = 5.dp, // See DarculaCheckBoxUI#textIconGap
),
icons = IntUiCheckboxIcons(
checkbox = retrieveIcon(
baseIconPath = "${iconsBasePath}checkBox.svg",
iconData = iconData,
checkbox = retrieveStatefulIcon(
iconPath = "${iconsBasePath}checkBox.svg",
svgLoader = svgLoader,
iconData = iconData,
prefixTokensProvider = { state: CheckboxState ->
if (state.toggleableState == ToggleableState.Indeterminate) "Indeterminate" else ""
},
Expand Down Expand Up @@ -381,8 +381,8 @@ private fun readDropdownStyle(
borderWidth = DarculaUIUtil.BW.dp,
),
icons = IntUiDropdownIcons(
chevronDown = retrieveIcon(
baseIconPath = "${iconsBasePath}general/chevron-down.svg",
chevronDown = retrieveStatefulIcon(
iconPath = "${iconsBasePath}general/chevron-down.svg",
iconData = iconData,
svgLoader = svgLoader,
),
Expand Down Expand Up @@ -497,13 +497,13 @@ private fun readLinkStyle(
iconSize = DpSize.Unspecified,
),
icons = IntUiLinkIcons(
dropdownChevron = retrieveIcon(
baseIconPath = "${iconsBasePath}general/chevron-down.svg",
dropdownChevron = retrieveStatefulIcon(
iconPath = "${iconsBasePath}general/chevron-down.svg",
iconData = iconData,
svgLoader = svgLoader,
),
externalLink = retrieveIcon(
baseIconPath = "${iconsBasePath}ide/external_link_arrow.svg",
externalLink = retrieveStatefulIcon(
iconPath = "${iconsBasePath}ide/external_link_arrow.svg",
iconData = iconData,
svgLoader = svgLoader,
),
Expand Down Expand Up @@ -571,8 +571,8 @@ private fun readMenuStyle(iconData: IntelliJThemeIconData, svgLoader: SvgLoader)
),
),
icons = IntUiMenuIcons(
submenuChevron = retrieveIcon(
baseIconPath = "${iconsBasePath}general/chevron-down.svg",
submenuChevron = retrieveStatefulIcon(
iconPath = "${iconsBasePath}general/chevron-down.svg",
iconData = iconData,
svgLoader = svgLoader,
),
Expand All @@ -599,8 +599,8 @@ private fun readRadioButtonStyle(iconData: IntelliJThemeIconData, svgLoader: Svg
iconContentGap = retrieveIntAsDpOrUnspecified("RadioButton.textIconGap").takeOrElse { 4.dp },
),
icons = IntUiRadioButtonIcons(
radioButton = retrieveIcon(
baseIconPath = "${iconsBasePath}radio.svg",
radioButton = retrieveStatefulIcon(
iconPath = "${iconsBasePath}radio.svg",
iconData = iconData,
svgLoader = svgLoader,
),
Expand Down Expand Up @@ -740,13 +740,13 @@ private fun readLazyTreeStyle(iconData: IntelliJThemeIconData, svgLoader: SvgLoa
chevronContentGap = 2.dp, // See com.intellij.ui.tree.ui.ClassicPainter.GAP
),
icons = IntUiLazyTreeIcons(
nodeChevronCollapsed = retrieveIcon(
baseIconPath = "${iconsBasePath}general/chevron-right.svg",
nodeChevronCollapsed = retrieveStatefulIcon(
iconPath = "${iconsBasePath}general/chevron-right.svg",
iconData = iconData,
svgLoader = svgLoader,
),
nodeChevronExpanded = retrieveIcon(
baseIconPath = "${iconsBasePath}general/chevron-down.svg",
nodeChevronExpanded = retrieveStatefulIcon(
iconPath = "${iconsBasePath}general/chevron-down.svg",
iconData = iconData,
svgLoader = svgLoader,
),
Expand Down Expand Up @@ -791,8 +791,8 @@ private fun readDefaultTabStyle(iconData: IntelliJThemeIconData, svgLoader: SvgL
tabHeight = retrieveIntAsDpOrUnspecified("TabbedPane.tabHeight").takeOrElse { 24.dp },
),
icons = IntUiTabIcons(
close = retrieveIcon(
baseIconPath = "${iconsBasePath}expui/general/closeSmall.svg",
close = retrieveStatefulIcon(
iconPath = "${iconsBasePath}expui/general/closeSmall.svg",
iconData = iconData,
svgLoader = svgLoader,
),
Expand Down Expand Up @@ -850,8 +850,8 @@ private fun readEditorTabStyle(iconData: IntelliJThemeIconData, svgLoader: SvgLo
tabHeight = retrieveIntAsDpOrUnspecified("TabbedPane.tabHeight").takeOrElse { 24.dp },
),
icons = IntUiTabIcons(
close = retrieveIcon(
baseIconPath = "${iconsBasePath}expui/general/closeSmall.svg",
close = retrieveStatefulIcon(
iconPath = "${iconsBasePath}expui/general/closeSmall.svg",
iconData = iconData,
svgLoader = svgLoader,
),
Expand Down

0 comments on commit 0258458

Please sign in to comment.