Skip to content

Build your personal knowledge base with Trilium Notes

client/media player & image viewer: do not block the global keyboard shortcuts that overlap with viewer/player's local ones

Adorian Doran73cb2bd71d1a
diff --git a/apps/client/src/services/shortcuts.ts b/apps/client/src/services/shortcuts.ts
index d0284cc175..165168581d 100644
--- a/apps/client/src/services/shortcuts.ts
+++ b/apps/client/src/services/shortcuts.ts
@@ -55,6 +55,21 @@ export const KEYCODES_WITH_NO_MODIFIER = new Set([
...functionKeyCodes
]);
+/**
+ * Whether a keystroke carries an application-level modifier, and so belongs to Trilium's own shortcuts
+ * rather than to whichever widget holds focus. A widget that binds bare keys has to stand aside for these:
+ * global shortcuts are bound on `document` in the bubble phase and don't check `defaultPrevented`, so a
+ * widget acting on a modified key either swallows the app shortcut (if it stops propagation) or fires
+ * alongside it. Shift is deliberately not one of them — it modifies a widget's own key (the image viewer
+ * pans faster with it held) and no shortcut is Shift-only.
+ *
+ * A widget may still claim a specific chord where the modifier *is* its gesture (Ctrl+= zooms the focused
+ * image rather than the whole UI); those are exceptions the widget states for itself.
+ */
+export function isAppShortcutChord(e: { ctrlKey: boolean; metaKey: boolean; altKey: boolean }): boolean {
+ return e.ctrlKey || e.metaKey || e.altKey;
+}
+
/**
* Check if IME (Input Method Editor) is composing
* This is used to prevent keyboard shortcuts from firing during IME composition
diff --git a/apps/client/src/widgets/react/image_viewer_keyboard.spec.ts b/apps/client/src/widgets/react/image_viewer_keyboard.spec.ts
index bdbf0736b3..0ae424d271 100644
--- a/apps/client/src/widgets/react/image_viewer_keyboard.spec.ts
+++ b/apps/client/src/widgets/react/image_viewer_keyboard.spec.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
-import { clampPan, codeToControl, getPanDelta, zoomToPointPosition } from "./image_viewer_keyboard";
+import { claimsKeystroke, clampPan, codeToControl, getPanDelta, zoomToPointPosition } from "./image_viewer_keyboard";
describe("codeToControl", () => {
it("maps zoom/reset keys (Equal/Minus/Slash, numpad, Q/E) regardless of modifiers", () => {
@@ -31,6 +31,34 @@ describe("codeToControl", () => {
});
});
+describe("claimsKeystroke", () => {
+ const keystroke = (code: string, modifiers: Partial<{ ctrlKey: boolean; metaKey: boolean; altKey: boolean }> = {}) =>
+ ({ code, ctrlKey: false, metaKey: false, altKey: false, ...modifiers });
+
+ it("takes bare keys, which are the viewer's own", () => {
+ expect(claimsKeystroke(keystroke("KeyW"))).toBe(true);
+ expect(claimsKeystroke(keystroke("ArrowLeft"))).toBe(true);
+ expect(claimsKeystroke(keystroke("Equal"))).toBe(true);
+ });
+
+ it("leaves chords to the application, so Ctrl+W still closes the tab", () => {
+ expect(claimsKeystroke(keystroke("KeyW", { ctrlKey: true }))).toBe(false);
+ expect(claimsKeystroke(keystroke("ArrowUp", { ctrlKey: true }))).toBe(false);
+ expect(claimsKeystroke(keystroke("Slash", { altKey: true }))).toBe(false);
+ // The letter aliases for zoom are ordinary letters as far as the app is concerned (Ctrl+Q quits).
+ expect(claimsKeystroke(keystroke("KeyE", { ctrlKey: true }))).toBe(false);
+ expect(claimsKeystroke(keystroke("KeyQ", { ctrlKey: true }))).toBe(false);
+ });
+
+ it("keeps the zoom gesture, where the modifier is the point", () => {
+ // Looking at an image, Ctrl+= zooms the image rather than the whole UI.
+ expect(claimsKeystroke(keystroke("Equal", { ctrlKey: true }))).toBe(true);
+ expect(claimsKeystroke(keystroke("Minus", { metaKey: true }))).toBe(true);
+ expect(claimsKeystroke(keystroke("NumpadAdd", { ctrlKey: true }))).toBe(true);
+ expect(claimsKeystroke(keystroke("NumpadSubtract", { ctrlKey: true }))).toBe(true);
+ });
+});
+
describe("getPanDelta", () => {
it("translates the content opposite the viewed direction", () => {
expect(getPanDelta([ "panRight" ], false, 1).dx).toBeLessThan(0);
diff --git a/apps/client/src/widgets/react/image_viewer_keyboard.ts b/apps/client/src/widgets/react/image_viewer_keyboard.ts
index 676ee8030a..660699f47d 100644
--- a/apps/client/src/widgets/react/image_viewer_keyboard.ts
+++ b/apps/client/src/widgets/react/image_viewer_keyboard.ts
@@ -1,6 +1,8 @@
import { useEffect } from "preact/hooks";
import type { ReactZoomPanPinchRef } from "react-zoom-pan-pinch";
+import { isAppShortcutChord } from "../../services/shortcuts";
+
export type ImageViewerControl =
| "zoomIn" | "zoomOut" | "reset"
| "panUp" | "panDown" | "panLeft" | "panRight";
@@ -13,8 +15,25 @@ const PAN_SPEED = 1200;
const PAN_FAST_FACTOR = 2.5;
/**
- * Maps a physical key (`KeyboardEvent.code`) to a viewer control, independent of modifiers — so the
- * zoom/reset keys work with or without Ctrl/Cmd. Using `code` keeps it keyboard-layout independent.
+ * The keys the browser and the OS zoom with, which the viewer claims even with Ctrl/Cmd held: while an
+ * image is focused, Ctrl+= should zoom the image rather than the whole UI. Their letter aliases (E/Q) are
+ * deliberately absent — Ctrl+E and Ctrl+Q belong to the application (Ctrl+Q quits, on Linux).
+ */
+const ZOOM_GESTURE_CODES = new Set([ "Equal", "Minus", "NumpadAdd", "NumpadSubtract" ]);
+
+/**
+ * Whether the viewer acts on this keystroke, or leaves it to Trilium's own shortcuts. Bare keys (and
+ * Shift, which pans faster) are the viewer's; a chord is the application's — otherwise Ctrl+W would pan up
+ * instead of closing the tab, since the viewer stops the event from ever reaching the global handler.
+ */
+export function claimsKeystroke(e: { code: string; ctrlKey: boolean; metaKey: boolean; altKey: boolean }): boolean {
+ return !isAppShortcutChord(e) || ZOOM_GESTURE_CODES.has(e.code);
+}
+
+/**
+ * Maps a physical key (`KeyboardEvent.code`) to a viewer control, independent of modifiers — whether the
+ * viewer may act on a modified one is {@link claimsKeystroke}'s call. Using `code` keeps it
+ * keyboard-layout independent.
*/
export function codeToControl(code: string): ImageViewerControl | null {
switch (code) {
@@ -141,7 +160,7 @@ export function useImageViewerKeyboard(
const onKeyDown = (e: KeyboardEvent) => {
shift = e.shiftKey;
const control = codeToControl(e.code);
- if (!control) return;
+ if (!control || !claimsKeystroke(e)) return;
// Claim the key so the browser (Ctrl +/-) and Trilium's global shortcuts
// (arrow tree navigation, app zoom, quick search) don't also act on it.
e.preventDefault();
diff --git a/apps/client/src/widgets/type_widgets/file/Audio.spec.tsx b/apps/client/src/widgets/type_widgets/file/Audio.spec.tsx
index f73750013c..fdd29baa6d 100644
--- a/apps/client/src/widgets/type_widgets/file/Audio.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/file/Audio.spec.tsx
@@ -128,6 +128,24 @@ describe("AudioPreview", () => {
expect(audio.currentTime).toBe(20);
expect(audio.play).not.toHaveBeenCalled();
});
+
+ it("stands aside for the application's own chords", async () => {
+ const audio = renderPlayer();
+ setDuration(audio, 100);
+ audio.currentTime = 20;
+
+ await press(" ", { ctrlKey: true });
+ await press("m", { ctrlKey: true });
+ await press("End", { metaKey: true });
+
+ expect(audio.play).not.toHaveBeenCalled();
+ expect(audio.muted).toBe(false);
+ expect(audio.currentTime).toBe(20);
+
+ // Its own Ctrl+Left/Right minute jump still belongs to the player.
+ await press("ArrowRight", { ctrlKey: true });
+ expect(audio.currentTime).toBe(80);
+ });
});
describe("waveform", () => {
diff --git a/apps/client/src/widgets/type_widgets/file/Audio.tsx b/apps/client/src/widgets/type_widgets/file/Audio.tsx
index b233599419..ca52004c04 100644
--- a/apps/client/src/widgets/type_widgets/file/Audio.tsx
+++ b/apps/client/src/widgets/type_widgets/file/Audio.tsx
@@ -9,7 +9,7 @@ import { loadWaveform } from "./audio_waveform";
import { AudioVisualizer } from "./AudioVisualizer";
import MediaFileActions from "./MediaFileActions";
import { playerRootClasses, preloadFor, showsFileActions, usesCompactControls } from "./media_environment";
-import { MediaPlayerProps, MediaSiblingButton, PlaybackSpeed, PlayModeButton, PlayPauseButton, SkipButton, useMediaPlayerShortcutHints, useMediaPlayMode, useMediaSessionController, VolumeControl } from "./MediaPlayer";
+import { claimsKeystroke, MediaPlayerProps, MediaSiblingButton, PlaybackSpeed, PlayModeButton, PlayPauseButton, SkipButton, useMediaPlayerShortcutHints, useMediaPlayMode, useMediaSessionController, VolumeControl } from "./MediaPlayer";
import { WaveformSeekBar } from "./WaveformSeekBar";
export default function AudioPreview({ source, entity, environment, noteContext, ownerNote, viewScope, isVisible = true, autoPlay }: MediaPlayerProps) {
@@ -131,7 +131,7 @@ function useWaveformPeaks(fullUrl: string): number[] | null {
function useKeyboardShortcuts(audioRef: MutableRef<HTMLAudioElement | null>, togglePlayback: () => void) {
return useCallback((e: KeyboardEvent) => {
const audio = audioRef.current;
- if (!audio) return;
+ if (!audio || !claimsKeystroke(e)) return;
switch (e.key) {
case " ":
diff --git a/apps/client/src/widgets/type_widgets/file/MediaPlayer.spec.tsx b/apps/client/src/widgets/type_widgets/file/MediaPlayer.spec.tsx
index baee99c39d..acc29c2da6 100644
--- a/apps/client/src/widgets/type_widgets/file/MediaPlayer.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/file/MediaPlayer.spec.tsx
@@ -25,7 +25,7 @@ import Component from "../../../components/component";
import type FNote from "../../../entities/fnote";
import { collectShortcutHints } from "../../../services/shortcut_hints";
import { ParentComponent } from "../../react/react_utils";
-import { formatTime, PlaybackSpeed, PlayModeButton, PlayPauseButton, SeekBar, SkipButton, useMediaPlayerShortcutHints, useMediaPlayMode, useMediaSessionController, VolumeControl } from "./MediaPlayer";
+import { claimsKeystroke, formatTime, PlaybackSpeed, PlayModeButton, PlayPauseButton, SeekBar, SkipButton, useMediaPlayerShortcutHints, useMediaPlayMode, useMediaSessionController, VolumeControl } from "./MediaPlayer";
import type { MediaPlayMode } from "./media_play_mode";
import type { MediaSource } from "./media_source";
@@ -115,6 +115,35 @@ describe("useMediaPlayerShortcutHints", () => {
});
});
+describe("claimsKeystroke", () => {
+ const keystroke = (key: string, modifiers: Partial<{ ctrlKey: boolean; metaKey: boolean; altKey: boolean; shiftKey: boolean }> = {}) =>
+ ({ key, ctrlKey: false, metaKey: false, altKey: false, shiftKey: false, ...modifiers }) as KeyboardEvent;
+
+ it("takes the bare keys the player binds", () => {
+ expect(claimsKeystroke(keystroke(" "))).toBe(true);
+ expect(claimsKeystroke(keystroke("f"))).toBe(true);
+ expect(claimsKeystroke(keystroke("Home"))).toBe(true);
+ // Shift isn't an application modifier, so it doesn't take the key away.
+ expect(claimsKeystroke(keystroke("m", { shiftKey: true }))).toBe(true);
+ });
+
+ it("leaves chords to the application, which owns Ctrl+F and the rest", () => {
+ expect(claimsKeystroke(keystroke("f", { ctrlKey: true }))).toBe(false);
+ expect(claimsKeystroke(keystroke("m", { ctrlKey: true }))).toBe(false);
+ expect(claimsKeystroke(keystroke(" ", { ctrlKey: true }))).toBe(false);
+ expect(claimsKeystroke(keystroke("Home", { metaKey: true }))).toBe(false);
+ expect(claimsKeystroke(keystroke("ArrowUp", { altKey: true }))).toBe(false);
+ });
+
+ it("keeps its own Ctrl+Left/Right minute jump", () => {
+ expect(claimsKeystroke(keystroke("ArrowLeft", { ctrlKey: true }))).toBe(true);
+ expect(claimsKeystroke(keystroke("ArrowRight", { ctrlKey: true }))).toBe(true);
+ // Only with Ctrl alone: Alt+Left is the application's (history back), not a minute jump.
+ expect(claimsKeystroke(keystroke("ArrowLeft", { altKey: true }))).toBe(false);
+ expect(claimsKeystroke(keystroke("ArrowRight", { ctrlKey: true, altKey: true }))).toBe(false);
+ });
+});
+
describe("formatTime", () => {
it("renders m:ss, zero-padding the seconds and flooring the remainder", () => {
expect(formatTime(0)).toBe("0:00");
diff --git a/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx b/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx
index e1ed41e9fa..1a1088b08f 100644
--- a/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx
+++ b/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx
@@ -11,6 +11,7 @@ import froca from "../../../services/froca";
import { t } from "../../../services/i18n";
import type { ViewScope } from "../../../services/link";
import type { ShortcutHint, ShortcutHintDefinition, ShortcutHintSection } from "../../../services/shortcut_hints";
+import { isAppShortcutChord } from "../../../services/shortcuts";
import { isMobile } from "../../../services/utils";
import { logError } from "../../../services/ws";
import ActionButton from "../../react/ActionButton";
@@ -81,6 +82,18 @@ export function useMediaPlayerShortcutHints({ fullscreen }: { fullscreen: boolea
});
}
+/**
+ * Whether a keystroke belongs to the player rather than to Trilium's own shortcuts. The players bind bare
+ * keys (Space, arrows, M, F), which as chords are the application's — Ctrl+F is not "fullscreen", and acting
+ * on it would fire alongside whatever the app does with it. The single exception is the player's own
+ * Ctrl+Left/Right minute jump.
+ */
+export function claimsKeystroke(e: KeyboardEvent): boolean {
+ if (!isAppShortcutChord(e)) return true;
+ const isMinuteJump = e.key === "ArrowLeft" || e.key === "ArrowRight";
+ return isMinuteJump && e.ctrlKey && !e.altKey && !e.metaKey;
+}
+
export function SeekBar({ mediaRef }: { mediaRef: RefObject<HTMLVideoElement | HTMLAudioElement> }) {
const [currentTime, setCurrentTime] = useState(0);
const [duration, setDuration] = useState(0);
diff --git a/apps/client/src/widgets/type_widgets/file/Video.spec.tsx b/apps/client/src/widgets/type_widgets/file/Video.spec.tsx
index 24b6973b3c..6ed17233b8 100644
--- a/apps/client/src/widgets/type_widgets/file/Video.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/file/Video.spec.tsx
@@ -142,6 +142,30 @@ describe("VideoPreview", () => {
expect(video.currentTime).toBe(20);
expect(video.play).not.toHaveBeenCalled();
});
+
+ it("stands aside for the application's own chords", async () => {
+ const video = renderPlayer();
+ setDuration(video, 100);
+ video.currentTime = 20;
+ video.volume = 0.5;
+
+ // Ctrl+F is the app's, not "fullscreen"; likewise Ctrl+Space, Ctrl+M and the rest.
+ await press("f", { ctrlKey: true });
+ expect(wrapper().requestFullscreen).not.toHaveBeenCalled();
+
+ await press(" ", { ctrlKey: true });
+ expect(video.play).not.toHaveBeenCalled();
+
+ await press("m", { ctrlKey: true });
+ expect(video.muted).toBe(false);
+
+ await press("Home", { metaKey: true });
+ await press("End", { altKey: true });
+ expect(video.currentTime).toBe(20);
+
+ await press("ArrowUp", { altKey: true });
+ expect(video.volume).toBeCloseTo(0.5);
+ });
});
describe("click and tap", () => {
diff --git a/apps/client/src/widgets/type_widgets/file/Video.tsx b/apps/client/src/widgets/type_widgets/file/Video.tsx
index 4349440f26..c61a6ca340 100644
--- a/apps/client/src/widgets/type_widgets/file/Video.tsx
+++ b/apps/client/src/widgets/type_widgets/file/Video.tsx
@@ -10,7 +10,7 @@ import NoItems from "../../react/NoItems";
import ShortcutHintButton from "../../shortcut_hints/shortcut_hint_button";
import MediaFileActions from "./MediaFileActions";
import { playerRootClasses, preloadFor, showsFileActions, showsViewportControls, usesCompactControls } from "./media_environment";
-import { MediaPlayerProps, MediaSiblingButton, PlaybackSpeed, PlayModeButton, PlayPauseButton, SeekBar, SkipButton, useMediaPlayerShortcutHints, useMediaPlayMode, useMediaSessionController, VolumeControl } from "./MediaPlayer";
+import { claimsKeystroke, MediaPlayerProps, MediaSiblingButton, PlaybackSpeed, PlayModeButton, PlayPauseButton, SeekBar, SkipButton, useMediaPlayerShortcutHints, useMediaPlayMode, useMediaSessionController, VolumeControl } from "./MediaPlayer";
const AUTO_HIDE_DELAY = 3000;
@@ -140,7 +140,7 @@ export default function VideoPreview({ source, entity, environment, noteContext,
function useKeyboardShortcuts(videoRef: MutableRef<HTMLVideoElement | null>, wrapperRef: MutableRef<HTMLDivElement | null>, togglePlayback: () => void, flashControls: () => void) {
return useCallback((e: KeyboardEvent) => {
const video = videoRef.current;
- if (!video) return;
+ if (!video || !claimsKeystroke(e)) return;
switch (e.key) {
case " ":