接入原生壳外观查询能力

新增 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

@@ -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');
}

View File

@@ -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({

View File

@@ -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':