/* * Copyright (c) 2026 Haven for HarmonyOS contributors. * Port of Haven by GlassHaven (AGPL-3.0). See LICENSE. * * T9: terminal page skeleton — session tab strip plus per-state overlays * (connecting progress, host-key notice, inline password prompt, error * and closed panels with reconnect). The terminal canvas itself lands in * the next stage. */ import { HostColors } from '../common/HostColors'; import { SessionManager } from '../session/SessionManager'; import { SessionState, TerminalSession } from '../session/TerminalSession'; import { showToast } from './SessionLaunch'; @Component export struct TerminalPage { @State sessions: TerminalSession[] = []; @State activeId: number = -1; @State activeSnapshot: TerminalSession | null = null; @State passwordInput: string = ''; @State rememberPassword: boolean = true; private unsubscribe: (() => void) | null = null; aboutToAppear(): void { this.unsubscribe = SessionManager.getInstance().subscribe((): void => { this.refresh(); }); this.refresh(); } aboutToDisappear(): void { if (this.unsubscribe !== null) { this.unsubscribe(); this.unsubscribe = null; } } private refresh(): void { const manager: SessionManager = SessionManager.getInstance(); this.sessions = manager.all(); const active: number | null = manager.getActiveId(); this.activeId = active === null ? -1 : active; this.activeSnapshot = manager.getActive(); const current: TerminalSession | null = manager.getActive(); if (current === null || current.getState() !== SessionState.AUTH_REQUIRED) { this.passwordInput = ''; } } private submitPassword(session: TerminalSession): void { const pwd: string = this.passwordInput; if (pwd.length === 0) { showToast(this.getUIContext(), '请输入密码'); return; } this.passwordInput = ''; session.providePassword(pwd, this.rememberPassword); } // ---------------------------------------------------------------- builders @Builder sessionTab(session: TerminalSession) { Row({ space: 4 }) { Row({ space: 6 }) { Circle({ width: 8, height: 8 }) .fill(HostColors.at(session.getColorTag())) Text(session.getTitle()) .fontSize(13) .maxLines(1) .fontColor(session.getId() === this.activeId ? '#0A59F7' : '#333333') } .padding({ left: 12, right: 10, top: 6, bottom: 6 }) .borderRadius(14) .backgroundColor(session.getId() === this.activeId ? '#E8F1FF' : '#FFFFFF') .onClick((): void => { SessionManager.getInstance().setActive(session.getId()); }) Text('×') .fontSize(15) .fontColor('#999999') .width(22) .height(22) .textAlign(TextAlign.Center) .onClick((): void => { SessionManager.getInstance().remove(session.getId()); }) } .margin({ right: 4 }) } @Builder progressPanel(text: string) { Column({ space: 12 }) { LoadingProgress() .width(36) .height(36) .color('#0A59F7') Text(text) .fontSize(14) .fontColor('#666666') } .width('100%') .layoutWeight(1) .justifyContent(FlexAlign.Center) } @Builder passwordPanel(session: TerminalSession) { Column({ space: 12 }) { Text(session.isPasswordFailed() ? '密码错误,请重试' : '需要密码') .fontSize(16) .fontWeight(FontWeight.Medium) .fontColor(session.isPasswordFailed() ? '#E5484D' : '#333333') Text(session.getSubtitle()) .fontSize(13) .fontColor('#8C8C8C') TextInput({ text: this.passwordInput, placeholder: '密码' }) .type(InputType.Password) .width('80%') .height(44) .fontSize(14) .backgroundColor('#F5F6F7') .borderRadius(10) .onChange((value: string): void => { this.passwordInput = value; }) Row({ space: 8 }) { Checkbox() .select(this.rememberPassword) .selectedColor('#0A59F7') .onChange((value: boolean): void => { this.rememberPassword = value; }) Text('记住密码') .fontSize(14) .fontColor('#333333') } Button('连接') .width('80%') .height(44) .fontSize(15) .backgroundColor('#0A59F7') .fontColor('#FFFFFF') .margin({ top: 4 }) .onClick((): void => { this.submitPassword(session); }) } .width('100%') .layoutWeight(1) .justifyContent(FlexAlign.Center) } @Builder endPanel(title: string, detail: string, session: TerminalSession) { Column({ space: 12 }) { Text(title) .fontSize(16) .fontWeight(FontWeight.Medium) .fontColor('#333333') if (detail.length > 0) { Text(detail) .fontSize(13) .fontColor('#8C8C8C') .textAlign(TextAlign.Center) .padding({ left: 32, right: 32 }) } Row({ space: 12 }) { Button('重新连接') .height(40) .fontSize(14) .padding({ left: 20, right: 20 }) .backgroundColor('#0A59F7') .fontColor('#FFFFFF') .onClick((): void => { session.reconnect(); }) Button('关闭会话') .height(40) .fontSize(14) .padding({ left: 20, right: 20 }) .backgroundColor('#FFFFFF') .fontColor('#666666') .onClick((): void => { SessionManager.getInstance().remove(session.getId()); }) } .margin({ top: 4 }) } .width('100%') .layoutWeight(1) .justifyContent(FlexAlign.Center) } @Builder sessionBody(session: TerminalSession) { Column() { if (session.getState() === SessionState.READY) { Column({ space: 8 }) { Text('会话已就绪') .fontSize(14) .fontColor('#4ADE80') Text('终端渲染将在下一阶段启用') .fontSize(12) .fontColor('#888888') } .width('100%') .layoutWeight(1) .justifyContent(FlexAlign.Center) .backgroundColor('#1E1E1E') } else if (session.getState() === SessionState.CONNECTING) { this.progressPanel(`正在连接 ${session.getSubtitle()} …`) } else if (session.getState() === SessionState.HOSTKEY_WAIT) { this.progressPanel('请在弹窗中确认主机密钥…') } else if (session.getState() === SessionState.AUTHENTICATING) { this.progressPanel('正在认证…') } else if (session.getState() === SessionState.AUTH_REQUIRED) { this.passwordPanel(session) } else if (session.getState() === SessionState.ERROR) { this.endPanel('连接失败', session.getErrorText(), session) } else if (session.getState() === SessionState.CLOSED) { this.endPanel('连接已关闭', session.getCloseReason(), session) } } .width('100%') .layoutWeight(1) } // ---------------------------------------------------------------- build build() { Column() { if (this.sessions.length === 0) { Column({ space: 8 }) { Text('暂无会话') .fontSize(15) .fontColor('#8C8C8C') Text('在“连接”页点击主机开始 SSH 会话') .fontSize(13) .fontColor('#B0B0B0') } .width('100%') .layoutWeight(1) .justifyContent(FlexAlign.Center) } else { Scroll() { Row({ space: 4 }) { ForEach(this.sessions, (session: TerminalSession) => { this.sessionTab(session) }, (session: TerminalSession) => session.getId().toString()) } .padding({ left: 12, right: 12, top: 8, bottom: 8 }) } .scrollable(ScrollDirection.Horizontal) .scrollBar(BarState.Off) .width('100%') Divider() .color('#E5E5E5') .strokeWidth(1) if (this.activeSnapshot !== null) { this.sessionBody(this.activeSnapshot) } } } .width('100%') .height('100%') .backgroundColor('#F7F8FA') } }