-
Notifications
You must be signed in to change notification settings - Fork 319
fix(desktop): resync renderer after Windows maximize #3472
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
Astro-Han
merged 7 commits into
apache:main
from
1625567290:fix/windows-maximize-renderer-resize
Aug 23, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
35e543d
修复 Windows 最大化后渲染区未同步
1625567290 31ac94f
补充 Windows 最大化打包烟测
1625567290 96ea703
改用原生窗口句柄验证最大化
1625567290 81cb3a7
强化 Windows 最大化尺寸烟测
1625567290 3b90c1d
补充 Windows 最大化文件许可头
1625567290 a058b74
合并窗口尺寸同步事件处理器
1625567290 d95e89c
收紧 Windows 最大化同步容错
1625567290 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
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
143 changes: 143 additions & 0 deletions
143
apps/desktop/src/main/__tests__/windows-maximize-renderer-sync.test.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,143 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import assert from 'node:assert/strict'; | ||
| import { describe, it } from 'node:test'; | ||
| import { | ||
| createWindowsMaximizeRendererSync, | ||
| type MaximizedRendererSyncWindow, | ||
| } from '../windows-maximize-renderer-sync.js'; | ||
|
|
||
| function createFixture() { | ||
| const calls: string[] = []; | ||
| const deferred: Array<() => void> = []; | ||
| let destroyed = false; | ||
| let maximized = true; | ||
| let webContentsDestroyed = false; | ||
| const contentView = {}; | ||
| const window: MaximizedRendererSyncWindow<object> = { | ||
| contentView, | ||
| webContents: { | ||
| isDestroyed: () => webContentsDestroyed, | ||
| invalidate: () => calls.push('invalidate'), | ||
| }, | ||
| isDestroyed: () => destroyed, | ||
| isMaximized: () => maximized, | ||
| setContentView: (view) => { | ||
| assert.equal(view, contentView); | ||
| calls.push('layout'); | ||
| }, | ||
| }; | ||
|
|
||
| return { | ||
| calls, | ||
| deferred, | ||
| window, | ||
| defer: (callback: () => void) => deferred.push(callback), | ||
| setDestroyed: (value: boolean) => { destroyed = value; }, | ||
| setMaximized: (value: boolean) => { maximized = value; }, | ||
| setWebContentsDestroyed: (value: boolean) => { webContentsDestroyed = value; }, | ||
| }; | ||
| } | ||
|
|
||
| describe('Windows maximize renderer sync', () => { | ||
| it('defers one root layout and repaint for a maximized Windows window', () => { | ||
| const fixture = createFixture(); | ||
| const schedule = createWindowsMaximizeRendererSync(fixture.window, { | ||
| platform: 'win32', | ||
| defer: fixture.defer, | ||
| }); | ||
|
|
||
| schedule(); | ||
| schedule(); | ||
| assert.equal(fixture.deferred.length, 1); | ||
| assert.deepEqual(fixture.calls, []); | ||
|
|
||
| fixture.deferred.shift()?.(); | ||
| assert.deepEqual(fixture.calls, ['layout', 'invalidate']); | ||
| }); | ||
|
|
||
| it('does nothing on non-Windows platforms', () => { | ||
| const fixture = createFixture(); | ||
| const schedule = createWindowsMaximizeRendererSync(fixture.window, { | ||
| platform: 'darwin', | ||
| defer: fixture.defer, | ||
| }); | ||
|
|
||
| schedule(); | ||
| assert.equal(fixture.deferred.length, 0); | ||
| assert.deepEqual(fixture.calls, []); | ||
| }); | ||
|
|
||
| it('drops deferred work when the window leaves maximized state or is destroyed', () => { | ||
| const restored = createFixture(); | ||
| const scheduleRestored = createWindowsMaximizeRendererSync(restored.window, { | ||
| platform: 'win32', | ||
| defer: restored.defer, | ||
| }); | ||
| scheduleRestored(); | ||
| restored.setMaximized(false); | ||
| restored.deferred.shift()?.(); | ||
|
|
||
| const destroyed = createFixture(); | ||
| const scheduleDestroyed = createWindowsMaximizeRendererSync(destroyed.window, { | ||
| platform: 'win32', | ||
| defer: destroyed.defer, | ||
| }); | ||
| scheduleDestroyed(); | ||
| destroyed.setDestroyed(true); | ||
| destroyed.deferred.shift()?.(); | ||
|
|
||
| assert.deepEqual(restored.calls, []); | ||
| assert.deepEqual(destroyed.calls, []); | ||
| }); | ||
|
|
||
| it('does not touch a destroyed WebContents', () => { | ||
| const fixture = createFixture(); | ||
| const schedule = createWindowsMaximizeRendererSync(fixture.window, { | ||
| platform: 'win32', | ||
| defer: fixture.defer, | ||
| }); | ||
|
|
||
| schedule(); | ||
| fixture.setWebContentsDestroyed(true); | ||
| fixture.deferred.shift()?.(); | ||
|
|
||
| assert.deepEqual(fixture.calls, []); | ||
| }); | ||
|
|
||
| it('reports a native layout failure without stranding future syncs', () => { | ||
| const fixture = createFixture(); | ||
| const failure = new Error('native layout failed'); | ||
| const errors: unknown[] = []; | ||
| fixture.window.setContentView = () => { throw failure; }; | ||
| const schedule = createWindowsMaximizeRendererSync(fixture.window, { | ||
| platform: 'win32', | ||
| defer: fixture.defer, | ||
| reportError: (error) => errors.push(error), | ||
| }); | ||
|
|
||
| schedule(); | ||
| assert.doesNotThrow(() => fixture.deferred.shift()?.()); | ||
| assert.deepEqual(errors, [failure]); | ||
|
|
||
| schedule(); | ||
| assert.equal(fixture.deferred.length, 1); | ||
| }); | ||
| }); | ||
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,77 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| export interface MaximizedRendererSyncWindow<ContentView = unknown> { | ||
| readonly contentView: ContentView; | ||
| readonly webContents: { | ||
| isDestroyed(): boolean; | ||
| invalidate(): void; | ||
| }; | ||
| isDestroyed(): boolean; | ||
| isMaximized(): boolean; | ||
| setContentView(view: ContentView): void; | ||
| } | ||
|
|
||
| interface MaximizedRendererSyncOptions { | ||
| platform?: NodeJS.Platform; | ||
| defer?: (callback: () => void) => void; | ||
| reportError?: (error: unknown) => void; | ||
| } | ||
|
|
||
| /** | ||
| * Re-runs Electron's root view layout after a native Windows maximize. | ||
| * | ||
| * Electron's BrowserWindow WebContentsView and the public contentView are | ||
| * siblings under one default-fill root view. Re-applying the same contentView | ||
| * makes Electron invalidate and immediately lay out that root without changing | ||
| * the native window bounds or its restored bounds. The repaint then covers the | ||
| * newly maximized client area. | ||
| */ | ||
| export function createWindowsMaximizeRendererSync<ContentView>( | ||
| window: MaximizedRendererSyncWindow<ContentView>, | ||
| options: MaximizedRendererSyncOptions = {}, | ||
| ): () => void { | ||
| const platform = options.platform ?? process.platform; | ||
| const defer = options.defer ?? setImmediate; | ||
| const reportError = options.reportError ?? ((error: unknown) => { | ||
| console.warn('[desktop] Windows maximize renderer sync failed:', error); | ||
| }); | ||
| let pending = false; | ||
|
|
||
| return () => { | ||
| if (platform !== 'win32' || pending) return; | ||
| if (window.isDestroyed() || !window.isMaximized()) return; | ||
| pending = true; | ||
|
|
||
| defer(() => { | ||
|
1625567290 marked this conversation as resolved.
|
||
| pending = false; | ||
| try { | ||
| if (window.isDestroyed() || !window.isMaximized()) return; | ||
| if (window.webContents.isDestroyed()) return; | ||
|
|
||
| window.setContentView(window.contentView); | ||
| window.webContents.invalidate(); | ||
| } catch (error) { | ||
| // A best-effort layout correction must not terminate the main process | ||
| // if Electron tears down the native window between the guards and call. | ||
| reportError(error); | ||
| } | ||
| }); | ||
| }; | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.