Send messages to render process when window state changes

This commit is contained in:
Markus Olsson 2016-06-22 17:39:44 +02:00
parent 2398af38d4
commit 9c63c17173
2 changed files with 29 additions and 0 deletions

1
src/lib/window-state.ts Normal file
View file

@ -0,0 +1 @@
export type WindowState = 'minimized' | 'normal' | 'maximized' | 'full-screen'

View file

@ -2,6 +2,7 @@ import {BrowserWindow} from 'electron'
import Stats from './stats'
import {URLActionType} from '../lib/parse-url'
import {WindowState} from '../lib/window-state'
export default class AppWindow {
private window: Electron.BrowserWindow
@ -54,9 +55,36 @@ export default class AppWindow {
this.window.show()
})
this.registerWindowStateChangedEvents()
this.window.loadURL(`file://${__dirname}/../../index.html`)
}
private registerWindowStateChangedEvents() {
this.window.on('enter-full-screen', () => this.sendWindowStateEvent('full-screen'))
this.window.on('leave-full-screen', () => this.sendWindowStateEvent(this.getWindowState()))
this.window.on('maximize', () => this.sendWindowStateEvent('maximized'))
this.window.on('minimize', () => this.sendWindowStateEvent('minimized'))
this.window.on('unmaximize', () => this.sendWindowStateEvent('normal'))
this.window.on('restore', () => this.sendWindowStateEvent('normal'))
}
private getWindowState(): WindowState {
if (this.window.isFullScreen) {
return 'full-screen'
} else if (this.window.isMaximized()) {
return 'maximized'
} else if (this.window.isMinimized()) {
return 'minimized'
} else {
return 'normal'
}
}
private sendWindowStateEvent(state: WindowState) {
this.send('window-state-changed', state)
}
public onClose(fn: () => void) {
this.window.on('closed', fn)
}