fixed window toggling with multiple windows (fixes #619)

This commit is contained in:
Eugene Pankov
2020-04-19 11:47:31 +02:00
parent 11767f7d27
commit f58cab0820
8 changed files with 95 additions and 68 deletions

View File

@@ -1,4 +1,4 @@
import { app, ipcMain, Menu, Tray, shell } from 'electron'
import { app, ipcMain, Menu, Tray, shell, globalShortcut } from 'electron'
// eslint-disable-next-line no-duplicate-imports
import * as electron from 'electron'
import { loadConfig } from './config'
@@ -9,8 +9,17 @@ export class Application {
private windows: Window[] = []
constructor () {
ipcMain.on('app:config-change', () => {
this.broadcast('host:config-change')
ipcMain.on('app:config-change', (_event, config) => {
this.broadcast('host:config-change', config)
})
ipcMain.on('app:register-global-hotkey', (_event, specs) => {
globalShortcut.unregisterAll()
for (let spec of specs) {
globalShortcut.register(spec, () => {
this.onGlobalHotkey()
})
}
})
const configData = loadConfig()
@@ -45,6 +54,9 @@ export class Application {
this.enableTray()
}
})
window.closed$.subscribe(() => {
this.windows = this.windows.filter(x => x !== window)
})
if (process.platform === 'darwin') {
this.setupMenu()
}
@@ -52,6 +64,24 @@ export class Application {
return window
}
onGlobalHotkey () {
if (this.windows.some(x => x.isFocused())) {
for (let window of this.windows) {
window.hide()
}
} else {
for (let window of this.windows) {
window.present()
}
}
}
presentAllWindows() {
for (let window of this.windows) {
window.present()
}
}
broadcast (event: string, ...args): void {
for (const window of this.windows) {
window.send(event, ...args)

View File

@@ -34,6 +34,7 @@ process.on('uncaughtException' as any, err => {
})
app.on('second-instance', (_event, argv, cwd) => {
application.presentAllWindows()
application.send('host:second-instance', parseArgs(argv, cwd), cwd)
})

View File

@@ -1,6 +1,6 @@
import { Subject, Observable } from 'rxjs'
import { debounceTime } from 'rxjs/operators'
import { BrowserWindow, app, ipcMain, Rectangle, screen } from 'electron'
import { BrowserWindow, app, ipcMain, Rectangle, Menu, screen } from 'electron'
import ElectronConfig = require('electron-config')
import * as os from 'os'
import * as path from 'path'
@@ -23,17 +23,20 @@ export interface WindowOptions {
export class Window {
ready: Promise<void>
private visible = new Subject<boolean>()
private closed = new Subject<void>()
private window: BrowserWindow
private windowConfig: ElectronConfig
private windowBounds: Rectangle
private closing = false
private lastVibrancy: {enabled: boolean, type?: string} | null = null
private disableVibrancyWhileDragging = false
private configStore: any
get visible$ (): Observable<boolean> { return this.visible }
get closed$ (): Observable<void> { return this.closed }
constructor (options?: WindowOptions) {
let configData = loadConfig()
this.configStore = loadConfig()
options = options || {}
@@ -70,7 +73,7 @@ export class Window {
}
}
if ((configData.appearance || {}).frame === 'native') {
if ((this.configStore.appearance || {}).frame === 'native') {
bwOptions.frame = true
} else {
if (process.platform === 'darwin') {
@@ -86,7 +89,7 @@ export class Window {
this.window.once('ready-to-show', () => {
if (process.platform === 'darwin') {
this.window.setVibrancy('window')
} else if (process.platform === 'win32' && (configData.appearance || {}).vibrancy) {
} else if (process.platform === 'win32' && (this.configStore.appearance || {}).vibrancy) {
this.setVibrancy(true)
}
@@ -153,12 +156,49 @@ export class Window {
return
}
this.window.webContents.send(event, ...args)
if (event === 'host:config-change') {
this.configStore = args[0]
}
}
isDestroyed (): boolean {
return !this.window || this.window.isDestroyed()
}
isFocused (): boolean {
return this.window.isFocused()
}
hide () {
if (process.platform === 'darwin') {
// Lose focus
Menu.sendActionToFirstResponder('hide:')
}
this.window.blur()
if (process.platform !== 'darwin') {
this.window.hide()
}
}
present () {
if (!this.window.isVisible()) {
// unfocused, invisible
this.window.show()
this.window.focus()
} else {
if (!this.configStore.appearance?.dock || this.configStore.appearance?.dock === 'off') {
// not docked, visible
setTimeout(() => {
this.window.show()
this.window.focus()
})
} else {
// docked, visible
this.window.hide()
}
}
}
private setupWindowManagement () {
this.window.on('show', () => {
this.visible.next(true)
@@ -326,6 +366,8 @@ export class Window {
private destroy () {
this.window = null
this.closed.next()
this.visible.complete()
this.closed.complete()
}
}