import { EventEmitter } from 'node:events';
import path from 'node:path';
function makeCase(name, expected, actual) {
return {
name,
passed: JSON.stringify(actual) === JSON.stringify(expected),
expected,
actual,
};
}
class SimulatedBrowserWindow extends EventEmitter {
constructor(opts = {}) {
super();
this.minWidth = opts.minWidth ?? 0;
this.minHeight = opts.minHeight ?? 0;
this.bounds = {
x: 0,
y: 0,
width: Math.max(opts.width ?? 800, this.minWidth),
height: Math.max(opts.height ?? 600, this.minHeight),
};
this.resizable = opts.resizable ?? true;
this.fullscreenable = opts.fullscreenable ?? true;
this.fullscreen = opts.fullscreen ?? false;
this.frame = opts.frame ?? true;
this.transparent = opts.transparent ?? false;
this.alwaysOnTop = opts.alwaysOnTop ?? false;
this.visible = opts.show ?? true;
this.state = 'normal';
this.closed = false;
this.parent = opts.parent ?? null;
this.modal = opts.modal ?? false;
this.children = new Set();
this.loadedTarget = null;
this.webContents = new EventEmitter();
if (this.parent) {
this.parent.children.add(this);
}
}
loadFile(filePath) {
this.loadedTarget = { mode: 'file', target: filePath };
}
loadURL(url) {
this.loadedTarget = { mode: 'url', target: url };
}
show() {
this.visible = true;
}
hide() {
this.visible = false;
}
maximize() {
this.state = 'maximized';
this.emit('resize', this.bounds);
}
minimize() {
this.state = 'minimized';
}
restore() {
this.state = 'normal';
}
setBounds(next) {
this.bounds = {
x: next.x,
y: next.y,
width: Math.max(next.width, this.minWidth),
height: Math.max(next.height, this.minHeight),
};
this.emit('move', { x: this.bounds.x, y: this.bounds.y });
this.emit('resize', { width: this.bounds.width, height: this.bounds.height });
}
close() {
let prevented = false;
this.emit('close', {
preventDefault() {
prevented = true;
},
});
if (prevented) return false;
for (const child of [...this.children]) {
child.close();
}
this.visible = false;
this.closed = true;
this.emit('closed');
return true;
}
}
const cases = [];
// Case 1: Window options, bounds clamping, state transitions & close interception
{
const win = new SimulatedBrowserWindow({
width: 1200,
height: 800,
minWidth: 640,
minHeight: 480,
show: false,
resizable: true,
fullscreenable: true,
});
const events = [];
win.on('resize', () => events.push('resize'));
win.on('move', () => events.push('move'));
win.on('focus', () => events.push('focus'));
win.on('blur', () => events.push('blur'));
win.on('close', (e) => {
e.preventDefault();
win.hide();
events.push('close-intercepted');
});
win.show();
win.maximize();
win.minimize();
win.restore();
win.setBounds({ x: 100, y: 100, width: 800, height: 600 });
win.emit('focus');
win.emit('blur');
const actuallyClosed = win.close();
const expected = {
bounds: { x: 100, y: 100, width: 800, height: 600 },
state: 'normal',
visibleAfterCloseIntercept: false,
actuallyClosed: false,
events: ['resize', 'move', 'resize', 'focus', 'blur', 'close-intercepted'],
};
const actual = {
bounds: win.bounds,
state: win.state,
visibleAfterCloseIntercept: win.visible,
actuallyClosed,
events,
};
cases.push(makeCase('window_options_bounds_and_events', expected, actual));
}
// Case 2: Frameless window CSS drag regions & local/dist/dev URL loading
{
const frameless = new SimulatedBrowserWindow({
width: 900,
height: 600,
frame: false,
transparent: true,
alwaysOnTop: true,
});
const titlebarCss = '.titlebar { -webkit-app-region: drag; } .titlebar button { -webkit-app-region: no-drag; }';
frameless.loadFile('index.html');
const localLoad = { ...frameless.loadedTarget };
const distFile = path.posix.join('/app', 'dist/index.html');
frameless.loadFile(distFile);
const distLoad = { ...frameless.loadedTarget };
frameless.loadURL('http://localhost:5173');
const devLoad = { ...frameless.loadedTarget };
frameless.loadURL('https://example.com');
const remoteLoad = { ...frameless.loadedTarget };
const expected = {
frame: false,
transparent: true,
alwaysOnTop: true,
hasDragRegion: true,
hasNoDragForButtons: true,
localLoad: { mode: 'file', target: 'index.html' },
distLoad: { mode: 'file', target: '/app/dist/index.html' },
devLoad: { mode: 'url', target: 'http://localhost:5173' },
remoteLoad: { mode: 'url', target: 'https://example.com' },
};
const actual = {
frame: frameless.frame,
transparent: frameless.transparent,
alwaysOnTop: frameless.alwaysOnTop,
hasDragRegion: titlebarCss.includes('-webkit-app-region: drag'),
hasNoDragForButtons: titlebarCss.includes('-webkit-app-region: no-drag'),
localLoad,
distLoad,
devLoad,
remoteLoad,
};
cases.push(makeCase('frameless_titlebar_and_content_loading', expected, actual));
}
// Case 3: Parent-child modal window lifecycle & main-to-child IPC push
{
const parent = new SimulatedBrowserWindow({ width: 1200, height: 800 });
const child = new SimulatedBrowserWindow({ parent, modal: true, width: 400, height: 300 });
let childReceived = null;
child.webContents.on('update-data', (data) => {
childReceived = data;
});
child.webContents.emit('update-data', { theme: 'dark', count: 3 });
const parentClosed = parent.close();
const expected = {
childModal: true,
childReceived: { theme: 'dark', count: 3 },
parentClosed: true,
childAutoClosedWithParent: true,
};
const actual = {
childModal: child.modal,
childReceived,
parentClosed,
childAutoClosedWithParent: child.closed,
};
cases.push(makeCase('parent_child_modal_and_ipc_push', expected, actual));
}
// Case 4: Splash screen -> ready-to-show handoff & multi-window demo flow
{
const timeline = [];
const splash = new SimulatedBrowserWindow({
width: 400,
height: 300,
frame: false,
transparent: true,
alwaysOnTop: true,
});
splash.loadFile('splash.html');
timeline.push(`splash-shown:${splash.visible}`);
let mainWindow = new SimulatedBrowserWindow({ width: 1200, height: 800, show: false });
mainWindow.loadFile('index.html');
timeline.push(`main-hidden-before-ready:${!mainWindow.visible}`);
mainWindow.once('ready-to-show', () => {
splash.close();
timeline.push(`splash-closed:${splash.closed}`);
mainWindow.show();
timeline.push(`main-shown:${mainWindow.visible}`);
});
mainWindow.on('closed', () => {
mainWindow = null;
});
mainWindow.emit('ready-to-show');
const appInfo = { name: 'multi-window-demo', version: '1.0.0' };
mainWindow.close();
const expected = {
timeline: [
'splash-shown:true',
'main-hidden-before-ready:true',
'splash-closed:true',
'main-shown:true',
],
appInfo: { name: 'multi-window-demo', version: '1.0.0' },
mainRefClearedOnClosed: true,
};
const actual = {
timeline,
appInfo,
mainRefClearedOnClosed: mainWindow === null,
};
cases.push(makeCase('splash_ready_to_show_anti_flicker', expected, actual));
}
console.log(JSON.stringify({ version: process.version, cases }));
本例需在 Windows x64、Node.js v24.22.3 本地运行环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、Node.js v24.22.3 本地运行环境。
入口文件:projects/electron-windows-lab/explore-windows.mjs
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-windows.mjs,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、Node.js v24.22.3 本地运行环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联21章) · 含当前章节或实例 · mbskycom202612a1081.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1099.zip