@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
@@ -16,6 +16,7 @@
16
16
  #import "symbol-view.h"
17
17
  #import "glass-view.h"
18
18
 
19
+ #include "ngn/dom-web-view.h"
19
20
  #include "ngn/glass.h"
20
21
  #import "web-view-component.h"
21
22
  #import "expo-image-view.h"
@@ -742,6 +743,25 @@ NSFont *inputFont(const DesktopTextInputProps &p) {
742
743
  return font;
743
744
  }
744
745
 
746
+ // How a TextInput aligns its text and placeholder (inputAlignment), in a layout running `direction`,
747
+ // and the base writing direction its `writingDirection` sets, natural when unset.
748
+ void alignInput(id field, const DesktopTextInputProps &p, LayoutDirection direction) {
749
+ NSTextAlignment alignment = NSTextAlignmentLeft;
750
+ switch (ngn::inputAlignment(p.textAttributes.alignment, direction)) {
751
+ case TextAlignment::Right: alignment = NSTextAlignmentRight; break;
752
+ case TextAlignment::Center: alignment = NSTextAlignmentCenter; break;
753
+ case TextAlignment::Justified: alignment = NSTextAlignmentJustified; break;
754
+ default: break;
755
+ }
756
+ NSWritingDirection writing = NSWritingDirectionNatural;
757
+ if (p.textAttributes.baseWritingDirection == WritingDirection::LeftToRight) {
758
+ writing = NSWritingDirectionLeftToRight;
759
+ } else if (p.textAttributes.baseWritingDirection == WritingDirection::RightToLeft) {
760
+ writing = NSWritingDirectionRightToLeft;
761
+ }
762
+ [field setTextAlignment:alignment writingDirection:writing];
763
+ }
764
+
745
765
  // The `selection` prop, when it is new in this update: React Native iOS applies it only then.
746
766
  std::optional<NSRange> newSelection(const Props::Shared &old, const DesktopTextInputProps &p) {
747
767
  if (!p.selection) {
@@ -810,6 +830,7 @@ std::optional<NSRange> newSelection(const Props::Shared &old, const DesktopTextI
810
830
  field.font = inputFont(*p);
811
831
  field.textColor = attrs.foregroundColor ? nsColor(attrs.foregroundColor) : NSColor.textColor;
812
832
  [_box setPlaceholder:nsString(p->placeholder) color:p->placeholderTextColor ? nsColor(p->placeholderTextColor) : nil];
833
+ alignInput(_box, *p, self.layoutMetrics.layoutDirection);
813
834
  field.editable = p->editable && !p->readOnly;
814
835
  [_box applyOptions:ngn::textInputOptions(*p)];
815
836
  // The text prop wins only when it is not older than what the field has sent (React Native iOS's
@@ -827,6 +848,9 @@ std::optional<NSRange> newSelection(const Props::Shared &old, const DesktopTextI
827
848
 
828
849
  - (void)updateLayout:(const LayoutMetrics &)metrics {
829
850
  [super updateLayout:metrics];
851
+ if (auto p = std::dynamic_pointer_cast<const DesktopTextInputProps>(self.props)) {
852
+ alignInput(_box, *p, metrics.layoutDirection);
853
+ }
830
854
  const auto &i = metrics.contentInsets;
831
855
  NSRect content = NSMakeRect(
832
856
  i.left, i.top, metrics.frame.size.width - i.left - i.right, metrics.frame.size.height - i.top - i.bottom);
@@ -993,6 +1017,7 @@ std::optional<NSRange> newSelection(const Props::Shared &old, const DesktopTextI
993
1017
  _text.font = inputFont(*p);
994
1018
  _text.textColor = attrs.foregroundColor ? nsColor(attrs.foregroundColor) : NSColor.textColor;
995
1019
  [_text setPlaceholder:nsString(p->placeholder) color:p->placeholderTextColor ? nsColor(p->placeholderTextColor) : nil];
1020
+ alignInput(_text, *p, self.layoutMetrics.layoutDirection);
996
1021
  _text.editable = p->editable && !p->readOnly;
997
1022
  [_text applyOptions:ngn::textInputOptions(*p)];
998
1023
  if (ngn::controlledTextIsCurrent(p->mostRecentEventCount, _eventCount, _text.hasMarkedText) && !p->text.empty()) {
@@ -1010,6 +1035,9 @@ std::optional<NSRange> newSelection(const Props::Shared &old, const DesktopTextI
1010
1035
 
1011
1036
  - (void)updateLayout:(const LayoutMetrics &)metrics {
1012
1037
  [super updateLayout:metrics];
1038
+ if (auto p = std::dynamic_pointer_cast<const DesktopTextInputProps>(self.props)) {
1039
+ alignInput(_text, *p, metrics.layoutDirection);
1040
+ }
1013
1041
  const auto &i = metrics.contentInsets;
1014
1042
  _scroll.frame = NSMakeRect(
1015
1043
  i.left, i.top, metrics.frame.size.width - i.left - i.right, metrics.frame.size.height - i.top - i.bottom);
@@ -1190,7 +1218,7 @@ NGNView *NGNMakeView(const ShadowView &view) {
1190
1218
  if (name == ngn::ExpoImageComponentName) {
1191
1219
  return [[NGNExpoImageView alloc] initWithShadowView:view];
1192
1220
  }
1193
- if (name == "RNCWebView") {
1221
+ if (name == "RNCWebView" || name == ngn::DomWebViewComponentName) {
1194
1222
  return [[NGNWebView alloc] initWithShadowView:view];
1195
1223
  }
1196
1224
  if (Class library = libraryViews()[@(view.componentName)]) {
@@ -1,6 +1,8 @@
1
1
  #import "image-view.h"
2
2
  #import "image-request.h"
3
3
 
4
+ #import <QuartzCore/QuartzCore.h>
5
+
4
6
  #include "ngn/file-system.h"
5
7
 
6
8
  #include <react/renderer/components/image/ImageEventEmitter.h>
@@ -14,6 +16,10 @@ using namespace facebook::react;
14
16
  std::optional<ImageSource> _source;
15
17
  NSURLSessionDataTask *_task;
16
18
  NSImage *_image;
19
+ // Holds the image under the view's children. The view's own layer is no place for it: NGNView
20
+ // draws its box in drawRect:, and each redraw (every layout asks for one) replaces the layer's
21
+ // contents, so an image that loaded before the redraw was wiped.
22
+ CALayer *_imageLayer;
17
23
  // Moved on by each load: a completion already queued for an earlier one, even of the same uri,
18
24
  // is ignored.
19
25
  uint64_t _load;
@@ -23,6 +29,24 @@ using namespace facebook::react;
23
29
  [_task cancel];
24
30
  }
25
31
 
32
+ - (CALayer *)imageLayer {
33
+ if (_imageLayer == nil) {
34
+ _imageLayer = [CALayer layer];
35
+ _imageLayer.contentsGravity = self.layer.contentsGravity;
36
+ _imageLayer.frame = self.layer.bounds;
37
+ [self.layer insertSublayer:_imageLayer atIndex:0];
38
+ }
39
+ return _imageLayer;
40
+ }
41
+
42
+ - (void)updateLayout:(const LayoutMetrics &)metrics {
43
+ [super updateLayout:metrics];
44
+ [CATransaction begin];
45
+ [CATransaction setDisableActions:YES];
46
+ _imageLayer.frame = self.layer.bounds;
47
+ [CATransaction commit];
48
+ }
49
+
26
50
  - (std::shared_ptr<const ImageEventEmitter>)imageEmitter {
27
51
  return std::dynamic_pointer_cast<const ImageEventEmitter>(self.eventEmitter);
28
52
  }
@@ -48,6 +72,7 @@ using namespace facebook::react;
48
72
  break;
49
73
  }
50
74
  self.layer.masksToBounds = YES;
75
+ _imageLayer.contentsGravity = self.layer.contentsGravity;
51
76
  // The source whose scale is nearest the screen's, as React Native iOS picks.
52
77
  double screenScale = self.window ? self.window.backingScaleFactor : NSScreen.mainScreen.backingScaleFactor;
53
78
  auto source = ngn::appkit::pickImageSource(p->sources, screenScale);
@@ -63,7 +88,7 @@ using namespace facebook::react;
63
88
  [_task cancel];
64
89
  _task = nil;
65
90
  _load++;
66
- self.layer.contents = nil;
91
+ _imageLayer.contents = nil;
67
92
  // A file is drawn only inside the file boundary, and exactly the file checked.
68
93
  std::shared_ptr<ngn::FileAccess::Use> use;
69
94
  NSString *load = _uri;
@@ -118,11 +143,15 @@ using namespace facebook::react;
118
143
 
119
144
  - (void)loaded:(NSImage *)image {
120
145
  _image = image;
121
- self.layer.contents = image;
146
+ CALayer *layer = [self imageLayer];
147
+ layer.contentsGravity = self.layer.contentsGravity;
148
+ layer.contents = image;
122
149
  if (auto e = [self imageEmitter]) {
123
150
  NSImageRep *rep = image.representations.firstObject;
124
151
  ImageSource source;
125
152
  source.uri = std::string(_uri.UTF8String);
153
+ // In pixels, at a scale of 1: ImageSource's default scale of 3 would report three times the size.
154
+ source.scale = 1;
126
155
  source.size = {static_cast<Float>(rep ? rep.pixelsWide : image.size.width),
127
156
  static_cast<Float>(rep ? rep.pixelsHigh : image.size.height)};
128
157
  e->onLoad(source);
@@ -48,6 +48,7 @@
48
48
  #import "image-manipulator.h"
49
49
  #import "printing.h"
50
50
  #import "expo-image-view.h"
51
+ #import "camera-sheet.h"
51
52
  #import "camera-view.h"
52
53
  #import "camera.h"
53
54
  #import "map-view.h"
@@ -68,6 +69,7 @@
68
69
  #import "test-launch.h"
69
70
  #import "updates.h"
70
71
  #import "surface-view.h"
72
+ #import "tab-walk.h"
71
73
  #include "text-layout.h"
72
74
 
73
75
  #include "ngn/app-menus.h"
@@ -211,71 +213,13 @@ void logFirstFrame(NSWindow *window) {
211
213
  }
212
214
  }
213
215
 
214
- // The container the focus is in, and the view holding it, in `window`. An in-window cover (a modal)
215
- // is the container while it is up, wherever the first responder is: Tab stays inside it.
216
- struct FocusContext {
217
- NGNSurfaceView *container = nil;
218
- NGNView *focused = nil;
219
- };
220
-
221
- FocusContext focusContext(NSWindow *window) {
222
- FocusContext context;
223
- NSResponder *first = window.firstResponder;
224
- // A field editor stands in for its text field, inside the field's view.
225
- for (NSView *v = [first isKindOfClass:NSView.class] ? (NSView *)first : nil; v != nil; v = v.superview) {
226
- if (context.focused == nil && [v isKindOfClass:NGNView.class]) {
227
- context.focused = (NGNView *)v;
228
- }
229
- if ([v isKindOfClass:NGNSurfaceView.class]) {
230
- context.container = (NGNSurfaceView *)v;
231
- break;
232
- }
233
- }
234
- if (context.container == nil) {
235
- context.container =
236
- [window.contentView isKindOfClass:NGNSurfaceView.class] ? (NGNSurfaceView *)window.contentView : nil;
237
- }
238
- NSView *cover = [NGNModalHostView frontCoverIn:window];
239
- if (cover != nil && ![context.container isDescendantOf:cover]) {
240
- context.container = (NGNSurfaceView *)cover;
241
- context.focused = nil;
242
- }
243
- return context;
244
- }
245
-
246
- // Tab and Shift-Tab, walked in the core (focus-order.h) over the container the focus is in, in
247
- // place of AppKit's key-view loop. True when the key is used: the focus moved, or it is inside a
248
- // sheet, cover or popover, which Tab never leaves.
216
+ // Tab and Shift-Tab (tab-walk.h). True when the key is used.
249
217
  bool moveFocus(NSEvent *event) {
250
- NSWindow *window = event.window;
251
- auto [container, focused] = focusContext(window);
252
- if (container == nil) {
253
- return false;
254
- }
255
- bool contained = [NGNModalHostView isPresentationContainer:container];
256
218
  // Option-Tab reaches every focusable view, whatever the keyboard navigation setting.
257
219
  bool everything = NSApp.isFullKeyboardAccessEnabled || (event.modifierFlags & NSEventModifierFlagOption) != 0;
258
220
  auto reach = everything ? ngn::TabReach::All : ngn::TabReach::TextInputs;
259
221
  bool backwards = (event.modifierFlags & NSEventModifierFlagShift) != 0;
260
- const auto &tree = gHost.mounting->tree();
261
- std::optional<Tag> from;
262
- if (focused != nil) {
263
- from = static_cast<Tag>(focused.reactTag);
264
- }
265
- auto count = ngn::tabOrder(tree, container.rootTag, reach, from).size();
266
- // A control AppKit will not focus (a button, with keyboard navigation off) passes Tab on.
267
- for (size_t tries = 0; tries < count; tries++) {
268
- auto next = ngn::nextInTabOrder(tree, container.rootTag, from, backwards, reach);
269
- if (!next) {
270
- return contained;
271
- }
272
- NGNView *view = gHost.backend->view(*next);
273
- if (view != nil && [window makeFirstResponder:view.keyboardResponder]) {
274
- return true;
275
- }
276
- from = next;
277
- }
278
- return contained;
222
+ return ngn::appkit::moveFocus(event.window, gHost.mounting->tree(), *gHost.backend, backwards, reach);
279
223
  }
280
224
 
281
225
  // Arrow keys, Home and End inside a composite widget (a tablist, radiogroup, menu, menubar, listbox
@@ -291,7 +235,7 @@ bool moveInComposite(NSEvent *event) {
291
235
  return false;
292
236
  }
293
237
  NSWindow *window = event.window;
294
- auto [container, focused] = focusContext(window);
238
+ auto [container, focused] = ngn::appkit::focusContext(window);
295
239
  if (container == nil || focused == nil || window.firstResponder != focused) {
296
240
  return false;
297
241
  }
@@ -394,6 +338,8 @@ struct Swipe {
394
338
  NGNView *below;
395
339
  NSWindow *window;
396
340
  SurfaceId surface = 0;
341
+ // The direction the swipe began in, which its later moves keep though a reload changes the app's.
342
+ bool rightToLeft = false;
397
343
  // The momentum scrolls after a swipe's fingers lift are the swipe's, not the content's.
398
344
  bool swallowMomentum = false;
399
345
  // Bumped as a swipe starts or ends, so a late animation step or timeout of an old one stops.
@@ -457,6 +403,11 @@ double fingersMovedRight(NSEvent *event) {
457
403
  return event.isDirectionInvertedFromDevice ? event.scrollingDeltaX : -event.scrollingDeltaX;
458
404
  }
459
405
 
406
+ // Whether the app lays out right to left, which turns the swipes round: left goes back.
407
+ bool swipesRightToLeft() {
408
+ return gHost.runtime && gHost.runtime->laysOutRightToLeft();
409
+ }
410
+
460
411
  // The fingers lifted from a tracking swipe: it goes back, or slides home.
461
412
  void liftSwipe(NSEvent *event) {
462
413
  bool back = gSwipe.swipe.lifted(event.phase == NSEventPhaseCancelled);
@@ -506,7 +457,7 @@ NSEvent *swipeScroll(NSEvent *event, NSWindow *window) {
506
457
  if (window != gSwipe.window) {
507
458
  return event;
508
459
  }
509
- double dx = fingersMovedRight(event);
460
+ double dx = ngn::towardsBack(fingersMovedRight(event), gSwipe.rightToLeft);
510
461
  if (dx != 0) {
511
462
  drawSwipe(gSwipe.swipe.moved(dx));
512
463
  }
@@ -530,23 +481,25 @@ NSEvent *swipeScroll(NSEvent *event, NSWindow *window) {
530
481
  auto history = gHost.menus->history(*surface);
531
482
  bool canGoBack = !history || !history->back.empty();
532
483
  bool canGoForward = !history || !history->forward.empty();
533
- double dx = fingersMovedRight(event);
484
+ bool rightToLeft = swipesRightToLeft();
485
+ double dx = ngn::towardsBack(fingersMovedRight(event), rightToLeft);
534
486
  if (dx > 0 && canGoBack && gSwipe.swipe.phase() == ngn::BackSwipe::Phase::Idle) {
535
487
  auto target = gHost.mounting->backSwipeTarget(*surface);
536
488
  NGNView *top = target ? gHost.backend->view(target->top) : nil;
537
489
  NGNView *below = target ? gHost.backend->view(target->below) : nil;
538
- if (top != nil && below != nil && gSwipe.swipe.begin(target, NSWidth(top.bounds))) {
490
+ if (top != nil && below != nil && gSwipe.swipe.begin(target, NSWidth(top.bounds), rightToLeft)) {
539
491
  endSwipe();
540
492
  gSwipe.top = top;
541
493
  gSwipe.below = below;
542
494
  gSwipe.window = window;
543
495
  gSwipe.surface = *surface;
496
+ gSwipe.rightToLeft = rightToLeft;
544
497
  gSwipe.swallowMomentum = true;
545
498
  drawSwipe(gSwipe.swipe.moved(dx));
546
499
  return nil;
547
500
  }
548
501
  }
549
- auto bounds = ngn::swipeBounds(canGoBack, canGoForward);
502
+ auto bounds = ngn::swipeBounds(canGoBack, canGoForward, rightToLeft);
550
503
  if (bounds == ngn::SwipeBounds{}) {
551
504
  return event;
552
505
  }
@@ -556,7 +509,8 @@ NSEvent *swipeScroll(NSEvent *event, NSWindow *window) {
556
509
  usingHandler:^(CGFloat amount, NSEventPhase phase, BOOL complete, BOOL *stop) {
557
510
  if (phase == NSEventPhaseEnded && amount != 0) {
558
511
  navigate(*ngn::navigationFor(
559
- amount > 0 ? ngn::NavigationInput::SwipeRight : ngn::NavigationInput::SwipeLeft),
512
+ amount > 0 ? ngn::NavigationInput::SwipeRight : ngn::NavigationInput::SwipeLeft,
513
+ rightToLeft),
560
514
  window);
561
515
  }
562
516
  }];
@@ -1258,7 +1212,11 @@ int main(int argc, const char *argv[]) {
1258
1212
  ngn::addModule(*modules, "NgNativeSplashScreen", [] { return ngn::makeSplashScreenModule(gSplash); });
1259
1213
  static auto photos = ngn::appkit::makePhotoLibrary(caches);
1260
1214
  ngn::addModule(*modules, "NgNativeMediaLibrary", [] { return ngn::makeMediaLibraryModule(photos, noPrompts, fileAccess); });
1261
- ngn::addModule(*modules, "NgNativeImagePicker", [] { return ngn::makeImagePickerModule(photos, noPrompts); });
1215
+ static auto cameraHardware = ngn::appkit::makeCameraHardware();
1216
+ static auto cameraSheet = ngn::appkit::makeCameraSheet();
1217
+ ngn::addModule(*modules, "NgNativeImagePicker", [] {
1218
+ return ngn::makeImagePickerModule(photos, noPrompts, ngn::PickerCamera{.hardware = cameraHardware, .microphone = audioInput, .sheet = cameraSheet, .folder = caches + "/ImagePicker"});
1219
+ });
1262
1220
  static auto tracking = ngn::appkit::makeTrackingAuthority();
1263
1221
  ngn::addModule(*modules, "NgNativeTrackingTransparency", [] { return ngn::makeTrackingModule(tracking, noPrompts); });
1264
1222
  static auto imageHttp = network.http;
@@ -1275,7 +1233,6 @@ int main(int argc, const char *argv[]) {
1275
1233
  });
1276
1234
  // <expo-image>'s bytes: 64 MB of them in memory, the rest on disk in the caches.
1277
1235
  [NGNExpoImageView useHttp:imageHttp cache:std::make_shared<ngn::ImageCache>(caches + "/ExpoImage", 64 << 20)];
1278
- static auto cameraHardware = ngn::appkit::makeCameraHardware();
1279
1236
  static auto cameraViews = std::make_shared<ngn::CameraViews>();
1280
1237
  [NGNCameraView useHardware:cameraHardware views:cameraViews noPrompts:noPrompts];
1281
1238
  ngn::addModule(*modules, "NgNativeCamera", [] { return ngn::makeCameraModule(cameraHardware, cameraViews, caches + "/Camera", noPrompts); });
@@ -1349,6 +1306,9 @@ int main(int argc, const char *argv[]) {
1349
1306
  ngn::DebuggerIdentity{
1350
1307
  .appId = appId,
1351
1308
  .deviceName = NSHost.currentHost.localizedName ? NSHost.currentHost.localizedName.UTF8String : "Mac"});
1309
+ // Each surface lays out the way I18nManager.isRTL says for the instance running, read again
1310
+ // as a reload starts the next.
1311
+ gHost.runtime->rightToLeft = [system] { return ngn::appRightToLeft(*system); };
1352
1312
  // The first launch owes its first frame from the instance the runtime just began.
1353
1313
  gHost.launchFrame.loaded(gHost.updates->launchGeneration(), gHost.mounting->instance());
1354
1314
 
@@ -1497,7 +1457,7 @@ int main(int argc, const char *argv[]) {
1497
1457
  }
1498
1458
  auto input = event.deltaX > 0 ? ngn::NavigationInput::SwipeRight
1499
1459
  : ngn::NavigationInput::SwipeLeft;
1500
- navigate(*ngn::navigationFor(input), event.window);
1460
+ navigate(*ngn::navigationFor(input, swipesRightToLeft()), event.window);
1501
1461
  return nil;
1502
1462
  }];
1503
1463
  // "Swipe between pages" with two fingers (swipeScroll), for real scrolls and replayed ones.
@@ -1,5 +1,7 @@
1
1
  #include "photo-library.h"
2
2
 
3
+ #import "camera.h"
4
+
3
5
  #include <algorithm>
4
6
 
5
7
  #import <AVFoundation/AVFoundation.h>
@@ -501,12 +503,10 @@ class PhotoKitLibrary final : public ngn::PhotoLibrary {
501
503
  item[@"assetId"] = result.assetIdentifier;
502
504
  }
503
505
  if (video) {
504
- AVURLAsset *asset = [AVURLAsset URLAssetWithURL:copy options:nil];
505
- AVAssetTrack *track = [asset tracksWithMediaType:AVMediaTypeVideo].firstObject;
506
- CGSize size = CGSizeApplyAffineTransform(track.naturalSize, track.preferredTransform);
507
- item[@"width"] = @(fabs(size.width));
508
- item[@"height"] = @(fabs(size.height));
509
- item[@"duration"] = @(CMTimeGetSeconds(asset.duration) * 1000);
506
+ auto clip = ngn::appkit::movieClip(copy.path.UTF8String).value_or(ngn::CameraClip{});
507
+ item[@"width"] = @(clip.width);
508
+ item[@"height"] = @(clip.height);
509
+ item[@"duration"] = @(clip.duration);
510
510
  } else if (CGImageSourceRef source = CGImageSourceCreateWithURL((__bridge CFURLRef)copy, nil)) {
511
511
  NSDictionary *properties = CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source, 0, nil));
512
512
  CFRelease(source);
@@ -65,6 +65,9 @@ std::function<void(NSInteger, CGPoint)> gOffsetChanged;
65
65
  object:_scrollView.contentView];
66
66
  [self addSubview:_scrollView];
67
67
  __weak NGNScrollView *weakSelf = self;
68
+ _scrollView.phaseChanged = ^(NGNScrollPhase phase, NSPoint velocity) {
69
+ [weakSelf report:phase velocity:velocity];
70
+ };
68
71
  _styleObserver = [[NGNScrollerStyleObserver alloc] initWithChanged:^{
69
72
  [weakSelf updateScrollers];
70
73
  }];
@@ -94,6 +97,7 @@ std::function<void(NSInteger, CGPoint)> gOffsetChanged;
94
97
  _scrollView.horizontalScrollElasticity = _scrollView.verticalScrollElasticity;
95
98
  // The inner scroll view takes the wheel before this view does.
96
99
  _scrollView.scrollingDisabled = !p->scrollEnabled;
100
+ _scrollView.pagingEnabled = p->pagingEnabled;
97
101
  if (_offsetProp != p->contentOffset) {
98
102
  bool first = !_offsetProp;
99
103
  _offsetProp = p->contentOffset;
@@ -198,15 +202,6 @@ std::function<void(NSInteger, CGPoint)> gOffsetChanged;
198
202
  [self applyPendingOffsetAfterMount];
199
203
  }
200
204
 
201
- - (void)scrollWheel:(NSEvent *)event {
202
- auto p = std::dynamic_pointer_cast<const ScrollViewProps>(self.props);
203
- if (p && !p->scrollEnabled) {
204
- [self.nextResponder scrollWheel:event];
205
- return;
206
- }
207
- [_scrollView scrollWheel:event];
208
- }
209
-
210
205
  - (void)boundsDidChange:(NSNotification *)note {
211
206
  NSPoint origin = _scrollView.contentView.bounds.origin;
212
207
  if (gOffsetChanged) {
@@ -220,13 +215,45 @@ std::function<void(NSInteger, CGPoint)> gOffsetChanged;
220
215
  }
221
216
  auto emitter = std::dynamic_pointer_cast<const ScrollViewEventEmitter>(self.eventEmitter);
222
217
  if (emitter) {
223
- ScrollEvent event;
224
- event.contentOffset = {static_cast<Float>(origin.x), static_cast<Float>(origin.y)};
225
- event.contentSize = {static_cast<Float>(_document.frame.size.width), static_cast<Float>(_document.frame.size.height)};
226
- event.containerSize = {static_cast<Float>(self.bounds.size.width), static_cast<Float>(self.bounds.size.height)};
227
- event.zoomScale = 1;
228
- event.timestamp = NSProcessInfo.processInfo.systemUptime;
229
- emitter->onScroll(event);
218
+ emitter->onScroll([self scrollEvent]);
219
+ }
220
+ }
221
+
222
+ - (ScrollEvent)scrollEvent {
223
+ NSPoint origin = _scrollView.contentView.bounds.origin;
224
+ ScrollEvent event;
225
+ event.contentOffset = {static_cast<Float>(origin.x), static_cast<Float>(origin.y)};
226
+ event.contentSize = {static_cast<Float>(_document.frame.size.width), static_cast<Float>(_document.frame.size.height)};
227
+ event.containerSize = {static_cast<Float>(self.bounds.size.width), static_cast<Float>(self.bounds.size.height)};
228
+ event.zoomScale = 1;
229
+ event.timestamp = NSProcessInfo.processInfo.systemUptime;
230
+ return event;
231
+ }
232
+
233
+ // A trackpad gesture's drag and momentum, as onScrollBeginDrag, onScrollEndDrag,
234
+ // onMomentumScrollBegin and onMomentumScrollEnd report a touch's on iOS.
235
+ - (void)report:(NGNScrollPhase)phase velocity:(NSPoint)velocity {
236
+ auto emitter = std::dynamic_pointer_cast<const ScrollViewEventEmitter>(self.eventEmitter);
237
+ if (!emitter) {
238
+ return;
239
+ }
240
+ switch (phase) {
241
+ case NGNScrollPhaseDragBegan:
242
+ emitter->onScrollBeginDrag([self scrollEvent]);
243
+ break;
244
+ case NGNScrollPhaseDragEnded: {
245
+ ScrollEndDragEvent event([self scrollEvent]);
246
+ event.velocity = {static_cast<Float>(velocity.x), static_cast<Float>(velocity.y)};
247
+ event.targetContentOffset = event.contentOffset;
248
+ emitter->onScrollEndDrag(event);
249
+ break;
250
+ }
251
+ case NGNScrollPhaseMomentumBegan:
252
+ emitter->onMomentumScrollBegin([self scrollEvent]);
253
+ break;
254
+ case NGNScrollPhaseMomentumEnded:
255
+ emitter->onMomentumScrollEnd([self scrollEvent]);
256
+ break;
230
257
  }
231
258
  }
232
259
 
@@ -244,6 +271,20 @@ std::function<void(NSInteger, CGPoint)> gOffsetChanged;
244
271
  } else {
245
272
  return;
246
273
  }
274
+ // scrollTo's third argument, and scrollToEnd's only one, is whether it animates.
275
+ const auto &animated = name == "scrollTo" ? (args.size() >= 3 ? args[2] : folly::dynamic()) : (args.isArray() && !args.empty() ? args[0] : folly::dynamic());
276
+ if (animated.isBool() && animated.asBool()) {
277
+ NGNWheelScrollView *scroll = _scrollView;
278
+ [NSAnimationContext
279
+ runAnimationGroup:^(NSAnimationContext *context) {
280
+ context.duration = 0.3;
281
+ clip.animator.boundsOrigin = target;
282
+ }
283
+ completionHandler:^{
284
+ [scroll reflectScrolledClipView:clip];
285
+ }];
286
+ return;
287
+ }
247
288
  [clip scrollToPoint:target];
248
289
  [_scrollView reflectScrolledClipView:clip];
249
290
  }
@@ -18,8 +18,28 @@ float NGNLegacyScrollerWidth(BOOL thin);
18
18
  - (instancetype)initWithChanged:(void (^)(void))changed;
19
19
  @end
20
20
 
21
- // A scroll view's NSScrollView, which takes a wheel turn before the view holding it: with
22
- // `scrollingDisabled` (`scrollEnabled={false}`) the turn goes past it, up the responder chain.
21
+ // Where a trackpad scroll is, as a scroll view's drag and momentum events report it.
22
+ typedef NS_ENUM(NSInteger, NGNScrollPhase) {
23
+ NGNScrollPhaseDragBegan,
24
+ NGNScrollPhaseDragEnded,
25
+ NGNScrollPhaseMomentumBegan,
26
+ NGNScrollPhaseMomentumEnded,
27
+ };
28
+
29
+ // A scroll view's NSScrollView, which takes a wheel turn before the view holding it. A turn goes
30
+ // past it, up the responder chain, with `scrollingDisabled` (`scrollEnabled={false}`), or when the
31
+ // content cannot scroll along the turn's axis: a vertical turn over a horizontal scroll view
32
+ // scrolls the one around it. A trackpad gesture is routed once, as it begins, and its momentum
33
+ // follows it. A view drawn mirrored (an inverted list's scale of -1) scrolls by mirrored deltas, so
34
+ // the content follows the fingers, and the user's scroll direction, as drawn.
23
35
  @interface NGNWheelScrollView : NSScrollView
24
36
  @property (nonatomic) BOOL scrollingDisabled;
37
+ // `pagingEnabled`: a gesture's end settles on a page, the view's size along the axis.
38
+ @property (nonatomic) BOOL pagingEnabled;
39
+ // Called as a trackpad gesture this view scrolls with begins and ends, and as its momentum, or a
40
+ // page settling, begins and ends. `velocity` is the drag's at its end, in points a millisecond.
41
+ @property (nonatomic, copy) void (^phaseChanged)(NGNScrollPhase phase, NSPoint velocity);
25
42
  @end
43
+
44
+ // `event` with its deltas along the axes given turned round.
45
+ NSEvent *NGNMirroredScroll(NSEvent *event, BOOL x, BOOL y);