Skip to content

Commit

Permalink
Iterate on keyboard shortcuts (#549)
Browse files Browse the repository at this point in the history
* add keyboardShortcut to ViewInfo

Signed-off-by: Ivan Morgillo <[email protected]>

* make keybindings UI fairly multiplatform

Signed-off-by: Ivan Morgillo <[email protected]>

* iterate on KeyBinding.forCurrentOs()

reference: #549 (comment)
Signed-off-by: Ivan Morgillo <[email protected]>

---------

Signed-off-by: Ivan Morgillo <[email protected]>
  • Loading branch information
hamen authored Aug 19, 2024
1 parent 6ade536 commit 37bd706
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.compose.ui.unit.dp
import org.jetbrains.jewel.samples.standalone.IntUiThemes
import org.jetbrains.jewel.samples.standalone.StandaloneSampleIcons
import org.jetbrains.jewel.samples.standalone.viewmodel.MainViewModel
import org.jetbrains.jewel.samples.standalone.viewmodel.forCurrentOs
import org.jetbrains.jewel.ui.component.Dropdown
import org.jetbrains.jewel.ui.component.Icon
import org.jetbrains.jewel.ui.component.IconButton
Expand All @@ -32,9 +33,8 @@ fun DecoratedWindowScope.TitleBarView() {
MainViewModel.views.forEach {
selectableItem(
selected = MainViewModel.currentView == it,
onClick = {
MainViewModel.currentView = it
},
onClick = { MainViewModel.currentView = it },
keybinding = it.keyboardShortcut?.forCurrentOs(),
) {
Row(
horizontalArrangement = Arrangement.spacedBy(4.dp),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,4 @@ fun Dropdowns() {
}

private val dropdownIconsSample = listOf(AllIconsKeys.Actions.Find, AllIconsKeys.Actions.Close, null)
private val dropdownKeybindingsSample = setOf('A', 'B', '', '', '')
private val dropdownKeybindingsSample = setOf("A", "B", "", "", "")
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,19 @@ private val mainMenuItems =
ViewInfo(
title = "Welcome",
iconKey = StandaloneSampleIcons.welcome,
keyboardShortcut = KeyBinding(macOs = setOf("", "W"), windows = setOf("Alt", "W")),
content = { WelcomeView() },
),
ViewInfo(
title = "Components",
iconKey = StandaloneSampleIcons.componentsMenu,
keyboardShortcut = KeyBinding(macOs = setOf("", "C"), windows = setOf("Alt", "C")),
content = { ComponentsView() },
),
ViewInfo(
title = "Markdown",
iconKey = StandaloneSampleIcons.markdown,
keyboardShortcut = KeyBinding(macOs = setOf("", "M"), windows = setOf("Alt", "M"), linux = setOf("Alt", "M")),
content = { MarkdownDemo() },
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@ package org.jetbrains.jewel.samples.standalone.viewmodel

import androidx.compose.runtime.Composable
import org.jetbrains.jewel.ui.icon.IconKey
import org.jetbrains.skiko.hostOs

data class KeyBinding(
val macOs: Set<String> = emptySet(),
val windows: Set<String> = emptySet(),
val linux: Set<String> = emptySet(),
)

fun KeyBinding.forCurrentOs(): Set<String> =
when {
hostOs.isMacOS -> macOs
hostOs.isLinux -> linux
else -> windows
}

data class ViewInfo(
val title: String,
val iconKey: IconKey,
val keyboardShortcut: KeyBinding? = null,
val content: @Composable () -> Unit,
)
15 changes: 10 additions & 5 deletions ui/src/main/kotlin/org/jetbrains/jewel/ui/component/Menu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import org.jetbrains.jewel.ui.component.styling.MenuStyle
import org.jetbrains.jewel.ui.icon.IconKey
import org.jetbrains.jewel.ui.painter.hints.Stateful
import org.jetbrains.jewel.ui.theme.menuStyle
import org.jetbrains.skiko.hostOs

@Composable
public fun PopupMenu(
Expand Down Expand Up @@ -222,7 +223,7 @@ public interface MenuScope {
selected: Boolean,
iconKey: IconKey? = null,
iconClass: Class<*>? = iconKey?.let { it::class.java },
keybinding: Set<Char>? = null,
keybinding: Set<String>? = null,
onClick: () -> Unit,
enabled: Boolean = true,
content: @Composable () -> Unit,
Expand Down Expand Up @@ -275,7 +276,7 @@ private fun (MenuScope.() -> Unit).asList() =
selected: Boolean,
iconKey: IconKey?,
iconClass: Class<*>?,
keybinding: Set<Char>?,
keybinding: Set<String>?,
onClick: () -> Unit,
enabled: Boolean,
content: @Composable () -> Unit,
Expand Down Expand Up @@ -319,7 +320,7 @@ private data class MenuSelectableItem(
val isEnabled: Boolean,
val iconKey: IconKey?,
val iconClass: Class<*>?,
val keybinding: Set<Char>?,
val keybinding: Set<String>?,
val onClick: () -> Unit = {},
override val content: @Composable () -> Unit,
) : MenuItem
Expand Down Expand Up @@ -360,7 +361,7 @@ internal fun MenuItem(
enabled: Boolean = true,
iconKey: IconKey?,
iconClass: Class<*>?,
keybinding: Set<Char>?,
keybinding: Set<String>?,
canShowIcon: Boolean,
canShowKeybinding: Boolean,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
Expand Down Expand Up @@ -459,7 +460,11 @@ internal fun MenuItem(
if (canShowKeybinding) {
val keybindingText =
remember(keybinding) {
keybinding?.joinToString("") { it.toString() }.orEmpty()
if(hostOs.isMacOS) {
keybinding?.joinToString(" ") { it }.orEmpty()
} else {
keybinding?.joinToString(" + ") { it }.orEmpty()
}
}
Text(
modifier = Modifier.padding(style.metrics.itemMetrics.keybindingsPadding),
Expand Down

0 comments on commit 37bd706

Please sign in to comment.