Skip to content

Commit

Permalink
Improved window management (#118)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
simonhamp authored Oct 30, 2024
1 parent 5ccc92c commit 9f2443d
Show file tree
Hide file tree
Showing 4 changed files with 1,319 additions and 3,958 deletions.
104 changes: 85 additions & 19 deletions resources/js/electron-plugin/dist/server/api/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,36 @@ router.post('/resize', (req, res) => {
(_a = state.windows[id]) === null || _a === void 0 ? void 0 : _a.setSize(parseInt(width), parseInt(height));
res.sendStatus(200);
});
router.post('/title', (req, res) => {
var _a;
const { id, title } = req.body;
(_a = state.windows[id]) === null || _a === void 0 ? void 0 : _a.setTitle(title);
res.sendStatus(200);
});
router.post('/url', (req, res) => {
var _a;
const { id, url } = req.body;
(_a = state.windows[id]) === null || _a === void 0 ? void 0 : _a.loadURL(appendWindowIdToUrl(url, id));
res.sendStatus(200);
});
router.post('/closable', (req, res) => {
var _a;
const { id, closable } = req.body;
(_a = state.windows[id]) === null || _a === void 0 ? void 0 : _a.setClosable(closable);
res.sendStatus(200);
});
router.post('/show-dev-tools', (req, res) => {
var _a;
const { id } = req.body;
(_a = state.windows[id]) === null || _a === void 0 ? void 0 : _a.webContents.openDevTools();
res.sendStatus(200);
});
router.post('/hide-dev-tools', (req, res) => {
var _a;
const { id } = req.body;
(_a = state.windows[id]) === null || _a === void 0 ? void 0 : _a.webContents.closeDevTools();
res.sendStatus(200);
});
router.post('/position', (req, res) => {
var _a;
const { id, x, y, animate } = req.body;
Expand Down Expand Up @@ -50,33 +80,73 @@ router.post('/hide', (req, res) => {
}
return res.sendStatus(200);
});
router.post('/always-on-top', (req, res) => {
var _a;
const { id, alwaysOnTop } = req.body;
(_a = state.windows[id]) === null || _a === void 0 ? void 0 : _a.setAlwaysOnTop(alwaysOnTop);
res.sendStatus(200);
});
router.get('/current', (req, res) => {
const currentWindow = Object.values(state.windows).find(window => window.id === BrowserWindow.getFocusedWindow().id);
const id = Object.keys(state.windows).find(key => state.windows[key] === currentWindow);
res.json({
res.json(getWindowData(id));
});
router.get('/get/:id', (req, res) => {
const { id } = req.params;
if (state.windows[id] === undefined) {
res.sendStatus(404);
return;
}
res.json(getWindowData(id));
});
function appendWindowIdToUrl(url, id) {
return url + (url.indexOf('?') === -1 ? '?' : '&') + '_windowId=' + id;
}
function getWindowData(id) {
const currentWindow = state.windows[id];
if (state.windows[id] === undefined) {
throw `Window [${id}] not found`;
}
return {
id: id,
x: currentWindow.getPosition()[0],
y: currentWindow.getPosition()[1],
width: currentWindow.getSize()[0],
height: currentWindow.getSize()[1],
title: currentWindow.getTitle(),
alwaysOnTop: currentWindow.isAlwaysOnTop(),
});
});
router.post('/always-on-top', (req, res) => {
var _a;
const { id, alwaysOnTop } = req.body;
(_a = state.windows[id]) === null || _a === void 0 ? void 0 : _a.setAlwaysOnTop(alwaysOnTop);
res.sendStatus(200);
});
url: currentWindow.webContents.getURL(),
autoHideMenuBar: currentWindow.isMenuBarAutoHide(),
fullscreen: currentWindow.isFullScreen(),
fullscreenable: currentWindow.isFullScreenable(),
kiosk: currentWindow.isKiosk(),
devToolsOpen: currentWindow.webContents.isDevToolsOpened(),
resizable: currentWindow.isResizable(),
movable: currentWindow.isMovable(),
minimizable: currentWindow.isMinimizable(),
maximizable: currentWindow.isMaximizable(),
closable: currentWindow.isClosable(),
focusable: currentWindow.isFocusable(),
focused: currentWindow.isFocused(),
hasShadow: currentWindow.hasShadow(),
};
}
router.post('/open', (req, res) => {
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;
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;
if (state.windows[id]) {
state.windows[id].show();
state.windows[id].focus();
return res.sendStatus(200);
}
let preloadPath = join(__dirname, '../../electron-plugin/dist/preload/index.js');
const defaultWebPreferences = {
backgroundThrottling: false,
spellcheck: false,
preload: preloadPath,
sandbox: false,
contextIsolation: false,
nodeIntegration: true,
};
let windowState = undefined;
if (req.body.rememberState === true) {
windowState = windowStateKeeper({
Expand All @@ -97,14 +167,7 @@ router.post('/open', (req, res) => {
trafficLightPosition,
vibrancy,
focusable,
autoHideMenuBar }, (process.platform === 'linux' ? { icon: state.icon } : {})), { webPreferences: {
backgroundThrottling: false,
spellcheck: false,
preload: preloadPath,
sandbox: false,
contextIsolation: false,
nodeIntegration: true,
}, fullscreen,
autoHideMenuBar }, (process.platform === 'linux' ? { icon: state.icon } : {})), { webPreferences: Object.assign(Object.assign({}, webPreferences), defaultWebPreferences), fullscreen,
fullscreenable,
kiosk }));
if ((process.env.NODE_ENV === 'development' || showDevTools === true) && showDevTools !== false) {
Expand Down Expand Up @@ -168,11 +231,14 @@ router.post('/open', (req, res) => {
payload: [id]
});
});
url += (url.indexOf('?') === -1 ? '?' : '&') + '_windowId=' + id;
url = appendWindowIdToUrl(url, id);
window.loadURL(url);
window.webContents.on('did-finish-load', () => {
window.show();
});
window.webContents.on('did-fail-load', (event) => {
console.error('failed to open window...', event);
});
state.windows[id] = window;
res.sendStatus(200);
});
Expand Down
Loading

0 comments on commit 9f2443d

Please sign in to comment.