tauri-plugin-live-activity 0.2.1 → 0.5.0

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,138 @@
1
+ export const IDLE = Object.freeze({ status: 'idle', record: undefined, error: undefined });
2
+ const IMMEDIATE = Object.freeze({ kind: 'immediate' });
3
+ export class Session {
4
+ constructor(commands, changed) {
5
+ this.commands = commands;
6
+ this.changed = changed;
7
+ this.state = IDLE;
8
+ this.endPolicy = IMMEDIATE;
9
+ this.busy = false;
10
+ this.mounts = 0;
11
+ }
12
+ /** The id of the running activity. */
13
+ get activityId() {
14
+ return this.running?.record.id;
15
+ }
16
+ /** The options of a render. */
17
+ sync(options) {
18
+ this.endPolicy = options.endPolicy ?? IMMEDIATE;
19
+ if (options.enabled === false) {
20
+ this.wanted = undefined;
21
+ this.failedKey = undefined;
22
+ }
23
+ else {
24
+ this.wanted = options;
25
+ }
26
+ this.pump();
27
+ }
28
+ /** The component is gone: the activity ends, now or once the request in flight has answered. */
29
+ dispose() {
30
+ this.wanted = undefined;
31
+ this.pump();
32
+ }
33
+ /** The component mounted, also again in Strict Mode. */
34
+ keep() {
35
+ this.mounts += 1;
36
+ }
37
+ /** The component unmounted: it is disposed after a microtask, unless it mounts again before (Strict Mode). */
38
+ release() {
39
+ const mount = this.mounts;
40
+ queueMicrotask(() => {
41
+ if (this.mounts === mount)
42
+ this.dispose();
43
+ });
44
+ }
45
+ pump() {
46
+ if (this.busy)
47
+ return;
48
+ const wanted = this.wanted;
49
+ const running = this.running;
50
+ if (running && wanted?.activityKey !== running.record.activityKey)
51
+ return this.end(running.record);
52
+ if (!wanted)
53
+ return;
54
+ if (!running) {
55
+ if (wanted.activityKey !== this.failedKey)
56
+ this.start(wanted);
57
+ return;
58
+ }
59
+ if (!sameValues(wanted.values, running.values) && !sameValues(wanted.values, this.refused)) {
60
+ this.update(running, { ...wanted.values });
61
+ }
62
+ }
63
+ start(options) {
64
+ // A copy: values that the caller changes in place are compared with what was sent, not with themselves.
65
+ const values = { ...options.values };
66
+ const input = {
67
+ activityKey: options.activityKey,
68
+ schemaVersion: 2,
69
+ fallback: options.fallback ?? 'notification',
70
+ layout: options.layout,
71
+ snapshot: { revision: 0, values },
72
+ ...(options.styles === undefined ? {} : { styles: options.styles }),
73
+ ...(options.android === undefined ? {} : { android: options.android }),
74
+ };
75
+ this.failedKey = undefined;
76
+ this.refused = undefined;
77
+ this.set({ status: 'starting', record: undefined, error: undefined });
78
+ this.send(this.commands.startLayout(input), (record) => {
79
+ this.running = { record, values, revision: 0 };
80
+ this.set({ status: 'active', record, error: undefined });
81
+ }, (error) => {
82
+ // Blocks only the key still wanted: `enabled` off meanwhile, or another key, starts again.
83
+ if (this.wanted?.activityKey === options.activityKey)
84
+ this.failedKey = options.activityKey;
85
+ this.set({ status: 'failed', record: undefined, error });
86
+ });
87
+ }
88
+ update(running, values) {
89
+ // Every attempt takes the next revision, so a refused one is never reused.
90
+ const revision = running.revision + 1;
91
+ this.running = { ...running, revision };
92
+ this.send(this.commands.updateLayout({ id: running.record.id, snapshot: { revision, values } }), (record) => {
93
+ this.refused = undefined;
94
+ this.running = { record, values, revision };
95
+ this.set({ status: 'active', record, error: undefined });
96
+ }, (error) => {
97
+ this.refused = values;
98
+ this.set({ ...this.state, error });
99
+ });
100
+ }
101
+ end(record) {
102
+ this.set({ status: 'ending', record, error: this.state.error });
103
+ this.send(this.commands.endLayout({ id: record.id, policy: this.endPolicy }), (ended) => {
104
+ this.running = undefined;
105
+ this.set({ status: 'ended', record: ended, error: undefined });
106
+ }, (error) => {
107
+ // Not tried again: the activity is left to the system.
108
+ this.running = undefined;
109
+ this.set({ status: 'failed', record, error });
110
+ });
111
+ }
112
+ send(request, accepted, refused) {
113
+ this.busy = true;
114
+ request.then(accepted, (error) => refused(asError(error))).finally(() => {
115
+ this.busy = false;
116
+ this.pump();
117
+ });
118
+ }
119
+ set(state) {
120
+ this.state = state;
121
+ this.changed(state);
122
+ }
123
+ }
124
+ function sameValues(a, b) {
125
+ if (b === undefined)
126
+ return false;
127
+ const keys = Object.keys(a);
128
+ return keys.length === Object.keys(b).length && keys.every((key) => Object.is(a[key], b[key]));
129
+ }
130
+ /** The plugin rejects with `{ code, message }`; anything else reads as a `NATIVE_ERROR`. */
131
+ function asError(error) {
132
+ if (typeof error === 'object' && error !== null) {
133
+ const { code, message } = error;
134
+ if (typeof code === 'string' && typeof message === 'string')
135
+ return { code: code, message };
136
+ }
137
+ return { code: 'NATIVE_ERROR', message: error instanceof Error ? error.message : String(error) };
138
+ }
@@ -81,7 +81,8 @@ export interface EndInput {
81
81
  /** Unsupported policies fail explicitly instead of being silently ignored. */
82
82
  readonly policy: EndPolicy;
83
83
  }
84
- export type ActivityState = 'active' | 'stale' | 'ended' | 'dismissed' | 'unknown';
84
+ /** `pending`: scheduled and not started yet (iOS 26). */
85
+ export type ActivityState = 'pending' | 'active' | 'stale' | 'ended' | 'dismissed' | 'unknown';
85
86
  export type PromotionState = 'promoted' | 'notPromoted' | 'unknown' | 'notApplicable';
86
87
  export interface ActivityRecord {
87
88
  /** Opaque handle for this generation; a later start receives a new handle. */
@@ -99,7 +100,7 @@ export interface ActivityRecord {
99
100
  readonly promotionRequested: boolean;
100
101
  readonly promotionState: PromotionState;
101
102
  }
102
- export type ErrorCode = 'UNSUPPORTED_PLATFORM' | 'UNSUPPORTED_FEATURE' | 'PERMISSION_DENIED' | 'CONFIGURATION_REQUIRED' | 'INVALID_INPUT' | 'PAYLOAD_TOO_LARGE' | 'NOT_FOUND' | 'STALE_REVISION' | 'CONFLICT' | 'SYSTEM_LIMIT' | 'NATIVE_ERROR';
103
+ export type ErrorCode = 'UNSUPPORTED_PLATFORM' | 'UNSUPPORTED_FEATURE' | 'PERMISSION_DENIED' | 'NOT_FOREGROUND' | 'CONFIGURATION_REQUIRED' | 'INVALID_INPUT' | 'PAYLOAD_TOO_LARGE' | 'NOT_FOUND' | 'STALE_REVISION' | 'CONFLICT' | 'SYSTEM_LIMIT' | 'NATIVE_ERROR';
103
104
  /** Native plugin rejections are structured objects, not JavaScript Error instances. */
104
105
  export interface LiveActivityError {
105
106
  readonly code: ErrorCode;
@@ -112,12 +113,17 @@ export interface LayoutValues {
112
113
  readonly [key: string]: LayoutValue;
113
114
  }
114
115
  export interface LayoutSnapshot {
115
- /** Nonnegative JavaScript safe integer; strictly increases when the values change. */
116
+ /** Nonnegative JavaScript safe integer; strictly increases when the values change. Sent as given. */
116
117
  readonly revision: number;
117
118
  /** The complete map. An update keeps the keys and the value types accepted at start. */
118
119
  readonly values: LayoutValues;
119
120
  /** UTC Unix epoch milliseconds. */
120
121
  readonly staleAt?: number;
122
+ /**
123
+ * When the content was made, UTC Unix epoch milliseconds. ActivityKit (iOS 17.2) drops content older than what it
124
+ * shows; without it iOS takes the time the content is sent. Android keeps it in the record.
125
+ */
126
+ readonly timestamp?: number;
121
127
  }
122
128
  type ColorName = 'primary' | 'secondary' | 'accent' | 'red' | 'orange' | 'yellow' | 'green' | 'mint' | 'teal' | 'cyan' | 'blue' | 'indigo' | 'purple' | 'pink' | 'brown' | 'white' | 'black' | 'gray' | 'clear';
123
129
  /** `#RRGGBB` or `#RRGGBBAA`. */
@@ -127,6 +133,17 @@ export type LayoutColor = ColorName | HexColor | {
127
133
  readonly light: HexColor;
128
134
  readonly dark: HexColor;
129
135
  };
136
+ /**
137
+ * A value key in place of a literal, as `layout.bind(key)` writes it. The member reads the value on
138
+ * every render, so an update can change it. The value must be what the member takes as a literal: a
139
+ * color name or a hex color (not a light and dark pair), a weight name, or a number in the member's range.
140
+ */
141
+ export interface LayoutBound {
142
+ readonly bind: string;
143
+ }
144
+ /** A literal, or a value key that an update can change. */
145
+ export type LayoutBindable<T> = T | LayoutBound;
146
+ type FontWeight = 'ultraLight' | 'thin' | 'light' | 'regular' | 'medium' | 'semibold' | 'bold' | 'heavy' | 'black';
130
147
  type Anchor = 'center' | 'leading' | 'trailing' | 'top' | 'bottom' | 'topLeading' | 'topTrailing' | 'bottomLeading' | 'bottomTrailing';
131
148
  /** At least one member. Sizes are points from 0 to 1024. */
132
149
  export interface LayoutFrame {
@@ -138,12 +155,15 @@ export interface LayoutFrame {
138
155
  readonly maxHeight?: number | 'infinity';
139
156
  readonly alignment?: Anchor;
140
157
  }
158
+ /**
159
+ * `weight`, `color`, `background`, `cornerRadius`, `opacity`, `tint` and `size` may be bound to a value
160
+ * with `layout.bind(key)`. The other members are fixed at start.
161
+ */
141
162
  interface StyleFields {
142
- readonly weight?: 'ultraLight' | 'thin' | 'light' | 'regular' | 'medium' | 'semibold' | 'bold' | 'heavy' | 'black';
143
- readonly design?: 'default' | 'rounded' | 'serif' | 'monospaced';
163
+ readonly weight?: LayoutBindable<FontWeight>;
144
164
  readonly monospacedDigit?: boolean;
145
165
  /** Foreground color. */
146
- readonly color?: LayoutColor;
166
+ readonly color?: LayoutBindable<LayoutColor>;
147
167
  /** Integer from 1 to 10. */
148
168
  readonly lineLimit?: number;
149
169
  /** From 0.1 to 1. */
@@ -153,22 +173,34 @@ interface StyleFields {
153
173
  /** Points from 0 to 64: one number for every edge, or `[top, leading, bottom, trailing]`. */
154
174
  readonly padding?: number | readonly [number, number, number, number];
155
175
  readonly frame?: LayoutFrame;
156
- readonly background?: LayoutColor;
176
+ readonly background?: LayoutBindable<LayoutColor>;
157
177
  /** Points from 0 to 64. */
158
- readonly cornerRadius?: number;
178
+ readonly cornerRadius?: LayoutBindable<number>;
159
179
  /** From 0 to 1. */
160
- readonly opacity?: number;
180
+ readonly opacity?: LayoutBindable<number>;
161
181
  /** Tint of buttons, progress views and gauges. */
162
- readonly tint?: LayoutColor;
182
+ readonly tint?: LayoutBindable<LayoutColor>;
163
183
  readonly buttonStyle?: 'automatic' | 'bordered' | 'borderedProminent' | 'plain';
164
184
  }
165
- /** At least one member. `font`, a system text style, and `size`, points from 6 to 96, exclude each other. */
185
+ /**
186
+ * At least one member. `font`, a system text style, and `size`, points from 6 to 96, exclude each other.
187
+ * `fontFamily`, the PostScript or family name of a font that the widget extension bundles (`setup --font`),
188
+ * and `design`, a design of the system font, exclude each other too.
189
+ */
166
190
  export type LayoutStyle = StyleFields & ({
167
191
  readonly font?: 'largeTitle' | 'title' | 'title2' | 'title3' | 'headline' | 'subheadline' | 'body' | 'callout' | 'footnote' | 'caption' | 'caption2';
168
192
  readonly size?: never;
169
193
  } | {
170
- readonly size?: number;
194
+ readonly size?: LayoutBindable<number>;
171
195
  readonly font?: never;
196
+ }) & ({
197
+ readonly design?: 'default' | 'rounded' | 'serif' | 'monospaced';
198
+ readonly fontFamily?: never;
199
+ }
200
+ /** 1 to 64 ASCII letters, digits, spaces, `.`, `_` and `-`, starting with a letter or digit. */
201
+ | {
202
+ readonly fontFamily?: string;
203
+ readonly design?: never;
172
204
  });
173
205
  /** Members every node may carry. A condition names a value key; `if` and `unless` exclude each other. */
174
206
  type NodeFields = {
@@ -229,12 +261,20 @@ export type LayoutSymbolNode = NodeFields & {
229
261
  /** Key of a number value: the symbol's variable value, from 0 to 1. */
230
262
  readonly variableBind?: string;
231
263
  };
264
+ /**
265
+ * `asset`: an image in the widget extension's asset catalog. `file`: a PNG that the app wrote with
266
+ * `writeImage(file, png)`. A name is 1 to 64 ASCII letters, digits, `.`, `_` and `-`, starting with a letter or digit.
267
+ */
268
+ export type LayoutImageSource = {
269
+ readonly asset: string;
270
+ readonly file?: never;
271
+ } | {
272
+ readonly file: string;
273
+ readonly asset?: never;
274
+ };
232
275
  export type LayoutImageNode = NodeFields & {
233
276
  readonly type: 'image';
234
- /** Name of an image in the widget extension's asset catalog. */
235
- readonly source: {
236
- readonly asset: string;
237
- };
277
+ readonly source: LayoutImageSource;
238
278
  readonly resizable?: boolean;
239
279
  readonly contentMode?: 'fit' | 'fill';
240
280
  };
@@ -279,7 +319,24 @@ export type LayoutButtonNode = NodeFields & {
279
319
  readonly actionId: string;
280
320
  readonly child: LayoutNode;
281
321
  };
282
- export type LayoutNode = LayoutVStackNode | LayoutHStackNode | LayoutZStackNode | LayoutSpacerNode | LayoutDividerNode | LayoutTextNode | LayoutSymbolNode | LayoutImageNode | LayoutPathNode | LayoutProgressNode | LayoutGaugeNode | LayoutTimerTextNode | LayoutButtonNode;
322
+ /**
323
+ * A SwiftUI view that the app registers under `name` in LiveActivityCustomViews.swift of its widget extension.
324
+ * A name without a view draws nothing and is reported in `degradedFeatures` as `layout.custom.<name>`; Android
325
+ * shows no custom view (`layout.custom`).
326
+ */
327
+ export type LayoutCustomNode = NodeFields & {
328
+ readonly type: 'custom';
329
+ /** 1 to 64 ASCII letters, digits, `.`, `_` and `-`, starting with a letter or digit. */
330
+ readonly name: string;
331
+ /**
332
+ * 1 to 16 inputs of the view under value-key names: text of at most 200 code points, a number, a boolean, or
333
+ * `layout.bind(key)` for a value that each update can change.
334
+ */
335
+ readonly props?: {
336
+ readonly [name: string]: LayoutBindable<LayoutValue>;
337
+ };
338
+ };
339
+ export type LayoutNode = LayoutVStackNode | LayoutHStackNode | LayoutZStackNode | LayoutSpacerNode | LayoutDividerNode | LayoutTextNode | LayoutSymbolNode | LayoutImageNode | LayoutPathNode | LayoutProgressNode | LayoutGaugeNode | LayoutTimerTextNode | LayoutButtonNode | LayoutCustomNode;
283
340
  /** At least one region, each of at most 12 nodes. */
284
341
  export interface LayoutIslandExpanded {
285
342
  readonly leading?: LayoutNode;
@@ -303,9 +360,9 @@ export interface Layout {
303
360
  readonly lockScreen: LayoutNode;
304
361
  readonly island?: LayoutIsland;
305
362
  /** Tint behind the lock screen presentation. */
306
- readonly background?: LayoutColor;
363
+ readonly background?: LayoutBindable<LayoutColor>;
307
364
  /** Tint of the Dynamic Island keyline. */
308
- readonly keyline?: LayoutColor;
365
+ readonly keyline?: LayoutBindable<LayoutColor>;
309
366
  }
310
367
  export interface LayoutAndroidOptions {
311
368
  /** Lock screen visibility of the notification. */
@@ -332,6 +389,13 @@ export interface LayoutUpdateInput {
332
389
  /** Complete replacement snapshot, not a partial update. */
333
390
  readonly snapshot: LayoutSnapshot;
334
391
  }
392
+ /**
393
+ * The end of a layout activity. With `snapshot` it ends with that final content, checked as an update; without one it
394
+ * ends with the last accepted values.
395
+ */
396
+ export interface LayoutEndInput extends EndInput {
397
+ readonly snapshot?: LayoutSnapshot;
398
+ }
335
399
  /** The record does not repeat the layout. */
336
400
  export interface LayoutActivityRecord {
337
401
  /** Opaque handle for this generation; contract v1 commands do not see it. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tauri-plugin-live-activity",
3
- "version": "0.2.1",
3
+ "version": "0.5.0",
4
4
  "description": "Tauri 2 plugin for iOS Live Activities (ActivityKit and Dynamic Island) and Android Live Updates with ongoing notification fallback",
5
5
  "author": "1eedaegon",
6
6
  "type": "module",
@@ -17,6 +17,10 @@
17
17
  "main": "./dist-js/index.js",
18
18
  "exports": {
19
19
  ".": { "types": "./dist-js/index.d.ts", "import": "./dist-js/index.js" },
20
+ "./jsx": { "types": "./dist-js/jsx/index.d.ts", "import": "./dist-js/jsx/index.js" },
21
+ "./jsx/jsx-runtime": { "types": "./dist-js/jsx/jsx-runtime.d.ts", "import": "./dist-js/jsx/jsx-runtime.js" },
22
+ "./jsx/jsx-dev-runtime": { "types": "./dist-js/jsx/jsx-dev-runtime.d.ts", "import": "./dist-js/jsx/jsx-dev-runtime.js" },
23
+ "./react": { "types": "./dist-js/react/index.d.ts", "import": "./dist-js/react/index.js" },
20
24
  "./package.json": "./package.json"
21
25
  },
22
26
  "bin": { "tauri-plugin-live-activity": "cli/bin.mjs" },
@@ -28,9 +32,11 @@
28
32
  "scripts": {
29
33
  "build": "tsc -p tsconfig.json",
30
34
  "typecheck": "tsc -p tsconfig.json --noEmit && tsc -p examples/demo/tsconfig.json",
31
- "test": "npm run build && npm run test:contracts && npm run test:api && npm run test:demo && npm run test:setup",
32
- "test:contracts": "node --test --experimental-test-coverage \"--test-coverage-include=tools/validate-request.mjs\" --test-coverage-lines=80 --test-coverage-branches=80 --test-coverage-functions=80 tests/requests.test.mjs tests/layout-requests.test.mjs",
35
+ "test": "npm run build && npm run test:contracts && npm run test:api && npm run test:jsx && npm run test:react && npm run test:demo && npm run test:setup",
36
+ "test:contracts": "node --test --experimental-test-coverage \"--test-coverage-include=tools/validate-request.mjs\" --test-coverage-lines=80 --test-coverage-branches=80 --test-coverage-functions=80 tests/requests.test.mjs tests/layout-requests.test.mjs tests/contract-sync.test.mjs tests/android-liveupdate.test.mjs tests/ci.test.mjs tests/ci-cache.test.mjs tests/spec-coverage.test.mjs",
33
37
  "test:api": "node --test --experimental-test-coverage \"--test-coverage-include=dist-js/*.js\" --test-coverage-lines=80 --test-coverage-branches=80 --test-coverage-functions=80 tests/api.test.mjs",
38
+ "test:jsx": "node --experimental-strip-types --test --experimental-test-coverage \"--test-coverage-include=dist-js/jsx/*.js\" --test-coverage-lines=80 --test-coverage-branches=80 --test-coverage-functions=80 tests/jsx.test.mjs",
39
+ "test:react": "node --test --experimental-test-coverage \"--test-coverage-include=dist-js/react/*.js\" --test-coverage-lines=80 --test-coverage-branches=80 --test-coverage-functions=80 tests/react.test.mjs",
34
40
  "test:demo": "node --experimental-strip-types --test --experimental-test-coverage \"--test-coverage-include=examples/demo/src/*.ts\" \"--test-coverage-exclude=examples/demo/tests/**\" --test-coverage-lines=80 --test-coverage-branches=80 --test-coverage-functions=80 examples/demo/tests/self-test.test.mjs examples/demo/tests/ui-state.test.mjs examples/demo/tests/layouts.test.mjs examples/demo/tests/setup.test.mjs",
35
41
  "test:setup": "node --test --experimental-test-coverage \"--test-coverage-include=cli/*.mjs\" --test-coverage-lines=80 --test-coverage-branches=80 --test-coverage-functions=80 tests/setup.test.mjs",
36
42
  "build:demo": "npm run build && node examples/demo/build.mjs",
@@ -38,13 +44,20 @@
38
44
  "pack:check": "npm pack --dry-run"
39
45
  },
40
46
  "peerDependencies": {
41
- "@tauri-apps/api": "^2.12.4"
47
+ "@tauri-apps/api": "^2.12.4",
48
+ "react": ">=18"
49
+ },
50
+ "peerDependenciesMeta": {
51
+ "react": { "optional": true }
42
52
  },
43
53
  "devDependencies": {
44
54
  "@tauri-apps/cli": "2.12.4",
45
55
  "@tauri-apps/api": "2.12.4",
56
+ "@types/react": "19.3.0",
46
57
  "ajv": "8.20.0",
47
58
  "esbuild": "0.28.2",
59
+ "react": "19.3.1",
60
+ "react-test-renderer": "19.3.1",
48
61
  "typescript": "7.0.2"
49
62
  }
50
63
  }