Repository navigation
feat(desktop): add viewport pinch zoom to browser and page panes #7519
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
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
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
apps/desktop/src/renderer/lib/browser-viewport-zoom/browser-viewport-zoom.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,31 @@ | ||
| import { | ||
| applyPageViewportZoom, | ||
| NEXT_PAGE_VIEWPORT_ZOOM_SCRIPT, | ||
| type PageViewportZoom, | ||
| } from "@superset/shared/page-zoom"; | ||
|
|
||
| export function attachBrowserViewportZoom(webview: Electron.WebviewTag) { | ||
| let generation = 0; | ||
| const onReady = () => { | ||
| const current = ++generation; | ||
| const listen = async () => { | ||
| while (generation === current) { | ||
| try { | ||
| const viewport: PageViewportZoom = await webview.executeJavaScript( | ||
| NEXT_PAGE_VIEWPORT_ZOOM_SCRIPT, | ||
| ); | ||
| if (generation !== current) return; | ||
| applyPageViewportZoom(webview, viewport); | ||
| } catch { | ||
| return; | ||
| } | ||
| } | ||
| }; | ||
| void listen(); | ||
| }; | ||
| webview.addEventListener("dom-ready", onReady); | ||
| return () => { | ||
| generation++; | ||
| webview.removeEventListener("dom-ready", onReady); | ||
| }; | ||
| } |
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 { attachBrowserViewportZoom } from "./browser-viewport-zoom"; |
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
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
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,148 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { createContext, runInContext, Script } from "node:vm"; | ||
| import { PAGE_COMMENTS_RUNTIME_SOURCE } from "./page-comments-runtime"; | ||
| import { | ||
| applyPageViewportZoom, | ||
| NEXT_PAGE_VIEWPORT_ZOOM_SCRIPT, | ||
| PAGE_PINCH_ZOOM_RUNTIME_SOURCE, | ||
| type PageViewportZoom, | ||
| } from "./page-zoom"; | ||
|
|
||
| function setup() { | ||
| const listeners = new Map<string, (event: unknown) => void>(); | ||
| const style = { zoom: "", width: "", transform: "" }; | ||
| let viewport: PageViewportZoom | null = null; | ||
| const context = createContext({ | ||
| document: { documentElement: { style } }, | ||
| innerWidth: 800, | ||
| innerHeight: 600, | ||
| scrollX: 0, | ||
| scrollY: 100, | ||
| locked: false, | ||
| onZoom: (value: PageViewportZoom) => { | ||
| viewport = value; | ||
| }, | ||
| addEventListener: (type: string, listener: (event: unknown) => void) => | ||
| listeners.set(type, listener), | ||
| scrollBy: ({ left, top }: { left: number; top: number }) => { | ||
| context.scrollX += left; | ||
| context.scrollY += top; | ||
| }, | ||
| }); | ||
| runInContext( | ||
| `const pinchZoom = (${PAGE_PINCH_ZOOM_RUNTIME_SOURCE})(onZoom, () => locked)`, | ||
| context, | ||
| ); | ||
| return { | ||
| context, | ||
| style, | ||
| enable: () => runInContext("pinchZoom.enable()", context), | ||
| viewport: () => viewport, | ||
| wheel: (deltaY: number, overrides = {}) => { | ||
| let prevented = false; | ||
| listeners.get("wheel")?.({ | ||
| ctrlKey: true, | ||
| deltaMode: 0, | ||
| deltaX: 0, | ||
| deltaY, | ||
| clientX: 200, | ||
| clientY: 150, | ||
| preventDefault: () => { | ||
| prevented = true; | ||
| }, | ||
| ...overrides, | ||
| }); | ||
| return prevented; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| describe("page viewport zoom", () => { | ||
| test("composes into valid frame and browser scripts", () => { | ||
| expect(() => new Script(PAGE_COMMENTS_RUNTIME_SOURCE)).not.toThrow(); | ||
| expect(() => new Script(NEXT_PAGE_VIEWPORT_ZOOM_SCRIPT)).not.toThrow(); | ||
| }); | ||
| test("preserves ordinary scrolling and requires host opt-in", () => { | ||
| const page = setup(); | ||
| expect(page.wheel(-50)).toBe(false); | ||
| page.enable(); | ||
| expect(page.wheel(-50, { ctrlKey: false })).toBe(false); | ||
| expect(page.wheel(-50, { defaultPrevented: true })).toBe(false); | ||
| page.context.locked = true; | ||
| expect(page.wheel(-50)).toBe(false); | ||
| }); | ||
| test("magnifies around the pointer without changing content styles or layout scroll", () => { | ||
| const page = setup(); | ||
| page.enable(); | ||
| page.wheel(-Math.log(2) / 0.01); | ||
| expect(page.viewport()).toEqual({ | ||
| scale: 2, | ||
| x: 200, | ||
| y: 150, | ||
| width: 800, | ||
| height: 600, | ||
| }); | ||
| expect(page.style).toEqual({ zoom: "", width: "", transform: "" }); | ||
| expect(page.context.scrollX).toBe(0); | ||
| expect(page.context.scrollY).toBe(100); | ||
| }); | ||
| test("pans within the magnified viewport before scrolling the document", () => { | ||
| const page = setup(); | ||
| page.enable(); | ||
| page.wheel(-Math.log(2) / 0.01); | ||
| page.wheel(100, { ctrlKey: false, deltaX: 50 }); | ||
| expect(page.viewport()?.x).toBe(250); | ||
| expect(page.viewport()?.y).toBe(250); | ||
| expect(page.context.scrollY).toBe(100); | ||
| page.wheel(400, { ctrlKey: false }); | ||
| expect(page.viewport()?.y).toBe(600); | ||
| expect(page.context.scrollY).toBe(125); | ||
| }); | ||
| test("clamps repeated zooms and returns to an untransformed surface", () => { | ||
| const page = setup(); | ||
| page.enable(); | ||
| for (let i = 0; i < 20; i++) page.wheel(-100); | ||
| expect(page.viewport()?.scale).toBe(5); | ||
| for (let i = 0; i < 20; i++) page.wheel(100); | ||
| expect(page.viewport()).toEqual({ | ||
| scale: 1, | ||
| x: 0, | ||
| y: 0, | ||
| width: 800, | ||
| height: 600, | ||
| }); | ||
| }); | ||
| test("clips the compositor surface to its original pane without changing its dimensions", () => { | ||
| const style = { | ||
| width: "800px", | ||
| height: "600px", | ||
| transform: "", | ||
| transformOrigin: "", | ||
| clipPath: "", | ||
| }; | ||
| const surface = { style }; | ||
| applyPageViewportZoom(surface, { | ||
| scale: 2, | ||
| x: 200, | ||
| y: 150, | ||
| width: 800, | ||
| height: 600, | ||
| }); | ||
| expect(style).toEqual({ | ||
| width: "800px", | ||
| height: "600px", | ||
| transform: "translate(-200px, -150px) scale(2)", | ||
| transformOrigin: "0 0", | ||
| clipPath: "inset(75px 300px 225px 100px)", | ||
| }); | ||
| applyPageViewportZoom(surface, { | ||
| scale: 1, | ||
| x: 0, | ||
| y: 0, | ||
| width: 800, | ||
| height: 600, | ||
| }); | ||
| expect(style.transform).toBe(""); | ||
| expect(style.clipPath).toBe(""); | ||
| }); | ||
| }); |
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,95 @@ | ||
| export const PAGE_PINCH_ZOOM_LIMITS = Object.freeze({ min: 1, max: 5 }); | ||
|
|
||
| export interface PageViewportZoom { | ||
| scale: number; | ||
| x: number; | ||
| y: number; | ||
| width: number; | ||
| height: number; | ||
| } | ||
|
|
||
| export function applyPageViewportZoom( | ||
| element: { | ||
| style: { transform: string; transformOrigin: string; clipPath: string }; | ||
| }, | ||
| viewport: PageViewportZoom, | ||
| ): void { | ||
| const { scale, x, y, width, height } = viewport; | ||
| if ( | ||
| ![scale, x, y, width, height].every(Number.isFinite) || | ||
| scale < PAGE_PINCH_ZOOM_LIMITS.min || | ||
| scale > PAGE_PINCH_ZOOM_LIMITS.max || | ||
| width <= 0 || | ||
| height <= 0 | ||
| ) | ||
| return; | ||
| if (scale === 1) { | ||
| element.style.transform = ""; | ||
| element.style.transformOrigin = ""; | ||
| element.style.clipPath = ""; | ||
| return; | ||
| } | ||
| const left = Math.max(0, Math.min(x, width * (scale - 1))); | ||
| const top = Math.max(0, Math.min(y, height * (scale - 1))); | ||
| element.style.transformOrigin = "0 0"; | ||
| element.style.transform = `translate(${-left}px, ${-top}px) scale(${scale})`; | ||
| element.style.clipPath = `inset(${top / scale}px ${width - (left + width) / scale}px ${height - (top + height) / scale}px ${left / scale}px)`; | ||
| } | ||
|
|
||
| export const PAGE_PINCH_ZOOM_RUNTIME_SOURCE = `(onZoom = () => {}, isLocked = () => false) => { | ||
| let enabled = false; | ||
| let scale = 1; | ||
| let x = 0; | ||
| let y = 0; | ||
| const clamp = () => { | ||
| x = Math.max(0, Math.min(x, innerWidth * (scale - 1))); | ||
| y = Math.max(0, Math.min(y, innerHeight * (scale - 1))); | ||
| }; | ||
| const report = () => onZoom({ scale, x, y, width: innerWidth, height: innerHeight }); | ||
| addEventListener("resize", () => { clamp(); if (enabled) report(); }); | ||
| addEventListener("wheel", (event) => { | ||
| if (!enabled || event.defaultPrevented || isLocked()) return; | ||
| if (!event.ctrlKey && scale === 1) return; | ||
| event.preventDefault(); | ||
| const unit = event.deltaMode === 1 ? 16 : event.deltaMode === 2 ? innerHeight : 1; | ||
| if (event.ctrlKey) { | ||
| const next = Math.min(${PAGE_PINCH_ZOOM_LIMITS.max}, Math.max( | ||
| ${PAGE_PINCH_ZOOM_LIMITS.min}, scale * Math.exp(-event.deltaY * unit * 0.01), | ||
| )); | ||
| if (!Number.isFinite(next)) return; | ||
| x += event.clientX * (next - scale); | ||
| y += event.clientY * (next - scale); | ||
| scale = next; | ||
| clamp(); | ||
| } else { | ||
| const nextX = x + event.deltaX * unit; | ||
| const nextY = y + event.deltaY * unit; | ||
| x = nextX; | ||
| y = nextY; | ||
| clamp(); | ||
| if (nextX !== x || nextY !== y) { | ||
| scrollBy({ left: (nextX - x) / scale, top: (nextY - y) / scale, behavior: "instant" }); | ||
| } | ||
| } | ||
| report(); | ||
| }, { passive: false }); | ||
| return { enable: () => { enabled = true; report(); } }; | ||
| }`; | ||
|
|
||
| export const NEXT_PAGE_VIEWPORT_ZOOM_SCRIPT = `(() => { | ||
| const key = Symbol.for("superset.viewport-zoom"); | ||
| if (!globalThis[key]) { | ||
| let latest = null; | ||
| let resolve = null; | ||
| const controller = (${PAGE_PINCH_ZOOM_RUNTIME_SOURCE})((viewport) => { | ||
| if (resolve) { const notify = resolve; resolve = null; notify(viewport); } | ||
| else latest = viewport; | ||
| }); | ||
| globalThis[key] = () => new Promise((notify) => { | ||
| if (latest) { const viewport = latest; latest = null; notify(viewport); } | ||
| else resolve = notify; | ||
| }); | ||
| controller.enable(); | ||
| } | ||
| return globalThis[key](); | ||
| })();`; | ||
Oops, something went wrong.
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: superset-sh/superset
Length of output: 50377
🏁 Script executed:
Repository: superset-sh/superset
Length of output: 50376
🏁 Script executed:
Repository: superset-sh/superset
Length of output: 39331
🌐 Web query:
Electron 41 BrowserWindow WebContents unprevented Ctrl+wheel pinch zoom default setVisualZoomLevelLimits documentation💡 Result:
<search_synthesis>
In Electron, including version 41, the visual pinch-zoom behavior is disabled by default [1][2][3]. The
webContents.setVisualZoomLevelLimits(minimumLevel, maximumLevel)API is used to explicitly configure or re-enable this behavior [1][4]. Key details regarding visual zoom andsetVisualZoomLevelLimits: 1. Default Behavior: Visual zoom (pinch-to-zoom) is disabled by default [1][3]. You do not need to take action to disable it if you do not want it; however, if it has been enabled elsewhere in your application, you can explicitly restrict it by setting both the minimum and maximum levels to 1 [5][6]:js win.webContents.setVisualZoomLevelLimits(1, 1);2. Enabling Visual Zoom: To allow pinch-to-zoom, you must provide a range greater than 1 [1][3]. For example, to allow zooming between 100% and 300%:js win.webContents.setVisualZoomLevelLimits(1, 3);3. Scope and Distinctions: * Visual Zoom vs. Layout Zoom:setVisualZoomLevelLimitsapplies specifically to pinch-to-zoom [2][3]. It does not affect standard keyboard-based or menu-based zoom shortcuts (e.g., Ctrl+/-/0 or Cmd+/-/0), which modify the layout zoom level [2][3]. * Menu Controls: Layout zoom is controlled by the application menu's MenuItem roles (zoomIn,zoomOut,resetZoom) [2][3]. To disable these shortcuts, you must define your own menu and omit these roles [2][3]. * Implementation:webContents.setVisualZoomLevelLimitsis available on thewebContentsobject in the main process [1][7]. A similar method,webFrame.setVisualZoomLevelLimits, exists for use within the renderer process [2][3]. Documentation for these methods can be found in the official Electron API references forwebContentsandwebFrame[1][2][3].</search_synthesis>
<source_evidence>
Citations:
🌐 Web query:
Electron 41 Ctrl+wheel WheelEvent ctrlKey page zoom visual zoom default behavior💡 Result:
<search_synthesis>
In Electron, the default behavior for Ctrl+Mouse Wheel (or Command+Mouse Wheel on macOS) is to perform page zooming (often referred to as layout zoom) [1]. This behavior is inherited from the underlying Chromium engine [1]. When this action occurs, a WheelEvent is dispatched in the web contents where the
ctrlKeyproperty is set totrue[2][3]. This aligns with standard web behavior where Ctrl+Wheel is used for zooming [2][3]. Key technical details regarding this behavior in Electron: 1. Layout vs. Visual Zoom: Electron distinguishes between layout zoom (scaling the page content, which is the default for Ctrl+Wheel) and visual zoom (pinch-to-zoom). Visual zoom is disabled by default in Electron [4][5]. To enable visual zoom, you must explicitly call webContents.setVisualZoomLevelLimits(min, max) or webFrame.setVisualZoomLevelLimits(min, max) [4][5]. 2. Event Handling: When the user zooms using the mouse wheel, Electron emits a 'zoom-changed' event on the webContents instance [6][7]. It is important to note that this event is specifically emitted for mouse wheel/trackpad zoom actions and is not triggered by other zoom methods, such as keyboard shortcuts (e.g., Ctrl+/-) or menu-based zoom roles [6][7]. 3. Disabling Default Behavior: Many Electron developers choose to disable unwanted default browser behaviors, including pinch-zoom, to provide a more native application experience [1]. This is typically managed by configuring the zoom limits or by overriding event listeners [1]. If you are developing an Electron application and need to intercept or customize this behavior, you can use the 'zoom-changed' event for monitoring or call the appropriate setVisualZoomLevelLimits methods to control the zoom policy [4][5][6].</search_synthesis>
<source_evidence>
Citations:
Prevent native Ctrl-wheel zoom while pinch zoom is locked.
When
isLocked()is true,PAGE_PINCH_ZOOM_RUNTIME_SOURCEreturns beforeevent.preventDefault(). Electron treats an unprevented Ctrl-wheel event as page zoom, and thebefore-input-eventhandlers do not intercept wheel input. Prevent Ctrl-wheel defaults before the locked-state return and update the locked-state test to expect prevention.🤖 Prompt for AI Agents