@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.
Files changed (279) hide show
  1. package/README.md +13 -2
  2. package/bin/cli.ts +255 -0
  3. package/bin/web-sim.js +39 -0
  4. package/overrides/@backpackapp-io/react-native-toast/overrides.ts +15 -0
  5. package/overrides/@react-native-firebase/app/overrides.ts +34 -0
  6. package/overrides/@tiktok-for-business/react-native-sdk/NativeTiktokSdk.ts +29 -0
  7. package/overrides/@tiktok-for-business/react-native-sdk/overrides.ts +9 -0
  8. package/overrides/expo-application/ExpoApplication.ts +81 -0
  9. package/overrides/expo-application/overrides.ts +16 -0
  10. package/overrides/expo-background-task/ExpoBackgroundTaskModule.ts +30 -0
  11. package/overrides/expo-background-task/overrides.ts +9 -0
  12. package/overrides/expo-constants/ExponentConstants.ts +66 -0
  13. package/overrides/expo-constants/overrides.ts +9 -0
  14. package/overrides/expo-device/ExpoDevice.ts +104 -0
  15. package/overrides/expo-device/overrides.ts +9 -0
  16. package/overrides/expo-glass-effect/GlassContainer.tsx +9 -0
  17. package/overrides/expo-glass-effect/GlassView.tsx +95 -0
  18. package/overrides/expo-glass-effect/availability.ts +14 -0
  19. package/overrides/expo-glass-effect/overrides.ts +14 -0
  20. package/overrides/expo-haptics/Haptics.ts +67 -0
  21. package/overrides/expo-haptics/overrides.ts +5 -0
  22. package/overrides/expo-mail-composer/ExpoMailComposer.ts +40 -0
  23. package/overrides/expo-mail-composer/overrides.ts +9 -0
  24. package/overrides/expo-mesh-gradient/MeshGradientView.tsx +44 -0
  25. package/overrides/expo-mesh-gradient/overrides.ts +9 -0
  26. package/overrides/expo-modules-core/overrides.ts +12 -0
  27. package/overrides/expo-navigation-bar/ExpoNavigationBar.ts +29 -0
  28. package/overrides/expo-navigation-bar/NavigationBar.ts +43 -0
  29. package/overrides/expo-navigation-bar/overrides.ts +12 -0
  30. package/overrides/expo-router/NativeStackView.tsx +74 -0
  31. package/overrides/expo-router/NativeTabsView.tsx +263 -0
  32. package/overrides/expo-router/SafeAreaProvider.ts +6 -0
  33. package/overrides/expo-router/overrides.ts +23 -0
  34. package/overrides/expo-router/statusBarRequests.ts +51 -0
  35. package/overrides/expo-screen-orientation/ExpoScreenOrientation.ts +173 -0
  36. package/overrides/expo-screen-orientation/overrides.ts +21 -0
  37. package/overrides/expo-sharing/SharingNativeModule.ts +21 -0
  38. package/overrides/expo-sharing/overrides.ts +9 -0
  39. package/overrides/expo-sms/ExpoSMS.ts +24 -0
  40. package/overrides/expo-sms/overrides.ts +9 -0
  41. package/overrides/expo-splash-screen/SplashScreen.ts +25 -0
  42. package/overrides/expo-splash-screen/overrides.ts +5 -0
  43. package/overrides/expo-status-bar/StatusBar.tsx +44 -0
  44. package/overrides/expo-status-bar/overrides.ts +5 -0
  45. package/overrides/expo-store-review/ExpoStoreReview.ts +18 -0
  46. package/overrides/expo-store-review/overrides.ts +9 -0
  47. package/overrides/expo-symbols/SymbolView.tsx +192 -0
  48. package/overrides/expo-symbols/overrides.ts +9 -0
  49. package/overrides/expo-system-ui/ExpoSystemUI.ts +34 -0
  50. package/overrides/expo-system-ui/overrides.ts +9 -0
  51. package/overrides/expo-task-manager/ExpoTaskManager.ts +39 -0
  52. package/overrides/expo-task-manager/overrides.ts +9 -0
  53. package/overrides/expo-tracking-transparency/ExpoTrackingTransparency.ts +30 -0
  54. package/overrides/expo-tracking-transparency/overrides.ts +11 -0
  55. package/overrides/expo-video/ExpoVideo.ts +16 -0
  56. package/overrides/expo-video/overrides.ts +9 -0
  57. package/overrides/lottie-react-native/LottieView.tsx +87 -0
  58. package/overrides/lottie-react-native/overrides.ts +9 -0
  59. package/overrides/react-native-pager-view/PagerViewNativeComponent.tsx +254 -0
  60. package/overrides/react-native-pager-view/overrides.ts +9 -0
  61. package/overrides/react-native-reanimated/overrides.ts +28 -0
  62. package/overrides/react-native-safe-area-context/NativeSafeAreaProvider.tsx +40 -0
  63. package/overrides/react-native-safe-area-context/overrides.ts +12 -0
  64. package/overrides/react-native-share/NativeRNShare.ts +22 -0
  65. package/overrides/react-native-share/overrides.ts +12 -0
  66. package/overrides/react-native-share/platform.ts +14 -0
  67. package/overrides/react-native-turbo-image/TurboImage.tsx +101 -0
  68. package/overrides/react-native-turbo-image/overrides.ts +9 -0
  69. package/overrides/react-native-web/RefreshControl.tsx +57 -0
  70. package/overrides/react-native-web/overrides.ts +153 -0
  71. package/overrides/react-native-web/useElementLayout.ts +140 -0
  72. package/package.json +78 -4
  73. package/plugins/battery/overrides/expo-battery/ExpoBattery.ts +57 -0
  74. package/plugins/battery/overrides/expo-battery/overrides.ts +9 -0
  75. package/plugins/battery/plugin.ts +8 -0
  76. package/plugins/battery/shell.tsx +56 -0
  77. package/plugins/battery/state.ts +11 -0
  78. package/plugins/biometrics/overrides/expo-local-authentication/ExpoLocalAuthentication.ts +90 -0
  79. package/plugins/biometrics/overrides/expo-local-authentication/overrides.ts +11 -0
  80. package/plugins/biometrics/plugin.ts +8 -0
  81. package/plugins/biometrics/shell.tsx +88 -0
  82. package/plugins/biometrics/state.ts +41 -0
  83. package/plugins/brightness/app.ts +5 -0
  84. package/plugins/brightness/overrides/expo-brightness/Brightness.ts +98 -0
  85. package/plugins/brightness/overrides/expo-brightness/overrides.ts +10 -0
  86. package/plugins/brightness/plugin.ts +9 -0
  87. package/plugins/brightness/shell.tsx +55 -0
  88. package/plugins/brightness/state.ts +19 -0
  89. package/plugins/calendar/calendar.ts +299 -0
  90. package/plugins/calendar/overrides/expo-calendar/ExpoCalendar.ts +358 -0
  91. package/plugins/calendar/overrides/expo-calendar/LegacyExpoCalendar.ts +163 -0
  92. package/plugins/calendar/overrides/expo-calendar/overrides.ts +14 -0
  93. package/plugins/calendar/plugin.ts +8 -0
  94. package/plugins/calendar/shell.tsx +123 -0
  95. package/plugins/camera/overrides/expo-camera/ExpoCamera.tsx +269 -0
  96. package/plugins/camera/overrides/expo-camera/ExpoCameraManager.ts +56 -0
  97. package/plugins/camera/overrides/expo-camera/overrides.ts +12 -0
  98. package/plugins/camera/plugin.ts +9 -0
  99. package/plugins/camera/shell.tsx +107 -0
  100. package/plugins/camera/state.ts +61 -0
  101. package/plugins/clipboard/overrides/expo-clipboard/ExpoClipboard.ts +138 -0
  102. package/plugins/clipboard/overrides/expo-clipboard/overrides.ts +9 -0
  103. package/plugins/clipboard/plugin.ts +8 -0
  104. package/plugins/clipboard/shell.tsx +122 -0
  105. package/plugins/clipboard/state.ts +93 -0
  106. package/plugins/clock/app.ts +25 -0
  107. package/plugins/clock/plugin.ts +8 -0
  108. package/plugins/clock/shell.tsx +225 -0
  109. package/plugins/clock/state.ts +12 -0
  110. package/plugins/contacts/contacts.ts +125 -0
  111. package/plugins/contacts/overrides/expo-contacts/ExpoContacts.ts +153 -0
  112. package/plugins/contacts/overrides/expo-contacts/ExpoContactsNext.ts +232 -0
  113. package/plugins/contacts/overrides/expo-contacts/overrides.ts +12 -0
  114. package/plugins/contacts/plugin.ts +8 -0
  115. package/plugins/contacts/shell.tsx +109 -0
  116. package/plugins/home-indicator/app.ts +5 -0
  117. package/plugins/home-indicator/plugin.ts +9 -0
  118. package/plugins/home-indicator/requests.ts +48 -0
  119. package/plugins/home-indicator/shell.tsx +68 -0
  120. package/plugins/home-indicator/state.ts +31 -0
  121. package/plugins/keep-awake/app.ts +5 -0
  122. package/plugins/keep-awake/overrides/expo-keep-awake/ExpoKeepAwake.ts +27 -0
  123. package/plugins/keep-awake/overrides/expo-keep-awake/overrides.ts +9 -0
  124. package/plugins/keep-awake/plugin.ts +9 -0
  125. package/plugins/keep-awake/shell.tsx +80 -0
  126. package/plugins/keep-awake/state.ts +12 -0
  127. package/plugins/keyboard/Keyboard.tsx +354 -0
  128. package/plugins/keyboard/app.ts +90 -0
  129. package/plugins/keyboard/controller.ts +384 -0
  130. package/plugins/keyboard/fields.ts +244 -0
  131. package/plugins/keyboard/icons.tsx +283 -0
  132. package/plugins/keyboard/keyboard.css +362 -0
  133. package/plugins/keyboard/layouts/android.ts +175 -0
  134. package/plugins/keyboard/layouts/index.ts +10 -0
  135. package/plugins/keyboard/layouts/ios.ts +219 -0
  136. package/plugins/keyboard/layouts/types.ts +80 -0
  137. package/plugins/keyboard/overrides/react-native-keyboard-controller/KeyboardController.tsx +470 -0
  138. package/plugins/keyboard/overrides/react-native-keyboard-controller/overrides.ts +9 -0
  139. package/plugins/keyboard/plugin.ts +13 -0
  140. package/plugins/keyboard/shell.tsx +133 -0
  141. package/plugins/keyboard/state.ts +92 -0
  142. package/plugins/keyboard/suggestions.ts +63 -0
  143. package/plugins/keyboard/taps.ts +77 -0
  144. package/plugins/keyboard/typing.ts +112 -0
  145. package/plugins/locale/app.ts +232 -0
  146. package/plugins/locale/locale.ts +143 -0
  147. package/plugins/locale/overrides/expo-localization/ExpoLocalization.ts +41 -0
  148. package/plugins/locale/overrides/expo-localization/overrides.ts +9 -0
  149. package/plugins/locale/plugin.ts +9 -0
  150. package/plugins/locale/shell.tsx +147 -0
  151. package/plugins/locale/state.ts +36 -0
  152. package/plugins/location/geo.ts +56 -0
  153. package/plugins/location/overrides/expo-location/ExpoLocation.ts +235 -0
  154. package/plugins/location/overrides/expo-location/overrides.ts +11 -0
  155. package/plugins/location/places.ts +38 -0
  156. package/plugins/location/plugin.ts +8 -0
  157. package/plugins/location/shell.tsx +188 -0
  158. package/plugins/location/state.ts +51 -0
  159. package/plugins/motion/overrides/expo-sensors/DeviceSensor.ts +169 -0
  160. package/plugins/motion/overrides/expo-sensors/ExponentPedometer.ts +54 -0
  161. package/plugins/motion/overrides/expo-sensors/overrides.ts +14 -0
  162. package/plugins/motion/plugin.ts +8 -0
  163. package/plugins/motion/sensors.ts +116 -0
  164. package/plugins/motion/shell.tsx +136 -0
  165. package/plugins/motion/state.ts +46 -0
  166. package/plugins/network/app.ts +400 -0
  167. package/plugins/network/overrides/expo-network/ExpoNetwork.ts +51 -0
  168. package/plugins/network/overrides/expo-network/overrides.ts +9 -0
  169. package/plugins/network/plugin.ts +8 -0
  170. package/plugins/network/shell.tsx +67 -0
  171. package/plugins/network/state.ts +29 -0
  172. package/plugins/notifications/center.ts +255 -0
  173. package/plugins/notifications/content.ts +36 -0
  174. package/plugins/notifications/overrides/expo-notifications/NotificationsModule.ts +175 -0
  175. package/plugins/notifications/overrides/expo-notifications/channels.ts +108 -0
  176. package/plugins/notifications/overrides/expo-notifications/getDevicePushTokenAsync.ts +19 -0
  177. package/plugins/notifications/overrides/expo-notifications/getExpoPushTokenAsync.ts +16 -0
  178. package/plugins/notifications/overrides/expo-notifications/overrides.ts +36 -0
  179. package/plugins/notifications/plugin.ts +8 -0
  180. package/plugins/notifications/shell.tsx +192 -0
  181. package/plugins/notifications/state.ts +56 -0
  182. package/plugins/notifications/tokens.ts +47 -0
  183. package/plugins/notifications/trigger.ts +107 -0
  184. package/plugins/permissions/plugin.ts +6 -0
  185. package/plugins/permissions/shell.tsx +50 -0
  186. package/plugins/permissions/state.ts +38 -0
  187. package/plugins/photos/appLibrary.ts +114 -0
  188. package/plugins/photos/library.ts +322 -0
  189. package/plugins/photos/overrides/expo-image-picker/ExponentImagePicker.ts +153 -0
  190. package/plugins/photos/overrides/expo-image-picker/overrides.ts +13 -0
  191. package/plugins/photos/overrides/expo-media-library/ExpoMediaLibrary.ts +235 -0
  192. package/plugins/photos/overrides/expo-media-library/NativeMediaLibraryModule.ts +318 -0
  193. package/plugins/photos/overrides/expo-media-library/overrides.ts +19 -0
  194. package/plugins/photos/plugin.ts +8 -0
  195. package/plugins/photos/samples.ts +367 -0
  196. package/plugins/photos/shell.tsx +164 -0
  197. package/plugins/photos/state.ts +88 -0
  198. package/plugins/storage/files.ts +351 -0
  199. package/plugins/storage/overrides/expo-file-system/ExpoFileSystem.ts +770 -0
  200. package/plugins/storage/overrides/expo-file-system/ExponentFileSystem.ts +196 -0
  201. package/plugins/storage/overrides/expo-file-system/overrides.ts +13 -0
  202. package/plugins/storage/overrides/expo-secure-store/ExpoSecureStore.ts +75 -0
  203. package/plugins/storage/overrides/expo-secure-store/overrides.ts +9 -0
  204. package/plugins/storage/plugin.ts +8 -0
  205. package/plugins/storage/reset.ts +136 -0
  206. package/plugins/storage/secure.ts +71 -0
  207. package/plugins/storage/shell.tsx +365 -0
  208. package/plugins/storage/state.ts +13 -0
  209. package/src/client/app/appDocument.ts +113 -0
  210. package/src/client/app/appFocus.ts +97 -0
  211. package/src/client/app/console.ts +118 -0
  212. package/src/client/app/fonts.ts +59 -0
  213. package/src/client/app/main.tsx +99 -0
  214. package/src/client/app/routeContext.ts +45 -0
  215. package/src/client/app/touch.ts +640 -0
  216. package/src/client/entry.ts +20 -0
  217. package/src/client/modules.d.ts +8 -0
  218. package/src/client/react-native/app.ts +57 -0
  219. package/src/client/react-native/deep.ts +13 -0
  220. package/src/client/react-native/keyboard.tsx +192 -0
  221. package/src/client/react-native/library.ts +256 -0
  222. package/src/client/shell/Cookies.tsx +95 -0
  223. package/src/client/shell/LogDrawer.tsx +415 -0
  224. package/src/client/shell/PhotoPicker.tsx +174 -0
  225. package/src/client/shell/PluginOverlays.tsx +42 -0
  226. package/src/client/shell/PluginPanels.tsx +82 -0
  227. package/src/client/shell/ResetApp.tsx +87 -0
  228. package/src/client/shell/Resizer.tsx +99 -0
  229. package/src/client/shell/Shell.tsx +531 -0
  230. package/src/client/shell/SystemBars.tsx +376 -0
  231. package/src/client/shell/SystemCamera.tsx +181 -0
  232. package/src/client/shell/SystemOverlay.tsx +422 -0
  233. package/src/client/shell/SystemSheets.tsx +462 -0
  234. package/src/client/shell/backdrop.ts +229 -0
  235. package/src/client/shell/control.ts +91 -0
  236. package/src/client/shell/events.ts +226 -0
  237. package/src/client/shell/geometry.ts +68 -0
  238. package/src/client/shell/host.ts +492 -0
  239. package/src/client/shell/icons.tsx +34 -0
  240. package/src/client/shell/logFeed.ts +102 -0
  241. package/src/client/shell/main.tsx +22 -0
  242. package/src/client/shell/permissionCopy.ts +169 -0
  243. package/src/client/shell/plugins.ts +266 -0
  244. package/src/client/shell/popout.ts +185 -0
  245. package/src/client/shell/popover.ts +27 -0
  246. package/src/client/shell/shell.css +878 -0
  247. package/src/client/shell/statusTime.ts +31 -0
  248. package/src/client/shell/tooltips.ts +104 -0
  249. package/src/client/virtual.d.ts +21 -0
  250. package/src/devices/catalog.ts +223 -0
  251. package/src/devices/types.ts +83 -0
  252. package/src/node/appInfo.ts +186 -0
  253. package/src/node/appSource.ts +138 -0
  254. package/src/node/config.ts +180 -0
  255. package/src/node/control.ts +144 -0
  256. package/src/node/doctor.ts +199 -0
  257. package/src/node/events/cli.ts +206 -0
  258. package/src/node/events/shared.ts +238 -0
  259. package/src/node/events/store.ts +266 -0
  260. package/src/node/index.ts +5 -0
  261. package/src/node/overrides/define.ts +56 -0
  262. package/src/node/overrides/packages.ts +136 -0
  263. package/src/node/overrides/platformPatch.ts +56 -0
  264. package/src/node/overrides/plugin.ts +166 -0
  265. package/src/node/overrides/registry.ts +258 -0
  266. package/src/node/paths.ts +18 -0
  267. package/src/node/platform.ts +171 -0
  268. package/src/node/plugins.ts +98 -0
  269. package/src/node/proxy.ts +421 -0
  270. package/src/node/server.ts +253 -0
  271. package/src/plugin/index.ts +33 -0
  272. package/src/plugin/types.ts +127 -0
  273. package/src/plugin/ui.tsx +562 -0
  274. package/src/runtime/clock.ts +112 -0
  275. package/src/runtime/draw.ts +196 -0
  276. package/src/runtime/index.ts +513 -0
  277. package/src/runtime/media.ts +379 -0
  278. package/src/runtime/records.ts +76 -0
  279. package/src/runtime/timezone.ts +81 -0
@@ -0,0 +1,379 @@
1
+ import type { Orientation } from '../devices/types.ts'
2
+ import { circle, cloud, grain, hills, linear, loadImage, rng, shadowed, tree, vignette, wood } from './draw.ts'
3
+
4
+ /**
5
+ * The simulated camera's feed, and the image helpers the camera and the photo modules share. A feed is a drawn
6
+ * scene, plainly not a real photo, that moves a little as a hand-held phone would; or an image the user gave it.
7
+ */
8
+
9
+ export type CameraFacing = 'front' | 'back'
10
+
11
+ export type CapturedPhoto = { uri: string; width: number; height: number }
12
+
13
+ /** The drawn scenes a camera can look at. */
14
+ export const CAMERA_SCENES = {
15
+ park: 'Park',
16
+ selfie: 'Indoor figure',
17
+ desk: 'Desk with a document',
18
+ qr: 'QR code on a table',
19
+ dark: 'Dark room',
20
+ } as const
21
+
22
+ export type CameraScene = keyof typeof CAMERA_SCENES
23
+
24
+ /** What a camera sees: a drawn scene (the QR scene draws `code`), or an image as a data URL. */
25
+ export type CameraFeed = { scene: CameraScene; code?: string } | { image: string }
26
+
27
+ /** What the camera's light is doing while it draws: the torch lights up a dark scene. */
28
+ export type FrameOptions = { zoom?: number; torch?: boolean }
29
+
30
+ /**
31
+ * One frame of the camera feed at time `t` (seconds). The preview of the front camera is mirrored by whoever draws
32
+ * it, as phones mirror it.
33
+ */
34
+ export function drawCameraFrame(ctx: CanvasRenderingContext2D, width: number, height: number, t: number, feed: CameraFeed, options: FrameOptions = {}) {
35
+ ctx.save()
36
+ const sway = Math.min(width, height) * 0.006
37
+ const scale = 1.04 + (options.zoom ?? 0) * 3
38
+ ctx.translate(width / 2 + Math.sin(t * 0.9) * sway, height / 2 + Math.cos(t * 1.3) * sway)
39
+ ctx.scale(scale, scale)
40
+ ctx.translate(-width / 2, -height / 2)
41
+ if ('image' in feed) {
42
+ drawImageFeed(ctx, width, height, feed.image)
43
+ } else {
44
+ switch (feed.scene) {
45
+ case 'park':
46
+ drawPark(ctx, width, height, t)
47
+ break
48
+ case 'selfie':
49
+ drawSelfie(ctx, width, height, t)
50
+ break
51
+ case 'desk':
52
+ drawDesk(ctx, width, height)
53
+ break
54
+ case 'qr':
55
+ drawQrTable(ctx, width, height, feed.code ?? '')
56
+ break
57
+ case 'dark':
58
+ drawDarkRoom(ctx, width, height, t, options.torch ?? false)
59
+ break
60
+ }
61
+ }
62
+ ctx.restore()
63
+ vignette(ctx, width, height, 0.35)
64
+ const dark = 'scene' in feed && feed.scene === 'dark' && !options.torch
65
+ grain(ctx, width, height, Math.floor(t * 24), dark ? 0.16 : 0.05)
66
+ // A mark in the corner, so a captured frame is never taken for a real photo.
67
+ const size = Math.max(9, Math.round(Math.min(width, height) * 0.022))
68
+ ctx.font = `600 ${size}px -apple-system, system-ui, sans-serif`
69
+ ctx.fillStyle = dark ? 'rgba(255, 255, 255, 0.25)' : 'rgba(255, 255, 255, 0.55)'
70
+ ctx.textAlign = 'right'
71
+ ctx.textBaseline = 'bottom'
72
+ ctx.fillText('web-sim', width - size, height - size)
73
+ }
74
+
75
+ /**
76
+ * Takes a picture: the current frame, in the device's orientation, as a JPEG data URL. The front camera's picture
77
+ * is not mirrored unless `mirror` is set, as on the device.
78
+ */
79
+ export function capturePhoto(options: { facing: CameraFacing; feed: CameraFeed; orientation?: Orientation; mirror?: boolean; quality?: number } & FrameOptions): CapturedPhoto {
80
+ const [long, short] = options.facing === 'back' ? [2016, 1512] : [1440, 1080]
81
+ const portrait = options.orientation !== 'landscape'
82
+ const canvas = document.createElement('canvas')
83
+ canvas.width = portrait ? short : long
84
+ canvas.height = portrait ? long : short
85
+ const ctx = canvas.getContext('2d')!
86
+ if (options.mirror) {
87
+ ctx.translate(canvas.width, 0)
88
+ ctx.scale(-1, 1)
89
+ }
90
+ drawCameraFrame(ctx, canvas.width, canvas.height, performance.now() / 1000, options.feed, options)
91
+ const quality = Math.min(Math.max(options.quality ?? 0.9, 0.1), 0.92)
92
+ return { uri: canvas.toDataURL('image/jpeg', quality), width: canvas.width, height: canvas.height }
93
+ }
94
+
95
+ /**
96
+ * Decoded images for image feeds, by data URL, so each frame draws without decoding again. A cache only: an image
97
+ * not decoded yet draws black for a frame or two.
98
+ */
99
+ const decoded = new Map<string, HTMLImageElement | null>()
100
+
101
+ function drawImageFeed(ctx: CanvasRenderingContext2D, w: number, h: number, uri: string) {
102
+ ctx.fillStyle = '#000'
103
+ ctx.fillRect(0, 0, w, h)
104
+ const image = decoded.get(uri)
105
+ if (image === undefined) {
106
+ if (decoded.size > 8) {
107
+ decoded.clear()
108
+ }
109
+ decoded.set(uri, null)
110
+ loadImage(uri).then(
111
+ (loaded) => decoded.set(uri, loaded),
112
+ () => undefined,
113
+ )
114
+ return
115
+ }
116
+ if (image === null) {
117
+ return
118
+ }
119
+ // Fills the frame, cropping the image about its centre, as a camera's preview fills the screen.
120
+ const scale = Math.max(w / image.naturalWidth, h / image.naturalHeight)
121
+ const dw = image.naturalWidth * scale
122
+ const dh = image.naturalHeight * scale
123
+ ctx.drawImage(image, (w - dw) / 2, (h - dh) / 2, dw, dh)
124
+ }
125
+
126
+ function drawPark(ctx: CanvasRenderingContext2D, w: number, h: number, t: number) {
127
+ const horizon = h * 0.58
128
+ linear(ctx, 0, 0, 0, horizon, ['#5b9be0', '#a7cdf0', '#e3eef6'])
129
+ ctx.fillRect(0, 0, w, horizon + 2)
130
+ // Clouds drift across.
131
+ ctx.fillStyle = 'rgba(255, 255, 255, 0.85)'
132
+ for (let i = 0; i < 4; i++) {
133
+ const span = w * 1.6
134
+ const x = ((i * 0.31 * span + t * w * 0.012 * (1 + i * 0.3)) % span) - w * 0.3
135
+ cloud(ctx, x, h * (0.1 + i * 0.07), w * (0.12 + (i % 2) * 0.05))
136
+ }
137
+ hills(ctx, w, horizon, h * 0.08, '#8fae9a', 3)
138
+ hills(ctx, w, horizon + h * 0.03, h * 0.05, '#6f9a6c', 7)
139
+ linear(ctx, 0, horizon, 0, h, ['#6c9c4f', '#4f7f37', '#3a6328'])
140
+ ctx.fillRect(0, horizon + h * 0.03, w, h)
141
+ // A path into the distance.
142
+ ctx.fillStyle = '#c9b48c'
143
+ ctx.beginPath()
144
+ ctx.moveTo(w * 0.47, horizon + h * 0.03)
145
+ ctx.lineTo(w * 0.53, horizon + h * 0.03)
146
+ ctx.lineTo(w * 0.85, h)
147
+ ctx.lineTo(w * 0.25, h)
148
+ ctx.closePath()
149
+ ctx.fill()
150
+ // Trees, the near one swaying.
151
+ tree(ctx, w * 0.18, horizon + h * 0.1, h * 0.32, Math.sin(t * 1.1) * 0.015)
152
+ tree(ctx, w * 0.82, horizon + h * 0.05, h * 0.2, Math.sin(t * 0.8 + 1) * 0.01)
153
+ tree(ctx, w * 0.66, horizon + h * 0.035, h * 0.12, 0)
154
+ // A bench.
155
+ ctx.fillStyle = '#6b4a2f'
156
+ ctx.fillRect(w * 0.6, h * 0.74, w * 0.22, h * 0.018)
157
+ ctx.fillRect(w * 0.6, h * 0.71, w * 0.22, h * 0.012)
158
+ ctx.fillRect(w * 0.62, h * 0.74, w * 0.012, h * 0.05)
159
+ ctx.fillRect(w * 0.79, h * 0.74, w * 0.012, h * 0.05)
160
+ }
161
+
162
+ function drawSelfie(ctx: CanvasRenderingContext2D, w: number, h: number, t: number) {
163
+ linear(ctx, 0, 0, w, h, ['#e9d9c4', '#d8c3a8', '#b99c7d'])
164
+ ctx.fillRect(0, 0, w, h)
165
+ // A window behind, bright.
166
+ ctx.fillStyle = 'rgba(255, 252, 240, 0.85)'
167
+ ctx.fillRect(w * 0.62, h * 0.08, w * 0.32, h * 0.34)
168
+ ctx.fillStyle = '#c4ab8c'
169
+ ctx.fillRect(w * 0.775, h * 0.08, w * 0.012, h * 0.34)
170
+ ctx.fillRect(w * 0.62, h * 0.245, w * 0.32, h * 0.008)
171
+ // A plant on a shelf.
172
+ ctx.fillStyle = '#9a6b4c'
173
+ ctx.fillRect(w * 0.05, h * 0.44, w * 0.14, h * 0.06)
174
+ ctx.fillStyle = '#4f7d4a'
175
+ for (let i = 0; i < 6; i++) {
176
+ ctx.beginPath()
177
+ ctx.ellipse(w * (0.08 + i * 0.016), h * (0.41 - (i % 3) * 0.03), w * 0.025, h * 0.045, (i - 2.5) * 0.35, 0, Math.PI * 2)
178
+ ctx.fill()
179
+ }
180
+ // The figure: head and shoulders, breathing and bobbing a little.
181
+ const bob = Math.sin(t * 1.6) * h * 0.006
182
+ const cx = w * 0.5 + Math.sin(t * 0.7) * w * 0.01
183
+ ctx.fillStyle = '#3c5a7a'
184
+ ctx.beginPath()
185
+ ctx.ellipse(cx, h * 1.02 + bob, w * 0.46, h * 0.3, 0, Math.PI, 0)
186
+ ctx.fill()
187
+ ctx.fillStyle = '#c99b7b'
188
+ ctx.fillRect(cx - w * 0.07, h * 0.6 + bob, w * 0.14, h * 0.14)
189
+ ctx.beginPath()
190
+ ctx.ellipse(cx, h * 0.5 + bob, w * 0.17, h * 0.15, 0, 0, Math.PI * 2)
191
+ ctx.fill()
192
+ ctx.fillStyle = '#3b2a20'
193
+ ctx.beginPath()
194
+ ctx.ellipse(cx, h * 0.42 + bob, w * 0.18, h * 0.09, 0, Math.PI, 0)
195
+ ctx.fill()
196
+ ctx.fillStyle = 'rgba(50, 35, 25, 0.75)'
197
+ ctx.beginPath()
198
+ ctx.arc(cx - w * 0.06, h * 0.5 + bob, w * 0.012, 0, Math.PI * 2)
199
+ ctx.arc(cx + w * 0.06, h * 0.5 + bob, w * 0.012, 0, Math.PI * 2)
200
+ ctx.fill()
201
+ ctx.strokeStyle = 'rgba(120, 60, 50, 0.7)'
202
+ ctx.lineWidth = Math.max(1, w * 0.008)
203
+ ctx.beginPath()
204
+ ctx.arc(cx, h * 0.555 + bob, w * 0.05, 0.2 * Math.PI, 0.8 * Math.PI)
205
+ ctx.stroke()
206
+ }
207
+
208
+ function drawDesk(ctx: CanvasRenderingContext2D, w: number, h: number) {
209
+ wood(ctx, w, h, rng(31))
210
+ // A sheet of paper, a little askew, with a heading, lines of text and a signature.
211
+ const pw = Math.min(w * 0.72, h * 0.55)
212
+ const ph = pw * 1.414
213
+ ctx.save()
214
+ ctx.translate(w * 0.5, h * 0.48)
215
+ ctx.rotate(-0.06)
216
+ shadowed(ctx, () => {
217
+ ctx.fillStyle = '#fbfaf6'
218
+ ctx.fillRect(-pw / 2, -ph / 2, pw, ph)
219
+ })
220
+ const margin = pw * 0.1
221
+ const random = rng(7)
222
+ ctx.fillStyle = '#2b2b2b'
223
+ ctx.fillRect(-pw / 2 + margin, -ph / 2 + margin, pw * 0.45, ph * 0.025)
224
+ ctx.fillStyle = '#9a9a9a'
225
+ ctx.fillRect(-pw / 2 + margin, -ph / 2 + margin + ph * 0.04, pw * 0.3, ph * 0.012)
226
+ ctx.fillStyle = '#5a5a5a'
227
+ for (let line = 0; line < 22; line++) {
228
+ const y = -ph / 2 + margin + ph * 0.1 + line * ph * 0.03
229
+ if (line % 7 === 6) {
230
+ continue
231
+ }
232
+ const length = line % 7 === 5 ? 0.3 + random() * 0.3 : 0.75 + random() * 0.05
233
+ ctx.fillRect(-pw / 2 + margin, y, (pw - margin * 2) * length, ph * 0.008)
234
+ }
235
+ ctx.strokeStyle = '#1d3f8f'
236
+ ctx.lineWidth = Math.max(1, pw * 0.006)
237
+ ctx.beginPath()
238
+ const sy = ph / 2 - margin * 1.2
239
+ ctx.moveTo(-pw / 2 + margin, sy)
240
+ for (let i = 0; i <= 12; i++) {
241
+ ctx.lineTo(-pw / 2 + margin + i * pw * 0.025, sy + Math.sin(i * 1.7) * ph * 0.012)
242
+ }
243
+ ctx.stroke()
244
+ ctx.restore()
245
+ // A pen beside it, and the rim of a mug in the corner.
246
+ ctx.save()
247
+ ctx.translate(w * 0.86, h * 0.62)
248
+ ctx.rotate(0.5)
249
+ ctx.fillStyle = '#20232a'
250
+ ctx.fillRect(-w * 0.012, -h * 0.13, w * 0.024, h * 0.26)
251
+ ctx.fillStyle = '#c9a54a'
252
+ ctx.fillRect(-w * 0.012, -h * 0.13, w * 0.024, h * 0.02)
253
+ ctx.restore()
254
+ circle(ctx, w * 0.06, h * 0.08, Math.min(w, h) * 0.16, '#e6e2dc')
255
+ circle(ctx, w * 0.06, h * 0.08, Math.min(w, h) * 0.13, '#4a2c18')
256
+ }
257
+
258
+ /** A deterministic QR-like pattern for `code`: finder squares, timing lines and modules from its hash. */
259
+ function drawQrCode(ctx: CanvasRenderingContext2D, x: number, y: number, size: number, code: string) {
260
+ const n = 29
261
+ const cell = size / n
262
+ let seed = 7
263
+ for (let i = 0; i < code.length; i++) {
264
+ seed = (Math.imul(seed, 31) + code.charCodeAt(i)) | 0
265
+ }
266
+ const random = rng(seed)
267
+ const finder = (r: number, c: number) => (r < 8 && c < 8) || (r < 8 && c >= n - 8) || (r >= n - 8 && c < 8)
268
+ ctx.fillStyle = '#111'
269
+ for (let r = 0; r < n; r++) {
270
+ for (let c = 0; c < n; c++) {
271
+ const on = finder(r, c) ? false : r === 6 || c === 6 ? (r + c) % 2 === 0 : random() > 0.52
272
+ if (on) {
273
+ ctx.fillRect(x + c * cell, y + r * cell, cell + 0.5, cell + 0.5)
274
+ }
275
+ }
276
+ }
277
+ for (const [r, c] of [
278
+ [0, 0],
279
+ [0, n - 7],
280
+ [n - 7, 0],
281
+ ]) {
282
+ ctx.fillRect(x + c * cell, y + r * cell, cell * 7, cell * 7)
283
+ ctx.fillStyle = '#fff'
284
+ ctx.fillRect(x + (c + 1) * cell, y + (r + 1) * cell, cell * 5, cell * 5)
285
+ ctx.fillStyle = '#111'
286
+ ctx.fillRect(x + (c + 2) * cell, y + (r + 2) * cell, cell * 3, cell * 3)
287
+ }
288
+ }
289
+
290
+ function drawQrTable(ctx: CanvasRenderingContext2D, w: number, h: number, code: string) {
291
+ linear(ctx, 0, 0, w, h, ['#d9d4cc', '#c7c0b5', '#b3ab9f'])
292
+ ctx.fillRect(0, 0, w, h)
293
+ // A table tent card with the code, as on a café table.
294
+ const size = Math.min(w, h) * 0.5
295
+ const cx = w / 2
296
+ const cy = h * 0.47
297
+ shadowed(ctx, () => {
298
+ ctx.fillStyle = '#ffffff'
299
+ ctx.beginPath()
300
+ ctx.roundRect(cx - size * 0.62, cy - size * 0.75, size * 1.24, size * 1.5, size * 0.06)
301
+ ctx.fill()
302
+ })
303
+ drawQrCode(ctx, cx - size / 2, cy - size / 2 - size * 0.08, size, code)
304
+ ctx.fillStyle = '#333'
305
+ ctx.font = `600 ${Math.round(size * 0.07)}px -apple-system, system-ui, sans-serif`
306
+ ctx.textAlign = 'center'
307
+ ctx.textBaseline = 'middle'
308
+ ctx.fillText('Scan me', cx, cy + size * 0.6)
309
+ // A glass beside it.
310
+ circle(ctx, w * 0.12, h * 0.85, Math.min(w, h) * 0.12, 'rgba(255, 255, 255, 0.45)')
311
+ circle(ctx, w * 0.12, h * 0.85, Math.min(w, h) * 0.1, 'rgba(210, 230, 240, 0.4)')
312
+ }
313
+
314
+ function drawDarkRoom(ctx: CanvasRenderingContext2D, w: number, h: number, t: number, torch: boolean) {
315
+ ctx.fillStyle = torch ? '#2a2622' : '#060606'
316
+ ctx.fillRect(0, 0, w, h)
317
+ // A window's faint outline, and a standby light.
318
+ ctx.strokeStyle = torch ? 'rgba(120, 130, 150, 0.4)' : 'rgba(70, 80, 110, 0.35)'
319
+ ctx.lineWidth = Math.max(2, w * 0.01)
320
+ ctx.strokeRect(w * 0.58, h * 0.12, w * 0.3, h * 0.28)
321
+ circle(ctx, w * 0.2, h * 0.7, Math.max(2, w * 0.006), `rgba(255, 60, 40, ${0.5 + Math.sin(t * 2) * 0.3})`)
322
+ if (!torch) {
323
+ return
324
+ }
325
+ // The torch lights what is in front of the phone: a chair and the floor, fading off at the edges.
326
+ ctx.fillStyle = '#5b4636'
327
+ ctx.fillRect(w * 0.3, h * 0.42, w * 0.4, h * 0.05)
328
+ ctx.fillRect(w * 0.3, h * 0.18, w * 0.05, h * 0.3)
329
+ ctx.fillRect(w * 0.32, h * 0.47, w * 0.03, h * 0.25)
330
+ ctx.fillRect(w * 0.65, h * 0.47, w * 0.03, h * 0.25)
331
+ const light = ctx.createRadialGradient(w / 2, h / 2, 0, w / 2, h / 2, Math.max(w, h) * 0.6)
332
+ light.addColorStop(0, 'rgba(255, 248, 230, 0.35)')
333
+ light.addColorStop(1, 'rgba(0, 0, 0, 0.6)')
334
+ ctx.fillStyle = light
335
+ ctx.fillRect(0, 0, w, h)
336
+ }
337
+
338
+ /**
339
+ * Crops an image to an aspect ratio about its centre, as the system's crop step does by default.
340
+ */
341
+ export async function cropToAspect(photo: CapturedPhoto, aspect: [number, number], quality = 0.9): Promise<CapturedPhoto> {
342
+ const target = aspect[0] / aspect[1]
343
+ let width = photo.width
344
+ let height = photo.height
345
+ if (width / height > target) {
346
+ width = Math.round(height * target)
347
+ } else {
348
+ height = Math.round(width / target)
349
+ }
350
+ if (width === photo.width && height === photo.height) {
351
+ return photo
352
+ }
353
+ const canvas = document.createElement('canvas')
354
+ canvas.width = width
355
+ canvas.height = height
356
+ const image = await loadImage(photo.uri)
357
+ canvas.getContext('2d')!.drawImage(image, (photo.width - width) / 2, (photo.height - height) / 2, width, height, 0, 0, width, height)
358
+ return { uri: canvas.toDataURL('image/jpeg', Math.min(quality, 0.92)), width, height }
359
+ }
360
+
361
+ /** Re-encodes an image as a JPEG data URL no larger than `maxSize` on its long side. */
362
+ export async function downscaleImage(uri: string, maxSize: number, quality = 0.85): Promise<CapturedPhoto> {
363
+ const image = await loadImage(uri)
364
+ const scale = Math.min(1, maxSize / Math.max(image.naturalWidth, image.naturalHeight))
365
+ const canvas = document.createElement('canvas')
366
+ canvas.width = Math.max(1, Math.round(image.naturalWidth * scale))
367
+ canvas.height = Math.max(1, Math.round(image.naturalHeight * scale))
368
+ canvas.getContext('2d')!.drawImage(image, 0, 0, canvas.width, canvas.height)
369
+ return { uri: canvas.toDataURL('image/jpeg', quality), width: canvas.width, height: canvas.height }
370
+ }
371
+
372
+ /** The bytes a data URL holds, and its base64 payload. */
373
+ export function dataUrlInfo(uri: string): { base64: string; size: number; mimeType: string } {
374
+ const comma = uri.indexOf(',')
375
+ const base64 = uri.slice(comma + 1)
376
+ const mimeType = uri.slice(5, uri.indexOf(';'))
377
+ const padding = base64.endsWith('==') ? 2 : base64.endsWith('=') ? 1 : 0
378
+ return { base64, size: Math.floor((base64.length * 3) / 4) - padding, mimeType }
379
+ }
@@ -0,0 +1,76 @@
1
+ /**
2
+ * A small dataset a simulated module keeps for the app, such as contacts or calendar events: built-in records, plus
3
+ * the changes made to them. The built-in records are made fresh on every read, so ones dated relative to today stay
4
+ * current; only the changes are stored, wherever `storage` keeps them (a plugin's state).
5
+ */
6
+ export type SimRecords<T extends { id: string }> = {
7
+ all(): T[]
8
+ get(id: string): T | null
9
+ /** Adds or replaces a record. */
10
+ put(record: T): T
11
+ remove(id: string): void
12
+ /** An id no record has yet. */
13
+ newId(prefix?: string): string
14
+ /** Back to the built-in records. */
15
+ reset(): void
16
+ }
17
+
18
+ /** The changes to the built-in records: replaced or added records, removed ids, and the order records were added in. */
19
+ export type RecordChanges<T> = { put: Record<string, T>; removed: string[]; order: string[] }
20
+
21
+ export function noRecordChanges<T>(): RecordChanges<T> {
22
+ return { put: {}, removed: [], order: [] }
23
+ }
24
+
25
+ export type RecordStorage<T> = { read(): RecordChanges<T> | undefined; write(changes: RecordChanges<T>): void }
26
+
27
+ export function simRecords<T extends { id: string }>(builtIn: () => T[], storage: RecordStorage<T>): SimRecords<T> {
28
+ // A copy, so changes are made to it and written back whole, never to the stored state in place.
29
+ const read = (): RecordChanges<T> => {
30
+ const stored = storage.read()
31
+ return stored === undefined ? noRecordChanges<T>() : (JSON.parse(JSON.stringify(stored)) as RecordChanges<T>)
32
+ }
33
+ const all = (): T[] => {
34
+ const changes = read()
35
+ const removed = new Set(changes.removed)
36
+ const base = builtIn()
37
+ const baseIds = new Set(base.map((record) => record.id))
38
+ const merged = base.filter((record) => !removed.has(record.id)).map((record) => changes.put[record.id] ?? record)
39
+ const added = changes.order.filter((id) => !baseIds.has(id) && !removed.has(id) && changes.put[id] !== undefined)
40
+ return [...merged, ...added.map((id) => changes.put[id])]
41
+ }
42
+ return {
43
+ all,
44
+ get: (id) => all().find((record) => record.id === id) ?? null,
45
+ put(record) {
46
+ const changes = read()
47
+ changes.put[record.id] = record
48
+ changes.removed = changes.removed.filter((id) => id !== record.id)
49
+ if (!changes.order.includes(record.id)) {
50
+ changes.order.push(record.id)
51
+ }
52
+ storage.write(changes)
53
+ return record
54
+ },
55
+ remove(id) {
56
+ const changes = read()
57
+ delete changes.put[id]
58
+ changes.order = changes.order.filter((other) => other !== id)
59
+ if (!changes.removed.includes(id)) {
60
+ changes.removed.push(id)
61
+ }
62
+ storage.write(changes)
63
+ },
64
+ newId(prefix = '') {
65
+ const ids = new Set([...builtIn().map((record) => record.id), ...Object.keys(read().put)])
66
+ let n = ids.size + 1
67
+ while (ids.has(`${prefix}${n}`)) {
68
+ n++
69
+ }
70
+ return `${prefix}${n}`
71
+ },
72
+ reset() {
73
+ storage.write(noRecordChanges<T>())
74
+ },
75
+ }
76
+ }
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Wall-clock time in a named time zone, without the browser's own zone: for the phone's time zone, which the locale
3
+ * plugin sets, where it differs from the computer's.
4
+ */
5
+
6
+ const formatters = new Map<string, Intl.DateTimeFormat>()
7
+ const offsets = new Map<string, number>()
8
+ /** Zones change their offset on the quarter hour at the finest, so an offset holds for its quarter hour. */
9
+ const QUARTER_HOUR = 15 * 60 * 1000
10
+
11
+ function formatter(timeZone: string): Intl.DateTimeFormat {
12
+ let format = formatters.get(timeZone)
13
+ if (format === undefined) {
14
+ format = new Intl.DateTimeFormat('en-US', {
15
+ timeZone,
16
+ hourCycle: 'h23',
17
+ year: 'numeric',
18
+ month: 'numeric',
19
+ day: 'numeric',
20
+ hour: 'numeric',
21
+ minute: 'numeric',
22
+ second: 'numeric',
23
+ era: 'short',
24
+ })
25
+ formatters.set(timeZone, format)
26
+ }
27
+ return format
28
+ }
29
+
30
+ /** The zone's offset from UTC at an instant, in milliseconds east of Greenwich. */
31
+ export function zoneOffset(timeZone: string, time: number): number {
32
+ if (!Number.isFinite(time)) {
33
+ return 0
34
+ }
35
+ const key = `${timeZone}\0${Math.floor(time / QUARTER_HOUR)}`
36
+ const cached = offsets.get(key)
37
+ if (cached !== undefined) {
38
+ return cached
39
+ }
40
+ const parts: Record<string, string> = {}
41
+ for (const part of formatter(timeZone).formatToParts(time)) {
42
+ parts[part.type] = part.value
43
+ }
44
+ const year = parts.era === 'BC' || parts.era === 'B' ? 1 - Number(parts.year) : Number(parts.year)
45
+ const wall = Date.UTC(year, Number(parts.month) - 1, Number(parts.day), Number(parts.hour), Number(parts.minute), Number(parts.second))
46
+ const offset = Math.round((wall - (time - (((time % 1000) + 1000) % 1000))) / 60000) * 60000
47
+ if (offsets.size > 5000) {
48
+ offsets.clear()
49
+ }
50
+ offsets.set(key, offset)
51
+ return offset
52
+ }
53
+
54
+ /** An instant's wall-clock time in the zone, as a timestamp to read with the `getUTC*` methods. */
55
+ export function toWallTime(timeZone: string, time: number): number {
56
+ return time + zoneOffset(timeZone, time)
57
+ }
58
+
59
+ /**
60
+ * The instant at which the zone's clocks show a wall-clock time (read as UTC). A time skipped by a change to summer
61
+ * time moves forward past the gap; one that happens twice is the first.
62
+ */
63
+ export function fromWallTime(timeZone: string, wall: number): number {
64
+ const first = wall - zoneOffset(timeZone, wall)
65
+ const second = wall - zoneOffset(timeZone, first)
66
+ if (first === second) {
67
+ return first
68
+ }
69
+ const third = wall - zoneOffset(timeZone, second)
70
+ return Math.min(second, third)
71
+ }
72
+
73
+ /** Whether the browser knows the zone. */
74
+ export function isTimeZone(timeZone: string): boolean {
75
+ try {
76
+ new Intl.DateTimeFormat('en-US', { timeZone })
77
+ return true
78
+ } catch {
79
+ return false
80
+ }
81
+ }