|
1 | 1 | import { IconButton } from "./IconButton" |
| 2 | +import { useRef, useEffect } from "react" |
2 | 3 |
|
3 | 4 | interface ZoomControlsProps { |
4 | 5 | zoomLevel: number |
5 | | - onZoomIn: () => void |
6 | | - onZoomOut: () => void |
7 | 6 | zoomInTitle?: string |
8 | 7 | zoomOutTitle?: string |
| 8 | + useContinuousZoom?: boolean |
| 9 | + adjustZoom?: (amount: number) => void |
| 10 | + zoomInStep?: number |
| 11 | + zoomOutStep?: number |
| 12 | + onZoomIn?: () => void |
| 13 | + onZoomOut?: () => void |
9 | 14 | } |
10 | 15 |
|
11 | | -export function ZoomControls({ zoomLevel, onZoomIn, onZoomOut, zoomInTitle, zoomOutTitle }: ZoomControlsProps) { |
| 16 | +export function ZoomControls({ |
| 17 | + zoomLevel, |
| 18 | + zoomInTitle, |
| 19 | + zoomOutTitle, |
| 20 | + useContinuousZoom = false, |
| 21 | + adjustZoom, |
| 22 | + zoomInStep = 0.1, |
| 23 | + zoomOutStep = -0.1, |
| 24 | + onZoomIn, |
| 25 | + onZoomOut, |
| 26 | +}: ZoomControlsProps) { |
| 27 | + const zoomIntervalRef = useRef<NodeJS.Timeout | null>(null) |
| 28 | + |
| 29 | + /** |
| 30 | + * Start continuous zoom on mouse down |
| 31 | + */ |
| 32 | + const startContinuousZoom = (amount: number) => { |
| 33 | + if (!useContinuousZoom || !adjustZoom) return |
| 34 | + |
| 35 | + // Clear any existing interval first |
| 36 | + if (zoomIntervalRef.current) { |
| 37 | + clearInterval(zoomIntervalRef.current) |
| 38 | + } |
| 39 | + |
| 40 | + // Immediately apply first zoom adjustment |
| 41 | + adjustZoom(amount) |
| 42 | + |
| 43 | + // Set up interval for continuous zooming |
| 44 | + zoomIntervalRef.current = setInterval(() => { |
| 45 | + adjustZoom(amount) |
| 46 | + }, 150) // Adjust every 150ms while button is held down |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Stop continuous zoom on mouse up or mouse leave |
| 51 | + */ |
| 52 | + const stopContinuousZoom = () => { |
| 53 | + if (zoomIntervalRef.current) { |
| 54 | + clearInterval(zoomIntervalRef.current) |
| 55 | + zoomIntervalRef.current = null |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // Clean up interval on unmount |
| 60 | + useEffect(() => { |
| 61 | + return () => { |
| 62 | + if (zoomIntervalRef.current) { |
| 63 | + clearInterval(zoomIntervalRef.current) |
| 64 | + } |
| 65 | + } |
| 66 | + }, []) |
| 67 | + |
| 68 | + // If using continuous zoom, render buttons with mouse down/up handlers |
| 69 | + if (useContinuousZoom && adjustZoom) { |
| 70 | + return ( |
| 71 | + <div className="flex items-center gap-2"> |
| 72 | + <button |
| 73 | + className="w-7 h-7 flex items-center justify-center border-none text-vscode-editor-foreground cursor-pointer rounded-[3px] bg-transparent hover:bg-vscode-toolbar-hoverBackground" |
| 74 | + onMouseDown={() => startContinuousZoom(zoomOutStep)} |
| 75 | + onMouseUp={stopContinuousZoom} |
| 76 | + onMouseLeave={stopContinuousZoom} |
| 77 | + title={zoomOutTitle}> |
| 78 | + <span className="codicon codicon-zoom-out"></span> |
| 79 | + </button> |
| 80 | + <div className="text-sm text-vscode-editor-foreground min-w-[50px] text-center"> |
| 81 | + {Math.round(zoomLevel * 100)}% |
| 82 | + </div> |
| 83 | + <button |
| 84 | + className="w-7 h-7 flex items-center justify-center border-none text-vscode-editor-foreground cursor-pointer rounded-[3px] bg-transparent hover:bg-vscode-toolbar-hoverBackground" |
| 85 | + onMouseDown={() => startContinuousZoom(zoomInStep)} |
| 86 | + onMouseUp={stopContinuousZoom} |
| 87 | + onMouseLeave={stopContinuousZoom} |
| 88 | + title={zoomInTitle}> |
| 89 | + <span className="codicon codicon-zoom-in"></span> |
| 90 | + </button> |
| 91 | + </div> |
| 92 | + ) |
| 93 | + } |
| 94 | + |
| 95 | + // Default rendering with simple click handlers |
12 | 96 | return ( |
13 | 97 | <div className="flex items-center gap-2"> |
14 | | - <IconButton icon="zoom-out" onClick={onZoomOut} title={zoomOutTitle} /> |
| 98 | + <IconButton icon="zoom-out" onClick={onZoomOut || (() => adjustZoom?.(zoomOutStep))} title={zoomOutTitle} /> |
15 | 99 | <div className="text-sm text-vscode-editor-foreground min-w-[50px] text-center"> |
16 | 100 | {Math.round(zoomLevel * 100)}% |
17 | 101 | </div> |
18 | | - <IconButton icon="zoom-in" onClick={onZoomIn} title={zoomInTitle} /> |
| 102 | + <IconButton icon="zoom-in" onClick={onZoomIn || (() => adjustZoom?.(zoomInStep))} title={zoomInTitle} /> |
19 | 103 | </div> |
20 | 104 | ) |
21 | 105 | } |
0 commit comments