From 02584585e8be462af0a233e7d27f9cb708294c94 Mon Sep 17 00:00:00 2001 From: Sebastiano Poggi Date: Tue, 12 Sep 2023 12:47:53 +0200 Subject: [PATCH] Clean up painter provider usage --- .../jewel/styling/ResourcePainterProvider.kt | 3 +- ...er.kt => BridgeResourcePainterProvider.kt} | 29 +++++++++---- .../org/jetbrains/jewel/bridge/BridgeUtils.kt | 18 +++++--- .../org/jetbrains/jewel/bridge/IntUiBridge.kt | 42 +++++++++---------- 4 files changed, 57 insertions(+), 35 deletions(-) rename ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/{IntelliJResourcePainterProvider.kt => BridgeResourcePainterProvider.kt} (57%) diff --git a/core/src/main/kotlin/org/jetbrains/jewel/styling/ResourcePainterProvider.kt b/core/src/main/kotlin/org/jetbrains/jewel/styling/ResourcePainterProvider.kt index 276fb7a439..60c8e720ca 100644 --- a/core/src/main/kotlin/org/jetbrains/jewel/styling/ResourcePainterProvider.kt +++ b/core/src/main/kotlin/org/jetbrains/jewel/styling/ResourcePainterProvider.kt @@ -62,7 +62,8 @@ open class ResourcePainterProvider @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(basePath, svgLoader, SimpleResourcePathPatcher()) diff --git a/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/IntelliJResourcePainterProvider.kt b/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/BridgeResourcePainterProvider.kt similarity index 57% rename from ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/IntelliJResourcePainterProvider.kt rename to ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/BridgeResourcePainterProvider.kt index ecfd3fe6be..82c271645d 100644 --- a/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/IntelliJResourcePainterProvider.kt +++ b/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/BridgeResourcePainterProvider.kt @@ -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 @@ -11,27 +12,39 @@ import org.jetbrains.jewel.styling.SimpleResourcePathPatcher import org.jetbrains.jewel.styling.StatefulResourcePathPatcher @OptIn(InternalJewelApi::class) -class IntelliJResourcePainterProvider @InternalJewelApi constructor( +internal class BridgeResourcePainterProvider @InternalJewelApi constructor( basePath: String, svgLoader: SvgLoader, pathPatcher: ResourcePathPatcher, private val iconMapper: IconMapper, + private val iconData: IntelliJThemeIconData, ) : ResourcePainterProvider(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(basePath, svgLoader, SimpleResourcePathPatcher(), BridgeIconMapper) + fun stateless(basePath: String, svgLoader: SvgLoader, iconData: IntelliJThemeIconData) = + BridgeResourcePainterProvider( + basePath, + svgLoader, + SimpleResourcePathPatcher(), + BridgeIconMapper, + iconData, + ) fun stateful( - basePath: String, + iconPath: String, svgLoader: SvgLoader, + iconData: IntelliJThemeIconData, prefixTokensProvider: (state: T) -> String = { "" }, suffixTokensProvider: (state: T) -> String = { "" }, pathPatcher: ResourcePathPatcher = StatefulResourcePathPatcher( @@ -39,6 +52,6 @@ class IntelliJResourcePainterProvider @InternalJewelApi constructor( suffixTokensProvider, ), ) = - IntelliJResourcePainterProvider(basePath, svgLoader, pathPatcher, BridgeIconMapper) + BridgeResourcePainterProvider(iconPath, svgLoader, pathPatcher, BridgeIconMapper, iconData) } } diff --git a/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/BridgeUtils.kt b/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/BridgeUtils.kt index e6ee327640..9938d35a69 100644 --- a/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/BridgeUtils.kt +++ b/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/BridgeUtils.kt @@ -155,16 +155,24 @@ internal operator fun TextUnit.plus(delta: Float) = else -> this } -internal fun retrieveIcon( - baseIconPath: String, - iconData: IntelliJThemeIconData, +internal fun retrieveStatefulIcon( + iconPath: String, svgLoader: SvgLoader, + iconData: IntelliJThemeIconData, prefixTokensProvider: (state: T) -> String = { "" }, suffixTokensProvider: (state: T) -> String = { "" }, ): PainterProvider = - 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 = + BridgeResourcePainterProvider.stateless(iconPath, svgLoader, iconData) diff --git a/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/IntUiBridge.kt b/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/IntUiBridge.kt index a20528102e..f9f1389a25 100644 --- a/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/IntUiBridge.kt +++ b/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/IntUiBridge.kt @@ -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 "" }, @@ -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, ), @@ -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, ), @@ -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, ), @@ -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, ), @@ -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, ), @@ -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, ), @@ -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, ),