/* * Copyright (c) 2026 Haven for HarmonyOS contributors. * Port of Haven by GlassHaven (AGPL-3.0). See LICENSE. * * 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('设置') .fontSize(26) .fontWeight(FontWeight.Bold) .width('100%') .padding({ left: 20, right: 20, top: 16 }) 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) // ------------------------------------------------- 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%') .backgroundColor('#F7F8FA') } }