@a16n/web-sim 0.0.0-stage → 0.1.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/README.md +13 -2
- package/bin/cli.ts +255 -0
- package/bin/web-sim.js +39 -0
- package/overrides/@backpackapp-io/react-native-toast/overrides.ts +15 -0
- package/overrides/@react-native-firebase/app/overrides.ts +34 -0
- package/overrides/@tiktok-for-business/react-native-sdk/NativeTiktokSdk.ts +29 -0
- package/overrides/@tiktok-for-business/react-native-sdk/overrides.ts +9 -0
- package/overrides/expo-application/ExpoApplication.ts +81 -0
- package/overrides/expo-application/overrides.ts +16 -0
- package/overrides/expo-background-task/ExpoBackgroundTaskModule.ts +30 -0
- package/overrides/expo-background-task/overrides.ts +9 -0
- package/overrides/expo-constants/ExponentConstants.ts +66 -0
- package/overrides/expo-constants/overrides.ts +9 -0
- package/overrides/expo-device/ExpoDevice.ts +104 -0
- package/overrides/expo-device/overrides.ts +9 -0
- package/overrides/expo-glass-effect/GlassContainer.tsx +9 -0
- package/overrides/expo-glass-effect/GlassView.tsx +95 -0
- package/overrides/expo-glass-effect/availability.ts +14 -0
- package/overrides/expo-glass-effect/overrides.ts +14 -0
- package/overrides/expo-haptics/Haptics.ts +67 -0
- package/overrides/expo-haptics/overrides.ts +5 -0
- package/overrides/expo-mail-composer/ExpoMailComposer.ts +40 -0
- package/overrides/expo-mail-composer/overrides.ts +9 -0
- package/overrides/expo-mesh-gradient/MeshGradientView.tsx +44 -0
- package/overrides/expo-mesh-gradient/overrides.ts +9 -0
- package/overrides/expo-modules-core/overrides.ts +12 -0
- package/overrides/expo-navigation-bar/ExpoNavigationBar.ts +29 -0
- package/overrides/expo-navigation-bar/NavigationBar.ts +43 -0
- package/overrides/expo-navigation-bar/overrides.ts +12 -0
- package/overrides/expo-router/NativeStackView.tsx +74 -0
- package/overrides/expo-router/NativeTabsView.tsx +263 -0
- package/overrides/expo-router/SafeAreaProvider.ts +6 -0
- package/overrides/expo-router/overrides.ts +23 -0
- package/overrides/expo-router/statusBarRequests.ts +51 -0
- package/overrides/expo-screen-orientation/ExpoScreenOrientation.ts +173 -0
- package/overrides/expo-screen-orientation/overrides.ts +21 -0
- package/overrides/expo-sharing/SharingNativeModule.ts +21 -0
- package/overrides/expo-sharing/overrides.ts +9 -0
- package/overrides/expo-sms/ExpoSMS.ts +24 -0
- package/overrides/expo-sms/overrides.ts +9 -0
- package/overrides/expo-splash-screen/SplashScreen.ts +25 -0
- package/overrides/expo-splash-screen/overrides.ts +5 -0
- package/overrides/expo-status-bar/StatusBar.tsx +44 -0
- package/overrides/expo-status-bar/overrides.ts +5 -0
- package/overrides/expo-store-review/ExpoStoreReview.ts +18 -0
- package/overrides/expo-store-review/overrides.ts +9 -0
- package/overrides/expo-symbols/SymbolView.tsx +192 -0
- package/overrides/expo-symbols/overrides.ts +9 -0
- package/overrides/expo-system-ui/ExpoSystemUI.ts +34 -0
- package/overrides/expo-system-ui/overrides.ts +9 -0
- package/overrides/expo-task-manager/ExpoTaskManager.ts +39 -0
- package/overrides/expo-task-manager/overrides.ts +9 -0
- package/overrides/expo-tracking-transparency/ExpoTrackingTransparency.ts +30 -0
- package/overrides/expo-tracking-transparency/overrides.ts +11 -0
- package/overrides/expo-video/ExpoVideo.ts +16 -0
- package/overrides/expo-video/overrides.ts +9 -0
- package/overrides/lottie-react-native/LottieView.tsx +87 -0
- package/overrides/lottie-react-native/overrides.ts +9 -0
- package/overrides/react-native-pager-view/PagerViewNativeComponent.tsx +254 -0
- package/overrides/react-native-pager-view/overrides.ts +9 -0
- package/overrides/react-native-reanimated/overrides.ts +28 -0
- package/overrides/react-native-safe-area-context/NativeSafeAreaProvider.tsx +40 -0
- package/overrides/react-native-safe-area-context/overrides.ts +12 -0
- package/overrides/react-native-share/NativeRNShare.ts +22 -0
- package/overrides/react-native-share/overrides.ts +12 -0
- package/overrides/react-native-share/platform.ts +14 -0
- package/overrides/react-native-turbo-image/TurboImage.tsx +101 -0
- package/overrides/react-native-turbo-image/overrides.ts +9 -0
- package/overrides/react-native-web/RefreshControl.tsx +57 -0
- package/overrides/react-native-web/overrides.ts +153 -0
- package/overrides/react-native-web/useElementLayout.ts +140 -0
- package/package.json +78 -4
- package/plugins/battery/overrides/expo-battery/ExpoBattery.ts +57 -0
- package/plugins/battery/overrides/expo-battery/overrides.ts +9 -0
- package/plugins/battery/plugin.ts +8 -0
- package/plugins/battery/shell.tsx +56 -0
- package/plugins/battery/state.ts +11 -0
- package/plugins/biometrics/overrides/expo-local-authentication/ExpoLocalAuthentication.ts +90 -0
- package/plugins/biometrics/overrides/expo-local-authentication/overrides.ts +11 -0
- package/plugins/biometrics/plugin.ts +8 -0
- package/plugins/biometrics/shell.tsx +88 -0
- package/plugins/biometrics/state.ts +41 -0
- package/plugins/brightness/app.ts +5 -0
- package/plugins/brightness/overrides/expo-brightness/Brightness.ts +98 -0
- package/plugins/brightness/overrides/expo-brightness/overrides.ts +10 -0
- package/plugins/brightness/plugin.ts +9 -0
- package/plugins/brightness/shell.tsx +55 -0
- package/plugins/brightness/state.ts +19 -0
- package/plugins/calendar/calendar.ts +299 -0
- package/plugins/calendar/overrides/expo-calendar/ExpoCalendar.ts +358 -0
- package/plugins/calendar/overrides/expo-calendar/LegacyExpoCalendar.ts +163 -0
- package/plugins/calendar/overrides/expo-calendar/overrides.ts +14 -0
- package/plugins/calendar/plugin.ts +8 -0
- package/plugins/calendar/shell.tsx +123 -0
- package/plugins/camera/overrides/expo-camera/ExpoCamera.tsx +269 -0
- package/plugins/camera/overrides/expo-camera/ExpoCameraManager.ts +56 -0
- package/plugins/camera/overrides/expo-camera/overrides.ts +12 -0
- package/plugins/camera/plugin.ts +9 -0
- package/plugins/camera/shell.tsx +107 -0
- package/plugins/camera/state.ts +61 -0
- package/plugins/clipboard/overrides/expo-clipboard/ExpoClipboard.ts +138 -0
- package/plugins/clipboard/overrides/expo-clipboard/overrides.ts +9 -0
- package/plugins/clipboard/plugin.ts +8 -0
- package/plugins/clipboard/shell.tsx +122 -0
- package/plugins/clipboard/state.ts +93 -0
- package/plugins/clock/app.ts +25 -0
- package/plugins/clock/plugin.ts +8 -0
- package/plugins/clock/shell.tsx +225 -0
- package/plugins/clock/state.ts +12 -0
- package/plugins/contacts/contacts.ts +125 -0
- package/plugins/contacts/overrides/expo-contacts/ExpoContacts.ts +153 -0
- package/plugins/contacts/overrides/expo-contacts/ExpoContactsNext.ts +232 -0
- package/plugins/contacts/overrides/expo-contacts/overrides.ts +12 -0
- package/plugins/contacts/plugin.ts +8 -0
- package/plugins/contacts/shell.tsx +109 -0
- package/plugins/home-indicator/app.ts +5 -0
- package/plugins/home-indicator/plugin.ts +9 -0
- package/plugins/home-indicator/requests.ts +48 -0
- package/plugins/home-indicator/shell.tsx +68 -0
- package/plugins/home-indicator/state.ts +31 -0
- package/plugins/keep-awake/app.ts +5 -0
- package/plugins/keep-awake/overrides/expo-keep-awake/ExpoKeepAwake.ts +27 -0
- package/plugins/keep-awake/overrides/expo-keep-awake/overrides.ts +9 -0
- package/plugins/keep-awake/plugin.ts +9 -0
- package/plugins/keep-awake/shell.tsx +80 -0
- package/plugins/keep-awake/state.ts +12 -0
- package/plugins/keyboard/Keyboard.tsx +354 -0
- package/plugins/keyboard/app.ts +90 -0
- package/plugins/keyboard/controller.ts +384 -0
- package/plugins/keyboard/fields.ts +244 -0
- package/plugins/keyboard/icons.tsx +283 -0
- package/plugins/keyboard/keyboard.css +362 -0
- package/plugins/keyboard/layouts/android.ts +175 -0
- package/plugins/keyboard/layouts/index.ts +10 -0
- package/plugins/keyboard/layouts/ios.ts +219 -0
- package/plugins/keyboard/layouts/types.ts +80 -0
- package/plugins/keyboard/overrides/react-native-keyboard-controller/KeyboardController.tsx +470 -0
- package/plugins/keyboard/overrides/react-native-keyboard-controller/overrides.ts +9 -0
- package/plugins/keyboard/plugin.ts +13 -0
- package/plugins/keyboard/shell.tsx +133 -0
- package/plugins/keyboard/state.ts +92 -0
- package/plugins/keyboard/suggestions.ts +63 -0
- package/plugins/keyboard/taps.ts +77 -0
- package/plugins/keyboard/typing.ts +112 -0
- package/plugins/locale/app.ts +232 -0
- package/plugins/locale/locale.ts +143 -0
- package/plugins/locale/overrides/expo-localization/ExpoLocalization.ts +41 -0
- package/plugins/locale/overrides/expo-localization/overrides.ts +9 -0
- package/plugins/locale/plugin.ts +9 -0
- package/plugins/locale/shell.tsx +147 -0
- package/plugins/locale/state.ts +36 -0
- package/plugins/location/geo.ts +56 -0
- package/plugins/location/overrides/expo-location/ExpoLocation.ts +235 -0
- package/plugins/location/overrides/expo-location/overrides.ts +11 -0
- package/plugins/location/places.ts +38 -0
- package/plugins/location/plugin.ts +8 -0
- package/plugins/location/shell.tsx +188 -0
- package/plugins/location/state.ts +51 -0
- package/plugins/motion/overrides/expo-sensors/DeviceSensor.ts +169 -0
- package/plugins/motion/overrides/expo-sensors/ExponentPedometer.ts +54 -0
- package/plugins/motion/overrides/expo-sensors/overrides.ts +14 -0
- package/plugins/motion/plugin.ts +8 -0
- package/plugins/motion/sensors.ts +116 -0
- package/plugins/motion/shell.tsx +136 -0
- package/plugins/motion/state.ts +46 -0
- package/plugins/network/app.ts +400 -0
- package/plugins/network/overrides/expo-network/ExpoNetwork.ts +51 -0
- package/plugins/network/overrides/expo-network/overrides.ts +9 -0
- package/plugins/network/plugin.ts +8 -0
- package/plugins/network/shell.tsx +67 -0
- package/plugins/network/state.ts +29 -0
- package/plugins/notifications/center.ts +255 -0
- package/plugins/notifications/content.ts +36 -0
- package/plugins/notifications/overrides/expo-notifications/NotificationsModule.ts +175 -0
- package/plugins/notifications/overrides/expo-notifications/channels.ts +108 -0
- package/plugins/notifications/overrides/expo-notifications/getDevicePushTokenAsync.ts +19 -0
- package/plugins/notifications/overrides/expo-notifications/getExpoPushTokenAsync.ts +16 -0
- package/plugins/notifications/overrides/expo-notifications/overrides.ts +36 -0
- package/plugins/notifications/plugin.ts +8 -0
- package/plugins/notifications/shell.tsx +192 -0
- package/plugins/notifications/state.ts +56 -0
- package/plugins/notifications/tokens.ts +47 -0
- package/plugins/notifications/trigger.ts +107 -0
- package/plugins/permissions/plugin.ts +6 -0
- package/plugins/permissions/shell.tsx +50 -0
- package/plugins/permissions/state.ts +38 -0
- package/plugins/photos/appLibrary.ts +114 -0
- package/plugins/photos/library.ts +322 -0
- package/plugins/photos/overrides/expo-image-picker/ExponentImagePicker.ts +153 -0
- package/plugins/photos/overrides/expo-image-picker/overrides.ts +13 -0
- package/plugins/photos/overrides/expo-media-library/ExpoMediaLibrary.ts +235 -0
- package/plugins/photos/overrides/expo-media-library/NativeMediaLibraryModule.ts +318 -0
- package/plugins/photos/overrides/expo-media-library/overrides.ts +19 -0
- package/plugins/photos/plugin.ts +8 -0
- package/plugins/photos/samples.ts +367 -0
- package/plugins/photos/shell.tsx +164 -0
- package/plugins/photos/state.ts +88 -0
- package/plugins/storage/files.ts +351 -0
- package/plugins/storage/overrides/expo-file-system/ExpoFileSystem.ts +770 -0
- package/plugins/storage/overrides/expo-file-system/ExponentFileSystem.ts +196 -0
- package/plugins/storage/overrides/expo-file-system/overrides.ts +13 -0
- package/plugins/storage/overrides/expo-secure-store/ExpoSecureStore.ts +75 -0
- package/plugins/storage/overrides/expo-secure-store/overrides.ts +9 -0
- package/plugins/storage/plugin.ts +8 -0
- package/plugins/storage/reset.ts +136 -0
- package/plugins/storage/secure.ts +71 -0
- package/plugins/storage/shell.tsx +365 -0
- package/plugins/storage/state.ts +13 -0
- package/src/client/app/appDocument.ts +113 -0
- package/src/client/app/appFocus.ts +97 -0
- package/src/client/app/console.ts +118 -0
- package/src/client/app/fonts.ts +59 -0
- package/src/client/app/main.tsx +99 -0
- package/src/client/app/routeContext.ts +45 -0
- package/src/client/app/touch.ts +640 -0
- package/src/client/entry.ts +20 -0
- package/src/client/modules.d.ts +8 -0
- package/src/client/react-native/app.ts +57 -0
- package/src/client/react-native/deep.ts +13 -0
- package/src/client/react-native/keyboard.tsx +192 -0
- package/src/client/react-native/library.ts +256 -0
- package/src/client/shell/Cookies.tsx +95 -0
- package/src/client/shell/LogDrawer.tsx +415 -0
- package/src/client/shell/PhotoPicker.tsx +174 -0
- package/src/client/shell/PluginOverlays.tsx +42 -0
- package/src/client/shell/PluginPanels.tsx +82 -0
- package/src/client/shell/ResetApp.tsx +87 -0
- package/src/client/shell/Resizer.tsx +99 -0
- package/src/client/shell/Shell.tsx +531 -0
- package/src/client/shell/SystemBars.tsx +376 -0
- package/src/client/shell/SystemCamera.tsx +181 -0
- package/src/client/shell/SystemOverlay.tsx +422 -0
- package/src/client/shell/SystemSheets.tsx +462 -0
- package/src/client/shell/backdrop.ts +229 -0
- package/src/client/shell/control.ts +91 -0
- package/src/client/shell/events.ts +226 -0
- package/src/client/shell/geometry.ts +68 -0
- package/src/client/shell/host.ts +492 -0
- package/src/client/shell/icons.tsx +34 -0
- package/src/client/shell/logFeed.ts +102 -0
- package/src/client/shell/main.tsx +22 -0
- package/src/client/shell/permissionCopy.ts +169 -0
- package/src/client/shell/plugins.ts +266 -0
- package/src/client/shell/popout.ts +185 -0
- package/src/client/shell/popover.ts +27 -0
- package/src/client/shell/shell.css +878 -0
- package/src/client/shell/statusTime.ts +31 -0
- package/src/client/shell/tooltips.ts +104 -0
- package/src/client/virtual.d.ts +21 -0
- package/src/devices/catalog.ts +223 -0
- package/src/devices/types.ts +83 -0
- package/src/node/appInfo.ts +186 -0
- package/src/node/appSource.ts +138 -0
- package/src/node/config.ts +180 -0
- package/src/node/control.ts +144 -0
- package/src/node/doctor.ts +199 -0
- package/src/node/events/cli.ts +206 -0
- package/src/node/events/shared.ts +238 -0
- package/src/node/events/store.ts +266 -0
- package/src/node/index.ts +5 -0
- package/src/node/overrides/define.ts +56 -0
- package/src/node/overrides/packages.ts +136 -0
- package/src/node/overrides/platformPatch.ts +56 -0
- package/src/node/overrides/plugin.ts +166 -0
- package/src/node/overrides/registry.ts +258 -0
- package/src/node/paths.ts +18 -0
- package/src/node/platform.ts +171 -0
- package/src/node/plugins.ts +98 -0
- package/src/node/proxy.ts +421 -0
- package/src/node/server.ts +253 -0
- package/src/plugin/index.ts +33 -0
- package/src/plugin/types.ts +127 -0
- package/src/plugin/ui.tsx +562 -0
- package/src/runtime/clock.ts +112 -0
- package/src/runtime/draw.ts +196 -0
- package/src/runtime/index.ts +513 -0
- package/src/runtime/media.ts +379 -0
- package/src/runtime/records.ts +76 -0
- package/src/runtime/timezone.ts +81 -0
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { plugin } from 'web-sim/runtime'
|
|
2
|
+
import { INITIAL_KEEP_AWAKE, type KeepAwakeState } from './state.ts'
|
|
3
|
+
|
|
4
|
+
// A new run of the app holds no keep-awake: the last run's went with its process.
|
|
5
|
+
plugin<KeepAwakeState>('keep-awake', INITIAL_KEEP_AWAKE).set({ tags: [] })
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { plugin, realNow } from 'web-sim/runtime'
|
|
2
|
+
import { INITIAL_KEEP_AWAKE, type KeepAwakeState } from '../../state.ts'
|
|
3
|
+
|
|
4
|
+
const keepAwake = () => plugin<KeepAwakeState>('keep-awake', INITIAL_KEEP_AWAKE)
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* `ExpoKeepAwake` as on a device: each tag holds the screen awake until it is released, and releasing a tag that
|
|
8
|
+
* isn't held does nothing. Native modules have no `addListenerForTag`, so `addListener` throws `UnavailabilityError`.
|
|
9
|
+
*/
|
|
10
|
+
export default {
|
|
11
|
+
async isAvailableAsync() {
|
|
12
|
+
return true
|
|
13
|
+
},
|
|
14
|
+
async activate(tag: string) {
|
|
15
|
+
const { tags } = keepAwake().get()
|
|
16
|
+
if (!tags.includes(tag)) {
|
|
17
|
+
keepAwake().set({ tags: [...tags, tag], screen: 'on', idleSince: realNow() })
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
async deactivate(tag: string) {
|
|
21
|
+
const { tags } = keepAwake().get()
|
|
22
|
+
if (tags.includes(tag)) {
|
|
23
|
+
// The idle timer starts again from the release.
|
|
24
|
+
keepAwake().set({ tags: tags.filter((held) => held !== tag), idleSince: realNow() })
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { defineOverrides } from '../../../../src/node/overrides/define.ts'
|
|
2
|
+
|
|
3
|
+
export default defineOverrides('expo-keep-awake', [
|
|
4
|
+
{
|
|
5
|
+
versions: '>=58 <59',
|
|
6
|
+
note: "keep-awake tags held in the keep-awake panel, which hold off its auto-lock; never the browser's Wake Lock",
|
|
7
|
+
files: { 'build/ExpoKeepAwake.web.js': './ExpoKeepAwake.ts' },
|
|
8
|
+
},
|
|
9
|
+
])
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { CoffeeIcon } from '@phosphor-icons/react'
|
|
2
|
+
import { Button, defineShellPlugin, Field, Row, Select } from 'web-sim/shell'
|
|
3
|
+
import { INITIAL_KEEP_AWAKE, type KeepAwakeState } from './state.ts'
|
|
4
|
+
|
|
5
|
+
/** How long the screen stays dimmed before it goes black. */
|
|
6
|
+
const DIM_MS = 5000
|
|
7
|
+
|
|
8
|
+
const AUTO_LOCK = [
|
|
9
|
+
{ value: '0', label: 'Never' },
|
|
10
|
+
{ value: '15', label: '15 seconds' },
|
|
11
|
+
{ value: '30', label: '30 seconds' },
|
|
12
|
+
{ value: '60', label: '1 minute' },
|
|
13
|
+
{ value: '120', label: '2 minutes' },
|
|
14
|
+
{ value: '300', label: '5 minutes' },
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
const SCREEN = { on: 'On', dim: 'Dimmed', off: 'Off' } as const
|
|
18
|
+
|
|
19
|
+
/** Where the screen should be now: on while a tag is held or auto-lock is off, else by how long it has sat idle. */
|
|
20
|
+
function screenNow(state: KeepAwakeState, now: number): KeepAwakeState['screen'] {
|
|
21
|
+
if (state.tags.length > 0 || state.autoLock === 0) {
|
|
22
|
+
return state.screen === 'off' ? 'off' : 'on'
|
|
23
|
+
}
|
|
24
|
+
const idle = now - state.idleSince
|
|
25
|
+
return idle >= state.autoLock * 1000 + DIM_MS ? 'off' : idle >= state.autoLock * 1000 ? 'dim' : state.screen === 'off' ? 'off' : 'on'
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export default defineShellPlugin<KeepAwakeState>({
|
|
29
|
+
id: 'keep-awake',
|
|
30
|
+
title: 'Keep awake',
|
|
31
|
+
icon: CoffeeIcon,
|
|
32
|
+
initialState: INITIAL_KEEP_AWAKE,
|
|
33
|
+
screen: (state) => (state.screen === 'off' ? { filter: 'brightness(0)', blockInput: true } : state.screen === 'dim' ? { filter: 'brightness(0.35)' } : {}),
|
|
34
|
+
onPress: (context) => context.set({ screen: 'on', idleSince: Date.now() }),
|
|
35
|
+
actions: {
|
|
36
|
+
/** Wakes the screen, as a press does. */
|
|
37
|
+
wake: (context) => {
|
|
38
|
+
context.set({ screen: 'on', idleSince: Date.now() })
|
|
39
|
+
return context.state()
|
|
40
|
+
},
|
|
41
|
+
/** Locks the screen now: black until a press. */
|
|
42
|
+
lock: (context) => {
|
|
43
|
+
context.set({ screen: 'off' })
|
|
44
|
+
return context.state()
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
setup: (context) => {
|
|
48
|
+
context.set({ screen: 'on', idleSince: Date.now() })
|
|
49
|
+
const timer = setInterval(() => {
|
|
50
|
+
const screen = screenNow(context.state(), Date.now())
|
|
51
|
+
if (screen !== context.state().screen) {
|
|
52
|
+
context.set({ screen })
|
|
53
|
+
}
|
|
54
|
+
}, 500)
|
|
55
|
+
return () => clearInterval(timer)
|
|
56
|
+
},
|
|
57
|
+
Panel: ({ state, set, run }) => (
|
|
58
|
+
<>
|
|
59
|
+
<Field label="Kept awake by app" value={state.tags.length > 0 ? 'Yes' : 'No'}>
|
|
60
|
+
{state.tags.length > 0 && <span className="wsp-value">{state.tags.join(', ')}</span>}
|
|
61
|
+
</Field>
|
|
62
|
+
<Field label="Auto-lock">
|
|
63
|
+
<Select label="Auto-lock" value={String(state.autoLock)} options={AUTO_LOCK} onChange={(value) => set({ autoLock: Number(value), idleSince: Date.now() })} />
|
|
64
|
+
</Field>
|
|
65
|
+
<Field label="Screen" value={SCREEN[state.screen]}>
|
|
66
|
+
<Row>
|
|
67
|
+
{state.screen === 'on' ? (
|
|
68
|
+
<Button title="Lock the screen" onClick={() => run('lock')}>
|
|
69
|
+
Lock
|
|
70
|
+
</Button>
|
|
71
|
+
) : (
|
|
72
|
+
<Button title="Wake the screen" onClick={() => run('wake')}>
|
|
73
|
+
Wake
|
|
74
|
+
</Button>
|
|
75
|
+
)}
|
|
76
|
+
</Row>
|
|
77
|
+
</Field>
|
|
78
|
+
</>
|
|
79
|
+
),
|
|
80
|
+
})
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** The screen's sleep: what the app keeps awake with `expo-keep-awake`, and the auto-lock that dims and blanks it. */
|
|
2
|
+
export type KeepAwakeState = {
|
|
3
|
+
/** The app's active keep-awake tags. While any is held the screen never sleeps. */
|
|
4
|
+
tags: string[]
|
|
5
|
+
/** Seconds without a press before the screen dims, then goes black. 0 never locks. */
|
|
6
|
+
autoLock: number
|
|
7
|
+
screen: 'on' | 'dim' | 'off'
|
|
8
|
+
/** When the screen was last pressed or woken, in ms since the epoch. */
|
|
9
|
+
idleSince: number
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const INITIAL_KEEP_AWAKE: KeepAwakeState = { tags: [], autoLock: 0, screen: 'on', idleSince: 0 }
|
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
import { type CSSProperties, type PointerEvent, type ReactNode, useEffect, useState, useSyncExternalStore } from 'react'
|
|
2
|
+
import type { Appearance } from 'web-sim/shell'
|
|
3
|
+
import type { KeyboardController, Live } from './controller.ts'
|
|
4
|
+
import {
|
|
5
|
+
ArrowRightIcon,
|
|
6
|
+
ArrowUpIcon,
|
|
7
|
+
CheckmarkIcon,
|
|
8
|
+
DeleteIcon,
|
|
9
|
+
EmojiIcon,
|
|
10
|
+
MaterialApps,
|
|
11
|
+
MaterialArrow,
|
|
12
|
+
MaterialBackspace,
|
|
13
|
+
MaterialCheck,
|
|
14
|
+
MaterialClipboard,
|
|
15
|
+
MaterialEmoji,
|
|
16
|
+
MaterialGif,
|
|
17
|
+
MaterialMic,
|
|
18
|
+
MaterialNext,
|
|
19
|
+
MaterialReturn,
|
|
20
|
+
MaterialSearch,
|
|
21
|
+
MaterialSend,
|
|
22
|
+
MaterialSettings,
|
|
23
|
+
MaterialShift,
|
|
24
|
+
MaterialSticker,
|
|
25
|
+
MicIcon,
|
|
26
|
+
ReturnIcon,
|
|
27
|
+
SearchIcon,
|
|
28
|
+
ShiftIcon,
|
|
29
|
+
} from './icons.tsx'
|
|
30
|
+
import type { KeyboardLayout, KeySpec } from './layouts/index.ts'
|
|
31
|
+
import type { FieldConfig, KeyboardState, ReturnKeyType } from './state.ts'
|
|
32
|
+
import { TIMING } from './state.ts'
|
|
33
|
+
import { currentWord, suggestionsFor } from './suggestions.ts'
|
|
34
|
+
import './keyboard.css'
|
|
35
|
+
|
|
36
|
+
/** The iOS key popup's shape, in points at portrait size. */
|
|
37
|
+
const POPUP = { overhang: 11, headHeight: 52, headRise: 58, headCornerRadius: 11, screenMargin: 1 }
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The drawn keyboard, at the bottom of the screen. It slides in while the plugin's state says `shown`, and stays
|
|
41
|
+
* drawn until it has slid out. Presses on it never take the focus from the app's field.
|
|
42
|
+
*/
|
|
43
|
+
export function KeyboardView({ controller, state, layout, appearance }: { controller: KeyboardController; state: KeyboardState; layout: KeyboardLayout; appearance: { device: Appearance; app: Appearance } }) {
|
|
44
|
+
const live = useSyncExternalStore(controller.subscribe, controller.getLive)
|
|
45
|
+
const timing = TIMING[layout.platform]
|
|
46
|
+
const [present, setPresent] = useState(state.shown)
|
|
47
|
+
const [entered, setEntered] = useState(state.shown)
|
|
48
|
+
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
if (state.shown) {
|
|
51
|
+
setPresent(true)
|
|
52
|
+
// Drawn off the screen for a frame first, so it slides in.
|
|
53
|
+
let frame = requestAnimationFrame(() => {
|
|
54
|
+
frame = requestAnimationFrame(() => setEntered(true))
|
|
55
|
+
})
|
|
56
|
+
return () => cancelAnimationFrame(frame)
|
|
57
|
+
}
|
|
58
|
+
setEntered(false)
|
|
59
|
+
const timer = setTimeout(() => setPresent(false), timing.drawnMs)
|
|
60
|
+
return () => clearTimeout(timer)
|
|
61
|
+
}, [state.shown, timing.drawnMs])
|
|
62
|
+
|
|
63
|
+
if (!present) {
|
|
64
|
+
return null
|
|
65
|
+
}
|
|
66
|
+
const config = controller.fieldConfig()
|
|
67
|
+
// iOS draws its keyboard in the app's window, so it takes the app's appearance unless the field pins one. Gboard is
|
|
68
|
+
// its own app, themed by the device's setting whatever the app or field ask.
|
|
69
|
+
const scheme = layout.platform === 'ios' ? (config.appearance === 'default' ? appearance.app : config.appearance) : appearance.device
|
|
70
|
+
const visible = state.shown && entered
|
|
71
|
+
const pressedKey = live.pressed === null ? undefined : layout.keys.find((key) => key.id === live.pressed)
|
|
72
|
+
const style = {
|
|
73
|
+
width: layout.width,
|
|
74
|
+
height: layout.height,
|
|
75
|
+
transform: visible ? 'translateY(0)' : `translateY(${layout.height + 8}px)`,
|
|
76
|
+
transitionDuration: `${timing.drawnMs}ms`,
|
|
77
|
+
transitionTimingFunction: timing.curve,
|
|
78
|
+
'--wsk-scale': layout.scale,
|
|
79
|
+
} as CSSProperties
|
|
80
|
+
const keep = (event: { preventDefault(): void }) => event.preventDefault()
|
|
81
|
+
return (
|
|
82
|
+
<div
|
|
83
|
+
className={`wsk-keyboard wsk-${layout.platform}`}
|
|
84
|
+
data-scheme={scheme}
|
|
85
|
+
data-testid="web-sim-keyboard"
|
|
86
|
+
style={style}
|
|
87
|
+
onPointerDown={keep}
|
|
88
|
+
onMouseDown={keep}
|
|
89
|
+
onTouchStart={keep}
|
|
90
|
+
onContextMenu={keep}
|
|
91
|
+
>
|
|
92
|
+
<div className="wsk-panel" />
|
|
93
|
+
{layout.strip !== null && <Strip controller={controller} layout={layout} live={live} config={config} state={state} />}
|
|
94
|
+
{layout.keys.map((key) => (
|
|
95
|
+
<Key key={key.id} controller={controller} spec={key} state={state} config={config} layout={layout} pressed={live.pressed === key.id} />
|
|
96
|
+
))}
|
|
97
|
+
{pressedKey?.popup === true && (layout.platform === 'ios' ? <IosPopup spec={pressedKey} state={state} layout={layout} /> : <AndroidPopup spec={pressedKey} state={state} />)}
|
|
98
|
+
</div>
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
type KeyProps = { controller: KeyboardController; spec: KeySpec; state: KeyboardState; config: FieldConfig; layout: KeyboardLayout; pressed: boolean }
|
|
103
|
+
|
|
104
|
+
function Key({ controller, spec, state, config, layout, pressed }: KeyProps) {
|
|
105
|
+
const accent = spec.tone === 'accent' || (layout.platform === 'ios' && spec.action.kind === 'return' && isAccentReturn(config.returnKeyType))
|
|
106
|
+
const className = [
|
|
107
|
+
'wsk-key',
|
|
108
|
+
`wsk-key-${accent ? 'accent' : spec.tone}`,
|
|
109
|
+
layout.numberPad ? 'wsk-key-number-pad' : '',
|
|
110
|
+
pressed && !(spec.popup && layout.platform === 'ios') ? 'wsk-key-pressed' : '',
|
|
111
|
+
]
|
|
112
|
+
.filter(Boolean)
|
|
113
|
+
.join(' ')
|
|
114
|
+
const onPointerDown = (event: PointerEvent<HTMLDivElement>) => {
|
|
115
|
+
event.preventDefault()
|
|
116
|
+
event.currentTarget.setPointerCapture(event.pointerId)
|
|
117
|
+
controller.press(spec.id)
|
|
118
|
+
}
|
|
119
|
+
return (
|
|
120
|
+
<div
|
|
121
|
+
className={className}
|
|
122
|
+
data-key={spec.id}
|
|
123
|
+
style={{ left: spec.x, top: spec.y, width: spec.width, height: spec.height, borderRadius: spec.radius }}
|
|
124
|
+
onPointerDown={onPointerDown}
|
|
125
|
+
onPointerUp={() => controller.release(spec.id)}
|
|
126
|
+
onPointerCancel={() => controller.cancel()}
|
|
127
|
+
>
|
|
128
|
+
{layout.platform === 'ios' ? <IosFace spec={spec} state={state} config={config} numberPad={layout.numberPad} /> : <AndroidFace spec={spec} state={state} config={config} />}
|
|
129
|
+
</div>
|
|
130
|
+
)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function letterLabel(spec: KeySpec, state: KeyboardState): { text: string; upper: boolean } {
|
|
134
|
+
if (spec.action.kind !== 'letter') {
|
|
135
|
+
return { text: spec.label ?? '', upper: false }
|
|
136
|
+
}
|
|
137
|
+
const upper = state.shift !== 'off'
|
|
138
|
+
return { text: upper ? spec.action.char.toUpperCase() : spec.action.char, upper }
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function Icon({ children }: { children: ReactNode }) {
|
|
142
|
+
return <span className="wsk-icon">{children}</span>
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function IosFace({ spec, state, config, numberPad }: { spec: KeySpec; state: KeyboardState; config: FieldConfig; numberPad: boolean }) {
|
|
146
|
+
const { action } = spec
|
|
147
|
+
switch (action.kind) {
|
|
148
|
+
case 'letter': {
|
|
149
|
+
const { text, upper } = letterLabel(spec, state)
|
|
150
|
+
return <span className={upper ? 'wsk-label wsk-letter-upper' : 'wsk-label wsk-letter-lower'}>{text}</span>
|
|
151
|
+
}
|
|
152
|
+
case 'char':
|
|
153
|
+
if (numberPad) {
|
|
154
|
+
return spec.tone === 'bare' ? (
|
|
155
|
+
<span className="wsk-label wsk-pad-symbol">{spec.label}</span>
|
|
156
|
+
) : (
|
|
157
|
+
<>
|
|
158
|
+
<span className="wsk-label wsk-pad-digit">{spec.label}</span>
|
|
159
|
+
{spec.subLabel !== undefined && <span className="wsk-label wsk-pad-letters">{spec.subLabel}</span>}
|
|
160
|
+
</>
|
|
161
|
+
)
|
|
162
|
+
}
|
|
163
|
+
return <span className="wsk-label wsk-character">{spec.label}</span>
|
|
164
|
+
case 'layer':
|
|
165
|
+
return <span className={spec.label === '#+=' ? 'wsk-label wsk-layer-small' : 'wsk-label wsk-layer'}>{spec.label}</span>
|
|
166
|
+
case 'shift':
|
|
167
|
+
return <Icon>{<ShiftIcon state={state.shift} />}</Icon>
|
|
168
|
+
case 'delete':
|
|
169
|
+
return <Icon>{<DeleteIcon />}</Icon>
|
|
170
|
+
case 'return':
|
|
171
|
+
return <Icon>{iosReturnIcon(config.returnKeyType)}</Icon>
|
|
172
|
+
case 'emoji':
|
|
173
|
+
return <Icon>{<EmojiIcon />}</Icon>
|
|
174
|
+
case 'dictation':
|
|
175
|
+
return <Icon>{<MicIcon />}</Icon>
|
|
176
|
+
case 'space':
|
|
177
|
+
return null
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function AndroidFace({ spec, state, config }: { spec: KeySpec; state: KeyboardState; config: FieldConfig }) {
|
|
182
|
+
const { action } = spec
|
|
183
|
+
switch (action.kind) {
|
|
184
|
+
case 'letter':
|
|
185
|
+
case 'char': {
|
|
186
|
+
const { text } = letterLabel(spec, state)
|
|
187
|
+
return (
|
|
188
|
+
<>
|
|
189
|
+
<span className={spec.subLabel !== undefined ? 'wsk-label wsk-pad-digit' : 'wsk-label wsk-character'}>{text}</span>
|
|
190
|
+
{spec.subLabel !== undefined && <span className="wsk-label wsk-pad-letters">{spec.subLabel}</span>}
|
|
191
|
+
{spec.hint !== undefined && <span className="wsk-hint">{spec.hint}</span>}
|
|
192
|
+
</>
|
|
193
|
+
)
|
|
194
|
+
}
|
|
195
|
+
case 'layer':
|
|
196
|
+
return <span className="wsk-label wsk-layer">{spec.label}</span>
|
|
197
|
+
case 'shift':
|
|
198
|
+
return <Icon>{<MaterialShift state={state.shift} />}</Icon>
|
|
199
|
+
case 'delete':
|
|
200
|
+
return <Icon>{<MaterialBackspace />}</Icon>
|
|
201
|
+
case 'return':
|
|
202
|
+
return <Icon>{androidReturnIcon(config)}</Icon>
|
|
203
|
+
case 'emoji':
|
|
204
|
+
return <Icon>{<MaterialEmoji />}</Icon>
|
|
205
|
+
case 'dictation':
|
|
206
|
+
return <Icon>{<MaterialMic />}</Icon>
|
|
207
|
+
case 'space':
|
|
208
|
+
return spec.width < 120 ? <span className="wsk-label wsk-space-bar">␣</span> : null
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function isAccentReturn(type: ReturnKeyType): boolean {
|
|
213
|
+
return type === 'search' || type === 'go' || type === 'send' || type === 'done'
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function iosReturnIcon(type: ReturnKeyType) {
|
|
217
|
+
switch (type) {
|
|
218
|
+
case 'search':
|
|
219
|
+
return <SearchIcon />
|
|
220
|
+
case 'go':
|
|
221
|
+
return <ArrowRightIcon />
|
|
222
|
+
case 'send':
|
|
223
|
+
return <ArrowUpIcon />
|
|
224
|
+
case 'done':
|
|
225
|
+
return <CheckmarkIcon />
|
|
226
|
+
case 'default':
|
|
227
|
+
case 'next':
|
|
228
|
+
return <ReturnIcon />
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function androidReturnIcon(config: FieldConfig) {
|
|
233
|
+
switch (config.returnKeyType) {
|
|
234
|
+
case 'search':
|
|
235
|
+
return <MaterialSearch />
|
|
236
|
+
case 'go':
|
|
237
|
+
return <MaterialArrow />
|
|
238
|
+
case 'send':
|
|
239
|
+
return <MaterialSend />
|
|
240
|
+
case 'done':
|
|
241
|
+
return <MaterialCheck />
|
|
242
|
+
case 'next':
|
|
243
|
+
return <MaterialNext />
|
|
244
|
+
case 'default':
|
|
245
|
+
return config.multiline ? <MaterialReturn /> : <MaterialCheck />
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function Strip({ controller, layout, live, config, state }: { controller: KeyboardController; layout: KeyboardLayout; live: Live; config: FieldConfig; state: KeyboardState }) {
|
|
250
|
+
const strip = layout.strip!
|
|
251
|
+
const android = layout.platform === 'android'
|
|
252
|
+
const showSuggestions = state.suggestions && config.suggestions && (!android || currentWord(live.textBefore) !== '')
|
|
253
|
+
if (!showSuggestions) {
|
|
254
|
+
return android ? <Toolbar layout={layout} /> : null
|
|
255
|
+
}
|
|
256
|
+
const suggestions = suggestionsFor(live.textBefore, layout.platform)
|
|
257
|
+
const width = strip.width / 3
|
|
258
|
+
const centerY = strip.y + strip.height / 2 - (android ? 0 : 1)
|
|
259
|
+
return (
|
|
260
|
+
<>
|
|
261
|
+
{[0, 1, 2].map((index) => {
|
|
262
|
+
const suggestion = suggestions[index]
|
|
263
|
+
return (
|
|
264
|
+
<div
|
|
265
|
+
key={index}
|
|
266
|
+
className={android && index === 1 ? 'wsk-suggestion wsk-suggestion-best' : 'wsk-suggestion'}
|
|
267
|
+
style={{ left: strip.x + index * width, width, top: centerY }}
|
|
268
|
+
onPointerDown={(event) => event.preventDefault()}
|
|
269
|
+
onClick={() => suggestion !== undefined && controller.applySuggestion(suggestion)}
|
|
270
|
+
>
|
|
271
|
+
{suggestion?.label}
|
|
272
|
+
</div>
|
|
273
|
+
)
|
|
274
|
+
})}
|
|
275
|
+
{[1, 2].map((index) => (
|
|
276
|
+
<div key={index} className="wsk-suggestion-divider" style={{ left: strip.x + index * width, top: centerY - 11, height: 22 }} />
|
|
277
|
+
))}
|
|
278
|
+
</>
|
|
279
|
+
)
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** Gboard's toolbar, where the suggestions go while there are none. */
|
|
283
|
+
function Toolbar({ layout }: { layout: KeyboardLayout }) {
|
|
284
|
+
const strip = layout.strip!
|
|
285
|
+
const icons = [<MaterialApps />, <MaterialSticker />, <MaterialGif />, <MaterialClipboard />, <MaterialSettings />, <MaterialMic />]
|
|
286
|
+
const cell = strip.width / icons.length
|
|
287
|
+
return (
|
|
288
|
+
<>
|
|
289
|
+
{icons.map((icon, index) => (
|
|
290
|
+
<span key={index} className="wsk-tool" style={{ left: strip.x + index * cell, width: cell, top: strip.y, height: strip.height }}>
|
|
291
|
+
{icon}
|
|
292
|
+
</span>
|
|
293
|
+
))}
|
|
294
|
+
</>
|
|
295
|
+
)
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function IosPopup({ spec, state, layout }: { spec: KeySpec; state: KeyboardState; layout: KeyboardLayout }) {
|
|
299
|
+
const s = layout.scale
|
|
300
|
+
const overhang = POPUP.overhang * s
|
|
301
|
+
const keyLeft = spec.x
|
|
302
|
+
const keyRight = spec.x + spec.width
|
|
303
|
+
const keyTop = spec.y
|
|
304
|
+
const keyBottom = keyTop + spec.height
|
|
305
|
+
const headWidth = spec.width + overhang * 2
|
|
306
|
+
const headLeft = Math.min(Math.max(keyLeft - overhang, POPUP.screenMargin), layout.width - POPUP.screenMargin - headWidth)
|
|
307
|
+
const headRight = headLeft + headWidth
|
|
308
|
+
const headHeight = POPUP.headHeight * s
|
|
309
|
+
const headTop = keyTop - POPUP.headRise * s
|
|
310
|
+
const headBottom = headTop + headHeight
|
|
311
|
+
const r = POPUP.headCornerRadius * s
|
|
312
|
+
const keyRadius = spec.radius
|
|
313
|
+
const neckTop = headBottom - 4 * s
|
|
314
|
+
const neckBottom = keyTop + 12 * s
|
|
315
|
+
const path = [
|
|
316
|
+
`M ${headLeft + r} ${headTop}`,
|
|
317
|
+
`H ${headRight - r}`,
|
|
318
|
+
`Q ${headRight} ${headTop} ${headRight} ${headTop + r}`,
|
|
319
|
+
`V ${neckTop}`,
|
|
320
|
+
`C ${headRight} ${neckTop + 10 * s} ${keyRight} ${neckBottom - 12 * s} ${keyRight} ${neckBottom}`,
|
|
321
|
+
`V ${keyBottom - keyRadius}`,
|
|
322
|
+
`Q ${keyRight} ${keyBottom} ${keyRight - keyRadius} ${keyBottom}`,
|
|
323
|
+
`H ${keyLeft + keyRadius}`,
|
|
324
|
+
`Q ${keyLeft} ${keyBottom} ${keyLeft} ${keyBottom - keyRadius}`,
|
|
325
|
+
`V ${neckBottom}`,
|
|
326
|
+
`C ${keyLeft} ${neckBottom - 12 * s} ${headLeft} ${neckTop + 10 * s} ${headLeft} ${neckTop}`,
|
|
327
|
+
`V ${headTop + r}`,
|
|
328
|
+
`Q ${headLeft} ${headTop} ${headLeft + r} ${headTop}`,
|
|
329
|
+
'Z',
|
|
330
|
+
].join(' ')
|
|
331
|
+
const { text, upper } = letterLabel(spec, state)
|
|
332
|
+
const lower = spec.action.kind === 'letter' && !upper
|
|
333
|
+
return (
|
|
334
|
+
<>
|
|
335
|
+
<svg className="wsk-popup" width={layout.width} height={layout.height} viewBox={`0 0 ${layout.width} ${layout.height}`}>
|
|
336
|
+
<path d={path} />
|
|
337
|
+
</svg>
|
|
338
|
+
<span className={lower ? 'wsk-popup-label wsk-popup-label-lower' : 'wsk-popup-label'} style={{ left: (headLeft + headRight) / 2, top: headTop + headHeight / 2 }}>
|
|
339
|
+
{text}
|
|
340
|
+
</span>
|
|
341
|
+
</>
|
|
342
|
+
)
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** Gboard's key preview: the key's character in a bubble over it. */
|
|
346
|
+
function AndroidPopup({ spec, state }: { spec: KeySpec; state: KeyboardState }) {
|
|
347
|
+
const { text } = letterLabel(spec, state)
|
|
348
|
+
const height = spec.height * 1.15
|
|
349
|
+
return (
|
|
350
|
+
<span className="wsk-preview" style={{ left: spec.x - 2, width: spec.width + 4, top: spec.y - height - 6, height }}>
|
|
351
|
+
{text}
|
|
352
|
+
</span>
|
|
353
|
+
)
|
|
354
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { plugin } from 'web-sim/runtime'
|
|
2
|
+
import { deleteBackward, focusedField, insertText, pressReturn, readFieldConfig, replaceBeforeCaret, textBeforeCaret } from './fields.ts'
|
|
3
|
+
import { type Edit, INITIAL_KEYBOARD, type KeyboardState } from './state.ts'
|
|
4
|
+
import { enableKeyboardTaps } from './taps.ts'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The keyboard's half in the app's window. It tells the shell which field has the focus and what it asks for, and
|
|
8
|
+
* the text before the caret; it makes the edits the drawn keyboard's keys ask for. It keeps nothing itself: the
|
|
9
|
+
* focused field is always the document's.
|
|
10
|
+
*/
|
|
11
|
+
const keyboard = plugin<KeyboardState>('keyboard', INITIAL_KEYBOARD)
|
|
12
|
+
|
|
13
|
+
const announceFocus = () => {
|
|
14
|
+
const field = focusedField(document)
|
|
15
|
+
if (field !== null) {
|
|
16
|
+
keyboard.emit('focus', { config: readFieldConfig(field), textBefore: textBeforeCaret(field) })
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const onFocusIn = (event: FocusEvent) => {
|
|
21
|
+
if (event.target === document.activeElement && focusedField(document) !== null) {
|
|
22
|
+
announceFocus()
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Focus moving from one field to the next passes through the body; only a focus that stays out hides the keyboard.
|
|
27
|
+
const onFocusOut = () => {
|
|
28
|
+
setTimeout(() => {
|
|
29
|
+
if (focusedField(document) === null) {
|
|
30
|
+
keyboard.emit('blur')
|
|
31
|
+
}
|
|
32
|
+
}, 0)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const onTextChange = () => {
|
|
36
|
+
const field = focusedField(document)
|
|
37
|
+
if (field !== null) {
|
|
38
|
+
keyboard.emit('text', { textBefore: textBeforeCaret(field) })
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// The computer's keyboard types into the field as usual; the drawn keyboard lights the key up.
|
|
43
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
44
|
+
if (event.isTrusted && !event.metaKey && !event.ctrlKey && focusedField(document) === event.target) {
|
|
45
|
+
keyboard.emit('physical-key', { key: event.key })
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const edit = (payload: unknown) => {
|
|
50
|
+
const field = focusedField(document)
|
|
51
|
+
if (field === null) {
|
|
52
|
+
return
|
|
53
|
+
}
|
|
54
|
+
const change = payload as Edit
|
|
55
|
+
switch (change.type) {
|
|
56
|
+
case 'insert':
|
|
57
|
+
insertText(field, change.text)
|
|
58
|
+
break
|
|
59
|
+
case 'delete':
|
|
60
|
+
deleteBackward(field)
|
|
61
|
+
break
|
|
62
|
+
case 'replace':
|
|
63
|
+
replaceBeforeCaret(field, change.length, change.text)
|
|
64
|
+
break
|
|
65
|
+
case 'return':
|
|
66
|
+
pressReturn(field)
|
|
67
|
+
break
|
|
68
|
+
}
|
|
69
|
+
onTextChange()
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const dismiss = () => focusedField(document)?.blur()
|
|
73
|
+
|
|
74
|
+
document.addEventListener('focusin', onFocusIn)
|
|
75
|
+
document.addEventListener('focusout', onFocusOut)
|
|
76
|
+
document.addEventListener('input', onTextChange)
|
|
77
|
+
document.addEventListener('selectionchange', onTextChange)
|
|
78
|
+
document.addEventListener('keydown', onKeyDown, true)
|
|
79
|
+
const stopTaps = enableKeyboardTaps(document)
|
|
80
|
+
|
|
81
|
+
// The shell keeps these listeners, so they go when this document does: a reload, or a device change.
|
|
82
|
+
const removals = [keyboard.on('edit', edit), keyboard.on('dismiss', dismiss), keyboard.on('sync', announceFocus)]
|
|
83
|
+
window.addEventListener('pagehide', () => {
|
|
84
|
+
removals.forEach((remove) => remove())
|
|
85
|
+
stopTaps()
|
|
86
|
+
keyboard.emit('blur')
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
// A new app run starts with no field focused, whatever the last run left.
|
|
90
|
+
keyboard.emit('blur')
|