@ng-native/device 0.18.1 → 0.18.2
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/keyboard.d.ts +12 -0
- package/dist/keyboard.js +10 -2
- package/dist/keyboard.js.map +1 -1
- package/package.json +2 -2
package/dist/keyboard.d.ts
CHANGED
|
@@ -43,6 +43,12 @@ export interface KeyboardSource {
|
|
|
43
43
|
* bar. See `keyboardSource`.
|
|
44
44
|
*/
|
|
45
45
|
readonly windowTop?: number;
|
|
46
|
+
/**
|
|
47
|
+
* How far below the top of the screen the app's window itself starts, in points, for a source
|
|
48
|
+
* that can read it: the lower half of a split screen, or a freeform window, on Android. Read when
|
|
49
|
+
* it is compared, as the window can move.
|
|
50
|
+
*/
|
|
51
|
+
readonly screenTop?: number;
|
|
46
52
|
}
|
|
47
53
|
/**
|
|
48
54
|
* The events RN's own KeyboardAvoidingView listens for, which differ by platform.
|
|
@@ -127,8 +133,14 @@ export declare class Keyboard {
|
|
|
127
133
|
* of the screen: React Native takes the bar off its measurements as it lays the root out, and keeps
|
|
128
134
|
* doing so when the bar is hidden or shown later, until the root is laid out again. The bar's
|
|
129
135
|
* own height stands in where the root has not been measured.
|
|
136
|
+
*
|
|
137
|
+
* A window that itself starts below the top of the screen, in a split screen or freeform on
|
|
138
|
+
* Android, adds its own top, where the source can read it: `provideNativeKeyboard()` from
|
|
139
|
+
* `@ng-native/expo` can, and React Native alone cannot.
|
|
130
140
|
*/
|
|
131
141
|
get windowTop(): number;
|
|
142
|
+
/** How far the window a view is measured in starts below the top of the app's own window. */
|
|
143
|
+
private belowBar;
|
|
132
144
|
/** Where the keyboard is now: at every frame of its motion where `followsMotion`. */
|
|
133
145
|
readonly position: Signal<KeyboardMetrics>;
|
|
134
146
|
constructor();
|
package/dist/keyboard.js
CHANGED
|
@@ -175,11 +175,19 @@ export class Keyboard {
|
|
|
175
175
|
* of the screen: React Native takes the bar off its measurements as it lays the root out, and keeps
|
|
176
176
|
* doing so when the bar is hidden or shown later, until the root is laid out again. The bar's
|
|
177
177
|
* own height stands in where the root has not been measured.
|
|
178
|
+
*
|
|
179
|
+
* A window that itself starts below the top of the screen, in a split screen or freeform on
|
|
180
|
+
* Android, adds its own top, where the source can read it: `provideNativeKeyboard()` from
|
|
181
|
+
* `@ng-native/expo` can, and React Native alone cannot.
|
|
178
182
|
*/
|
|
179
183
|
get windowTop() {
|
|
184
|
+
return this.belowBar() + (this.source.screenTop ?? 0);
|
|
185
|
+
}
|
|
186
|
+
/** How far the window a view is measured in starts below the top of the app's own window. */
|
|
187
|
+
belowBar() {
|
|
180
188
|
let top = this.source.windowTop ?? 0;
|
|
181
|
-
// ponytail: the first root component is taken to start at the top of the
|
|
182
|
-
// root does; a root placed lower would read as a window starting higher.
|
|
189
|
+
// ponytail: the first root component is taken to start at the top of the app's window, as an
|
|
190
|
+
// app's root does; a root placed lower would read as a window starting higher.
|
|
183
191
|
const root = this.app?.components[0]?.location.nativeElement;
|
|
184
192
|
if (root && nativePlatform() === 'android')
|
|
185
193
|
this.engine?.measure(root, (frame) => (top = -frame.y));
|
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;;AAwC3B,gEAAgE;AAChE,SAAS,QAAQ,CAAC,KAA0B;IAC1C,IAAI,KAAK,CAAC,QAAQ,KAAK,SAAS;QAAE,OAAO,EAAE,CAAC;IAC5C,OAAO,EAAE,QAAQ,EAAE,KAAK,CAAC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cAAc,EAAE,KAAK,SAAS;YACxC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;QAC1D,OAAO,GAAG,CAAC;IACb,CAAC;IAED,qFAAqF;IAC5E,QAAQ,GAA4B,QAAQ,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,IAAI,CAAC,OAAO,EAAE;iFAAC,CAAC;IAE5F;QACE,MAAM,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC,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;uGAxEU,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\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 get windowTop(): number {\n let top = this.source.windowTop ?? 0;\n // ponytail: the first root component is taken to start at the top of the screen, as an app's\n // 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,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,E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C;QACrC,6FAA6F;QAC7F,+EAA+E;QAC/E,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,aAAqC,CAAC;QACrF,IAAI,IAAI,IAAI,cAAc,EAAE,KAAK,SAAS;YACxC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;QAC1D,OAAO,GAAG,CAAC;IACb,CAAC;IAED,qFAAqF;IAC5E,QAAQ,GAA4B,QAAQ,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,IAAI,CAAC,OAAO,EAAE;iFAAC,CAAC;IAE5F;QACE,MAAM,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC,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"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ng-native/device",
|
|
3
|
-
"version": "0.18.
|
|
3
|
+
"version": "0.18.2",
|
|
4
4
|
"description": "Angular Native device services: keyboard, screen, color scheme, app state, accessibility, hardware back and deep links.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"angular",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"type": "module",
|
|
18
18
|
"main": "dist/index.js",
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@ng-native/fabric": "0.18.
|
|
20
|
+
"@ng-native/fabric": "0.18.2"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
23
|
"@angular/core": "^22.0.0",
|