接入原生壳应用角标能力

新增 HostBridge app.setBadgeCount 契约和 H5 能力门控

Expo 壳按平台声明能力并在 iOS 调用系统角标 API

Tauri 壳通过主窗口设置任务栏角标并校验 payload

补齐角标能力测试、漂移检查和架构文档
This commit is contained in:
2026-06-18 01:50:15 +08:00
parent 910625d5e1
commit 6b39bdbe19
15 changed files with 336 additions and 19 deletions

View File

@@ -2,6 +2,7 @@ import { describe, expect, test } from 'vitest';
import {
isHostBridgeCapability,
normalizeHostBridgeBadgeCount,
normalizeHostBridgeExportFileName,
normalizeHostBridgeExternalUrl,
} from './hostBridge';
@@ -46,8 +47,19 @@ describe('HostBridge shared contract helpers', () => {
test('识别 HostBridge 能力白名单', () => {
expect(isHostBridgeCapability('share.open')).toBe(true);
expect(isHostBridgeCapability('app.setBadgeCount')).toBe(true);
expect(isHostBridgeCapability('navigation.canGoBack')).toBe(true);
expect(isHostBridgeCapability('unknown.capability')).toBe(false);
expect(isHostBridgeCapability(null)).toBe(false);
});
test('归一化宿主角标数量', () => {
expect(normalizeHostBridgeBadgeCount(0)).toBe(0);
expect(normalizeHostBridgeBadgeCount(12)).toBe(12);
expect(normalizeHostBridgeBadgeCount(99999)).toBe(99999);
expect(normalizeHostBridgeBadgeCount(-1)).toBeNull();
expect(normalizeHostBridgeBadgeCount(1.5)).toBeNull();
expect(normalizeHostBridgeBadgeCount(100000)).toBeNull();
expect(normalizeHostBridgeBadgeCount('1')).toBeNull();
});
});

View File

@@ -22,6 +22,7 @@ export const HOST_BRIDGE_METHODS = [
'navigation.openNativePage',
'app.openExternalUrl',
'app.setTitle',
'app.setBadgeCount',
'clipboard.writeText',
'file.exportText',
'file.exportImage',
@@ -113,6 +114,25 @@ export type SetTitlePayload = {
title: string;
};
export type SetBadgeCountPayload = {
count: number;
};
export const HOST_BRIDGE_BADGE_COUNT_MAX = 99999;
export function normalizeHostBridgeBadgeCount(rawCount: unknown) {
if (
typeof rawCount !== 'number' ||
!Number.isInteger(rawCount) ||
rawCount < 0 ||
rawCount > HOST_BRIDGE_BADGE_COUNT_MAX
) {
return null;
}
return rawCount;
}
export const HOST_BRIDGE_EXTERNAL_URL_PROTOCOLS = [
'http:',
'https:',