@ng-native/desktop 0.0.0-stage → 0.23.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 (393) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +51 -2
  3. package/app.plugin.cjs +17 -0
  4. package/bin/app-config.mjs +168 -0
  5. package/bin/autolink.mjs +224 -0
  6. package/bin/cli.mjs +158 -0
  7. package/bin/host.mjs +427 -0
  8. package/bin/ng-native-desktop.mjs +4 -0
  9. package/bin/package.mjs +971 -0
  10. package/bin/plist.mjs +68 -0
  11. package/bin/prebuilt.mjs +328 -0
  12. package/bin/toolchain.mjs +192 -0
  13. package/native/CMakeLists.txt +276 -0
  14. package/native/appkit/CMakeLists.txt +109 -0
  15. package/native/appkit/address-search.h +26 -0
  16. package/native/appkit/address-search.mm +277 -0
  17. package/native/appkit/animation-frames.h +14 -0
  18. package/native/appkit/animation-frames.mm +106 -0
  19. package/native/appkit/app-info.h +12 -0
  20. package/native/appkit/app-info.mm +98 -0
  21. package/native/appkit/app-updater.mm +128 -0
  22. package/native/appkit/app-windows.h +81 -0
  23. package/native/appkit/app-windows.mm +318 -0
  24. package/native/appkit/appkit-backend.h +73 -0
  25. package/native/appkit/appkit-backend.mm +276 -0
  26. package/native/appkit/apple-authentication.h +19 -0
  27. package/native/appkit/apple-authentication.mm +248 -0
  28. package/native/appkit/apple-button-view.h +10 -0
  29. package/native/appkit/apple-button-view.mm +37 -0
  30. package/native/appkit/apple-id-button.h +16 -0
  31. package/native/appkit/apple-id-button.mm +50 -0
  32. package/native/appkit/audio.h +16 -0
  33. package/native/appkit/audio.mm +275 -0
  34. package/native/appkit/battery.h +13 -0
  35. package/native/appkit/battery.mm +92 -0
  36. package/native/appkit/bookmarks.h +17 -0
  37. package/native/appkit/bookmarks.mm +145 -0
  38. package/native/appkit/calendar.h +25 -0
  39. package/native/appkit/calendar.mm +660 -0
  40. package/native/appkit/camera-view.h +15 -0
  41. package/native/appkit/camera-view.mm +130 -0
  42. package/native/appkit/camera.h +39 -0
  43. package/native/appkit/camera.mm +362 -0
  44. package/native/appkit/cg-canvas.h +17 -0
  45. package/native/appkit/cg-canvas.mm +518 -0
  46. package/native/appkit/component-view.h +17 -0
  47. package/native/appkit/crypto.h +19 -0
  48. package/native/appkit/crypto.mm +78 -0
  49. package/native/appkit/desktop-expo.h +29 -0
  50. package/native/appkit/desktop-expo.mm +385 -0
  51. package/native/appkit/expo-image-drawing.h +46 -0
  52. package/native/appkit/expo-image-drawing.mm +131 -0
  53. package/native/appkit/expo-image-view.h +15 -0
  54. package/native/appkit/expo-image-view.mm +182 -0
  55. package/native/appkit/expo-ui-renderer.h +68 -0
  56. package/native/appkit/expo-ui-renderer.mm +101 -0
  57. package/native/appkit/expo-ui-view.h +26 -0
  58. package/native/appkit/expo-ui-view.mm +134 -0
  59. package/native/appkit/expo-ui.swift +1284 -0
  60. package/native/appkit/file-system.h +16 -0
  61. package/native/appkit/file-system.mm +140 -0
  62. package/native/appkit/fonts.h +12 -0
  63. package/native/appkit/fonts.mm +48 -0
  64. package/native/appkit/geocoder.h +19 -0
  65. package/native/appkit/geocoder.mm +101 -0
  66. package/native/appkit/glass-effect.h +22 -0
  67. package/native/appkit/glass-effect.mm +63 -0
  68. package/native/appkit/glass-view.h +8 -0
  69. package/native/appkit/glass-view.mm +65 -0
  70. package/native/appkit/header-toolbar.h +60 -0
  71. package/native/appkit/header-toolbar.mm +629 -0
  72. package/native/appkit/host-view.h +54 -0
  73. package/native/appkit/host-view.mm +1200 -0
  74. package/native/appkit/image-manipulator.h +14 -0
  75. package/native/appkit/image-manipulator.mm +191 -0
  76. package/native/appkit/image-request.h +22 -0
  77. package/native/appkit/image-request.mm +62 -0
  78. package/native/appkit/image-view.h +8 -0
  79. package/native/appkit/image-view.mm +142 -0
  80. package/native/appkit/input-replay.h +25 -0
  81. package/native/appkit/input-replay.mm +418 -0
  82. package/native/appkit/language-model.h +14 -0
  83. package/native/appkit/language-model.mm +99 -0
  84. package/native/appkit/language-model.swift +472 -0
  85. package/native/appkit/local-authentication.h +15 -0
  86. package/native/appkit/local-authentication.mm +133 -0
  87. package/native/appkit/location.h +16 -0
  88. package/native/appkit/location.mm +214 -0
  89. package/native/appkit/main.mm +1515 -0
  90. package/native/appkit/map-view.h +15 -0
  91. package/native/appkit/map-view.mm +109 -0
  92. package/native/appkit/maps.h +35 -0
  93. package/native/appkit/maps.mm +343 -0
  94. package/native/appkit/menu-bar.h +22 -0
  95. package/native/appkit/menu-bar.mm +208 -0
  96. package/native/appkit/modal-focus.h +16 -0
  97. package/native/appkit/modal-focus.mm +26 -0
  98. package/native/appkit/modal-host-view.h +79 -0
  99. package/native/appkit/modal-host-view.mm +442 -0
  100. package/native/appkit/native-controls.h +22 -0
  101. package/native/appkit/native-controls.mm +431 -0
  102. package/native/appkit/network-state.h +12 -0
  103. package/native/appkit/network-state.mm +64 -0
  104. package/native/appkit/network.h +14 -0
  105. package/native/appkit/network.mm +396 -0
  106. package/native/appkit/notifications.h +19 -0
  107. package/native/appkit/notifications.mm +309 -0
  108. package/native/appkit/photo-library.h +33 -0
  109. package/native/appkit/photo-library.mm +548 -0
  110. package/native/appkit/pointer-buttons.h +32 -0
  111. package/native/appkit/presentation-label.h +15 -0
  112. package/native/appkit/presentation-label.mm +12 -0
  113. package/native/appkit/presented-screen.h +19 -0
  114. package/native/appkit/presented-screen.mm +181 -0
  115. package/native/appkit/printing.h +42 -0
  116. package/native/appkit/printing.mm +393 -0
  117. package/native/appkit/push.h +19 -0
  118. package/native/appkit/push.mm +81 -0
  119. package/native/appkit/red-box.h +26 -0
  120. package/native/appkit/red-box.mm +387 -0
  121. package/native/appkit/screen-capture.h +21 -0
  122. package/native/appkit/screen-capture.mm +92 -0
  123. package/native/appkit/scroll-view.h +13 -0
  124. package/native/appkit/scroll-view.mm +251 -0
  125. package/native/appkit/scrollers.h +25 -0
  126. package/native/appkit/scrollers.mm +43 -0
  127. package/native/appkit/search-field.h +34 -0
  128. package/native/appkit/search-field.mm +291 -0
  129. package/native/appkit/search-toolbar-item.h +19 -0
  130. package/native/appkit/search-toolbar-item.mm +269 -0
  131. package/native/appkit/share-picker.h +34 -0
  132. package/native/appkit/share-picker.mm +121 -0
  133. package/native/appkit/split-view.h +77 -0
  134. package/native/appkit/split-view.mm +1206 -0
  135. package/native/appkit/store-review.h +13 -0
  136. package/native/appkit/store-review.mm +28 -0
  137. package/native/appkit/surface-view.h +45 -0
  138. package/native/appkit/surface-view.mm +558 -0
  139. package/native/appkit/svg-view.h +17 -0
  140. package/native/appkit/svg-view.mm +57 -0
  141. package/native/appkit/symbol-drawing.h +24 -0
  142. package/native/appkit/symbol-drawing.mm +117 -0
  143. package/native/appkit/symbol-view.h +8 -0
  144. package/native/appkit/symbol-view.mm +33 -0
  145. package/native/appkit/system-access.h +34 -0
  146. package/native/appkit/system-access.mm +210 -0
  147. package/native/appkit/system.h +24 -0
  148. package/native/appkit/system.mm +403 -0
  149. package/native/appkit/test-launch.h +36 -0
  150. package/native/appkit/test-launch.mm +115 -0
  151. package/native/appkit/text-input-field.h +76 -0
  152. package/native/appkit/text-input-field.mm +444 -0
  153. package/native/appkit/text-layout.h +58 -0
  154. package/native/appkit/text-layout.mm +477 -0
  155. package/native/appkit/text-recognition.h +13 -0
  156. package/native/appkit/text-recognition.mm +102 -0
  157. package/native/appkit/tracking.h +13 -0
  158. package/native/appkit/tracking.mm +51 -0
  159. package/native/appkit/tray-and-dock.h +45 -0
  160. package/native/appkit/tray-and-dock.mm +130 -0
  161. package/native/appkit/undo.h +23 -0
  162. package/native/appkit/undo.mm +125 -0
  163. package/native/appkit/updates.h +17 -0
  164. package/native/appkit/updates.mm +71 -0
  165. package/native/appkit/video-view.h +10 -0
  166. package/native/appkit/video-view.mm +42 -0
  167. package/native/appkit/video.h +38 -0
  168. package/native/appkit/video.mm +404 -0
  169. package/native/appkit/web-browser.h +12 -0
  170. package/native/appkit/web-browser.mm +97 -0
  171. package/native/appkit/web-view-component.h +9 -0
  172. package/native/appkit/web-view-component.mm +56 -0
  173. package/native/appkit/web-view.h +31 -0
  174. package/native/appkit/web-view.mm +394 -0
  175. package/native/cmake/libraries/react-native-reanimated.cmake +45 -0
  176. package/native/cmake/libraries/react-native-worklets.cmake +46 -0
  177. package/native/cmake/libraries.cmake +109 -0
  178. package/native/cmake/linked-modules.cmake +112 -0
  179. package/native/cmake/react-native.cmake +175 -0
  180. package/native/cmake/runtime-dependencies.cmake +34 -0
  181. package/native/cmake/swift.cmake +43 -0
  182. package/native/cmake/third-party.cmake +74 -0
  183. package/native/core/include/ngn/absent-views.h +27 -0
  184. package/native/core/include/ngn/accessibility.h +140 -0
  185. package/native/core/include/ngn/address-search.h +69 -0
  186. package/native/core/include/ngn/animation-frames.h +39 -0
  187. package/native/core/include/ngn/app-info.h +52 -0
  188. package/native/core/include/ngn/app-menus.h +146 -0
  189. package/native/core/include/ngn/app-windows.h +181 -0
  190. package/native/core/include/ngn/apple-authentication.h +116 -0
  191. package/native/core/include/ngn/asset.h +28 -0
  192. package/native/core/include/ngn/audio.h +125 -0
  193. package/native/core/include/ngn/back-swipe.h +112 -0
  194. package/native/core/include/ngn/battery.h +48 -0
  195. package/native/core/include/ngn/bytes.h +59 -0
  196. package/native/core/include/ngn/calendar.h +180 -0
  197. package/native/core/include/ngn/camera.h +128 -0
  198. package/native/core/include/ngn/control-sizes.h +111 -0
  199. package/native/core/include/ngn/core-modules.h +105 -0
  200. package/native/core/include/ngn/crypto.h +38 -0
  201. package/native/core/include/ngn/default-menu.h +91 -0
  202. package/native/core/include/ngn/desktop-expo.h +193 -0
  203. package/native/core/include/ngn/desktop-modal.h +109 -0
  204. package/native/core/include/ngn/desktop-props.h +75 -0
  205. package/native/core/include/ngn/desktop-view-props.h +110 -0
  206. package/native/core/include/ngn/dev-server.h +33 -0
  207. package/native/core/include/ngn/download.h +24 -0
  208. package/native/core/include/ngn/expo-image.h +192 -0
  209. package/native/core/include/ngn/expo-ui.h +133 -0
  210. package/native/core/include/ngn/file-system.h +274 -0
  211. package/native/core/include/ngn/focus-order.h +63 -0
  212. package/native/core/include/ngn/fonts.h +64 -0
  213. package/native/core/include/ngn/gestures.h +305 -0
  214. package/native/core/include/ngn/glass.h +67 -0
  215. package/native/core/include/ngn/header-bar.h +112 -0
  216. package/native/core/include/ngn/host-state.h +243 -0
  217. package/native/core/include/ngn/image-manipulator.h +70 -0
  218. package/native/core/include/ngn/input-dispatcher.h +211 -0
  219. package/native/core/include/ngn/input-script.h +56 -0
  220. package/native/core/include/ngn/input.h +112 -0
  221. package/native/core/include/ngn/key-value-store.h +41 -0
  222. package/native/core/include/ngn/language-model.h +69 -0
  223. package/native/core/include/ngn/launch-frame.h +47 -0
  224. package/native/core/include/ngn/launch-settings.h +98 -0
  225. package/native/core/include/ngn/layout-animations.h +24 -0
  226. package/native/core/include/ngn/library-components.h +13 -0
  227. package/native/core/include/ngn/library-glue.h +53 -0
  228. package/native/core/include/ngn/live-regions.h +54 -0
  229. package/native/core/include/ngn/local-authentication.h +74 -0
  230. package/native/core/include/ngn/location.h +89 -0
  231. package/native/core/include/ngn/maps.h +226 -0
  232. package/native/core/include/ngn/module-registry.h +51 -0
  233. package/native/core/include/ngn/mounting-backend.h +96 -0
  234. package/native/core/include/ngn/mounting-manager.h +155 -0
  235. package/native/core/include/ngn/native-controls.h +161 -0
  236. package/native/core/include/ngn/native-module.h +109 -0
  237. package/native/core/include/ngn/network-state.h +44 -0
  238. package/native/core/include/ngn/network.h +100 -0
  239. package/native/core/include/ngn/notifications.h +185 -0
  240. package/native/core/include/ngn/photo-library.h +130 -0
  241. package/native/core/include/ngn/printing.h +69 -0
  242. package/native/core/include/ngn/processes.h +114 -0
  243. package/native/core/include/ngn/red-box.h +119 -0
  244. package/native/core/include/ngn/reload-thread.h +258 -0
  245. package/native/core/include/ngn/runtime.h +161 -0
  246. package/native/core/include/ngn/safe-areas.h +77 -0
  247. package/native/core/include/ngn/screen-capture.h +35 -0
  248. package/native/core/include/ngn/screen-presentation.h +85 -0
  249. package/native/core/include/ngn/screen-stacks.h +80 -0
  250. package/native/core/include/ngn/scrollers.h +106 -0
  251. package/native/core/include/ngn/sharing.h +39 -0
  252. package/native/core/include/ngn/spawn-rules.h +78 -0
  253. package/native/core/include/ngn/splash-screen.h +41 -0
  254. package/native/core/include/ngn/split-view.h +270 -0
  255. package/native/core/include/ngn/sqlite.h +38 -0
  256. package/native/core/include/ngn/store-review.h +26 -0
  257. package/native/core/include/ngn/surface-book.h +101 -0
  258. package/native/core/include/ngn/svg.h +393 -0
  259. package/native/core/include/ngn/symbol.h +90 -0
  260. package/native/core/include/ngn/system.h +102 -0
  261. package/native/core/include/ngn/tabs-host.h +171 -0
  262. package/native/core/include/ngn/text-input.h +125 -0
  263. package/native/core/include/ngn/text-measurer.h +38 -0
  264. package/native/core/include/ngn/text-recognition.h +55 -0
  265. package/native/core/include/ngn/tracking.h +34 -0
  266. package/native/core/include/ngn/tray-and-dock.h +107 -0
  267. package/native/core/include/ngn/undo.h +37 -0
  268. package/native/core/include/ngn/updates.h +253 -0
  269. package/native/core/include/ngn/video.h +126 -0
  270. package/native/core/include/ngn/view-box.h +126 -0
  271. package/native/core/include/ngn/view-transform.h +26 -0
  272. package/native/core/include/ngn/view-tree.h +85 -0
  273. package/native/core/include/ngn/web-browser.h +45 -0
  274. package/native/core/include/ngn/web-view.h +133 -0
  275. package/native/core/include/ngn/worklet-host.h +98 -0
  276. package/native/core/src/absent-views.cpp +41 -0
  277. package/native/core/src/accessibility.cpp +374 -0
  278. package/native/core/src/animation-frames.cpp +49 -0
  279. package/native/core/src/app-menus.cpp +352 -0
  280. package/native/core/src/app-windows.cpp +444 -0
  281. package/native/core/src/back-swipe.cpp +91 -0
  282. package/native/core/src/bytes.cpp +158 -0
  283. package/native/core/src/control-sizes.cpp +232 -0
  284. package/native/core/src/default-menu.cpp +96 -0
  285. package/native/core/src/desktop-modal.cpp +162 -0
  286. package/native/core/src/desktop-view-props.cpp +80 -0
  287. package/native/core/src/dev-server-helper.cpp +152 -0
  288. package/native/core/src/dev-server.cpp +103 -0
  289. package/native/core/src/download.cpp +53 -0
  290. package/native/core/src/expo-image.cpp +548 -0
  291. package/native/core/src/expo-ui.cpp +203 -0
  292. package/native/core/src/focus-order.cpp +441 -0
  293. package/native/core/src/gestures.cpp +1806 -0
  294. package/native/core/src/glass.cpp +49 -0
  295. package/native/core/src/header-bar.cpp +104 -0
  296. package/native/core/src/host-services.cpp +50 -0
  297. package/native/core/src/host-state.cpp +61 -0
  298. package/native/core/src/input-dispatcher.cpp +727 -0
  299. package/native/core/src/input-script.cpp +123 -0
  300. package/native/core/src/key-value-store.cpp +169 -0
  301. package/native/core/src/launch-settings.cpp +148 -0
  302. package/native/core/src/layout-animations.cpp +62 -0
  303. package/native/core/src/libraries/RNCWebViewSpec.cpp +80 -0
  304. package/native/core/src/libraries/react-native-reanimated.cpp +216 -0
  305. package/native/core/src/libraries/react-native-worklets.cpp +192 -0
  306. package/native/core/src/libraries/rnscreens.cpp +125 -0
  307. package/native/core/src/libraries/rnsvg.cpp +345 -0
  308. package/native/core/src/libraries/safeareacontext.cpp +70 -0
  309. package/native/core/src/libraries/worklets-idle-frames.js +70 -0
  310. package/native/core/src/library-glue.cpp +35 -0
  311. package/native/core/src/live-regions.cpp +122 -0
  312. package/native/core/src/module-registry.cpp +50 -0
  313. package/native/core/src/modules/accessibility-manager.cpp +98 -0
  314. package/native/core/src/modules/address-search.cpp +219 -0
  315. package/native/core/src/modules/alert-manager.cpp +140 -0
  316. package/native/core/src/modules/app-info.cpp +74 -0
  317. package/native/core/src/modules/app-modules.cpp +405 -0
  318. package/native/core/src/modules/apple-authentication.cpp +188 -0
  319. package/native/core/src/modules/asset.cpp +111 -0
  320. package/native/core/src/modules/async-storage.cpp +192 -0
  321. package/native/core/src/modules/audio.cpp +541 -0
  322. package/native/core/src/modules/battery.cpp +88 -0
  323. package/native/core/src/modules/calendar.cpp +457 -0
  324. package/native/core/src/modules/camera.cpp +221 -0
  325. package/native/core/src/modules/core-modules.cpp +62 -0
  326. package/native/core/src/modules/crypto.cpp +117 -0
  327. package/native/core/src/modules/desktop-expo.cpp +527 -0
  328. package/native/core/src/modules/file-system.cpp +941 -0
  329. package/native/core/src/modules/fonts.cpp +163 -0
  330. package/native/core/src/modules/image-loader.cpp +105 -0
  331. package/native/core/src/modules/image-manipulator.cpp +305 -0
  332. package/native/core/src/modules/language-model.cpp +195 -0
  333. package/native/core/src/modules/local-authentication.cpp +140 -0
  334. package/native/core/src/modules/location.cpp +186 -0
  335. package/native/core/src/modules/maps.cpp +500 -0
  336. package/native/core/src/modules/network-state.cpp +72 -0
  337. package/native/core/src/modules/networking.cpp +387 -0
  338. package/native/core/src/modules/notifications.cpp +549 -0
  339. package/native/core/src/modules/photo-library.cpp +371 -0
  340. package/native/core/src/modules/printing.cpp +201 -0
  341. package/native/core/src/modules/screen-capture.cpp +71 -0
  342. package/native/core/src/modules/sharing.cpp +59 -0
  343. package/native/core/src/modules/splash-screen.cpp +70 -0
  344. package/native/core/src/modules/sqlite.cpp +421 -0
  345. package/native/core/src/modules/store-review.cpp +23 -0
  346. package/native/core/src/modules/text-recognition.cpp +107 -0
  347. package/native/core/src/modules/tracking.cpp +46 -0
  348. package/native/core/src/modules/undo.cpp +59 -0
  349. package/native/core/src/modules/video.cpp +366 -0
  350. package/native/core/src/modules/web-browser.cpp +100 -0
  351. package/native/core/src/modules/web-socket.cpp +266 -0
  352. package/native/core/src/mounting-manager.cpp +380 -0
  353. package/native/core/src/native-controls.cpp +129 -0
  354. package/native/core/src/native-module.cpp +250 -0
  355. package/native/core/src/network.cpp +219 -0
  356. package/native/core/src/posix-launcher.cpp +366 -0
  357. package/native/core/src/processes.cpp +324 -0
  358. package/native/core/src/red-box-errors.js +83 -0
  359. package/native/core/src/red-box.cpp +306 -0
  360. package/native/core/src/runtime.cpp +422 -0
  361. package/native/core/src/safe-areas.cpp +130 -0
  362. package/native/core/src/screen-presentation.cpp +61 -0
  363. package/native/core/src/screen-stacks.cpp +254 -0
  364. package/native/core/src/scrollers.cpp +181 -0
  365. package/native/core/src/spawn-rules.cpp +292 -0
  366. package/native/core/src/split-view.cpp +343 -0
  367. package/native/core/src/svg.cpp +1883 -0
  368. package/native/core/src/symbol.cpp +94 -0
  369. package/native/core/src/tabs-host.cpp +234 -0
  370. package/native/core/src/text-input.cpp +126 -0
  371. package/native/core/src/text-layout-manager.cpp +63 -0
  372. package/native/core/src/tray-and-dock.cpp +154 -0
  373. package/native/core/src/updates.cpp +965 -0
  374. package/native/core/src/view-box.cpp +788 -0
  375. package/native/core/src/view-transform.cpp +45 -0
  376. package/native/core/src/view-tree.cpp +234 -0
  377. package/native/core/src/web-view.cpp +143 -0
  378. package/native/core/src/worklet-host.cpp +10 -0
  379. package/native/scripts/build-hermes.sh +44 -0
  380. package/native/scripts/build.sh +171 -0
  381. package/native/scripts/cache-dir.sh +18 -0
  382. package/native/scripts/codegen-library.sh +193 -0
  383. package/native/scripts/compiler-launcher.sh +17 -0
  384. package/native/scripts/fetch.sh +174 -0
  385. package/native/scripts/hermes-built.sh +10 -0
  386. package/native/scripts/host-dir.sh +16 -0
  387. package/native/scripts/macos-target.sh +8 -0
  388. package/native/scripts/make-prebuilt.mjs +360 -0
  389. package/native/scripts/prebuilt-host-cache.mjs +158 -0
  390. package/native/shim/openssl/sha.h +22 -0
  391. package/native/shim/view-platform/react/renderer/components/view/HostPlatformViewTraitsInitializer.h +37 -0
  392. package/package.json +70 -3
  393. package/prebuilt.json +33 -0
@@ -0,0 +1,457 @@
1
+ #include "ngn/calendar.h"
2
+
3
+ #include <algorithm>
4
+ #include <cmath>
5
+ #include <cstdio>
6
+ #include <ctime>
7
+ #include <set>
8
+
9
+ namespace ngn {
10
+
11
+ namespace {
12
+
13
+ using Access = CalendarStore::Access;
14
+ using Entity = CalendarStore::Entity;
15
+
16
+ // Whether the access answers the ask: full access any ask, write-only access a write-only one.
17
+ bool grants(Access access, bool writeOnly) {
18
+ return access == Access::FullAccess || (writeOnly && access == Access::WriteOnly);
19
+ }
20
+
21
+ // Whether an ask can still show the prompt: nothing decided yet, or write-only access that can
22
+ // still become full.
23
+ bool askable(Access access, bool writeOnly) {
24
+ return access == Access::NotDetermined || (!writeOnly && access == Access::WriteOnly);
25
+ }
26
+
27
+ folly::dynamic permissionOf(Access access, bool writeOnly) {
28
+ const char *status = grants(access, writeOnly) ? "granted" : askable(access, writeOnly) ? "undetermined" : "denied";
29
+ return folly::dynamic::object("status", status)("granted", grants(access, writeOnly))(
30
+ "canAskAgain", askable(access, writeOnly))("expires", "never");
31
+ }
32
+
33
+ folly::dynamic orNull(const std::optional<std::string> &value) {
34
+ return value ? folly::dynamic(*value) : folly::dynamic(nullptr);
35
+ }
36
+
37
+ folly::dynamic sourceOf(const CalendarStore::Source &source) {
38
+ return folly::dynamic::object("id", source.id)("name", source.name)("type", source.type)("isLocalAccount", source.type == "local");
39
+ }
40
+
41
+ folly::dynamic calendarOf(const CalendarStore::Calendar &calendar, Entity entity) {
42
+ return folly::dynamic::object("id", calendar.id)("title", calendar.title)("sourceId", calendar.source.id)(
43
+ "source", sourceOf(calendar.source))("type", calendar.type)("color", calendar.color)(
44
+ "entityType", entity == Entity::Event ? "event" : "reminder")("allowsModifications", calendar.allowsModifications)(
45
+ "allowedAvailabilities", folly::dynamic::array())("isPrimary", calendar.primary);
46
+ }
47
+
48
+ folly::dynamic dateOrNull(const std::optional<double> &ms) {
49
+ return ms ? folly::dynamic(isoDate(*ms)) : folly::dynamic(nullptr);
50
+ }
51
+
52
+ folly::dynamic alarmsOf(const std::vector<CalendarStore::Alarm> &alarms) {
53
+ folly::dynamic out = folly::dynamic::array();
54
+ for (const auto &alarm : alarms) {
55
+ folly::dynamic each = folly::dynamic::object();
56
+ if (alarm.relativeOffset) {
57
+ each["relativeOffset"] = *alarm.relativeOffset;
58
+ }
59
+ if (alarm.absoluteDate) {
60
+ each["absoluteDate"] = isoDate(*alarm.absoluteDate);
61
+ }
62
+ out.push_back(std::move(each));
63
+ }
64
+ return out;
65
+ }
66
+
67
+ folly::dynamic recurrenceOf(const std::optional<CalendarStore::Recurrence> &rule) {
68
+ if (!rule) {
69
+ return nullptr;
70
+ }
71
+ folly::dynamic out = folly::dynamic::object("frequency", rule->frequency)("interval", rule->interval);
72
+ if (rule->endDate) {
73
+ out["endDate"] = isoDate(*rule->endDate);
74
+ }
75
+ if (rule->occurrence) {
76
+ out["occurrence"] = *rule->occurrence;
77
+ }
78
+ return out;
79
+ }
80
+
81
+ folly::dynamic eventOf(const CalendarStore::Event &event) {
82
+ return folly::dynamic::object("id", event.id)("calendarId", event.calendarId)("title", event.title)(
83
+ "location", orNull(event.location))("notes", event.notes)("startDate", isoDate(event.start))(
84
+ "endDate", isoDate(event.end))("allDay", event.allDay)("timeZone", event.timeZone)("url", orNull(event.url))(
85
+ "alarms", alarmsOf(event.alarms))("recurrenceRule", recurrenceOf(event.recurrence))(
86
+ "availability", event.availability)("status", event.status);
87
+ }
88
+
89
+ folly::dynamic reminderOf(const CalendarStore::Reminder &reminder) {
90
+ return folly::dynamic::object("id", reminder.id)("calendarId", reminder.calendarId)("title", reminder.title)(
91
+ "location", orNull(reminder.location))("notes", reminder.notes)("url", orNull(reminder.url))(
92
+ "timeZone", reminder.timeZone)("startDate", dateOrNull(reminder.start))("dueDate", dateOrNull(reminder.due))(
93
+ "allDay", reminder.allDay)("completed", reminder.completed)("completionDate", dateOrNull(reminder.completionDate))(
94
+ "alarms", alarmsOf(reminder.alarms))("recurrenceRule", recurrenceOf(reminder.recurrence));
95
+ }
96
+
97
+ std::optional<double> numberArg(const folly::dynamic &args, size_t at) {
98
+ return args.size() > at && args[at].isNumber() ? std::optional(args[at].asDouble()) : std::nullopt;
99
+ }
100
+
101
+ // Why a write was refused: the permission it needs, as the service names it.
102
+ constexpr const char *kNoEventWrite =
103
+ "Calendar: no access to add events. Ask with Calendar.writePermission or Calendar.permission first.";
104
+ constexpr const char *kNoEventChange =
105
+ "Calendar: no full access to the calendars, which changing or deleting an event needs. Ask with Calendar.permission first.";
106
+ constexpr const char *kNoReminders = "Calendar: no access to the reminders. Ask with Calendar.remindersPermission first.";
107
+ constexpr const char *kNoCalendarChange =
108
+ "Calendar: no full access to the calendars, which creating or deleting a calendar needs. Ask with Calendar.permission first.";
109
+
110
+ Entity entityOf(const folly::dynamic &args) {
111
+ return !args.empty() && args[0].isString() && args[0].asString() == "reminder" ? Entity::Reminder : Entity::Event;
112
+ }
113
+
114
+ bool flag(const folly::dynamic &args, size_t at) {
115
+ return args.size() > at && args[at].isBool() && args[at].asBool();
116
+ }
117
+
118
+ std::optional<std::string> stringArg(const folly::dynamic &args, size_t at) {
119
+ return args.size() > at && args[at].isString() ? std::optional(args[at].asString()) : std::nullopt;
120
+ }
121
+
122
+ } // namespace
123
+
124
+ CalendarStore::Details calendarDetails(const folly::dynamic &details, const folly::dynamic &nullable) {
125
+ CalendarStore::Details out;
126
+ auto wrong = [](const std::string &name, const char *type) {
127
+ return std::invalid_argument("Calendar: " + name + " must be " + type);
128
+ };
129
+ if (nullable.isArray()) {
130
+ for (const auto &name : nullable) {
131
+ if (name.isString()) {
132
+ out.cleared.insert(name.getString());
133
+ }
134
+ }
135
+ }
136
+ if (!details.isObject()) {
137
+ return out;
138
+ }
139
+ for (const auto &[key, value] : details.items()) {
140
+ if (!key.isString()) {
141
+ continue;
142
+ }
143
+ const std::string name = key.getString();
144
+ if (value.isNull()) {
145
+ out.cleared.insert(name);
146
+ continue;
147
+ }
148
+ auto text = [&](std::optional<std::string> &field) {
149
+ if (!value.isString()) {
150
+ throw wrong(name, "a string");
151
+ }
152
+ field = value.getString();
153
+ };
154
+ auto date = [&](std::optional<double> &field) {
155
+ if (!value.isNumber()) {
156
+ throw wrong(name, "a date");
157
+ }
158
+ field = value.asDouble();
159
+ };
160
+ auto boolean = [&](std::optional<bool> &field) {
161
+ if (!value.isBool()) {
162
+ throw wrong(name, "true or false");
163
+ }
164
+ field = value.getBool();
165
+ };
166
+ if (name == "title") {
167
+ text(out.title);
168
+ } else if (name == "location") {
169
+ text(out.location);
170
+ } else if (name == "notes") {
171
+ text(out.notes);
172
+ } else if (name == "url") {
173
+ text(out.url);
174
+ } else if (name == "timeZone") {
175
+ text(out.timeZone);
176
+ } else if (name == "availability") {
177
+ text(out.availability);
178
+ } else if (name == "startDate") {
179
+ date(out.start);
180
+ } else if (name == "endDate") {
181
+ date(out.end);
182
+ } else if (name == "dueDate") {
183
+ date(out.due);
184
+ } else if (name == "completionDate") {
185
+ date(out.completionDate);
186
+ } else if (name == "allDay") {
187
+ boolean(out.allDay);
188
+ } else if (name == "completed") {
189
+ boolean(out.completed);
190
+ } else if (name == "alarms") {
191
+ if (!value.isArray()) {
192
+ throw wrong(name, "a list");
193
+ }
194
+ out.alarms.emplace();
195
+ for (const auto &alarm : value) {
196
+ auto *offset = alarm.isObject() ? alarm.get_ptr("relativeOffset") : nullptr;
197
+ auto *at = alarm.isObject() ? alarm.get_ptr("absoluteDate") : nullptr;
198
+ if (offset != nullptr && offset->isNumber()) {
199
+ out.alarms->push_back({.relativeOffset = offset->asDouble(), .absoluteDate = std::nullopt});
200
+ } else if (at != nullptr && at->isNumber()) {
201
+ out.alarms->push_back({.relativeOffset = std::nullopt, .absoluteDate = at->asDouble()});
202
+ }
203
+ }
204
+ } else if (name == "recurrenceRule") {
205
+ auto *frequency = value.isObject() ? value.get_ptr("frequency") : nullptr;
206
+ static const std::set<std::string> kFrequencies{"daily", "weekly", "monthly", "yearly"};
207
+ if (frequency == nullptr || !frequency->isString() || !kFrequencies.contains(frequency->getString())) {
208
+ throw wrong(name, "a rule with a frequency of daily, weekly, monthly or yearly");
209
+ }
210
+ CalendarStore::Recurrence rule{.frequency = frequency->getString()};
211
+ if (auto *interval = value.get_ptr("interval"); interval != nullptr && interval->isNumber()) {
212
+ rule.interval = std::max(1, static_cast<int>(interval->asDouble()));
213
+ }
214
+ if (auto *end = value.get_ptr("endDate"); end != nullptr && end->isNumber()) {
215
+ rule.endDate = end->asDouble();
216
+ }
217
+ if (auto *count = value.get_ptr("occurrence"); count != nullptr && count->isNumber()) {
218
+ if (count->asDouble() < 1) {
219
+ throw wrong(name, "a rule whose occurrence is 1 or more");
220
+ }
221
+ rule.occurrence = static_cast<int>(count->asDouble());
222
+ }
223
+ out.recurrence = rule;
224
+ }
225
+ }
226
+ return out;
227
+ }
228
+
229
+ std::string isoDate(double ms) {
230
+ auto seconds = static_cast<time_t>(std::floor(ms / 1000));
231
+ auto millis = static_cast<int>(ms - static_cast<double>(seconds) * 1000);
232
+ std::tm utc{};
233
+ gmtime_r(&seconds, &utc);
234
+ char out[32];
235
+ std::snprintf(out, sizeof out, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", utc.tm_year + 1900, utc.tm_mon + 1, utc.tm_mday,
236
+ utc.tm_hour, utc.tm_min, utc.tm_sec, millis);
237
+ return out;
238
+ }
239
+
240
+ std::shared_ptr<NativeModule> makeCalendarModule(std::shared_ptr<CalendarStore> store, bool noPrompts) {
241
+ struct Calendar : NativeModule {
242
+ Calendar(std::shared_ptr<CalendarStore> store, bool noPrompts) : NativeModule("NgNativeCalendar") {
243
+ auto get = [store](Entity entity, bool writeOnly) { return permissionOf(store->access(entity), writeOnly); };
244
+ auto request = [store, noPrompts](Entity entity, bool writeOnly, JsPromise promise) {
245
+ auto access = store->access(entity);
246
+ if (noPrompts || !askable(access, writeOnly)) {
247
+ promise.resolve(permissionOf(access, writeOnly));
248
+ return;
249
+ }
250
+ store->requestAccess(entity, writeOnly, [promise, writeOnly](Access decided) { promise.resolve(permissionOf(decided, writeOnly)); });
251
+ };
252
+ promise("getCalendarPermissionsAsync", [get](const folly::dynamic &args, JsPromise promise) {
253
+ promise.resolve(get(Entity::Event, flag(args, 0)));
254
+ });
255
+ promise("requestCalendarPermissionsAsync", [request](const folly::dynamic &args, JsPromise promise) {
256
+ request(Entity::Event, flag(args, 0), std::move(promise));
257
+ });
258
+ promise("getRemindersPermissionsAsync", [get](const folly::dynamic &, JsPromise promise) {
259
+ promise.resolve(get(Entity::Reminder, false));
260
+ });
261
+ promise("requestRemindersPermissionsAsync", [request](const folly::dynamic &, JsPromise promise) {
262
+ request(Entity::Reminder, false, std::move(promise));
263
+ });
264
+
265
+ auto reads = [store](Entity entity) { return store->access(entity) == Access::FullAccess; };
266
+ promise("getCalendarsAsync", [store, reads](const folly::dynamic &args, JsPromise promise) {
267
+ auto entity = entityOf(args);
268
+ folly::dynamic out = folly::dynamic::array();
269
+ if (reads(entity)) {
270
+ for (const auto &calendar : store->calendars(entity)) {
271
+ out.push_back(calendarOf(calendar, entity));
272
+ }
273
+ }
274
+ promise.resolve(std::move(out));
275
+ });
276
+ promise("getCalendarAsync", [store, reads](const folly::dynamic &args, JsPromise promise) {
277
+ auto id = stringArg(args, 0);
278
+ for (auto entity : {Entity::Event, Entity::Reminder}) {
279
+ if (!id || !reads(entity)) {
280
+ continue;
281
+ }
282
+ for (const auto &calendar : store->calendars(entity)) {
283
+ if (calendar.id == *id) {
284
+ promise.resolve(calendarOf(calendar, entity));
285
+ return;
286
+ }
287
+ }
288
+ }
289
+ promise.resolve(nullptr);
290
+ });
291
+ sync("getDefaultCalendarSync", [store, reads](const folly::dynamic &) {
292
+ auto calendar = reads(Entity::Event) ? store->defaultCalendar() : std::nullopt;
293
+ return calendar ? calendarOf(*calendar, Entity::Event) : folly::dynamic(nullptr);
294
+ });
295
+ sync("getSourcesSync", [store, reads](const folly::dynamic &) {
296
+ folly::dynamic out = folly::dynamic::array();
297
+ if (reads(Entity::Event) || reads(Entity::Reminder)) {
298
+ for (const auto &source : store->sources()) {
299
+ out.push_back(sourceOf(source));
300
+ }
301
+ }
302
+ return out;
303
+ });
304
+ promise("listEventsAsync", [store, reads](const folly::dynamic &args, JsPromise promise) {
305
+ folly::dynamic out = folly::dynamic::array();
306
+ if (args.size() < 3 || !args[0].isArray() || !args[1].isNumber() || !args[2].isNumber()) {
307
+ promise.reject("Calendar.listEvents: takes calendar ids, a start and an end");
308
+ return;
309
+ }
310
+ std::vector<std::string> ids;
311
+ for (const auto &id : args[0]) {
312
+ if (id.isString()) {
313
+ ids.push_back(id.asString());
314
+ }
315
+ }
316
+ if (reads(Entity::Event) && !ids.empty()) {
317
+ for (const auto &event : store->events(ids, args[1].asDouble(), args[2].asDouble())) {
318
+ out.push_back(eventOf(event));
319
+ }
320
+ }
321
+ promise.resolve(std::move(out));
322
+ });
323
+ promise("getEventAsync", [store, reads](const folly::dynamic &args, JsPromise promise) {
324
+ auto id = stringArg(args, 0);
325
+ auto event = id && reads(Entity::Event) ? store->event(*id) : std::nullopt;
326
+ promise.resolve(event ? eventOf(*event) : folly::dynamic(nullptr));
327
+ });
328
+
329
+ // A write: the item's id or calendar's id first, then the details and the fields to empty.
330
+ // `allowed` says whether the access lets it run; `refused` why not.
331
+ auto write = [](const char *method, bool allowed, const char *refused, const folly::dynamic &args, JsPromise &promise,
332
+ const std::function<folly::dynamic(const std::string &id, const CalendarStore::Details &)> &run) {
333
+ if (!allowed) {
334
+ promise.reject(refused);
335
+ return;
336
+ }
337
+ auto id = stringArg(args, 0);
338
+ if (!id) {
339
+ promise.reject(std::string("Calendar.") + method + ": takes an id");
340
+ return;
341
+ }
342
+ try {
343
+ auto details = calendarDetails(args.size() > 1 ? args[1] : folly::dynamic(nullptr), args.size() > 2 ? args[2] : folly::dynamic(nullptr));
344
+ promise.resolve(run(*id, details));
345
+ } catch (const std::invalid_argument &e) {
346
+ promise.reject(e.what());
347
+ } catch (const CalendarStore::Failure &e) {
348
+ promise.reject(e.what());
349
+ }
350
+ };
351
+ auto writes = [store](Entity entity) {
352
+ auto access = store->access(entity);
353
+ return access == Access::FullAccess || (entity == Entity::Event && access == Access::WriteOnly);
354
+ };
355
+ promise("createEventAsync", [store, writes, write](const folly::dynamic &args, JsPromise promise) {
356
+ write("createEvent", writes(Entity::Event), kNoEventWrite, args, promise, [&](const std::string &calendarId, const auto &details) {
357
+ if (!details.start || !details.end) {
358
+ throw std::invalid_argument("Calendar.createEvent: an event needs a startDate and an endDate");
359
+ }
360
+ if (*details.end < *details.start) {
361
+ throw std::invalid_argument("Calendar.createEvent: the endDate is before the startDate");
362
+ }
363
+ return eventOf(store->createEvent(calendarId, details));
364
+ });
365
+ });
366
+ promise("updateEventAsync", [store, reads, write](const folly::dynamic &args, JsPromise promise) {
367
+ write("update", reads(Entity::Event), kNoEventChange, args, promise, [&](const std::string &id, const auto &details) {
368
+ if (details.start && details.end && *details.end < *details.start) {
369
+ throw std::invalid_argument("Calendar.update: the endDate is before the startDate");
370
+ }
371
+ return eventOf(store->updateEvent(id, details));
372
+ });
373
+ });
374
+ promise("deleteEventAsync", [store, reads, write](const folly::dynamic &args, JsPromise promise) {
375
+ write("delete", reads(Entity::Event), kNoEventChange, args, promise, [&](const std::string &id, const auto &) {
376
+ store->deleteEvent(id);
377
+ return folly::dynamic(nullptr);
378
+ });
379
+ });
380
+
381
+ promise("createCalendarAsync", [store, reads](const folly::dynamic &args, JsPromise promise) {
382
+ const folly::dynamic details = !args.empty() && args[0].isObject() ? args[0] : folly::dynamic::object();
383
+ auto field = [&](const char *name) {
384
+ const auto *value = details.get_ptr(name);
385
+ return value != nullptr && value->isString() ? value->asString() : std::string();
386
+ };
387
+ auto entity = field("entityType") == "reminder" ? Entity::Reminder : Entity::Event;
388
+ if (!reads(entity)) {
389
+ promise.reject(entity == Entity::Event ? kNoCalendarChange : kNoReminders);
390
+ return;
391
+ }
392
+ CalendarStore::NewCalendar calendar{.title = field("title"), .color = field("color"), .sourceId = field("sourceId")};
393
+ if (calendar.title.empty()) {
394
+ promise.reject("Calendar.createCalendar: a calendar needs a title");
395
+ return;
396
+ }
397
+ try {
398
+ promise.resolve(calendarOf(store->createCalendar(entity, calendar), entity));
399
+ } catch (const CalendarStore::Failure &e) {
400
+ promise.reject(e.what());
401
+ }
402
+ });
403
+ promise("deleteCalendarAsync", [store, reads, write](const folly::dynamic &args, JsPromise promise) {
404
+ bool allowed = reads(Entity::Event) || reads(Entity::Reminder);
405
+ write("delete", allowed, kNoCalendarChange, args, promise, [&](const std::string &id, const auto &) {
406
+ store->deleteCalendar(id);
407
+ return folly::dynamic(nullptr);
408
+ });
409
+ });
410
+ promise("listRemindersAsync", [store, reads](const folly::dynamic &args, JsPromise promise) {
411
+ std::vector<std::string> ids;
412
+ if (!args.empty() && args[0].isArray()) {
413
+ for (const auto &id : args[0]) {
414
+ if (id.isString()) {
415
+ ids.push_back(id.asString());
416
+ }
417
+ }
418
+ }
419
+ if (!reads(Entity::Reminder) || ids.empty()) {
420
+ promise.resolve(folly::dynamic::array());
421
+ return;
422
+ }
423
+ store->reminders(ids, stringArg(args, 3), numberArg(args, 1), numberArg(args, 2), [promise](std::vector<CalendarStore::Reminder> found) {
424
+ folly::dynamic out = folly::dynamic::array();
425
+ for (const auto &reminder : found) {
426
+ out.push_back(reminderOf(reminder));
427
+ }
428
+ promise.resolve(std::move(out));
429
+ });
430
+ });
431
+ promise("getReminderAsync", [store, reads](const folly::dynamic &args, JsPromise promise) {
432
+ auto id = stringArg(args, 0);
433
+ auto reminder = id && reads(Entity::Reminder) ? store->reminder(*id) : std::nullopt;
434
+ promise.resolve(reminder ? reminderOf(*reminder) : folly::dynamic(nullptr));
435
+ });
436
+ promise("createReminderAsync", [store, reads, write](const folly::dynamic &args, JsPromise promise) {
437
+ write("createReminder", reads(Entity::Reminder), kNoReminders, args, promise, [&](const std::string &calendarId, const auto &details) {
438
+ return reminderOf(store->createReminder(calendarId, details));
439
+ });
440
+ });
441
+ promise("updateReminderAsync", [store, reads, write](const folly::dynamic &args, JsPromise promise) {
442
+ write("update", reads(Entity::Reminder), kNoReminders, args, promise, [&](const std::string &id, const auto &details) {
443
+ return reminderOf(store->updateReminder(id, details));
444
+ });
445
+ });
446
+ promise("deleteReminderAsync", [store, reads, write](const folly::dynamic &args, JsPromise promise) {
447
+ write("delete", reads(Entity::Reminder), kNoReminders, args, promise, [&](const std::string &id, const auto &) {
448
+ store->deleteReminder(id);
449
+ return folly::dynamic(nullptr);
450
+ });
451
+ });
452
+ }
453
+ };
454
+ return std::make_shared<Calendar>(std::move(store), noPrompts);
455
+ }
456
+
457
+ } // namespace ngn
@@ -0,0 +1,221 @@
1
+ #include "ngn/camera.h"
2
+
3
+ #include "ngn/bytes.h"
4
+ #include "ngn/crypto.h"
5
+ #include "ngn/desktop-view-props.h"
6
+ #include "ngn/file-system.h"
7
+
8
+ #include <react/renderer/core/propsConversions.h>
9
+
10
+ #include <algorithm>
11
+ #include <filesystem>
12
+ #include <fstream>
13
+ #include <random>
14
+
15
+ using namespace facebook::react;
16
+
17
+ namespace ngn {
18
+
19
+ const char CameraComponentName[] = "ViewManagerAdapter_ExpoCamera";
20
+
21
+ CameraProps::CameraProps(const PropsParserContext &context, const CameraProps &sourceProps, const RawProps &rawProps)
22
+ : ViewProps(context, sourceProps, rawProps),
23
+ HasDesktopProps{parseDesktopProps(context, sourceProps.desktop, rawProps)},
24
+ facing(convertRawProp(context, rawProps, "facing", sourceProps.facing, std::string{"back"})),
25
+ active(convertRawProp(context, rawProps, "active", sourceProps.active, true)),
26
+ mirror(convertRawProp(context, rawProps, "mirror", sourceProps.mirror, false)) {}
27
+
28
+ void CameraViews::attach(int tag, std::shared_ptr<CameraCapture> capture) {
29
+ std::lock_guard lock(mutex_);
30
+ captures_[tag] = std::move(capture);
31
+ }
32
+
33
+ void CameraViews::detach(int tag) {
34
+ std::lock_guard lock(mutex_);
35
+ captures_.erase(tag);
36
+ }
37
+
38
+ std::shared_ptr<CameraCapture> CameraViews::find(int tag) const {
39
+ std::lock_guard lock(mutex_);
40
+ auto found = captures_.find(tag);
41
+ return found == captures_.end() ? nullptr : found->second;
42
+ }
43
+
44
+ CameraMount cameraMount(CameraHardware::Access access, bool hasCamera, bool noPrompts, bool asked) {
45
+ using Access = CameraHardware::Access;
46
+ if (access == Access::NotDetermined && !noPrompts && !asked) {
47
+ return CameraMount::Ask;
48
+ }
49
+ if (access != Access::Granted) {
50
+ return CameraMount::NotPermitted;
51
+ }
52
+ return hasCamera ? CameraMount::Start : CameraMount::NoCamera;
53
+ }
54
+
55
+ int chooseCamera(const std::vector<CameraCandidate> &cameras) {
56
+ int other = -1;
57
+ for (size_t i = 0; i < cameras.size(); i++) {
58
+ if (cameras[i].suspended) {
59
+ continue;
60
+ }
61
+ if (cameras[i].builtIn) {
62
+ return static_cast<int>(i);
63
+ }
64
+ other = other < 0 ? static_cast<int>(i) : other;
65
+ }
66
+ return other;
67
+ }
68
+
69
+ std::string cameraMountError(CameraMount mount) {
70
+ switch (mount) {
71
+ case CameraMount::NotPermitted:
72
+ return "Camera permissions not granted - component could not be rendered.";
73
+ case CameraMount::NoCamera:
74
+ return "Camera could not be started - this Mac has no camera.";
75
+ default:
76
+ return "";
77
+ }
78
+ }
79
+
80
+ namespace {
81
+
82
+ folly::dynamic permissionOf(CameraHardware::Access access) {
83
+ using Access = CameraHardware::Access;
84
+ const char *status = access == Access::Granted ? "granted" : access == Access::NotDetermined ? "undetermined" : "denied";
85
+ return folly::dynamic::object("status", status)("granted", access == Access::Granted)("canAskAgain", access == Access::NotDetermined)(
86
+ "expires", "never");
87
+ }
88
+
89
+ // A new file's name: a random UUID with `extension`, as expo names its pictures and videos.
90
+ std::string newFileName(const std::string &extension) {
91
+ static std::mutex mutex;
92
+ static std::random_device device;
93
+ std::lock_guard lock(mutex);
94
+ std::string bytes(16, '\0');
95
+ for (auto &byte : bytes) {
96
+ byte = static_cast<char>(device() & 0xff);
97
+ }
98
+ auto id = uuidV4(bytes);
99
+ std::transform(id.begin(), id.end(), id.begin(), [](unsigned char c) { return std::toupper(c); });
100
+ return id + extension;
101
+ }
102
+
103
+ std::string contentsOf(const std::string &path) {
104
+ std::ifstream in(path, std::ios::binary);
105
+ return {std::istreambuf_iterator<char>(in), {}};
106
+ }
107
+
108
+ } // namespace
109
+
110
+ std::shared_ptr<NativeModule> makeCameraModule(
111
+ std::shared_ptr<CameraHardware> hardware, std::shared_ptr<CameraViews> views, std::string pictures, bool noPrompts) {
112
+ struct Camera : NativeModule {
113
+ Camera(std::shared_ptr<CameraHardware> hardware, std::shared_ptr<CameraViews> views, std::string pictures, bool noPrompts)
114
+ : NativeModule("NgNativeCamera") {
115
+ using Access = CameraHardware::Access;
116
+ promise("getCameraPermissionsAsync", [hardware](const folly::dynamic &, JsPromise promise) {
117
+ promise.resolve(permissionOf(hardware->access()));
118
+ });
119
+ promise("requestCameraPermissionsAsync", [hardware, noPrompts](const folly::dynamic &, JsPromise promise) {
120
+ auto access = hardware->access();
121
+ if (noPrompts || access != Access::NotDetermined) {
122
+ promise.resolve(permissionOf(access));
123
+ return;
124
+ }
125
+ hardware->requestAccess([promise](Access decided) { promise.resolve(permissionOf(decided)); });
126
+ });
127
+ promise("isAvailableAsync", [hardware](const folly::dynamic &, JsPromise promise) { promise.resolve(hardware->hasCamera()); });
128
+ promise("takePicture", [hardware, views, pictures](const folly::dynamic &args, JsPromise promise) {
129
+ int tag = !args.empty() && args[0].isNumber() ? static_cast<int>(args[0].asDouble()) : -1;
130
+ const folly::dynamic options = args.size() > 1 ? args[1] : folly::dynamic(nullptr);
131
+ if (hardware->access() != Access::Granted) {
132
+ promise.reject("Camera.takePicture: camera permission has not been granted");
133
+ return;
134
+ }
135
+ auto capture = views->find(tag);
136
+ if (!capture) {
137
+ promise.reject("Camera is not ready yet. Wait for 'onCameraReady' callback");
138
+ return;
139
+ }
140
+ double quality = 1;
141
+ bool base64 = false;
142
+ if (options.isObject()) {
143
+ if (const auto *q = options.get_ptr("quality"); q && q->isNumber()) {
144
+ quality = std::clamp(q->asDouble(), 0.0, 1.0);
145
+ }
146
+ if (const auto *b = options.get_ptr("base64"); b && b->isBool()) {
147
+ base64 = b->asBool();
148
+ }
149
+ }
150
+ std::error_code ignored;
151
+ std::filesystem::create_directories(pictures, ignored);
152
+ auto path = pictures + "/" + newFileName(".jpg");
153
+ capture->takePicture(quality, path, [promise, path, base64](int width, int height, const std::string &error) {
154
+ if (!error.empty()) {
155
+ promise.reject("Camera.takePicture: " + error);
156
+ return;
157
+ }
158
+ folly::dynamic picture = folly::dynamic::object("uri", fileUri(path))("width", width)("height", height)("format", "jpg");
159
+ if (base64) {
160
+ picture["base64"] = base64Encode(contentsOf(path));
161
+ }
162
+ promise.resolve(std::move(picture));
163
+ });
164
+ });
165
+ promise("record", [hardware, views, pictures](const folly::dynamic &args, JsPromise promise) {
166
+ int tag = !args.empty() && args[0].isNumber() ? static_cast<int>(args[0].asDouble()) : -1;
167
+ const folly::dynamic options = args.size() > 1 ? args[1] : folly::dynamic(nullptr);
168
+ if (hardware->access() != Access::Granted) {
169
+ promise.reject("Camera.record: camera permission has not been granted");
170
+ return;
171
+ }
172
+ auto capture = views->find(tag);
173
+ if (!capture) {
174
+ promise.reject("Camera is not ready yet. Wait for 'onCameraReady' callback");
175
+ return;
176
+ }
177
+ if (capture->recording()) {
178
+ promise.reject("Camera.record: a recording is already in progress");
179
+ return;
180
+ }
181
+ CameraRecordingRequest request;
182
+ bool mute = false;
183
+ if (options.isObject()) {
184
+ if (const auto *d = options.get_ptr("maxDuration"); d && d->isNumber()) {
185
+ request.maxDuration = std::max(0.0, d->asDouble());
186
+ }
187
+ if (const auto *f = options.get_ptr("maxFileSize"); f && f->isNumber()) {
188
+ request.maxFileSize = std::max(0.0, f->asDouble());
189
+ }
190
+ if (const auto *m = options.get_ptr("mute"); m && m->isBool()) {
191
+ mute = m->asBool();
192
+ }
193
+ if (const auto *c = options.get_ptr("codec"); c && c->isString()) {
194
+ request.codec = c->asString();
195
+ }
196
+ }
197
+ request.sound = !mute && hardware->microphoneAllowed();
198
+ std::error_code ignored;
199
+ std::filesystem::create_directories(pictures, ignored);
200
+ request.path = pictures + "/" + newFileName(".mov");
201
+ capture->startRecording(request, [promise, path = request.path](const std::string &error) {
202
+ if (!error.empty()) {
203
+ promise.reject("Camera.record: " + error);
204
+ return;
205
+ }
206
+ promise.resolve(folly::dynamic::object("uri", fileUri(path)));
207
+ });
208
+ });
209
+ promise("stopRecording", [views](const folly::dynamic &args, JsPromise promise) {
210
+ int tag = !args.empty() && args[0].isNumber() ? static_cast<int>(args[0].asDouble()) : -1;
211
+ if (auto capture = views->find(tag)) {
212
+ capture->stopRecording();
213
+ }
214
+ promise.resolve(nullptr);
215
+ });
216
+ }
217
+ };
218
+ return std::make_shared<Camera>(std::move(hardware), std::move(views), std::move(pictures), noPrompts);
219
+ }
220
+
221
+ } // namespace ngn