From eb81b9fd0180e901d22c7a5d0d97d996d6fab94e Mon Sep 17 00:00:00 2001 From: Eugene Pankov Date: Thu, 22 Nov 2018 17:51:12 +0100 Subject: [PATCH] check tabs before closing the window (fixes #520) --- app/lib/window.ts | 13 ++++++++++++- .../src/components/windowControls.component.pug | 2 +- .../src/components/windowControls.component.ts | 3 ++- terminus-core/src/services/app.service.ts | 14 ++++++++++++++ terminus-core/src/services/hostApp.service.ts | 10 ++++++++++ .../src/components/settingsTab.component.pug | 2 +- 6 files changed, 40 insertions(+), 4 deletions(-) diff --git a/app/lib/window.ts b/app/lib/window.ts index 75926843..527f489f 100644 --- a/app/lib/window.ts +++ b/app/lib/window.ts @@ -24,6 +24,7 @@ export class Window { private window: BrowserWindow private windowConfig: ElectronConfig private windowBounds: Rectangle + private closing = false get visible$ (): Observable { return this.visible } @@ -145,7 +146,12 @@ export class Window { this.window.on('enter-full-screen', () => this.window.webContents.send('host:window-enter-full-screen')) this.window.on('leave-full-screen', () => this.window.webContents.send('host:window-leave-full-screen')) - this.window.on('close', () => { + this.window.on('close', event => { + if (!this.closing) { + event.preventDefault() + this.window.webContents.send('host:window-close-request') + return + } this.windowConfig.set('windowBoundaries', this.windowBounds) this.windowConfig.set('maximized', this.window.isMaximized()) }) @@ -244,6 +250,11 @@ export class Window { this.window.moveTop() }) + ipcMain.on('window-close', () => { + this.closing = true + this.window.close() + }) + this.window.webContents.on('new-window', event => event.preventDefault()) } diff --git a/terminus-core/src/components/windowControls.component.pug b/terminus-core/src/components/windowControls.component.pug index 005691d8..ad14083f 100644 --- a/terminus-core/src/components/windowControls.component.pug +++ b/terminus-core/src/components/windowControls.component.pug @@ -9,7 +9,7 @@ button.btn.btn-secondary.btn-maximize( svg(version='1.1', width='10', height='10') path(d='M 0,0 0,10 10,10 10,0 Z M 1,1 9,1 9,9 1,9 Z') button.btn.btn-secondary.btn-close( - (click)='hostApp.getWindow().close()', + (click)='app.closeWindow()' ) svg(version='1.1', width='10', height='10') path(d='M 0,0 0,0.7 4.3,5 0,9.3 0,10 0.7,10 5,5.7 9.3,10 10,10 10,9.3 5.7,5 10,0.7 10,0 9.3,0 5,4.3 0.7,0 Z') diff --git a/terminus-core/src/components/windowControls.component.ts b/terminus-core/src/components/windowControls.component.ts index 34b3202f..2b5188e7 100644 --- a/terminus-core/src/components/windowControls.component.ts +++ b/terminus-core/src/components/windowControls.component.ts @@ -1,5 +1,6 @@ import { Component } from '@angular/core' import { HostAppService } from '../services/hostApp.service' +import { AppService } from '../services/app.service' @Component({ selector: 'window-controls', @@ -7,5 +8,5 @@ import { HostAppService } from '../services/hostApp.service' styles: [require('./windowControls.component.scss')], }) export class WindowControlsComponent { - constructor (public hostApp: HostAppService) { } + constructor (public hostApp: HostAppService, public app: AppService) { } } diff --git a/terminus-core/src/services/app.service.ts b/terminus-core/src/services/app.service.ts index c7cd8e21..fef30982 100644 --- a/terminus-core/src/services/app.service.ts +++ b/terminus-core/src/services/app.service.ts @@ -64,6 +64,8 @@ export class AppService { log: LogService, ) { this.logger = log.create('app') + + this.hostApp.windowCloseRequest$.subscribe(() => this.closeWindow()) } openNewTab (type: TabComponentType, inputs?: any): BaseTabComponent { @@ -158,6 +160,18 @@ export class AppService { this.tabClosed.next(tab) } + async closeWindow () { + for (let tab of this.tabs) { + if (!await tab.canClose()) { + return + } + } + for (let tab of this.tabs) { + tab.destroy() + } + this.hostApp.closeWindow() + } + emitReady () { this.ready.next(null) this.ready.complete() diff --git a/terminus-core/src/services/hostApp.service.ts b/terminus-core/src/services/hostApp.service.ts index f091b5c2..cf863e51 100644 --- a/terminus-core/src/services/hostApp.service.ts +++ b/terminus-core/src/services/hostApp.service.ts @@ -28,6 +28,7 @@ export class HostAppService { private cliRunCommand = new Subject() private cliPaste = new Subject() private configChangeBroadcast = new Subject() + private windowCloseRequest = new Subject() private logger: Logger private windowId: number @@ -37,6 +38,7 @@ export class HostAppService { get cliRunCommand$ (): Observable { return this.cliRunCommand } get cliPaste$ (): Observable { return this.cliPaste } get configChangeBroadcast$ (): Observable { return this.configChangeBroadcast } + get windowCloseRequest$ (): Observable { return this.windowCloseRequest } constructor ( private zone: NgZone, @@ -72,6 +74,10 @@ export class HostAppService { this.zone.run(() => this.shown.emit()) }) + electron.ipcRenderer.on('host:window-close-request', () => { + this.zone.run(() => this.windowCloseRequest.next()) + }) + electron.ipcRenderer.on('host:second-instance', (_$event, argv: any, cwd: string) => this.zone.run(() => { this.logger.info('Second instance', argv) const op = argv._[0] @@ -186,6 +192,10 @@ export class HostAppService { this.electron.ipcRenderer.send('window-bring-to-front') } + closeWindow () { + this.electron.ipcRenderer.send('window-close') + } + quit () { this.logger.info('Quitting') this.electron.app.quit() diff --git a/terminus-settings/src/components/settingsTab.component.pug b/terminus-settings/src/components/settingsTab.component.pug index c44c2b75..fdc073f8 100644 --- a/terminus-settings/src/components/settingsTab.component.pug +++ b/terminus-settings/src/components/settingsTab.component.pug @@ -266,7 +266,7 @@ ngb-tabset.vertical(type='pills', [activeId]='activeTab') tr(*ngFor='let hotkey of hotkeyDescriptions|filterBy:["name"]:hotkeyFilter') td {{hotkey.name}} td {{hotkey.id}} - td + td.pr-5 multi-hotkey-input( [model]='getHotkey(hotkey.id)', (modelChange)='setHotkey(hotkey.id, $event); config.save(); docking.dock()'