tauri-plugin-live-activity 0.3.0 → 0.5.1
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.
- package/README.ko.md +69 -8
- package/README.md +70 -9
- package/cli/ios.mjs +65 -46
- package/cli/migration.mjs +55 -0
- package/cli/report.mjs +2 -2
- package/cli/setup.mjs +18 -7
- package/cli/steps.mjs +1 -1
- package/cli/templates/LiveActivityCustomViews.swift +25 -0
- package/cli/xcodegen.mjs +28 -20
- package/dist-js/index.d.ts +9 -5
- package/dist-js/index.js +7 -3
- package/dist-js/jsx/index.d.ts +59 -0
- package/dist-js/jsx/index.js +101 -0
- package/dist-js/jsx/jsx-dev-runtime.d.ts +4 -0
- package/dist-js/jsx/jsx-dev-runtime.js +6 -0
- package/dist-js/jsx/jsx-runtime.d.ts +16 -0
- package/dist-js/jsx/jsx-runtime.js +9 -0
- package/dist-js/react/index.d.ts +12 -0
- package/dist-js/react/index.js +42 -0
- package/dist-js/react/session.d.ts +68 -0
- package/dist-js/react/session.js +138 -0
- package/dist-js/types.d.ts +16 -3
- package/package.json +17 -4
package/cli/xcodegen.mjs
CHANGED
|
@@ -6,10 +6,15 @@ export const SPEC_FILE = 'live-activity.yml';
|
|
|
6
6
|
export const WIDGET = 'LiveActivityWidget';
|
|
7
7
|
/** The extension's entitlements, next to its folder so that XcodeGen does not take them for a source. */
|
|
8
8
|
export const WIDGET_ENTITLEMENTS = `${WIDGET}.entitlements`;
|
|
9
|
-
/** The Info.plist key of the extension that names the App Group, for
|
|
10
|
-
export const APP_GROUP_KEY = '
|
|
11
|
-
/** The
|
|
12
|
-
|
|
9
|
+
/** The Info.plist key of the extension that names the App Group, for activitykit and the widget. */
|
|
10
|
+
export const APP_GROUP_KEY = 'ActivityKitAppGroup';
|
|
11
|
+
/** The key of 0.2 and 0.3, which a spec they wrote may hold. */
|
|
12
|
+
const OLD_APP_GROUP_KEY = 'TauriLiveActivityAppGroup';
|
|
13
|
+
/**
|
|
14
|
+
* The files of the widget folder that the app target compiles too. The custom views file is Swift, so the app target
|
|
15
|
+
* compiles Swift, which Xcode needs to take the App Intents metadata of the linked ActivityKitIntent product.
|
|
16
|
+
*/
|
|
17
|
+
export const APP_SOURCES = ['LiveActivityCustomViews.swift'];
|
|
13
18
|
|
|
14
19
|
const DIGEST_LINE = /^# setup-digest: ([0-9a-f]{16})\n/m;
|
|
15
20
|
|
|
@@ -76,9 +81,9 @@ export function readProject(text) {
|
|
|
76
81
|
};
|
|
77
82
|
}
|
|
78
83
|
|
|
79
|
-
/** The App Group of a spec that setup wrote, so that a later run keeps it. */
|
|
84
|
+
/** The App Group of a spec that setup wrote, this version or 0.2 and 0.3, so that a later run keeps it. */
|
|
80
85
|
export function appGroupOf(spec = '') {
|
|
81
|
-
const value = new RegExp(`^\\s
|
|
86
|
+
const value = new RegExp(`^\\s+(?:${APP_GROUP_KEY}|${OLD_APP_GROUP_KEY}): ("[^"\\n]*")$`, 'm').exec(spec)?.[1];
|
|
82
87
|
return value && JSON.parse(value);
|
|
83
88
|
}
|
|
84
89
|
|
|
@@ -114,10 +119,10 @@ export function writtenBySetup(text) {
|
|
|
114
119
|
const quoted = value => JSON.stringify(String(value));
|
|
115
120
|
|
|
116
121
|
/**
|
|
117
|
-
* The included spec: the widget extension target, and
|
|
118
|
-
*
|
|
119
|
-
* targets, the app's at `appEntitlements`, and to the extension's Info.plist, where
|
|
120
|
-
* read it. XcodeGen merges the app target's part into the target of project.yml.
|
|
122
|
+
* The included spec: activitykit's Swift package, the widget extension target, and in the app target the extension,
|
|
123
|
+
* the button intent and the custom views. `fonts` go to the extension's UIAppFonts. An `appGroup` goes to the
|
|
124
|
+
* entitlements of both targets, the app's at `appEntitlements`, and to the extension's Info.plist, where activitykit
|
|
125
|
+
* and the widget read it. XcodeGen merges the app target's part into the target of project.yml.
|
|
121
126
|
*/
|
|
122
127
|
export function widgetSpec({
|
|
123
128
|
pluginVersion, packagePath, appTarget, bundleId, appVersion, buildVersion, teamGroup, fonts = [], appGroup, appEntitlements,
|
|
@@ -134,14 +139,19 @@ export function widgetSpec({
|
|
|
134
139
|
' properties:',
|
|
135
140
|
` com.apple.security.application-groups: [${quoted(appGroup)}]`,
|
|
136
141
|
]);
|
|
142
|
+
const product = name => [' - package: ActivityKit', ` product: ${name}`];
|
|
137
143
|
const body = [
|
|
138
144
|
'packages:',
|
|
139
|
-
'
|
|
145
|
+
' ActivityKit:',
|
|
140
146
|
` path: ${quoted(packagePath)}`,
|
|
141
147
|
'targets:',
|
|
142
148
|
` ${quoted(appTarget)}:`,
|
|
143
149
|
` sources: [${APP_SOURCES.map(file => `${WIDGET}/${file}`).join(', ')}]`,
|
|
144
|
-
|
|
150
|
+
' dependencies:',
|
|
151
|
+
` - target: ${WIDGET}`,
|
|
152
|
+
// The system runs a button's intent in the app. The bridge itself is in the Rust library already.
|
|
153
|
+
...product('ActivityKitIntent'),
|
|
154
|
+
...product('ActivityKitViews'),
|
|
145
155
|
...entitlements(quoted(appEntitlements)),
|
|
146
156
|
' settings:',
|
|
147
157
|
' base:',
|
|
@@ -149,21 +159,19 @@ export function widgetSpec({
|
|
|
149
159
|
` ${WIDGET}:`,
|
|
150
160
|
' type: app-extension',
|
|
151
161
|
' platform: iOS',
|
|
152
|
-
' deploymentTarget: "16.
|
|
162
|
+
' deploymentTarget: "16.2"',
|
|
153
163
|
` sources: [${WIDGET}]`,
|
|
154
164
|
' dependencies:',
|
|
155
|
-
'
|
|
156
|
-
|
|
157
|
-
' - package: LiveActivity',
|
|
158
|
-
' product: LiveActivityUI',
|
|
165
|
+
...product('ActivityKitWidget'),
|
|
166
|
+
...product('ActivityKitViews'),
|
|
159
167
|
' info:',
|
|
160
168
|
` path: ${WIDGET}/Info.plist`,
|
|
161
169
|
' properties:',
|
|
162
170
|
...versions,
|
|
163
171
|
' NSExtension:',
|
|
164
172
|
' NSExtensionPointIdentifier: com.apple.widgetkit-extension',
|
|
165
|
-
' #
|
|
166
|
-
'
|
|
173
|
+
' # A start answers CONFIGURATION_REQUIRED without an extension that declares this.',
|
|
174
|
+
' ActivityKitLayout: 2',
|
|
167
175
|
...fontList,
|
|
168
176
|
...group,
|
|
169
177
|
...entitlements(WIDGET_ENTITLEMENTS),
|
|
@@ -179,7 +187,7 @@ export function widgetSpec({
|
|
|
179
187
|
].join('\n');
|
|
180
188
|
return [
|
|
181
189
|
`# Written by \`npx tauri-plugin-live-activity setup\` for tauri-plugin-live-activity ${pluginVersion}.`,
|
|
182
|
-
'# project.yml includes it. Setup writes it again for a new plugin version or app version, unless',
|
|
190
|
+
'# project.yml includes it. Setup writes it again for a new plugin or activitykit version or app version, unless',
|
|
183
191
|
'# the lines below were changed by hand.',
|
|
184
192
|
`# setup-digest: ${digest(body)}`,
|
|
185
193
|
body,
|
package/dist-js/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type UnlistenFn } from '@tauri-apps/api/event';
|
|
2
|
-
import type { ActionEvent, ActivityRecord, Capabilities, EndInput, LayoutActivityRecord, LayoutStartInput, LayoutUpdateInput, PermissionKind, Permissions, StartInput, UpdateInput } from './types.js';
|
|
2
|
+
import type { ActionEvent, ActivityRecord, Capabilities, EndInput, LayoutActivityRecord, LayoutEndInput, LayoutStartInput, LayoutUpdateInput, PermissionKind, Permissions, StartInput, UpdateInput } from './types.js';
|
|
3
3
|
export type * from './types.js';
|
|
4
4
|
/** Builders for the nodes of a layout tree. */
|
|
5
5
|
export * as layout from './layout.js';
|
|
@@ -21,20 +21,24 @@ export declare function list(): Promise<readonly ActivityRecord[]>;
|
|
|
21
21
|
export declare function startLayout(input: LayoutStartInput): Promise<LayoutActivityRecord>;
|
|
22
22
|
/** Replace every value with a newer revision; the layout stays as started. */
|
|
23
23
|
export declare function updateLayout(input: LayoutUpdateInput): Promise<LayoutActivityRecord>;
|
|
24
|
-
/** End this layout generation. A late update cannot revive it. */
|
|
25
|
-
export declare function endLayout(input:
|
|
24
|
+
/** End this layout generation, with final content when `snapshot` is given. A late update cannot revive it. */
|
|
25
|
+
export declare function endLayout(input: LayoutEndInput): Promise<LayoutActivityRecord>;
|
|
26
26
|
/** Reconcile plugin-owned layout records with the OS; `list` reports contract v1 activities. */
|
|
27
27
|
export declare function listLayout(): Promise<readonly LayoutActivityRecord[]>;
|
|
28
28
|
/**
|
|
29
29
|
* Write a PNG that `image` nodes with the source `{ file: name }` show, replacing one of that name. The widget
|
|
30
30
|
* reads it each time it draws the activity, on start and on every update, so write the picture first and then
|
|
31
31
|
* update. At most 1 MiB and 1224 x 480 pixels (`PAYLOAD_TOO_LARGE` beyond). On iOS it needs the App Group
|
|
32
|
-
* that `setup --app-group` configures (`CONFIGURATION_REQUIRED` without one)
|
|
32
|
+
* that `setup --app-group` configures (`CONFIGURATION_REQUIRED` without one), where activitykit writes it to
|
|
33
|
+
* `activitykit/images`; on Android the first such image
|
|
33
34
|
* becomes the notification's large icon.
|
|
34
35
|
*/
|
|
35
36
|
export declare function writeImage(name: string, png: Uint8Array | ArrayBuffer): Promise<void>;
|
|
36
37
|
/**
|
|
37
38
|
* Receive button taps while this webview is alive; resolves to the function that stops listening.
|
|
38
|
-
* Listening uses Tauri's event permissions (`core:event:default`).
|
|
39
|
+
* Listening uses Tauri's event permissions (`core:event:default`). The tap reaches the page after the Rust
|
|
40
|
+
* `on_action` handlers have run; the tap's intent does not wait for this handler, which runs in the webview whenever it
|
|
41
|
+
* gets the event, and not at all while the webview is suspended or was never created. Act on taps in Rust when they
|
|
42
|
+
* must not be lost.
|
|
39
43
|
*/
|
|
40
44
|
export declare function onAction(handler: (event: ActionEvent) => void): Promise<UnlistenFn>;
|
package/dist-js/index.js
CHANGED
|
@@ -38,7 +38,7 @@ export function startLayout(input) {
|
|
|
38
38
|
export function updateLayout(input) {
|
|
39
39
|
return invoke('plugin:live-activity|update_layout', { input });
|
|
40
40
|
}
|
|
41
|
-
/** End this layout generation. A late update cannot revive it. */
|
|
41
|
+
/** End this layout generation, with final content when `snapshot` is given. A late update cannot revive it. */
|
|
42
42
|
export function endLayout(input) {
|
|
43
43
|
return invoke('plugin:live-activity|end_layout', { input });
|
|
44
44
|
}
|
|
@@ -50,7 +50,8 @@ export function listLayout() {
|
|
|
50
50
|
* Write a PNG that `image` nodes with the source `{ file: name }` show, replacing one of that name. The widget
|
|
51
51
|
* reads it each time it draws the activity, on start and on every update, so write the picture first and then
|
|
52
52
|
* update. At most 1 MiB and 1224 x 480 pixels (`PAYLOAD_TOO_LARGE` beyond). On iOS it needs the App Group
|
|
53
|
-
* that `setup --app-group` configures (`CONFIGURATION_REQUIRED` without one)
|
|
53
|
+
* that `setup --app-group` configures (`CONFIGURATION_REQUIRED` without one), where activitykit writes it to
|
|
54
|
+
* `activitykit/images`; on Android the first such image
|
|
54
55
|
* becomes the notification's large icon.
|
|
55
56
|
*/
|
|
56
57
|
export function writeImage(name, png) {
|
|
@@ -59,7 +60,10 @@ export function writeImage(name, png) {
|
|
|
59
60
|
}
|
|
60
61
|
/**
|
|
61
62
|
* Receive button taps while this webview is alive; resolves to the function that stops listening.
|
|
62
|
-
* Listening uses Tauri's event permissions (`core:event:default`).
|
|
63
|
+
* Listening uses Tauri's event permissions (`core:event:default`). The tap reaches the page after the Rust
|
|
64
|
+
* `on_action` handlers have run; the tap's intent does not wait for this handler, which runs in the webview whenever it
|
|
65
|
+
* gets the event, and not at all while the webview is suspended or was never created. Act on taps in Rust when they
|
|
66
|
+
* must not be lost.
|
|
63
67
|
*/
|
|
64
68
|
export function onAction(handler) {
|
|
65
69
|
return listen('live-activity://action', event => handler(event.payload));
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { LayoutButtonNode, LayoutCustomNode, LayoutDividerNode, LayoutGaugeNode, LayoutHStackNode, LayoutImageNode, LayoutNode, LayoutPathNode, LayoutProgressNode, LayoutSpacerNode, LayoutSymbolNode, LayoutTextNode, LayoutTimerTextNode, LayoutVStackNode, LayoutZStackNode } from '../types.js';
|
|
2
|
+
export { bind } from '../layout.js';
|
|
3
|
+
/** What a stack takes as children: nodes, arrays of them, and booleans, `null` or `undefined`, which are left out. */
|
|
4
|
+
export type LayoutChild = LayoutNode | readonly LayoutChild[] | boolean | null | undefined;
|
|
5
|
+
/** What `Text` takes as children: strings and numbers, joined, and booleans, `null` or `undefined`, left out. */
|
|
6
|
+
export type TextChild = string | number | readonly TextChild[] | boolean | null | undefined;
|
|
7
|
+
/** The members of node `N` but its type and the members `Taken` that a component takes another way. */
|
|
8
|
+
type Props<N extends LayoutNode, Taken extends PropertyKey = never> = N extends unknown ? Omit<N, 'type' | Taken> : never;
|
|
9
|
+
type StackProps<N extends LayoutNode> = Props<N, 'children'> & {
|
|
10
|
+
readonly children: LayoutChild;
|
|
11
|
+
};
|
|
12
|
+
export type VStackProps = StackProps<LayoutVStackNode>;
|
|
13
|
+
export type HStackProps = StackProps<LayoutHStackNode>;
|
|
14
|
+
export type ZStackProps = StackProps<LayoutZStackNode>;
|
|
15
|
+
export type TextProps = Props<LayoutDividerNode> & ({
|
|
16
|
+
readonly children: TextChild;
|
|
17
|
+
readonly bind?: never;
|
|
18
|
+
} | {
|
|
19
|
+
readonly bind: string;
|
|
20
|
+
readonly children?: never;
|
|
21
|
+
});
|
|
22
|
+
export type ImageProps = Props<LayoutImageNode, 'source'> & ({
|
|
23
|
+
readonly asset: string;
|
|
24
|
+
readonly file?: never;
|
|
25
|
+
} | {
|
|
26
|
+
readonly file: string;
|
|
27
|
+
readonly asset?: never;
|
|
28
|
+
});
|
|
29
|
+
export type ButtonProps = Props<LayoutButtonNode, 'child'> & {
|
|
30
|
+
readonly children: LayoutNode;
|
|
31
|
+
};
|
|
32
|
+
/** Children from top to bottom. */
|
|
33
|
+
export declare function VStack({ children, ...options }: VStackProps): LayoutVStackNode;
|
|
34
|
+
/** Children side by side. */
|
|
35
|
+
export declare function HStack({ children, ...options }: HStackProps): LayoutHStackNode;
|
|
36
|
+
/** Children on top of each other, the first at the back. */
|
|
37
|
+
export declare function ZStack({ children, ...options }: ZStackProps): LayoutZStackNode;
|
|
38
|
+
/** Flexible space inside a stack. */
|
|
39
|
+
export declare function Spacer(props: Props<LayoutSpacerNode>): LayoutSpacerNode;
|
|
40
|
+
/** A separator line. */
|
|
41
|
+
export declare function Divider(props: Props<LayoutDividerNode>): LayoutDividerNode;
|
|
42
|
+
/** Literal text from its children, or with `bind` the value of that key. */
|
|
43
|
+
export declare function Text({ children, bind, ...options }: TextProps): LayoutTextNode;
|
|
44
|
+
/** An SF Symbol. */
|
|
45
|
+
export declare function SFSymbol({ name, ...options }: Props<LayoutSymbolNode>): LayoutSymbolNode;
|
|
46
|
+
/** An image of the widget extension's asset catalog (`asset`), or a PNG that the app wrote with `writeImage` (`file`). */
|
|
47
|
+
export declare function Image({ asset, file, ...options }: ImageProps): LayoutImageNode;
|
|
48
|
+
/** A shape from SVG path data `d`, drawn in a `box` of `[width, height]`. */
|
|
49
|
+
export declare function Path({ d, box, ...options }: Props<LayoutPathNode>): LayoutPathNode;
|
|
50
|
+
/** A progress bar or ring for the number value of `bind`. */
|
|
51
|
+
export declare function Progress({ bind, ...options }: Props<LayoutProgressNode>): LayoutProgressNode;
|
|
52
|
+
/** A gauge for the number value of `bind`. */
|
|
53
|
+
export declare function Gauge({ bind, ...options }: Props<LayoutGaugeNode>): LayoutGaugeNode;
|
|
54
|
+
/** A timer the system draws between the epoch-millisecond values of the `start` and `end` keys. */
|
|
55
|
+
export declare function TimerText({ start, end, ...options }: Props<LayoutTimerTextNode>): LayoutTimerTextNode;
|
|
56
|
+
/** A button around its one child; a tap reports `actionId` to `onAction`. */
|
|
57
|
+
export declare function Button({ actionId, children, ...options }: ButtonProps): LayoutButtonNode;
|
|
58
|
+
/** A SwiftUI view that the app registers under `name`; `props` are its inputs. */
|
|
59
|
+
export declare function Custom({ name, ...options }: Props<LayoutCustomNode>): LayoutCustomNode;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// Layout components for TSX. Each returns the plain node object of its `layout` builder; nothing here
|
|
2
|
+
// imports Tauri or React, so a server can build the same layout.
|
|
3
|
+
import { bound, button, custom, divider, gauge, hstack, image, path, progress, spacer, symbol, text, timerText, vstack, zstack, } from '../layout.js';
|
|
4
|
+
export { bind } from '../layout.js';
|
|
5
|
+
/** Children from top to bottom. */
|
|
6
|
+
export function VStack({ children, ...options }) {
|
|
7
|
+
return vstack(nodes('VStack', children), options);
|
|
8
|
+
}
|
|
9
|
+
/** Children side by side. */
|
|
10
|
+
export function HStack({ children, ...options }) {
|
|
11
|
+
return hstack(nodes('HStack', children), options);
|
|
12
|
+
}
|
|
13
|
+
/** Children on top of each other, the first at the back. */
|
|
14
|
+
export function ZStack({ children, ...options }) {
|
|
15
|
+
return zstack(nodes('ZStack', children), options);
|
|
16
|
+
}
|
|
17
|
+
/** Flexible space inside a stack. */
|
|
18
|
+
export function Spacer(props) {
|
|
19
|
+
return spacer(props);
|
|
20
|
+
}
|
|
21
|
+
/** A separator line. */
|
|
22
|
+
export function Divider(props) {
|
|
23
|
+
return divider(props);
|
|
24
|
+
}
|
|
25
|
+
/** Literal text from its children, or with `bind` the value of that key. */
|
|
26
|
+
export function Text({ children, bind, ...options }) {
|
|
27
|
+
if (bind !== undefined) {
|
|
28
|
+
if (children !== undefined)
|
|
29
|
+
throw new TypeError('Text: takes children or bind, not both');
|
|
30
|
+
return bound(bind, options);
|
|
31
|
+
}
|
|
32
|
+
if (children === undefined)
|
|
33
|
+
throw new TypeError('Text: needs children or bind');
|
|
34
|
+
return text(words(children), options);
|
|
35
|
+
}
|
|
36
|
+
/** An SF Symbol. */
|
|
37
|
+
export function SFSymbol({ name, ...options }) {
|
|
38
|
+
return symbol(name, options);
|
|
39
|
+
}
|
|
40
|
+
/** An image of the widget extension's asset catalog (`asset`), or a PNG that the app wrote with `writeImage` (`file`). */
|
|
41
|
+
export function Image({ asset, file, ...options }) {
|
|
42
|
+
if ((asset === undefined) === (file === undefined))
|
|
43
|
+
throw new TypeError('Image: takes either asset or file');
|
|
44
|
+
return image(asset ?? { file: file }, options);
|
|
45
|
+
}
|
|
46
|
+
/** A shape from SVG path data `d`, drawn in a `box` of `[width, height]`. */
|
|
47
|
+
export function Path({ d, box, ...options }) {
|
|
48
|
+
return path(d, box, options);
|
|
49
|
+
}
|
|
50
|
+
/** A progress bar or ring for the number value of `bind`. */
|
|
51
|
+
export function Progress({ bind, ...options }) {
|
|
52
|
+
return progress(bind, options);
|
|
53
|
+
}
|
|
54
|
+
/** A gauge for the number value of `bind`. */
|
|
55
|
+
export function Gauge({ bind, ...options }) {
|
|
56
|
+
return gauge(bind, options);
|
|
57
|
+
}
|
|
58
|
+
/** A timer the system draws between the epoch-millisecond values of the `start` and `end` keys. */
|
|
59
|
+
export function TimerText({ start, end, ...options }) {
|
|
60
|
+
return timerText(start, end, options);
|
|
61
|
+
}
|
|
62
|
+
/** A button around its one child; a tap reports `actionId` to `onAction`. */
|
|
63
|
+
export function Button({ actionId, children, ...options }) {
|
|
64
|
+
const child = Array.isArray(children) && children.length === 1 ? children[0] : children;
|
|
65
|
+
if (!isNode(child))
|
|
66
|
+
throw new TypeError('Button: needs exactly one node as its child');
|
|
67
|
+
return button(actionId, child, options);
|
|
68
|
+
}
|
|
69
|
+
/** A SwiftUI view that the app registers under `name`; `props` are its inputs. */
|
|
70
|
+
export function Custom({ name, ...options }) {
|
|
71
|
+
return custom(name, options);
|
|
72
|
+
}
|
|
73
|
+
function isNode(value) {
|
|
74
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
75
|
+
&& typeof value.type === 'string';
|
|
76
|
+
}
|
|
77
|
+
function nodes(component, children) {
|
|
78
|
+
const found = [];
|
|
79
|
+
const visit = (child) => {
|
|
80
|
+
if (typeof child === 'boolean' || child === null || child === undefined)
|
|
81
|
+
return;
|
|
82
|
+
if (Array.isArray(child))
|
|
83
|
+
return child.forEach(visit);
|
|
84
|
+
if (!isNode(child))
|
|
85
|
+
throw new TypeError(`${component}: a child must be a layout node; put text in <Text>`);
|
|
86
|
+
found.push(child);
|
|
87
|
+
};
|
|
88
|
+
visit(children);
|
|
89
|
+
if (found.length === 0)
|
|
90
|
+
throw new TypeError(`${component}: needs at least one child`);
|
|
91
|
+
return found;
|
|
92
|
+
}
|
|
93
|
+
function words(children) {
|
|
94
|
+
if (typeof children === 'boolean' || children === null || children === undefined)
|
|
95
|
+
return '';
|
|
96
|
+
if (Array.isArray(children))
|
|
97
|
+
return children.map(words).join('');
|
|
98
|
+
if (typeof children === 'string' || typeof children === 'number')
|
|
99
|
+
return String(children);
|
|
100
|
+
throw new TypeError('Text: a child must be a string or a number');
|
|
101
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { LayoutNode } from '../types.js';
|
|
2
|
+
export type { JSX } from './jsx-runtime.js';
|
|
3
|
+
/** Calls the component with its props; the key and the source location are dropped. */
|
|
4
|
+
export declare function jsxDEV(type: unknown, props: unknown, _key?: unknown, _isStatic?: boolean, _source?: unknown, _self?: unknown): LayoutNode;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// The development JSX runtime (`"jsx": "react-jsxdev"`): the same elements as the production runtime.
|
|
2
|
+
import { jsx } from './jsx-runtime.js';
|
|
3
|
+
/** Calls the component with its props; the key and the source location are dropped. */
|
|
4
|
+
export function jsxDEV(type, props, _key, _isStatic, _source, _self) {
|
|
5
|
+
return jsx(type, props);
|
|
6
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { LayoutNode } from '../types.js';
|
|
2
|
+
type Component = (props: never) => LayoutNode;
|
|
3
|
+
export declare namespace JSX {
|
|
4
|
+
type Element = LayoutNode;
|
|
5
|
+
type ElementType = Component;
|
|
6
|
+
interface IntrinsicElements {
|
|
7
|
+
}
|
|
8
|
+
interface ElementChildrenAttribute {
|
|
9
|
+
children: unknown;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/** Calls the component with its props. The key has no place in a layout node. */
|
|
13
|
+
export declare function jsx(type: unknown, props: unknown, _key?: unknown): LayoutNode;
|
|
14
|
+
/** The same for an element with several static children. */
|
|
15
|
+
export declare const jsxs: typeof jsx;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Calls the component with its props. The key has no place in a layout node. */
|
|
2
|
+
export function jsx(type, props, _key) {
|
|
3
|
+
if (typeof type !== 'function') {
|
|
4
|
+
throw new TypeError(`a layout has no element <${String(type)}>: use the components of tauri-plugin-live-activity/jsx`);
|
|
5
|
+
}
|
|
6
|
+
return type(props);
|
|
7
|
+
}
|
|
8
|
+
/** The same for an element with several static children. */
|
|
9
|
+
export const jsxs = jsx;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ActionEvent } from '../types.js';
|
|
2
|
+
import { type LiveActivityState, type SessionOptions } from './session.js';
|
|
3
|
+
export type { LiveActivityState, LiveActivityStatus } from './session.js';
|
|
4
|
+
export interface UseLiveActivityOptions extends SessionOptions {
|
|
5
|
+
/** Taps on the buttons of this activity; taps of other activities are not passed on. */
|
|
6
|
+
readonly onAction?: (event: ActionEvent) => void;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Starts a layout activity on mount, sends `values` as an update whenever they change, and ends the activity on
|
|
10
|
+
* unmount or when `enabled` turns false.
|
|
11
|
+
*/
|
|
12
|
+
export declare function useLiveActivity(options: UseLiveActivityOptions): LiveActivityState;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// `useLiveActivity`: one layout activity kept in step with a React component. Only this entry imports React.
|
|
2
|
+
import { useEffect, useRef, useState } from 'react';
|
|
3
|
+
import { endLayout, onAction, startLayout, updateLayout } from '../index.js';
|
|
4
|
+
import { IDLE, Session } from './session.js';
|
|
5
|
+
const commands = { startLayout, updateLayout, endLayout };
|
|
6
|
+
/**
|
|
7
|
+
* Starts a layout activity on mount, sends `values` as an update whenever they change, and ends the activity on
|
|
8
|
+
* unmount or when `enabled` turns false.
|
|
9
|
+
*/
|
|
10
|
+
export function useLiveActivity(options) {
|
|
11
|
+
const [state, setState] = useState(IDLE);
|
|
12
|
+
const session = useRef(null);
|
|
13
|
+
if (session.current === null)
|
|
14
|
+
session.current = new Session(commands, setState);
|
|
15
|
+
const handler = useRef(options.onAction);
|
|
16
|
+
handler.current = options.onAction;
|
|
17
|
+
const listening = options.onAction !== undefined;
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
session.current?.sync(options);
|
|
20
|
+
});
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
const current = session.current;
|
|
23
|
+
// Strict Mode unmounts and mounts again at once: the end waits for a microtask, which the mount cancels.
|
|
24
|
+
current?.keep();
|
|
25
|
+
return () => current?.release();
|
|
26
|
+
}, []);
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
if (!listening)
|
|
29
|
+
return undefined;
|
|
30
|
+
let open = true;
|
|
31
|
+
const stop = onAction((event) => {
|
|
32
|
+
const running = session.current?.activityId;
|
|
33
|
+
if (open && running !== undefined && event.activityId === running)
|
|
34
|
+
handler.current?.(event);
|
|
35
|
+
});
|
|
36
|
+
return () => {
|
|
37
|
+
open = false;
|
|
38
|
+
stop.then((unlisten) => unlisten(), () => { });
|
|
39
|
+
};
|
|
40
|
+
}, [listening]);
|
|
41
|
+
return state;
|
|
42
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { EndPolicy, Fallback, Layout, LayoutActivityRecord, LayoutAndroidOptions, LayoutEndInput, LayoutStartInput, LayoutStyle, LayoutUpdateInput, LayoutValues, LiveActivityError } from '../types.js';
|
|
2
|
+
export interface SessionOptions {
|
|
3
|
+
/** The app's key of the activity; another key ends the running activity and starts one for it. */
|
|
4
|
+
readonly activityKey: string;
|
|
5
|
+
/** Read at start: a change while the activity runs is not sent, since the layout is fixed for a generation. */
|
|
6
|
+
readonly layout: Layout;
|
|
7
|
+
/** The values to show; a render with other values sends them as an update. */
|
|
8
|
+
readonly values: LayoutValues;
|
|
9
|
+
/** Read at start. */
|
|
10
|
+
readonly styles?: {
|
|
11
|
+
readonly [token: string]: LayoutStyle;
|
|
12
|
+
};
|
|
13
|
+
/** Read at start. Defaults to `notification`. */
|
|
14
|
+
readonly fallback?: Fallback;
|
|
15
|
+
/** Read at start. */
|
|
16
|
+
readonly android?: LayoutAndroidOptions;
|
|
17
|
+
/** False ends the activity, and true starts it again. Defaults to true. */
|
|
18
|
+
readonly enabled?: boolean;
|
|
19
|
+
/** How the activity ends on unmount or when `enabled` turns false. Defaults to `{ kind: 'immediate' }`. */
|
|
20
|
+
readonly endPolicy?: EndPolicy;
|
|
21
|
+
}
|
|
22
|
+
export type LiveActivityStatus = 'idle' | 'starting' | 'active' | 'ending' | 'ended' | 'failed';
|
|
23
|
+
export interface LiveActivityState {
|
|
24
|
+
readonly status: LiveActivityStatus;
|
|
25
|
+
/** The plugin's newest record of the activity. */
|
|
26
|
+
readonly record: LayoutActivityRecord | undefined;
|
|
27
|
+
/** The last refusal; an accepted request clears it. */
|
|
28
|
+
readonly error: LiveActivityError | undefined;
|
|
29
|
+
}
|
|
30
|
+
/** The commands the session sends. */
|
|
31
|
+
export interface SessionCommands {
|
|
32
|
+
readonly startLayout: (input: LayoutStartInput) => Promise<LayoutActivityRecord>;
|
|
33
|
+
readonly updateLayout: (input: LayoutUpdateInput) => Promise<LayoutActivityRecord>;
|
|
34
|
+
readonly endLayout: (input: LayoutEndInput) => Promise<LayoutActivityRecord>;
|
|
35
|
+
}
|
|
36
|
+
export declare const IDLE: LiveActivityState;
|
|
37
|
+
export declare class Session {
|
|
38
|
+
private readonly commands;
|
|
39
|
+
private readonly changed;
|
|
40
|
+
private state;
|
|
41
|
+
private wanted;
|
|
42
|
+
private endPolicy;
|
|
43
|
+
/** The running activity of this generation, and the values and revision it last took. */
|
|
44
|
+
private running;
|
|
45
|
+
/** Values that an update was refused with: not sent again until they change. */
|
|
46
|
+
private refused;
|
|
47
|
+
/** The key whose start was refused: not started again until `enabled` turns off or the key changes. */
|
|
48
|
+
private failedKey;
|
|
49
|
+
private busy;
|
|
50
|
+
private mounts;
|
|
51
|
+
constructor(commands: SessionCommands, changed: (state: LiveActivityState) => void);
|
|
52
|
+
/** The id of the running activity. */
|
|
53
|
+
get activityId(): string | undefined;
|
|
54
|
+
/** The options of a render. */
|
|
55
|
+
sync(options: SessionOptions): void;
|
|
56
|
+
/** The component is gone: the activity ends, now or once the request in flight has answered. */
|
|
57
|
+
dispose(): void;
|
|
58
|
+
/** The component mounted, also again in Strict Mode. */
|
|
59
|
+
keep(): void;
|
|
60
|
+
/** The component unmounted: it is disposed after a microtask, unless it mounts again before (Strict Mode). */
|
|
61
|
+
release(): void;
|
|
62
|
+
private pump;
|
|
63
|
+
private start;
|
|
64
|
+
private update;
|
|
65
|
+
private end;
|
|
66
|
+
private send;
|
|
67
|
+
private set;
|
|
68
|
+
}
|
|
@@ -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
|
+
}
|