Skip to content

Instantly share code, notes, and snippets.

@yuhki50
Last active October 1, 2021 00:44
Show Gist options
  • Save yuhki50/60b4be60b231e1f6f13a1bd7fc2f9e29 to your computer and use it in GitHub Desktop.
Save yuhki50/60b4be60b231e1f6f13a1bd7fc2f9e29 to your computer and use it in GitHub Desktop.
import Store from 'electron-store';
import path from 'path'; // <-- Add
import {BrowserWindow, BrowserWindowConstructorOptions, screen} from 'electron';
export default (
windowName: string,
options: BrowserWindowConstructorOptions
): BrowserWindow => {
const key = 'window-state';
const name = `window-state-${windowName}`;
const store = new Store({name});
const defaultSize = {
width: options.width,
height: options.height,
};
let state = {};
let win; // eslint-disable-line prefer-const
const restore = () => store.get(key, defaultSize);
const getCurrentPosition = () => {
const position = win.getPosition();
const size = win.getSize();
return {
x: position[0],
y: position[1],
width: size[0],
height: size[1],
};
};
const windowWithinBounds = (windowState, bounds) => {
return (
windowState.x >= bounds.x &&
windowState.y >= bounds.y &&
windowState.x + windowState.width <= bounds.x + bounds.width &&
windowState.y + windowState.height <= bounds.y + bounds.height
);
};
const resetToDefaults = () => {
const bounds = screen.getPrimaryDisplay().bounds;
return Object.assign({}, defaultSize, {
x: (bounds.width - defaultSize.width) / 2,
y: (bounds.height - defaultSize.height) / 2,
});
};
const ensureVisibleOnSomeDisplay = windowState => {
const visible = screen.getAllDisplays().some(display => {
return windowWithinBounds(windowState, display.bounds);
});
if (!visible) {
// Window is partially or fully not visible now.
// Reset it to safe defaults.
return resetToDefaults();
}
return windowState;
};
const saveState = () => {
if (!win.isMinimized() && !win.isMaximized()) {
Object.assign(state, getCurrentPosition());
}
store.set(key, state);
};
state = ensureVisibleOnSomeDisplay(restore());
const browserOptions: BrowserWindowConstructorOptions = {
...options,
...state,
webPreferences: {
// nodeIntegration: true, // <-- Comment out
// contextIsolation: false, // <-- Comment out
preload: path.join(__dirname, 'preload.js'), // <-- Add
...options.webPreferences,
},
};
win = new BrowserWindow(browserOptions);
win.on('close', saveState);
return win;
};
module.exports = {
webpack: (config, { isServer }) => {
if (!isServer) {
// config.target = 'electron-renderer'; // <--- Comment out
config.target = 'web'; // <-- Add
config.node = { // <-- Add
__dirname: true, // <-- Add
}; // <-- Add
}
return config;
},
};
const path = require('path');
module.exports = {
webpack: config => {
config.entry.preload = path.join(__dirname, 'main/preload.ts');
return config;
},
};
// New file
import {contextBridge, ipcRenderer} from 'electron';
contextBridge.exposeInMainWorld('electron', {
doThing: () => ipcRenderer.send('do-a-thing'),
});
@yuhki50
Copy link
Author

yuhki50 commented Oct 1, 2021

Nextron + preload.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment