mirror of
https://github.com/Eugeny/tabby.git
synced 2025-07-20 02:18:01 +00:00
Merge pull request #8416 from Clem-Fern/connectable-refactoring
Refactoring connectable tab
This commit is contained in:
@@ -9,7 +9,7 @@ import { BaseSession } from '../session'
|
||||
|
||||
import { Frontend } from '../frontends/frontend'
|
||||
import { XTermFrontend, XTermWebGLFrontend } from '../frontends/xtermFrontend'
|
||||
import { ResizeEvent, BaseTerminalProfile, isReconnectable } from './interfaces'
|
||||
import { ResizeEvent, BaseTerminalProfile } from './interfaces'
|
||||
import { TerminalDecorator } from './decorator'
|
||||
import { SearchPanelComponent } from '../components/searchPanel.component'
|
||||
import { MultifocusService } from '../services/multifocus.service'
|
||||
@@ -312,11 +312,6 @@ export class BaseTerminalTabComponent<P extends BaseTerminalProfile> extends Bas
|
||||
case 'scroll-to-bottom':
|
||||
this.frontend?.scrollToBottom()
|
||||
break
|
||||
case 'reconnect-tab':
|
||||
if (isReconnectable(this)) {
|
||||
this.reconnect()
|
||||
}
|
||||
break
|
||||
}
|
||||
})
|
||||
|
||||
@@ -784,7 +779,7 @@ export class BaseTerminalTabComponent<P extends BaseTerminalProfile> extends Bas
|
||||
})
|
||||
|
||||
this.attachSessionHandler(this.session.destroyed$, () => {
|
||||
this.setSession(null)
|
||||
this.onSessionDestroyed()
|
||||
})
|
||||
|
||||
this.attachSessionHandler(this.session.oscProcessor.copyRequested$, content => {
|
||||
@@ -793,6 +788,13 @@ export class BaseTerminalTabComponent<P extends BaseTerminalProfile> extends Bas
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Method called when session is destroyed. Set the session to null
|
||||
*/
|
||||
protected onSessionDestroyed (): void {
|
||||
this.setSession(null)
|
||||
}
|
||||
|
||||
protected detachSessionHandlers (): void {
|
||||
this.sessionHandlers.cancelAll()
|
||||
}
|
||||
|
94
tabby-terminal/src/api/connectableTerminalTab.component.ts
Normal file
94
tabby-terminal/src/api/connectableTerminalTab.component.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker'
|
||||
|
||||
import { Injector, Component } from '@angular/core'
|
||||
|
||||
import { first } from 'rxjs'
|
||||
|
||||
import { BaseTerminalProfile } from './interfaces'
|
||||
import { BaseTerminalTabComponent } from './baseTerminalTab.component'
|
||||
import { GetRecoveryTokenOptions, RecoveryToken } from 'tabby-core'
|
||||
|
||||
|
||||
/**
|
||||
* A class to base your custom connectable terminal tabs on
|
||||
*/
|
||||
@Component({ template: '' })
|
||||
export abstract class ConnectableTerminalTabComponent<P extends BaseTerminalProfile> extends BaseTerminalTabComponent<P> {
|
||||
|
||||
protected reconnectOffered = false
|
||||
|
||||
constructor (protected injector: Injector) {
|
||||
super(injector)
|
||||
|
||||
this.subscribeUntilDestroyed(this.hotkeys.hotkey$, hotkey => {
|
||||
if (this.hasFocus && hotkey === 'reconnect-tab') {
|
||||
this.reconnect()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
ngOnInit (): void {
|
||||
this.logger = this.log.create(`${this.profile.type}Tab`)
|
||||
|
||||
super.ngOnInit()
|
||||
}
|
||||
|
||||
protected onFrontendReady (): void {
|
||||
this.initializeSession()
|
||||
super.onFrontendReady()
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize Connectable Session.
|
||||
* Set reconnectOffered to false
|
||||
*/
|
||||
async initializeSession (): Promise<void> {
|
||||
this.reconnectOffered = false
|
||||
}
|
||||
|
||||
/**
|
||||
* Method called when session is destroyed. Handle the tab behavior on session end for connectable tab
|
||||
*/
|
||||
protected onSessionDestroyed (): void {
|
||||
super.onSessionDestroyed()
|
||||
|
||||
if (this.frontend) {
|
||||
if (this.profile.behaviorOnSessionEnd === 'reconnect') {
|
||||
this.reconnect()
|
||||
} else if (this.profile.behaviorOnSessionEnd === 'keep' || this.profile.behaviorOnSessionEnd === 'auto' && !this.isSessionExplicitlyTerminated()) {
|
||||
this.offerReconnection()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Offering reconnection to the user if it hasn't been done yet.
|
||||
* Set reconnectOffered to true
|
||||
*/
|
||||
offerReconnection (): void {
|
||||
if (!this.reconnectOffered) {
|
||||
this.reconnectOffered = true
|
||||
this.write(this.translate.instant(_('Press any key to reconnect')) + '\r\n')
|
||||
this.input$.pipe(first()).subscribe(() => {
|
||||
if (!this.session?.open && this.reconnectOffered) {
|
||||
this.reconnect()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async getRecoveryToken (options?: GetRecoveryTokenOptions): Promise<RecoveryToken> {
|
||||
return {
|
||||
type: `app:${this.profile.type}-tab`,
|
||||
profile: this.profile,
|
||||
savedState: options?.includeState && this.frontend?.saveState(),
|
||||
}
|
||||
}
|
||||
|
||||
async reconnect (): Promise<void> {
|
||||
this.session?.destroy()
|
||||
await this.initializeSession()
|
||||
this.session?.releaseInitialDataBuffer()
|
||||
}
|
||||
|
||||
}
|
@@ -19,12 +19,3 @@ export interface TerminalColorScheme {
|
||||
export interface BaseTerminalProfile extends Profile {
|
||||
terminalColorScheme?: TerminalColorScheme
|
||||
}
|
||||
|
||||
export interface Reconnectable {
|
||||
reconnect: () => Promise<void>;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
||||
export function isReconnectable (object: any): object is Reconnectable {
|
||||
return 'reconnect' in object
|
||||
}
|
||||
|
@@ -90,6 +90,7 @@ export default class TerminalModule { } // eslint-disable-line @typescript-eslin
|
||||
export { TerminalDecorator, TerminalContextMenuItemProvider, TerminalColorSchemeProvider }
|
||||
export { Frontend, XTermFrontend, XTermWebGLFrontend }
|
||||
export { BaseTerminalTabComponent } from './api/baseTerminalTab.component'
|
||||
export { ConnectableTerminalTabComponent } from './api/connectableTerminalTab.component'
|
||||
export * from './api/interfaces'
|
||||
export * from './middleware/streamProcessing'
|
||||
export * from './middleware/loginScriptProcessing'
|
||||
|
@@ -1,9 +1,9 @@
|
||||
import { Injectable, Optional, Inject } from '@angular/core'
|
||||
import { BaseTabComponent, TabContextMenuItemProvider, NotificationsService, MenuItemOptions, TranslateService, SplitTabComponent } from 'tabby-core'
|
||||
import { BaseTerminalTabComponent } from './api/baseTerminalTab.component'
|
||||
import { isReconnectable } from './api/interfaces'
|
||||
import { TerminalContextMenuItemProvider } from './api/contextMenuProvider'
|
||||
import { MultifocusService } from './services/multifocus.service'
|
||||
import { ConnectableTerminalTabComponent } from './api/connectableTerminalTab.component'
|
||||
|
||||
/** @hidden */
|
||||
@Injectable()
|
||||
@@ -97,7 +97,7 @@ export class ReconnectContextMenu extends TabContextMenuItemProvider {
|
||||
) { super() }
|
||||
|
||||
async getItems (tab: BaseTabComponent): Promise<MenuItemOptions[]> {
|
||||
if (isReconnectable(tab)) {
|
||||
if (tab instanceof ConnectableTerminalTabComponent) {
|
||||
return [
|
||||
{
|
||||
label: this.translate.instant('Reconnect'),
|
||||
|
Reference in New Issue
Block a user