Skip to content

Commit c8c5c9a

Browse files
committed
Adopt tinykeys and add more keyboard accessible controls, this time on the Full screen manager.
1 parent 3e9fc3c commit c8c5c9a

4 files changed

Lines changed: 117 additions & 15 deletions

File tree

‎web/js/components/preact/FullscreenManager.jsx‎

Lines changed: 105 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,100 @@
22
* Fullscreen functionality for LiveView
33
* React component for managing fullscreen mode
44
*/
5-
import { useState, useEffect, useCallback } from 'preact/hooks';
5+
import { useState, useEffect, useCallback, useRef } from 'preact/hooks';
6+
import { tinykeys } from 'tinykeys';
7+
8+
/**
9+
* Navigate to an adjacent stream in the native fullscreen grid.
10+
* Finds the currently fullscreen .video-cell, locates its grid position, then
11+
* steps in the requested direction (with wrap-around), skipping empty cells.
12+
*
13+
* @param {'ArrowLeft'|'ArrowRight'|'ArrowUp'|'ArrowDown'} direction
14+
* @param {Array} streamsToShow - streams visible in the current page
15+
* @param {number} cols - grid column count
16+
* @param {number} rows - grid row count
17+
*/
18+
function navigateFullscreenGrid(direction, streamsToShow, cols, rows) {
19+
const fullscreenEl = document.fullscreenElement;
20+
if (!fullscreenEl) return;
21+
22+
// The fullscreen element is the .video-cell div (data-stream-name is on it)
23+
const streamName = fullscreenEl.dataset.streamName;
24+
if (!streamName) return;
25+
26+
const currentIndex = streamsToShow.findIndex(s => s.name === streamName);
27+
if (currentIndex === -1) return;
28+
29+
let nextRow = Math.floor(currentIndex / cols);
30+
let nextCol = currentIndex % cols;
31+
32+
// Walk one step at a time in the requested direction, wrapping around, until
33+
// we land on a populated cell (or exhaust all possibilities).
34+
const maxAttempts = cols * rows;
35+
for (let i = 0; i < maxAttempts; i++) {
36+
if (direction === 'ArrowRight') {
37+
nextCol = (nextCol + 1) % cols;
38+
} else if (direction === 'ArrowLeft') {
39+
nextCol = (nextCol - 1 + cols) % cols;
40+
} else if (direction === 'ArrowDown') {
41+
nextRow = (nextRow + 1) % rows;
42+
} else if (direction === 'ArrowUp') {
43+
nextRow = (nextRow - 1 + rows) % rows;
44+
}
45+
46+
const nextIndex = nextRow * cols + nextCol;
47+
if (nextIndex < streamsToShow.length) {
48+
const nextStream = streamsToShow[nextIndex];
49+
// Query the DOM for the cell; it remains in the DOM even while another
50+
// element is in fullscreen mode.
51+
const nextCell = document.querySelector(
52+
`[data-stream-name="${CSS.escape(nextStream.name)}"].video-cell`
53+
);
54+
if (nextCell && nextCell !== fullscreenEl) {
55+
nextCell.requestFullscreen().catch(err => {
56+
console.warn(`Grid nav fullscreen switch failed: ${err.message}`);
57+
});
58+
}
59+
return;
60+
}
61+
}
62+
}
63+
64+
/**
65+
* Hook: bind arrow keys for grid navigation while a stream is in native
66+
* fullscreen. Refs keep streamsToShow/cols/rows fresh without re-subscribing
67+
* tinykeys on every render.
68+
*
69+
* @param {Array} streamsToShow
70+
* @param {number} cols
71+
* @param {number} rows
72+
*/
73+
export function useFullscreenGridNav(streamsToShow, cols, rows) {
74+
const streamsRef = useRef(streamsToShow);
75+
const colsRef = useRef(cols);
76+
const rowsRef = useRef(rows);
77+
78+
useEffect(() => { streamsRef.current = streamsToShow; }, [streamsToShow]);
79+
useEffect(() => { colsRef.current = cols; }, [cols]);
80+
useEffect(() => { rowsRef.current = rows; }, [rows]);
81+
82+
useEffect(() => {
83+
const guard = (dir) => (e) => {
84+
if (!document.fullscreenElement) return;
85+
e.preventDefault();
86+
navigateFullscreenGrid(dir, streamsRef.current, colsRef.current, rowsRef.current);
87+
};
88+
89+
const unsub = tinykeys(window, {
90+
ArrowLeft: guard('ArrowLeft'),
91+
ArrowRight: guard('ArrowRight'),
92+
ArrowUp: guard('ArrowUp'),
93+
ArrowDown: guard('ArrowDown'),
94+
});
95+
96+
return unsub; // tinykeys returns the cleanup function directly
97+
}, []); // empty deps: refs keep values current
98+
}
699

7100
/**
8101
* Custom hook for fullscreen functionality
@@ -107,29 +200,27 @@ export function FullscreenButton({ isFullscreen, onToggle }) {
107200
* @returns {JSX.Element} FullscreenManager component
108201
*/
109202
export function FullscreenManager({ isFullscreen, setIsFullscreen, targetId = 'live-page' }) {
110-
// Handle escape key
203+
// Handle escape key (via tinykeys) and apply/remove fullscreen CSS
111204
useEffect(() => {
112205
if (!isFullscreen) return;
113206

114-
const handleEscapeKey = (e) => {
115-
if (e.key === 'Escape') {
116-
console.log("Escape key pressed in fullscreen mode");
117-
setIsFullscreen(false);
118-
}
119-
};
120-
121-
document.addEventListener('keydown', handleEscapeKey);
122-
123-
// Apply fullscreen styles to the target element
124207
const targetElement = document.getElementById(targetId);
125208
if (targetElement) {
126209
targetElement.classList.add('fullscreen-mode');
127210
document.body.style.overflow = 'hidden';
128211
}
129212

213+
const unsub = tinykeys(window, {
214+
Escape: (e) => {
215+
console.log("Escape key pressed in fullscreen mode");
216+
e.preventDefault();
217+
setIsFullscreen(false);
218+
},
219+
});
220+
130221
return () => {
131-
document.removeEventListener('keydown', handleEscapeKey);
132-
222+
unsub();
223+
133224
// Remove fullscreen styles when component unmounts or fullscreen is exited
134225
if (targetElement) {
135226
targetElement.classList.remove('fullscreen-mode');

‎web/js/components/preact/LiveView.jsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { useState, useEffect, useMemo } from 'preact/hooks';
77
import { showStatusMessage } from './ToastContainer.jsx';
8-
import { useFullscreenManager, FullscreenManager } from './FullscreenManager.jsx';
8+
import { useFullscreenManager, FullscreenManager, useFullscreenGridNav } from './FullscreenManager.jsx';
99
import { useQuery, useQueryClient } from '../../query-client.js';
1010
import { SnapshotManager, useSnapshotManager } from './SnapshotManager.jsx';
1111
import { HLSVideoCell } from './HLSVideoCell.jsx';
@@ -466,6 +466,9 @@ export function LiveView({isWebRTCDisabled}) {
466466
return result;
467467
}, [orderedStreams, isSingleStream, selectedStream, currentPage, maxStreams, cols, rows]);
468468

469+
// Arrow-key navigation between streams while one is in native fullscreen.
470+
useFullscreenGridNav(streamsToShow, cols, rows);
471+
469472
return (
470473
<section
471474
id="live-page"

‎web/package-lock.json‎

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎web/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"preact": "^10.28.3",
2525
"qrcode": "^1.5.4",
2626
"tailwindcss": "^4.1.18",
27+
"tinykeys": "^3.0.0",
2728
"webrtc-adapter": "^9.0.3"
2829
},
2930
"overrides": {

0 commit comments

Comments
 (0)