diff --git a/docs/plugin-development.md b/docs/plugin-development.md index 9e3909f7..063fab87 100644 --- a/docs/plugin-development.md +++ b/docs/plugin-development.md @@ -25,6 +25,18 @@ Rust 模板还包含编译脚本,编译后自动填入当前平台的可执行 将生成的 `.nyap` 安装到插件面板,检查权限并启用。UI 模板需先选中已连接终端; Rust 模板可以打开界面点击运行,也可以直接执行插件卡片上的原生命令。 +## 页面字体 + +宿主会将应用的界面字体栈通过 `NyaTerm.context.theme` 中的 `--font-sans`、 +`--font-display` 同步到插件页面;修改设置时,已打开的页面也会收到更新,无需重载。 +`--font-mono` 提供宿主的默认等宽字体栈。SDK 默认使用界面字体,表单控件继承字体; +插件自定义 CSS 应使用 `font-family: var(--font-sans, system-ui, sans-serif)`, +代码区域可使用 `var(--font-mono, monospace)`,避免写死 `system-ui`。 + +iframe 可以使用系统安装的字体,但不会继承主界面的 `@font-face`。 +若使用未安装到系统的内置字体或其他 Web 字体,需要在插件包内提供字体文件并声明 +`@font-face`。已有插件若写死字体,需要修改 CSS 并重新打包安装。 + ## 编写 Rust 后台 入口实现 `Plugin`,由 `Server::from_env()` 启动服务。SDK 负责握手、传输、 diff --git a/docs/plugins.md b/docs/plugins.md index cfa53417..1f6fd51d 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -194,7 +194,16 @@ must match exactly; no wildcards, paths or implicit subdomain grants. `NyaTerm.context` contains plugin identity/version and CSS theme variables; `NyaTerm.onContextChange(listener)` returns an unsubscribe function. Theme variables -are applied to the iframe document automatically. This context contains no scope +are applied to the iframe document automatically, including `--font-sans` and +`--font-display` (the configured UI font stack) and `--font-mono`. Font changes +are pushed to open panels without reloading them. The SDK defaults to the UI font +and makes form controls inherit it; plugin styles should use +`font-family: var(--font-sans, system-ui, sans-serif)` instead of a fixed family. +Use `var(--font-mono, monospace)` for code. System-installed fonts are available +inside the iframe, but the host's `@font-face` declarations are not inherited; +package any required web fonts with the plugin and declare them in its CSS. +Existing packages with a fixed font family need to update their CSS and be rebuilt. +This context contains no scope token. Tokens are managed by the parent bridge. Scopes expire after 30 minutes; open panels renew at 25 minutes by recreating their document. Preserve needed nonsecret preferences in plugin storage. diff --git a/plugins/examples/gpu-monitor/frontend/gpu.css b/plugins/examples/gpu-monitor/frontend/gpu.css index abad1a57..5c725a1c 100644 --- a/plugins/examples/gpu-monitor/frontend/gpu.css +++ b/plugins/examples/gpu-monitor/frontend/gpu.css @@ -36,7 +36,7 @@ body, body { background: var(--background); color: var(--foreground); - font-family: system-ui, sans-serif; + font-family: var(--font-sans, system-ui, sans-serif); } .terminal-scroll { scrollbar-width: thin; diff --git a/plugins/examples/session-toolbox/ui/index.html b/plugins/examples/session-toolbox/ui/index.html index 8d0493ab..037c5cf7 100644 --- a/plugins/examples/session-toolbox/ui/index.html +++ b/plugins/examples/session-toolbox/ui/index.html @@ -11,9 +11,7 @@ body { margin: 0; padding: 16px; - font: - 13px/1.5 system-ui, - sans-serif; + font: 13px/1.5 var(--font-sans, system-ui, sans-serif); background: var(--df-bg, #111827); color: var(--df-fg, #e5e7eb); } diff --git a/plugins/sdk/nyaterm.d.ts b/plugins/sdk/nyaterm.d.ts index 9b5f85d8..9f7b03ba 100644 --- a/plugins/sdk/nyaterm.d.ts +++ b/plugins/sdk/nyaterm.d.ts @@ -9,6 +9,7 @@ type NyaTermJson = interface NyaTermPluginContext { pluginId: string; version: string; + /** Host CSS variables, including --font-sans, --font-display and --font-mono. */ theme: Record; language?: string; monitorIntervalSeconds?: number; diff --git a/plugins/sdk/nyaterm.js b/plugins/sdk/nyaterm.js index b8d2f2d5..996bae67 100644 --- a/plugins/sdk/nyaterm.js +++ b/plugins/sdk/nyaterm.js @@ -1,6 +1,14 @@ /* NyaTerm Plugin UI SDK v1. Include this as a classic script before plugin code. */ (() => { "use strict"; + // A sandboxed document cannot inherit typography from the host. Keep these + // defaults easy for plugin styles to override, including code/monospace text. + const typography = document.createElement("style"); + typography.textContent = ` + :where(html) { font-family: var(--font-sans, system-ui, sans-serif); } + :where(button, input, select, textarea) { font-family: inherit; } + `; + document.head.prepend(typography); const pending = new Map(); const contextListeners = new Set(); const monitors = new Map(); diff --git a/plugins/templates/ui/index.html b/plugins/templates/ui/index.html index 03886e1d..cfdb3999 100644 --- a/plugins/templates/ui/index.html +++ b/plugins/templates/ui/index.html @@ -7,7 +7,7 @@