Skip to content

Commit

Permalink
Remove one more usage of resource-based Icon APIs (#578)
Browse files Browse the repository at this point in the history
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...
  • Loading branch information
rock3r authored Sep 5, 2024
1 parent 8e12870 commit ff87cca
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -89,7 +88,6 @@ fun Dropdowns() {
} else {
selectableItem(
iconKey = dropdownIconsSample.random(),
iconClass = DropdownStyle::class.java,
keybinding =
if (Random.nextBoolean()) {
null
Expand All @@ -111,7 +109,6 @@ fun Dropdowns() {
} else {
selectableItem(
iconKey = dropdownIconsSample.random(),
iconClass = DropdownStyle::class.java,
keybinding =
if (Random.nextBoolean()) {
null
Expand All @@ -134,7 +131,6 @@ fun Dropdowns() {
} else {
selectableItem(
iconKey = dropdownIconsSample.random(),
iconClass = DropdownStyle::class.java,
selected = false,
onClick = {},
) {
Expand Down
9 changes: 5 additions & 4 deletions ui/api/ui.api
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
55 changes: 32 additions & 23 deletions ui/src/main/kotlin/org/jetbrains/jewel/ui/component/Menu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
Expand All @@ -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,
)

Expand All @@ -210,7 +209,6 @@ public interface MenuScope {
public fun selectableItem(
selected: Boolean,
iconKey: IconKey? = null,
iconClass: Class<*>? = iconKey?.let { it::class.java },
keybinding: Set<String>? = null,
onClick: () -> Unit,
enabled: Boolean = true,
Expand All @@ -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,
)
Expand Down Expand Up @@ -258,7 +255,6 @@ private fun (MenuScope.() -> Unit).asList() = buildList {
override fun selectableItem(
selected: Boolean,
iconKey: IconKey?,
iconClass: Class<*>?,
keybinding: Set<String>?,
onClick: () -> Unit,
enabled: Boolean,
Expand All @@ -269,7 +265,6 @@ private fun (MenuScope.() -> Unit).asList() = buildList {
isSelected = selected,
isEnabled = enabled,
iconKey = iconKey,
iconClass = iconClass,
keybinding = keybinding,
onClick = onClick,
content = content,
Expand All @@ -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))
}
}
)
Expand All @@ -302,7 +296,6 @@ private data class MenuSelectableItem(
val isSelected: Boolean,
val isEnabled: Boolean,
val iconKey: IconKey?,
val iconClass: Class<*>?,
val keybinding: Set<String>?,
val onClick: () -> Unit = {},
override val content: @Composable () -> Unit,
Expand All @@ -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
Expand Down Expand Up @@ -341,7 +333,6 @@ internal fun MenuItem(
modifier: Modifier = Modifier,
enabled: Boolean = true,
iconKey: IconKey?,
iconClass: Class<*>?,
keybinding: Set<String>?,
canShowIcon: Boolean,
canShowKeybinding: Boolean,
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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,
Expand All @@ -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)) }
Expand Down Expand Up @@ -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))
}
Expand Down

0 comments on commit ff87cca

Please sign in to comment.