接入原生壳外观查询能力
新增 HostBridge appearance.getColorScheme 只读契约和 H5 facade Expo 壳通过 React Native Appearance 读取系统配色 Tauri 壳通过主窗口 theme 读取桌面配色 补齐外观查询测试、漂移检查和架构文档
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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!(
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -153,6 +153,7 @@ if (!appSource.includes('capabilities: resolveMobileHostCapabilities()')) {
|
||||
|
||||
for (const capability of [
|
||||
'host.getRuntime',
|
||||
'appearance.getColorScheme',
|
||||
'share.open',
|
||||
'share.setTarget',
|
||||
'navigation.openNativePage',
|
||||
@@ -175,3 +176,7 @@ if (!iosMobileCapabilitySet.has('app.setBadgeCount')) {
|
||||
if (mobileCapabilitySet.has('app.setBadgeCount')) {
|
||||
throw new Error('Android mobile shell base capabilities must not include app.setBadgeCount');
|
||||
}
|
||||
|
||||
if (!bridgeSource.includes('Appearance.getColorScheme()')) {
|
||||
throw new Error('mobile shell HostBridge must read the native color scheme');
|
||||
}
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
import * as Haptics from 'expo-haptics';
|
||||
import * as Linking from 'expo-linking';
|
||||
import * as Sharing from 'expo-sharing';
|
||||
import { Platform, PushNotificationIOS, Share } from 'react-native';
|
||||
import {
|
||||
Appearance,
|
||||
Platform,
|
||||
PushNotificationIOS,
|
||||
Share,
|
||||
} from 'react-native';
|
||||
import { afterEach, describe, expect, test, vi } from 'vitest';
|
||||
|
||||
import {
|
||||
@@ -70,6 +75,9 @@ vi.mock('expo-sharing', () => ({
|
||||
}));
|
||||
|
||||
vi.mock('react-native', () => ({
|
||||
Appearance: {
|
||||
getColorScheme: vi.fn(() => 'light'),
|
||||
},
|
||||
Platform: {
|
||||
OS: 'ios',
|
||||
},
|
||||
@@ -130,6 +138,8 @@ function setPlatformOS(os: 'ios' | 'android') {
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
vi.mocked(Appearance.getColorScheme).mockReset();
|
||||
vi.mocked(Appearance.getColorScheme).mockReturnValue('light');
|
||||
vi.mocked(Haptics.impactAsync).mockReset();
|
||||
vi.mocked(Linking.openURL).mockReset();
|
||||
vi.mocked(PushNotificationIOS.setApplicationIconBadgeNumber).mockReset();
|
||||
@@ -162,6 +172,7 @@ describe('handleMobileHostBridgeMessage', () => {
|
||||
(okResponse.result as { capabilities: string[] }).capabilities,
|
||||
).toEqual(
|
||||
expect.arrayContaining([
|
||||
'appearance.getColorScheme',
|
||||
'host.events',
|
||||
'navigation.canGoBack',
|
||||
'app.setBadgeCount',
|
||||
@@ -184,6 +195,26 @@ describe('handleMobileHostBridgeMessage', () => {
|
||||
).not.toContain('app.setBadgeCount');
|
||||
});
|
||||
|
||||
test('appearance.getColorScheme 返回系统配色模式', async () => {
|
||||
vi.mocked(Appearance.getColorScheme).mockReturnValue('dark');
|
||||
|
||||
const response = await send(request('appearance.getColorScheme'));
|
||||
|
||||
expect(expectOk(response).result).toEqual({
|
||||
colorScheme: 'dark',
|
||||
});
|
||||
});
|
||||
|
||||
test('appearance.getColorScheme 归一化未知系统配色', async () => {
|
||||
vi.mocked(Appearance.getColorScheme).mockReturnValue('unspecified');
|
||||
|
||||
const response = await send(request('appearance.getColorScheme'));
|
||||
|
||||
expect(expectOk(response).result).toEqual({
|
||||
colorScheme: 'unknown',
|
||||
});
|
||||
});
|
||||
|
||||
test('navigation.openNativePage 把同源路径切到移动壳 WebView', async () => {
|
||||
const openWebViewUrl = vi.fn();
|
||||
configureMobileHostBridgeNavigation({
|
||||
|
||||
@@ -3,7 +3,12 @@ import { File, Paths } from 'expo-file-system';
|
||||
import * as Haptics from 'expo-haptics';
|
||||
import * as Linking from 'expo-linking';
|
||||
import * as Sharing from 'expo-sharing';
|
||||
import { Platform, PushNotificationIOS, Share } from 'react-native';
|
||||
import {
|
||||
Appearance,
|
||||
Platform,
|
||||
PushNotificationIOS,
|
||||
Share,
|
||||
} from 'react-native';
|
||||
|
||||
import {
|
||||
type ClipboardWriteTextPayload,
|
||||
@@ -21,6 +26,7 @@ import {
|
||||
type HostBridgeResponse,
|
||||
type NavigateNativePagePayload,
|
||||
normalizeHostBridgeBadgeCount,
|
||||
normalizeHostBridgeColorScheme,
|
||||
normalizeHostBridgeExportFileName,
|
||||
normalizeHostBridgeExternalUrl,
|
||||
type OpenExternalUrlPayload,
|
||||
@@ -40,6 +46,7 @@ const EXPORT_IMAGE_MIME_TYPES = new Set([
|
||||
|
||||
export const MOBILE_HOST_CAPABILITIES: HostBridgeCapability[] = [
|
||||
'host.getRuntime',
|
||||
'appearance.getColorScheme',
|
||||
'host.events',
|
||||
'share.open',
|
||||
'share.setTarget',
|
||||
@@ -309,6 +316,12 @@ function setBadgeCount(payload: unknown) {
|
||||
return true;
|
||||
}
|
||||
|
||||
function getColorScheme() {
|
||||
return {
|
||||
colorScheme: normalizeHostBridgeColorScheme(Appearance.getColorScheme()),
|
||||
};
|
||||
}
|
||||
|
||||
function stringField(value: unknown, field: string) {
|
||||
if (!value || typeof value !== 'object') {
|
||||
return undefined;
|
||||
@@ -419,6 +432,8 @@ async function handleRequest(request: HostBridgeRequest) {
|
||||
bridgeVersion: HOST_BRIDGE_VERSION,
|
||||
capabilities: resolveMobileHostCapabilities(),
|
||||
});
|
||||
case 'appearance.getColorScheme':
|
||||
return ok(request, getColorScheme());
|
||||
case 'app.openExternalUrl':
|
||||
return ok(request, await openExternalUrl(request.payload));
|
||||
case 'clipboard.writeText':
|
||||
|
||||
Reference in New Issue
Block a user