Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -34,11 +35,19 @@ 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
if (modifiersEx and shortcutMask == 0) return false
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
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
}
}