@orielweb/api 0.0.0-stage → 0.4.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,180 @@
1
+ export { __setDebugWarnings };
2
+ /**
3
+ * 系统托盘图标(对标 Tauri 2 的 `TrayIcon`)。
4
+ *
5
+ * 用 {@linkcode TrayIcon.new} 创建;本库中它**不是资源句柄**(见文件头说明)。
6
+ *
7
+ * @example
8
+ * ```js
9
+ * import { TrayIcon } from '@orielweb/api/tray';
10
+ * const tray = await TrayIcon.new({ tooltip: 'awesome tray tooltip' });
11
+ * await tray.setTooltip('new tooltip');
12
+ * ```
13
+ */
14
+ export class TrayIcon {
15
+ /**
16
+ * 按 id 取回句柄(不存在时返回 null)。
17
+ *
18
+ * @param {string} id
19
+ * @returns {Promise<TrayIcon | null>}
20
+ */
21
+ static getById(id: string): Promise<TrayIcon | null>;
22
+ /**
23
+ * 按 id 移除托盘(图标随之消失)。
24
+ *
25
+ * @param {string} id
26
+ * @returns {Promise<boolean>} 是否确实移除了一个。
27
+ */
28
+ static removeById(id: string): Promise<boolean>;
29
+ /**
30
+ * 新建托盘图标。
31
+ *
32
+ * @param {{ id?: string, menu?: any, icon?: any, tooltip?: string, title?: string,
33
+ * tempDirPath?: string, iconAsTemplate?: boolean, menuOnLeftClick?: boolean,
34
+ * showMenuOnLeftClick?: boolean, action?: (event: any) => void }} [options]
35
+ * @returns {Promise<TrayIcon>}
36
+ *
37
+ * #### 平台差异
38
+ * - **Linux**:有时要设了菜单图标才可见(Tauri 同);`showMenuOnLeftClick` 无效。
39
+ * - **Windows**:`title` 不支持(平台无此概念)。
40
+ * - **macOS**:`iconAsTemplate` 决定菜单栏图标的模板着色。
41
+ */
42
+ static "new"(options?: {
43
+ id?: string;
44
+ menu?: any;
45
+ icon?: any;
46
+ tooltip?: string;
47
+ title?: string;
48
+ tempDirPath?: string;
49
+ iconAsTemplate?: boolean;
50
+ menuOnLeftClick?: boolean;
51
+ showMenuOnLeftClick?: boolean;
52
+ action?: (event: any) => void;
53
+ }): Promise<TrayIcon>;
54
+ /**
55
+ * @param {string} id 宿主侧的托盘 id(页面持有的唯一标识)。
56
+ * @internal 请用 {@linkcode TrayIcon.new}。
57
+ */
58
+ constructor(id: string);
59
+ /** 本托盘的 id。 */
60
+ id: string;
61
+ /**
62
+ * 资源句柄的**补壳**:本库没有资源表,`rid` 就是 `id`。
63
+ *
64
+ * 迁移代码里的 `tray.rid` 不会报错,但它不是 Rust 侧句柄——
65
+ * debug 模式下调用会打一条警告(补壳必须可观测)。
66
+ */
67
+ get rid(): string;
68
+ /**
69
+ * 订阅本托盘的事件(对标 Tauri 的 `TrayIconOptions.action`,也可在 `new` 后随时加)。
70
+ *
71
+ * handler 收到 `{ id, type, button, buttonState, position, rect }`;
72
+ * 平台未提供的字段为 `null`(Linux 的 button、macOS 的 position 等)。
73
+ *
74
+ * @param {(event: any) => void} handler
75
+ * @returns {() => void} 取消订阅函数。
76
+ */
77
+ onEvent(handler: (event: any) => void): () => void;
78
+ /**
79
+ * 设置新图标;`null` 移除图标。
80
+ *
81
+ * @param {any} icon
82
+ * @returns {Promise<void>}
83
+ */
84
+ setIcon(icon: any): Promise<void>;
85
+ /**
86
+ * 设置托盘菜单。
87
+ *
88
+ * @param {any} menu
89
+ * @returns {Promise<void>}
90
+ *
91
+ * #### 平台差异
92
+ * - **Linux**:菜单一旦设置就移除不掉,`null` 只清空项(Tauri 同)。
93
+ */
94
+ setMenu(menu: any): Promise<void>;
95
+ /**
96
+ * 设置悬停提示。
97
+ *
98
+ * @param {string | null} tooltip
99
+ * @returns {Promise<void>}
100
+ *
101
+ * #### 平台差异
102
+ * - **Linux**:不支持(GtkStatusIcon 无 tooltip 之外的等价物时行为各异)。
103
+ */
104
+ setTooltip(tooltip: string | null): Promise<void>;
105
+ /**
106
+ * 设置图标旁的标题文本。
107
+ *
108
+ * @param {string | null} title
109
+ * @returns {Promise<void>}
110
+ *
111
+ * #### 平台差异
112
+ * - **Windows**:不支持。
113
+ * - **Linux**:只有同时有图标时才显示。
114
+ */
115
+ setTitle(title: string | null): Promise<void>;
116
+ /**
117
+ * 显示/隐藏本托盘图标。
118
+ *
119
+ * @param {boolean} visible
120
+ * @returns {Promise<void>}
121
+ */
122
+ setVisible(visible: boolean): Promise<void>;
123
+ /**
124
+ * 把当前图标设为 macOS 的模板图像(随菜单栏明暗自动着色)。
125
+ *
126
+ * @param {boolean} asTemplate
127
+ * @returns {Promise<void>}
128
+ *
129
+ * #### 平台差异
130
+ * - **macOS 专属**;其他平台接受参数但不生效(与 Tauri 一致)。
131
+ */
132
+ setIconAsTemplate(asTemplate: boolean): Promise<void>;
133
+ /**
134
+ * 原子地"换图标 + 改模板标记"(Tauri 为了避免两次调用之间闪一下旧模板态)。
135
+ *
136
+ * 本库是两次调用的组合(宿主没有原子入口),短暂闪烁理论上可能出现;
137
+ * 与逐次调 `setIcon` + `setIconAsTemplate` 的差别只在顺序保证。
138
+ *
139
+ * @param {any} icon
140
+ * @param {boolean} asTemplate
141
+ * @returns {Promise<void>}
142
+ */
143
+ setIconWithAsTemplate(icon: any, asTemplate: boolean): Promise<void>;
144
+ /**
145
+ * 开关"左键点击弹出菜单"(`setShowMenuOnLeftClick` 的旧名,Tauri 已弃用同款)。
146
+ *
147
+ * @param {boolean} onLeft
148
+ * @returns {Promise<void>}
149
+ * @deprecated 用 {@linkcode TrayIcon.setShowMenuOnLeftClick}。
150
+ */
151
+ setMenuOnLeftClick(onLeft: boolean): Promise<void>;
152
+ /**
153
+ * 开关"左键点击弹出菜单"(默认开)。
154
+ *
155
+ * 关掉后左键点击交给你自己的 `action` 处理器,右键仍弹菜单。
156
+ *
157
+ * @param {boolean} onLeft
158
+ * @returns {Promise<void>}
159
+ *
160
+ * #### 平台差异
161
+ * - **Windows**:生效。
162
+ * - **Linux / macOS**:抛 `not_supported`——那两个平台"左键弹菜单"是平台惯例,
163
+ * 本库后端暂未接通开关(Tauri 标注 Linux Unsupported;macOS 有 API 而我们没接)。
164
+ */
165
+ setShowMenuOnLeftClick(onLeft: boolean): Promise<void>;
166
+ /**
167
+ * 设置图标临时目录。**Linux 专属补壳**:本库不落盘图标,调用不报错但不生效。
168
+ *
169
+ * @param {string | null} _path
170
+ * @returns {Promise<void>}
171
+ */
172
+ setTempDirPath(_path: string | null): Promise<void>;
173
+ /**
174
+ * 移除本托盘(等价 `TrayIcon.removeById(this.id)`)。
175
+ *
176
+ * @returns {Promise<boolean>}
177
+ */
178
+ close(): Promise<boolean>;
179
+ }
180
+ import { __setDebugWarnings } from './internal.js';
@@ -0,0 +1,61 @@
1
+ // 传输层形状:注入脚本(src/OrielWeb/Bridge/oriel-bridge.js)暴露的 `window.oriel`。
2
+ //
3
+ // 这是**内部契约**,不是公开 API。页面应当 import `@orielweb/api/*`,而不是直接用这个对象。
4
+ // 单独写出来是为了让 `internal.js` 与 `mocks.js` 有个可校验的形状——
5
+ // 没有它,`setTransportOverride` 接受任何东西,装错 mock 要到运行时才发现。
6
+ //
7
+ // 文件名带 `.d.ts` 后缀,但 JSDoc 里按 ESM 惯例引用为 `./types.js`:
8
+ // TypeScript 会把 `.js` 解析到同目录的 `.d.ts`,这样打包器与编辑器都能正确跟进。
9
+
10
+ /** OrielWeb 的注入式传输层(`window.oriel`)。 */
11
+ export interface OrielTransport {
12
+ /**
13
+ * 调用宿主命令。
14
+ *
15
+ * @param name 命令名。
16
+ * @param args 参数对象;省略时宿主收到 `null`。
17
+ * @returns 命令返回值;失败时 reject 一个 `Error`(message 是宿主给的文案)。
18
+ */
19
+ invoke(name: string, args?: Record<string, unknown> | null): Promise<unknown>;
20
+
21
+ /**
22
+ * 是否支持 `invoke(name, args, { timeout })` 三参形态(P1-7 之后为 `true`)。
23
+ *
24
+ * 这是个**显式声明**而不是靠 `invoke.length` 探测:探测在包装/压缩后会给出错误答案,
25
+ * 而错误答案的表现是"设了超时却没生效"。
26
+ */
27
+ supportsPerCallTimeout?: boolean;
28
+
29
+ /** 宿主平台:`windows` / `macos` / `linux`。 */
30
+ platform?: string;
31
+
32
+ /** 库版本(`Major.Minor.Build`),由宿主注入。注意这是**库**版本,不是应用版本。 */
33
+ version?: string;
34
+
35
+ /** 本窗口的 label(P0-2 之后可用)。 */
36
+ label?: string;
37
+
38
+ /**
39
+ * 把本地路径拼成页面能用的 URL(对标 Tauri 的 `convertFileSrc`)。
40
+ *
41
+ * **同步**:只拼字符串,不发 IPC、不查权限——真正的强制点在宿主侧
42
+ * (页面请求那个 URL 时查 `asset.read` 的 scope)。
43
+ */
44
+ convertFileSrc?(filePath: string): string;
45
+
46
+ /** 桥接脚本就绪的 Promise。 */
47
+ ready?: Promise<void>;
48
+
49
+ /**
50
+ * 订阅宿主事件(底层通道,**裸 payload**)。
51
+ *
52
+ * `@orielweb/api/event` 的 `listen` 在它之上包成 `{ event, id, payload }`。
53
+ * 直接用它得到的是裸值(`maximized` 给 `bool`、`theme.changed` 给字符串、
54
+ * `navigation.*` 给对象)——形状不统一,所以不推荐直接用。
55
+ *
56
+ * @param name 事件名。
57
+ * @param handler 处理器,参数是裸 payload。
58
+ * @returns 退订函数。
59
+ */
60
+ on?(name: string, handler: (payload: unknown) => void): () => void;
61
+ }
@@ -0,0 +1,112 @@
1
+ /**
2
+ * 当前窗口的 `WebviewWindow` 实例(对标 Tauri 的 `getCurrentWebviewWindow`)。
3
+ *
4
+ * @returns {WebviewWindow}
5
+ * @remarks **不会**建新窗口:它包装的是本窗口。
6
+ */
7
+ export function getCurrentWebviewWindow(): WebviewWindow;
8
+ /**
9
+ * 全部存活窗口(对标 Tauri 的 `getAllWebviewWindows`)。
10
+ *
11
+ * @returns {Promise<WebviewWindow[]>}
12
+ * @remarks 同样不建窗,只是把 `getAllWindows()` 的结果包成 `WebviewWindow`。
13
+ */
14
+ export function getAllWebviewWindows(): Promise<WebviewWindow[]>;
15
+ /**
16
+ * 一个可创建的窗口(对标 Tauri 的 `WebviewWindow`)。
17
+ *
18
+ * @extends Window
19
+ * @remarks
20
+ * 本库窗口即 webview,所以它与 {@link Window} 的行为完全相同;区别只在**构造时**:
21
+ * `new WebviewWindow(label, options)` 会让宿主建一个新窗口。
22
+ *
23
+ * 创建结果用 `created` 属性取(一个 Promise)。构造时挂了一个空 catch 防止"没人 await"
24
+ * 变成未处理拒绝——真正要处理失败时 `await w.created`,它仍然会 reject。
25
+ */
26
+ export class WebviewWindow extends Window {
27
+ /**
28
+ * 建一个新窗口并等它创建完成(推荐入口)。
29
+ *
30
+ * @param {string} label
31
+ * @param {Record<string, any>} [options]
32
+ * @returns {Promise<WebviewWindow>} 创建完成后的实例。
33
+ * @remarks 这是本库的扩展(Tauri 没有静态 `create`):把"构造 + 等结果"合成一步,
34
+ * 避免忘记 `await created()` 就往下走。
35
+ */
36
+ static create(label: string, options?: Record<string, any>): Promise<WebviewWindow>;
37
+ /** 当前窗口(不创建新窗口)。 */
38
+ static getCurrentWebviewWindow(): WebviewWindow;
39
+ /** 全部存活窗口(不创建新窗口)。 */
40
+ static getAllWebviewWindows(): Promise<WebviewWindow[]>;
41
+ /**
42
+ * @param {string} label 新窗口的 label(应用内唯一)。
43
+ * @param {Record<string, any>} [options] 建窗选项(见文件头)。
44
+ * @param {symbol} [internal] 内部标记:包装已存在的窗口时不建窗。
45
+ */
46
+ constructor(label: string, options?: Record<string, any>, internal?: symbol);
47
+ /** @type {Promise<void>} */
48
+ created: Promise<void>;
49
+ /**
50
+ * webview 在窗口内的偏移——本库恒为 `(0, 0)`(webview 铺满客户区)。
51
+ *
52
+ * @returns {Promise<LogicalPosition>}
53
+ */
54
+ position(): Promise<LogicalPosition>;
55
+ /**
56
+ * webview 在窗口内的尺寸——本库等于客户区尺寸({@link Window.innerSize})。
57
+ *
58
+ * @returns {Promise<any>}
59
+ */
60
+ size(): Promise<any>;
61
+ /**
62
+ * 开关"webview 随窗口缩放"——**补壳**:本库的 webview 永远跟着窗口变,没有可关的开关。
63
+ * 调用不报错、不生效;debug 模式下打警告。
64
+ *
65
+ * @param {boolean} [_autoResize]
66
+ * @returns {Promise<void>}
67
+ */
68
+ setAutoResize(_autoResize?: boolean): Promise<void>;
69
+ /**
70
+ * 把 webview 移到另一个窗口——**补壳**:本库没有"webview 可从窗口脱离"的模型。
71
+ * 调用不报错、不生效;debug 模式下打警告。
72
+ *
73
+ * @param {any} [_window]
74
+ * @returns {Promise<void>}
75
+ */
76
+ reparent(_window?: any): Promise<void>;
77
+ /**
78
+ * 设置缩放倍率(对标 Tauri 的 `Webview.setZoom`)。
79
+ *
80
+ * @param {number} scale 缩放倍率,`1.0` = 100%。必须是有限正数。
81
+ * @returns {Promise<void>}
82
+ * @remarks
83
+ * 三平台落点语义一致:Windows 是 WebView2 的 `ZoomFactor`、macOS 是
84
+ * `WKWebView.pageZoom`、Linux 是 `webkit_web_view_set_zoom_level`。
85
+ * 改的是**布局视口**(与浏览器 Ctrl+滚轮同一机制),不是 CSS transform——
86
+ * `100vw`、媒体查询、`devicePixelRatio` 都会跟着变。
87
+ *
88
+ * Windows 的引擎内部只接受 0.02–10.0,越界值会被引擎钳制(并以
89
+ * `ZoomFactorChanged` 回报实际值);本库不做二次钳制。
90
+ *
91
+ * **需要能力声明**(`window.setZoom`)。
92
+ */
93
+ setZoom(scale: number): Promise<void>;
94
+ /**
95
+ * 清除全部浏览数据(对标 Tauri 的 `Webview.clearAllBrowsingData`):
96
+ * 缓存、Cookie、localStorage、IndexedDB、Service Worker……
97
+ *
98
+ * @returns {Promise<void>}
99
+ * @remarks
100
+ * 三平台清的都是**本应用自己的**数据存储(Windows 是 profile 目录、
101
+ * macOS 是 `WKWebsiteDataStore.default`、Linux 是 `WebKitWebsiteDataManager`),
102
+ * 不涉及其他应用。
103
+ *
104
+ * **返回时清除已发起,不是已清完**:三平台的原生 API 都是异步的,
105
+ * 而本库的命令是同步契约——紧随其后的读取看到旧数据(理论上可能)
106
+ * 属于这个已知窗口。清除会**销毁登录态与本地存储**,所以
107
+ * **需要能力声明**(`window.clearAllBrowsingData`)——不该被一个被注入的页面随手调用。
108
+ */
109
+ clearAllBrowsingData(): Promise<void>;
110
+ }
111
+ import { Window } from './window.js';
112
+ import { LogicalPosition } from './dpi.js';