接入原生壳外观查询能力

新增 HostBridge appearance.getColorScheme 只读契约和 H5 facade

Expo 壳通过 React Native Appearance 读取系统配色

Tauri 壳通过主窗口 theme 读取桌面配色

补齐外观查询测试、漂移检查和架构文档
This commit is contained in:
2026-06-18 02:00:49 +08:00
parent 6b39bdbe19
commit 45eec17007
13 changed files with 164 additions and 15 deletions

View File

@@ -133,6 +133,7 @@ const requiredPermissions = [
const requiredBuildCommands = ['host_bridge_request'];
const requiredMainSnippets = [
'tauri_plugin_clipboard_manager::init()',
'"appearance.getColorScheme"',
'"share.open"',
'"share.setTarget"',
'"navigation.openNativePage"',
@@ -147,6 +148,7 @@ const requiredMainSnippets = [
'BASE64_STANDARD.decode',
'set_title',
'set_badge_count',
'window.theme()',
];
for (const permission of requiredPermissions) {

View File

@@ -5,6 +5,7 @@ use std::fs;
use std::path::PathBuf;
use std::sync::Mutex;
use tauri::Manager;
use tauri::Theme;
use tauri::Url;
use tauri_plugin_clipboard_manager::ClipboardExt;
use tauri_plugin_dialog::DialogExt;
@@ -79,6 +80,7 @@ fn desktop_platform() -> &'static str {
fn capabilities() -> Vec<&'static str> {
vec![
"host.getRuntime",
"appearance.getColorScheme",
"share.open",
"share.setTarget",
"navigation.openNativePage",
@@ -116,6 +118,14 @@ fn failed(id: String, code: &'static str, message: impl Into<String>) -> HostBri
}
}
fn color_scheme_from_theme(theme: Theme) -> &'static str {
match theme {
Theme::Light => "light",
Theme::Dark => "dark",
_ => "unknown",
}
}
fn validate_request(request: &HostBridgeRequest) -> Option<HostBridgeResponse> {
if request.bridge != HOST_BRIDGE_PROTOCOL || request.version != HOST_BRIDGE_VERSION {
return Some(failed(
@@ -520,6 +530,18 @@ async fn host_bridge_request(
Err(error) => failed(request.id, "host_error", error.to_string()),
}
}
"appearance.getColorScheme" => match app.get_webview_window("main") {
Some(window) => match window.theme() {
Ok(theme) => ok(
request.id,
json!({
"colorScheme": color_scheme_from_theme(theme)
}),
),
Err(error) => failed(request.id, "host_error", error.to_string()),
},
None => failed(request.id, "host_error", "main window not found"),
},
"navigation.openNativePage" => {
let url = match required_string_payload(&request, "url")
.ok()
@@ -737,6 +759,10 @@ mod tests {
assert_eq!(result["shell"], "tauri_desktop");
assert_eq!(result["bridgeVersion"], HOST_BRIDGE_VERSION);
assert_eq!(result["capabilities"], json!(capabilities()));
assert!(result["capabilities"]
.as_array()
.unwrap()
.contains(&json!("appearance.getColorScheme")));
assert!(result["capabilities"]
.as_array()
.unwrap()
@@ -801,6 +827,12 @@ mod tests {
assert_eq!(error.message, "text is required");
}
#[test]
fn color_scheme_maps_window_theme() {
assert_eq!(color_scheme_from_theme(Theme::Light), "light");
assert_eq!(color_scheme_from_theme(Theme::Dark), "dark");
}
#[test]
fn external_url_normalization_allows_only_safe_protocols() {
assert_eq!(

View File

@@ -6,7 +6,7 @@
"build": {
"beforeDevCommand": "npm --prefix ../.. run dev:web",
"beforeBuildCommand": "npm --prefix ../.. run build:raw && npm run typecheck",
"devUrl": "http://127.0.0.1:3000/?clientRuntime=native_app&clientType=native_app&hostShell=tauri_desktop&hostPlatform=unknown&hostVersion=0.1.0&bridgeVersion=1&hostCapabilities=host.getRuntime,share.open,share.setTarget,navigation.openNativePage,app.openExternalUrl,app.setTitle,app.setBadgeCount,clipboard.writeText,file.exportText,file.exportImage",
"devUrl": "http://127.0.0.1:3000/?clientRuntime=native_app&clientType=native_app&hostShell=tauri_desktop&hostPlatform=unknown&hostVersion=0.1.0&bridgeVersion=1&hostCapabilities=host.getRuntime,appearance.getColorScheme,share.open,share.setTarget,navigation.openNativePage,app.openExternalUrl,app.setTitle,app.setBadgeCount,clipboard.writeText,file.exportText,file.exportImage",
"frontendDist": "../../../dist"
},
"app": {
@@ -14,7 +14,7 @@
{
"create": false,
"label": "main",
"url": "index.html?clientRuntime=native_app&clientType=native_app&hostShell=tauri_desktop&hostPlatform=unknown&hostVersion=0.1.0&bridgeVersion=1&hostCapabilities=host.getRuntime,share.open,share.setTarget,navigation.openNativePage,app.openExternalUrl,app.setTitle,app.setBadgeCount,clipboard.writeText,file.exportText,file.exportImage",
"url": "index.html?clientRuntime=native_app&clientType=native_app&hostShell=tauri_desktop&hostPlatform=unknown&hostVersion=0.1.0&bridgeVersion=1&hostCapabilities=host.getRuntime,appearance.getColorScheme,share.open,share.setTarget,navigation.openNativePage,app.openExternalUrl,app.setTitle,app.setBadgeCount,clipboard.writeText,file.exportText,file.exportImage",
"title": "Genarrative",
"width": 1280,
"height": 820,