From 6cb5505ded2bcab0308945709b9564cc79172ffd Mon Sep 17 00:00:00 2001 From: Eugene Pankov Date: Mon, 16 Mar 2020 21:40:22 +0100 Subject: [PATCH] better tab recovery (fixes #501, fixes #2214, fixes #2185) --- .../src/components/appRoot.component.ts | 4 +- .../components/windowControls.component.ts | 4 +- terminus-core/src/services/app.service.ts | 48 +++++++++++-------- .../src/services/tabRecovery.service.ts | 12 ++++- 4 files changed, 40 insertions(+), 28 deletions(-) diff --git a/terminus-core/src/components/appRoot.component.ts b/terminus-core/src/components/appRoot.component.ts index dedf3181..9031ace2 100644 --- a/terminus-core/src/components/appRoot.component.ts +++ b/terminus-core/src/components/appRoot.component.ts @@ -133,9 +133,7 @@ export class AppRootComponent { }) this.hostApp.windowCloseRequest$.subscribe(async () => { - if (await this.app.closeAllTabs()) { - this.hostApp.closeWindow() - } + this.app.closeWindow() }) if (window['safeModeReason']) { diff --git a/terminus-core/src/components/windowControls.component.ts b/terminus-core/src/components/windowControls.component.ts index 4cfbc1c0..22994bcb 100644 --- a/terminus-core/src/components/windowControls.component.ts +++ b/terminus-core/src/components/windowControls.component.ts @@ -13,8 +13,6 @@ export class WindowControlsComponent { constructor (public hostApp: HostAppService, public app: AppService) { } async closeWindow () { - if (await this.app.closeAllTabs()) { - this.hostApp.closeWindow() - } + this.app.closeWindow() } } diff --git a/terminus-core/src/services/app.service.ts b/terminus-core/src/services/app.service.ts index 07164f79..550ecff7 100644 --- a/terminus-core/src/services/app.service.ts +++ b/terminus-core/src/services/app.service.ts @@ -74,32 +74,30 @@ export class AppService { private tabsService: TabsService, private ngbModal: NgbModal, ) { - if (hostApp.getWindow().id === 1) { - if (config.store.terminal.recoverTabs) { - this.tabRecovery.recoverTabs().then(tabs => { - for (const tab of tabs) { - this.openNewTabRaw(tab.type, tab.options) - } - this.startTabStorage() - }) - } else { - /** Continue to store the tabs even if the setting is currently off */ - this.startTabStorage() - } - } - - hostApp.windowFocused$.subscribe(() => { - this._activeTab?.emitFocused() - }) - } - - startTabStorage (): void { this.tabsChanged$.subscribe(() => { this.tabRecovery.saveTabs(this.tabs) }) setInterval(() => { this.tabRecovery.saveTabs(this.tabs) }, 30000) + + if (hostApp.getWindow().id === 1) { + if (config.store.terminal.recoverTabs) { + this.tabRecovery.recoverTabs().then(tabs => { + for (const tab of tabs) { + this.openNewTabRaw(tab.type, tab.options) + } + this.tabRecovery.enabled = true + }) + } else { + /** Continue to store the tabs even if the setting is currently off */ + this.tabRecovery.enabled = true + } + } + + hostApp.windowFocused$.subscribe(() => { + this._activeTab?.emitFocused() + }) } addTabRaw (tab: BaseTabComponent, index: number|null = null): void { @@ -295,6 +293,16 @@ export class AppService { return true } + async closeWindow (): Promise { + this.tabRecovery.enabled = false + await this.tabRecovery.saveTabs(this.tabs) + if (await this.closeAllTabs()) { + this.hostApp.closeWindow() + } else { + this.tabRecovery.enabled = true + } + } + /** @hidden */ emitReady (): void { this.ready.next() diff --git a/terminus-core/src/services/tabRecovery.service.ts b/terminus-core/src/services/tabRecovery.service.ts index 9cf30f2e..34a97fdb 100644 --- a/terminus-core/src/services/tabRecovery.service.ts +++ b/terminus-core/src/services/tabRecovery.service.ts @@ -8,6 +8,7 @@ import { ConfigService } from '../services/config.service' @Injectable({ providedIn: 'root' }) export class TabRecoveryService { logger: Logger + enabled = false constructor ( @Inject(TabRecoveryProvider) private tabRecoveryProviders: TabRecoveryProvider[], @@ -18,6 +19,9 @@ export class TabRecoveryService { } async saveTabs (tabs: BaseTabComponent[]): Promise { + if (!this.enabled) { + return + } window.localStorage.tabsRecovery = JSON.stringify( await Promise.all( tabs @@ -25,8 +29,11 @@ export class TabRecoveryService { let token = tab.getRecoveryToken() if (token) { token = token.then(r => { - if (r && tab.color) { - r.tabColor = tab.color + if (r) { + r.tabTitle = tab.title + if (tab.color) { + r.tabColor = tab.color + } } return r }) @@ -45,6 +52,7 @@ export class TabRecoveryService { if (tab !== null) { tab.options = tab.options || {} tab.options.color = token.tabColor || null + tab.options.title = token.tabTitle || '' return tab } } catch (error) {