@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,169 @@
|
|
|
1
|
+
import type { PermissionKind, Platform } from '../../runtime/index.ts'
|
|
2
|
+
|
|
3
|
+
export type PromptButton = { text: string; result: number; bold?: boolean }
|
|
4
|
+
|
|
5
|
+
export type PermissionPrompt = {
|
|
6
|
+
title: string
|
|
7
|
+
message?: string
|
|
8
|
+
buttons: PromptButton[]
|
|
9
|
+
/** Buttons stacked rather than side by side. */
|
|
10
|
+
stacked: boolean
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const DENY = 0
|
|
14
|
+
const ALLOW = 1
|
|
15
|
+
const ALLOW_ONCE = 2
|
|
16
|
+
|
|
17
|
+
const IOS_SUBJECT: Partial<Record<PermissionKind, string>> = {
|
|
18
|
+
camera: 'the Camera',
|
|
19
|
+
microphone: 'the Microphone',
|
|
20
|
+
photos: 'Your Photos',
|
|
21
|
+
contacts: 'Your Contacts',
|
|
22
|
+
calendar: 'Your Calendar',
|
|
23
|
+
reminders: 'Your Reminders',
|
|
24
|
+
motion: 'Your Motion & Fitness Activity',
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const USAGE_KEYS: Partial<Record<PermissionKind, string>> = {
|
|
28
|
+
camera: 'NSCameraUsageDescription',
|
|
29
|
+
microphone: 'NSMicrophoneUsageDescription',
|
|
30
|
+
photos: 'NSPhotoLibraryUsageDescription',
|
|
31
|
+
location: 'NSLocationWhenInUseUsageDescription',
|
|
32
|
+
'location-background': 'NSLocationAlwaysAndWhenInUseUsageDescription',
|
|
33
|
+
contacts: 'NSContactsUsageDescription',
|
|
34
|
+
calendar: 'NSCalendarsUsageDescription',
|
|
35
|
+
reminders: 'NSRemindersUsageDescription',
|
|
36
|
+
tracking: 'NSUserTrackingUsageDescription',
|
|
37
|
+
motion: 'NSMotionUsageDescription',
|
|
38
|
+
'face-id': 'NSFaceIDUsageDescription',
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const ANDROID_ASK: Partial<Record<PermissionKind, string>> = {
|
|
42
|
+
camera: 'take pictures and record video',
|
|
43
|
+
microphone: 'record audio',
|
|
44
|
+
photos: 'access photos and videos on this device',
|
|
45
|
+
location: "access this device's location",
|
|
46
|
+
'location-background': "access this device's location all the time",
|
|
47
|
+
contacts: 'access your contacts',
|
|
48
|
+
calendar: 'access your calendar',
|
|
49
|
+
reminders: 'access your calendar',
|
|
50
|
+
notifications: 'send you notifications',
|
|
51
|
+
motion: 'access your physical activity',
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Each OS's own wording for its permission prompts. On iOS the app's usage description from `app.json` is the
|
|
56
|
+
* message, as iOS shows it.
|
|
57
|
+
*/
|
|
58
|
+
export function permissionPrompt(
|
|
59
|
+
platform: Platform,
|
|
60
|
+
kind: PermissionKind,
|
|
61
|
+
app: string,
|
|
62
|
+
usageDescriptions: Record<string, string>,
|
|
63
|
+
message?: string,
|
|
64
|
+
): PermissionPrompt {
|
|
65
|
+
if (platform === 'android') {
|
|
66
|
+
if (kind === 'location-background') {
|
|
67
|
+
return {
|
|
68
|
+
title: `Allow ${app} to access this device's location all the time?`,
|
|
69
|
+
stacked: true,
|
|
70
|
+
buttons: [
|
|
71
|
+
{ text: 'Allow all the time', result: ALLOW },
|
|
72
|
+
{ text: 'Allow only while using the app', result: DENY },
|
|
73
|
+
{ text: "Don't allow", result: DENY },
|
|
74
|
+
],
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const location = kind === 'location'
|
|
78
|
+
return {
|
|
79
|
+
title: `Allow ${app} to ${ANDROID_ASK[kind] ?? 'access this feature'}?`,
|
|
80
|
+
stacked: true,
|
|
81
|
+
buttons: location
|
|
82
|
+
? [
|
|
83
|
+
{ text: 'While using the app', result: ALLOW },
|
|
84
|
+
{ text: 'Only this time', result: ALLOW_ONCE },
|
|
85
|
+
{ text: "Don't allow", result: DENY },
|
|
86
|
+
]
|
|
87
|
+
: [
|
|
88
|
+
{ text: 'Allow', result: ALLOW },
|
|
89
|
+
{ text: "Don't allow", result: DENY },
|
|
90
|
+
],
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const description = message ?? (USAGE_KEYS[kind] ? usageDescriptions[USAGE_KEYS[kind]!] : undefined)
|
|
94
|
+
const quoted = `“${app}”`
|
|
95
|
+
switch (kind) {
|
|
96
|
+
case 'location-background':
|
|
97
|
+
// Asked once the app has location while in use: the upgrade to Always.
|
|
98
|
+
return {
|
|
99
|
+
title: `Allow ${quoted} to also use your location even when you are not using the app?`,
|
|
100
|
+
message: description,
|
|
101
|
+
stacked: true,
|
|
102
|
+
buttons: [
|
|
103
|
+
{ text: 'Keep Only While Using', result: DENY },
|
|
104
|
+
{ text: 'Change to Always Allow', result: ALLOW },
|
|
105
|
+
],
|
|
106
|
+
}
|
|
107
|
+
case 'location':
|
|
108
|
+
return {
|
|
109
|
+
title: `Allow ${quoted} to use your location?`,
|
|
110
|
+
message: description,
|
|
111
|
+
stacked: true,
|
|
112
|
+
buttons: [
|
|
113
|
+
{ text: 'Allow Once', result: ALLOW_ONCE },
|
|
114
|
+
{ text: 'Allow While Using App', result: ALLOW },
|
|
115
|
+
{ text: 'Don’t Allow', result: DENY },
|
|
116
|
+
],
|
|
117
|
+
}
|
|
118
|
+
case 'notifications':
|
|
119
|
+
return {
|
|
120
|
+
title: `${quoted} Would Like to Send You Notifications`,
|
|
121
|
+
message: 'Notifications may include alerts, sounds, and icon badges. These can be configured in Settings.',
|
|
122
|
+
stacked: false,
|
|
123
|
+
buttons: [
|
|
124
|
+
{ text: 'Don’t Allow', result: DENY },
|
|
125
|
+
{ text: 'Allow', result: ALLOW, bold: true },
|
|
126
|
+
],
|
|
127
|
+
}
|
|
128
|
+
case 'tracking':
|
|
129
|
+
return {
|
|
130
|
+
title: `Allow ${quoted} to track your activity across other companies’ apps and websites?`,
|
|
131
|
+
message: description,
|
|
132
|
+
stacked: true,
|
|
133
|
+
buttons: [
|
|
134
|
+
{ text: 'Ask App Not to Track', result: DENY },
|
|
135
|
+
{ text: 'Allow', result: ALLOW },
|
|
136
|
+
],
|
|
137
|
+
}
|
|
138
|
+
case 'face-id':
|
|
139
|
+
return {
|
|
140
|
+
title: `Do you want to allow ${quoted} to use Face ID?`,
|
|
141
|
+
message: description,
|
|
142
|
+
stacked: false,
|
|
143
|
+
buttons: [
|
|
144
|
+
{ text: 'Don’t Allow', result: DENY },
|
|
145
|
+
{ text: 'OK', result: ALLOW, bold: true },
|
|
146
|
+
],
|
|
147
|
+
}
|
|
148
|
+
case 'photos':
|
|
149
|
+
return {
|
|
150
|
+
title: `${quoted} Would Like to Access Your Photos`,
|
|
151
|
+
message: description,
|
|
152
|
+
stacked: true,
|
|
153
|
+
buttons: [
|
|
154
|
+
{ text: 'Allow Full Access', result: ALLOW },
|
|
155
|
+
{ text: 'Don’t Allow', result: DENY, bold: true },
|
|
156
|
+
],
|
|
157
|
+
}
|
|
158
|
+
default:
|
|
159
|
+
return {
|
|
160
|
+
title: `${quoted} Would Like to Access ${IOS_SUBJECT[kind] ?? 'This Feature'}`,
|
|
161
|
+
message: description,
|
|
162
|
+
stacked: false,
|
|
163
|
+
buttons: [
|
|
164
|
+
{ text: 'Don’t Allow', result: DENY },
|
|
165
|
+
{ text: 'Allow', result: ALLOW, bold: true },
|
|
166
|
+
],
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import type { PluginContext, PluginState, ScreenEffect, ShellPlugin, StatusBarItems } from '../../plugin/types.ts'
|
|
2
|
+
import type { Dialog, DialogResult, PluginHost, SimState } from '../../runtime/index.ts'
|
|
3
|
+
import { APP, brief, type EventLog, type Origin, SYSTEM, USER } from './events.ts'
|
|
4
|
+
|
|
5
|
+
type Side = 'app' | 'shell'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The plugins' state, kept by the shell. Panels, overrides in the app and the CLI all read and change it here, so
|
|
9
|
+
* whichever changes it, the others see the change.
|
|
10
|
+
*/
|
|
11
|
+
export class PluginStore implements PluginHost {
|
|
12
|
+
readonly plugins: ShellPlugin[]
|
|
13
|
+
private states = new Map<string, PluginState>()
|
|
14
|
+
private listeners = new Map<string, Set<() => void>>()
|
|
15
|
+
private eventListeners = new Map<string, Set<(payload: unknown) => void>>()
|
|
16
|
+
private readonly slug: string
|
|
17
|
+
private readonly sim: () => SimState
|
|
18
|
+
private readonly answerDialog: (kind: Dialog['kind'], result: DialogResult, origin: Origin) => number
|
|
19
|
+
readonly events: EventLog
|
|
20
|
+
|
|
21
|
+
constructor(
|
|
22
|
+
plugins: ShellPlugin[],
|
|
23
|
+
slug: string,
|
|
24
|
+
sim: () => SimState,
|
|
25
|
+
answerDialog: (kind: Dialog['kind'], result: DialogResult, origin: Origin) => number,
|
|
26
|
+
events: EventLog,
|
|
27
|
+
) {
|
|
28
|
+
this.plugins = plugins
|
|
29
|
+
this.events = events
|
|
30
|
+
this.slug = slug
|
|
31
|
+
this.sim = sim
|
|
32
|
+
this.answerDialog = answerDialog
|
|
33
|
+
for (const plugin of plugins) {
|
|
34
|
+
const stored = plugin.persist === false ? null : read(this.key(plugin.id))
|
|
35
|
+
// Stored state over the initial state, so fields a newer plugin adds get their defaults.
|
|
36
|
+
this.states.set(plugin.id, { ...plugin.initialState, ...stored })
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Runs each plugin's `setup`, once the host is in place on the window for the plugins to reach. */
|
|
41
|
+
start(): void {
|
|
42
|
+
for (const plugin of this.plugins) {
|
|
43
|
+
plugin.setup?.(this.context(plugin, SYSTEM))
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** A new app run: the last run's listeners on the app's side are gone with its window. */
|
|
48
|
+
startRun(): void {
|
|
49
|
+
for (const key of [...this.eventListeners.keys()]) {
|
|
50
|
+
if (key.endsWith('\0app')) {
|
|
51
|
+
this.eventListeners.delete(key)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
for (const plugin of this.plugins) {
|
|
55
|
+
plugin.appStarted?.(this.context(plugin, SYSTEM))
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
plugin(id: string): ShellPlugin | undefined {
|
|
60
|
+
return this.plugins.find((plugin) => plugin.id === id)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
get = (id: string): PluginState | undefined => this.states.get(id)
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Changes a plugin's state. Without an origin the change is the app's: the app reaches this through the host, and
|
|
67
|
+
* every entry point in the shell passes its own.
|
|
68
|
+
*/
|
|
69
|
+
set = (id: string, patch: Record<string, unknown>, origin: Origin = APP): void => {
|
|
70
|
+
const current = this.states.get(id)
|
|
71
|
+
if (current === undefined) {
|
|
72
|
+
throw new Error(`No plugin '${id}'. Loaded: ${this.plugins.map((plugin) => plugin.id).join(', ')}`)
|
|
73
|
+
}
|
|
74
|
+
const changed = Object.keys(patch).filter((key) => !Object.is(current[key], patch[key]))
|
|
75
|
+
if (changed.length === 0) {
|
|
76
|
+
return
|
|
77
|
+
}
|
|
78
|
+
// A copy made in this window: a patch from the app's window holds objects of its realm.
|
|
79
|
+
const copy = structuredClone(patch)
|
|
80
|
+
const next = { ...current, ...copy }
|
|
81
|
+
this.logState(id, changed, copy, origin)
|
|
82
|
+
this.states.set(id, next)
|
|
83
|
+
if (this.plugin(id)?.persist !== false) {
|
|
84
|
+
write(this.key(id), next)
|
|
85
|
+
}
|
|
86
|
+
this.version += 1
|
|
87
|
+
this.listeners.get(id)?.forEach((listener) => listener())
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Every plugin's status bar items, merged; a new object only when a plugin's state changes. */
|
|
91
|
+
statusBarItems(sim: SimState): StatusBarItems {
|
|
92
|
+
return Object.assign({}, ...this.plugins.map((plugin) => plugin.statusBar?.(this.states.get(plugin.id)!, sim) ?? {})) as StatusBarItems
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Every plugin's screen effect, combined: filters in plugin order, input blocked if any plugin blocks it. */
|
|
96
|
+
screenEffect(sim: SimState): ScreenEffect {
|
|
97
|
+
const effects = this.plugins.map((plugin) => plugin.screen?.(this.states.get(plugin.id)!, sim) ?? {})
|
|
98
|
+
const filter = effects.map((effect) => effect.filter).filter(Boolean).join(' ')
|
|
99
|
+
return { filter: filter === '' ? undefined : filter, blockInput: effects.some((effect) => effect.blockInput === true) }
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Whether any plugin listens to presses on the screen. */
|
|
103
|
+
get hearsPresses(): boolean {
|
|
104
|
+
return this.plugins.some((plugin) => plugin.onPress !== undefined)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** A press on the device's screen, passed to each plugin's `onPress`. */
|
|
108
|
+
press = (): void => {
|
|
109
|
+
for (const plugin of this.plugins) {
|
|
110
|
+
plugin.onPress?.(this.context(plugin, USER))
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Listens to every plugin's state. */
|
|
115
|
+
subscribeAll = (listener: () => void): (() => void) => {
|
|
116
|
+
const removals = this.plugins.map((plugin) => this.subscribe(plugin.id, listener))
|
|
117
|
+
return () => removals.forEach((remove) => remove())
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Bumped on every change to any plugin's state, for `useSyncExternalStore`. */
|
|
121
|
+
version = 0
|
|
122
|
+
|
|
123
|
+
reset(id: string, origin: Origin = USER): void {
|
|
124
|
+
const plugin = this.plugin(id)
|
|
125
|
+
if (plugin !== undefined) {
|
|
126
|
+
this.set(id, { ...plugin.initialState }, origin)
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** A `<plugin>.state` event for the keys a change set, at the plugin's `logState` level (debug by default). */
|
|
131
|
+
private logState(id: string, keys: string[], patch: Record<string, unknown>, origin: Origin): void {
|
|
132
|
+
const level = this.plugin(id)?.logState ?? 'debug'
|
|
133
|
+
if (level === false) {
|
|
134
|
+
return
|
|
135
|
+
}
|
|
136
|
+
const ignored = new Set(this.plugin(id)?.logIgnore ?? [])
|
|
137
|
+
const shown = keys.filter((key) => !ignored.has(key))
|
|
138
|
+
if (shown.length === 0) {
|
|
139
|
+
return
|
|
140
|
+
}
|
|
141
|
+
this.events.log({
|
|
142
|
+
kind: `${id}.state`,
|
|
143
|
+
level,
|
|
144
|
+
actor: origin.actor,
|
|
145
|
+
cause: origin.cause,
|
|
146
|
+
summary: `${id}: ${shown.map((key) => `${key}=${brief(patch[key], 40)}`).join(' ')}`,
|
|
147
|
+
data: Object.fromEntries(shown.map((key) => [key, patch[key]])),
|
|
148
|
+
})
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
subscribe = (id: string, listener: () => void): (() => void) => {
|
|
152
|
+
const listeners = this.listeners.get(id) ?? new Set()
|
|
153
|
+
this.listeners.set(id, listeners)
|
|
154
|
+
listeners.add(listener)
|
|
155
|
+
return () => listeners.delete(listener)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
emit = (id: string, event: string, payload: unknown, from: Side, origin: Origin = from === 'app' ? APP : SYSTEM): void => {
|
|
159
|
+
const to: Side = from === 'app' ? 'shell' : 'app'
|
|
160
|
+
this.events.log({
|
|
161
|
+
kind: `${id}.event`,
|
|
162
|
+
level: 'debug',
|
|
163
|
+
actor: origin.actor,
|
|
164
|
+
cause: origin.cause,
|
|
165
|
+
summary: `${id}: ${event} (${from} → ${to})${payload === undefined ? '' : ` ${brief(payload)}`}`,
|
|
166
|
+
data: { event, from, payload },
|
|
167
|
+
})
|
|
168
|
+
this.eventListeners.get(`${id}\0${event}\0${to}`)?.forEach((listener) => listener(payload))
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
on = (id: string, event: string, listener: (payload: unknown) => void, side: Side): (() => void) => {
|
|
172
|
+
const key = `${id}\0${event}\0${side}`
|
|
173
|
+
const listeners = this.eventListeners.get(key) ?? new Set()
|
|
174
|
+
this.eventListeners.set(key, listeners)
|
|
175
|
+
listeners.add(listener)
|
|
176
|
+
return () => listeners.delete(listener)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
listenerCount = (id: string, event: string, side: Side): number => this.eventListeners.get(`${id}\0${event}\0${side}`)?.size ?? 0
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Runs a plugin's action, as its panel or the CLI asks. The action gets a context of its own, so what it changes,
|
|
183
|
+
* even after an `await`, is put down to whoever ran it and to this action's `<plugin>.action` event.
|
|
184
|
+
*/
|
|
185
|
+
run(id: string, action: string, args?: unknown, origin: Origin = USER): unknown {
|
|
186
|
+
const plugin = this.plugin(id)
|
|
187
|
+
const run = plugin?.actions?.[action]
|
|
188
|
+
if (plugin === undefined || run === undefined) {
|
|
189
|
+
throw new Error(`No action '${action}' on plugin '${id}'. Actions: ${Object.keys(plugin?.actions ?? {}).join(', ') || 'none'}`)
|
|
190
|
+
}
|
|
191
|
+
const span = this.events.span('act')
|
|
192
|
+
this.events.log({
|
|
193
|
+
kind: `${id}.action`,
|
|
194
|
+
actor: origin.actor,
|
|
195
|
+
cause: origin.cause,
|
|
196
|
+
span,
|
|
197
|
+
summary: `${id}.${action}${args === undefined ? '' : ` ${brief(args)}`}`,
|
|
198
|
+
data: { action, args },
|
|
199
|
+
})
|
|
200
|
+
const failed = (error: unknown) =>
|
|
201
|
+
this.events.log({
|
|
202
|
+
kind: `${id}.action.failed`,
|
|
203
|
+
level: 'error',
|
|
204
|
+
actor: origin.actor,
|
|
205
|
+
cause: span,
|
|
206
|
+
span,
|
|
207
|
+
summary: `${id}.${action} failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
208
|
+
data: { action, error },
|
|
209
|
+
})
|
|
210
|
+
try {
|
|
211
|
+
const result = run(this.context(plugin, { actor: origin.actor, cause: span }), args)
|
|
212
|
+
if (result instanceof Promise) {
|
|
213
|
+
result.catch(failed)
|
|
214
|
+
}
|
|
215
|
+
return result
|
|
216
|
+
} catch (error) {
|
|
217
|
+
failed(error)
|
|
218
|
+
throw error
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** A plugin's context, whose changes are put down to `origin`. */
|
|
223
|
+
context<S extends PluginState>(plugin: ShellPlugin<S>, origin: Origin = SYSTEM): PluginContext<S> {
|
|
224
|
+
return {
|
|
225
|
+
state: () => this.states.get(plugin.id) as S,
|
|
226
|
+
set: (patch) => this.set(plugin.id, patch, origin),
|
|
227
|
+
subscribe: (listener) => this.subscribe(plugin.id, listener),
|
|
228
|
+
emit: (event, payload) => this.emit(plugin.id, event, payload, 'shell', origin),
|
|
229
|
+
on: (event, listener) => this.on(plugin.id, event, listener, 'shell'),
|
|
230
|
+
listenerCount: (event) => this.listenerCount(plugin.id, event, 'app'),
|
|
231
|
+
sim: this.sim,
|
|
232
|
+
answerDialog: (kind, result) => this.answerDialog(kind, result, origin),
|
|
233
|
+
run: (action, args) => this.run(plugin.id, action, args, origin),
|
|
234
|
+
log: (kind, summary, data, options) =>
|
|
235
|
+
this.events.log({
|
|
236
|
+
kind: kind.startsWith(`${plugin.id}.`) ? kind : `${plugin.id}.${kind}`,
|
|
237
|
+
summary,
|
|
238
|
+
data,
|
|
239
|
+
actor: origin.actor,
|
|
240
|
+
cause: origin.cause,
|
|
241
|
+
...options,
|
|
242
|
+
}),
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
private key(id: string): string {
|
|
247
|
+
return `web-sim:plugin:${this.slug}:${id}`
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function read(key: string): PluginState | null {
|
|
252
|
+
try {
|
|
253
|
+
const raw = localStorage.getItem(key)
|
|
254
|
+
return raw === null ? null : (JSON.parse(raw) as PluginState)
|
|
255
|
+
} catch {
|
|
256
|
+
return null
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function write(key: string, value: PluginState): void {
|
|
261
|
+
try {
|
|
262
|
+
localStorage.setItem(key, JSON.stringify(value))
|
|
263
|
+
} catch {
|
|
264
|
+
// Storage blocked: kept until the tab closes.
|
|
265
|
+
}
|
|
266
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from 'react'
|
|
2
|
+
import type { SimHost } from '../../runtime/index.ts'
|
|
3
|
+
import { installTooltips } from './tooltips.ts'
|
|
4
|
+
|
|
5
|
+
/** Space around the phone in its own window, so the frame's shadow shows a little. */
|
|
6
|
+
export const POPOUT_PADDING = 16
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Chromium's Document Picture-in-Picture: an always-on-top window that holds any DOM, like a native simulator's
|
|
10
|
+
* window. Not in TypeScript's DOM types yet.
|
|
11
|
+
*/
|
|
12
|
+
type DocumentPictureInPicture = {
|
|
13
|
+
requestWindow(options?: {
|
|
14
|
+
width?: number
|
|
15
|
+
height?: number
|
|
16
|
+
disallowReturnToOpener?: boolean
|
|
17
|
+
preferInitialWindowPlacement?: boolean
|
|
18
|
+
}): Promise<Window>
|
|
19
|
+
window: Window | null
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
declare global {
|
|
23
|
+
interface Window {
|
|
24
|
+
documentPictureInPicture?: DocumentPictureInPicture
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type Popout = {
|
|
29
|
+
/** The element the phone renders into while it is popped out. */
|
|
30
|
+
container: HTMLElement
|
|
31
|
+
window: Window
|
|
32
|
+
kind: 'pip' | 'popup'
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Moves the phone into its own window and back. The app's frame reloads on each move, because a frame that changes
|
|
37
|
+
* documents starts over, so `beforeMove` is the moment to remember where the app was.
|
|
38
|
+
*/
|
|
39
|
+
export function usePopout({
|
|
40
|
+
host,
|
|
41
|
+
size,
|
|
42
|
+
beforeMove,
|
|
43
|
+
}: {
|
|
44
|
+
host: SimHost
|
|
45
|
+
/** The window's content size the phone wants now. */
|
|
46
|
+
size: { width: number; height: number }
|
|
47
|
+
beforeMove: () => void
|
|
48
|
+
}) {
|
|
49
|
+
const [popout, setPopout] = useState<Popout | null>(null)
|
|
50
|
+
|
|
51
|
+
const open = useCallback(async () => {
|
|
52
|
+
if (popout !== null) {
|
|
53
|
+
popout.window.focus()
|
|
54
|
+
return
|
|
55
|
+
}
|
|
56
|
+
const target = popoutTarget(size)
|
|
57
|
+
const view = window.documentPictureInPicture
|
|
58
|
+
? await window.documentPictureInPicture.requestWindow({ ...target, preferInitialWindowPlacement: true }).catch(() => null)
|
|
59
|
+
: null
|
|
60
|
+
const popup =
|
|
61
|
+
view ??
|
|
62
|
+
window.open(
|
|
63
|
+
'',
|
|
64
|
+
'web-sim-phone',
|
|
65
|
+
`popup,width=${target.width},height=${target.height},left=${Math.round(screen.availWidth - target.width - 40)},top=60`,
|
|
66
|
+
)
|
|
67
|
+
if (popup === null) {
|
|
68
|
+
return
|
|
69
|
+
}
|
|
70
|
+
beforeMove()
|
|
71
|
+
setPopout(preparePopout(popup, view !== null ? 'pip' : 'popup', host))
|
|
72
|
+
}, [popout, size, beforeMove, host])
|
|
73
|
+
|
|
74
|
+
const close = useCallback(() => {
|
|
75
|
+
popout?.window.close()
|
|
76
|
+
}, [popout])
|
|
77
|
+
|
|
78
|
+
// The window closing (its own close button, the "back to tab" button, or `close`) brings the phone back.
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
if (popout === null) {
|
|
81
|
+
return
|
|
82
|
+
}
|
|
83
|
+
const view = popout.window
|
|
84
|
+
const onClose = () => {
|
|
85
|
+
beforeMove()
|
|
86
|
+
setPopout(null)
|
|
87
|
+
}
|
|
88
|
+
view.addEventListener('pagehide', onClose)
|
|
89
|
+
// A popup outlives the shell's tab, a Picture-in-Picture window doesn't.
|
|
90
|
+
const closeWithShell = () => view.close()
|
|
91
|
+
window.addEventListener('pagehide', closeWithShell)
|
|
92
|
+
return () => {
|
|
93
|
+
view.removeEventListener('pagehide', onClose)
|
|
94
|
+
window.removeEventListener('pagehide', closeWithShell)
|
|
95
|
+
}
|
|
96
|
+
}, [popout, beforeMove])
|
|
97
|
+
|
|
98
|
+
// A new device or orientation resizes the window to fit. Chromium lets a Picture-in-Picture window resize only
|
|
99
|
+
// with a user gesture, so the phone also scales to whatever size the window has.
|
|
100
|
+
useEffect(() => {
|
|
101
|
+
if (popout === null) {
|
|
102
|
+
return
|
|
103
|
+
}
|
|
104
|
+
const view = popout.window
|
|
105
|
+
const resizeTo = (target: { width: number; height: number }) => {
|
|
106
|
+
const dw = target.width - view.innerWidth
|
|
107
|
+
const dh = target.height - view.innerHeight
|
|
108
|
+
if (Math.abs(dw) > 1 || Math.abs(dh) > 1) {
|
|
109
|
+
try {
|
|
110
|
+
view.resizeBy(dw, dh)
|
|
111
|
+
} catch {
|
|
112
|
+
// Not allowed without a gesture.
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const target = popoutTarget(size)
|
|
117
|
+
resizeTo(target)
|
|
118
|
+
// Chromium caps a Picture-in-Picture window's size, in one dimension only, which would leave a band of empty
|
|
119
|
+
// stage beside the phone: shrink the other to match.
|
|
120
|
+
const settle = setTimeout(() => {
|
|
121
|
+
const scale = Math.min(view.innerWidth / target.width, view.innerHeight / target.height)
|
|
122
|
+
if (scale < 0.98) {
|
|
123
|
+
resizeTo({ width: Math.round(target.width * scale), height: Math.round(target.height * scale) })
|
|
124
|
+
}
|
|
125
|
+
}, 150)
|
|
126
|
+
return () => clearTimeout(settle)
|
|
127
|
+
}, [popout, size.width, size.height])
|
|
128
|
+
|
|
129
|
+
return { popout, open, close }
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* The window's content size for a phone this big: actual size, unless the screen is too small for it. Chromium
|
|
134
|
+
* opens a Picture-in-Picture window at most 80% of the screen in each dimension.
|
|
135
|
+
*/
|
|
136
|
+
function popoutTarget(size: { width: number; height: number }): { width: number; height: number } {
|
|
137
|
+
const full = { width: size.width + POPOUT_PADDING * 2, height: size.height + POPOUT_PADDING * 2 }
|
|
138
|
+
const scale = Math.min(1, (screen.availWidth * 0.8) / full.width, (screen.availHeight * 0.8) / full.height)
|
|
139
|
+
return { width: Math.round(full.width * scale), height: Math.round(full.height * scale) }
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function preparePopout(view: Window, kind: Popout['kind'], host: SimHost): Popout {
|
|
143
|
+
const document = view.document
|
|
144
|
+
document.title = window.document.title
|
|
145
|
+
// The app finds the simulator on its parent's window, which is now this one.
|
|
146
|
+
view.__webSimHost = host
|
|
147
|
+
view.__webSimShell = window
|
|
148
|
+
for (const attribute of window.document.documentElement.attributes) {
|
|
149
|
+
document.documentElement.setAttribute(attribute.name, attribute.value)
|
|
150
|
+
}
|
|
151
|
+
const stopMirroring = mirrorStyles(window.document, document)
|
|
152
|
+
view.addEventListener('pagehide', stopMirroring)
|
|
153
|
+
document.body.replaceChildren()
|
|
154
|
+
const container = document.createElement('div')
|
|
155
|
+
container.className = 'ws-popout'
|
|
156
|
+
document.body.append(container)
|
|
157
|
+
installTooltips(document)
|
|
158
|
+
return { container, window: view, kind }
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Copies the shell's styles into the other window and keeps them in step: Vite adds and rewrites `<style>` tags
|
|
163
|
+
* as CSS changes.
|
|
164
|
+
*/
|
|
165
|
+
function mirrorStyles(from: Document, to: Document): () => void {
|
|
166
|
+
let copies: Node[] = []
|
|
167
|
+
const sync = () => {
|
|
168
|
+
for (const copy of copies) {
|
|
169
|
+
copy.parentNode?.removeChild(copy)
|
|
170
|
+
}
|
|
171
|
+
copies = [...from.head.querySelectorAll('style, link[rel="stylesheet"]')].map((node) => {
|
|
172
|
+
const copy = to.importNode(node, true)
|
|
173
|
+
if (copy instanceof to.defaultView!.HTMLLinkElement) {
|
|
174
|
+
// Resolved against the shell's address: the other window's own is about:blank.
|
|
175
|
+
copy.href = (node as HTMLLinkElement).href
|
|
176
|
+
}
|
|
177
|
+
to.head.append(copy)
|
|
178
|
+
return copy
|
|
179
|
+
})
|
|
180
|
+
}
|
|
181
|
+
sync()
|
|
182
|
+
const observer = new MutationObserver(sync)
|
|
183
|
+
observer.observe(from.head, { childList: true, subtree: true, characterData: true })
|
|
184
|
+
return () => observer.disconnect()
|
|
185
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A toolbar popover's open state: closed again by Escape or a press outside the element `root` is put on.
|
|
5
|
+
*/
|
|
6
|
+
export function usePopover() {
|
|
7
|
+
const [open, setOpen] = useState(false)
|
|
8
|
+
const root = useRef<HTMLDivElement>(null)
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
if (!open) {
|
|
11
|
+
return
|
|
12
|
+
}
|
|
13
|
+
const onPointer = (event: PointerEvent) => {
|
|
14
|
+
if (!root.current?.contains(event.target as Node)) {
|
|
15
|
+
setOpen(false)
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
const onKey = (event: KeyboardEvent) => event.key === 'Escape' && setOpen(false)
|
|
19
|
+
window.addEventListener('pointerdown', onPointer, true)
|
|
20
|
+
window.addEventListener('keydown', onKey)
|
|
21
|
+
return () => {
|
|
22
|
+
window.removeEventListener('pointerdown', onPointer, true)
|
|
23
|
+
window.removeEventListener('keydown', onKey)
|
|
24
|
+
}
|
|
25
|
+
}, [open])
|
|
26
|
+
return { open, setOpen, root }
|
|
27
|
+
}
|