|
2 | 2 | * Fullscreen functionality for LiveView |
3 | 3 | * React component for managing fullscreen mode |
4 | 4 | */ |
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 | +} |
6 | 99 |
|
7 | 100 | /** |
8 | 101 | * Custom hook for fullscreen functionality |
@@ -107,29 +200,27 @@ export function FullscreenButton({ isFullscreen, onToggle }) { |
107 | 200 | * @returns {JSX.Element} FullscreenManager component |
108 | 201 | */ |
109 | 202 | export function FullscreenManager({ isFullscreen, setIsFullscreen, targetId = 'live-page' }) { |
110 | | - // Handle escape key |
| 203 | + // Handle escape key (via tinykeys) and apply/remove fullscreen CSS |
111 | 204 | useEffect(() => { |
112 | 205 | if (!isFullscreen) return; |
113 | 206 |
|
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 |
124 | 207 | const targetElement = document.getElementById(targetId); |
125 | 208 | if (targetElement) { |
126 | 209 | targetElement.classList.add('fullscreen-mode'); |
127 | 210 | document.body.style.overflow = 'hidden'; |
128 | 211 | } |
129 | 212 |
|
| 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 | + |
130 | 221 | return () => { |
131 | | - document.removeEventListener('keydown', handleEscapeKey); |
132 | | - |
| 222 | + unsub(); |
| 223 | + |
133 | 224 | // Remove fullscreen styles when component unmounts or fullscreen is exited |
134 | 225 | if (targetElement) { |
135 | 226 | targetElement.classList.remove('fullscreen-mode'); |
|
0 commit comments