@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,492 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AppInfo,
|
|
3
|
+
Dialog,
|
|
4
|
+
DialogResult,
|
|
5
|
+
HapticCall,
|
|
6
|
+
HapticKind,
|
|
7
|
+
PermissionKind,
|
|
8
|
+
PermissionStatus,
|
|
9
|
+
SimHost,
|
|
10
|
+
SimNotification,
|
|
11
|
+
SimState,
|
|
12
|
+
StatusBarStyle,
|
|
13
|
+
StatusBarUpdate,
|
|
14
|
+
} from '../../runtime/index.ts'
|
|
15
|
+
import type { Appearance, Device, Orientation } from '../../devices/types.ts'
|
|
16
|
+
import type { ShellPlugin } from '../../plugin/types.ts'
|
|
17
|
+
import type { EventInput } from '../../node/events/shared.ts'
|
|
18
|
+
import { APP, brief, EventLog, type Origin, SYSTEM, USER } from './events.ts'
|
|
19
|
+
import { PluginStore } from './plugins.ts'
|
|
20
|
+
|
|
21
|
+
const STORAGE_KEY = 'web-sim:state'
|
|
22
|
+
/** The plugin that keeps the permissions, when it is loaded. */
|
|
23
|
+
const PERMISSIONS = 'permissions'
|
|
24
|
+
|
|
25
|
+
type Size = { width: number; height: number }
|
|
26
|
+
|
|
27
|
+
type Stored = {
|
|
28
|
+
deviceId: string
|
|
29
|
+
orientation: Orientation
|
|
30
|
+
appearance: Appearance
|
|
31
|
+
/** Resizable devices' sizes, by device id, in portrait points. */
|
|
32
|
+
sizes?: Record<string, Size>
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** What the app last asked of the status bar; the focused screen's `screenStyle`, when set, wins over `style`. */
|
|
36
|
+
export type StatusBarState = { style: StatusBarStyle; hidden: boolean; screenStyle: StatusBarStyle | null }
|
|
37
|
+
|
|
38
|
+
/** The call a haptic of this kind most likely came from, for callers that don't say. */
|
|
39
|
+
const hapticCall = (kind: HapticKind): HapticCall => ({
|
|
40
|
+
method: kind === 'selection' ? 'selection' : kind === 'success' || kind === 'warning' || kind === 'error' ? 'notification' : 'impact',
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
export type QueuedDialog = { id: number; dialog: Dialog; span: string; resolve: (result: DialogResult) => void }
|
|
44
|
+
|
|
45
|
+
export type QueuedNotification = SimNotification & { key: number; span: string; resolve: (tapped: boolean) => void }
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The system UI the shell draws over the app. A new object on every change, so React sees it change.
|
|
49
|
+
*/
|
|
50
|
+
export type SystemUi = {
|
|
51
|
+
dialogs: QueuedDialog[]
|
|
52
|
+
splash: { visible: boolean; fadeMs: number }
|
|
53
|
+
notifications: QueuedNotification[]
|
|
54
|
+
permissions: Partial<Record<PermissionKind, PermissionStatus>>
|
|
55
|
+
orientationLock: Orientation | null
|
|
56
|
+
navigationBar: { style: StatusBarStyle; hidden: boolean }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The simulator's state, kept by the shell and read by the app through `window.__webSimHost`. The device,
|
|
61
|
+
* orientation and appearance survive reloads; the status bar is whatever the running app last asked for.
|
|
62
|
+
*/
|
|
63
|
+
export class HostStore implements SimHost {
|
|
64
|
+
private listeners = new Set<() => void>()
|
|
65
|
+
private state: SimState
|
|
66
|
+
private statusBar: StatusBarState = { style: 'default', hidden: false, screenStyle: null }
|
|
67
|
+
private ui: SystemUi
|
|
68
|
+
private nextId = 1
|
|
69
|
+
private sizes: Record<string, Size>
|
|
70
|
+
private splashPrevented = false
|
|
71
|
+
/** What the app last passed to `Appearance.setColorScheme` this run; null follows its config. */
|
|
72
|
+
private appColorScheme: Appearance | null = null
|
|
73
|
+
|
|
74
|
+
readonly devices: Device[]
|
|
75
|
+
readonly app: AppInfo
|
|
76
|
+
readonly plugins: PluginStore
|
|
77
|
+
readonly events: EventLog
|
|
78
|
+
|
|
79
|
+
constructor(devices: Device[], defaultDeviceId: string, app: AppInfo, plugins: ShellPlugin[] = []) {
|
|
80
|
+
this.devices = devices
|
|
81
|
+
this.app = app
|
|
82
|
+
this.ui = {
|
|
83
|
+
dialogs: [],
|
|
84
|
+
splash: { visible: false, fadeMs: 0 },
|
|
85
|
+
notifications: [],
|
|
86
|
+
permissions: {},
|
|
87
|
+
orientationLock: null,
|
|
88
|
+
navigationBar: { style: 'auto', hidden: false },
|
|
89
|
+
}
|
|
90
|
+
const stored = readStored()
|
|
91
|
+
this.sizes = stored?.sizes ?? {}
|
|
92
|
+
const device =
|
|
93
|
+
devices.find((candidate) => candidate.id === stored?.deviceId) ??
|
|
94
|
+
devices.find((candidate) => candidate.id === defaultDeviceId) ??
|
|
95
|
+
devices[0]
|
|
96
|
+
this.state = this.withAppAppearance({
|
|
97
|
+
device: this.sized(device),
|
|
98
|
+
orientation: stored?.orientation ?? 'portrait',
|
|
99
|
+
appearance: stored?.appearance ?? (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'),
|
|
100
|
+
})
|
|
101
|
+
this.events = new EventLog()
|
|
102
|
+
this.plugins = new PluginStore(plugins, app.slug, () => this.state, (kind, result, origin) => this.answerDialogs(kind, result, origin), this.events)
|
|
103
|
+
// With the permissions plugin, its state is the permissions: its panel and the CLI change them too.
|
|
104
|
+
if (this.plugins.get(PERMISSIONS) !== undefined) {
|
|
105
|
+
this.ui.permissions = this.plugins.get(PERMISSIONS) as SystemUi['permissions']
|
|
106
|
+
this.plugins.subscribe(PERMISSIONS, () => this.setUi({ permissions: this.plugins.get(PERMISSIONS) as SystemUi['permissions'] }))
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
getState = (): SimState => this.state
|
|
111
|
+
|
|
112
|
+
private relauncher: ((work: () => Promise<void>) => Promise<void>) | null = null
|
|
113
|
+
|
|
114
|
+
/** The shell sets how the app is stopped and started again; returns its removal. */
|
|
115
|
+
onRelaunch(relauncher: (work: () => Promise<void>) => Promise<void>): () => void {
|
|
116
|
+
this.relauncher = relauncher
|
|
117
|
+
return () => {
|
|
118
|
+
if (this.relauncher === relauncher) {
|
|
119
|
+
this.relauncher = null
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
relaunch = async (work: () => Promise<void> = async () => undefined): Promise<void> => {
|
|
125
|
+
if (this.relauncher === null) {
|
|
126
|
+
await work()
|
|
127
|
+
return
|
|
128
|
+
}
|
|
129
|
+
await this.relauncher(work)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
getStatusBar = (): StatusBarState => this.statusBar
|
|
133
|
+
|
|
134
|
+
subscribe = (listener: () => void): (() => void) => {
|
|
135
|
+
this.listeners.add(listener)
|
|
136
|
+
return () => this.listeners.delete(listener)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
setStatusBar = (update: StatusBarUpdate): void => {
|
|
140
|
+
const next: StatusBarState = {
|
|
141
|
+
style: update.style ?? this.statusBar.style,
|
|
142
|
+
hidden: update.hidden ?? this.statusBar.hidden,
|
|
143
|
+
screenStyle: update.screenStyle === undefined ? this.statusBar.screenStyle : update.screenStyle,
|
|
144
|
+
}
|
|
145
|
+
if (next.style !== this.statusBar.style || next.hidden !== this.statusBar.hidden || next.screenStyle !== this.statusBar.screenStyle) {
|
|
146
|
+
this.statusBar = next
|
|
147
|
+
const style = next.screenStyle ?? next.style
|
|
148
|
+
this.log({
|
|
149
|
+
kind: 'ui.statusBar',
|
|
150
|
+
level: 'debug',
|
|
151
|
+
summary: `Status bar: ${style === 'default' ? 'default, follows the content under it' : style}${next.screenStyle !== null ? ' (screen)' : ''}${next.hidden ? ', hidden' : ''}`,
|
|
152
|
+
data: next,
|
|
153
|
+
})
|
|
154
|
+
this.emit()
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
getUi = (): SystemUi => this.ui
|
|
159
|
+
|
|
160
|
+
/** Adds an event to the log. The app's `log()` from `web-sim/runtime` comes here, so its events are the app's. */
|
|
161
|
+
log = (input: EventInput & { maxString?: number }): void => {
|
|
162
|
+
this.events.log({ actor: 'app', ...input })
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* A new app run: the status bar as the OS draws it, the splash screen up, and no dialogs or banners left from the
|
|
167
|
+
* last run.
|
|
168
|
+
*/
|
|
169
|
+
startRun(): void {
|
|
170
|
+
this.events.startRun()
|
|
171
|
+
const { device, orientation, appearance } = this.state
|
|
172
|
+
const appStyle = this.app.userInterfaceStyle?.[device.platform] ?? 'automatic'
|
|
173
|
+
this.events.log({
|
|
174
|
+
kind: 'sim.launch',
|
|
175
|
+
actor: 'system',
|
|
176
|
+
summary: `Launch on ${device.label} (${device.platform}), ${orientation}, ${appearance}${appStyle !== 'automatic' && appStyle !== appearance ? ` (the app pins ${appStyle})` : ''}`,
|
|
177
|
+
data: { device: device.id, platform: device.platform, orientation, appearance, userInterfaceStyle: appStyle, width: device.screen.width, height: device.screen.height },
|
|
178
|
+
})
|
|
179
|
+
this.ui.dialogs.forEach((queued) => queued.resolve({ button: -1 }))
|
|
180
|
+
this.ui.notifications.forEach((queued) => queued.resolve(false))
|
|
181
|
+
this.statusBar = { style: 'default', hidden: false, screenStyle: null }
|
|
182
|
+
this.splashPrevented = false
|
|
183
|
+
if (this.appColorScheme !== null) {
|
|
184
|
+
this.appColorScheme = null
|
|
185
|
+
this.update({})
|
|
186
|
+
}
|
|
187
|
+
this.plugins.startRun()
|
|
188
|
+
this.setUi({
|
|
189
|
+
dialogs: [],
|
|
190
|
+
notifications: [],
|
|
191
|
+
splash: { visible: this.app.splash !== null, fadeMs: 0 },
|
|
192
|
+
orientationLock: null,
|
|
193
|
+
navigationBar: { style: 'auto', hidden: false },
|
|
194
|
+
})
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
setOrientationLock = (lock: Orientation | null): void => {
|
|
198
|
+
if (lock !== this.ui.orientationLock) {
|
|
199
|
+
this.log({ kind: 'sim.orientationLock', summary: lock === null ? 'Orientation unlocked' : `Orientation locked to ${lock}`, data: { lock } })
|
|
200
|
+
}
|
|
201
|
+
this.setUi({ orientationLock: lock })
|
|
202
|
+
if (lock !== null && lock !== this.state.orientation) {
|
|
203
|
+
this.setOrientation(lock, APP)
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
getOrientationLock = (): Orientation | null => this.ui.orientationLock
|
|
208
|
+
|
|
209
|
+
setNavigationBar = (update: { style?: StatusBarStyle; hidden?: boolean }): void => {
|
|
210
|
+
this.log({ kind: 'ui.navigationBar', level: 'debug', summary: `Navigation bar: ${brief(update)}`, data: update })
|
|
211
|
+
this.setUi({ navigationBar: { ...this.ui.navigationBar, ...update } })
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
getNavigationBar = (): { style: StatusBarStyle; hidden: boolean } => this.ui.navigationBar
|
|
215
|
+
showDialog = (dialog: Dialog): Promise<DialogResult> =>
|
|
216
|
+
new Promise((resolve) => {
|
|
217
|
+
const queued: QueuedDialog = { id: this.nextId++, dialog, span: this.events.span('dialog'), resolve }
|
|
218
|
+
this.log({ kind: 'ui.dialog.shown', span: queued.span, summary: `${dialogName(dialog)} shown`, data: dialogData(dialog) })
|
|
219
|
+
this.setUi({ dialogs: [...this.ui.dialogs, queued] })
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
/** Answers a dialog: by default the user's press on it, else a plugin's action or the CLI standing in for them. */
|
|
223
|
+
respondToDialog(id: number, result: DialogResult, origin: Origin = USER): void {
|
|
224
|
+
const queued = this.ui.dialogs.find((candidate) => candidate.id === id)
|
|
225
|
+
if (queued !== undefined) {
|
|
226
|
+
this.events.log({
|
|
227
|
+
kind: 'ui.dialog.answered',
|
|
228
|
+
actor: origin.actor,
|
|
229
|
+
cause: origin.cause,
|
|
230
|
+
span: queued.span,
|
|
231
|
+
summary: `${dialogName(queued.dialog)} answered: ${dialogAnswer(queued.dialog, result)}`,
|
|
232
|
+
data: { button: result.button, label: dialogAnswer(queued.dialog, result), text: result.text, selected: result.selected },
|
|
233
|
+
})
|
|
234
|
+
this.setUi({ dialogs: this.ui.dialogs.filter((candidate) => candidate !== queued) })
|
|
235
|
+
queued.resolve(result)
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** Answers every open dialog of a kind with `result`, as if the user had. Returns how many it answered. */
|
|
240
|
+
answerDialogs(kind: Dialog['kind'], result: DialogResult, origin: Origin = USER): number {
|
|
241
|
+
const open = this.ui.dialogs.filter((queued) => queued.dialog.kind === kind)
|
|
242
|
+
open.forEach((queued) => this.respondToDialog(queued.id, result, origin))
|
|
243
|
+
return open.length
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** A haptic shows nowhere on the device: it is an entry in the event log, one kind for each of the app's calls. */
|
|
247
|
+
haptic = (kind: HapticKind, call: HapticCall = hapticCall(kind)): void => {
|
|
248
|
+
const field = call.method === 'impact' ? 'style' : 'type'
|
|
249
|
+
const named = call.method === 'android' ? call.type : kind
|
|
250
|
+
this.log({
|
|
251
|
+
kind: `haptics.${call.method}`,
|
|
252
|
+
summary: call.method === 'selection' ? 'Haptic selection' : `Haptic ${call.method}: ${named}`,
|
|
253
|
+
data: call.method === 'selection' ? {} : call.method === 'android' ? { type: call.type, as: kind } : { [field]: kind },
|
|
254
|
+
})
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
getPermission = (kind: PermissionKind): PermissionStatus => this.ui.permissions[kind] ?? 'undetermined'
|
|
258
|
+
|
|
259
|
+
setPermission = (kind: PermissionKind, status: PermissionStatus): void => {
|
|
260
|
+
if (this.plugins.get(PERMISSIONS) !== undefined) {
|
|
261
|
+
this.plugins.set(PERMISSIONS, { [kind]: status })
|
|
262
|
+
} else {
|
|
263
|
+
this.log({ kind: 'permissions.state', summary: `permissions: ${kind}=${status}`, data: { [kind]: status } })
|
|
264
|
+
this.setUi({ permissions: { ...this.ui.permissions, [kind]: status } })
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** The toolbar's reset: every permission back to not asked. */
|
|
269
|
+
resetPermissions(origin: Origin = USER): void {
|
|
270
|
+
if (this.plugins.get(PERMISSIONS) !== undefined) {
|
|
271
|
+
this.plugins.reset(PERMISSIONS, origin)
|
|
272
|
+
} else {
|
|
273
|
+
this.events.log({ kind: 'permissions.reset', actor: origin.actor, cause: origin.cause, summary: 'Permissions reset' })
|
|
274
|
+
this.setUi({ permissions: {} })
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
notify = (notification: SimNotification): Promise<boolean> =>
|
|
279
|
+
new Promise((resolve) => {
|
|
280
|
+
const queued: QueuedNotification = { ...notification, key: this.nextId++, span: `notification-${notification.id}`, resolve }
|
|
281
|
+
this.log({
|
|
282
|
+
kind: 'ui.banner.shown',
|
|
283
|
+
span: queued.span,
|
|
284
|
+
summary: `Banner: ${[notification.title, notification.body].filter(Boolean).join(' — ') || notification.id}`,
|
|
285
|
+
data: notification,
|
|
286
|
+
})
|
|
287
|
+
this.setUi({ notifications: [...this.ui.notifications, queued] })
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
/** A banner goes away: tapped or swiped by the user, or timed out (`system`). */
|
|
291
|
+
dismissNotification(key: number, tapped: boolean, origin: Origin = tapped ? USER : SYSTEM): void {
|
|
292
|
+
const queued = this.ui.notifications.find((candidate) => candidate.key === key)
|
|
293
|
+
if (queued !== undefined) {
|
|
294
|
+
this.events.log({
|
|
295
|
+
kind: tapped ? 'ui.banner.tapped' : 'ui.banner.dismissed',
|
|
296
|
+
actor: origin.actor,
|
|
297
|
+
cause: origin.cause,
|
|
298
|
+
span: queued.span,
|
|
299
|
+
summary: `Banner ${tapped ? 'tapped' : 'dismissed'}: ${queued.title ?? queued.body ?? queued.id}`,
|
|
300
|
+
data: { id: queued.id },
|
|
301
|
+
})
|
|
302
|
+
this.setUi({ notifications: this.ui.notifications.filter((candidate) => candidate !== queued) })
|
|
303
|
+
queued.resolve(tapped)
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
preventAutoHideSplash = (): void => {
|
|
308
|
+
this.splashPrevented = true
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
hideSplash = (options?: { fade?: boolean; duration?: number }): void => {
|
|
312
|
+
if (this.ui.splash.visible) {
|
|
313
|
+
this.events.log({ kind: 'ui.splash.hidden', level: 'debug', actor: this.splashPrevented ? 'app' : 'system', summary: 'Splash screen hidden' })
|
|
314
|
+
this.setUi({ splash: { visible: false, fadeMs: options?.fade === false ? 0 : (options?.duration ?? 250) } })
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
appRendered = (): void => {
|
|
319
|
+
if (!this.splashPrevented) {
|
|
320
|
+
this.hideSplash()
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
private setUi(change: Partial<SystemUi>): void {
|
|
325
|
+
this.ui = { ...this.ui, ...change }
|
|
326
|
+
this.emit()
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
setDevice(id: string, origin: Origin = USER): void {
|
|
330
|
+
const device = this.devices.find((candidate) => candidate.id === id)
|
|
331
|
+
if (device !== undefined && device.id !== this.state.device.id) {
|
|
332
|
+
const sized = this.sized(device)
|
|
333
|
+
this.events.log({
|
|
334
|
+
kind: 'sim.device',
|
|
335
|
+
actor: origin.actor,
|
|
336
|
+
cause: origin.cause,
|
|
337
|
+
summary: `Device: ${device.label} (${device.platform})`,
|
|
338
|
+
data: { device: device.id, platform: device.platform, width: sized.screen.width, height: sized.screen.height },
|
|
339
|
+
})
|
|
340
|
+
this.update({ device: sized })
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/** A resizable device with the size it was last given. */
|
|
345
|
+
private sized(device: Device): Device {
|
|
346
|
+
const size = device.resizable === undefined ? undefined : this.sizes[device.id]
|
|
347
|
+
return size === undefined ? device : { ...device, screen: size }
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Resizes a resizable device's screen, in portrait points, within its bounds. Other devices keep their size.
|
|
352
|
+
* Returns the size it got.
|
|
353
|
+
*/
|
|
354
|
+
setScreenSize(size: Size, origin: Origin = USER): Size {
|
|
355
|
+
const { device } = this.state
|
|
356
|
+
if (device.resizable === undefined) {
|
|
357
|
+
return device.screen
|
|
358
|
+
}
|
|
359
|
+
const { min, max } = device.resizable
|
|
360
|
+
const clamped = {
|
|
361
|
+
width: Math.round(Math.min(max.width, Math.max(min.width, size.width))),
|
|
362
|
+
height: Math.round(Math.min(max.height, Math.max(min.height, size.height))),
|
|
363
|
+
}
|
|
364
|
+
if (clamped.width !== device.screen.width || clamped.height !== device.screen.height) {
|
|
365
|
+
this.sizes = { ...this.sizes, [device.id]: clamped }
|
|
366
|
+
this.events.log({ kind: 'sim.resize', actor: origin.actor, cause: origin.cause, summary: `Screen: ${clamped.width}×${clamped.height}`, data: clamped })
|
|
367
|
+
this.update({ device: { ...device, screen: clamped } })
|
|
368
|
+
}
|
|
369
|
+
return clamped
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
setOrientation(orientation: Orientation, origin: Origin = USER): void {
|
|
373
|
+
if (orientation !== this.state.orientation) {
|
|
374
|
+
this.events.log({ kind: 'sim.orientation', actor: origin.actor, cause: origin.cause, summary: `Orientation: ${orientation}`, data: { orientation } })
|
|
375
|
+
}
|
|
376
|
+
this.update({ orientation })
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
setAppearance(appearance: Appearance, origin: Origin = USER): void {
|
|
380
|
+
if (appearance !== this.state.appearance) {
|
|
381
|
+
this.events.log({ kind: 'sim.appearance', actor: origin.actor, cause: origin.cause, summary: `Appearance: ${appearance}`, data: { appearance } })
|
|
382
|
+
}
|
|
383
|
+
this.update({ appearance })
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
setAppColorScheme = (scheme: Appearance | null): void => {
|
|
387
|
+
if (scheme !== this.appColorScheme) {
|
|
388
|
+
this.appColorScheme = scheme
|
|
389
|
+
this.log({ kind: 'app.colorScheme', summary: `Appearance.setColorScheme(${scheme === null ? 'unspecified' : scheme})`, data: { scheme } })
|
|
390
|
+
this.update({})
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* The state with the appearance the app's views get: what the app set with `Appearance.setColorScheme`, else what
|
|
396
|
+
* its config pins on this platform, else the device's.
|
|
397
|
+
*/
|
|
398
|
+
private withAppAppearance(state: Omit<SimState, 'appAppearance'>): SimState {
|
|
399
|
+
const pinned = this.appColorScheme ?? this.app.userInterfaceStyle?.[state.device.platform] ?? 'automatic'
|
|
400
|
+
return { ...state, appAppearance: pinned === 'automatic' ? state.appearance : pinned }
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
private update(change: Partial<SimState>): void {
|
|
404
|
+
this.state = this.withAppAppearance({ ...this.state, ...change })
|
|
405
|
+
try {
|
|
406
|
+
const stored: Stored = {
|
|
407
|
+
deviceId: this.state.device.id,
|
|
408
|
+
orientation: this.state.orientation,
|
|
409
|
+
appearance: this.state.appearance,
|
|
410
|
+
sizes: this.sizes,
|
|
411
|
+
}
|
|
412
|
+
localStorage.setItem(STORAGE_KEY, JSON.stringify(stored))
|
|
413
|
+
} catch {
|
|
414
|
+
// Storage blocked: the state lasts until the tab closes.
|
|
415
|
+
}
|
|
416
|
+
this.emit()
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
private emit(): void {
|
|
420
|
+
this.listeners.forEach((listener) => listener())
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/** What a dialog is, in a few words: `Alert "Delete?"`, `Permission prompt (camera)`. */
|
|
425
|
+
function dialogName(dialog: Dialog): string {
|
|
426
|
+
switch (dialog.kind) {
|
|
427
|
+
case 'alert':
|
|
428
|
+
return `Alert "${dialog.title}"`
|
|
429
|
+
case 'action-sheet':
|
|
430
|
+
return dialog.title === undefined ? 'Action sheet' : `Action sheet "${dialog.title}"`
|
|
431
|
+
case 'permission':
|
|
432
|
+
return `Permission prompt (${dialog.permission})`
|
|
433
|
+
case 'biometric':
|
|
434
|
+
return 'Biometric prompt'
|
|
435
|
+
case 'store-review':
|
|
436
|
+
return 'Review prompt'
|
|
437
|
+
case 'share':
|
|
438
|
+
return 'Share sheet'
|
|
439
|
+
case 'compose':
|
|
440
|
+
return dialog.channel === 'sms' ? 'Message composer' : 'Mail composer'
|
|
441
|
+
case 'photo-picker':
|
|
442
|
+
return 'Photo picker'
|
|
443
|
+
case 'camera':
|
|
444
|
+
return 'Camera'
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/** A dialog without its bulk: the picker's photos and the camera's feeds are many kilobytes of images. */
|
|
449
|
+
function dialogData(dialog: Dialog): unknown {
|
|
450
|
+
if (dialog.kind === 'photo-picker') {
|
|
451
|
+
return { ...dialog, photos: dialog.photos.length, albums: dialog.albums.map((album) => album.title) }
|
|
452
|
+
}
|
|
453
|
+
if (dialog.kind === 'camera') {
|
|
454
|
+
return { kind: dialog.kind, facing: dialog.facing }
|
|
455
|
+
}
|
|
456
|
+
return dialog
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/** The answer in words: the button's text where the dialog has buttons. */
|
|
460
|
+
function dialogAnswer(dialog: Dialog, result: DialogResult): string {
|
|
461
|
+
if (result.button === -1) {
|
|
462
|
+
return 'closed by a new run'
|
|
463
|
+
}
|
|
464
|
+
switch (dialog.kind) {
|
|
465
|
+
case 'alert':
|
|
466
|
+
case 'action-sheet':
|
|
467
|
+
return dialog.buttons[result.button]?.text ?? `button ${result.button}`
|
|
468
|
+
case 'permission':
|
|
469
|
+
return ['Don’t allow', 'Allow', 'Allow once'][result.button] ?? `button ${result.button}`
|
|
470
|
+
case 'biometric':
|
|
471
|
+
return ['match', 'no match', 'cancel'][result.button] ?? `button ${result.button}`
|
|
472
|
+
case 'photo-picker':
|
|
473
|
+
return result.button === 1 ? `picked ${result.selected?.length ?? 0}` : 'cancelled'
|
|
474
|
+
case 'camera':
|
|
475
|
+
return result.button === 1 ? 'photo taken' : 'cancelled'
|
|
476
|
+
default:
|
|
477
|
+
return result.button === 0 ? 'dismissed' : `button ${result.button}`
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
function readStored(): Stored | null {
|
|
482
|
+
return readJson<Stored>(STORAGE_KEY)
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
function readJson<T>(key: string): T | null {
|
|
486
|
+
try {
|
|
487
|
+
const raw = localStorage.getItem(key)
|
|
488
|
+
return raw === null ? null : (JSON.parse(raw) as T)
|
|
489
|
+
} catch {
|
|
490
|
+
return null
|
|
491
|
+
}
|
|
492
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// Icons Phosphor doesn't have, drawn on its 256 grid with its bold stroke (24, round caps and joins) so they sit
|
|
2
|
+
// beside the Phosphor icons in the toolbar.
|
|
3
|
+
|
|
4
|
+
const stroke = { fill: 'none', stroke: 'currentColor', strokeWidth: 24, strokeLinecap: 'round', strokeLinejoin: 'round' } as const
|
|
5
|
+
|
|
6
|
+
function Svg({ size, children, flip = false }: { size: number; children: React.ReactNode; flip?: boolean }) {
|
|
7
|
+
return (
|
|
8
|
+
<svg width={size} height={size} viewBox="0 0 256 256" aria-hidden style={flip ? { transform: 'scaleX(-1)' } : undefined}>
|
|
9
|
+
{children}
|
|
10
|
+
</svg>
|
|
11
|
+
)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** A phone with an arrow turning over its top corner, like Xcode's Simulator: left turns it counter-clockwise. */
|
|
15
|
+
export function RotateDeviceIcon({ direction, size = 16 }: { direction: 'left' | 'right'; size?: number }) {
|
|
16
|
+
return (
|
|
17
|
+
<Svg size={size} flip={direction === 'right'}>
|
|
18
|
+
<rect x="120" y="72" width="112" height="164" rx="20" {...stroke} />
|
|
19
|
+
<path d="M120 32 A80 80 0 0 0 40 112" {...stroke} />
|
|
20
|
+
<polyline points="16 88 40 112 64 88" {...stroke} />
|
|
21
|
+
</Svg>
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** A phone with a padlock beside it: the app has locked the orientation. */
|
|
26
|
+
export function OrientationLockIcon({ size = 16 }: { size?: number }) {
|
|
27
|
+
return (
|
|
28
|
+
<Svg size={size}>
|
|
29
|
+
<rect x="120" y="72" width="112" height="164" rx="20" {...stroke} />
|
|
30
|
+
<rect x="16" y="104" width="80" height="64" rx="8" {...stroke} />
|
|
31
|
+
<path d="M32 104 V84 a24 24 0 0 1 48 0 V104" {...stroke} />
|
|
32
|
+
</Svg>
|
|
33
|
+
)
|
|
34
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { SimEvent } from '../../node/events/shared.ts'
|
|
2
|
+
|
|
3
|
+
const HISTORY = 5000
|
|
4
|
+
const KEEP = 10_000
|
|
5
|
+
const NOTIFY_MS = 60
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The event log as the dev server keeps it, for the shell's drawer: the newest few thousand events, then each new one
|
|
9
|
+
* as it is stored, over Server-Sent Events. It is the server's log rather than this tab's, so it has the server's ids
|
|
10
|
+
* and runs, the CLI's commands and other tabs' events, and history from before this tab loaded. When the server
|
|
11
|
+
* restarts the stream reconnects and sends its history again; events already here are skipped by id.
|
|
12
|
+
*/
|
|
13
|
+
export class LogFeed {
|
|
14
|
+
events: SimEvent[] = []
|
|
15
|
+
/** Bumped on every change, for `useSyncExternalStore`. */
|
|
16
|
+
version = 0
|
|
17
|
+
/** Whether the stream is connected. */
|
|
18
|
+
connected = false
|
|
19
|
+
private ids = new Set<number>()
|
|
20
|
+
private listeners = new Set<() => void>()
|
|
21
|
+
private timer: ReturnType<typeof setTimeout> | undefined
|
|
22
|
+
private source: EventSource | null = null
|
|
23
|
+
/** The newest run this tab launched, by the server's numbering. */
|
|
24
|
+
private runs = new Map<string, number>()
|
|
25
|
+
|
|
26
|
+
start(): void {
|
|
27
|
+
if (this.source !== null) {
|
|
28
|
+
return
|
|
29
|
+
}
|
|
30
|
+
const source = new EventSource(`/__web-sim/events/stream?limit=${HISTORY}`)
|
|
31
|
+
this.source = source
|
|
32
|
+
source.onmessage = (message) => this.add(JSON.parse(message.data as string) as SimEvent)
|
|
33
|
+
source.addEventListener('live', () => {
|
|
34
|
+
// History comes in time order and live events in the order stored; after a reconnect they meet here.
|
|
35
|
+
this.events.sort((a, b) => a.time - b.time || a.id - b.id)
|
|
36
|
+
this.setConnected(true)
|
|
37
|
+
})
|
|
38
|
+
source.onerror = () => this.setConnected(false)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
private setConnected(connected: boolean): void {
|
|
42
|
+
if (this.connected !== connected) {
|
|
43
|
+
this.connected = connected
|
|
44
|
+
this.changed()
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private add(event: SimEvent): void {
|
|
49
|
+
if (this.ids.has(event.id)) {
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
this.ids.add(event.id)
|
|
53
|
+
this.events.push(event)
|
|
54
|
+
if (event.kind === 'sim.launch') {
|
|
55
|
+
this.runs.set(event.tab, Math.max(this.runs.get(event.tab) ?? 0, event.run))
|
|
56
|
+
}
|
|
57
|
+
if (this.events.length > KEEP) {
|
|
58
|
+
const dropped = this.events.splice(0, this.events.length - KEEP)
|
|
59
|
+
dropped.forEach((old) => this.ids.delete(old.id))
|
|
60
|
+
}
|
|
61
|
+
this.changed()
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The newest run a tab launched, or the newest run of all when it has launched none the log still holds. */
|
|
65
|
+
currentRun(tab: string): number {
|
|
66
|
+
return this.runs.get(tab) ?? this.events.reduce((max, event) => Math.max(max, event.run), 0)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Empties the log on the server, and here. */
|
|
70
|
+
async clear(): Promise<void> {
|
|
71
|
+
await fetch('/__web-sim/events', { method: 'DELETE' })
|
|
72
|
+
this.events = []
|
|
73
|
+
this.ids.clear()
|
|
74
|
+
this.runs.clear()
|
|
75
|
+
this.changed()
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
subscribe = (listener: () => void): (() => void) => {
|
|
79
|
+
this.listeners.add(listener)
|
|
80
|
+
return () => this.listeners.delete(listener)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
getVersion = (): number => this.version
|
|
84
|
+
|
|
85
|
+
private changed(): void {
|
|
86
|
+
// Many events can arrive at once (history, a burst of state changes): one render for them all.
|
|
87
|
+
this.timer ??= setTimeout(() => {
|
|
88
|
+
this.timer = undefined
|
|
89
|
+
this.version += 1
|
|
90
|
+
this.listeners.forEach((listener) => listener())
|
|
91
|
+
}, NOTIFY_MS)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
let feed: LogFeed | null = null
|
|
96
|
+
|
|
97
|
+
/** The shell's one feed, connected on first use. */
|
|
98
|
+
export function logFeed(): LogFeed {
|
|
99
|
+
feed ??= new LogFeed()
|
|
100
|
+
feed.start()
|
|
101
|
+
return feed
|
|
102
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import './shell.css'
|
|
2
|
+
import { IconContext } from '@phosphor-icons/react'
|
|
3
|
+
import { createRoot } from 'react-dom/client'
|
|
4
|
+
import { appInfo, defaultDevice, devices, plugins } from 'virtual:web-sim/sim-config'
|
|
5
|
+
import { listenForControl } from './control.ts'
|
|
6
|
+
import { installTooltips } from './tooltips.ts'
|
|
7
|
+
import { HostStore } from './host.ts'
|
|
8
|
+
import { Shell } from './Shell.tsx'
|
|
9
|
+
|
|
10
|
+
const host = new HostStore(devices, defaultDevice, appInfo, plugins)
|
|
11
|
+
window.__webSimHost = host
|
|
12
|
+
host.plugins.start()
|
|
13
|
+
listenForControl(host)
|
|
14
|
+
installTooltips(document)
|
|
15
|
+
|
|
16
|
+
// One icon set and weight for the whole shell (toolbar, panels, system UI drawn in the phone). Plugin panels that
|
|
17
|
+
// use Phosphor icons pick up the same weight.
|
|
18
|
+
createRoot(document.getElementById('root')!).render(
|
|
19
|
+
<IconContext.Provider value={{ weight: 'bold' }}>
|
|
20
|
+
<Shell host={host} />
|
|
21
|
+
</IconContext.Provider>,
|
|
22
|
+
)
|