Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved window management #118

Merged
merged 11 commits into from
Oct 30, 2024
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
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
Loading