Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/release-windows-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ on:
# The packaged updater's feed behavior — and the boot wiring that hands
# MAKA_UPDATE_TEST_FEED to it — is only observable on this path.
- 'apps/desktop/src/main/app-update-service.ts'
- 'apps/desktop/src/main/main-window.ts'
- 'apps/desktop/src/main/runtime-host-boot.ts'
- 'packages/runtime-host/src/client/connect-or-spawn.ts'
- 'packages/runtime-host/src/client/launcher.ts'
- 'apps/desktop/src/main/windows-maximize-renderer-sync.ts'
- 'scripts/prepare-windows-upgrade-baseline.mjs'
- 'scripts/windows-upgrade-baseline.json'
- 'scripts/verify-packaged-app.mjs'
Expand Down
143 changes: 143 additions & 0 deletions apps/desktop/src/main/__tests__/windows-maximize-renderer-sync.test.ts
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']);
Comment thread
1625567290 marked this conversation as resolved.
});

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);
});
});
10 changes: 8 additions & 2 deletions apps/desktop/src/main/main-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { installMainWindowPermissionPolicy } from './main-window-permission-poli
import { observeMainRendererProcessGone } from './main-renderer-process-gone.js';
import { isThemePreference, toNativeThemeSource } from './theme-source.js';
import { createWindowRevealGate } from './window-reveal.js';
import { createWindowsMaximizeRendererSync } from './windows-maximize-renderer-sync.js';
import {
parseDesktopSessionResourceKey,
} from '../shared/runtime-host-identity.js';
Expand Down Expand Up @@ -468,9 +469,14 @@ export function createMainWindowController(deps: MainWindowControllerDeps): Main
void writeSavedBounds(workspaceRoot, next);
}, 400);
};
mainWindow.on('resize', scheduleSave);
const scheduleMaximizedRendererSync = createWindowsMaximizeRendererSync(mainWindow);
const handleWindowGeometryChange = (): void => {
scheduleSave();
scheduleMaximizedRendererSync();
};
mainWindow.on('resize', handleWindowGeometryChange);
mainWindow.on('move', scheduleSave);
mainWindow.on('maximize', scheduleSave);
mainWindow.on('maximize', handleWindowGeometryChange);
mainWindow.on('unmaximize', scheduleSave);
mainWindow.on('close', () => {
clearShowFallbackTimer();
Expand Down
77 changes: 77 additions & 0 deletions apps/desktop/src/main/windows-maximize-renderer-sync.ts
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(() => {
Comment thread
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);
}
});
};
}
Loading