generated from ali/HarmonyTemplate
T11: terminal canvas, keyboard toolbar, settings
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
This commit is contained in:
@@ -2,17 +2,40 @@
|
||||
* 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.
|
||||
* 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[] = [];
|
||||
@@ -20,6 +43,12 @@ export struct TerminalPage {
|
||||
@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;
|
||||
|
||||
@@ -28,6 +57,7 @@ export struct TerminalPage {
|
||||
SessionManager.getInstance().subscribe((): void => {
|
||||
this.refresh();
|
||||
});
|
||||
this.loadSettings();
|
||||
this.refresh();
|
||||
}
|
||||
|
||||
@@ -38,6 +68,13 @@ export struct TerminalPage {
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
@@ -48,6 +85,10 @@ export struct TerminalPage {
|
||||
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 {
|
||||
@@ -60,6 +101,57 @@ export struct TerminalPage {
|
||||
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
|
||||
@@ -197,22 +289,145 @@ export struct TerminalPage {
|
||||
.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) {
|
||||
Column({ space: 8 }) {
|
||||
Text('会话已就绪')
|
||||
.fontSize(14)
|
||||
.fontColor('#4ADE80')
|
||||
Text('终端渲染将在下一阶段启用')
|
||||
.fontSize(12)
|
||||
.fontColor('#888888')
|
||||
}
|
||||
.width('100%')
|
||||
.layoutWeight(1)
|
||||
.justifyContent(FlexAlign.Center)
|
||||
.backgroundColor('#1E1E1E')
|
||||
this.readyBody(session)
|
||||
} else if (session.getState() === SessionState.CONNECTING) {
|
||||
this.progressPanel(`正在连接 ${session.getSubtitle()} …`)
|
||||
} else if (session.getState() === SessionState.HOSTKEY_WAIT) {
|
||||
|
||||
Reference in New Issue
Block a user