This commit is contained in:
Andrew Gundersen 2021-03-20 09:50:53 -05:00
commit deb9f2f84c
7 changed files with 104 additions and 74 deletions

View file

@ -14,16 +14,17 @@ interface IpcRendererPayload {
let win: BrowserWindow | null;
// Util function to handle window close & minimize.
const navBarHandler = (_event, action: string): void => {
switch(action) {
case 'close':
if (win) win.close();
break;
case 'min':
if (win) win.minimize();
break;
// Called when a NavBar button is pressed.
const onNavBar = (_event: any, action: string): void => {
console.log("onNavBar")
if (win) {
if (action === "close") {
saveWindowState()
win.close()
} else {
win.minimize()
}
}
}
// Util function to render message on ipc-renderer event.
@ -35,34 +36,42 @@ const renderMessage = (payload: IpcRendererPayload): void => {
}
}
const onWindowSave = (_event, _s: string) => {
// Write a json with position and size of window.
const saveWindowState = () => {
if (win) {
const bounds = win.getBounds();
const state = JSON.stringify({w: bounds.width, h: bounds.height});
const position = win.getPosition();
fs.writeFile('windowstate.json', state, (err: Error) => {
const state = JSON.stringify(
{
w: bounds.width,
h: bounds.height,
x: position[0],
y: position[1]
}
);
fs.writeFile('windowState.json', state, (err) => {
if (err) throw err;
return;
});
}
}
// Do this on window mount.
const windowMount = (): void => {
const onWindowMount = (): void => {
backgroundMitt.emit('window-active', true);
// handle win nav-bar event.
ipcMain.removeHandler('nav-bar'); // avoid setting duplicate handlers
ipcMain.handle('nav-bar', navBarHandler);
ipcMain.removeHandler('window-save'); // avoid setting duplicate handlers
ipcMain.handle('window-save', onWindowSave);
// render messages through ipc-renderer.
ipcMain.handle('nav-bar', onNavBar);
// Gateway for messages to the frontend.
backgroundMitt.on('ipc-renderer', renderMessage);
}
// do this on window dismount.
const windowDismount = (): void => {
// Do this on window dismount (close).
const onWindowDismount = (): void => {
win = null;
backgroundMitt.emit('window-active', false);
}
@ -74,49 +83,48 @@ export async function createWindow(): Promise<void> {
// avoid creating duplicate windows.
if (win) resolve();
// read windowState from json.
const rawData = fs.readFileSync('windowState.json');
const windowState = JSON.parse(rawData);
// Load the saved window state.
const state = JSON.parse(fs.readFileSync('windowState.json').toString());
// Define the browser window.
win = new BrowserWindow({
width: windowState.w,
height: windowState.h,
width: state.w,
height: state.h,
x: state.x,
y: state.y,
resizable: true,
backgroundColor: '#EBEBEB',
frame: false,
minWidth: 350,
minHeight: 500,
webPreferences: {
// Use pluginOptions.nodeIntegration, leave this alone
// See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
nodeIntegration: (process.env
.ELECTRON_NODE_INTEGRATION as unknown) as boolean,
preload: path.join(__dirname, "preload.js")
nodeIntegration: (process.env.ELECTRON_NODE_INTEGRATION as unknown) as boolean, preload: path.join(__dirname, "preload.js")
}
});
// Load the URL.
if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode
win.loadURL(process.env.WEBPACK_DEV_SERVER_URL as string);
} else {
win.loadURL(process.env.WEBPACK_DEV_SERVER_URL as string); // dev
}
else {
createProtocol("app");
// Load the index.html when not in development
win.loadURL("app://./index.html");
win.loadURL("app://./index.html"); // prod
}
// Handle window close.
win.on("closed", windowDismount);
win.on("closed", onWindowDismount);
win.once('ready-to-show', () => {
if (win) win.show()
})
win.webContents.on('did-finish-load', () => {
if (win) win.webContents.send('window-ready', {
message: true
});
windowMount();
if (win) win.webContents.send('window-ready', {
message: true
});
onWindowMount();
resolve();
});