Skip to content

The agent that grows with you

fix(desktop): debounce and re-verify zoom on Linux Wayland

Brooklyn Nicholsonfab0de1c5b06
diff --git a/apps/desktop/electron/zoom.test.ts b/apps/desktop/electron/zoom.test.ts
index a6affd0c5e5..461a7a8e598 100644
--- a/apps/desktop/electron/zoom.test.ts
+++ b/apps/desktop/electron/zoom.test.ts
@@ -13,7 +13,10 @@ import {
clampZoomLevel,
DEFAULT_ZOOM_LEVEL,
installZoomReassertOnWindowEvents,
+ isDebouncedReassertEvent,
percentToZoomLevel,
+ ZOOM_REASSERT_MAX_SETTLE_CHECKS,
+ ZOOM_REASSERT_SETTLE_DELAY_MS,
ZOOM_RESIZE_REASSERT_DELAY_MS,
ZOOM_STEP,
ZOOM_STORAGE_KEY,
@@ -101,7 +104,7 @@ test('installZoomReassertOnWindowEvents wires show, restore, focus, resize, and
assert.equal(calls, 5)
})
-test('focus event reasserts zoom immediately without debounce (Windows high-DPI alt-tab, #50837)', () => {
+test('focus event reasserts zoom immediately without debounce on Windows (high-DPI alt-tab, #50837)', () => {
const handlers = new Map()
const win = {
@@ -120,12 +123,22 @@ test('focus event reasserts zoom immediately without debounce (Windows high-DPI
'win32'
)
- // focus should trigger immediate reassert — no timer involved
+ // focus on Windows triggers immediate reassert — no timer involved
handlers.get('focus')()
assert.equal(calls, 1)
})
-test('installZoomReassertOnWindowEvents debounces Linux resize and move events at the trailing edge', () => {
+test('isDebouncedReassertEvent debounces focus only on Linux, not Windows/macOS', () => {
+ assert.equal(isDebouncedReassertEvent('focus', 'linux'), true)
+ assert.equal(isDebouncedReassertEvent('focus', 'win32'), false)
+ assert.equal(isDebouncedReassertEvent('focus', 'darwin'), false)
+ assert.equal(isDebouncedReassertEvent('resize', 'linux'), true)
+ assert.equal(isDebouncedReassertEvent('resize', 'win32'), true)
+ assert.equal(isDebouncedReassertEvent('show', 'linux'), false)
+ assert.equal(isDebouncedReassertEvent('restore', 'linux'), false)
+})
+
+test('installZoomReassertOnWindowEvents debounces Linux resize, move, and focus events at the trailing edge', () => {
vi.useFakeTimers()
try {
@@ -158,10 +171,102 @@ test('installZoomReassertOnWindowEvents debounces Linux resize and move events a
vi.advanceTimersByTime(ZOOM_RESIZE_REASSERT_DELAY_MS / 2)
assert.equal(calls, 1)
+ // focus on Linux is also debounced — a session switch fires focus but
+ // the reassert must not jump the zoom mid-interaction.
+ handlers.get('focus')()
+ vi.advanceTimersByTime(ZOOM_RESIZE_REASSERT_DELAY_MS / 2)
+ assert.equal(calls, 1)
+ vi.advanceTimersByTime(ZOOM_RESIZE_REASSERT_DELAY_MS / 2)
+ assert.equal(calls, 2)
+
handlers.get('resize')()
destroyed = true
vi.advanceTimersByTime(ZOOM_RESIZE_REASSERT_DELAY_MS)
- assert.equal(calls, 1)
+ assert.equal(calls, 2)
+ } finally {
+ vi.useRealTimers()
+ }
+})
+
+test('installZoomReassertOnWindowEvents re-verifies Linux zoom after the debounced re-assert so a dropped re-apply is retried', () => {
+ vi.useFakeTimers()
+
+ try {
+ const handlers = new Map()
+
+ const win = {
+ isDestroyed: () => false,
+ on(event, listener) {
+ handlers.set(event, listener)
+ }
+ }
+
+ let calls = 0
+ installZoomReassertOnWindowEvents(
+ win,
+ () => {
+ calls += 1
+ },
+ 'linux'
+ )
+
+ handlers.get('resize')()
+ vi.advanceTimersByTime(ZOOM_RESIZE_REASSERT_DELAY_MS)
+ assert.equal(calls, 1, 'debounced re-assert fires once')
+
+ // Cosmic tiling can drop the just-applied zoom mid-reconfigure; the
+ // settle-verify chain re-asserts on a bounded schedule until it converges:
+ // one initial re-assert plus ZOOM_REASSERT_MAX_SETTLE_CHECKS follow-ups.
+ for (let i = 1; i <= ZOOM_REASSERT_MAX_SETTLE_CHECKS; i++) {
+ vi.advanceTimersByTime(ZOOM_REASSERT_SETTLE_DELAY_MS)
+ assert.equal(calls, 1 + i, `settle check ${i} re-asserts`)
+ }
+
+ vi.advanceTimersByTime(ZOOM_REASSERT_SETTLE_DELAY_MS * 2)
+ assert.equal(calls, 1 + ZOOM_REASSERT_MAX_SETTLE_CHECKS, 'settle chain is bounded and stops')
+ } finally {
+ vi.useRealTimers()
+ }
+})
+
+test('installZoomReassertOnWindowEvents resets the settle chain on a fresh re-assert', () => {
+ vi.useFakeTimers()
+
+ try {
+ const handlers = new Map()
+
+ const win = {
+ isDestroyed: () => false,
+ on(event, listener) {
+ handlers.set(event, listener)
+ }
+ }
+
+ let calls = 0
+ installZoomReassertOnWindowEvents(
+ win,
+ () => {
+ calls += 1
+ },
+ 'linux'
+ )
+
+ handlers.get('show')()
+ vi.advanceTimersByTime(ZOOM_REASSERT_SETTLE_DELAY_MS)
+ assert.equal(calls, 2, 'initial re-assert plus one settle check')
+
+ // A second transition while the first settle chain is still pending must
+ // restart the chain from zero instead of exhausting the budget.
+ handlers.get('restore')()
+ assert.equal(calls, 3, 'fresh transition re-asserts immediately')
+
+ for (let i = 1; i <= ZOOM_REASSERT_MAX_SETTLE_CHECKS; i++) {
+ vi.advanceTimersByTime(ZOOM_REASSERT_SETTLE_DELAY_MS)
+ assert.equal(calls, 3 + i, `restarted chain settle check ${i} re-asserts`)
+ }
+
+ vi.advanceTimersByTime(ZOOM_REASSERT_SETTLE_DELAY_MS * 2)
+ assert.equal(calls, 3 + ZOOM_REASSERT_MAX_SETTLE_CHECKS, 'restarted chain is bounded too')
} finally {
vi.useRealTimers()
}
diff --git a/apps/desktop/electron/zoom.ts b/apps/desktop/electron/zoom.ts
index 4ba948c3624..061d069964d 100644
--- a/apps/desktop/electron/zoom.ts
+++ b/apps/desktop/electron/zoom.ts
@@ -65,18 +65,70 @@ export function applyZoomLevel(webContents, level) {
// fallbacks before re-applying the persisted level.
export const ZOOM_RESIZE_REASSERT_DELAY_MS = 100
+// Linux settle-verify: a re-assert can land while the compositor is still
+// reconfiguring the window's surface (Cosmic tiled mode fires a resize storm
+// the moment a new session window opens, and the final event can arrive before
+// the new scale is committed), in which case Chromium drops the just-applied
+// zoom and the window stays stuck at the wrong scale until the next
+// transition. After the debounced re-assert we therefore re-check a few times
+// at a settle delay; each re-assert is drift-guarded (see
+// restorePersistedZoomLevel), so a window that already matches the persisted
+// level costs nothing and the chain is bounded.
+export const ZOOM_REASSERT_SETTLE_DELAY_MS = 300
+export const ZOOM_REASSERT_MAX_SETTLE_CHECKS = 3
+
export function zoomReassertWindowEvents(platform = process.platform) {
return platform === 'linux'
? ['show', 'restore', 'focus', 'resize', 'move']
: ['show', 'restore', 'focus', 'resized', 'moved']
}
+// Linux/Wayland fires `focus` on intra-app focus shifts (sidebar clicks,
+// Ctrl+Tab session switching, tile activation) — not just the cross-app
+// alt-tab the Windows high-DPI immediate-reassert guard (#50837) targets.
+// An undebounced reassert on every such focus event re-applies the persisted
+// zoom level mid-interaction, producing a visible zoom/DPI jump. Debounce
+// `focus` alongside `resize`/`move` on Linux; Windows alt-tab keeps its
+// immediate path because `platform` is `win32` there.
+const DEBOUNCED_REASSERT_EVENTS = new Set(['resize', 'move'])
+
+export function isDebouncedReassertEvent(event, platform = process.platform) {
+ return DEBOUNCED_REASSERT_EVENTS.has(event) || (platform === 'linux' && event === 'focus')
+}
+
export function installZoomReassertOnWindowEvents(win, reassert, platform = process.platform) {
if (!win?.on) {
return
}
let resizeTimer
+ let settleTimer
+ let settleChecks = 0
+
+ const scheduleSettleCheck = () => {
+ if (platform !== 'linux') {
+ return
+ }
+
+ settleChecks += 1
+
+ if (settleChecks > ZOOM_REASSERT_MAX_SETTLE_CHECKS) {
+ return
+ }
+
+ settleTimer = setTimeout(() => {
+ if (!win.isDestroyed?.()) {
+ reassert()
+ scheduleSettleCheck()
+ }
+ }, ZOOM_REASSERT_SETTLE_DELAY_MS)
+ }
+
+ const reassertWithSettleCheck = () => {
+ settleChecks = 0
+ reassert()
+ scheduleSettleCheck()
+ }
for (const event of zoomReassertWindowEvents(platform)) {
win.on(event, () => {
@@ -84,8 +136,9 @@ export function installZoomReassertOnWindowEvents(win, reassert, platform = proc
return
}
- if (event !== 'resize' && event !== 'move') {
- reassert()
+ if (!isDebouncedReassertEvent(event, platform)) {
+ clearTimeout(settleTimer)
+ reassertWithSettleCheck()
return
}
@@ -93,7 +146,8 @@ export function installZoomReassertOnWindowEvents(win, reassert, platform = proc
clearTimeout(resizeTimer)
resizeTimer = setTimeout(() => {
if (!win.isDestroyed?.()) {
- reassert()
+ clearTimeout(settleTimer)
+ reassertWithSettleCheck()
}
}, ZOOM_RESIZE_REASSERT_DELAY_MS)
})