refactor: rename TopLevelWindow to BaseWindow (#24305)

This commit is contained in:
Cheng Zhao 2020-06-29 16:06:20 +09:00 committed by GitHub
parent 80e5007c47
commit ef3579eae3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 433 additions and 451 deletions

View file

@ -1,10 +1,10 @@
import { EventEmitter } from 'events';
import type { TopLevelWindow as TLWT } from 'electron';
const { TopLevelWindow } = process._linkedBinding('electron_browser_top_level_window') as { TopLevelWindow: typeof TLWT };
import type { BaseWindow as TLWT } from 'electron';
const { BaseWindow } = process._linkedBinding('electron_browser_base_window') as { BaseWindow: typeof TLWT };
Object.setPrototypeOf(TopLevelWindow.prototype, EventEmitter.prototype);
Object.setPrototypeOf(BaseWindow.prototype, EventEmitter.prototype);
(TopLevelWindow.prototype as any)._init = function () {
(BaseWindow.prototype as any)._init = function () {
// Avoid recursive require.
const { app } = require('electron');
@ -17,88 +17,88 @@ Object.setPrototypeOf(TopLevelWindow.prototype, EventEmitter.prototype);
// Properties
Object.defineProperty(TopLevelWindow.prototype, 'autoHideMenuBar', {
Object.defineProperty(BaseWindow.prototype, 'autoHideMenuBar', {
get: function () { return this.isMenuBarAutoHide(); },
set: function (autoHide) { this.setAutoHideMenuBar(autoHide); }
});
Object.defineProperty(TopLevelWindow.prototype, 'visibleOnAllWorkspaces', {
Object.defineProperty(BaseWindow.prototype, 'visibleOnAllWorkspaces', {
get: function () { return this.isVisibleOnAllWorkspaces(); },
set: function (visible) { this.setVisibleOnAllWorkspaces(visible); }
});
Object.defineProperty(TopLevelWindow.prototype, 'fullScreen', {
Object.defineProperty(BaseWindow.prototype, 'fullScreen', {
get: function () { return this.isFullScreen(); },
set: function (full) { this.setFullScreen(full); }
});
Object.defineProperty(TopLevelWindow.prototype, 'simpleFullScreen', {
Object.defineProperty(BaseWindow.prototype, 'simpleFullScreen', {
get: function () { return this.isSimpleFullScreen(); },
set: function (simple) { this.setSimpleFullScreen(simple); }
});
Object.defineProperty(TopLevelWindow.prototype, 'kiosk', {
Object.defineProperty(BaseWindow.prototype, 'kiosk', {
get: function () { return this.isKiosk(); },
set: function (kiosk) { this.setKiosk(kiosk); }
});
Object.defineProperty(TopLevelWindow.prototype, 'documentEdited', {
Object.defineProperty(BaseWindow.prototype, 'documentEdited', {
get: function () { return this.isFullscreen(); },
set: function (edited) { this.setDocumentEdited(edited); }
});
Object.defineProperty(TopLevelWindow.prototype, 'shadow', {
Object.defineProperty(BaseWindow.prototype, 'shadow', {
get: function () { return this.hasShadow(); },
set: function (shadow) { this.setHasShadow(shadow); }
});
Object.defineProperty(TopLevelWindow.prototype, 'representedFilename', {
Object.defineProperty(BaseWindow.prototype, 'representedFilename', {
get: function () { return this.getRepresentedFilename(); },
set: function (filename) { this.setRepresentedFilename(filename); }
});
Object.defineProperty(TopLevelWindow.prototype, 'minimizable', {
Object.defineProperty(BaseWindow.prototype, 'minimizable', {
get: function () { return this.isMinimizable(); },
set: function (min) { this.setMinimizable(min); }
});
Object.defineProperty(TopLevelWindow.prototype, 'title', {
Object.defineProperty(BaseWindow.prototype, 'title', {
get: function () { return this.getTitle(); },
set: function (title) { this.setTitle(title); }
});
Object.defineProperty(TopLevelWindow.prototype, 'maximizable', {
Object.defineProperty(BaseWindow.prototype, 'maximizable', {
get: function () { return this.isMaximizable(); },
set: function (max) { this.setMaximizable(max); }
});
Object.defineProperty(TopLevelWindow.prototype, 'resizable', {
Object.defineProperty(BaseWindow.prototype, 'resizable', {
get: function () { return this.isResizable(); },
set: function (res) { this.setResizable(res); }
});
Object.defineProperty(TopLevelWindow.prototype, 'menuBarVisible', {
Object.defineProperty(BaseWindow.prototype, 'menuBarVisible', {
get: function () { return this.isMenuBarVisible(); },
set: function (visible) { this.setMenuBarVisibility(visible); }
});
Object.defineProperty(TopLevelWindow.prototype, 'fullScreenable', {
Object.defineProperty(BaseWindow.prototype, 'fullScreenable', {
get: function () { return this.isFullScreenable(); },
set: function (full) { this.setFullScreenable(full); }
});
Object.defineProperty(TopLevelWindow.prototype, 'closable', {
Object.defineProperty(BaseWindow.prototype, 'closable', {
get: function () { return this.isClosable(); },
set: function (close) { this.setClosable(close); }
});
Object.defineProperty(TopLevelWindow.prototype, 'movable', {
Object.defineProperty(BaseWindow.prototype, 'movable', {
get: function () { return this.isMovable(); },
set: function (move) { this.setMovable(move); }
});
TopLevelWindow.getFocusedWindow = () => {
return TopLevelWindow.getAllWindows().find((win) => win.isFocused());
BaseWindow.getFocusedWindow = () => {
return BaseWindow.getAllWindows().find((win) => win.isFocused());
};
module.exports = TopLevelWindow;
module.exports = BaseWindow;

View file

@ -1,12 +1,12 @@
import { TopLevelWindow, WebContents, Event, BrowserView, TouchBar } from 'electron';
import { BaseWindow, WebContents, Event, BrowserView, TouchBar } from 'electron';
import type { BrowserWindow as BWT } from 'electron';
const { BrowserWindow } = process._linkedBinding('electron_browser_window') as { BrowserWindow: typeof BWT };
Object.setPrototypeOf(BrowserWindow.prototype, TopLevelWindow.prototype);
Object.setPrototypeOf(BrowserWindow.prototype, BaseWindow.prototype);
(BrowserWindow.prototype as any)._init = function (this: BWT) {
// Call parent class's _init.
(TopLevelWindow.prototype as any)._init.call(this);
(BaseWindow.prototype as any)._init.call(this);
// Avoid recursive require.
const { app } = require('electron');
@ -74,12 +74,12 @@ const isBrowserWindow = (win: any) => {
};
BrowserWindow.fromId = (id: number) => {
const win = TopLevelWindow.fromId(id);
const win = BaseWindow.fromId(id);
return isBrowserWindow(win) ? win as any as BWT : null;
};
BrowserWindow.getAllWindows = () => {
return TopLevelWindow.getAllWindows().filter(isBrowserWindow) as any[] as BWT[];
return BaseWindow.getAllWindows().filter(isBrowserWindow) as any[] as BWT[];
};
BrowserWindow.getFocusedWindow = () => {

View file

@ -1,6 +1,6 @@
'use strict';
const { TopLevelWindow, MenuItem, webContents } = require('electron');
const { BaseWindow, MenuItem, webContents } = require('electron');
const { sortMenuItems } = require('@electron/internal/browser/api/menu-utils');
const EventEmitter = require('events').EventEmitter;
const v8Util = process._linkedBinding('electron_common_v8_util');
@ -46,7 +46,7 @@ Menu.prototype._shouldRegisterAcceleratorForCommandId = function (id) {
Menu.prototype._executeCommand = function (event, id) {
const command = this.commandsMap[id];
if (!command) return;
command.click(event, TopLevelWindow.getFocusedWindow(), webContents.getFocusedWebContents());
command.click(event, BaseWindow.getFocusedWindow(), webContents.getFocusedWebContents());
};
Menu.prototype._menuWillShow = function () {
@ -72,14 +72,14 @@ Menu.prototype.popup = function (options = {}) {
if (typeof positioningItem !== 'number') positioningItem = -1;
// find which window to use
const wins = TopLevelWindow.getAllWindows();
const wins = BaseWindow.getAllWindows();
if (!wins || wins.indexOf(window) === -1) {
window = TopLevelWindow.getFocusedWindow();
window = BaseWindow.getFocusedWindow();
if (!window && wins && wins.length > 0) {
window = wins[0];
}
if (!window) {
throw new Error('Cannot open Menu without a TopLevelWindow present');
throw new Error('Cannot open Menu without a BaseWindow present');
}
}
@ -88,7 +88,7 @@ Menu.prototype.popup = function (options = {}) {
};
Menu.prototype.closePopup = function (window) {
if (window instanceof TopLevelWindow) {
if (window instanceof BaseWindow) {
this.closePopupAt(window.id);
} else {
// Passing -1 (invalid) would make closePopupAt close the all menu runners
@ -168,7 +168,7 @@ Menu.setApplicationMenu = function (menu) {
menu._callMenuWillShow();
bindings.setApplicationMenu(menu);
} else {
const windows = TopLevelWindow.getAllWindows();
const windows = BaseWindow.getAllWindows();
return windows.map(w => w.setMenu(menu));
}
};

View file

@ -4,6 +4,7 @@
export const browserModuleList: ElectronInternal.ModuleEntry[] = [
{ name: 'app', loader: () => require('./app') },
{ name: 'autoUpdater', loader: () => require('./auto-updater') },
{ name: 'BaseWindow', loader: () => require('./base-window') },
{ name: 'BrowserView', loader: () => require('./browser-view') },
{ name: 'BrowserWindow', loader: () => require('./browser-window') },
{ name: 'contentTracing', loader: () => require('./content-tracing') },
@ -25,7 +26,6 @@ export const browserModuleList: ElectronInternal.ModuleEntry[] = [
{ name: 'screen', loader: () => require('./screen') },
{ name: 'session', loader: () => require('./session') },
{ name: 'systemPreferences', loader: () => require('./system-preferences') },
{ name: 'TopLevelWindow', loader: () => require('./top-level-window') },
{ name: 'TouchBar', loader: () => require('./touch-bar') },
{ name: 'Tray', loader: () => require('./tray') },
{ name: 'View', loader: () => require('./view') },

View file

@ -7,6 +7,7 @@
export const browserModuleNames = [
'app',
'autoUpdater',
'BaseWindow',
'BrowserView',
'BrowserWindow',
'contentTracing',
@ -28,7 +29,6 @@ export const browserModuleNames = [
'screen',
'session',
'systemPreferences',
'TopLevelWindow',
'TouchBar',
'Tray',
'View',