Repository navigation
feat(desktop): add pan, pinch, and zoom to the image file pane #6285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...ks/usePaneRegistry/components/FilePane/registry/views/ImageView/hooks/usePanZoom/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { usePanZoom } from "./usePanZoom"; |
158 changes: 158 additions & 0 deletions
158
...ePaneRegistry/components/FilePane/registry/views/ImageView/hooks/usePanZoom/usePanZoom.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| import { | ||
| type KeyboardEvent as ReactKeyboardEvent, | ||
| type PointerEvent as ReactPointerEvent, | ||
| useCallback, | ||
| useEffect, | ||
| useRef, | ||
| useState, | ||
| } from "react"; | ||
|
|
||
| const MIN_SCALE = 0.25; | ||
| const MAX_SCALE = 16; | ||
| const DEFAULT_TRANSFORM: Transform = { scale: 1, x: 0, y: 0 }; | ||
|
|
||
| export interface Transform { | ||
| scale: number; | ||
| x: number; | ||
| y: number; | ||
| } | ||
|
|
||
| function zoomAtPoint( | ||
| prev: Transform, | ||
| nextScale: number, | ||
| pointX: number, | ||
| pointY: number, | ||
| ): Transform { | ||
| const scale = Math.min(MAX_SCALE, Math.max(MIN_SCALE, nextScale)); | ||
| const ratio = scale / prev.scale; | ||
| return { | ||
| scale, | ||
| x: pointX - (pointX - prev.x) * ratio, | ||
| y: pointY - (pointY - prev.y) * ratio, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Pan/pinch/zoom gestures for a container element. The container must be | ||
| * mounted for the lifetime of the hook so the wheel listener attaches. | ||
| */ | ||
| export function usePanZoom() { | ||
| const containerRef = useRef<HTMLDivElement>(null); | ||
| const [transform, setTransform] = useState<Transform>(DEFAULT_TRANSFORM); | ||
| const [isDragging, setIsDragging] = useState(false); | ||
| const dragRef = useRef<{ | ||
| pointerId: number; | ||
| startX: number; | ||
| startY: number; | ||
| originX: number; | ||
| originY: number; | ||
| } | null>(null); | ||
|
|
||
| const reset = useCallback(() => setTransform(DEFAULT_TRANSFORM), []); | ||
|
|
||
| // Native non-passive listener: React's onWheel can't preventDefault, | ||
| // and trackpad pinch arrives as a wheel event with ctrlKey set. | ||
| useEffect(() => { | ||
| const container = containerRef.current; | ||
| if (!container) { | ||
| return; | ||
| } | ||
| const handleWheel = (event: WheelEvent) => { | ||
| event.preventDefault(); | ||
| if (event.ctrlKey || event.metaKey) { | ||
| const rect = container.getBoundingClientRect(); | ||
| const pointX = event.clientX - rect.left - rect.width / 2; | ||
| const pointY = event.clientY - rect.top - rect.height / 2; | ||
| setTransform((prev) => | ||
| zoomAtPoint( | ||
| prev, | ||
| prev.scale * Math.exp(-event.deltaY * 0.01), | ||
| pointX, | ||
| pointY, | ||
| ), | ||
| ); | ||
| } else { | ||
| setTransform((prev) => ({ | ||
| ...prev, | ||
| x: prev.x - event.deltaX, | ||
| y: prev.y - event.deltaY, | ||
| })); | ||
| } | ||
| }; | ||
| container.addEventListener("wheel", handleWheel, { passive: false }); | ||
| return () => container.removeEventListener("wheel", handleWheel); | ||
| }, []); | ||
|
|
||
| const endDrag = (event: ReactPointerEvent<HTMLDivElement>) => { | ||
| if (dragRef.current?.pointerId !== event.pointerId) { | ||
| return; | ||
| } | ||
| dragRef.current = null; | ||
| setIsDragging(false); | ||
| }; | ||
|
|
||
| const handlers = { | ||
| onPointerDown: (event: ReactPointerEvent<HTMLDivElement>) => { | ||
| if (event.button !== 0 || dragRef.current) { | ||
| return; | ||
| } | ||
| event.currentTarget.setPointerCapture(event.pointerId); | ||
| dragRef.current = { | ||
| pointerId: event.pointerId, | ||
| startX: event.clientX, | ||
| startY: event.clientY, | ||
| originX: transform.x, | ||
| originY: transform.y, | ||
| }; | ||
| setIsDragging(true); | ||
| }, | ||
| onPointerMove: (event: ReactPointerEvent<HTMLDivElement>) => { | ||
| const drag = dragRef.current; | ||
| if (!drag || drag.pointerId !== event.pointerId) { | ||
| return; | ||
| } | ||
| setTransform((prev) => ({ | ||
| ...prev, | ||
| x: drag.originX + event.clientX - drag.startX, | ||
| y: drag.originY + event.clientY - drag.startY, | ||
| })); | ||
| }, | ||
| onPointerUp: endDrag, | ||
| onPointerCancel: endDrag, | ||
| onDoubleClick: reset, | ||
| onKeyDown: (event: ReactKeyboardEvent<HTMLDivElement>) => { | ||
| const KEYBOARD_PAN_STEP = 50; | ||
| const actions: Record<string, () => Transform> = { | ||
| "+": () => zoomAtPoint(transform, transform.scale * 1.25, 0, 0), | ||
| "=": () => zoomAtPoint(transform, transform.scale * 1.25, 0, 0), | ||
| "-": () => zoomAtPoint(transform, transform.scale / 1.25, 0, 0), | ||
| "0": () => DEFAULT_TRANSFORM, | ||
| ArrowLeft: () => ({ ...transform, x: transform.x + KEYBOARD_PAN_STEP }), | ||
| ArrowRight: () => ({ | ||
| ...transform, | ||
| x: transform.x - KEYBOARD_PAN_STEP, | ||
| }), | ||
| ArrowUp: () => ({ ...transform, y: transform.y + KEYBOARD_PAN_STEP }), | ||
| ArrowDown: () => ({ ...transform, y: transform.y - KEYBOARD_PAN_STEP }), | ||
| }; | ||
| const action = actions[event.key]; | ||
| if (!action || event.ctrlKey || event.metaKey || event.altKey) { | ||
| return; | ||
| } | ||
| event.preventDefault(); | ||
| setTransform(action()); | ||
| }, | ||
| }; | ||
|
|
||
| const isTransformed = | ||
| transform.scale !== 1 || transform.x !== 0 || transform.y !== 0; | ||
|
|
||
| return { | ||
| containerRef, | ||
| transform, | ||
| isDragging, | ||
| isTransformed, | ||
| reset, | ||
| handlers, | ||
| }; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3: role="application" disables normal screen-reader navigation (virtual cursor) inside the pane, so the image's alt text and the reset button stop being announced/reachable through standard reading keys. tabIndex={0} already supplies the focusable element the keyboard pan/zoom handlers need, so the region can typically be labeled without application — consider a more permissive role (or aria-roledescription on the focusable region) so the inner img/button stay accessible, unless full screen-reader key interception is intentional.
Prompt for AI agents