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:
STRIX
2026-09-08 18:48:25 +08:00
parent 72a4e127e3
commit 6aed2f3634
5 changed files with 1455 additions and 37 deletions
+221 -21
View File
@@ -2,13 +2,124 @@
* Copyright (c) 2026 Haven for HarmonyOS contributors.
* Port of Haven by GlassHaven (AGPL-3.0). See LICENSE.
*
* T9: settings placeholder — real settings (font size, color scheme,
* keepalive, background task) arrive with the terminal stage. The
* diagnostics page stays reachable here.
* T11: settings — default color scheme (with live preview blocks),
* default terminal font size (6-30vp slider), keep-screen-on toggle,
* about (AGPL-3.0 / upstream attribution) and the diagnostics entry.
*/
import { window } from '@kit.ArkUI';
import { common } from '@kit.AbilityKit';
import { SettingsRepository, FONT_SIZE_MAX, FONT_SIZE_MIN } from '../data/SettingsRepository';
import { COLOR_SCHEMES } from '../vterm/ColorSchemes';
import type { TerminalColorScheme } from '../vterm/ColorSchemes';
function rgbToCss(rgb: number): string {
const s: string = (rgb & 0xffffff).toString(16);
const padded: string = s.length >= 6 ? s : '000000'.substring(0, 6 - s.length) + s;
return `#${padded}`;
}
@Component
export struct SettingsPage {
@State schemeName: string = 'Haven';
@State fontSizeVp: number = 14;
@State keepScreenOn: boolean = false;
private settings: SettingsRepository | null = null;
aboutToAppear(): void {
const context: common.Context | undefined = this.getUIContext().getHostContext();
if (context === undefined) {
return;
}
this.settings = SettingsRepository.getInstance(context);
this.schemeName = this.settings.getDefaultColorScheme();
this.fontSizeVp = this.settings.getFontSize();
this.keepScreenOn = this.settings.getKeepScreenOn();
if (this.keepScreenOn) {
this.applyKeepScreenOn(true);
}
}
private applyKeepScreenOn(on: boolean): void {
const context: common.Context | undefined = this.getUIContext().getHostContext();
if (context === undefined) {
return;
}
window.getLastWindow(context).then((win: window.Window): void => {
win.setWindowKeepScreenOn(on).catch((): void => {
});
}).catch((): void => {
});
}
private pickScheme(scheme: TerminalColorScheme): void {
this.schemeName = scheme.name;
this.settings?.setDefaultColorScheme(scheme.name).catch((): void => {
});
}
private pickFontSize(sizeVp: number): void {
this.fontSizeVp = sizeVp;
this.settings?.setFontSize(sizeVp).catch((): void => {
});
}
private toggleKeepScreenOn(on: boolean): void {
this.keepScreenOn = on;
this.applyKeepScreenOn(on);
this.settings?.setKeepScreenOn(on).catch((): void => {
});
}
// ---------------------------------------------------------------- builders
@Builder
schemePreview(scheme: TerminalColorScheme) {
// Background + foreground sample + the first four ANSI colors.
Row({ space: 3 }) {
ForEach(scheme.ansi.slice(0, 4), (color: number) => {
Circle({ width: 12, height: 12 })
.fill(rgbToCss(color))
}, (color: number) => color.toString())
}
.padding({ left: 6, right: 6, top: 5, bottom: 5 })
.backgroundColor(rgbToCss(scheme.background))
.borderRadius(6)
}
@Builder
schemeCard(scheme: TerminalColorScheme) {
Column({ space: 8 }) {
this.schemePreview(scheme)
Text(scheme.name)
.fontSize(12)
.fontColor(scheme.name === this.schemeName ? '#0A59F7' : '#333333')
.fontWeight(scheme.name === this.schemeName ? FontWeight.Bold : FontWeight.Normal)
}
.padding(10)
.borderRadius(12)
.backgroundColor(scheme.name === this.schemeName ? '#E8F1FF' : '#FFFFFF')
.border({
width: scheme.name === this.schemeName ? 2 : 1,
color: scheme.name === this.schemeName ? '#0A59F7' : '#E5E5E5'
})
.onClick((): void => {
this.pickScheme(scheme);
})
}
@Builder
sectionTitle(title: string) {
Text(title)
.fontSize(14)
.fontWeight(FontWeight.Medium)
.fontColor('#333333')
.width('100%')
}
// ---------------------------------------------------------------- build
build() {
Column() {
Text('设置')
@@ -17,25 +128,114 @@ export struct SettingsPage {
.width('100%')
.padding({ left: 20, right: 20, top: 16 })
Column({ space: 12 }) {
Text('设置项将在终端阶段开放')
.fontSize(15)
.fontColor('#8C8C8C')
}
.width('100%')
.layoutWeight(1)
.justifyContent(FlexAlign.Center)
Scroll() {
Column({ space: 16 }) {
// ------------------------------------------------ default scheme
Column({ space: 10 }) {
this.sectionTitle('默认配色')
Flex({ wrap: FlexWrap.Wrap }) {
ForEach(COLOR_SCHEMES, (scheme: TerminalColorScheme) => {
this.schemeCard(scheme)
}, (scheme: TerminalColorScheme) => scheme.name)
}
.width('100%')
}
.width('100%')
.padding(16)
.backgroundColor('#FFFFFF')
.borderRadius(14)
Button('打开诊断页')
.width('60%')
.height(44)
.fontSize(14)
.backgroundColor('#FFFFFF')
.fontColor('#0A59F7')
.margin({ bottom: 32 })
.onClick((): void => {
this.getUIContext().getRouter().pushUrl({ url: 'pages/DiagPage' });
})
// ------------------------------------------------- default font
Column({ space: 10 }) {
this.sectionTitle('默认字号')
Row({ space: 12 }) {
Text('A')
.fontSize(10)
.fontColor('#8C8C8C')
Slider({
value: this.fontSizeVp,
min: FONT_SIZE_MIN,
max: FONT_SIZE_MAX,
step: 1,
style: SliderStyle.OutSet
})
.layoutWeight(1)
.selectedColor('#0A59F7')
.onChange((value: number, mode: SliderChangeMode): void => {
if (mode === SliderChangeMode.End || mode === SliderChangeMode.Click) {
this.pickFontSize(Math.round(value));
}
})
Text(`${this.fontSizeVp} vp`)
.fontSize(13)
.fontColor('#0A59F7')
.width(48)
.textAlign(TextAlign.End)
}
.width('100%')
}
.width('100%')
.padding(16)
.backgroundColor('#FFFFFF')
.borderRadius(14)
// ---------------------------------------------- keep screen on
Row({ space: 8 }) {
Text('屏幕常亮')
.fontSize(15)
.fontColor('#333333')
.layoutWeight(1)
Toggle({ type: ToggleType.Switch, isOn: this.keepScreenOn })
.selectedColor('#0A59F7')
.onChange((isOn: boolean): void => {
this.toggleKeepScreenOn(isOn);
})
}
.width('100%')
.padding(16)
.backgroundColor('#FFFFFF')
.borderRadius(14)
// -------------------------------------------------------- about
Column({ space: 8 }) {
this.sectionTitle('关于')
Text('Haven for HarmonyOS 0.1.0')
.fontSize(13)
.fontColor('#666666')
.width('100%')
Text('基于 Haven(GlassHaven)移植,遵循 AGPL-3.0 开源协议。' +
'SSH 协议栈与 VT 终端模拟器为纯 TypeScript 实现,运行于应用内。')
.fontSize(12)
.fontColor('#8C8C8C')
.width('100%')
Text('本程序为自由软件:你可以依据 Free Software Foundation 发布的 ' +
'GNU Affero General Public License(第 3 版或任意更新版本)重新发布或修改它。')
.fontSize(12)
.fontColor('#8C8C8C')
.width('100%')
}
.width('100%')
.padding(16)
.backgroundColor('#FFFFFF')
.borderRadius(14)
.alignItems(HorizontalAlign.Start)
Button('打开诊断页')
.width('100%')
.height(44)
.fontSize(14)
.backgroundColor('#FFFFFF')
.fontColor('#0A59F7')
.onClick((): void => {
this.getUIContext().getRouter().pushUrl({ url: 'pages/DiagPage' });
})
}
.width('100%')
.padding({ left: 16, right: 16, top: 12, bottom: 32 })
}
.layoutWeight(1)
.scrollBar(BarState.Off)
.width('100%')
}
.width('100%')
.height('100%')