mirror of
https://github.com/usebruno/bruno.git
synced 2025-01-11 00:18:46 +01:00
Merge pull request #452 from jarne/feature/persist-window-state
Persist window state (size, position)
This commit is contained in:
commit
c222bf47c3
@ -9,6 +9,7 @@ const LastOpenedCollections = require('./store/last-opened-collections');
|
|||||||
const registerNetworkIpc = require('./ipc/network');
|
const registerNetworkIpc = require('./ipc/network');
|
||||||
const registerCollectionsIpc = require('./ipc/collection');
|
const registerCollectionsIpc = require('./ipc/collection');
|
||||||
const Watcher = require('./app/watcher');
|
const Watcher = require('./app/watcher');
|
||||||
|
const { loadWindowState, saveWindowState } = require('./utils/window');
|
||||||
|
|
||||||
const lastOpenedCollections = new LastOpenedCollections();
|
const lastOpenedCollections = new LastOpenedCollections();
|
||||||
|
|
||||||
@ -27,9 +28,13 @@ let watcher;
|
|||||||
|
|
||||||
// Prepare the renderer once the app is ready
|
// Prepare the renderer once the app is ready
|
||||||
app.on('ready', async () => {
|
app.on('ready', async () => {
|
||||||
|
const { x, y, width, height } = loadWindowState();
|
||||||
|
|
||||||
mainWindow = new BrowserWindow({
|
mainWindow = new BrowserWindow({
|
||||||
width: 1280,
|
x,
|
||||||
height: 768,
|
y,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
webPreferences: {
|
webPreferences: {
|
||||||
nodeIntegration: true,
|
nodeIntegration: true,
|
||||||
contextIsolation: true,
|
contextIsolation: true,
|
||||||
@ -54,6 +59,9 @@ app.on('ready', async () => {
|
|||||||
mainWindow.loadURL(url);
|
mainWindow.loadURL(url);
|
||||||
watcher = new Watcher();
|
watcher = new Watcher();
|
||||||
|
|
||||||
|
mainWindow.on('resize', () => saveWindowState(mainWindow));
|
||||||
|
mainWindow.on('move', () => saveWindowState(mainWindow));
|
||||||
|
|
||||||
mainWindow.webContents.on('new-window', function (e, url) {
|
mainWindow.webContents.on('new-window', function (e, url) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
require('electron').shell.openExternal(url);
|
require('electron').shell.openExternal(url);
|
||||||
|
21
packages/bruno-electron/src/store/window-state.js
Normal file
21
packages/bruno-electron/src/store/window-state.js
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
const _ = require('lodash');
|
||||||
|
const Store = require('electron-store');
|
||||||
|
|
||||||
|
class WindowStateStore {
|
||||||
|
constructor() {
|
||||||
|
this.store = new Store({
|
||||||
|
name: 'window-state',
|
||||||
|
clearInvalidConfig: true
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
getBounds() {
|
||||||
|
return this.store.get('window-bounds') || {};
|
||||||
|
}
|
||||||
|
|
||||||
|
setBounds(bounds) {
|
||||||
|
this.store.set('window-bounds', bounds);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = WindowStateStore;
|
53
packages/bruno-electron/src/utils/window.js
Normal file
53
packages/bruno-electron/src/utils/window.js
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
const { screen } = require('electron');
|
||||||
|
const WindowStateStore = require('../store/window-state');
|
||||||
|
|
||||||
|
const windowStateStore = new WindowStateStore();
|
||||||
|
|
||||||
|
const DEFAULT_WINDOW_WIDTH = 1280;
|
||||||
|
const DEFAULT_WINDOW_HEIGHT = 768;
|
||||||
|
|
||||||
|
const loadWindowState = () => {
|
||||||
|
const bounds = windowStateStore.getBounds();
|
||||||
|
|
||||||
|
const positionValid = isPositionValid(bounds);
|
||||||
|
const sizeValid = isSizeValid(bounds);
|
||||||
|
|
||||||
|
return {
|
||||||
|
x: bounds.x && positionValid ? bounds.x : undefined,
|
||||||
|
y: bounds.y && positionValid ? bounds.y : undefined,
|
||||||
|
width: bounds.width && sizeValid ? bounds.width : DEFAULT_WINDOW_WIDTH,
|
||||||
|
height: bounds.height && sizeValid ? bounds.height : DEFAULT_WINDOW_HEIGHT
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const saveWindowState = (window) => {
|
||||||
|
const bounds = window.getBounds();
|
||||||
|
|
||||||
|
windowStateStore.setBounds(bounds);
|
||||||
|
};
|
||||||
|
|
||||||
|
const isPositionValid = (bounds) => {
|
||||||
|
const area = getArea(bounds);
|
||||||
|
|
||||||
|
return (
|
||||||
|
bounds.x >= area.x &&
|
||||||
|
bounds.y >= area.y &&
|
||||||
|
bounds.x + bounds.width <= area.x + area.width &&
|
||||||
|
bounds.y + bounds.height <= area.y + area.height
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const isSizeValid = (bounds) => {
|
||||||
|
const area = getArea(bounds);
|
||||||
|
|
||||||
|
return bounds.width <= area.width && bounds.height <= area.height;
|
||||||
|
};
|
||||||
|
|
||||||
|
const getArea = (bounds) => {
|
||||||
|
return screen.getDisplayMatching(bounds).workArea;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
loadWindowState,
|
||||||
|
saveWindowState
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user