save window size on close
This commit is contained in:
parent
db3c22b632
commit
5a1ab518b2
4 changed files with 33 additions and 16 deletions
|
|
@ -5,12 +5,7 @@ import { createProtocol } from "vue-cli-plugin-electron-builder/lib";
|
|||
import { backgroundMitt } from './emitter';
|
||||
import { RenderMessage } from "@/types/message/index";
|
||||
import * as path from "path";
|
||||
|
||||
interface WindowSettings {
|
||||
width: number;
|
||||
height: number;
|
||||
resizable: boolean;
|
||||
}
|
||||
import fs from 'fs';
|
||||
|
||||
interface IpcRendererPayload {
|
||||
endpoint: string;
|
||||
|
|
@ -40,12 +35,28 @@ const renderMessage = (payload: IpcRendererPayload): void => {
|
|||
}
|
||||
}
|
||||
|
||||
const onWindowSave = (_event, _s: string) => {
|
||||
if (win) {
|
||||
const bounds = win.getBounds();
|
||||
const state = JSON.stringify({w: bounds.width, h: bounds.height});
|
||||
|
||||
fs.writeFile('windowstate.json', state, (err: Error) => {
|
||||
if (err) throw err;
|
||||
return;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Do this on window mount.
|
||||
const windowMount = (): 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.
|
||||
backgroundMitt.on('ipc-renderer', renderMessage);
|
||||
}
|
||||
|
|
@ -57,16 +68,20 @@ const windowDismount = (): void => {
|
|||
}
|
||||
|
||||
// function used by run.ts to create the main window.
|
||||
export async function createWindow(options: WindowSettings): Promise<void> {
|
||||
export async function createWindow(): Promise<void> {
|
||||
return new Promise((resolve, _reject) => {
|
||||
|
||||
// avoid creating duplicate windows.
|
||||
if (win) resolve();
|
||||
|
||||
// read windowState from json.
|
||||
const rawData = fs.readFileSync('windowState.json');
|
||||
const windowState = JSON.parse(rawData);
|
||||
|
||||
win = new BrowserWindow({
|
||||
width: options.width,
|
||||
height: options.height,
|
||||
resizable: options.resizable,
|
||||
width: windowState.w,
|
||||
height: windowState.h,
|
||||
resizable: true,
|
||||
backgroundColor: '#EBEBEB',
|
||||
frame: false,
|
||||
minWidth: 350,
|
||||
|
|
|
|||
Loading…
Reference in a new issue