generated from ali/HarmonyTemplate
TerminalSession:
- shell integration: ensureShell() opens PTY+shell with emulator created
first (renderable during handshake); channel onClose -> CLOSED
- I/O API: sendBytes/sendText/sendKey (DECCKM-aware)/sendPaste
(bracketed-paste aware); resizeTerminal -> emulator.resize + pty
window-change; OSC 0/2 title wins over label; inputVersion counter
lets the canvas snap back to live view on any user input
TerminalCanvas:
- full-area Canvas renderer; cell size from measureText('M')
- merged style runs (one fillText per run), wide-cell 2-cell spans
- dirty-row incremental redraw live, full redraw while scrolled back
- block/underline cursor, always visible on alternate screen
- xterm-256 palette + truecolor + reverse/dim/bold/italic/underline/strike
- pinch zoom 6-30vp with persisted setting; PTY resize on relayout
- touch: SGR mouse reports in mouse mode, else scrollback drag + tap
focuses the hidden 1px IME anchor TextInput
KeyboardToolbar (Haven default two-row layout):
- Row1: snippets/keyboard/Esc/Tab/Paste/Text///Home/Up/End/PgUp
- Row2: Shift/Ctrl/Alt/arrows/PgDn/|/~/\backslash/`
- sticky modifiers: one-shot tap, double-tap lock (bordered), consume
on dispatch; Ctrl+symbol via ch & 0x1f; Alt prefixes ESC
TerminalPage:
- READY body mounts canvas + toolbar + whole-line text input overlay
(CJK path) + snippet macro panel (^C ^D ^Z ^L ^A ^E ^W ^U Shift-Tab)
- soft-keyboard toggle via FocusController on the hidden IME anchor
SettingsPage:
- color scheme picker with live preview blocks (4 schemes)
- font size slider 6-30vp, keep-screen-on toggle (window API)
- about (AGPL-3.0 + upstream attribution), diagnostics entry
492 lines
14 KiB
Plaintext
492 lines
14 KiB
Plaintext
/*
|
||
* Copyright (c) 2026 Haven for HarmonyOS contributors.
|
||
* Port of Haven by GlassHaven (AGPL-3.0). See LICENSE.
|
||
*
|
||
* T9/T11: terminal page — session tab strip, per-state overlays (connecting,
|
||
* host-key notice, inline password prompt, error/closed panels) and, once a
|
||
* session is READY, the full terminal canvas with the two-row keyboard
|
||
* toolbar, snippet panel and whole-line text input overlay.
|
||
*/
|
||
|
||
import { pasteboard } from '@kit.BasicServicesKit';
|
||
import { HostColors } from '../common/HostColors';
|
||
import { SessionManager } from '../session/SessionManager';
|
||
import { SessionState, TerminalSession } from '../session/TerminalSession';
|
||
import { SettingsRepository } from '../data/SettingsRepository';
|
||
import { VtKey } from '../vterm/VtInput';
|
||
import { KeyboardToolbar } from './KeyboardToolbar';
|
||
import { TerminalCanvas } from './TerminalCanvas';
|
||
import { showToast } from './SessionLaunch';
|
||
|
||
/** Hardcoded snippet macros (Haven MACRO_PRESETS). */
|
||
interface SnippetDef {
|
||
label: string;
|
||
seq: string;
|
||
}
|
||
|
||
const SNIPPETS: SnippetDef[] = [
|
||
{ label: '^C', seq: '\u0003' },
|
||
{ label: '^D', seq: '\u0004' },
|
||
{ label: '^Z', seq: '\u001a' },
|
||
{ label: '^L', seq: '\u000c' },
|
||
{ label: '^A', seq: '\u0001' },
|
||
{ label: '^E', seq: '\u0005' },
|
||
{ label: '^W', seq: '\u0017' },
|
||
{ label: '^U', seq: '\u0015' },
|
||
{ label: '⇧Tab', seq: '\u001b[Z' }
|
||
];
|
||
|
||
@Component
|
||
export struct TerminalPage {
|
||
@State sessions: TerminalSession[] = [];
|
||
@State activeId: number = -1;
|
||
@State activeSnapshot: TerminalSession | null = null;
|
||
@State passwordInput: string = '';
|
||
@State rememberPassword: boolean = true;
|
||
@State fontSizeVp: number = 14;
|
||
@State schemeName: string = 'Haven';
|
||
@State imeVisible: boolean = false;
|
||
@State snippetsVisible: boolean = false;
|
||
@State textInputVisible: boolean = false;
|
||
@State textInputValue: string = '';
|
||
|
||
private unsubscribe: (() => void) | null = null;
|
||
|
||
aboutToAppear(): void {
|
||
this.unsubscribe =
|
||
SessionManager.getInstance().subscribe((): void => {
|
||
this.refresh();
|
||
});
|
||
this.loadSettings();
|
||
this.refresh();
|
||
}
|
||
|
||
aboutToDisappear(): void {
|
||
if (this.unsubscribe !== null) {
|
||
this.unsubscribe();
|
||
this.unsubscribe = null;
|
||
}
|
||
}
|
||
|
||
private loadSettings(): void {
|
||
const settings: SettingsRepository =
|
||
SettingsRepository.getInstance(this.getUIContext().getHostContext());
|
||
this.fontSizeVp = settings.getFontSize();
|
||
this.schemeName = settings.getDefaultColorScheme();
|
||
}
|
||
|
||
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 = '';
|
||
}
|
||
if (current === null || current.getState() !== SessionState.READY) {
|
||
this.snippetsVisible = false;
|
||
this.textInputVisible = false;
|
||
}
|
||
}
|
||
|
||
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);
|
||
}
|
||
|
||
private applyFontSize(sizeVp: number): void {
|
||
this.fontSizeVp = sizeVp;
|
||
const settings: SettingsRepository =
|
||
SettingsRepository.getInstance(this.getUIContext().getHostContext());
|
||
settings.setFontSize(sizeVp).catch((): void => {
|
||
});
|
||
}
|
||
|
||
private toggleKeyboard(): void {
|
||
const controller = this.getUIContext().getFocusController();
|
||
if (this.imeVisible) {
|
||
controller.clearFocus();
|
||
} else {
|
||
try {
|
||
controller.requestFocus(`term-ime-${this.activeId}`);
|
||
} catch (e) {
|
||
// Canvas not mounted yet; non-fatal.
|
||
}
|
||
}
|
||
}
|
||
|
||
private pasteInto(session: TerminalSession): void {
|
||
this.readClipboard().then((text: string): void => {
|
||
if (text.length > 0) {
|
||
session.sendPaste(text);
|
||
} else {
|
||
showToast(this.getUIContext(), '剪贴板为空');
|
||
}
|
||
}).catch((): void => {
|
||
showToast(this.getUIContext(), '读取剪贴板失败');
|
||
});
|
||
}
|
||
|
||
private async readClipboard(): Promise<string> {
|
||
const board: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard();
|
||
if (!board.hasData()) {
|
||
return '';
|
||
}
|
||
const data: pasteboard.PasteData = await board.getData();
|
||
return data.getPrimaryText();
|
||
}
|
||
|
||
private sendTextLine(session: TerminalSession, withEnter: boolean): void {
|
||
const v: string = this.textInputValue;
|
||
this.textInputValue = '';
|
||
this.textInputVisible = false;
|
||
if (v.length > 0) {
|
||
session.sendText(withEnter ? v + '\r' : v);
|
||
}
|
||
}
|
||
|
||
// ---------------------------------------------------------------- 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)
|
||
}
|
||
|
||
/** Whole-line text input overlay (CJK IME composition path). */
|
||
@Builder
|
||
textInputPanel(session: TerminalSession) {
|
||
Column({ space: 8 }) {
|
||
TextArea({ text: this.textInputValue, placeholder: '输入整行文本…' })
|
||
.width('100%')
|
||
.height(72)
|
||
.fontSize(14)
|
||
.backgroundColor('#F5F6F7')
|
||
.borderRadius(10)
|
||
.onChange((value: string): void => {
|
||
this.textInputValue = value;
|
||
})
|
||
Row({ space: 8 }) {
|
||
Button('发送')
|
||
.layoutWeight(1)
|
||
.height(38)
|
||
.fontSize(14)
|
||
.backgroundColor('#0A59F7')
|
||
.fontColor('#FFFFFF')
|
||
.onClick((): void => {
|
||
this.sendTextLine(session, false);
|
||
})
|
||
Button('发送并回车')
|
||
.layoutWeight(2)
|
||
.height(38)
|
||
.fontSize(14)
|
||
.backgroundColor('#0A59F7')
|
||
.fontColor('#FFFFFF')
|
||
.onClick((): void => {
|
||
this.sendTextLine(session, true);
|
||
})
|
||
Button('取消')
|
||
.height(38)
|
||
.fontSize(14)
|
||
.padding({ left: 14, right: 14 })
|
||
.backgroundColor('#FFFFFF')
|
||
.fontColor('#666666')
|
||
.onClick((): void => {
|
||
this.textInputValue = '';
|
||
this.textInputVisible = false;
|
||
})
|
||
}
|
||
.width('100%')
|
||
}
|
||
.width('100%')
|
||
.padding(8)
|
||
.backgroundColor('#FFFFFF')
|
||
}
|
||
|
||
/** Snippet macro chips (Ctrl combos and Shift-Tab). */
|
||
@Builder
|
||
snippetsPanel(session: TerminalSession) {
|
||
Column({ space: 8 }) {
|
||
Text('宏')
|
||
.fontSize(13)
|
||
.fontColor('#8C8C8C')
|
||
Flex({ wrap: FlexWrap.Wrap, justifyContent: FlexAlign.Center }) {
|
||
ForEach(SNIPPETS, (snippet: SnippetDef) => {
|
||
Text(snippet.label)
|
||
.fontSize(14)
|
||
.fontColor('#333333')
|
||
.padding({ left: 14, right: 14, top: 8, bottom: 8 })
|
||
.backgroundColor('#F0F1F3')
|
||
.borderRadius(8)
|
||
.margin(4)
|
||
.onClick((): void => {
|
||
session.sendText(snippet.seq);
|
||
this.snippetsVisible = false;
|
||
})
|
||
}, (snippet: SnippetDef) => snippet.label)
|
||
}
|
||
.width('100%')
|
||
Text('收起')
|
||
.fontSize(13)
|
||
.fontColor('#0A59F7')
|
||
.padding(6)
|
||
.onClick((): void => {
|
||
this.snippetsVisible = false;
|
||
})
|
||
}
|
||
.width('100%')
|
||
.padding(8)
|
||
.backgroundColor('#FFFFFF')
|
||
}
|
||
|
||
@Builder
|
||
readyBody(session: TerminalSession) {
|
||
Column() {
|
||
TerminalCanvas({
|
||
sessionId: session.getId(),
|
||
schemeName: this.schemeName,
|
||
fontSizeVp: this.fontSizeVp,
|
||
onFontSizeChange: (sizeVp: number): void => {
|
||
this.applyFontSize(sizeVp);
|
||
},
|
||
onImeFocusChanged: (focused: boolean): void => {
|
||
this.imeVisible = focused;
|
||
}
|
||
})
|
||
.layoutWeight(1)
|
||
|
||
if (this.textInputVisible) {
|
||
this.textInputPanel(session)
|
||
}
|
||
if (this.snippetsVisible) {
|
||
this.snippetsPanel(session)
|
||
}
|
||
|
||
KeyboardToolbar({
|
||
onSendText: (text: string): void => {
|
||
session.sendText(text);
|
||
},
|
||
onSendKey: (key: VtKey, mods: number): void => {
|
||
session.sendKey(key, mods);
|
||
},
|
||
onPaste: (): void => {
|
||
this.pasteInto(session);
|
||
},
|
||
onToggleKeyboard: (): void => {
|
||
this.toggleKeyboard();
|
||
},
|
||
onOpenSnippets: (): void => {
|
||
this.snippetsVisible = !this.snippetsVisible;
|
||
},
|
||
onOpenTextInput: (): void => {
|
||
this.textInputVisible = !this.textInputVisible;
|
||
}
|
||
})
|
||
}
|
||
.width('100%')
|
||
.layoutWeight(1)
|
||
}
|
||
|
||
@Builder
|
||
sessionBody(session: TerminalSession) {
|
||
Column() {
|
||
if (session.getState() === SessionState.READY) {
|
||
this.readyBody(session)
|
||
} 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')
|
||
}
|
||
}
|