diff --git a/composeApp/src/jvmMain/kotlin/io/github/hayatoyagi/prvisualizer/ui/prlist/PrListPane.kt b/composeApp/src/jvmMain/kotlin/io/github/hayatoyagi/prvisualizer/ui/prlist/PrListPane.kt index f788c51..544c02e 100644 --- a/composeApp/src/jvmMain/kotlin/io/github/hayatoyagi/prvisualizer/ui/prlist/PrListPane.kt +++ b/composeApp/src/jvmMain/kotlin/io/github/hayatoyagi/prvisualizer/ui/prlist/PrListPane.kt @@ -35,6 +35,7 @@ import androidx.compose.ui.unit.dp import io.github.hayatoyagi.prvisualizer.PullRequest import io.github.hayatoyagi.prvisualizer.ui.shared.TooltipContainer import io.github.hayatoyagi.prvisualizer.ui.shared.TooltipIconButton +import io.github.hayatoyagi.prvisualizer.ui.shortcut.viewportResetShortcutHint import io.github.hayatoyagi.prvisualizer.ui.theme.AppColors import io.github.hayatoyagi.prvisualizer.ui.theme.prColor @@ -87,7 +88,7 @@ fun PrListPane( contentModifier = Modifier.weight(1f), ) Text( - text = "Cmd+R: reset view", + text = viewportResetShortcutHint(), color = AppColors.textHint, style = MaterialTheme.typography.bodySmall, ) diff --git a/composeApp/src/jvmMain/kotlin/io/github/hayatoyagi/prvisualizer/ui/shortcut/Shortcuts.kt b/composeApp/src/jvmMain/kotlin/io/github/hayatoyagi/prvisualizer/ui/shortcut/Shortcuts.kt index f4bcf7b..d097c6f 100644 --- a/composeApp/src/jvmMain/kotlin/io/github/hayatoyagi/prvisualizer/ui/shortcut/Shortcuts.kt +++ b/composeApp/src/jvmMain/kotlin/io/github/hayatoyagi/prvisualizer/ui/shortcut/Shortcuts.kt @@ -6,6 +6,7 @@ import io.github.hayatoyagi.prvisualizer.VisualizerViewModel import java.awt.KeyEventDispatcher import java.awt.KeyboardFocusManager import java.awt.Toolkit +import java.awt.event.InputEvent import java.awt.event.KeyEvent @Composable @@ -17,7 +18,7 @@ fun RegisterShortcuts(vm: VisualizerViewModel) { private fun RegisterResetViewportShortcut(vm: VisualizerViewModel) { DisposableEffect(vm) { val focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager() - val shortcutMask = Toolkit.getDefaultToolkit().menuShortcutKeyMaskEx + val shortcutMask = defaultShortcutMask() val dispatcher = KeyEventDispatcher { event -> if (!event.isViewportResetShortcut(shortcutMask)) return@KeyEventDispatcher false if (!focusManager.hasActiveWindow()) return@KeyEventDispatcher false @@ -34,6 +35,9 @@ private fun RegisterResetViewportShortcut(vm: VisualizerViewModel) { private fun KeyEvent.isViewportResetShortcut(shortcutMask: Int): Boolean = isViewportResetShortcut(id, keyCode, modifiersEx, shortcutMask) +internal fun viewportResetShortcutHint(shortcutMask: Int = defaultShortcutMask()): String = + "${shortcutModifierLabel(shortcutMask)}+R: reset view" + internal fun isViewportResetShortcut(id: Int, keyCode: Int, modifiersEx: Int, shortcutMask: Int): Boolean { if (id != KeyEvent.KEY_PRESSED) return false if (keyCode != KeyEvent.VK_R) return false @@ -41,4 +45,9 @@ internal fun isViewportResetShortcut(id: Int, keyCode: Int, modifiersEx: Int, sh return true } +private fun defaultShortcutMask(): Int = Toolkit.getDefaultToolkit().menuShortcutKeyMaskEx + +private fun shortcutModifierLabel(shortcutMask: Int): String = + if (shortcutMask and InputEvent.META_DOWN_MASK != 0) "Cmd" else "Ctrl" + private fun KeyboardFocusManager.hasActiveWindow(): Boolean = activeWindow != null diff --git a/composeApp/src/jvmTest/kotlin/io/github/hayatoyagi/prvisualizer/ui/shortcut/ShortcutsTest.kt b/composeApp/src/jvmTest/kotlin/io/github/hayatoyagi/prvisualizer/ui/shortcut/ShortcutsTest.kt index 3d870ad..6b7cc92 100644 --- a/composeApp/src/jvmTest/kotlin/io/github/hayatoyagi/prvisualizer/ui/shortcut/ShortcutsTest.kt +++ b/composeApp/src/jvmTest/kotlin/io/github/hayatoyagi/prvisualizer/ui/shortcut/ShortcutsTest.kt @@ -3,6 +3,7 @@ package io.github.hayatoyagi.prvisualizer.ui.shortcut import java.awt.event.InputEvent import java.awt.event.KeyEvent import kotlin.test.Test +import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue @@ -78,4 +79,14 @@ class ShortcutsTest { ), ) } + + @Test + fun `viewport reset hint uses Ctrl label on Windows`() { + assertEquals("Ctrl+R: reset view", viewportResetShortcutHint(InputEvent.CTRL_DOWN_MASK)) + } + + @Test + fun `viewport reset hint uses Cmd label on macOS`() { + assertEquals("Cmd+R: reset view", viewportResetShortcutHint(InputEvent.META_DOWN_MASK)) + } }