@ng-native/device 0.22.1 → 0.23.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.
- package/dist/accessibility.d.ts +7 -0
- package/dist/accessibility.js +14 -2
- package/dist/accessibility.js.map +1 -1
- package/dist/app-lifecycle.d.ts +49 -0
- package/dist/app-lifecycle.js +67 -0
- package/dist/app-lifecycle.js.map +1 -0
- package/dist/app-state.d.ts +2 -0
- package/dist/app-state.js +2 -0
- package/dist/app-state.js.map +1 -1
- package/dist/app-windows.d.ts +321 -0
- package/dist/app-windows.js +526 -0
- package/dist/app-windows.js.map +1 -0
- package/dist/color-scheme.d.ts +2 -0
- package/dist/color-scheme.js +2 -0
- package/dist/color-scheme.js.map +1 -1
- package/dist/conditions.d.ts +11 -3
- package/dist/conditions.js +42 -9
- package/dist/conditions.js.map +1 -1
- package/dist/control-size.d.ts +6 -0
- package/dist/control-size.js +2 -0
- package/dist/control-size.js.map +1 -0
- package/dist/deep-links.d.ts +3 -1
- package/dist/deep-links.js +3 -1
- package/dist/deep-links.js.map +1 -1
- package/dist/desktop-options.d.ts +73 -0
- package/dist/desktop-options.js +182 -0
- package/dist/desktop-options.js.map +1 -0
- package/dist/desktop-permissions.d.ts +30 -0
- package/dist/desktop-permissions.js +169 -0
- package/dist/desktop-permissions.js.map +1 -0
- package/dist/dev-menu.d.ts +2 -2
- package/dist/dev-menu.js +3 -5
- package/dist/dev-menu.js.map +1 -1
- package/dist/dialogs.d.ts +9 -7
- package/dist/dialogs.js +15 -10
- package/dist/dialogs.js.map +1 -1
- package/dist/direction.d.ts +2 -0
- package/dist/direction.js +2 -0
- package/dist/direction.js.map +1 -1
- package/dist/dock.d.ts +48 -0
- package/dist/dock.js +70 -0
- package/dist/dock.js.map +1 -0
- package/dist/drag-and-drop.d.ts +67 -0
- package/dist/drag-and-drop.js +74 -0
- package/dist/drag-and-drop.js.map +1 -0
- package/dist/expo-view-name.d.ts +16 -0
- package/dist/expo-view-name.js +22 -0
- package/dist/expo-view-name.js.map +1 -0
- package/dist/hardware-back.d.ts +3 -10
- package/dist/hardware-back.js +4 -1
- package/dist/hardware-back.js.map +1 -1
- package/dist/index.d.ts +26 -2
- package/dist/index.js +26 -2
- package/dist/index.js.map +1 -1
- package/dist/interaction-observer.d.ts +117 -0
- package/dist/interaction-observer.js +241 -0
- package/dist/interaction-observer.js.map +1 -0
- package/dist/keyboard.d.ts +2 -8
- package/dist/keyboard.js +3 -0
- package/dist/keyboard.js.map +1 -1
- package/dist/layout-animation.d.ts +3 -1
- package/dist/layout-animation.js +3 -1
- package/dist/layout-animation.js.map +1 -1
- package/dist/menu-model.d.ts +108 -0
- package/dist/menu-model.js +259 -0
- package/dist/menu-model.js.map +1 -0
- package/dist/menu-template.d.ts +19 -0
- package/dist/menu-template.js +31 -0
- package/dist/menu-template.js.map +1 -0
- package/dist/menus.d.ts +225 -0
- package/dist/menus.js +341 -0
- package/dist/menus.js.map +1 -0
- package/dist/navigation-bar.d.ts +2 -12
- package/dist/navigation-bar.js +4 -1
- package/dist/navigation-bar.js.map +1 -1
- package/dist/os-support.d.ts +42 -0
- package/dist/os-support.js +207 -0
- package/dist/os-support.js.map +1 -0
- package/dist/os.d.ts +26 -5
- package/dist/os.js +35 -7
- package/dist/os.js.map +1 -1
- package/dist/own-host.d.ts +27 -0
- package/dist/own-host.js +79 -0
- package/dist/own-host.js.map +1 -0
- package/dist/processes.d.ts +116 -0
- package/dist/processes.js +177 -0
- package/dist/processes.js.map +1 -0
- package/dist/react-native.d.ts +19 -0
- package/dist/react-native.js.map +1 -1
- package/dist/safe-area.d.ts +12 -1
- package/dist/safe-area.js +16 -2
- package/dist/safe-area.js.map +1 -1
- package/dist/screen.d.ts +2 -0
- package/dist/screen.js +2 -0
- package/dist/screen.js.map +1 -1
- package/dist/sharing.d.ts +3 -1
- package/dist/sharing.js +8 -1
- package/dist/sharing.js.map +1 -1
- package/dist/shell.d.ts +33 -0
- package/dist/shell.js +73 -0
- package/dist/shell.js.map +1 -0
- package/dist/shortcut.d.ts +49 -0
- package/dist/shortcut.js +92 -0
- package/dist/shortcut.js.map +1 -0
- package/dist/sidebar-selection.d.ts +8 -0
- package/dist/sidebar-selection.js +9 -0
- package/dist/sidebar-selection.js.map +1 -0
- package/dist/spawn-rules.d.ts +108 -0
- package/dist/spawn-rules.js +313 -0
- package/dist/spawn-rules.js.map +1 -0
- package/dist/split-views.d.ts +7 -0
- package/dist/split-views.js +21 -0
- package/dist/split-views.js.map +1 -0
- package/dist/status-bar.d.ts +2 -12
- package/dist/status-bar.js +3 -0
- package/dist/status-bar.js.map +1 -1
- package/dist/tray.d.ts +63 -0
- package/dist/tray.js +76 -0
- package/dist/tray.js.map +1 -0
- package/dist/undo-stack.d.ts +64 -0
- package/dist/undo-stack.js +110 -0
- package/dist/undo-stack.js.map +1 -0
- package/dist/vibration.d.ts +3 -14
- package/dist/vibration.js +4 -1
- package/dist/vibration.js.map +1 -1
- package/dist/window-navigator.d.ts +25 -0
- package/dist/window-navigator.js +7 -0
- package/dist/window-navigator.js.map +1 -0
- package/dist/window-restore.d.ts +45 -0
- package/dist/window-restore.js +66 -0
- package/dist/window-restore.js.map +1 -0
- package/package.json +10 -2
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interactions as plain events, for an analytics or monitoring tool to record.
|
|
3
|
+
*
|
|
4
|
+
* Such a tool usually learns about taps by wrapping React Native's own `Pressable` and
|
|
5
|
+
* `ScrollView`, which never render here: every control is a host view on the press machine in
|
|
6
|
+
* `@ng-native/components`, or a view the platform draws, such as an `@expo/ui` control or a native
|
|
7
|
+
* tab. So the control says what happened, and an app maps it to whatever its tool takes. Nothing is
|
|
8
|
+
* built while no observer is registered.
|
|
9
|
+
*
|
|
10
|
+
* Here rather than in `@ng-native/components` because `@ng-native/expo` and `@ng-native/router`
|
|
11
|
+
* report too, and the Angular-facing packages cannot import each other.
|
|
12
|
+
*/
|
|
13
|
+
import { InjectionToken } from '@angular/core';
|
|
14
|
+
import { INTERACTION_NAME, registeredViewName, } from '@ng-native/fabric';
|
|
15
|
+
/** Every observer the app registered. See `provideInteractionObserver`. */
|
|
16
|
+
export const INTERACTION_OBSERVERS = new InjectionToken('INTERACTION_OBSERVERS');
|
|
17
|
+
/**
|
|
18
|
+
* Hear every tap, long press, scroll and swipe in the app. Can be given more than once, and every
|
|
19
|
+
* observer hears every interaction, after the control's own `(press)` has run.
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* providers: [
|
|
23
|
+
* provideInteractionObserver((event) => {
|
|
24
|
+
* if (event.type === 'click') analytics.tap(event.label ?? event.element);
|
|
25
|
+
* }),
|
|
26
|
+
* ]
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function provideInteractionObserver(observer) {
|
|
30
|
+
return { provide: INTERACTION_OBSERVERS, useValue: observer, multi: true };
|
|
31
|
+
}
|
|
32
|
+
/** How much of a control's text an event carries. */
|
|
33
|
+
export const MAX_TEXT_LENGTH = 100;
|
|
34
|
+
/** The observers an injector can see, or null. Read at the moment of an interaction, not before. */
|
|
35
|
+
export function interactionObservers(injector) {
|
|
36
|
+
return injector.get(INTERACTION_OBSERVERS, null, { optional: true });
|
|
37
|
+
}
|
|
38
|
+
/** Observers that have thrown once already, so a broken one warns once rather than on every tap. */
|
|
39
|
+
const warned = new WeakSet();
|
|
40
|
+
/**
|
|
41
|
+
* Hand the event to each observer. One that throws is reported, and stops neither the others nor
|
|
42
|
+
* the app.
|
|
43
|
+
*/
|
|
44
|
+
export function notifyInteraction(observers, event) {
|
|
45
|
+
for (const observer of observers) {
|
|
46
|
+
try {
|
|
47
|
+
observer(event);
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
if (warned.has(observer))
|
|
51
|
+
continue;
|
|
52
|
+
warned.add(observer);
|
|
53
|
+
console.warn('[ng-native] An interaction observer threw:', error);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The parts of an event that come from the element: what it committed, so a label or role a
|
|
59
|
+
* component composed onto it, or implied, is the one reported, as a screen reader hears it.
|
|
60
|
+
*/
|
|
61
|
+
export function describeElement(engine, node) {
|
|
62
|
+
const props = node.props;
|
|
63
|
+
const component = componentAround(node);
|
|
64
|
+
const label = text(props['accessibilityLabel']);
|
|
65
|
+
const testID = text(props['testID']);
|
|
66
|
+
const nativeID = text(props['nativeID']);
|
|
67
|
+
const role = text(props['role']) ?? text(props['accessibilityRole']);
|
|
68
|
+
return {
|
|
69
|
+
element: elementName(node),
|
|
70
|
+
...(component !== undefined && { component }),
|
|
71
|
+
...(label !== undefined && { label }),
|
|
72
|
+
...(testID !== undefined && { testID }),
|
|
73
|
+
...(nativeID !== undefined && { nativeID }),
|
|
74
|
+
...(role !== undefined && { role }),
|
|
75
|
+
nativeTag: engine.tagOf?.(node) ?? null,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
const text = (value) => typeof value === 'string' && value !== '' ? value : undefined;
|
|
79
|
+
/** `tag` on the native host, `tagName` on the web host, lowercased as templates write it. */
|
|
80
|
+
function elementName(node) {
|
|
81
|
+
const named = node;
|
|
82
|
+
return (named.tag ?? named.tagName ?? '').toLowerCase();
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* The name of the nearest component around `node`, read at the moment of an interaction from the
|
|
86
|
+
* definition the platform adapter keeps on each component's host, so nothing is kept per element.
|
|
87
|
+
* A primitive, whose name is a native view's, is passed over, and so is the root component, the
|
|
88
|
+
* outermost one: it is the app rather than a part of it. Told by what is marked rather than by
|
|
89
|
+
* depth, since the root's host is under a surface on a device and is the page's own mount point on
|
|
90
|
+
* the web. An element a package marks with no definition, to keep it a plain view, is no component.
|
|
91
|
+
*/
|
|
92
|
+
function componentAround(node) {
|
|
93
|
+
let nearest;
|
|
94
|
+
for (let current = node.parent; current; current = current.parent) {
|
|
95
|
+
const definition = current.componentHost;
|
|
96
|
+
if (typeof definition !== 'object')
|
|
97
|
+
continue;
|
|
98
|
+
if (nearest !== undefined)
|
|
99
|
+
return nearest;
|
|
100
|
+
// The name its host gives it, and an empty one to be passed over for the one around it.
|
|
101
|
+
const given = current.props[INTERACTION_NAME];
|
|
102
|
+
if (typeof given === 'string') {
|
|
103
|
+
if (given)
|
|
104
|
+
nearest = given;
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
const name = componentName(definition, current);
|
|
108
|
+
// A component with no selector of its own, such as a routed page, names nothing around it.
|
|
109
|
+
if (name === null)
|
|
110
|
+
return undefined;
|
|
111
|
+
if (name !== undefined)
|
|
112
|
+
nearest = name;
|
|
113
|
+
}
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
/** The selector Angular gives a component that has none. */
|
|
117
|
+
const NO_SELECTOR = 'ng-component';
|
|
118
|
+
/**
|
|
119
|
+
* A component's name by its selector: the element name a template writes, or for a selector of
|
|
120
|
+
* attributes the selector rebuilt, such as `section[x-card]`, since its element is a plain one.
|
|
121
|
+
* Of several selectors, the one its host matches, by element name, attributes and classes, or the
|
|
122
|
+
* first where none does. Undefined for a primitive, whose element is a native view's, and null for
|
|
123
|
+
* a component with no selector.
|
|
124
|
+
*/
|
|
125
|
+
function componentName(definition, host) {
|
|
126
|
+
const selectors = definition.selectors ?? [];
|
|
127
|
+
const element = elementName(host);
|
|
128
|
+
const selector = selectors.find((each) => hostMatches(each, host, element)) ?? selectors[0];
|
|
129
|
+
const tag = typeof selector?.[0] === 'string' ? selector[0] : '';
|
|
130
|
+
if (!selector || tag === NO_SELECTOR)
|
|
131
|
+
return null;
|
|
132
|
+
if (selector.length > 1)
|
|
133
|
+
return selectorText(selector, tag);
|
|
134
|
+
return registeredViewName(tag) ? undefined : tag;
|
|
135
|
+
}
|
|
136
|
+
/** Angular's selector flags: what the parts after one are. */
|
|
137
|
+
const NOT = 1;
|
|
138
|
+
const ELEMENT = 4;
|
|
139
|
+
const CLASS = 8;
|
|
140
|
+
/**
|
|
141
|
+
* Whether a host matches a compiled selector: its element name, attributes and classes, and none
|
|
142
|
+
* of its `:not()` fragments, each of which starts at a flag with `NOT` set and rules the host out
|
|
143
|
+
* when the host has every part of it, as Angular reads them.
|
|
144
|
+
*/
|
|
145
|
+
function hostMatches(selector, host, element) {
|
|
146
|
+
let from = 0;
|
|
147
|
+
let negative = false;
|
|
148
|
+
for (let index = 1; index <= selector.length; index++) {
|
|
149
|
+
const flag = selector[index];
|
|
150
|
+
if (index < selector.length && !(typeof flag === 'number' && flag & NOT))
|
|
151
|
+
continue;
|
|
152
|
+
if (fragmentMatches(selector.slice(from, index), host, element) === negative)
|
|
153
|
+
return false;
|
|
154
|
+
from = index;
|
|
155
|
+
negative = true;
|
|
156
|
+
}
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Whether a host has every part of one fragment of a compiled selector: an element name first,
|
|
161
|
+
* then attributes as name and value pairs, and classes after a `CLASS` flag.
|
|
162
|
+
*/
|
|
163
|
+
function fragmentMatches(parts, host, element) {
|
|
164
|
+
let mode = ELEMENT;
|
|
165
|
+
for (let index = 0; index < parts.length; index++) {
|
|
166
|
+
const part = parts[index];
|
|
167
|
+
if (typeof part === 'number')
|
|
168
|
+
mode = part;
|
|
169
|
+
else if (mode & ELEMENT) {
|
|
170
|
+
if (part && part !== element)
|
|
171
|
+
return false;
|
|
172
|
+
mode = 0;
|
|
173
|
+
}
|
|
174
|
+
else if (mode & CLASS) {
|
|
175
|
+
if (!hasClass(host, String(part)))
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
else if (!hasAttribute(host, String(part), parts[++index]))
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
/** Whether a host has an attribute, and with `value` where it is not empty. */
|
|
184
|
+
function hasAttribute(host, name, value) {
|
|
185
|
+
const actual = host.props[name];
|
|
186
|
+
if (actual === undefined || actual === null || actual === false)
|
|
187
|
+
return false;
|
|
188
|
+
return !value || String(actual).toLowerCase() === String(value).toLowerCase();
|
|
189
|
+
}
|
|
190
|
+
/** Whether a host has a class: in the engine's own set, or on the web host's real element. */
|
|
191
|
+
function hasClass(host, name) {
|
|
192
|
+
const classes = host.classes;
|
|
193
|
+
if (classes)
|
|
194
|
+
return classes.has(name);
|
|
195
|
+
const element = host.el;
|
|
196
|
+
return element?.classList?.contains(name) ?? false;
|
|
197
|
+
}
|
|
198
|
+
/** A compiled selector written out again, up to any `:not()`. */
|
|
199
|
+
function selectorText(selector, tag) {
|
|
200
|
+
let text = tag;
|
|
201
|
+
let mode = 0;
|
|
202
|
+
for (let index = 1; index < selector.length; index++) {
|
|
203
|
+
const part = selector[index];
|
|
204
|
+
if (typeof part === 'number') {
|
|
205
|
+
if (part & NOT)
|
|
206
|
+
break;
|
|
207
|
+
mode = part;
|
|
208
|
+
}
|
|
209
|
+
else if (mode & CLASS)
|
|
210
|
+
text += `.${String(part)}`;
|
|
211
|
+
else {
|
|
212
|
+
const value = selector[++index];
|
|
213
|
+
text += value ? `[${String(part)}=${String(value)}]` : `[${String(part)}]`;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return text;
|
|
217
|
+
}
|
|
218
|
+
/** The text under a node, in order, with runs of whitespace collapsed, or undefined if none. */
|
|
219
|
+
export function textWithin(node) {
|
|
220
|
+
const parts = [];
|
|
221
|
+
let length = 0;
|
|
222
|
+
const visit = (current) => {
|
|
223
|
+
for (const child of current.childList) {
|
|
224
|
+
if (length > MAX_TEXT_LENGTH)
|
|
225
|
+
return;
|
|
226
|
+
if (child.nodeKind === 'text') {
|
|
227
|
+
const words = child.nodeText.trim();
|
|
228
|
+
if (words) {
|
|
229
|
+
parts.push(words);
|
|
230
|
+
length += words.length + 1;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
else
|
|
234
|
+
visit(child);
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
visit(node);
|
|
238
|
+
const text = parts.join(' ').replace(/\s+/g, ' ');
|
|
239
|
+
return text ? text.slice(0, MAX_TEXT_LENGTH) : undefined;
|
|
240
|
+
}
|
|
241
|
+
//# sourceMappingURL=interaction-observer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interaction-observer.js","sourceRoot":"","sources":["../src/interaction-observer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,cAAc,EAAgC,MAAM,eAAe,CAAC;AAC7E,OAAO,EACL,gBAAgB,EAChB,kBAAkB,GAInB,MAAM,mBAAmB,CAAC;AAgF3B,2EAA2E;AAC3E,MAAM,CAAC,MAAM,qBAAqB,GAAG,IAAI,cAAc,CACrD,uBAAuB,CACxB,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,0BAA0B,CAAC,QAA6B;IACtE,OAAO,EAAE,OAAO,EAAE,qBAAqB,EAAE,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AAC7E,CAAC;AAED,qDAAqD;AACrD,MAAM,CAAC,MAAM,eAAe,GAAG,GAAG,CAAC;AAEnC,oGAAoG;AACpG,MAAM,UAAU,oBAAoB,CAAC,QAAkB;IACrD,OAAO,QAAQ,CAAC,GAAG,CAAC,qBAAqB,EAAE,IAAI,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;AACvE,CAAC;AAED,oGAAoG;AACpG,MAAM,MAAM,GAAG,IAAI,OAAO,EAAuB,CAAC;AAElD;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAC/B,SAAyC,EACzC,KAAuB;IAEvB,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;QACjC,IAAI,CAAC;YACH,QAAQ,CAAC,KAAK,CAAC,CAAC;QAClB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;gBAAE,SAAS;YACnC,MAAM,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CAAC;QAC7E,CAAC;IACH,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,gGAAgG;AAChG,MAAM,UAAU,UAAU,CAAC,IAAc;IACvC,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,MAAM,KAAK,GAAG,CAAC,OAAiB,EAAQ,EAAE;QACxC,KAAK,MAAM,KAAK,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC;YACtC,IAAI,MAAM,GAAG,eAAe;gBAAE,OAAO;YACrC,IAAI,KAAK,CAAC,QAAQ,KAAK,MAAM,EAAE,CAAC;gBAC9B,MAAM,KAAK,GAAG,KAAK,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;gBACpC,IAAI,KAAK,EAAE,CAAC;oBACV,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;oBAClB,MAAM,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;gBAC7B,CAAC;YACH,CAAC;;gBAAM,KAAK,CAAC,KAAK,CAAC,CAAC;QACtB,CAAC;IACH,CAAC,CAAC;IACF,KAAK,CAAC,IAAI,CAAC,CAAC;IACZ,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAClD,OAAO,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,eAAe,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;AAC3D,CAAC","sourcesContent":["/**\n * Interactions as plain events, for an analytics or monitoring tool to record.\n *\n * Such a tool usually learns about taps by wrapping React Native's own `Pressable` and\n * `ScrollView`, which never render here: every control is a host view on the press machine in\n * `@ng-native/components`, or a view the platform draws, such as an `@expo/ui` control or a native\n * tab. So the control says what happened, and an app maps it to whatever its tool takes. Nothing is\n * built while no observer is registered.\n *\n * Here rather than in `@ng-native/components` because `@ng-native/expo` and `@ng-native/router`\n * report too, and the Angular-facing packages cannot import each other.\n */\nimport { InjectionToken, type Injector, type Provider } from '@angular/core';\nimport {\n INTERACTION_NAME,\n registeredViewName,\n type EngineNode,\n type HostEngine,\n type HostNode,\n} from '@ng-native/fabric';\n\n/**\n * What the user did. `gesture` is a native gesture that is none of the others: a slow pan, a\n * pinch, a rotation.\n */\nexport type InteractionType = 'click' | 'long-press' | 'scroll' | 'swipe' | 'gesture';\n\n/**\n * Which way a scroll view's drag moved the content into view (`down` shows what is below), or\n * which way a native gesture's finger moved (`down` is towards the bottom of the screen).\n */\nexport type InteractionDirection = 'up' | 'down' | 'left' | 'right';\n\n/**\n * The native gesture an event came from: the builder's name, `Gesture.LongPress()` as\n * `long-press`. A string, so a gesture added to the library later still has a name.\n */\nexport type InteractionGesture =\n 'tap' | 'long-press' | 'pan' | 'fling' | 'pinch' | 'rotation' | 'manual' | (string & {});\n\n/** One interaction, as an observer receives it. */\nexport interface InteractionEvent {\n readonly type: InteractionType;\n /** The element's name as written in the template: `pressable`, `text`, `scroll-view`, ... */\n readonly element: string;\n /**\n * The selector of the nearest component the control is rendered inside, such as `app-button`\n * for the `<pressable>` in its template, or for one projected into its `<ng-content>`. A\n * component that is the control itself is named by `element` instead, and this is the component\n * around it. A routed page is named the same way. A component matched by an attribute is named\n * by its selector written out, such as `section[x-card]`. Absent for a control in the root\n * component's own template, and in a component with no selector, such as a page that has none.\n * A component's host can give another name with an `interactionName` attribute, or `''` to be\n * passed over for the component around it.\n */\n readonly component?: string;\n /** `accessibilityLabel`, or `aria-label`. */\n readonly label?: string;\n readonly testID?: string;\n /** `nativeID`, or `id`. */\n readonly nativeID?: string;\n /** `role`, or `accessibilityRole`. */\n readonly role?: string;\n /**\n * The text inside the control, joined with spaces and cut to `MAX_TEXT_LENGTH` characters. On a\n * `click` and a `long-press`, never a `scroll` or `swipe`. It can hold what the user typed or was\n * shown, so redact it before it leaves the device.\n */\n readonly text?: string;\n /** The tag the control's own view was committed with, or null where the host has none. */\n readonly nativeTag: number | null;\n /** Where the touch landed in the window. Absent for a press that had no touch. */\n readonly pageX?: number;\n readonly pageY?: number;\n /** Where the touch landed in the control. */\n readonly locationX?: number;\n readonly locationY?: number;\n /** The control's size from its last layout, once it has had one. */\n readonly width?: number;\n readonly height?: number;\n /** Scrolls, swipes and pans only. */\n readonly direction?: InteractionDirection;\n /** The native gesture this came from, if it came from one. */\n readonly gesture?: InteractionGesture;\n /** A pan's or fling's straight-line distance from where it started, in points. */\n readonly distance?: number;\n /** A pinch's scale as it ended: above 1 spread apart, below 1 pinched together. */\n readonly scale?: number;\n /**\n * A touch, or a press with none: Android's click from a keyboard or TalkBack, or a control the\n * platform draws, such as a `<switch>`, which reports no touch point.\n */\n readonly source: 'touch' | 'click';\n /** Milliseconds since the epoch. */\n readonly timestamp: number;\n}\n\nexport type InteractionObserver = (event: InteractionEvent) => void;\n\n/** Every observer the app registered. See `provideInteractionObserver`. */\nexport const INTERACTION_OBSERVERS = new InjectionToken<readonly InteractionObserver[]>(\n 'INTERACTION_OBSERVERS',\n);\n\n/**\n * Hear every tap, long press, scroll and swipe in the app. Can be given more than once, and every\n * observer hears every interaction, after the control's own `(press)` has run.\n *\n * ```ts\n * providers: [\n * provideInteractionObserver((event) => {\n * if (event.type === 'click') analytics.tap(event.label ?? event.element);\n * }),\n * ]\n * ```\n */\nexport function provideInteractionObserver(observer: InteractionObserver): Provider {\n return { provide: INTERACTION_OBSERVERS, useValue: observer, multi: true };\n}\n\n/** How much of a control's text an event carries. */\nexport const MAX_TEXT_LENGTH = 100;\n\n/** The observers an injector can see, or null. Read at the moment of an interaction, not before. */\nexport function interactionObservers(injector: Injector): readonly InteractionObserver[] | null {\n return injector.get(INTERACTION_OBSERVERS, null, { optional: true });\n}\n\n/** Observers that have thrown once already, so a broken one warns once rather than on every tap. */\nconst warned = new WeakSet<InteractionObserver>();\n\n/**\n * Hand the event to each observer. One that throws is reported, and stops neither the others nor\n * the app.\n */\nexport function notifyInteraction(\n observers: readonly InteractionObserver[],\n event: InteractionEvent,\n): void {\n for (const observer of observers) {\n try {\n observer(event);\n } catch (error) {\n if (warned.has(observer)) continue;\n warned.add(observer);\n console.warn('[ng-native] An interaction observer threw:', error);\n }\n }\n}\n\n/**\n * The parts of an event that come from the element: what it committed, so a label or role a\n * component composed onto it, or implied, is the one reported, as a screen reader hears it.\n */\nexport function describeElement(\n engine: HostEngine,\n node: HostNode,\n): Pick<\n InteractionEvent,\n 'element' | 'component' | 'label' | 'testID' | 'nativeID' | 'role' | 'nativeTag'\n> {\n const props = node.props;\n const component = componentAround(node);\n const label = text(props['accessibilityLabel']);\n const testID = text(props['testID']);\n const nativeID = text(props['nativeID']);\n const role = text(props['role']) ?? text(props['accessibilityRole']);\n return {\n element: elementName(node),\n ...(component !== undefined && { component }),\n ...(label !== undefined && { label }),\n ...(testID !== undefined && { testID }),\n ...(nativeID !== undefined && { nativeID }),\n ...(role !== undefined && { role }),\n nativeTag: engine.tagOf?.(node) ?? null,\n };\n}\n\nconst text = (value: unknown): string | undefined =>\n typeof value === 'string' && value !== '' ? value : undefined;\n\n/** `tag` on the native host, `tagName` on the web host, lowercased as templates write it. */\nfunction elementName(node: HostNode): string {\n const named = node as HostNode & { tag?: string; tagName?: string };\n return (named.tag ?? named.tagName ?? '').toLowerCase();\n}\n\n/**\n * The name of the nearest component around `node`, read at the moment of an interaction from the\n * definition the platform adapter keeps on each component's host, so nothing is kept per element.\n * A primitive, whose name is a native view's, is passed over, and so is the root component, the\n * outermost one: it is the app rather than a part of it. Told by what is marked rather than by\n * depth, since the root's host is under a surface on a device and is the page's own mount point on\n * the web. An element a package marks with no definition, to keep it a plain view, is no component.\n */\nfunction componentAround(node: HostNode): string | undefined {\n let nearest: string | undefined;\n for (let current = node.parent; current; current = current.parent) {\n const definition = (current as EngineNode).componentHost;\n if (typeof definition !== 'object') continue;\n if (nearest !== undefined) return nearest;\n // The name its host gives it, and an empty one to be passed over for the one around it.\n const given = current.props[INTERACTION_NAME];\n if (typeof given === 'string') {\n if (given) nearest = given;\n continue;\n }\n const name = componentName(definition, current);\n // A component with no selector of its own, such as a routed page, names nothing around it.\n if (name === null) return undefined;\n if (name !== undefined) nearest = name;\n }\n return undefined;\n}\n\n/** The slice of Angular's component definition a name is read from. */\ninterface NamedDefinition {\n /** Each selector, as Angular compiles it: the element name, then attributes and flags. */\n readonly selectors?: readonly (readonly unknown[])[];\n}\n\n/** The selector Angular gives a component that has none. */\nconst NO_SELECTOR = 'ng-component';\n\n/**\n * A component's name by its selector: the element name a template writes, or for a selector of\n * attributes the selector rebuilt, such as `section[x-card]`, since its element is a plain one.\n * Of several selectors, the one its host matches, by element name, attributes and classes, or the\n * first where none does. Undefined for a primitive, whose element is a native view's, and null for\n * a component with no selector.\n */\nfunction componentName(definition: NamedDefinition, host: HostNode): string | null | undefined {\n const selectors = definition.selectors ?? [];\n const element = elementName(host);\n const selector = selectors.find((each) => hostMatches(each, host, element)) ?? selectors[0];\n const tag = typeof selector?.[0] === 'string' ? selector[0] : '';\n if (!selector || tag === NO_SELECTOR) return null;\n if (selector.length > 1) return selectorText(selector, tag);\n return registeredViewName(tag) ? undefined : tag;\n}\n\n/** Angular's selector flags: what the parts after one are. */\nconst NOT = 1;\nconst ELEMENT = 4;\nconst CLASS = 8;\n\n/**\n * Whether a host matches a compiled selector: its element name, attributes and classes, and none\n * of its `:not()` fragments, each of which starts at a flag with `NOT` set and rules the host out\n * when the host has every part of it, as Angular reads them.\n */\nfunction hostMatches(selector: readonly unknown[], host: HostNode, element: string): boolean {\n let from = 0;\n let negative = false;\n for (let index = 1; index <= selector.length; index++) {\n const flag = selector[index];\n if (index < selector.length && !(typeof flag === 'number' && flag & NOT)) continue;\n if (fragmentMatches(selector.slice(from, index), host, element) === negative) return false;\n from = index;\n negative = true;\n }\n return true;\n}\n\n/**\n * Whether a host has every part of one fragment of a compiled selector: an element name first,\n * then attributes as name and value pairs, and classes after a `CLASS` flag.\n */\nfunction fragmentMatches(parts: readonly unknown[], host: HostNode, element: string): boolean {\n let mode = ELEMENT;\n for (let index = 0; index < parts.length; index++) {\n const part = parts[index];\n if (typeof part === 'number') mode = part;\n else if (mode & ELEMENT) {\n if (part && part !== element) return false;\n mode = 0;\n } else if (mode & CLASS) {\n if (!hasClass(host, String(part))) return false;\n } else if (!hasAttribute(host, String(part), parts[++index])) return false;\n }\n return true;\n}\n\n/** Whether a host has an attribute, and with `value` where it is not empty. */\nfunction hasAttribute(host: HostNode, name: string, value: unknown): boolean {\n const actual = host.props[name];\n if (actual === undefined || actual === null || actual === false) return false;\n return !value || String(actual).toLowerCase() === String(value).toLowerCase();\n}\n\n/** Whether a host has a class: in the engine's own set, or on the web host's real element. */\nfunction hasClass(host: HostNode, name: string): boolean {\n const classes = (host as { classes?: ReadonlySet<string> | null }).classes;\n if (classes) return classes.has(name);\n const element = (host as { el?: { classList?: { contains(name: string): boolean } } }).el;\n return element?.classList?.contains(name) ?? false;\n}\n\n/** A compiled selector written out again, up to any `:not()`. */\nfunction selectorText(selector: readonly unknown[], tag: string): string {\n let text = tag;\n let mode = 0;\n for (let index = 1; index < selector.length; index++) {\n const part = selector[index];\n if (typeof part === 'number') {\n if (part & NOT) break;\n mode = part;\n } else if (mode & CLASS) text += `.${String(part)}`;\n else {\n const value = selector[++index];\n text += value ? `[${String(part)}=${String(value)}]` : `[${String(part)}]`;\n }\n }\n return text;\n}\n\n/** The text under a node, in order, with runs of whitespace collapsed, or undefined if none. */\nexport function textWithin(node: HostNode): string | undefined {\n const parts: string[] = [];\n let length = 0;\n const visit = (current: HostNode): void => {\n for (const child of current.childList) {\n if (length > MAX_TEXT_LENGTH) return;\n if (child.nodeKind === 'text') {\n const words = child.nodeText.trim();\n if (words) {\n parts.push(words);\n length += words.length + 1;\n }\n } else visit(child);\n }\n };\n visit(node);\n const text = parts.join(' ').replace(/\\s+/g, ' ');\n return text ? text.slice(0, MAX_TEXT_LENGTH) : undefined;\n}\n"]}
|
package/dist/keyboard.d.ts
CHANGED
|
@@ -1,11 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The software keyboard: where it is, and how long it takes to get there.
|
|
3
|
-
*
|
|
4
|
-
* On iOS the metrics change as the keyboard starts to move, with the duration and curve it is
|
|
5
|
-
* moving on, so a layout can move with it. On Android React Native says only where the keyboard is
|
|
6
|
-
* going, as it starts to move, with no duration, so a layout jumps there while the keyboard slides;
|
|
7
|
-
* `@ng-native/expo`'s `provideNativeKeyboard()` adds the duration and curve. See `keyboardSource`.
|
|
8
|
-
*/
|
|
9
1
|
import { InjectionToken, type Signal } from '@angular/core';
|
|
10
2
|
import * as i0 from "@angular/core";
|
|
11
3
|
/** What the platform reports when the keyboard moves. */
|
|
@@ -106,6 +98,8 @@ export declare class Keyboard {
|
|
|
106
98
|
*/
|
|
107
99
|
static readonly SOURCE: InjectionToken<KeyboardSource>;
|
|
108
100
|
private readonly source;
|
|
101
|
+
/** Whether there is a software keyboard: on iOS and Android, and not on a Mac or the web. */
|
|
102
|
+
readonly available: Signal<boolean>;
|
|
109
103
|
private readonly engine;
|
|
110
104
|
private readonly app;
|
|
111
105
|
private readonly current;
|
package/dist/keyboard.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* going, as it starts to move, with no duration, so a layout jumps there while the keyboard slides;
|
|
7
7
|
* `@ng-native/expo`'s `provideNativeKeyboard()` adds the duration and curve. See `keyboardSource`.
|
|
8
8
|
*/
|
|
9
|
+
import { injectOs, isPhoneOs } from "./os.js";
|
|
9
10
|
import { ApplicationRef, DestroyRef, InjectionToken, Service, computed, inject, signal, } from '@angular/core';
|
|
10
11
|
import { HostEngine, nativePlatform } from '@ng-native/fabric';
|
|
11
12
|
import { edgeToEdgeFlag, reactNative, } from "./react-native.js";
|
|
@@ -144,6 +145,8 @@ export class Keyboard {
|
|
|
144
145
|
factory: keyboardSource,
|
|
145
146
|
});
|
|
146
147
|
source = inject(Keyboard.SOURCE);
|
|
148
|
+
/** Whether there is a software keyboard: on iOS and Android, and not on a Mac or the web. */
|
|
149
|
+
available = signal(isPhoneOs(injectOs())).asReadonly();
|
|
147
150
|
engine = inject(HostEngine, { optional: true });
|
|
148
151
|
app = inject(ApplicationRef, { optional: true });
|
|
149
152
|
current = signal({ height: 0 }, /* @ts-ignore */
|
package/dist/keyboard.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keyboard.js","sourceRoot":"","sources":["../src/keyboard.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EACL,cAAc,EACd,UAAU,EACV,cAAc,EACd,OAAO,EACP,QAAQ,EACR,MAAM,EACN,MAAM,GAEP,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,UAAU,EAAE,cAAc,EAAiB,MAAM,mBAAmB,CAAC;AAC9E,OAAO,EACL,cAAc,EACd,WAAW,GAGZ,MAAM,mBAAmB,CAAC;;AA8C3B,gEAAgE;AAChE,SAAS,QAAQ,CAAC,KAA0B;IAC1C,IAAI,KAAK,CAAC,QAAQ,KAAK,SAAS;QAAE,OAAO,EAAE,CAAC;IAC5C,OAAO,EAAE,QAAQ,EAAE,KAAK,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE,CAAC;AAC5D,CAAC;AAED,MAAM,SAAS,GAAG,CAAC,KAA0B,EAAmB,EAAE,CAAC,CAAC;IAClE,MAAM,EAAE,KAAK,CAAC,cAAc,CAAC,MAAM;IACnC,OAAO,EAAE,KAAK,CAAC,cAAc,CAAC,OAAO;IACrC,GAAG,QAAQ,CAAC,KAAK,CAAC;CACnB,CAAC,CAAC;AAEH;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,cAAc;IAC5B,MAAM,MAAM,GAAG,WAAW,EAAE,CAAC;IAC7B,IAAI,CAAC,MAAM;QAAE,OAAO,EAAE,SAAS,EAAE,GAAG,EAAE,CAAC,GAAG,EAAE,GAAE,CAAC,EAAE,OAAO,EAAE,GAAG,EAAE,GAAE,CAAC,EAAE,CAAC;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C,CAAC;QACtC,UAAU,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;QACpF,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,eAAe,EAAE,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC;QAC7E,IAAI,IAAI;YAAE,UAAU,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;IACvC,CAAC;IAED;;;;;;;OAOG;IACH,OAAO;QACL,IAAI,CAAC,MAAM,EAAE,WAAW,EAAE,CAAC;QAC3B,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;IACxB,CAAC;uGAjFU,QAAQ;wGAAR,QAAQ;;2FAAR,QAAQ;kBADpB,OAAO","sourcesContent":["/**\n * The software keyboard: where it is, and how long it takes to get there.\n *\n * On iOS the metrics change as the keyboard starts to move, with the duration and curve it is\n * moving on, so a layout can move with it. On Android React Native says only where the keyboard is\n * going, as it starts to move, with no duration, so a layout jumps there while the keyboard slides;\n * `@ng-native/expo`'s `provideNativeKeyboard()` adds the duration and curve. See `keyboardSource`.\n */\nimport {\n ApplicationRef,\n DestroyRef,\n InjectionToken,\n Service,\n computed,\n inject,\n signal,\n type Signal,\n} from '@angular/core';\nimport { HostEngine, nativePlatform, type HostNode } from '@ng-native/fabric';\nimport {\n edgeToEdgeFlag,\n reactNative,\n type NativeKeyboardEvent,\n type ReactNative,\n} from './react-native.ts';\n\n/** What the platform reports when the keyboard moves. */\nexport interface KeyboardMetrics {\n /** Height in dp; zero when the keyboard is hidden. */\n readonly height: number;\n /**\n * Where its top edge is, from the top of the screen. Absent when hidden. A view measured in the\n * window is compared with it less `Keyboard.windowTop`.\n */\n readonly screenY?: number;\n /** How long its own animation takes, in ms, for anything moving with it. */\n readonly duration?: number;\n /**\n * The curve its own animation runs on, for anything moving with it to match: one of\n * `LayoutEasing`'s names. On iOS that is usually `'keyboard'`, the keyboard's own curve. React\n * Native's own events on Android carry `'keyboard'` with a duration of zero: they say where the\n * keyboard is going and nothing of how it gets there, so nothing should try to animate alongside\n * them. `provideNativeKeyboard()` reports Android's duration, with `'easeOut'`.\n */\n readonly easing?: string;\n}\n\n/** Where the metrics come from. A fake stands in for the platform in tests. */\nexport interface KeyboardSource {\n subscribe(listener: (metrics: KeyboardMetrics) => void): () => void;\n /**\n * Where the keyboard is at each frame of its motion, for a source that knows: before the motion\n * starts, at every frame of it, and null once it is over. One that does not has no such method.\n */\n subscribeMotion?(listener: (frame: KeyboardMetrics | null) => void): () => void;\n dismiss(): void;\n /**\n * How far below the top of the screen the window a view is measured in starts, in points, for\n * a source that knows it is not zero. Read when it is compared, as it changes with the status\n * bar. See `keyboardSource`.\n */\n readonly windowTop?: number;\n /**\n * How far below the top of the screen the app's window itself starts, in points, for a source\n * that can read it: the lower half of a split screen, or a freeform window, on Android. Read when\n * it is compared, as the window can move.\n */\n readonly screenTop?: number;\n}\n\n/** The timing a keyboard event carries, when it carries any. */\nfunction timingOf(event: NativeKeyboardEvent): Pick<KeyboardMetrics, 'duration' | 'easing'> {\n if (event.duration === undefined) return {};\n return { duration: event.duration, easing: event.easing };\n}\n\nconst metricsOf = (event: NativeKeyboardEvent): KeyboardMetrics => ({\n height: event.endCoordinates.height,\n screenY: event.endCoordinates.screenY,\n ...timingOf(event),\n});\n\n/**\n * The events RN's own KeyboardAvoidingView listens for, which differ by platform.\n *\n * On iOS the Will events: they arrive as the keyboard starts to move, carrying its duration and\n * curve, so a layout configured from them moves with the keyboard. The Did events arrive once it\n * has landed, and a layout that waits for them moves after it, so they are heard only until the\nfirst Will event, for a keyboard already moving when listening starts. `keyboardWillChangeFrame` covers a\n * keyboard that changes size while it is up - the predictive bar, another language's layout - and\n * is ignored while it is down: UIKit posts one beside every show and hide as well, and the one\n * beside a hide is the frame the keyboard leaves by, off the bottom of the screen.\n *\n * Android sends no Will events at all, so there it is the Did events. They arrive once the\n * window's insets have changed, which is as the keyboard starts to move rather than once it has\n * stopped, with a duration of zero: they say nothing of the motion.\n *\n * Both platforms report the keyboard's `screenY` from the top of the screen. iOS measures a view\n * from there too. Android measures it from below the status bar when React Native's edge-to-edge\n * flag is off, even in an app drawn under the bar, such as Expo Go, which leaves the flag off: its\n * `measureInWindow` leaves out the bar's inset then. `windowTop` is that inset, zero when the flag\n * is on, which a layout takes off `screenY` to compare the keyboard with a view it measured.\n */\nexport function keyboardSource(): KeyboardSource {\n const native = reactNative();\n if (!native) return { subscribe: () => () => {}, dismiss: () => {} };\n const { Keyboard: keyboard, Platform } = native;\n\n return {\n get windowTop() {\n return windowTopOf(native);\n },\n subscribe: (listener) => {\n // A keyboard already up says nothing until it next moves. `Keyboard.js` keeps the last show\n // it heard, from whenever React Native first loaded it.\n const shown = keyboard.metrics?.();\n const already = shown ? { height: shown.height, screenY: shown.screenY } : null;\n if (Platform?.OS !== 'ios') {\n const subscriptions = [\n keyboard.addListener('keyboardDidShow', (event) => listener(metricsOf(event))),\n keyboard.addListener('keyboardDidHide', () => listener({ height: 0 })),\n ];\n if (already) listener(already);\n return () => subscriptions.forEach((subscription) => subscription.remove());\n }\n let up = already !== null;\n // Started while the keyboard moves, the Will event is missed and `Keyboard.js` still holds\n // where it started from, so the Did event says where it landed. After a Will event, the\n // Will events say it all: a show a hide interrupts can land after the hide begins.\n let heard = false;\n const subscriptions = [\n keyboard.addListener('keyboardWillShow', (event) => {\n up = heard = true;\n listener(metricsOf(event));\n }),\n keyboard.addListener('keyboardWillChangeFrame', (event) => {\n if (up) listener(metricsOf(event));\n }),\n keyboard.addListener('keyboardWillHide', (event) => {\n up = false;\n heard = true;\n listener({ height: 0, ...timingOf(event) });\n }),\n keyboard.addListener('keyboardDidShow', (event) => {\n if (heard) return;\n up = true;\n listener({ height: event.endCoordinates.height, screenY: event.endCoordinates.screenY });\n }),\n keyboard.addListener('keyboardDidHide', () => {\n if (heard) return;\n up = false;\n listener({ height: 0 });\n }),\n ];\n if (already) listener(already);\n return () => subscriptions.forEach((subscription) => subscription.remove());\n },\n dismiss: () => keyboard.dismiss(),\n };\n}\n\n/**\n * Require React Native's `Keyboard.js` now, so it records each show and hide from here on.\n *\n * It keeps the last one it heard, which `keyboardSource` starts from, but only from when it is\n * first required. Left to the `Keyboard` service, that is when the first `text-input` appears, and\n * a keyboard the native header's search bar opened before then reads as down until it next moves.\n */\nexport function recordKeyboard(): void {\n const native = reactNative();\n if (native) void native.Keyboard;\n}\n\n/**\n * What React Native's Android `measureInWindow` leaves out above a view: the status bar's height,\n * unless its edge-to-edge flag is on. Zero on iOS.\n */\nfunction windowTopOf(native: ReactNative): number {\n if (native.Platform?.OS !== 'android' || edgeToEdgeFlag(native)) return 0;\n // `StatusBar.currentHeight` is the bar's height when the app started. The module's own constant\n // is read from the window's current insets on each call, as the inset `measureInWindow` leaves\n // out is: after a rotation the two still agree.\n const manager = native.TurboModuleRegistry?.get('StatusBarManager') as\n { getConstants?(): { HEIGHT?: number } } | null | undefined;\n return manager?.getConstants?.().HEIGHT ?? native.StatusBar?.currentHeight ?? 0;\n}\n\n/**\n * A native view that reports the keyboard's motion frame by frame, for a view to follow it on the\n * native side: a `<keyboard-dock>` on Android, where React Native says only where the keyboard\n * is going. `@ng-native/expo`'s `provideNativeKeyboard()` provides one; without it this is null.\n *\n * The view sends `onKeyboardMoveStart`, `onKeyboardMove` and `onKeyboardMoveEnd`, each with the\n * `height` of the keyboard over the view's own bottom edge, in points, and is laid out over the\n * element it reports for.\n */\nexport interface KeyboardMotion {\n /** The Fabric name of the view. */\n readonly viewName: string;\n}\n\nexport const KEYBOARD_MOTION = new InjectionToken<KeyboardMotion | null>(\n 'angular-native.keyboardMotion',\n { providedIn: 'root', factory: () => null },\n);\n\n/**\n * Subscribed for the life of the app, which is the life of the service, and removed when the app\n * is destroyed. A component that mounts mid-gesture wants the height that is already true rather\n * than the next change, which is why the subscription is the service's and not the component's.\n */\n@Service()\nexport class Keyboard {\n /**\n * Where the metrics actually come from. Overriding this token is how a test drives the keyboard,\n * and it is finer than replacing the service: the code under test stays the real thing.\n */\n static readonly SOURCE = new InjectionToken<KeyboardSource>('angular-native.keyboardSource', {\n factory: keyboardSource,\n });\n\n private readonly source = inject(Keyboard.SOURCE);\n private readonly engine = inject(HostEngine, { optional: true });\n private readonly app = inject(ApplicationRef, { optional: true });\n private readonly current = signal<KeyboardMetrics>({ height: 0 });\n private readonly frame = signal<KeyboardMetrics | null>(null);\n\n /**\n * Everything the platform said, for a layout that needs the animation duration too. It changes\n * as the keyboard starts to move, to where the keyboard is going.\n */\n readonly metrics: Signal<KeyboardMetrics> = this.current.asReadonly();\n readonly height = computed(() => this.current().height);\n readonly visible = computed(() => this.current().height > 0);\n\n /**\n * Whether `position` follows the keyboard frame by frame: on Android with `@ng-native/expo`'s\n * `provideNativeKeyboard()`, where a layout cannot animate alongside the keyboard and has to\n * move with it instead. Elsewhere it is `metrics`, and a layout animates to where that says.\n */\n readonly followsMotion = this.source.subscribeMotion !== undefined;\n\n /**\n * How far below the top of the screen the window `measureInWindow` reports in starts: the status\n * bar's height on Android unless React Native's edge-to-edge flag is on, and zero otherwise. The\n * keyboard's top edge in that window is `screenY` less this. It changes with a rotation, so read\n * it when comparing rather than keeping it.\n *\n * On Android, read from where React Native measures the app's root, which is drawn from the top\n * of the screen: React Native takes the bar off its measurements as it lays the root out, and keeps\n * doing so when the bar is hidden or shown later, until the root is laid out again. The bar's\n * own height stands in where the root has not been measured.\n *\n * A window that itself starts below the top of the screen, in a split screen or freeform on\n * Android, adds its own top, where the source can read it: `provideNativeKeyboard()` from\n * `@ng-native/expo` can, and React Native alone cannot.\n */\n get windowTop(): number {\n return this.belowBar() + (this.source.screenTop ?? 0);\n }\n\n /** How far the window a view is measured in starts below the top of the app's own window. */\n private belowBar(): number {\n let top = this.source.windowTop ?? 0;\n // ponytail: the first root component is taken to start at the top of the app's window, as an\n // app's root does; a root placed lower would read as a window starting higher.\n const root = this.app?.components[0]?.location.nativeElement as HostNode | undefined;\n if (root && nativePlatform() === 'android')\n this.engine?.measure(root, (frame) => (top = -frame.y));\n return top;\n }\n\n /** Where the keyboard is now: at every frame of its motion where `followsMotion`. */\n readonly position: Signal<KeyboardMetrics> = computed(() => this.frame() ?? this.current());\n\n constructor() {\n const destroyRef = inject(DestroyRef);\n destroyRef.onDestroy(this.source.subscribe((metrics) => this.current.set(metrics)));\n const stop = this.source.subscribeMotion?.((frame) => this.frame.set(frame));\n if (stop) destroyRef.onDestroy(stop);\n }\n\n /**\n * Put the keyboard away, as tapping outside a field would: the focused text field is sent\n * `blur`, as a scroll view does at such a tap. A focused view or button is left as it is.\n *\n * React Native's own `Keyboard.dismiss()`, which the source calls too, blurs only a field its\n * `TextInput` component recorded in `TextInputState`. A `text-input` here is the native component\n * with no `TextInput.js` around it, so nothing is recorded and that call alone blurs nothing.\n */\n dismiss(): void {\n this.engine?.blurFocused();\n this.source.dismiss();\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"keyboard.js","sourceRoot":"","sources":["../src/keyboard.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAC9C,OAAO,EACL,cAAc,EACd,UAAU,EACV,cAAc,EACd,OAAO,EACP,QAAQ,EACR,MAAM,EACN,MAAM,GAEP,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,UAAU,EAAE,cAAc,EAAiB,MAAM,mBAAmB,CAAC;AAC9E,OAAO,EACL,cAAc,EACd,WAAW,GAGZ,MAAM,mBAAmB,CAAC;;AA8C3B,gEAAgE;AAChE,SAAS,QAAQ,CAAC,KAA0B;IAC1C,IAAI,KAAK,CAAC,QAAQ,KAAK,SAAS;QAAE,OAAO,EAAE,CAAC;IAC5C,OAAO,EAAE,QAAQ,EAAE,KAAK,CAAC,QAAQ,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AAO","sourcesContent":["/**\n * The software keyboard: where it is, and how long it takes to get there.\n *\n * On iOS the metrics change as the keyboard starts to move, with the duration and curve it is\n * moving on, so a layout can move with it. On Android React Native says only where the keyboard is\n * going, as it starts to move, with no duration, so a layout jumps there while the keyboard slides;\n * `@ng-native/expo`'s `provideNativeKeyboard()` adds the duration and curve. See `keyboardSource`.\n */\nimport { injectOs, isPhoneOs } from './os.ts';\nimport {\n ApplicationRef,\n DestroyRef,\n InjectionToken,\n Service,\n computed,\n inject,\n signal,\n type Signal,\n} from '@angular/core';\nimport { HostEngine, nativePlatform, type HostNode } from '@ng-native/fabric';\nimport {\n edgeToEdgeFlag,\n reactNative,\n type NativeKeyboardEvent,\n type ReactNative,\n} from './react-native.ts';\n\n/** What the platform reports when the keyboard moves. */\nexport interface KeyboardMetrics {\n /** Height in dp; zero when the keyboard is hidden. */\n readonly height: number;\n /**\n * Where its top edge is, from the top of the screen. Absent when hidden. A view measured in the\n * window is compared with it less `Keyboard.windowTop`.\n */\n readonly screenY?: number;\n /** How long its own animation takes, in ms, for anything moving with it. */\n readonly duration?: number;\n /**\n * The curve its own animation runs on, for anything moving with it to match: one of\n * `LayoutEasing`'s names. On iOS that is usually `'keyboard'`, the keyboard's own curve. React\n * Native's own events on Android carry `'keyboard'` with a duration of zero: they say where the\n * keyboard is going and nothing of how it gets there, so nothing should try to animate alongside\n * them. `provideNativeKeyboard()` reports Android's duration, with `'easeOut'`.\n */\n readonly easing?: string;\n}\n\n/** Where the metrics come from. A fake stands in for the platform in tests. */\nexport interface KeyboardSource {\n subscribe(listener: (metrics: KeyboardMetrics) => void): () => void;\n /**\n * Where the keyboard is at each frame of its motion, for a source that knows: before the motion\n * starts, at every frame of it, and null once it is over. One that does not has no such method.\n */\n subscribeMotion?(listener: (frame: KeyboardMetrics | null) => void): () => void;\n dismiss(): void;\n /**\n * How far below the top of the screen the window a view is measured in starts, in points, for\n * a source that knows it is not zero. Read when it is compared, as it changes with the status\n * bar. See `keyboardSource`.\n */\n readonly windowTop?: number;\n /**\n * How far below the top of the screen the app's window itself starts, in points, for a source\n * that can read it: the lower half of a split screen, or a freeform window, on Android. Read when\n * it is compared, as the window can move.\n */\n readonly screenTop?: number;\n}\n\n/** The timing a keyboard event carries, when it carries any. */\nfunction timingOf(event: NativeKeyboardEvent): Pick<KeyboardMetrics, 'duration' | 'easing'> {\n if (event.duration === undefined) return {};\n return { duration: event.duration, easing: event.easing };\n}\n\nconst metricsOf = (event: NativeKeyboardEvent): KeyboardMetrics => ({\n height: event.endCoordinates.height,\n screenY: event.endCoordinates.screenY,\n ...timingOf(event),\n});\n\n/**\n * The events RN's own KeyboardAvoidingView listens for, which differ by platform.\n *\n * On iOS the Will events: they arrive as the keyboard starts to move, carrying its duration and\n * curve, so a layout configured from them moves with the keyboard. The Did events arrive once it\n * has landed, and a layout that waits for them moves after it, so they are heard only until the\nfirst Will event, for a keyboard already moving when listening starts. `keyboardWillChangeFrame` covers a\n * keyboard that changes size while it is up - the predictive bar, another language's layout - and\n * is ignored while it is down: UIKit posts one beside every show and hide as well, and the one\n * beside a hide is the frame the keyboard leaves by, off the bottom of the screen.\n *\n * Android sends no Will events at all, so there it is the Did events. They arrive once the\n * window's insets have changed, which is as the keyboard starts to move rather than once it has\n * stopped, with a duration of zero: they say nothing of the motion.\n *\n * Both platforms report the keyboard's `screenY` from the top of the screen. iOS measures a view\n * from there too. Android measures it from below the status bar when React Native's edge-to-edge\n * flag is off, even in an app drawn under the bar, such as Expo Go, which leaves the flag off: its\n * `measureInWindow` leaves out the bar's inset then. `windowTop` is that inset, zero when the flag\n * is on, which a layout takes off `screenY` to compare the keyboard with a view it measured.\n */\nexport function keyboardSource(): KeyboardSource {\n const native = reactNative();\n if (!native) return { subscribe: () => () => {}, dismiss: () => {} };\n const { Keyboard: keyboard, Platform } = native;\n\n return {\n get windowTop() {\n return windowTopOf(native);\n },\n subscribe: (listener) => {\n // A keyboard already up says nothing until it next moves. `Keyboard.js` keeps the last show\n // it heard, from whenever React Native first loaded it.\n const shown = keyboard.metrics?.();\n const already = shown ? { height: shown.height, screenY: shown.screenY } : null;\n if (Platform?.OS !== 'ios') {\n const subscriptions = [\n keyboard.addListener('keyboardDidShow', (event) => listener(metricsOf(event))),\n keyboard.addListener('keyboardDidHide', () => listener({ height: 0 })),\n ];\n if (already) listener(already);\n return () => subscriptions.forEach((subscription) => subscription.remove());\n }\n let up = already !== null;\n // Started while the keyboard moves, the Will event is missed and `Keyboard.js` still holds\n // where it started from, so the Did event says where it landed. After a Will event, the\n // Will events say it all: a show a hide interrupts can land after the hide begins.\n let heard = false;\n const subscriptions = [\n keyboard.addListener('keyboardWillShow', (event) => {\n up = heard = true;\n listener(metricsOf(event));\n }),\n keyboard.addListener('keyboardWillChangeFrame', (event) => {\n if (up) listener(metricsOf(event));\n }),\n keyboard.addListener('keyboardWillHide', (event) => {\n up = false;\n heard = true;\n listener({ height: 0, ...timingOf(event) });\n }),\n keyboard.addListener('keyboardDidShow', (event) => {\n if (heard) return;\n up = true;\n listener({ height: event.endCoordinates.height, screenY: event.endCoordinates.screenY });\n }),\n keyboard.addListener('keyboardDidHide', () => {\n if (heard) return;\n up = false;\n listener({ height: 0 });\n }),\n ];\n if (already) listener(already);\n return () => subscriptions.forEach((subscription) => subscription.remove());\n },\n dismiss: () => keyboard.dismiss(),\n };\n}\n\n/**\n * Require React Native's `Keyboard.js` now, so it records each show and hide from here on.\n *\n * It keeps the last one it heard, which `keyboardSource` starts from, but only from when it is\n * first required. Left to the `Keyboard` service, that is when the first `text-input` appears, and\n * a keyboard the native header's search bar opened before then reads as down until it next moves.\n */\nexport function recordKeyboard(): void {\n const native = reactNative();\n if (native) void native.Keyboard;\n}\n\n/**\n * What React Native's Android `measureInWindow` leaves out above a view: the status bar's height,\n * unless its edge-to-edge flag is on. Zero on iOS.\n */\nfunction windowTopOf(native: ReactNative): number {\n if (native.Platform?.OS !== 'android' || edgeToEdgeFlag(native)) return 0;\n // `StatusBar.currentHeight` is the bar's height when the app started. The module's own constant\n // is read from the window's current insets on each call, as the inset `measureInWindow` leaves\n // out is: after a rotation the two still agree.\n const manager = native.TurboModuleRegistry?.get('StatusBarManager') as\n { getConstants?(): { HEIGHT?: number } } | null | undefined;\n return manager?.getConstants?.().HEIGHT ?? native.StatusBar?.currentHeight ?? 0;\n}\n\n/**\n * A native view that reports the keyboard's motion frame by frame, for a view to follow it on the\n * native side: a `<keyboard-dock>` on Android, where React Native says only where the keyboard\n * is going. `@ng-native/expo`'s `provideNativeKeyboard()` provides one; without it this is null.\n *\n * The view sends `onKeyboardMoveStart`, `onKeyboardMove` and `onKeyboardMoveEnd`, each with the\n * `height` of the keyboard over the view's own bottom edge, in points, and is laid out over the\n * element it reports for.\n */\nexport interface KeyboardMotion {\n /** The Fabric name of the view. */\n readonly viewName: string;\n}\n\nexport const KEYBOARD_MOTION = new InjectionToken<KeyboardMotion | null>(\n 'angular-native.keyboardMotion',\n { providedIn: 'root', factory: () => null },\n);\n\n/**\n * Subscribed for the life of the app, which is the life of the service, and removed when the app\n * is destroyed. A component that mounts mid-gesture wants the height that is already true rather\n * than the next change, which is why the subscription is the service's and not the component's.\n */\n@Service()\nexport class Keyboard {\n /**\n * Where the metrics actually come from. Overriding this token is how a test drives the keyboard,\n * and it is finer than replacing the service: the code under test stays the real thing.\n */\n static readonly SOURCE = new InjectionToken<KeyboardSource>('angular-native.keyboardSource', {\n factory: keyboardSource,\n });\n\n private readonly source = inject(Keyboard.SOURCE);\n\n /** Whether there is a software keyboard: on iOS and Android, and not on a Mac or the web. */\n readonly available: Signal<boolean> = signal(isPhoneOs(injectOs())).asReadonly();\n private readonly engine = inject(HostEngine, { optional: true });\n private readonly app = inject(ApplicationRef, { optional: true });\n private readonly current = signal<KeyboardMetrics>({ height: 0 });\n private readonly frame = signal<KeyboardMetrics | null>(null);\n\n /**\n * Everything the platform said, for a layout that needs the animation duration too. It changes\n * as the keyboard starts to move, to where the keyboard is going.\n */\n readonly metrics: Signal<KeyboardMetrics> = this.current.asReadonly();\n readonly height = computed(() => this.current().height);\n readonly visible = computed(() => this.current().height > 0);\n\n /**\n * Whether `position` follows the keyboard frame by frame: on Android with `@ng-native/expo`'s\n * `provideNativeKeyboard()`, where a layout cannot animate alongside the keyboard and has to\n * move with it instead. Elsewhere it is `metrics`, and a layout animates to where that says.\n */\n readonly followsMotion = this.source.subscribeMotion !== undefined;\n\n /**\n * How far below the top of the screen the window `measureInWindow` reports in starts: the status\n * bar's height on Android unless React Native's edge-to-edge flag is on, and zero otherwise. The\n * keyboard's top edge in that window is `screenY` less this. It changes with a rotation, so read\n * it when comparing rather than keeping it.\n *\n * On Android, read from where React Native measures the app's root, which is drawn from the top\n * of the screen: React Native takes the bar off its measurements as it lays the root out, and keeps\n * doing so when the bar is hidden or shown later, until the root is laid out again. The bar's\n * own height stands in where the root has not been measured.\n *\n * A window that itself starts below the top of the screen, in a split screen or freeform on\n * Android, adds its own top, where the source can read it: `provideNativeKeyboard()` from\n * `@ng-native/expo` can, and React Native alone cannot.\n */\n get windowTop(): number {\n return this.belowBar() + (this.source.screenTop ?? 0);\n }\n\n /** How far the window a view is measured in starts below the top of the app's own window. */\n private belowBar(): number {\n let top = this.source.windowTop ?? 0;\n // ponytail: the first root component is taken to start at the top of the app's window, as an\n // app's root does; a root placed lower would read as a window starting higher.\n const root = this.app?.components[0]?.location.nativeElement as HostNode | undefined;\n if (root && nativePlatform() === 'android')\n this.engine?.measure(root, (frame) => (top = -frame.y));\n return top;\n }\n\n /** Where the keyboard is now: at every frame of its motion where `followsMotion`. */\n readonly position: Signal<KeyboardMetrics> = computed(() => this.frame() ?? this.current());\n\n constructor() {\n const destroyRef = inject(DestroyRef);\n destroyRef.onDestroy(this.source.subscribe((metrics) => this.current.set(metrics)));\n const stop = this.source.subscribeMotion?.((frame) => this.frame.set(frame));\n if (stop) destroyRef.onDestroy(stop);\n }\n\n /**\n * Put the keyboard away, as tapping outside a field would: the focused text field is sent\n * `blur`, as a scroll view does at such a tap. A focused view or button is left as it is.\n *\n * React Native's own `Keyboard.dismiss()`, which the source calls too, blurs only a field its\n * `TextInput` component recorded in `TextInputState`. A `text-input` here is the native component\n * with no `TextInput.js` around it, so nothing is recorded and that call alone blurs nothing.\n */\n dismiss(): void {\n this.engine?.blurFocused();\n this.source.dismiss();\n }\n}\n"]}
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* much as the change meant. Here the animation is configured as the change's own commit is handed
|
|
14
14
|
* over, and not at all when the change commits nothing.
|
|
15
15
|
*/
|
|
16
|
-
import { InjectionToken } from '@angular/core';
|
|
16
|
+
import { InjectionToken, type Signal } from '@angular/core';
|
|
17
17
|
import * as i0 from "@angular/core";
|
|
18
18
|
/**
|
|
19
19
|
* How a change is eased. `spring` is the platform's own, and is what a native list uses.
|
|
@@ -41,6 +41,8 @@ export declare class LayoutAnimation {
|
|
|
41
41
|
/** Overridden in a test to watch a configuration without one being applied. */
|
|
42
42
|
static readonly SOURCE: InjectionToken<NativeLayoutAnimation | null>;
|
|
43
43
|
private readonly native;
|
|
44
|
+
/** Whether React Native's module is here. Where it is not, every call is inert. */
|
|
45
|
+
readonly available: Signal<boolean>;
|
|
44
46
|
private readonly engine;
|
|
45
47
|
private readonly application;
|
|
46
48
|
/**
|
package/dist/layout-animation.js
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* much as the change meant. Here the animation is configured as the change's own commit is handed
|
|
14
14
|
* over, and not at all when the change commits nothing.
|
|
15
15
|
*/
|
|
16
|
-
import { ApplicationRef, InjectionToken, Service, inject } from '@angular/core';
|
|
16
|
+
import { ApplicationRef, InjectionToken, Service, inject, signal, } from '@angular/core';
|
|
17
17
|
import { Engine } from '@ng-native/fabric';
|
|
18
18
|
import { reactNative } from "./react-native.js";
|
|
19
19
|
import * as i0 from "@angular/core";
|
|
@@ -38,6 +38,8 @@ export class LayoutAnimation {
|
|
|
38
38
|
/** Overridden in a test to watch a configuration without one being applied. */
|
|
39
39
|
static SOURCE = new InjectionToken('angular-native.layoutAnimationSource', { factory: () => reactNative()?.LayoutAnimation ?? null });
|
|
40
40
|
native = inject(LayoutAnimation.SOURCE);
|
|
41
|
+
/** Whether React Native's module is here. Where it is not, every call is inert. */
|
|
42
|
+
available = signal(this.native !== null).asReadonly();
|
|
41
43
|
engine = inject(Engine, { optional: true });
|
|
42
44
|
application = inject(ApplicationRef, { optional: true });
|
|
43
45
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"layout-animation.js","sourceRoot":"","sources":["../src/layout-animation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAChF,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;;AAsBhD,MAAM,QAAQ,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,EAAE,eAA+B,EAAE,CAAC;AAE5E;;;;;;GAMG;AACH,MAAM,mBAAmB,GAAG,EAAE,CAAC;AAE/B,4EAA4E;AAC5E,MAAM,cAAc,GAAG,GAAG,CAAC;AAE3B;;;;;GAKG;AAEH,MAAM,OAAO,eAAe;IAC1B,+EAA+E;IAC/E,MAAM,CAAU,MAAM,GAAG,IAAI,cAAc,CACzC,sCAAsC,EACtC,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,WAAW,EAAE,EAAE,eAAe,IAAI,IAAI,EAAE,CAC1D,CAAC;IAEe,MAAM,GAAG,MAAM,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC;IACxC,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;IAC5C,WAAW,GAAG,MAAM,CAAC,cAAc,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;IAE1E;;;;;;;;;;OAUG;IACH,KA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IACjD,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,SAAS,CAAC;IAC3C,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,SAAS,CAAC;IAEzC,OAAO;QACL,QAAQ;QACR,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,CAAC,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE;QAChF,GAAG,CAAC,MAAM,KAAK,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,CAAC;QAC5E,GAAG,CAAC,KAAK,KAAK,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE,CAAC;KAC3E,CAAC;AACJ,CAAC","sourcesContent":["/**\n * Animating a layout *change*, which CSS cannot express.\n *\n * A transition animates a property from one value to another, and the engine already does that. It\n * cannot animate a layout: when a row is removed and the rows below move up, nothing about those\n * rows changed - their `top` was never set, Yoga computed it, and there is no old value to\n * transition from. The web solves this with FLIP, measuring before and after in JavaScript; native\n * solves it in the shadow tree, which is where the two layouts both exist.\n *\n * So this is not a nicer `LayoutAnimation` - it is the only way to say the thing at all. What it\n * adds is the promise, and the scope: `configureNext` applies to the next commit *whenever that\n * happens* and whatever makes it, a frame of a CSS animation or an unrelated screen appearing as\n * much as the change meant. Here the animation is configured as the change's own commit is handed\n * over, and not at all when the change commits nothing.\n */\n\nimport { ApplicationRef, InjectionToken, Service, inject } from '@angular/core';\nimport { Engine } from '@ng-native/fabric';\nimport { reactNative } from './react-native.ts';\n\n/**\n * How a change is eased. `spring` is the platform's own, and is what a native list uses.\n * `keyboard` is the curve iOS moves its keyboard on, which a keyboard event reports by that name.\n */\nexport type LayoutEasing =\n 'spring' | 'linear' | 'easeInEaseOut' | 'easeIn' | 'easeOut' | 'keyboard';\n\nexport interface LayoutChange {\n readonly duration?: number;\n readonly easing?: LayoutEasing;\n /** What a view appearing does. `opacity` fades it in; the default is to scale it up. */\n readonly appear?: 'opacity' | 'scaleXY' | 'none';\n /** What a view leaving does. */\n readonly leave?: 'opacity' | 'scaleXY' | 'none';\n}\n\nexport interface NativeLayoutAnimation {\n configureNext(config: object, onDone?: () => void): void;\n}\n\nconst DEFAULTS = { duration: 300, easing: 'easeInEaseOut' as LayoutEasing };\n\n/**\n * How long past its duration an animation is given before `animate` resolves without its\n * completion: a few frames of slack, so that the completion is the one that resolves it wherever\n * one comes. React Native calls it at the end of the animation, or from a timer of its own a frame\n * after the duration where native reports nothing, as on Android with layout animations off. It\n * calls nothing at all where animations are disabled.\n */\nconst COMPLETION_GRACE_MS = 50;\n\n/** Angular's NG0101, `ApplicationRef.tick()` called while it is running. */\nconst RECURSIVE_TICK = 101;\n\n/**\n * `inject(LayoutAnimation).animate(() => this.rows.update(...))`.\n *\n * The only way to say \"animate the layout this change produces\": a transition needs a value to\n * animate from, and a row that moves up because the one above it left never had one.\n */\n@Service()\nexport class LayoutAnimation {\n /** Overridden in a test to watch a configuration without one being applied. */\n static readonly SOURCE = new InjectionToken<NativeLayoutAnimation | null>(\n 'angular-native.layoutAnimationSource',\n { factory: () => reactNative()?.LayoutAnimation ?? null },\n );\n\n private readonly native = inject(LayoutAnimation.SOURCE);\n private readonly engine = inject(Engine, { optional: true });\n private readonly application = inject(ApplicationRef, { optional: true });\n\n /**\n * Run `change` and animate the layout its commit produces.\n *\n * The change is taken rather than left to the caller because the platform animates whatever\n * commit comes next, and only this can make that the change's own: change detection runs and\n * the change commits before `animate` returns, with the animation configured as that commit is\n * handed over. A frame of another animation cannot land in between and take it, and a change\n * that commits nothing configures nothing, so no later commit is animated in its place. Resolves\n * when the animation ends, once its duration has passed where the platform does not report\n * that, or at once when there was nothing to animate.\n */\n async animate(change: () => void, options: LayoutChange = {}): Promise<void> {\n const native = this.native;\n if (!native) {\n change();\n return;\n }\n\n const own: Waiting = { configured: false, finish: () => {} };\n const done = new Promise<void>((resolve) => (own.finish = resolve));\n const configure = (): void => {\n // Every change still waiting is in this commit: one a pass makes together with this one.\n const together = new Set([...this.waiting, own]);\n const finish = (): void => together.forEach((change) => change.finish());\n for (const change of together) change.configured = true;\n native.configureNext(config(options), finish);\n // React Native calls no completion where animations are disabled, so nothing should wait\n // on it forever.\n setTimeout(finish, (options.duration ?? DEFAULTS.duration) + COMPLETION_GRACE_MS);\n };\n\n // With no engine there is no commit to wait for: a service built on its own, as in a test.\n if (!this.engine) {\n configure();\n change();\n return done;\n }\n\n const withdraw = this.engine.beforeNextCommit(configure);\n const settle = (): void => {\n withdraw();\n this.waiting.delete(own);\n if (!own.configured) own.finish();\n };\n this.waiting.add(own);\n try {\n change();\n // Called from inside a pass, the commit is that pass's own, which ends before any microtask.\n if (this.detectChanges()) settle();\n else queueMicrotask(settle);\n } catch (error) {\n // Nothing was changed, so nothing is to be animated, not even a commit made in this turn.\n withdraw();\n this.waiting.delete(own);\n throw error;\n }\n return done;\n }\n\n /** The changes whose commit has not come yet: more than one only inside a pass. */\n private readonly waiting = new Set<Waiting>();\n\n /**\n * Run change detection now and say so, or say that a pass is already running and will reach\n * the change.\n */\n private detectChanges(): boolean {\n try {\n this.application?.tick();\n return true;\n } catch (error) {\n if ((error as { code?: number }).code !== RECURSIVE_TICK) throw error;\n return false;\n }\n }\n}\n\n/** One `animate()` call: whether its commit was configured, and what resolves it. */\ninterface Waiting {\n configured: boolean;\n finish: () => void;\n}\n\n/**\n * The config React Native takes, from the three things worth naming.\n *\n * `create` and `delete` need a `property` as well as a type, because a view appearing has no\n * previous size to animate from - it has to be told what to animate *of*. Leaving that out is the\n * most common way one of these does nothing.\n */\nfunction config(options: LayoutChange): object {\n const duration = options.duration ?? DEFAULTS.duration;\n const easing = options.easing ?? DEFAULTS.easing;\n const appear = options.appear ?? 'opacity';\n const leave = options.leave ?? 'opacity';\n\n return {\n duration,\n update: { type: easing, ...(easing === 'spring' ? { springDamping: 0.7 } : {}) },\n ...(appear === 'none' ? {} : { create: { type: easing, property: appear } }),\n ...(leave === 'none' ? {} : { delete: { type: easing, property: leave } }),\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"layout-animation.js","sourceRoot":"","sources":["../src/layout-animation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EACL,cAAc,EACd,cAAc,EACd,OAAO,EACP,MAAM,EACN,MAAM,GAEP,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;;AAsBhD,MAAM,QAAQ,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,EAAE,eAA+B,EAAE,CAAC;AAE5E;;;;;;GAMG;AACH,MAAM,mBAAmB,GAAG,EAAE,CAAC;AAE/B,4EAA4E;AAC5E,MAAM,cAAc,GAAG,GAAG,CAAC;AAE3B;;;;;GAKG;AAEH,MAAM,OAAO,eAAe;IAC1B,+EAA+E;IAC/E,MAAM,CAAU,MAAM,GAAG,IAAI,cAAc,CACzC,sCAAsC,EACtC,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,WAAW,EAAE,EAAE,eAAe,IAAI,IAAI,EAAE,CAC1D,CAAC;IAEe,MAAM,GAAG,MAAM,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC;IAEzD,mFAAmF;IAC1E,SAAS,GAAoB,MAAM,CAAC,IAAI,CAAC,MAAM,KAAK,IAAI,CAAC,CAAC,UAAU,EAAE,CAAC;IAC/D,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;IAC5C,WAAW,GAAG,MAAM,CAAC,cAAc,EAAE,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C;IACvD,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,QAAQ,CAAC,MAAM,CAAC;IACjD,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,SAAS,CAAC;IAC3C,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,SAAS,CAAC;IAEzC,OAAO;QACL,QAAQ;QACR,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,CAAC,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE;QAChF,GAAG,CAAC,MAAM,KAAK,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,CAAC;QAC5E,GAAG,CAAC,KAAK,KAAK,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE,CAAC;KAC3E,CAAC;AACJ,CAAC","sourcesContent":["/**\n * Animating a layout *change*, which CSS cannot express.\n *\n * A transition animates a property from one value to another, and the engine already does that. It\n * cannot animate a layout: when a row is removed and the rows below move up, nothing about those\n * rows changed - their `top` was never set, Yoga computed it, and there is no old value to\n * transition from. The web solves this with FLIP, measuring before and after in JavaScript; native\n * solves it in the shadow tree, which is where the two layouts both exist.\n *\n * So this is not a nicer `LayoutAnimation` - it is the only way to say the thing at all. What it\n * adds is the promise, and the scope: `configureNext` applies to the next commit *whenever that\n * happens* and whatever makes it, a frame of a CSS animation or an unrelated screen appearing as\n * much as the change meant. Here the animation is configured as the change's own commit is handed\n * over, and not at all when the change commits nothing.\n */\n\nimport {\n ApplicationRef,\n InjectionToken,\n Service,\n inject,\n signal,\n type Signal,\n} from '@angular/core';\nimport { Engine } from '@ng-native/fabric';\nimport { reactNative } from './react-native.ts';\n\n/**\n * How a change is eased. `spring` is the platform's own, and is what a native list uses.\n * `keyboard` is the curve iOS moves its keyboard on, which a keyboard event reports by that name.\n */\nexport type LayoutEasing =\n 'spring' | 'linear' | 'easeInEaseOut' | 'easeIn' | 'easeOut' | 'keyboard';\n\nexport interface LayoutChange {\n readonly duration?: number;\n readonly easing?: LayoutEasing;\n /** What a view appearing does. `opacity` fades it in; the default is to scale it up. */\n readonly appear?: 'opacity' | 'scaleXY' | 'none';\n /** What a view leaving does. */\n readonly leave?: 'opacity' | 'scaleXY' | 'none';\n}\n\nexport interface NativeLayoutAnimation {\n configureNext(config: object, onDone?: () => void): void;\n}\n\nconst DEFAULTS = { duration: 300, easing: 'easeInEaseOut' as LayoutEasing };\n\n/**\n * How long past its duration an animation is given before `animate` resolves without its\n * completion: a few frames of slack, so that the completion is the one that resolves it wherever\n * one comes. React Native calls it at the end of the animation, or from a timer of its own a frame\n * after the duration where native reports nothing, as on Android with layout animations off. It\n * calls nothing at all where animations are disabled.\n */\nconst COMPLETION_GRACE_MS = 50;\n\n/** Angular's NG0101, `ApplicationRef.tick()` called while it is running. */\nconst RECURSIVE_TICK = 101;\n\n/**\n * `inject(LayoutAnimation).animate(() => this.rows.update(...))`.\n *\n * The only way to say \"animate the layout this change produces\": a transition needs a value to\n * animate from, and a row that moves up because the one above it left never had one.\n */\n@Service()\nexport class LayoutAnimation {\n /** Overridden in a test to watch a configuration without one being applied. */\n static readonly SOURCE = new InjectionToken<NativeLayoutAnimation | null>(\n 'angular-native.layoutAnimationSource',\n { factory: () => reactNative()?.LayoutAnimation ?? null },\n );\n\n private readonly native = inject(LayoutAnimation.SOURCE);\n\n /** Whether React Native's module is here. Where it is not, every call is inert. */\n readonly available: Signal<boolean> = signal(this.native !== null).asReadonly();\n private readonly engine = inject(Engine, { optional: true });\n private readonly application = inject(ApplicationRef, { optional: true });\n\n /**\n * Run `change` and animate the layout its commit produces.\n *\n * The change is taken rather than left to the caller because the platform animates whatever\n * commit comes next, and only this can make that the change's own: change detection runs and\n * the change commits before `animate` returns, with the animation configured as that commit is\n * handed over. A frame of another animation cannot land in between and take it, and a change\n * that commits nothing configures nothing, so no later commit is animated in its place. Resolves\n * when the animation ends, once its duration has passed where the platform does not report\n * that, or at once when there was nothing to animate.\n */\n async animate(change: () => void, options: LayoutChange = {}): Promise<void> {\n const native = this.native;\n if (!native) {\n change();\n return;\n }\n\n const own: Waiting = { configured: false, finish: () => {} };\n const done = new Promise<void>((resolve) => (own.finish = resolve));\n const configure = (): void => {\n // Every change still waiting is in this commit: one a pass makes together with this one.\n const together = new Set([...this.waiting, own]);\n const finish = (): void => together.forEach((change) => change.finish());\n for (const change of together) change.configured = true;\n native.configureNext(config(options), finish);\n // React Native calls no completion where animations are disabled, so nothing should wait\n // on it forever.\n setTimeout(finish, (options.duration ?? DEFAULTS.duration) + COMPLETION_GRACE_MS);\n };\n\n // With no engine there is no commit to wait for: a service built on its own, as in a test.\n if (!this.engine) {\n configure();\n change();\n return done;\n }\n\n const withdraw = this.engine.beforeNextCommit(configure);\n const settle = (): void => {\n withdraw();\n this.waiting.delete(own);\n if (!own.configured) own.finish();\n };\n this.waiting.add(own);\n try {\n change();\n // Called from inside a pass, the commit is that pass's own, which ends before any microtask.\n if (this.detectChanges()) settle();\n else queueMicrotask(settle);\n } catch (error) {\n // Nothing was changed, so nothing is to be animated, not even a commit made in this turn.\n withdraw();\n this.waiting.delete(own);\n throw error;\n }\n return done;\n }\n\n /** The changes whose commit has not come yet: more than one only inside a pass. */\n private readonly waiting = new Set<Waiting>();\n\n /**\n * Run change detection now and say so, or say that a pass is already running and will reach\n * the change.\n */\n private detectChanges(): boolean {\n try {\n this.application?.tick();\n return true;\n } catch (error) {\n if ((error as { code?: number }).code !== RECURSIVE_TICK) throw error;\n return false;\n }\n }\n}\n\n/** One `animate()` call: whether its commit was configured, and what resolves it. */\ninterface Waiting {\n configured: boolean;\n finish: () => void;\n}\n\n/**\n * The config React Native takes, from the three things worth naming.\n *\n * `create` and `delete` need a `property` as well as a type, because a view appearing has no\n * previous size to animate from - it has to be told what to animate *of*. Leaving that out is the\n * most common way one of these does nothing.\n */\nfunction config(options: LayoutChange): object {\n const duration = options.duration ?? DEFAULTS.duration;\n const easing = options.easing ?? DEFAULTS.easing;\n const appear = options.appear ?? 'opacity';\n const leave = options.leave ?? 'opacity';\n\n return {\n duration,\n update: { type: easing, ...(easing === 'spring' ? { springDamping: 0.7 } : {}) },\n ...(appear === 'none' ? {} : { create: { type: easing, property: appear } }),\n ...(leave === 'none' ? {} : { delete: { type: easing, property: leave } }),\n };\n}\n"]}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Menus as data, shared by every OS: the menu bar every desktop app gets, how an app's
|
|
3
|
+
* `<menu-bar>` merges into it, and the token a `<menu-item>` registers itself through.
|
|
4
|
+
*
|
|
5
|
+
* `@ng-native/components` builds this from `<menu-group>` and `<menu-item>`, a
|
|
6
|
+
* `<native-header-item [menu]>` reads it, and each OS draws it: the desktop host as the menu bar,
|
|
7
|
+
* iPadOS through UIMenuBuilder. The rules live here, in TypeScript, so every OS follows the same
|
|
8
|
+
* ones; only what AppKit adds itself (Services' contents, Spelling, Substitutions, Dictation and
|
|
9
|
+
* Emoji) is left to the desktop host.
|
|
10
|
+
*/
|
|
11
|
+
import { InjectionToken, type Signal } from '@angular/core';
|
|
12
|
+
/** A standard menu a `<menu-group menuRole>` adds its items to. */
|
|
13
|
+
export type MenuGroupRole = 'app' | 'file' | 'edit' | 'view' | 'window' | 'help';
|
|
14
|
+
/**
|
|
15
|
+
* What an item is for, where the OS has a standard one: an item with a `menuRole` replaces that
|
|
16
|
+
* role's default item, and without a handler does what the OS does for it.
|
|
17
|
+
*/
|
|
18
|
+
export type MenuRole = 'about' | 'settings' | 'services' | 'hide' | 'hideOthers' | 'showAll' | 'quit' | 'undo' | 'redo' | 'cut' | 'copy' | 'paste' | 'selectAll' | 'find' | 'toggleSidebar' | 'toggleFullScreen' | 'back' | 'forward' | 'minimize' | 'zoom' | 'bringAllToFront' | 'close' | 'print';
|
|
19
|
+
/** One item in a menu. */
|
|
20
|
+
export interface MenuItemModel {
|
|
21
|
+
readonly label: string;
|
|
22
|
+
readonly menuRole?: MenuRole;
|
|
23
|
+
/** `mod+s`, as `parseShortcut` reads it. */
|
|
24
|
+
readonly shortcut?: string;
|
|
25
|
+
readonly disabled?: boolean;
|
|
26
|
+
/** A check mark, where the item is a toggle. */
|
|
27
|
+
readonly checked?: boolean;
|
|
28
|
+
readonly destructive?: boolean;
|
|
29
|
+
readonly subtitle?: string;
|
|
30
|
+
/** An SF Symbol, shown on Apple's OSes. */
|
|
31
|
+
readonly sfSymbol?: string;
|
|
32
|
+
/** A drawable resource's name, shown on Android. */
|
|
33
|
+
readonly icon?: string;
|
|
34
|
+
/**
|
|
35
|
+
* The standard menu a top-level `<menu-group menuRole>` adds its items to, for a group written
|
|
36
|
+
* in a `<menu-bar>`: what becomes `MenuGroupModel.menuRole`.
|
|
37
|
+
*/
|
|
38
|
+
readonly group?: MenuGroupRole;
|
|
39
|
+
/** A submenu, or with `inline` a section of the menu the item is in. */
|
|
40
|
+
readonly items?: readonly MenuEntry[];
|
|
41
|
+
readonly inline?: boolean;
|
|
42
|
+
/** What choosing it does. Left out on an item with a `menuRole`, the OS does it. */
|
|
43
|
+
readonly select?: () => void;
|
|
44
|
+
}
|
|
45
|
+
/** A line between items. */
|
|
46
|
+
export interface MenuSeparator {
|
|
47
|
+
readonly separator: true;
|
|
48
|
+
}
|
|
49
|
+
export type MenuEntry = MenuItemModel | MenuSeparator;
|
|
50
|
+
/** One menu in the menu bar. */
|
|
51
|
+
export interface MenuGroupModel {
|
|
52
|
+
readonly label: string;
|
|
53
|
+
readonly menuRole?: MenuGroupRole;
|
|
54
|
+
readonly items: readonly MenuEntry[];
|
|
55
|
+
}
|
|
56
|
+
export declare const isSeparator: (entry: MenuEntry) => entry is MenuSeparator;
|
|
57
|
+
/**
|
|
58
|
+
* The menu bar every desktop app gets: App, File, Edit, View, Window and Help, each item with its
|
|
59
|
+
* role and standard shortcut. `Settings...` is in it until the merge, which keeps it only when the
|
|
60
|
+
* app gives it a handler. File holds Print... (`mod+p`), which prints the key window unless the app
|
|
61
|
+
* gives a `print` item a handler.
|
|
62
|
+
*/
|
|
63
|
+
export declare function defaultMenuBar(appName: string): MenuGroupModel[];
|
|
64
|
+
/**
|
|
65
|
+
* The menu bar an app ends up with, from the defaults and its `<menu-bar>`:
|
|
66
|
+
*
|
|
67
|
+
* - An app item with a `menuRole` replaces the default item with that role, where the default is,
|
|
68
|
+
* and leaves the group it was written in.
|
|
69
|
+
* - `Settings...` shows only when the app gives a `settings` item a handler.
|
|
70
|
+
* - A group whose `menuRole` names a default menu adds its items to the end of that menu; a `file`
|
|
71
|
+
* group's go before Print..., which stays last, as on a Mac.
|
|
72
|
+
* - A `file` group with no default File menu goes after the App menu, and any other group after
|
|
73
|
+
* View, in the order the app wrote them. A group left with no items is left out.
|
|
74
|
+
* - With a `file` group, the Window menu's Close Window (or the app's own `close` item) moves to
|
|
75
|
+
* the File menu, after the app's items and before the defaults.
|
|
76
|
+
*/
|
|
77
|
+
export declare function mergeMenuBar(defaults: readonly MenuGroupModel[], app: readonly MenuGroupModel[]): MenuGroupModel[];
|
|
78
|
+
/**
|
|
79
|
+
* The entries a menu's `<menu-item>`s registered, in the order they registered. Each item
|
|
80
|
+
* registers a signal of itself, so a changed label or a toggled check reaches the menu, and the
|
|
81
|
+
* function `register` answers takes it out again as the item goes.
|
|
82
|
+
*/
|
|
83
|
+
export interface MenuModel {
|
|
84
|
+
readonly entries: Signal<readonly MenuEntry[]>;
|
|
85
|
+
/**
|
|
86
|
+
* Add `entry`, at `place` where the container can order places: an item an `@if` shows later
|
|
87
|
+
* takes its place in the template, not the end.
|
|
88
|
+
*/
|
|
89
|
+
register(entry: Signal<MenuEntry>, place?: unknown): () => void;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* A menu model for a container to provide as `MENU_MODEL`. `order` compares two places, negative
|
|
93
|
+
* for the first one earlier; places it cannot compare keep the order they registered in.
|
|
94
|
+
*/
|
|
95
|
+
export declare function createMenuModel(order?: (a: unknown, b: unknown) => number): MenuModel;
|
|
96
|
+
/**
|
|
97
|
+
* Which of two elements comes first in the tree: negative for `a`. Elements in different trees, or
|
|
98
|
+
* not in one yet, compare equal, and keep the order they registered in.
|
|
99
|
+
*/
|
|
100
|
+
export declare function treeOrder(a: unknown, b: unknown): number;
|
|
101
|
+
/** A menu model whose entries keep their places in the template. */
|
|
102
|
+
export declare const menuModelInTreeOrder: () => MenuModel;
|
|
103
|
+
/**
|
|
104
|
+
* Where a `<menu-item>` registers itself: the menu it is written in, which a `<menu-group>`, a
|
|
105
|
+
* submenu or a `<native-header-item [menu]>` template provides. There is none at the root, so an
|
|
106
|
+
* item outside a menu is told so by its own injection.
|
|
107
|
+
*/
|
|
108
|
+
export declare const MENU_MODEL: InjectionToken<MenuModel>;
|