From ff87ccad066f6a0afe06a888e1299b3b53ccef87 Mon Sep 17 00:00:00 2001 From: Sebastiano Poggi Date: Thu, 5 Sep 2024 13:14:34 +0200 Subject: [PATCH] Remove one more usage of resource-based Icon APIs (#578) In this case, they were used in the Menus API. Not sure why find usages did not show these when I did the work for #576... --- .../standalone/view/component/Dropdowns.kt | 4 -- ui/api/ui.api | 9 +-- .../org/jetbrains/jewel/ui/component/Menu.kt | 55 +++++++++++-------- 3 files changed, 37 insertions(+), 31 deletions(-) diff --git a/samples/standalone/src/main/kotlin/org/jetbrains/jewel/samples/standalone/view/component/Dropdowns.kt b/samples/standalone/src/main/kotlin/org/jetbrains/jewel/samples/standalone/view/component/Dropdowns.kt index 81c73be1e..1f562c580 100644 --- a/samples/standalone/src/main/kotlin/org/jetbrains/jewel/samples/standalone/view/component/Dropdowns.kt +++ b/samples/standalone/src/main/kotlin/org/jetbrains/jewel/samples/standalone/view/component/Dropdowns.kt @@ -14,7 +14,6 @@ import org.jetbrains.jewel.ui.Outline import org.jetbrains.jewel.ui.component.Dropdown import org.jetbrains.jewel.ui.component.Text import org.jetbrains.jewel.ui.component.separator -import org.jetbrains.jewel.ui.component.styling.DropdownStyle import org.jetbrains.jewel.ui.icons.AllIconsKeys @Composable @@ -89,7 +88,6 @@ fun Dropdowns() { } else { selectableItem( iconKey = dropdownIconsSample.random(), - iconClass = DropdownStyle::class.java, keybinding = if (Random.nextBoolean()) { null @@ -111,7 +109,6 @@ fun Dropdowns() { } else { selectableItem( iconKey = dropdownIconsSample.random(), - iconClass = DropdownStyle::class.java, keybinding = if (Random.nextBoolean()) { null @@ -134,7 +131,6 @@ fun Dropdowns() { } else { selectableItem( iconKey = dropdownIconsSample.random(), - iconClass = DropdownStyle::class.java, selected = false, onClick = {}, ) { diff --git a/ui/api/ui.api b/ui/api/ui.api index 8b063f572..8712f3052 100644 --- a/ui/api/ui.api +++ b/ui/api/ui.api @@ -497,6 +497,7 @@ public final class org/jetbrains/jewel/ui/component/MenuItemState$Companion { public final class org/jetbrains/jewel/ui/component/MenuKt { public static final fun MenuSeparator (Landroidx/compose/ui/Modifier;Lorg/jetbrains/jewel/ui/component/styling/MenuItemMetrics;Lorg/jetbrains/jewel/ui/component/styling/MenuItemColors;Landroidx/compose/runtime/Composer;II)V public static final fun MenuSubmenuItem (Landroidx/compose/ui/Modifier;ZZLjava/lang/String;Ljava/lang/Class;Landroidx/compose/foundation/interaction/MutableInteractionSource;Lorg/jetbrains/jewel/ui/component/styling/MenuStyle;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V + public static final fun MenuSubmenuItem (Landroidx/compose/ui/Modifier;ZZLorg/jetbrains/jewel/ui/icon/IconKey;Landroidx/compose/foundation/interaction/MutableInteractionSource;Lorg/jetbrains/jewel/ui/component/styling/MenuStyle;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V public static final fun PopupMenu (Lkotlin/jvm/functions/Function1;Landroidx/compose/ui/Alignment$Horizontal;Landroidx/compose/ui/Modifier;Lorg/jetbrains/jewel/ui/component/styling/MenuStyle;Landroidx/compose/ui/window/PopupProperties;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)V public static final fun items (Lorg/jetbrains/jewel/ui/component/MenuScope;ILkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;)V public static final fun items (Lorg/jetbrains/jewel/ui/component/MenuScope;Ljava/util/List;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;)V @@ -521,13 +522,13 @@ public final class org/jetbrains/jewel/ui/component/MenuManagerKt { public abstract interface class org/jetbrains/jewel/ui/component/MenuScope { public abstract fun passiveItem (Lkotlin/jvm/functions/Function2;)V - public abstract fun selectableItem (ZLorg/jetbrains/jewel/ui/icon/IconKey;Ljava/lang/Class;Ljava/util/Set;Lkotlin/jvm/functions/Function0;ZLkotlin/jvm/functions/Function2;)V - public abstract fun submenu (ZLjava/lang/String;Ljava/lang/Class;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)V + public abstract fun selectableItem (ZLorg/jetbrains/jewel/ui/icon/IconKey;Ljava/util/Set;Lkotlin/jvm/functions/Function0;ZLkotlin/jvm/functions/Function2;)V + public abstract fun submenu (ZLorg/jetbrains/jewel/ui/icon/IconKey;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)V } public final class org/jetbrains/jewel/ui/component/MenuScope$DefaultImpls { - public static synthetic fun selectableItem$default (Lorg/jetbrains/jewel/ui/component/MenuScope;ZLorg/jetbrains/jewel/ui/icon/IconKey;Ljava/lang/Class;Ljava/util/Set;Lkotlin/jvm/functions/Function0;ZLkotlin/jvm/functions/Function2;ILjava/lang/Object;)V - public static synthetic fun submenu$default (Lorg/jetbrains/jewel/ui/component/MenuScope;ZLjava/lang/String;Ljava/lang/Class;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)V + public static synthetic fun selectableItem$default (Lorg/jetbrains/jewel/ui/component/MenuScope;ZLorg/jetbrains/jewel/ui/icon/IconKey;Ljava/util/Set;Lkotlin/jvm/functions/Function0;ZLkotlin/jvm/functions/Function2;ILjava/lang/Object;)V + public static synthetic fun submenu$default (Lorg/jetbrains/jewel/ui/component/MenuScope;ZLorg/jetbrains/jewel/ui/icon/IconKey;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)V } public final class org/jetbrains/jewel/ui/component/PlatformIconKt { diff --git a/ui/src/main/kotlin/org/jetbrains/jewel/ui/component/Menu.kt b/ui/src/main/kotlin/org/jetbrains/jewel/ui/component/Menu.kt index 4317e9040..c76b78a70 100644 --- a/ui/src/main/kotlin/org/jetbrains/jewel/ui/component/Menu.kt +++ b/ui/src/main/kotlin/org/jetbrains/jewel/ui/component/Menu.kt @@ -82,6 +82,7 @@ import org.jetbrains.jewel.ui.component.styling.MenuItemColors import org.jetbrains.jewel.ui.component.styling.MenuItemMetrics import org.jetbrains.jewel.ui.component.styling.MenuStyle import org.jetbrains.jewel.ui.icon.IconKey +import org.jetbrains.jewel.ui.icon.PathIconKey import org.jetbrains.jewel.ui.painter.hints.Stateful import org.jetbrains.jewel.ui.theme.menuStyle import org.jetbrains.skiko.hostOs @@ -187,7 +188,6 @@ private fun ShowMenuItem(item: MenuItem, canShowIcon: Boolean = false, canShowKe canShowIcon = canShowIcon, canShowKeybinding = canShowKeybinding, iconKey = item.iconKey, - iconClass = item.iconClass, keybinding = item.keybinding, content = item.content, ) @@ -197,8 +197,7 @@ private fun ShowMenuItem(item: MenuItem, canShowIcon: Boolean = false, canShowKe enabled = item.isEnabled, submenu = item.submenu, canShowIcon = canShowIcon, - iconResource = item.iconResource, - iconClass = item.iconClass, + iconKey = item.iconKey, content = item.content, ) @@ -210,7 +209,6 @@ public interface MenuScope { public fun selectableItem( selected: Boolean, iconKey: IconKey? = null, - iconClass: Class<*>? = iconKey?.let { it::class.java }, keybinding: Set? = null, onClick: () -> Unit, enabled: Boolean = true, @@ -219,8 +217,7 @@ public interface MenuScope { public fun submenu( enabled: Boolean = true, - iconResource: String? = null, - iconClass: Class<*> = this::class.java, + iconKey: IconKey? = null, submenu: MenuScope.() -> Unit, content: @Composable () -> Unit, ) @@ -258,7 +255,6 @@ private fun (MenuScope.() -> Unit).asList() = buildList { override fun selectableItem( selected: Boolean, iconKey: IconKey?, - iconClass: Class<*>?, keybinding: Set?, onClick: () -> Unit, enabled: Boolean, @@ -269,7 +265,6 @@ private fun (MenuScope.() -> Unit).asList() = buildList { isSelected = selected, isEnabled = enabled, iconKey = iconKey, - iconClass = iconClass, keybinding = keybinding, onClick = onClick, content = content, @@ -283,12 +278,11 @@ private fun (MenuScope.() -> Unit).asList() = buildList { override fun submenu( enabled: Boolean, - iconResource: String?, - iconClass: Class<*>, + iconKey: IconKey?, submenu: MenuScope.() -> Unit, content: @Composable () -> Unit, ) { - add(SubmenuItem(enabled, iconResource, iconClass, submenu, content)) + add(SubmenuItem(enabled, iconKey, submenu, content)) } } ) @@ -302,7 +296,6 @@ private data class MenuSelectableItem( val isSelected: Boolean, val isEnabled: Boolean, val iconKey: IconKey?, - val iconClass: Class<*>?, val keybinding: Set?, val onClick: () -> Unit = {}, override val content: @Composable () -> Unit, @@ -312,8 +305,7 @@ private data class MenuPassiveItem(override val content: @Composable () -> Unit) private data class SubmenuItem( val isEnabled: Boolean = true, - val iconResource: String?, - val iconClass: Class<*>, + val iconKey: IconKey?, val submenu: MenuScope.() -> Unit, override val content: @Composable () -> Unit, ) : MenuItem @@ -341,7 +333,6 @@ internal fun MenuItem( modifier: Modifier = Modifier, enabled: Boolean = true, iconKey: IconKey?, - iconClass: Class<*>?, keybinding: Set?, canShowIcon: Boolean, canShowKeybinding: Boolean, @@ -421,12 +412,7 @@ internal fun MenuItem( if (canShowIcon) { val iconModifier = Modifier.size(style.metrics.itemMetrics.iconSize) if (iconKey != null) { - Icon( - key = iconKey, - contentDescription = null, - iconClass = iconClass ?: iconKey.javaClass, - modifier = iconModifier, - ) + Icon(key = iconKey, contentDescription = null, modifier = iconModifier) } else { Box(modifier = iconModifier) } @@ -454,6 +440,14 @@ internal fun MenuItem( } } +@Deprecated( + "Use the IconKey variant", + ReplaceWith( + "MenuSubmenuItem(modifier, enabled, canShowIcon, iconResource?.let { PathIconKey(it, iconClass) }, " + + "interactionSource, style, submenu, content)", + "org/jetbrains/jewel/ui/component/Menu.kt:472", + ), +) @Composable public fun MenuSubmenuItem( modifier: Modifier = Modifier, @@ -465,6 +459,21 @@ public fun MenuSubmenuItem( style: MenuStyle = JewelTheme.menuStyle, submenu: MenuScope.() -> Unit, content: @Composable () -> Unit, +) { + val iconKey = remember(iconResource, iconClass) { iconResource?.let { PathIconKey(it, iconClass) } } + MenuSubmenuItem(modifier, enabled, canShowIcon, iconKey, interactionSource, style, submenu, content) +} + +@Composable +public fun MenuSubmenuItem( + modifier: Modifier = Modifier, + enabled: Boolean = true, + canShowIcon: Boolean, + iconKey: IconKey?, + interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, + style: MenuStyle = JewelTheme.menuStyle, + submenu: MenuScope.() -> Unit, + content: @Composable () -> Unit, ) { var itemState by remember(interactionSource) { mutableStateOf(MenuItemState.of(selected = false, enabled = enabled)) } @@ -525,8 +534,8 @@ public fun MenuSubmenuItem( horizontalArrangement = Arrangement.spacedBy(4.dp), ) { if (canShowIcon) { - if (iconResource != null) { - Icon(resource = iconResource, iconClass = iconClass, contentDescription = "") + if (iconKey != null) { + Icon(key = iconKey, contentDescription = null) } else { Box(Modifier.size(style.metrics.itemMetrics.iconSize)) }