@ng-native/desktop 0.23.0 → 0.25.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 (73) hide show
  1. package/bin/package.mjs +65 -9
  2. package/bin/prebuilt.mjs +25 -4
  3. package/native/CMakeLists.txt +7 -2
  4. package/native/appkit/CMakeLists.txt +39 -5
  5. package/native/appkit/appkit-backend.mm +28 -3
  6. package/native/appkit/camera-sheet.h +59 -0
  7. package/native/appkit/camera-sheet.mm +344 -0
  8. package/native/appkit/camera-view.mm +5 -1
  9. package/native/appkit/camera.h +22 -0
  10. package/native/appkit/camera.mm +203 -69
  11. package/native/appkit/cg-canvas.h +8 -1
  12. package/native/appkit/cg-canvas.mm +166 -21
  13. package/native/appkit/header-toolbar.mm +4 -1
  14. package/native/appkit/host-view.mm +29 -1
  15. package/native/appkit/image-view.mm +31 -2
  16. package/native/appkit/main.mm +29 -69
  17. package/native/appkit/photo-library.mm +6 -6
  18. package/native/appkit/scroll-view.mm +57 -16
  19. package/native/appkit/scrollers.h +22 -2
  20. package/native/appkit/scrollers.mm +221 -2
  21. package/native/appkit/split-view.h +4 -0
  22. package/native/appkit/split-view.mm +57 -3
  23. package/native/appkit/surface-view.mm +5 -1
  24. package/native/appkit/svg-view.h +2 -1
  25. package/native/appkit/svg-view.mm +49 -3
  26. package/native/appkit/tab-walk.h +31 -0
  27. package/native/appkit/tab-walk.mm +64 -0
  28. package/native/appkit/text-input-field.h +5 -0
  29. package/native/appkit/text-input-field.mm +101 -20
  30. package/native/appkit/text-layout.h +7 -1
  31. package/native/appkit/text-layout.mm +161 -30
  32. package/native/appkit/web-view-component.h +4 -3
  33. package/native/appkit/web-view-component.mm +8 -2
  34. package/native/appkit/web-view.mm +7 -0
  35. package/native/cmake/react-native.cmake +9 -0
  36. package/native/cmake/runtime-dependencies.cmake +21 -0
  37. package/native/core/include/ngn/app-menus.h +8 -3
  38. package/native/core/include/ngn/back-swipe.h +16 -7
  39. package/native/core/include/ngn/camera.h +103 -9
  40. package/native/core/include/ngn/core-modules.h +3 -0
  41. package/native/core/include/ngn/dom-web-view.h +53 -0
  42. package/native/core/include/ngn/photo-library.h +7 -3
  43. package/native/core/include/ngn/runtime.h +10 -8
  44. package/native/core/include/ngn/safe-areas.h +5 -0
  45. package/native/core/include/ngn/scrollers.h +5 -0
  46. package/native/core/include/ngn/surface-metrics.h +51 -0
  47. package/native/core/include/ngn/svg.h +49 -8
  48. package/native/core/include/ngn/text-input.h +9 -0
  49. package/native/core/include/ngn/text-measurer.h +11 -0
  50. package/native/core/include/ngn/web-view.h +6 -2
  51. package/native/core/src/app-menus.cpp +10 -3
  52. package/native/core/src/back-swipe.cpp +12 -5
  53. package/native/core/src/dom-web-view.cpp +45 -0
  54. package/native/core/src/libraries/rnsvg.cpp +13 -0
  55. package/native/core/src/modules/app-modules.cpp +24 -15
  56. package/native/core/src/modules/camera.cpp +203 -23
  57. package/native/core/src/modules/photo-library.cpp +22 -8
  58. package/native/core/src/mounting-manager.cpp +4 -0
  59. package/native/core/src/runtime.cpp +6 -8
  60. package/native/core/src/safe-areas.cpp +7 -0
  61. package/native/core/src/scrollers.cpp +11 -0
  62. package/native/core/src/svg.cpp +324 -17
  63. package/native/core/src/text-input.cpp +13 -0
  64. package/native/core/src/text-layout-manager.cpp +13 -0
  65. package/native/core/src/web-view.cpp +1 -1
  66. package/native/scripts/build.sh +12 -2
  67. package/native/scripts/fetch-hermes.sh +70 -0
  68. package/native/scripts/launch-probe.sh +60 -0
  69. package/native/scripts/make-prebuilt.mjs +13 -6
  70. package/native/scripts/prebuilt-host-cache.mjs +1 -1
  71. package/native/shim/text-platform/react/renderer/textlayoutmanager/TextLayoutManager.h +52 -0
  72. package/package.json +2 -2
  73. package/prebuilt.json +6 -6
@@ -7,19 +7,23 @@
7
7
  // an undetermined permission is asked, and only when the app's Info.plist explains why; with
8
8
  // `noPrompts` (NGN_NO_PROMPTS=1) nothing is.
9
9
  // - isAvailableAsync(): whether the Mac has a camera.
10
- // - takePicture(tag, options): expo's picture, {uri, width, height, format: 'jpg', base64?}, a
11
- // JPEG at `quality` in `<caches>/Camera`, from the view `tag` once its camera runs.
10
+ // - takePicture(tag, options): expo's picture, {uri, width, height, format, base64?}, in
11
+ // `<caches>/Camera`, from the view `tag` once its camera runs: a JPEG at `quality`, or with
12
+ // `imageType: 'png'` a PNG (lossless, so `quality` does nothing), as on iOS. Another imageType
13
+ // is refused.
12
14
  // - record(tag, options): expo's recording, {uri}, a QuickTime movie in `<caches>/Camera` from the
13
15
  // view `tag`, once it stops: stopRecording(tag), `maxDuration` seconds or `maxFileSize` bytes
14
- // reached, or the view going. It has sound unless `mute`, and only with the microphone allowed
15
- // (as on iOS, a recording without the microphone permission has none; nothing is asked). One
16
- // recording a view at a time.
16
+ // reached, or the view going. It has sound unless the view has `mute`, and only with the
17
+ // microphone allowed (as on iOS, a recording without the microphone permission has none; nothing
18
+ // is asked). The view's `mute` as the recording starts holds until it stops; like iOS, record()
19
+ // takes no `mute` of its own. One recording a view at a time.
17
20
  //
18
21
  // A view starts its camera when it mounts with the camera allowed and present: it sends
19
22
  // `cameraReady`, and `mountError` ({message}, expo-camera's words) when the permission is not
20
23
  // granted or the Mac has no camera. It asks for the permission first when it can.
21
24
  #pragma once
22
25
 
26
+ #include "ngn/audio.h"
23
27
  #include "ngn/desktop-props.h"
24
28
  #include "ngn/native-module.h"
25
29
 
@@ -32,6 +36,8 @@
32
36
  #include <map>
33
37
  #include <memory>
34
38
  #include <mutex>
39
+ #include <optional>
40
+ #include <set>
35
41
  #include <string>
36
42
  #include <vector>
37
43
 
@@ -50,6 +56,8 @@ class CameraProps final : public facebook::react::ViewProps, public HasDesktopPr
50
56
  std::string facing{"back"};
51
57
  bool active{true};
52
58
  bool mirror{false};
59
+ // expo-camera's `mute`: the view records without sound.
60
+ bool mute{false};
53
61
  };
54
62
 
55
63
  using CameraShadowNode = facebook::react::ConcreteViewShadowNode<CameraComponentName, CameraProps, facebook::react::ViewEventEmitter>;
@@ -68,6 +76,15 @@ class CameraHardware {
68
76
  virtual bool microphoneAllowed() = 0;
69
77
  };
70
78
 
79
+ // What a picture is asked for.
80
+ struct CameraPictureRequest {
81
+ std::string path;
82
+ // 0 to 1, for a JPEG.
83
+ double quality{1};
84
+ // expo-camera's PictureFormat: "jpg" or "png".
85
+ std::string format{"jpg"};
86
+ };
87
+
71
88
  // What a recording is asked for.
72
89
  struct CameraRecordingRequest {
73
90
  std::string path;
@@ -77,14 +94,17 @@ struct CameraRecordingRequest {
77
94
  bool sound{true};
78
95
  // expo-camera's VideoCodec ("avc1", "hvc1", "jpeg", "apcn", "ap4h"), or empty for the default.
79
96
  std::string codec;
97
+ // The session preset the movie is recorded at, AVFoundation's name for it without its prefix
98
+ // (High, Medium, Low, 640x480, iFrame1280x720, iFrame960x540); empty for High.
99
+ std::string preset;
80
100
  };
81
101
 
82
102
  // A view's running camera.
83
103
  class CameraCapture {
84
104
  public:
85
105
  virtual ~CameraCapture() = default;
86
- // Writes a JPEG at `quality` (0 to 1) to `path`; answers its size, or why not. Any thread.
87
- virtual void takePicture(double quality, const std::string &path, std::function<void(int width, int height, const std::string &error)> done) = 0;
106
+ // Writes the picture `request` asks for; answers its size, or why not. Any thread.
107
+ virtual void takePicture(const CameraPictureRequest &request, std::function<void(int width, int height, const std::string &error)> done) = 0;
88
108
  // Records a movie until stopRecording or a limit; `done` gets why not, or empty once the file
89
109
  // is finished. Any thread.
90
110
  virtual void startRecording(const CameraRecordingRequest &request, std::function<void(const std::string &error)> done) = 0;
@@ -92,16 +112,22 @@ class CameraCapture {
92
112
  virtual bool recording() const = 0;
93
113
  };
94
114
 
95
- // The running cameras by view tag, shared by the views and NgNativeCamera. Any thread.
115
+ // The running cameras by view tag, and the views with `mute`, shared by the views and
116
+ // NgNativeCamera. A view sets its `mute` as its props change; detaching forgets both. Any thread.
96
117
  class CameraViews {
97
118
  public:
98
119
  void attach(int tag, std::shared_ptr<CameraCapture> capture);
99
120
  void detach(int tag);
100
- std::shared_ptr<CameraCapture> find(int tag) const;
121
+ // With `muted`, also whether the view has `mute`, read under the same lock, so a view that
122
+ // detaches meanwhile cannot leave its camera without its mute.
123
+ std::shared_ptr<CameraCapture> find(int tag, bool *muted = nullptr) const;
124
+ void mute(int tag, bool muted);
125
+ bool muted(int tag) const;
101
126
 
102
127
  private:
103
128
  mutable std::mutex mutex_;
104
129
  std::map<int, std::shared_ptr<CameraCapture>> captures_;
130
+ std::set<int> muted_;
105
131
  };
106
132
 
107
133
  // A camera the host could start: the Mac's own, and whether it is suspended (a MacBook's with the
@@ -122,6 +148,74 @@ CameraMount cameraMount(CameraHardware::Access access, bool hasCamera, bool noPr
122
148
  // expo-camera's mountError message for a mount that cannot start, or empty.
123
149
  std::string cameraMountError(CameraMount mount);
124
150
 
151
+ // A picture the host's camera sheet took: its size, and the camera's EXIF (an object, or null).
152
+ struct CameraShot {
153
+ int width{0};
154
+ int height{0};
155
+ folly::dynamic exif{nullptr};
156
+ };
157
+
158
+ // A movie the host's camera sheet recorded: its size as it plays, and its length in milliseconds.
159
+ struct CameraClip {
160
+ int width{0};
161
+ int height{0};
162
+ double duration{0};
163
+ };
164
+
165
+ // What the sheet is asked for: a photo, a video, or either, with a switch between them.
166
+ struct CameraSheetRequest {
167
+ bool photos{true};
168
+ bool videos{false};
169
+ CameraPictureRequest photo;
170
+ CameraRecordingRequest video;
171
+ };
172
+
173
+ // What the sheet took: a photo or a video, or neither when the user cancelled.
174
+ struct CameraTake {
175
+ std::optional<CameraShot> photo;
176
+ std::optional<CameraClip> video;
177
+ };
178
+
179
+ // The host's own camera for expo-image-picker's launchCameraAsync: macOS has no system camera
180
+ // screen like iOS's UIImagePickerController, so the host shows the camera's preview and a shutter
181
+ // itself.
182
+ class CameraSheet {
183
+ public:
184
+ virtual ~CameraSheet() = default;
185
+ // Shows the camera; once the user takes a photo or records a video, writes it as `request` asks
186
+ // and answers it. Answers neither and no error when they cancel or there is no window to show it
187
+ // on, and an error when the camera fails. Only with the camera allowed and present. Any thread.
188
+ virtual void shoot(const CameraSheetRequest &request, std::function<void(CameraTake, const std::string &error)> done) = 0;
189
+ };
190
+
191
+ // What NgNativeImagePicker's camera half needs: the permission and the device, the microphone a
192
+ // video's sound comes from, the sheet, and the folder its photos and videos go in
193
+ // (`<caches>/ImagePicker`, as on iOS).
194
+ struct PickerCamera {
195
+ std::shared_ptr<CameraHardware> hardware;
196
+ std::shared_ptr<AudioInput> microphone;
197
+ std::shared_ptr<CameraSheet> sheet;
198
+ std::string folder;
199
+ };
200
+
201
+ // expo's camera permission response, asked as NgNativeCamera asks it: only when undetermined, and
202
+ // never with `noPrompts`.
203
+ folly::dynamic cameraPermissionOf(CameraHardware::Access access);
204
+ void requestCameraPermission(CameraHardware &hardware, bool noPrompts, JsPromise promise);
205
+
206
+ // expo-image-picker's launchCameraAsync(options) over `camera`, shaped as iOS shapes a camera
207
+ // shot (no assetId and no fileName):
208
+ // - a photo, a JPEG at `quality` (1 by default), with base64 and exif only when asked;
209
+ // - with `mediaTypes` asking for videos, a QuickTime movie (`video/quicktime`) recorded at
210
+ // `videoQuality` (UIImagePickerControllerQualityType, High by default) for at most
211
+ // `videoMaxDuration` seconds (0, the default, for no limit), with its duration in milliseconds.
212
+ // Videos alone show a record button; videos and photos, a switch between the two.
213
+ // Rejected without the camera permission, as iOS does, or on a Mac with no camera. A request for
214
+ // videos asks for the microphone when it is undetermined; refused, the video has no sound, as iOS's
215
+ // camera records it. Only an app that cannot ask (no NSMicrophoneUsageDescription) is rejected,
216
+ // with iOS's message. Canceled with `noPrompts`, which shows no sheet.
217
+ void launchPickerCamera(const PickerCamera &camera, bool noPrompts, const folly::dynamic &options, JsPromise promise);
218
+
125
219
  std::shared_ptr<NativeModule> makeCameraModule(
126
220
  std::shared_ptr<CameraHardware> hardware, std::shared_ptr<CameraViews> views, std::string pictures, bool noPrompts);
127
221
 
@@ -61,6 +61,9 @@ std::shared_ptr<NativeModule> makeAppearance(const CoreModuleServices &services)
61
61
  std::shared_ptr<NativeModule> makeAccessibilityManager(const CoreModuleServices &services);
62
62
  std::shared_ptr<NativeModule> makeLinking(const CoreModuleServices &services);
63
63
  std::shared_ptr<NativeModule> makeI18n(const CoreModuleServices &services);
64
+ // What I18nManager's isRTL answers, and the direction each surface lays out from: the system's,
65
+ // unless the app turned right to left off (allowRTL) or forced it on (forceRTL).
66
+ bool appRightToLeft(System &system);
64
67
  std::shared_ptr<NativeModule> makeAlertManager(const CoreModuleServices &services);
65
68
  std::shared_ptr<NativeModule> makeClipboard(const CoreModuleServices &services);
66
69
  std::shared_ptr<NativeModule> makeSettingsManager(const CoreModuleServices &services);
@@ -0,0 +1,53 @@
1
+ // `<dom-component>` on a desktop host: @expo/dom-webview's view (ViewManagerAdapter_ExpoDomWebViewModule,
2
+ // the component @ng-native/expo's `<dom-component>` commits) as a Fabric component of the desktop's
3
+ // own, drawn by the same web view as react-native-webview's RNCWebView (web-view.h).
4
+ //
5
+ // The core reads Expo's props into a WebViewConfig (domWebViewConfig): `source` ({uri}),
6
+ // `injectedJavaScript`, `injectedJavaScriptBeforeContentLoaded`, `injectedJavaScriptObject` and
7
+ // `webviewDebuggingEnabled`. As in Expo's iOS view, the page always has
8
+ // window.ReactNativeWebView.postMessage, each message is the `message` event ({url, title, data}),
9
+ // the injected scripts run in every frame, and a navigation goes ahead without asking the app.
10
+ // `injectJavaScript` is the view's command, where Expo's is a function on its view.
11
+ #pragma once
12
+
13
+ #include "ngn/desktop-props.h"
14
+ #include "ngn/web-view.h"
15
+
16
+ #include <react/renderer/components/view/ConcreteViewShadowNode.h>
17
+ #include <react/renderer/components/view/ViewEventEmitter.h>
18
+ #include <react/renderer/components/view/ViewProps.h>
19
+ #include <react/renderer/core/ConcreteComponentDescriptor.h>
20
+
21
+ #include <folly/dynamic.h>
22
+
23
+ #include <optional>
24
+ #include <string>
25
+
26
+ namespace ngn {
27
+
28
+ extern const char DomWebViewComponentName[];
29
+
30
+ class DomWebViewProps final : public facebook::react::ViewProps, public HasDesktopProps {
31
+ public:
32
+ DomWebViewProps() = default;
33
+ DomWebViewProps(
34
+ const facebook::react::PropsParserContext &context,
35
+ const DomWebViewProps &sourceProps,
36
+ const facebook::react::RawProps &rawProps);
37
+
38
+ // {uri}, as Expo's DomWebViewSource.
39
+ folly::dynamic source;
40
+ std::string injectedJavaScript;
41
+ std::string injectedJavaScriptBeforeContentLoaded;
42
+ std::string injectedJavaScriptObject;
43
+ bool webviewDebuggingEnabled{false};
44
+ };
45
+
46
+ // The web view's config for Expo's DOM web view's props; nothing for another component's.
47
+ std::optional<WebViewConfig> domWebViewConfig(const facebook::react::Props &props);
48
+
49
+ using DomWebViewShadowNode =
50
+ facebook::react::ConcreteViewShadowNode<DomWebViewComponentName, DomWebViewProps, facebook::react::ViewEventEmitter>;
51
+ using DomWebViewComponentDescriptor = facebook::react::ConcreteComponentDescriptor<DomWebViewShadowNode>;
52
+
53
+ } // namespace ngn
@@ -20,10 +20,12 @@
20
20
  //
21
21
  // NgNativeImagePicker, expo-image-picker's library half: launchImageLibraryAsync(options) ->
22
22
  // {canceled, assets}, the system's photo picker over the key window, which needs no permission;
23
- // get/requestMediaLibraryPermissionsAsync() as above; launchCameraAsync() answers canceled and
24
- // the camera permissions denied, since macOS has no system camera to open.
23
+ // get/requestMediaLibraryPermissionsAsync() as above. Its camera half is the host's own camera
24
+ // sheet (camera.h's launchPickerCamera), since macOS has no system camera screen to open:
25
+ // launchCameraAsync(options) and get/requestCameraPermissionsAsync(), the camera's permission.
25
26
  #pragma once
26
27
 
28
+ #include "ngn/camera.h"
27
29
  #include "ngn/file-system.h"
28
30
 
29
31
  #include "ngn/native-module.h"
@@ -125,6 +127,8 @@ class PhotoLibrary {
125
127
  PhotoLibrary::Query photoQueryOf(const folly::dynamic &query);
126
128
 
127
129
  std::shared_ptr<NativeModule> makeMediaLibraryModule(std::shared_ptr<PhotoLibrary> library, bool noPrompts, std::shared_ptr<FileAccess> files);
128
- std::shared_ptr<NativeModule> makeImagePickerModule(std::shared_ptr<PhotoLibrary> library, bool noPrompts);
130
+ // `camera`: the host's own camera for launchCameraAsync; without it the camera permissions answer
131
+ // denied and launchCameraAsync canceled.
132
+ std::shared_ptr<NativeModule> makeImagePickerModule(std::shared_ptr<PhotoLibrary> library, bool noPrompts, std::optional<PickerCamera> camera = std::nullopt);
129
133
 
130
134
  } // namespace ngn
@@ -10,6 +10,7 @@
10
10
  #include "ngn/network.h"
11
11
  #include "ngn/reload-thread.h"
12
12
  #include "ngn/surface-book.h"
13
+ #include "ngn/surface-metrics.h"
13
14
  #include "ngn/native-controls.h"
14
15
  #include "ngn/text-measurer.h"
15
16
 
@@ -36,14 +37,6 @@ class Runtime;
36
37
 
37
38
  namespace ngn {
38
39
 
39
- struct SurfaceMetrics {
40
- facebook::react::Size size{};
41
- // Device pixels per point.
42
- float pointScale{2};
43
- // The text size multiplier.
44
- float fontScale{1};
45
- };
46
-
47
40
  // The OS's HTTP and WebSocket clients, which Networking, WebSocket and the dev server use.
48
41
  struct NetworkClients {
49
42
  std::shared_ptr<Http> http;
@@ -85,6 +78,9 @@ class Runtime {
85
78
  // that resumes it; then that it resumed. On the inspector's thread. Set them before loadBundle.
86
79
  std::function<void(std::function<void()> resume)> onDebuggerPaused;
87
80
  std::function<void()> onDebuggerResumed;
81
+ // Whether the app lays out right to left, asked as each JavaScript instance loads (the first
82
+ // and each reload's) and kept for its run, as its I18nManager keeps isRTL. Set before loadBundle.
83
+ std::function<bool()> rightToLeft;
88
84
 
89
85
  // Loads the bundle (JavaScript or Hermes bytecode). False if it could not be read or downloaded.
90
86
  bool loadBundle();
@@ -92,6 +88,10 @@ class Runtime {
92
88
  const std::optional<DevServer> &devServer() const {
93
89
  return devServer_;
94
90
  }
91
+ // Whether the running instance's surfaces lay out right to left, as its I18nManager.isRTL says.
92
+ bool laysOutRightToLeft() const {
93
+ return direction_.rightToLeft();
94
+ }
95
95
  // Tears the runtime down and starts it again on a fresh download of the bundle, with every
96
96
  // running surface restarted. Runs off the calling thread; a second call while one runs is
97
97
  // dropped. `done` gets whether the bundle loaded, on the UI thread once the reload's thread has
@@ -156,6 +156,8 @@ class Runtime {
156
156
  // Taken before mutex_ when both are, and never held by a reload while it holds mutex_.
157
157
  std::mutex surfacesMutex_;
158
158
  SurfaceBook<Surface> surfaces_;
159
+ // The direction the running instance's surfaces lay out from.
160
+ SurfaceDirection direction_{[this] { return rightToLeft && rightToLeft(); }};
159
161
  };
160
162
 
161
163
  } // namespace ngn
@@ -36,6 +36,11 @@ struct ContainerInsets {
36
36
  // The part of a container's insets a view at `frame` (in the container's coordinates) overlaps.
37
37
  EdgeInsets insetsWithin(const ContainerInsets &container, const Rect &frame);
38
38
 
39
+ // How far window controls spanning `minX` to `maxX` (in a container `width` wide) reach in from
40
+ // its leading edge: the left one, or the right one right to left, where AppKit puts the controls
41
+ // on the right. 0 when they lie outside it.
42
+ double titlebarLeading(double minX, double maxX, double width, bool rightToLeft);
43
+
39
44
  class SafeAreas {
40
45
  public:
41
46
  virtual ~SafeAreas() = default;
@@ -53,6 +53,11 @@ ScrollerVisibility scrollerVisibility(
53
53
  // The gutter the visible scrollers take: their width for legacy scrollers, nothing for overlay.
54
54
  ScrollerGutter scrollerGutter(ScrollerStyle style, float legacyWidth, const ScrollerVisibility &visibility);
55
55
 
56
+ // Where a scroll view with `pagingEnabled` settles along one axis when a drag ends at `offset`,
57
+ // moving at `velocity` (points a millisecond, positive further into the content): the nearest page,
58
+ // or the next one in the drag's direction when it was flicked, within [0, `max`].
59
+ float pagedOffset(float offset, float velocity, float page, float max);
60
+
56
61
  // `style` with `gutter` added to the padding Yoga reads for the right and bottom edges in
57
62
  // `direction`. A percentage or `auto` there is left alone.
58
63
  facebook::yoga::Style withGutter(
@@ -0,0 +1,51 @@
1
+ // What a surface is laid out at: its size, scale, text size and direction. Header-only, and
2
+ // toolkit-free.
3
+ #pragma once
4
+
5
+ #include <react/renderer/core/LayoutConstraints.h>
6
+ #include <react/renderer/graphics/Size.h>
7
+
8
+ #include <atomic>
9
+ #include <functional>
10
+ #include <utility>
11
+
12
+ namespace ngn {
13
+
14
+ struct SurfaceMetrics {
15
+ facebook::react::Size size{};
16
+ // Device pixels per point.
17
+ float pointScale{2};
18
+ // The text size multiplier.
19
+ float fontScale{1};
20
+ };
21
+
22
+ // The surface's root constraints: its size, and the direction its layout starts from, which React
23
+ // Native's iOS surfaces take from I18nManager.
24
+ inline facebook::react::LayoutConstraints surfaceConstraints(const SurfaceMetrics &metrics, bool rightToLeft) {
25
+ using facebook::react::LayoutDirection;
26
+ return {
27
+ .minimumSize = metrics.size,
28
+ .maximumSize = metrics.size,
29
+ .layoutDirection = rightToLeft ? LayoutDirection::RightToLeft : LayoutDirection::LeftToRight,
30
+ };
31
+ }
32
+
33
+ // The direction a runtime's surfaces lay out from: what `read` answers as each JavaScript instance
34
+ // starts (the first load and each reload), kept for that instance's run as I18nManager's constants
35
+ // are, so a surface started or resized meanwhile agrees with what JavaScript reads.
36
+ class SurfaceDirection {
37
+ public:
38
+ explicit SurfaceDirection(std::function<bool()> read) : read_(std::move(read)) {}
39
+ void instanceStarted() {
40
+ rightToLeft_ = read_ && read_();
41
+ }
42
+ bool rightToLeft() const {
43
+ return rightToLeft_;
44
+ }
45
+
46
+ private:
47
+ std::function<bool()> read_;
48
+ std::atomic<bool> rightToLeft_{false};
49
+ };
50
+
51
+ } // namespace ngn
@@ -6,9 +6,9 @@
6
6
  // backend's view for the RNSVGSvgView draws it by calling drawSvg with an SvgCanvas over its own
7
7
  // graphics context. Everything SVG means (inheritance through `propList`, `currentColor`,
8
8
  // lengths and percentages, the viewBox, path data and arcs, gradients and their units, clip
9
- // paths, masks, `use` and `symbol`, markers, patterns and filters) is decided here; the canvas only
10
- // draws paths, gradients, images and runs of text, tiles a pattern's tile, and runs a filter's
11
- // steps over pixels with runSvgFilter.
9
+ // paths, masks, `use` and `symbol`, markers, patterns and filters, text anchors and text along a
10
+ // path) is decided here; the canvas only measures and draws runs of text, draws paths, gradients
11
+ // and images, tiles a pattern's tile, and runs a filter's steps over pixels with runSvgFilter.
12
12
  #pragma once
13
13
 
14
14
  #include "ngn/view-tree.h"
@@ -112,8 +112,11 @@ enum class SvgKind {
112
112
  Mask,
113
113
  Symbol,
114
114
  Image,
115
+ // A box of native views: what is in it is views, not svg elements, which the canvas draws.
116
+ ForeignObject,
115
117
  Text,
116
118
  TSpan,
119
+ TextPath,
117
120
  Marker,
118
121
  Pattern,
119
122
  Filter,
@@ -200,11 +203,19 @@ struct SvgElement {
200
203
 
201
204
  std::string uri; // an image's source
202
205
 
203
- // Text: a span's characters, the font, and the positions written on the element.
206
+ // A foreignObject's own view, whose children the canvas draws.
207
+ Tag view{-1};
208
+
209
+ // Text: a span's characters, the font, its text-anchor ("start", "middle" or "end"; empty
210
+ // inherits), and the positions written on the element. A textPath's path is `href`, how far
211
+ // along it the text starts is the length "startOffset", and `side` "right" sets it along the
212
+ // path reversed.
204
213
  std::string content;
205
214
  SvgLength fontSize;
206
215
  std::string fontFamily;
207
216
  std::string fontWeight;
217
+ std::string textAnchor;
218
+ std::string side;
208
219
  std::vector<SvgLength> xs, ys, dxs, dys;
209
220
 
210
221
  bool operator==(const SvgElement &) const = default;
@@ -267,6 +278,16 @@ struct SvgMarkerVertex {
267
278
  // The vertices a path's markers are drawn at, in order: the first is the start, the last the end.
268
279
  std::vector<SvgMarkerVertex> svgMarkerVertices(const SvgPath &path);
269
280
 
281
+ // A glyph as text along a path draws it: what draws as one (a grapheme cluster, or the clusters a
282
+ // ligature joins), where it starts in its shaped run and its advance there, kerning included, and
283
+ // the canvas's own record of the glyphs the run shaped for it, which glyphCluster draws.
284
+ struct SvgGlyphCluster {
285
+ std::string text;
286
+ double x{0};
287
+ double advance{0};
288
+ std::shared_ptr<const void> glyphs;
289
+ };
290
+
270
291
  // A pattern's tile: what one tile covers in the pattern's own space, and from that space to the
271
292
  // user space the pattern fills.
272
293
  struct SvgPatternTile {
@@ -347,15 +368,26 @@ class SvgCanvas {
347
368
  virtual void beginFilter(const SvgFilter &filter) = 0;
348
369
  virtual void endFilter() = 0;
349
370
  virtual void image(const std::string &uri, const SvgRect &rect, const std::string &align, int meetOrSlice, double opacity) = 0;
350
- // Draws `text` with its baseline's start at (x, y) and answers its advance.
351
- virtual double text(
371
+ // The advance of `text` in the font, as text() draws it.
372
+ virtual double measureText(const std::string &text, double fontSize, const std::string &fontFamily, const std::string &fontWeight) = 0;
373
+ // `text` shaped in the font as one run, split into the glyphs text along a path draws one at a
374
+ // time, in the order of the text.
375
+ virtual std::vector<SvgGlyphCluster> glyphClusters(
376
+ const std::string &text, double fontSize, const std::string &fontFamily, const std::string &fontWeight) = 0;
377
+ // Draws a cluster's glyphs as its run shaped them, contextual forms kept, with the cluster's start
378
+ // on the baseline at (x, y).
379
+ virtual void glyphCluster(const SvgGlyphCluster &cluster, double x, double y, const SvgPaint &fill) = 0;
380
+ // Draws `text` with its baseline's start at (x, y).
381
+ virtual void text(
352
382
  const std::string &text,
353
383
  double x,
354
384
  double y,
355
385
  double fontSize,
356
386
  const std::string &fontFamily,
357
387
  const std::string &fontWeight,
358
- const SvgPaint *fill) = 0;
388
+ const SvgPaint &fill) = 0;
389
+ // Draws the native views in `view` (a foreignObject's), each at its frame in `view`.
390
+ virtual void views(Tag view) = 0;
359
391
  };
360
392
 
361
393
  // Draws `document` into a viewport of `width` by `height`, its origin at the canvas's.
@@ -372,21 +404,30 @@ class SvgDrawings {
372
404
 
373
405
  // A view was made or its props changed, or `parent`'s children changed.
374
406
  void changed(const ViewTree &tree, Tag tag);
407
+ // A view's layout or state changed: what it draws, not what it is.
408
+ void relaidOut(const ViewTree &tree, Tag tag);
375
409
  void deleted(Tag tag);
376
410
 
377
411
  struct Drawing {
378
412
  Tag svg{-1};
379
413
  std::shared_ptr<const SvgDocument> document;
380
414
  };
381
- // The svgs whose documents changed since the last call.
415
+ // The svgs whose documents changed since the last call, and those whose documents are the same
416
+ // but draw a view in a foreignObject that changed.
382
417
  std::vector<Drawing> settle(const ViewTree &tree);
383
418
 
384
419
  private:
385
420
  std::optional<SvgElement> element(const ViewTree &tree, Tag tag) const;
386
421
  std::optional<SvgNode> node(const ViewTree &tree, Tag tag) const;
422
+ void mark(const ViewTree &tree, Tag tag, bool repaint);
423
+ void repaintAbove(const ViewTree &tree, Tag tag);
387
424
 
388
425
  Reader reader_;
389
426
  std::unordered_set<Tag> dirty_;
427
+ // Svgs to draw again though their documents are the same.
428
+ std::unordered_set<Tag> repaint_;
429
+ // The foreignObjects mounted, whose views are drawn into their svgs.
430
+ std::unordered_set<Tag> foreign_;
390
431
  std::unordered_map<Tag, std::shared_ptr<const SvgDocument>> drawn_;
391
432
  };
392
433
 
@@ -82,6 +82,15 @@ struct TextInputOptions {
82
82
  // secure entry turns every substitution and check off and never capitalizes.
83
83
  TextInputOptions textInputOptions(const DesktopTextInputProps &props);
84
84
 
85
+ // Where a field puts its text and placeholder, as React Native iOS has a TextInput's: Left, Right,
86
+ // Center or Justified. A TextInput's text attributes carry no layout direction, so an alignment that
87
+ // is set resolves as RCTResolveTextAlignment resolves a left-to-right fragment's (natural and start
88
+ // left, end right, left and right as they are). With none set, UIKit's natural alignment follows the
89
+ // view's direction, the layout's: right in a right-to-left layout, left otherwise.
90
+ facebook::react::TextAlignment inputAlignment(
91
+ std::optional<facebook::react::TextAlignment> alignment,
92
+ facebook::react::LayoutDirection layoutDirection);
93
+
85
94
  // How much of an insertion of `inserted` units, replacing `replaced` of the field's `current`, fits
86
95
  // in `maxLength`: all of it, a leading part, or none.
87
96
  std::size_t insertionThatFits(
@@ -8,6 +8,7 @@
8
8
  #include <react/renderer/attributedstring/AttributedString.h>
9
9
  #include <react/renderer/attributedstring/ParagraphAttributes.h>
10
10
  #include <react/renderer/graphics/Size.h>
11
+ #include <react/renderer/textlayoutmanager/TextMeasureCache.h>
11
12
  #include <react/utils/ContextContainer.h>
12
13
 
13
14
  #include <memory>
@@ -24,6 +25,16 @@ class TextMeasurer {
24
25
  const facebook::react::AttributedString &text,
25
26
  const facebook::react::ParagraphAttributes &paragraph,
26
27
  facebook::react::Float maxWidth) const = 0;
28
+
29
+ // The lines the text lays out in at `width`, each with its frame and its first font's metrics,
30
+ // y measured from the top of the text: what a baseline and onTextLayout read. None by default,
31
+ // which reads as a baseline at the top.
32
+ virtual facebook::react::LinesMeasurements lines(
33
+ const facebook::react::AttributedString &text,
34
+ const facebook::react::ParagraphAttributes &paragraph,
35
+ facebook::react::Float width) const {
36
+ return {};
37
+ }
27
38
  };
28
39
 
29
40
  // The ContextContainer key the core's TextLayoutManager reads the measurer from.
@@ -70,6 +70,9 @@ struct WebViewConfig {
70
70
  std::string applicationNameForUserAgent;
71
71
  // Nothing when the prop is not set, and then no origin is refused.
72
72
  std::optional<std::vector<std::string>> originWhitelist;
73
+ // Whether a navigation waits for the app's answer (shouldStartLoadWithRequest), as
74
+ // react-native-webview's does. Expo's DOM web view (dom-web-view.h) lets each one go ahead.
75
+ bool asksApp{true};
73
76
  bool operator==(const WebViewConfig &) const = default;
74
77
  };
75
78
 
@@ -85,8 +88,9 @@ bool passesOriginWhitelist(const std::vector<std::string> &whitelist, const std:
85
88
  // What a navigation to `url` waits for: the app's answer, or nothing. A page navigation outside
86
89
  // `originWhitelist` is handed to the OS only when the user made it (`navigationType`, the library's
87
90
  // name for it, is "click" or "formsubmit") and Linking would open it (externalUrlAllowed); any
88
- // other is cancelled, so a page's script cannot open a scheme or a file on its own.
89
- enum class NavigationStep { AskApp, OpenExternally, Cancel };
91
+ // other is cancelled, so a page's script cannot open a scheme or a file on its own. Any other
92
+ // navigation is the app's to answer, or, without `asksApp`, goes ahead.
93
+ enum class NavigationStep { AskApp, Allow, OpenExternally, Cancel };
90
94
  NavigationStep navigationStep(const WebViewConfig &config, const std::string &url, bool topFrame, std::string_view navigationType);
91
95
 
92
96
  // The script, at document start, that gives the page window.ReactNativeWebView.postMessage, sent
@@ -169,14 +169,16 @@ std::vector<MenuItem> parseMenuBar(const folly::dynamic &groups, const DefaultMe
169
169
  return bar;
170
170
  }
171
171
 
172
- std::optional<NavigationDirection> navigationFor(NavigationInput input) {
172
+ std::optional<NavigationDirection> navigationFor(NavigationInput input, bool rightToLeft) {
173
173
  switch (input) {
174
174
  case NavigationInput::MouseButton4:
175
- case NavigationInput::SwipeRight:
176
175
  return NavigationDirection::Back;
177
176
  case NavigationInput::MouseButton5:
178
- case NavigationInput::SwipeLeft:
179
177
  return NavigationDirection::Forward;
178
+ case NavigationInput::SwipeRight:
179
+ return rightToLeft ? NavigationDirection::Forward : NavigationDirection::Back;
180
+ case NavigationInput::SwipeLeft:
181
+ return rightToLeft ? NavigationDirection::Back : NavigationDirection::Forward;
180
182
  }
181
183
  return std::nullopt;
182
184
  }
@@ -304,6 +306,11 @@ std::vector<HistoryMenuItem> historyMenu(const WindowHistory &history, Navigatio
304
306
  return items;
305
307
  }
306
308
 
309
+ NavigationDirection chevronAt(double x, double width, bool rightToLeft) {
310
+ bool left = x < width / 2;
311
+ return left != rightToLeft ? NavigationDirection::Back : NavigationDirection::Forward;
312
+ }
313
+
307
314
  std::shared_ptr<void> MenuHost::listen(Listener listener) {
308
315
  return listeners_.add(std::move(listener));
309
316
  }