Skip to content

Commit 9f2443d

Browse files
authored
Improved window management (#118)
* Support getting a specific window * code style * code style * build * Dump an error when the window fails to load * bump deps * build * Add closable * Add webPreferences support * Support dynamic devTools toggling * Extract method
1 parent 5ccc92c commit 9f2443d

File tree

4 files changed

+1319
-3958
lines changed

4 files changed

+1319
-3958
lines changed

resources/js/electron-plugin/dist/server/api/window.js

Lines changed: 85 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,36 @@ router.post('/resize', (req, res) => {
2323
(_a = state.windows[id]) === null || _a === void 0 ? void 0 : _a.setSize(parseInt(width), parseInt(height));
2424
res.sendStatus(200);
2525
});
26+
router.post('/title', (req, res) => {
27+
var _a;
28+
const { id, title } = req.body;
29+
(_a = state.windows[id]) === null || _a === void 0 ? void 0 : _a.setTitle(title);
30+
res.sendStatus(200);
31+
});
32+
router.post('/url', (req, res) => {
33+
var _a;
34+
const { id, url } = req.body;
35+
(_a = state.windows[id]) === null || _a === void 0 ? void 0 : _a.loadURL(appendWindowIdToUrl(url, id));
36+
res.sendStatus(200);
37+
});
38+
router.post('/closable', (req, res) => {
39+
var _a;
40+
const { id, closable } = req.body;
41+
(_a = state.windows[id]) === null || _a === void 0 ? void 0 : _a.setClosable(closable);
42+
res.sendStatus(200);
43+
});
44+
router.post('/show-dev-tools', (req, res) => {
45+
var _a;
46+
const { id } = req.body;
47+
(_a = state.windows[id]) === null || _a === void 0 ? void 0 : _a.webContents.openDevTools();
48+
res.sendStatus(200);
49+
});
50+
router.post('/hide-dev-tools', (req, res) => {
51+
var _a;
52+
const { id } = req.body;
53+
(_a = state.windows[id]) === null || _a === void 0 ? void 0 : _a.webContents.closeDevTools();
54+
res.sendStatus(200);
55+
});
2656
router.post('/position', (req, res) => {
2757
var _a;
2858
const { id, x, y, animate } = req.body;
@@ -50,33 +80,73 @@ router.post('/hide', (req, res) => {
5080
}
5181
return res.sendStatus(200);
5282
});
83+
router.post('/always-on-top', (req, res) => {
84+
var _a;
85+
const { id, alwaysOnTop } = req.body;
86+
(_a = state.windows[id]) === null || _a === void 0 ? void 0 : _a.setAlwaysOnTop(alwaysOnTop);
87+
res.sendStatus(200);
88+
});
5389
router.get('/current', (req, res) => {
5490
const currentWindow = Object.values(state.windows).find(window => window.id === BrowserWindow.getFocusedWindow().id);
5591
const id = Object.keys(state.windows).find(key => state.windows[key] === currentWindow);
56-
res.json({
92+
res.json(getWindowData(id));
93+
});
94+
router.get('/get/:id', (req, res) => {
95+
const { id } = req.params;
96+
if (state.windows[id] === undefined) {
97+
res.sendStatus(404);
98+
return;
99+
}
100+
res.json(getWindowData(id));
101+
});
102+
function appendWindowIdToUrl(url, id) {
103+
return url + (url.indexOf('?') === -1 ? '?' : '&') + '_windowId=' + id;
104+
}
105+
function getWindowData(id) {
106+
const currentWindow = state.windows[id];
107+
if (state.windows[id] === undefined) {
108+
throw `Window [${id}] not found`;
109+
}
110+
return {
57111
id: id,
58112
x: currentWindow.getPosition()[0],
59113
y: currentWindow.getPosition()[1],
60114
width: currentWindow.getSize()[0],
61115
height: currentWindow.getSize()[1],
62116
title: currentWindow.getTitle(),
63117
alwaysOnTop: currentWindow.isAlwaysOnTop(),
64-
});
65-
});
66-
router.post('/always-on-top', (req, res) => {
67-
var _a;
68-
const { id, alwaysOnTop } = req.body;
69-
(_a = state.windows[id]) === null || _a === void 0 ? void 0 : _a.setAlwaysOnTop(alwaysOnTop);
70-
res.sendStatus(200);
71-
});
118+
url: currentWindow.webContents.getURL(),
119+
autoHideMenuBar: currentWindow.isMenuBarAutoHide(),
120+
fullscreen: currentWindow.isFullScreen(),
121+
fullscreenable: currentWindow.isFullScreenable(),
122+
kiosk: currentWindow.isKiosk(),
123+
devToolsOpen: currentWindow.webContents.isDevToolsOpened(),
124+
resizable: currentWindow.isResizable(),
125+
movable: currentWindow.isMovable(),
126+
minimizable: currentWindow.isMinimizable(),
127+
maximizable: currentWindow.isMaximizable(),
128+
closable: currentWindow.isClosable(),
129+
focusable: currentWindow.isFocusable(),
130+
focused: currentWindow.isFocused(),
131+
hasShadow: currentWindow.hasShadow(),
132+
};
133+
}
72134
router.post('/open', (req, res) => {
73-
let { id, x, y, frame, width, height, minWidth, minHeight, maxWidth, maxHeight, focusable, hasShadow, url, resizable, movable, minimizable, maximizable, closable, title, alwaysOnTop, titleBarStyle, trafficLightPosition, vibrancy, backgroundColor, transparency, showDevTools, fullscreen, fullscreenable, kiosk, autoHideMenuBar, } = req.body;
135+
let { id, x, y, frame, width, height, minWidth, minHeight, maxWidth, maxHeight, focusable, hasShadow, url, resizable, movable, minimizable, maximizable, closable, title, alwaysOnTop, titleBarStyle, trafficLightPosition, vibrancy, backgroundColor, transparency, showDevTools, fullscreen, fullscreenable, kiosk, autoHideMenuBar, webPreferences, } = req.body;
74136
if (state.windows[id]) {
75137
state.windows[id].show();
76138
state.windows[id].focus();
77139
return res.sendStatus(200);
78140
}
79141
let preloadPath = join(__dirname, '../../electron-plugin/dist/preload/index.js');
142+
const defaultWebPreferences = {
143+
backgroundThrottling: false,
144+
spellcheck: false,
145+
preload: preloadPath,
146+
sandbox: false,
147+
contextIsolation: false,
148+
nodeIntegration: true,
149+
};
80150
let windowState = undefined;
81151
if (req.body.rememberState === true) {
82152
windowState = windowStateKeeper({
@@ -97,14 +167,7 @@ router.post('/open', (req, res) => {
97167
trafficLightPosition,
98168
vibrancy,
99169
focusable,
100-
autoHideMenuBar }, (process.platform === 'linux' ? { icon: state.icon } : {})), { webPreferences: {
101-
backgroundThrottling: false,
102-
spellcheck: false,
103-
preload: preloadPath,
104-
sandbox: false,
105-
contextIsolation: false,
106-
nodeIntegration: true,
107-
}, fullscreen,
170+
autoHideMenuBar }, (process.platform === 'linux' ? { icon: state.icon } : {})), { webPreferences: Object.assign(Object.assign({}, webPreferences), defaultWebPreferences), fullscreen,
108171
fullscreenable,
109172
kiosk }));
110173
if ((process.env.NODE_ENV === 'development' || showDevTools === true) && showDevTools !== false) {
@@ -168,11 +231,14 @@ router.post('/open', (req, res) => {
168231
payload: [id]
169232
});
170233
});
171-
url += (url.indexOf('?') === -1 ? '?' : '&') + '_windowId=' + id;
234+
url = appendWindowIdToUrl(url, id);
172235
window.loadURL(url);
173236
window.webContents.on('did-finish-load', () => {
174237
window.show();
175238
});
239+
window.webContents.on('did-fail-load', (event) => {
240+
console.error('failed to open window...', event);
241+
});
176242
state.windows[id] = window;
177243
res.sendStatus(200);
178244
});

0 commit comments

Comments
 (0)