@a16n/web-sim 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.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 +157 -0
- package/overrides/react-native-web/useElementLayout.ts +152 -0
- package/package.json +80 -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 +129 -0
- package/src/client/app/appFocus.ts +97 -0
- package/src/client/app/console.ts +118 -0
- package/src/client/app/fonts.ts +76 -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 +23 -0
- package/src/devices/catalog.ts +223 -0
- package/src/devices/types.ts +83 -0
- package/src/node/appInfo.ts +188 -0
- package/src/node/appSource.ts +140 -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/fonts.ts +377 -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 +169 -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 +254 -0
- package/src/node/worklets.ts +86 -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 +519 -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,562 @@
|
|
|
1
|
+
import { CaretRightIcon, CheckIcon, CopyIcon } from '@phosphor-icons/react'
|
|
2
|
+
import { type ComponentType, type ReactNode, useEffect, useRef, useState } from 'react'
|
|
3
|
+
import type { LogEntry } from '../runtime/index.ts'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Controls for plugin panels, so every panel looks like part of the shell.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export function Field({ label, value, children }: { label: string; value?: ReactNode; children?: ReactNode }) {
|
|
10
|
+
return (
|
|
11
|
+
<div className="wsp-field">
|
|
12
|
+
<div className="wsp-field-head">
|
|
13
|
+
<span className="wsp-label">{label}</span>
|
|
14
|
+
{value !== undefined && <span className="wsp-value">{value}</span>}
|
|
15
|
+
</div>
|
|
16
|
+
{children}
|
|
17
|
+
</div>
|
|
18
|
+
)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function Slider({
|
|
22
|
+
value,
|
|
23
|
+
min = 0,
|
|
24
|
+
max = 1,
|
|
25
|
+
step = 0.01,
|
|
26
|
+
onChange,
|
|
27
|
+
label,
|
|
28
|
+
}: {
|
|
29
|
+
value: number
|
|
30
|
+
min?: number
|
|
31
|
+
max?: number
|
|
32
|
+
step?: number
|
|
33
|
+
onChange: (value: number) => void
|
|
34
|
+
label: string
|
|
35
|
+
}) {
|
|
36
|
+
return (
|
|
37
|
+
<input
|
|
38
|
+
className="wsp-slider"
|
|
39
|
+
type="range"
|
|
40
|
+
min={min}
|
|
41
|
+
max={max}
|
|
42
|
+
step={step}
|
|
43
|
+
value={value}
|
|
44
|
+
aria-label={label}
|
|
45
|
+
data-tooltip={label}
|
|
46
|
+
onChange={(event) => onChange(Number(event.target.value))}
|
|
47
|
+
/>
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function Toggle({ label, checked, onChange }: { label: string; checked: boolean; onChange: (checked: boolean) => void }) {
|
|
52
|
+
return (
|
|
53
|
+
<label className="wsp-toggle">
|
|
54
|
+
<span className="wsp-label">{label}</span>
|
|
55
|
+
<input type="checkbox" role="switch" checked={checked} onChange={(event) => onChange(event.target.checked)} />
|
|
56
|
+
<span className="wsp-switch" aria-hidden />
|
|
57
|
+
</label>
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function Segmented<T extends string>({
|
|
62
|
+
label,
|
|
63
|
+
value,
|
|
64
|
+
options,
|
|
65
|
+
onChange,
|
|
66
|
+
}: {
|
|
67
|
+
label: string
|
|
68
|
+
value: T
|
|
69
|
+
options: { value: T; label: string }[]
|
|
70
|
+
onChange: (value: T) => void
|
|
71
|
+
}) {
|
|
72
|
+
return (
|
|
73
|
+
<div className="wsp-segmented" role="radiogroup" aria-label={label}>
|
|
74
|
+
{options.map((option) => (
|
|
75
|
+
<button
|
|
76
|
+
key={option.value}
|
|
77
|
+
type="button"
|
|
78
|
+
role="radio"
|
|
79
|
+
aria-checked={option.value === value}
|
|
80
|
+
className={option.value === value ? 'wsp-segment wsp-segment-on' : 'wsp-segment'}
|
|
81
|
+
onClick={() => onChange(option.value)}
|
|
82
|
+
>
|
|
83
|
+
{option.label}
|
|
84
|
+
</button>
|
|
85
|
+
))}
|
|
86
|
+
</div>
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function Select<T extends string>({
|
|
91
|
+
label,
|
|
92
|
+
value,
|
|
93
|
+
options,
|
|
94
|
+
onChange,
|
|
95
|
+
}: {
|
|
96
|
+
label: string
|
|
97
|
+
value: T
|
|
98
|
+
options: { value: T; label: string }[]
|
|
99
|
+
onChange: (value: T) => void
|
|
100
|
+
}) {
|
|
101
|
+
return (
|
|
102
|
+
<select className="wsp-select" value={value} aria-label={label} onChange={(event) => onChange(event.target.value as T)}>
|
|
103
|
+
{options.map((option) => (
|
|
104
|
+
<option key={option.value} value={option.value}>
|
|
105
|
+
{option.label}
|
|
106
|
+
</option>
|
|
107
|
+
))}
|
|
108
|
+
</select>
|
|
109
|
+
)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function TextField({
|
|
113
|
+
label,
|
|
114
|
+
value,
|
|
115
|
+
onChange,
|
|
116
|
+
multiline = false,
|
|
117
|
+
placeholder,
|
|
118
|
+
type = 'text',
|
|
119
|
+
}: {
|
|
120
|
+
label: string
|
|
121
|
+
value: string
|
|
122
|
+
onChange: (value: string) => void
|
|
123
|
+
multiline?: boolean
|
|
124
|
+
placeholder?: string
|
|
125
|
+
/** An input type for the value: `datetime-local`, `date`, `email`, `tel`, `number`. */
|
|
126
|
+
type?: 'text' | 'datetime-local' | 'date' | 'time' | 'email' | 'tel' | 'number' | 'url'
|
|
127
|
+
}) {
|
|
128
|
+
return multiline ? (
|
|
129
|
+
<textarea className="wsp-input wsp-textarea" aria-label={label} value={value} placeholder={placeholder} onChange={(event) => onChange(event.target.value)} />
|
|
130
|
+
) : (
|
|
131
|
+
<input className="wsp-input" type={type} aria-label={label} value={value} placeholder={placeholder} onChange={(event) => onChange(event.target.value)} />
|
|
132
|
+
)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function Button({
|
|
136
|
+
children,
|
|
137
|
+
onClick,
|
|
138
|
+
title,
|
|
139
|
+
pressed,
|
|
140
|
+
disabled = false,
|
|
141
|
+
}: {
|
|
142
|
+
children: ReactNode
|
|
143
|
+
onClick: () => void
|
|
144
|
+
title?: string
|
|
145
|
+
pressed?: boolean
|
|
146
|
+
disabled?: boolean
|
|
147
|
+
}) {
|
|
148
|
+
return (
|
|
149
|
+
<button
|
|
150
|
+
type="button"
|
|
151
|
+
className={pressed === true ? 'wsp-button wsp-button-on' : 'wsp-button'}
|
|
152
|
+
onClick={onClick}
|
|
153
|
+
data-tooltip={title}
|
|
154
|
+
aria-pressed={pressed}
|
|
155
|
+
disabled={disabled}
|
|
156
|
+
>
|
|
157
|
+
{children}
|
|
158
|
+
</button>
|
|
159
|
+
)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function Row({ children }: { children: ReactNode }) {
|
|
163
|
+
return <div className="wsp-row">{children}</div>
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* A number typed in. It commits on Enter or when it loses focus, so a half-typed value ("-", "37.") never reaches the
|
|
168
|
+
* state; anything that doesn't parse goes back to the current value.
|
|
169
|
+
*/
|
|
170
|
+
export function NumberField({
|
|
171
|
+
label,
|
|
172
|
+
value,
|
|
173
|
+
onChange,
|
|
174
|
+
unit,
|
|
175
|
+
digits,
|
|
176
|
+
min,
|
|
177
|
+
max,
|
|
178
|
+
step = 1,
|
|
179
|
+
}: {
|
|
180
|
+
label: string
|
|
181
|
+
value: number
|
|
182
|
+
onChange: (value: number) => void
|
|
183
|
+
min?: number
|
|
184
|
+
max?: number
|
|
185
|
+
/** What the arrow keys add or take away. */
|
|
186
|
+
step?: number
|
|
187
|
+
/** Shown after the field. */
|
|
188
|
+
unit?: string
|
|
189
|
+
/** Decimal places shown. */
|
|
190
|
+
digits?: number
|
|
191
|
+
}) {
|
|
192
|
+
const shown = digits === undefined ? String(value) : value.toFixed(digits)
|
|
193
|
+
const [text, setText] = useState(shown)
|
|
194
|
+
const [editing, setEditing] = useState(false)
|
|
195
|
+
useEffect(() => {
|
|
196
|
+
if (!editing) {
|
|
197
|
+
setText(shown)
|
|
198
|
+
}
|
|
199
|
+
}, [shown, editing])
|
|
200
|
+
const clamp = (number: number) => Math.min(max ?? Infinity, Math.max(min ?? -Infinity, number))
|
|
201
|
+
const commit = () => {
|
|
202
|
+
setEditing(false)
|
|
203
|
+
const parsed = clamp(Number(text))
|
|
204
|
+
if (text.trim() !== '' && Number.isFinite(parsed) && parsed !== value) {
|
|
205
|
+
onChange(parsed)
|
|
206
|
+
} else {
|
|
207
|
+
setText(shown)
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return (
|
|
211
|
+
<label className="wsp-number">
|
|
212
|
+
<span className="wsp-label">{label}</span>
|
|
213
|
+
<input
|
|
214
|
+
className="wsp-input"
|
|
215
|
+
inputMode="decimal"
|
|
216
|
+
aria-label={label}
|
|
217
|
+
value={text}
|
|
218
|
+
onFocus={() => setEditing(true)}
|
|
219
|
+
onChange={(event) => setText(event.target.value)}
|
|
220
|
+
onBlur={commit}
|
|
221
|
+
onKeyDown={(event) => {
|
|
222
|
+
if (event.key === 'Enter') {
|
|
223
|
+
event.currentTarget.blur()
|
|
224
|
+
} else if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
|
|
225
|
+
event.preventDefault()
|
|
226
|
+
const next = clamp(value + (event.key === 'ArrowUp' ? step : -step))
|
|
227
|
+
setText(String(next))
|
|
228
|
+
onChange(next)
|
|
229
|
+
}
|
|
230
|
+
}}
|
|
231
|
+
/>
|
|
232
|
+
{unit !== undefined && <span className="wsp-unit">{unit}</span>}
|
|
233
|
+
</label>
|
|
234
|
+
)
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** A bar filled from 0 to 1. */
|
|
238
|
+
export function Progress({ label, value }: { label: string; value: number }) {
|
|
239
|
+
return (
|
|
240
|
+
<div className="wsp-progress" role="progressbar" aria-label={label} aria-valuemin={0} aria-valuemax={1} aria-valuenow={value}>
|
|
241
|
+
<div className="wsp-progress-fill" style={{ width: `${Math.max(0, Math.min(1, value)) * 100}%` }} />
|
|
242
|
+
</div>
|
|
243
|
+
)
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** A small button with only an icon; `title` is its tooltip and its accessible name. */
|
|
247
|
+
export function IconButton({
|
|
248
|
+
icon: Icon,
|
|
249
|
+
title,
|
|
250
|
+
onClick,
|
|
251
|
+
disabled = false,
|
|
252
|
+
}: {
|
|
253
|
+
icon: ComponentType<{ size?: number }>
|
|
254
|
+
title: string
|
|
255
|
+
onClick: () => void
|
|
256
|
+
disabled?: boolean
|
|
257
|
+
}) {
|
|
258
|
+
return (
|
|
259
|
+
<button type="button" className="wsp-icon-button" data-tooltip={title} aria-label={title} disabled={disabled} onClick={onClick}>
|
|
260
|
+
<Icon size={14} />
|
|
261
|
+
</button>
|
|
262
|
+
)
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** A button that opens the file chooser. */
|
|
266
|
+
export function FileButton({
|
|
267
|
+
children,
|
|
268
|
+
accept,
|
|
269
|
+
multiple = false,
|
|
270
|
+
onFiles,
|
|
271
|
+
title,
|
|
272
|
+
}: {
|
|
273
|
+
children: ReactNode
|
|
274
|
+
accept?: string
|
|
275
|
+
multiple?: boolean
|
|
276
|
+
onFiles: (files: File[]) => void
|
|
277
|
+
title?: string
|
|
278
|
+
}) {
|
|
279
|
+
const input = useRef<HTMLInputElement>(null)
|
|
280
|
+
return (
|
|
281
|
+
<>
|
|
282
|
+
<button type="button" className="wsp-button" data-tooltip={title} onClick={() => input.current?.click()}>
|
|
283
|
+
{children}
|
|
284
|
+
</button>
|
|
285
|
+
<input
|
|
286
|
+
ref={input}
|
|
287
|
+
type="file"
|
|
288
|
+
hidden
|
|
289
|
+
accept={accept}
|
|
290
|
+
multiple={multiple}
|
|
291
|
+
onChange={(event) => {
|
|
292
|
+
const files = [...(event.target.files ?? [])]
|
|
293
|
+
event.target.value = ''
|
|
294
|
+
if (files.length > 0) {
|
|
295
|
+
onFiles(files)
|
|
296
|
+
}
|
|
297
|
+
}}
|
|
298
|
+
/>
|
|
299
|
+
</>
|
|
300
|
+
)
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** A file's contents as a data URL. */
|
|
304
|
+
export function readDataUrl(file: Blob): Promise<string> {
|
|
305
|
+
return new Promise((resolve, reject) => {
|
|
306
|
+
const reader = new FileReader()
|
|
307
|
+
reader.onload = () => resolve(String(reader.result))
|
|
308
|
+
reader.onerror = () => reject(reader.error)
|
|
309
|
+
reader.readAsDataURL(file)
|
|
310
|
+
})
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
export type Thumbnail = { id: string; src: string | null; label: string; badge?: string }
|
|
314
|
+
|
|
315
|
+
/** A grid of square images, each with an optional corner badge and a remove button. */
|
|
316
|
+
export function Thumbnails({
|
|
317
|
+
items,
|
|
318
|
+
onRemove,
|
|
319
|
+
removeLabel = 'Remove',
|
|
320
|
+
empty = 'None',
|
|
321
|
+
}: {
|
|
322
|
+
items: Thumbnail[]
|
|
323
|
+
onRemove?: (id: string) => void
|
|
324
|
+
removeLabel?: string
|
|
325
|
+
empty?: string
|
|
326
|
+
}) {
|
|
327
|
+
if (items.length === 0) {
|
|
328
|
+
return <div className="wsp-empty">{empty}</div>
|
|
329
|
+
}
|
|
330
|
+
return (
|
|
331
|
+
<div className="wsp-thumbnails">
|
|
332
|
+
{items.map((item) => (
|
|
333
|
+
<div key={item.id} className="wsp-thumbnail" data-tooltip={item.label}>
|
|
334
|
+
{item.src !== null && <img src={item.src} alt={item.label} draggable={false} />}
|
|
335
|
+
{item.badge !== undefined && <span className="wsp-thumbnail-badge">{item.badge}</span>}
|
|
336
|
+
{onRemove !== undefined && (
|
|
337
|
+
<button type="button" className="wsp-thumbnail-remove" data-tooltip={`${removeLabel} ${item.label}`} aria-label={`${removeLabel} ${item.label}`} onClick={() => onRemove(item.id)}>
|
|
338
|
+
×
|
|
339
|
+
</button>
|
|
340
|
+
)}
|
|
341
|
+
</div>
|
|
342
|
+
))}
|
|
343
|
+
</div>
|
|
344
|
+
)
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/** Live values, label beside value, in a compact grid of one or two columns. */
|
|
348
|
+
export function Readout({ rows, columns = 1 }: { rows: [label: string, value: ReactNode][]; columns?: 1 | 2 }) {
|
|
349
|
+
return (
|
|
350
|
+
<dl className={columns === 2 ? 'wsp-readout wsp-readout-2' : 'wsp-readout'}>
|
|
351
|
+
{rows.map(([label, value]) => (
|
|
352
|
+
<div key={label} className="wsp-readout-row">
|
|
353
|
+
<dt className="wsp-label">{label}</dt>
|
|
354
|
+
<dd>{value}</dd>
|
|
355
|
+
</div>
|
|
356
|
+
))}
|
|
357
|
+
</dl>
|
|
358
|
+
)
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* A row in a list: a title and a detail line, actions at the end, and what `children` holds (an editor) below it.
|
|
363
|
+
* Pressing the row calls `onPress`.
|
|
364
|
+
*/
|
|
365
|
+
/**
|
|
366
|
+
* A row in a list: a title and a detail line, actions at the end, and what `children` holds (an editor) below it.
|
|
367
|
+
* Pressing the row calls `onPress`. `lead` goes before the title (a chevron, an icon), `indent` nests it (trees), and
|
|
368
|
+
* `mono` sets the title in monospace (keys, paths, tokens).
|
|
369
|
+
*/
|
|
370
|
+
export function ListItem({
|
|
371
|
+
title,
|
|
372
|
+
detail,
|
|
373
|
+
badge,
|
|
374
|
+
actions,
|
|
375
|
+
onPress,
|
|
376
|
+
onClick,
|
|
377
|
+
selected = false,
|
|
378
|
+
mono = false,
|
|
379
|
+
indent = 0,
|
|
380
|
+
lead,
|
|
381
|
+
children,
|
|
382
|
+
}: {
|
|
383
|
+
title: ReactNode
|
|
384
|
+
detail?: ReactNode
|
|
385
|
+
badge?: string
|
|
386
|
+
actions?: ReactNode
|
|
387
|
+
onPress?: () => void
|
|
388
|
+
/** The same as `onPress`. */
|
|
389
|
+
onClick?: () => void
|
|
390
|
+
selected?: boolean
|
|
391
|
+
mono?: boolean
|
|
392
|
+
indent?: number
|
|
393
|
+
lead?: ReactNode
|
|
394
|
+
children?: ReactNode
|
|
395
|
+
}) {
|
|
396
|
+
const press = onPress ?? onClick
|
|
397
|
+
const item = useRef<HTMLDivElement>(null)
|
|
398
|
+
const open = children !== undefined && children !== false && children !== null
|
|
399
|
+
// An item opened in a scrolling list scrolls into view, editor and all.
|
|
400
|
+
useEffect(() => {
|
|
401
|
+
if (open) {
|
|
402
|
+
item.current?.scrollIntoView({ block: 'nearest' })
|
|
403
|
+
}
|
|
404
|
+
}, [open])
|
|
405
|
+
return (
|
|
406
|
+
<div ref={item} className={selected ? 'wsp-item wsp-item-selected' : 'wsp-item'}>
|
|
407
|
+
<div className="wsp-item-row">
|
|
408
|
+
<button
|
|
409
|
+
type="button"
|
|
410
|
+
className="wsp-item-main"
|
|
411
|
+
onClick={press}
|
|
412
|
+
disabled={press === undefined}
|
|
413
|
+
aria-expanded={press === undefined || !open ? undefined : true}
|
|
414
|
+
style={indent > 0 ? { paddingLeft: 6 + indent * 14 } : undefined}
|
|
415
|
+
>
|
|
416
|
+
{lead !== undefined && <span className="wsp-item-lead">{lead}</span>}
|
|
417
|
+
<span className="wsp-item-text">
|
|
418
|
+
<span className={mono ? 'wsp-item-title wsp-mono' : 'wsp-item-title'}>
|
|
419
|
+
{title}
|
|
420
|
+
{badge !== undefined && <span className="wsp-badge">{badge}</span>}
|
|
421
|
+
</span>
|
|
422
|
+
{detail !== undefined && detail !== null && detail !== '' && <span className="wsp-item-detail">{detail}</span>}
|
|
423
|
+
</span>
|
|
424
|
+
</button>
|
|
425
|
+
{actions !== undefined && <span className="wsp-item-actions">{actions}</span>}
|
|
426
|
+
</div>
|
|
427
|
+
{open && <div className="wsp-item-body">{children}</div>}
|
|
428
|
+
</div>
|
|
429
|
+
)
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
export function List({ children, empty = 'None' }: { children?: ReactNode; empty?: string }) {
|
|
433
|
+
const hasItems = Array.isArray(children) ? children.flat().some((child) => child !== null && child !== false && child !== undefined) : children !== null && children !== false && children !== undefined
|
|
434
|
+
return <div className="wsp-list">{hasItems ? children : <div className="wsp-empty">{empty}</div>}</div>
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/** What happened, newest first, with the time of each. */
|
|
438
|
+
export function Log({ entries, empty = 'None' }: { entries: LogEntry[]; empty?: string }) {
|
|
439
|
+
if (entries.length === 0) {
|
|
440
|
+
return <div className="wsp-empty">{empty}</div>
|
|
441
|
+
}
|
|
442
|
+
return (
|
|
443
|
+
<ol className="wsp-log">
|
|
444
|
+
{[...entries].reverse().map((entry, index) => (
|
|
445
|
+
<li key={`${entry.time}-${index}`}>
|
|
446
|
+
<time>{new Date(entry.time).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', second: '2-digit' })}</time>
|
|
447
|
+
<span>{entry.text}</span>
|
|
448
|
+
</li>
|
|
449
|
+
))}
|
|
450
|
+
</ol>
|
|
451
|
+
)
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/** A titled group inside a panel, with optional controls on the right of its title. */
|
|
455
|
+
export function Section({ title, actions, children }: { title: string; actions?: ReactNode; children?: ReactNode }) {
|
|
456
|
+
return (
|
|
457
|
+
<div className="wsp-section">
|
|
458
|
+
<div className="wsp-section-head">
|
|
459
|
+
<span className="wsp-section-title">{title}</span>
|
|
460
|
+
{actions !== undefined && <span className="wsp-actions">{actions}</span>}
|
|
461
|
+
</div>
|
|
462
|
+
{children}
|
|
463
|
+
</div>
|
|
464
|
+
)
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
export type TreeNode = {
|
|
468
|
+
id: string
|
|
469
|
+
label: string
|
|
470
|
+
detail?: ReactNode
|
|
471
|
+
/** Present on folders, even when empty. */
|
|
472
|
+
children?: TreeNode[]
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* A tree of folders and leaves, folders expandable. `actions` draws a row's buttons; `onSelect` hears a press on a
|
|
477
|
+
* row.
|
|
478
|
+
*/
|
|
479
|
+
export function Tree({
|
|
480
|
+
nodes,
|
|
481
|
+
selected,
|
|
482
|
+
onSelect,
|
|
483
|
+
actions,
|
|
484
|
+
defaultExpanded = [],
|
|
485
|
+
empty,
|
|
486
|
+
}: {
|
|
487
|
+
nodes: TreeNode[]
|
|
488
|
+
selected?: string | null
|
|
489
|
+
onSelect?: (node: TreeNode) => void
|
|
490
|
+
actions?: (node: TreeNode) => ReactNode
|
|
491
|
+
defaultExpanded?: string[]
|
|
492
|
+
empty?: string
|
|
493
|
+
}) {
|
|
494
|
+
const [expanded, setExpanded] = useState<string[]>(defaultExpanded)
|
|
495
|
+
const toggle = (id: string) => setExpanded((current) => (current.includes(id) ? current.filter((other) => other !== id) : [...current, id]))
|
|
496
|
+
const rows: ReactNode[] = []
|
|
497
|
+
const walk = (list: TreeNode[], depth: number) => {
|
|
498
|
+
for (const node of list) {
|
|
499
|
+
const folder = node.children !== undefined
|
|
500
|
+
const open = folder && expanded.includes(node.id)
|
|
501
|
+
rows.push(
|
|
502
|
+
<ListItem
|
|
503
|
+
key={node.id}
|
|
504
|
+
mono
|
|
505
|
+
indent={depth}
|
|
506
|
+
title={node.label}
|
|
507
|
+
detail={node.detail}
|
|
508
|
+
selected={selected === node.id}
|
|
509
|
+
lead={
|
|
510
|
+
folder ? (
|
|
511
|
+
<CaretRightIcon size={12} className={open ? 'wsp-tree-chevron wsp-tree-chevron-open' : 'wsp-tree-chevron'} />
|
|
512
|
+
) : (
|
|
513
|
+
<span className="wsp-tree-leaf" />
|
|
514
|
+
)
|
|
515
|
+
}
|
|
516
|
+
onClick={() => {
|
|
517
|
+
if (folder) {
|
|
518
|
+
toggle(node.id)
|
|
519
|
+
}
|
|
520
|
+
onSelect?.(node)
|
|
521
|
+
}}
|
|
522
|
+
actions={actions?.(node)}
|
|
523
|
+
/>,
|
|
524
|
+
)
|
|
525
|
+
if (open) {
|
|
526
|
+
walk(node.children!, depth + 1)
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
walk(nodes, 0)
|
|
531
|
+
return <List empty={empty}>{rows}</List>
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/** A value shown in full, monospaced, with a button that copies it to the computer's clipboard. */
|
|
535
|
+
export function Copyable({ label, value }: { label: string; value: string }) {
|
|
536
|
+
const [copied, setCopied] = useState(false)
|
|
537
|
+
return (
|
|
538
|
+
<div className="wsp-copyable">
|
|
539
|
+
<span className="wsp-mono wsp-copyable-value">{value}</span>
|
|
540
|
+
<IconButton
|
|
541
|
+
icon={copied ? CheckIcon : CopyIcon}
|
|
542
|
+
title={copied ? 'Copied' : `Copy ${label}`}
|
|
543
|
+
onClick={() => {
|
|
544
|
+
void navigator.clipboard.writeText(value).then(() => {
|
|
545
|
+
setCopied(true)
|
|
546
|
+
setTimeout(() => setCopied(false), 1200)
|
|
547
|
+
})
|
|
548
|
+
}}
|
|
549
|
+
/>
|
|
550
|
+
</div>
|
|
551
|
+
)
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
/** Text shown as it is, monospaced and scrollable: a file's contents, a payload. */
|
|
555
|
+
export function CodeBlock({ children }: { children: string }) {
|
|
556
|
+
return <pre className="wsp-code">{children}</pre>
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/** An image, fitted to the panel's width. */
|
|
560
|
+
export function ImagePreview({ src, label }: { src: string; label: string }) {
|
|
561
|
+
return <img className="wsp-image" src={src} alt={label} />
|
|
562
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { getHost } from './index.ts'
|
|
2
|
+
import { toWallTime } from './timezone.ts'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The phone's clock, which the clock plugin sets: the time the app's `Date` reads, the status bar shows and
|
|
6
|
+
* notifications fire at. It runs from an anchor: at the real time `anchorReal` the phone read `anchorPhone`, and since
|
|
7
|
+
* then it has run at `rate` (or stood still, when frozen). The default anchors are 0 at rate 1: the real time.
|
|
8
|
+
*/
|
|
9
|
+
export type ClockState = {
|
|
10
|
+
frozen: boolean
|
|
11
|
+
/** A real time, in ms since the epoch. */
|
|
12
|
+
anchorReal: number
|
|
13
|
+
/** What the phone read at `anchorReal`. */
|
|
14
|
+
anchorPhone: number
|
|
15
|
+
/** Phone milliseconds per real millisecond. */
|
|
16
|
+
rate: number
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const REAL_CLOCK: ClockState = { frozen: false, anchorReal: 0, anchorPhone: 0, rate: 1 }
|
|
20
|
+
|
|
21
|
+
declare global {
|
|
22
|
+
interface Window {
|
|
23
|
+
/** The real `Date.now`, kept by the clock plugin before it makes the app's `Date.now` read the phone's clock. */
|
|
24
|
+
__webSimRealNow?: () => number
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The real time, never the phone's: for timing that runs on real time, and for times the shell compares. */
|
|
29
|
+
export function realNow(): number {
|
|
30
|
+
return typeof window !== 'undefined' && window.__webSimRealNow !== undefined ? window.__webSimRealNow() : Date.now()
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The clock plugin's state, or the real clock when it isn't loaded. */
|
|
34
|
+
export function clockState(): ClockState {
|
|
35
|
+
const state = getHost().plugins.get('clock') as Partial<ClockState> | undefined
|
|
36
|
+
return state === undefined ? REAL_CLOCK : { ...REAL_CLOCK, ...state }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** What a clock reads at a real time. */
|
|
40
|
+
export function clockTime(state: ClockState, real: number = realNow()): number {
|
|
41
|
+
const rate = Number.isFinite(state.rate) && state.rate > 0 ? state.rate : 1
|
|
42
|
+
return state.frozen ? state.anchorPhone : state.anchorPhone + (real - state.anchorReal) * rate
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The phone's time now, in ms since the epoch. */
|
|
46
|
+
export function phoneNow(): number {
|
|
47
|
+
return Math.floor(clockTime(clockState()))
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** How many real milliseconds until the phone's clock reads `time`: 0 if it has, null if it never will (frozen). */
|
|
51
|
+
export function realDelayUntil(time: number, state: ClockState = clockState()): number | null {
|
|
52
|
+
const left = time - clockTime(state)
|
|
53
|
+
if (left <= 0) {
|
|
54
|
+
return 0
|
|
55
|
+
}
|
|
56
|
+
return state.frozen ? null : left / (Number.isFinite(state.rate) && state.rate > 0 ? state.rate : 1)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const browserZone = () => new Intl.DateTimeFormat().resolvedOptions().timeZone
|
|
60
|
+
|
|
61
|
+
/** The phone's time zone, from the locale plugin when it is loaded; else the computer's. */
|
|
62
|
+
export function phoneTimeZone(): string {
|
|
63
|
+
const zone = getHost().plugins.get('locale')?.timeZone
|
|
64
|
+
if (typeof zone === 'string') {
|
|
65
|
+
try {
|
|
66
|
+
new Intl.DateTimeFormat('en-US', { timeZone: zone })
|
|
67
|
+
return zone
|
|
68
|
+
} catch {
|
|
69
|
+
// An unknown zone: the app's Date falls back to UTC for it too.
|
|
70
|
+
return 'UTC'
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return browserZone()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The phone's first language, from the locale plugin when it is loaded. */
|
|
77
|
+
export function phoneLanguage(): string {
|
|
78
|
+
const languages = getHost().plugins.get('locale')?.languages
|
|
79
|
+
return Array.isArray(languages) && typeof languages[0] === 'string' ? languages[0] : navigator.language
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Whether the phone shows a 24-hour clock: the locale plugin's setting, or its region's convention. */
|
|
83
|
+
export function phoneUses24HourClock(): boolean {
|
|
84
|
+
const locale = getHost().plugins.get('locale')
|
|
85
|
+
if (locale?.clock === '24' || locale?.clock === '12') {
|
|
86
|
+
return locale.clock === '24'
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
const language = new Intl.Locale(phoneLanguage()).language
|
|
90
|
+
const tag = typeof locale?.region === 'string' ? `${language}-${locale.region}` : phoneLanguage()
|
|
91
|
+
const hourCycle = new Intl.DateTimeFormat(tag, { hour: 'numeric', timeZone: 'UTC' }).resolvedOptions().hourCycle
|
|
92
|
+
return hourCycle === 'h23' || hourCycle === 'h24'
|
|
93
|
+
} catch {
|
|
94
|
+
return false
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* The time as the status bar shows it, without AM/PM as both iOS and Android show it: `9:41` on a 12-hour clock,
|
|
100
|
+
* `09:41` and `21:41` on a 24-hour one.
|
|
101
|
+
*/
|
|
102
|
+
export function statusBarTime(time: number, timeZone: string, hour24: boolean): string {
|
|
103
|
+
const wall = new Date(toWallTime(timeZone, time))
|
|
104
|
+
const hours = wall.getUTCHours()
|
|
105
|
+
const minutes = String(wall.getUTCMinutes()).padStart(2, '0')
|
|
106
|
+
return hour24 ? `${String(hours).padStart(2, '0')}:${minutes}` : `${hours % 12 === 0 ? 12 : hours % 12}:${minutes}`
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** An instant's wall-clock time in a zone as a `datetime-local` value: `2026-12-25T08:00`. */
|
|
110
|
+
export function wallTimeInput(time: number, timeZone: string): string {
|
|
111
|
+
return new Date(toWallTime(timeZone, time)).toISOString().slice(0, 16)
|
|
112
|
+
}
|