@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
@@ -1,5 +1,10 @@
1
1
  #import "scrollers.h"
2
2
 
3
+ #import <QuartzCore/QuartzCore.h>
4
+
5
+ #include <algorithm>
6
+ #include <cmath>
7
+
3
8
  ngn::ScrollerStyle NGNPreferredScrollerStyle() {
4
9
  return NSScroller.preferredScrollerStyle == NSScrollerStyleLegacy ? ngn::ScrollerStyle::Legacy
5
10
  : ngn::ScrollerStyle::Overlay;
@@ -32,12 +37,226 @@ float NGNLegacyScrollerWidth(BOOL thin) {
32
37
 
33
38
  @end
34
39
 
35
- @implementation NGNWheelScrollView
40
+ NSEvent *NGNMirroredScroll(NSEvent *event, BOOL x, BOOL y) {
41
+ if ((!x && !y) || event.CGEvent == nullptr) {
42
+ return event;
43
+ }
44
+ CGEventRef cg = CGEventCreateCopy(event.CGEvent);
45
+ // Setting the lines recomputes the other two from them, so all three are read first and set in
46
+ // this order. The points are read as a double, which keeps a trackpad's fractions.
47
+ auto negate = [cg](CGEventField lines, CGEventField fixed, CGEventField points) {
48
+ int64_t l = CGEventGetIntegerValueField(cg, lines);
49
+ double f = CGEventGetDoubleValueField(cg, fixed), p = CGEventGetDoubleValueField(cg, points);
50
+ CGEventSetIntegerValueField(cg, lines, -l);
51
+ CGEventSetDoubleValueField(cg, fixed, -f);
52
+ CGEventSetDoubleValueField(cg, points, -p);
53
+ };
54
+ if (y) {
55
+ negate(kCGScrollWheelEventDeltaAxis1, kCGScrollWheelEventFixedPtDeltaAxis1, kCGScrollWheelEventPointDeltaAxis1);
56
+ }
57
+ if (x) {
58
+ negate(kCGScrollWheelEventDeltaAxis2, kCGScrollWheelEventFixedPtDeltaAxis2, kCGScrollWheelEventPointDeltaAxis2);
59
+ }
60
+ NSEvent *mirrored = [NSEvent eventWithCGEvent:cg];
61
+ CFRelease(cg);
62
+ return mirrored != nil ? mirrored : event;
63
+ }
64
+
65
+ @implementation NGNWheelScrollView {
66
+ // The gesture under way goes up the responder chain: it began along an axis this view cannot
67
+ // scroll along.
68
+ BOOL _passing;
69
+ // A gesture's start with no deltas yet, held until an event says which axis it goes along.
70
+ NSEvent *_heldStart;
71
+ // The gesture under way ended in a page settling, so its momentum moves nothing.
72
+ BOOL _settling;
73
+ // A drag has begun and not yet ended: an end with no beginning (a touch that never moved) reports
74
+ // nothing and settles nothing.
75
+ BOOL _dragging;
76
+ NSPoint _velocity;
77
+ NSTimeInterval _lastWheel;
78
+ // When the deltas last moved: a drag held still before it ends was not flicked.
79
+ NSTimeInterval _lastMove;
80
+ // Moved on by each page settling, so only the latest one's end is reported.
81
+ uint64_t _settle;
82
+ BOOL _settleUnderway;
83
+ // A mouse's wheel queued a settling that has not run.
84
+ BOOL _settleQueued;
85
+ }
86
+
87
+ - (void)setPagingEnabled:(BOOL)pagingEnabled {
88
+ _pagingEnabled = pagingEnabled;
89
+ if (!pagingEnabled) {
90
+ [self dropSettle];
91
+ }
92
+ }
93
+
94
+ // Whether the content overflows the view along the axis the deltas mostly go along.
95
+ - (BOOL)scrollsAlong:(NSEvent *)event {
96
+ NSSize content = self.documentView.frame.size, view = self.contentView.bounds.size;
97
+ if (std::fabs(event.scrollingDeltaX) > std::fabs(event.scrollingDeltaY)) {
98
+ return content.width > view.width + 0.5;
99
+ }
100
+ return content.height > view.height + 0.5;
101
+ }
102
+
103
+ // Whether the view is drawn mirrored along each axis: an odd number of the layers above it scaled
104
+ // by a negative amount along it.
105
+ - (void)mirroredX:(BOOL *)x y:(BOOL *)y {
106
+ *x = NO;
107
+ *y = NO;
108
+ for (NSView *view = self; view != nil; view = view.superview) {
109
+ CATransform3D t = view.layer.transform;
110
+ *x ^= t.m11 < 0;
111
+ *y ^= t.m22 < 0;
112
+ }
113
+ }
114
+
36
115
  - (void)scrollWheel:(NSEvent *)event {
37
116
  if (self.scrollingDisabled) {
38
117
  [self.nextResponder scrollWheel:event];
39
118
  return;
40
119
  }
41
- [super scrollWheel:event];
120
+ NSEventPhase phase = event.phase, momentum = event.momentumPhase;
121
+ BOOL wheel = phase == NSEventPhaseNone && momentum == NSEventPhaseNone;
122
+ BOOL still = event.scrollingDeltaX == 0 && event.scrollingDeltaY == 0;
123
+ if (phase == NSEventPhaseMayBegin) {
124
+ _passing = NO;
125
+ _heldStart = nil;
126
+ } else if (phase == NSEventPhaseBegan && still) {
127
+ _heldStart = event;
128
+ _passing = NO;
129
+ // A settling the wheel queued waits until the gesture's direction says whose it is.
130
+ [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(settleOnPage) object:nil];
131
+ return;
132
+ } else if (phase == NSEventPhaseBegan || wheel || (_heldStart != nil && !still)) {
133
+ _passing = ![self scrollsAlong:event];
134
+ _settling = NO;
135
+ if (!wheel && _settleQueued) {
136
+ // A gesture this view takes settles itself; one that goes past leaves the wheel's to run.
137
+ if (_passing) {
138
+ [self queueSettle];
139
+ } else {
140
+ [self dropSettle];
141
+ }
142
+ }
143
+ }
144
+ if (_heldStart != nil && (!still || (phase != NSEventPhaseChanged && phase != NSEventPhaseStationary))) {
145
+ NSEvent *start = _heldStart;
146
+ _heldStart = nil;
147
+ [self scrollDecided:start];
148
+ }
149
+ [self scrollDecided:event];
42
150
  }
151
+
152
+ // An event of a gesture whose route is decided.
153
+ - (void)scrollDecided:(NSEvent *)event {
154
+ NSEventPhase phase = event.phase, momentum = event.momentumPhase;
155
+ BOOL wheel = phase == NSEventPhaseNone && momentum == NSEventPhaseNone;
156
+ if (_passing) {
157
+ [self.nextResponder scrollWheel:event];
158
+ return;
159
+ }
160
+ BOOL x, y;
161
+ [self mirroredX:&x y:&y];
162
+ NSEvent *scroll = NGNMirroredScroll(event, x, y);
163
+ if (momentum != NSEventPhaseNone && _settling) {
164
+ if (momentum == NSEventPhaseEnded || momentum == NSEventPhaseCancelled) {
165
+ _settling = NO;
166
+ }
167
+ return;
168
+ }
169
+ if (phase == NSEventPhaseBegan || phase == NSEventPhaseChanged || wheel) {
170
+ NSTimeInterval elapsed = std::max(scroll.timestamp - _lastWheel, 0.001);
171
+ _lastWheel = scroll.timestamp;
172
+ if (scroll.scrollingDeltaX != 0 || scroll.scrollingDeltaY != 0) {
173
+ // AppKit's deltas move the content; the offset goes the other way. A mouse's are in lines.
174
+ CGFloat across = scroll.hasPreciseScrollingDeltas ? 1 : self.horizontalLineScroll;
175
+ CGFloat down = scroll.hasPreciseScrollingDeltas ? 1 : self.verticalLineScroll;
176
+ _velocity = NSMakePoint(-scroll.scrollingDeltaX * across / (elapsed * 1000),
177
+ -scroll.scrollingDeltaY * down / (elapsed * 1000));
178
+ _lastMove = scroll.timestamp;
179
+ }
180
+ }
181
+ [super scrollWheel:scroll];
182
+ if (phase == NSEventPhaseBegan) {
183
+ _velocity = NSZeroPoint;
184
+ _dragging = YES;
185
+ [self report:NGNScrollPhaseDragBegan];
186
+ } else if ((phase == NSEventPhaseEnded || phase == NSEventPhaseCancelled) && _dragging) {
187
+ _dragging = NO;
188
+ // ponytail: a tenth of a second held still is no flick; tune here if it feels off.
189
+ if (scroll.timestamp - _lastMove > 0.1) {
190
+ _velocity = NSZeroPoint;
191
+ }
192
+ [self report:NGNScrollPhaseDragEnded];
193
+ if (self.pagingEnabled) {
194
+ _settling = YES;
195
+ [self settleOnPage];
196
+ }
197
+ } else if (momentum == NSEventPhaseBegan) {
198
+ [self report:NGNScrollPhaseMomentumBegan];
199
+ } else if (momentum == NSEventPhaseEnded || momentum == NSEventPhaseCancelled) {
200
+ [self report:NGNScrollPhaseMomentumEnded];
201
+ } else if (wheel && self.pagingEnabled) {
202
+ // A mouse's wheel has no end: the page settles once it stops turning.
203
+ [self queueSettle];
204
+ }
205
+ }
206
+
207
+ - (void)queueSettle {
208
+ [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(settleOnPage) object:nil];
209
+ [self performSelector:@selector(settleOnPage) withObject:nil afterDelay:0.2];
210
+ _settleQueued = YES;
211
+ }
212
+
213
+ - (void)dropSettle {
214
+ [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(settleOnPage) object:nil];
215
+ _settleQueued = NO;
216
+ }
217
+
218
+ - (void)report:(NGNScrollPhase)phase {
219
+ if (self.phaseChanged) {
220
+ self.phaseChanged(phase, _velocity);
221
+ }
222
+ }
223
+
224
+ // Animates to the page the drag points at (ngn::pagedOffset), reporting it as momentum.
225
+ - (void)settleOnPage {
226
+ _settleQueued = NO;
227
+ if (!self.pagingEnabled) {
228
+ return;
229
+ }
230
+ NSClipView *clip = self.contentView;
231
+ NSSize content = self.documentView.frame.size, view = clip.bounds.size;
232
+ NSPoint from = clip.bounds.origin;
233
+ NSPoint to = NSMakePoint(
234
+ ngn::pagedOffset(from.x, _velocity.x, view.width, content.width - view.width),
235
+ ngn::pagedOffset(from.y, _velocity.y, view.height, content.height - view.height));
236
+ _velocity = NSZeroPoint;
237
+ // One settling in front of another ends the first before the second begins.
238
+ if (_settleUnderway) {
239
+ [self report:NGNScrollPhaseMomentumEnded];
240
+ }
241
+ uint64_t settle = ++_settle;
242
+ _settleUnderway = YES;
243
+ [self report:NGNScrollPhaseMomentumBegan];
244
+ __weak NGNWheelScrollView *weakSelf = self;
245
+ [NSAnimationContext
246
+ runAnimationGroup:^(NSAnimationContext *context) {
247
+ context.duration = 0.25;
248
+ context.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
249
+ clip.animator.boundsOrigin = to;
250
+ }
251
+ completionHandler:^{
252
+ NGNWheelScrollView *strong = weakSelf;
253
+ if (strong == nil || strong->_settle != settle) {
254
+ return;
255
+ }
256
+ strong->_settleUnderway = NO;
257
+ [strong reflectScrolledClipView:clip];
258
+ [strong report:NGNScrollPhaseMomentumEnded];
259
+ }];
260
+ }
261
+
43
262
  @end
@@ -38,6 +38,10 @@
38
38
  - (void)placeAsWindows:(BOOL)window;
39
39
  // The split view is leaving: the window gets its surface back.
40
40
  - (void)uninstall;
41
+ // A screen above the one holding this split view covers it (`covered`), or it shows again: a
42
+ // window's split view gives the window its surface back while covered, so the screen in front
43
+ // shows, and takes the window again as its screen shows.
44
+ - (void)setScreenCovered:(BOOL)covered;
41
45
  @end
42
46
 
43
47
  // NgNativeSidebar and NgNativeSplitContent: filled by their container, not placed by Yoga.
@@ -1,4 +1,6 @@
1
1
  #import "split-view.h"
2
+
3
+ #include <optional>
2
4
  #import "test-launch.h"
3
5
 
4
6
  #import "surface-view.h"
@@ -169,6 +171,10 @@ NSWindow *NGNWindowOfSplit(NSView *surfaceView) {
169
171
  CGFloat _plainWidth;
170
172
  ngn::SidebarCollapse _collapse;
171
173
  std::string _autosave;
174
+ // The sidebar's width as a screen in front covered it, for when it takes the window again.
175
+ CGFloat _widthBeforeCover;
176
+ // What the app's `collapsed` asked for while no controller was there to take it.
177
+ std::optional<bool> _collapseWhileCovered;
172
178
  }
173
179
 
174
180
  + (ngn::InputSink *)input {
@@ -294,7 +300,7 @@ NSWindow *NGNWindowOfSplit(NSView *surfaceView) {
294
300
 
295
301
  - (void)viewDidMoveToWindow {
296
302
  [super viewDidMoveToWindow];
297
- if (_decided && _windows && _controller == nil && self.window != nil) {
303
+ if (_decided && _windows && _controller == nil && self.window != nil && ![self underCoveredScreen]) {
298
304
  [self build];
299
305
  }
300
306
  }
@@ -304,7 +310,9 @@ NSWindow *NGNWindowOfSplit(NSView *surfaceView) {
304
310
  return;
305
311
  }
306
312
  if (_windows) {
307
- [self installInWindow];
313
+ if (![self underCoveredScreen]) {
314
+ [self installInWindow];
315
+ }
308
316
  } else {
309
317
  [self buildPlain];
310
318
  }
@@ -369,13 +377,29 @@ NSWindow *NGNWindowOfSplit(NSView *surfaceView) {
369
377
  _controller.splitView.autosaveName = autosaveKey(_autosave);
370
378
  }
371
379
  BOOL restoring = [self hasAutosavedFrames];
380
+ // Installed again after a screen covered it: the sidebar stays collapsed if the app or the user
381
+ // had it so, unless the autosave restores the user's. After the divider is placed, which opens it.
382
+ std::optional<bool> collapse;
383
+ if (!restoring && p != nullptr && p->collapsed) {
384
+ collapse = true;
385
+ }
386
+ // The app asked while covered: that wins, autosave or not.
387
+ if (_collapseWhileCovered) {
388
+ collapse = *_collapseWhileCovered;
389
+ }
390
+ _collapseWhileCovered.reset();
372
391
  [_controller addSplitViewItem:_sidebarItem];
373
392
  [_controller addSplitViewItem:contentItem];
374
393
  window.contentViewController = _controller;
375
394
  [window setFrame:frame display:YES];
376
395
  if (!restoring) {
377
- [_controller.splitView setPosition:[self startingWidth] ofDividerAtIndex:0];
396
+ CGFloat width = _widthBeforeCover > 0 ? _widthBeforeCover : [self startingWidth];
397
+ [_controller.splitView setPosition:width ofDividerAtIndex:0];
398
+ }
399
+ if (collapse && *collapse != _sidebarItem.collapsed) {
400
+ _sidebarItem.collapsed = *collapse;
378
401
  }
402
+ _widthBeforeCover = 0;
379
403
  [_sidebarItem addObserver:self forKeyPath:@"collapsed" options:NSKeyValueObservingOptionNew context:nil];
380
404
  // A collapsed state the autosave restored is the user's: the app hears it.
381
405
  if (_sidebarItem.collapsed) {
@@ -429,6 +453,33 @@ NSWindow *NGNWindowOfSplit(NSView *surfaceView) {
429
453
  _ownWindow = nil;
430
454
  }
431
455
 
456
+ // Whether a screen above the one holding this split view covers it, however long ago.
457
+ - (BOOL)underCoveredScreen {
458
+ for (NSView *view = self.superview; view != nil; view = view.superview) {
459
+ if ([view isKindOfClass:NGNView.class] && ((NGNView *)view).covered) {
460
+ return YES;
461
+ }
462
+ }
463
+ return NO;
464
+ }
465
+
466
+ - (void)setScreenCovered:(BOOL)covered {
467
+ // Only a window's split view holds the window; a plain one stays in its screen.
468
+ if (!_windows) {
469
+ return;
470
+ }
471
+ if (covered) {
472
+ if (_controller != nil) {
473
+ // The divider's position: the sidebar pane's edge, whatever inset its view has in it.
474
+ NSArray<NSView *> *panes = _controller.splitView.arrangedSubviews;
475
+ _widthBeforeCover = _sidebarItem.collapsed || panes.count == 0 ? 0 : NSMaxX(panes[0].frame);
476
+ [self uninstall];
477
+ }
478
+ } else if (_decided && _controller == nil) {
479
+ [self build];
480
+ }
481
+ }
482
+
432
483
  - (void)dealloc {
433
484
  [self uninstall];
434
485
  }
@@ -452,6 +503,9 @@ NSWindow *NGNWindowOfSplit(NSView *surfaceView) {
452
503
  view.autosaveName = autosaveKey(_autosave);
453
504
  }
454
505
  if (auto collapsed = _collapse.propChanged(p->collapsed)) {
506
+ if (_windows && _controller == nil) {
507
+ _collapseWhileCovered = *collapsed;
508
+ }
455
509
  [self setToolkitCollapsed:*collapsed];
456
510
  }
457
511
  }
@@ -232,7 +232,11 @@ static std::optional<ngn::GesturePhase> phaseOf(NSEventPhase phase) {
232
232
  double leading = 0;
233
233
  if (NSButton *zoom = [self.window standardWindowButton:NSWindowZoomButton]; zoom != nil && safe.top > 0) {
234
234
  NSRect controls = [self convertRect:zoom.bounds fromView:zoom];
235
- leading = std::max<double>(0, NSMaxX(controls));
235
+ leading = ngn::titlebarLeading(
236
+ NSMinX(controls),
237
+ NSMaxX(controls),
238
+ NSWidth(self.bounds),
239
+ self.userInterfaceLayoutDirection == NSUserInterfaceLayoutDirectionRightToLeft);
236
240
  }
237
241
  ngn::ContainerInsets insets{
238
242
  .size = {static_cast<facebook::react::Float>(self.bounds.size.width),
@@ -1,6 +1,7 @@
1
1
  // react-native-svg's views on macOS. The RNSVGSvgView draws its document (ngn/svg.h), which the
2
2
  // core builds from the elements under it, with Core Graphics; the elements' own views are
3
- // hidden, as they draw nothing of their own.
3
+ // hidden, as they draw nothing of their own. The views in a foreignObject stay hidden too: the svg
4
+ // draws them from their layers, so they take no clicks.
4
5
  #pragma once
5
6
 
6
7
  #import "host-view.h"
@@ -6,10 +6,16 @@
6
6
 
7
7
  @implementation NGNSvgView {
8
8
  std::shared_ptr<const ngn::SvgDocument> _document;
9
+ // The documents set so far, and whether the next draw is the one after a draw of a
10
+ // foreignObject's views with no document set since: see drawRect.
11
+ uint64_t _documents;
12
+ BOOL _followUp;
9
13
  }
10
14
 
11
15
  - (void)setDocument:(std::shared_ptr<const ngn::SvgDocument>)document {
12
16
  _document = std::move(document);
17
+ _documents++;
18
+ _followUp = NO;
13
19
  [self setNeedsDisplay:YES];
14
20
  }
15
21
 
@@ -23,6 +29,23 @@
23
29
  [self setNeedsDisplay:YES];
24
30
  }
25
31
 
32
+ // A foreignObject's view: an element's, under the svg's own and its groups'.
33
+ - (NSView *)descendantTagged:(ngn::Tag)tag {
34
+ NSMutableArray<NSView *> *pending = [self.subviews mutableCopy];
35
+ while (pending.count > 0) {
36
+ NSView *view = pending.lastObject;
37
+ [pending removeLastObject];
38
+ if (![view isKindOfClass:NGNSvgElementView.class]) {
39
+ continue;
40
+ }
41
+ if (((NGNView *)view).reactTag == tag) {
42
+ return view;
43
+ }
44
+ [pending addObjectsFromArray:view.subviews];
45
+ }
46
+ return nil;
47
+ }
48
+
26
49
  - (void)drawRect:(NSRect)dirtyRect {
27
50
  if (!_document) {
28
51
  return;
@@ -31,12 +54,35 @@
31
54
  NSSize size = self.bounds.size;
32
55
  CGFloat scale = self.window ? self.window.backingScaleFactor : NSScreen.mainScreen.backingScaleFactor;
33
56
  __weak NGNSvgView *weakSelf = self;
34
- auto canvas = NGNMakeCGCanvas(context, size, scale, ^{
35
- [weakSelf setNeedsDisplay:YES];
36
- });
57
+ __block BOOL drewViews = NO;
58
+ auto canvas = NGNMakeCGCanvas(
59
+ context,
60
+ size,
61
+ scale,
62
+ ^{
63
+ [weakSelf setNeedsDisplay:YES];
64
+ },
65
+ ^NSView *(ngn::Tag tag) {
66
+ drewViews = YES;
67
+ return [weakSelf descendantTagged:tag];
68
+ });
37
69
  CGContextSaveGState(context);
38
70
  ngn::drawSvg(*_document, size.width, size.height, *canvas);
39
71
  CGContextRestoreGState(context);
72
+ // The views in a foreignObject draw after the svg in the same pass, so a change to one shows in
73
+ // the svg's next draw: that comes on the next turn of the run loop, as react-native-svg's
74
+ // ForeignObject asks for one on iOS, and asks for no other unless something changed first.
75
+ if (drewViews && !_followUp) {
76
+ uint64_t documents = _documents;
77
+ dispatch_async(dispatch_get_main_queue(), ^{
78
+ [weakSelf drawFollowUpOf:documents];
79
+ });
80
+ }
81
+ }
82
+
83
+ - (void)drawFollowUpOf:(uint64_t)documents {
84
+ _followUp = documents == _documents;
85
+ [self setNeedsDisplay:YES];
40
86
  }
41
87
 
42
88
  @end
@@ -0,0 +1,31 @@
1
+ // Tab and Shift-Tab in a window: the order is the core's (focus-order.h), walked over the
2
+ // container the focus is in, in place of AppKit's key-view loop.
3
+ #pragma once
4
+
5
+ #import <AppKit/AppKit.h>
6
+
7
+ #include "ngn/focus-order.h"
8
+
9
+ @class NGNSurfaceView;
10
+ @class NGNView;
11
+
12
+ namespace ngn::appkit {
13
+
14
+ class AppKitBackend;
15
+
16
+ // The container the focus is in, and the view holding it, in `window`. An in-window cover (a modal)
17
+ // is the container while it is up, wherever the first responder is: Tab stays inside it.
18
+ struct FocusContext {
19
+ NGNSurfaceView *container = nil;
20
+ NGNView *focused = nil;
21
+ };
22
+
23
+ FocusContext focusContext(NSWindow *window);
24
+
25
+ // Moves the focus in `window` to the next view in Tab order (the previous with `backwards`), over
26
+ // the views `backend` mounted for `tree`. A view that will not take the focus (a disabled control,
27
+ // or a field AppKit refuses it to) is passed over. True when the key is used: the focus moved,
28
+ // or it is inside a sheet, cover or popover, which Tab never leaves.
29
+ bool moveFocus(NSWindow *window, const ViewTree &tree, const AppKitBackend &backend, bool backwards, TabReach reach);
30
+
31
+ } // namespace ngn::appkit
@@ -0,0 +1,64 @@
1
+ #import "tab-walk.h"
2
+
3
+ #import "appkit-backend.h"
4
+ #import "host-view.h"
5
+ #import "modal-host-view.h"
6
+ #import "surface-view.h"
7
+
8
+ namespace ngn::appkit {
9
+
10
+ FocusContext focusContext(NSWindow *window) {
11
+ FocusContext context;
12
+ NSResponder *first = window.firstResponder;
13
+ // A field editor stands in for its text field, inside the field's view.
14
+ for (NSView *v = [first isKindOfClass:NSView.class] ? (NSView *)first : nil; v != nil; v = v.superview) {
15
+ if (context.focused == nil && [v isKindOfClass:NGNView.class]) {
16
+ context.focused = (NGNView *)v;
17
+ }
18
+ if ([v isKindOfClass:NGNSurfaceView.class]) {
19
+ context.container = (NGNSurfaceView *)v;
20
+ break;
21
+ }
22
+ }
23
+ if (context.container == nil) {
24
+ context.container =
25
+ [window.contentView isKindOfClass:NGNSurfaceView.class] ? (NGNSurfaceView *)window.contentView : nil;
26
+ }
27
+ NSView *cover = [NGNModalHostView frontCoverIn:window];
28
+ if (cover != nil && ![context.container isDescendantOf:cover]) {
29
+ context.container = (NGNSurfaceView *)cover;
30
+ context.focused = nil;
31
+ }
32
+ return context;
33
+ }
34
+
35
+ bool moveFocus(NSWindow *window, const ViewTree &tree, const AppKitBackend &backend, bool backwards, TabReach reach) {
36
+ auto [container, focused] = focusContext(window);
37
+ if (container == nil) {
38
+ return false;
39
+ }
40
+ bool contained = [NGNModalHostView isPresentationContainer:container];
41
+ std::optional<Tag> from;
42
+ if (focused != nil) {
43
+ from = static_cast<Tag>(focused.reactTag);
44
+ }
45
+ auto count = tabOrder(tree, container.rootTag, reach, from).size();
46
+ // A view that will not take the focus passes Tab on: a disabled control, which
47
+ // makeFirstResponder: would focus all the same, or a field or button AppKit refuses.
48
+ for (size_t tries = 0; tries < count; tries++) {
49
+ auto next = nextInTabOrder(tree, container.rootTag, from, backwards, reach);
50
+ if (!next) {
51
+ return contained;
52
+ }
53
+ NGNView *view = backend.view(*next);
54
+ NSResponder *responder = view.keyboardResponder;
55
+ bool disabled = [responder isKindOfClass:NSControl.class] && !((NSControl *)responder).enabled;
56
+ if (view != nil && !disabled && [window makeFirstResponder:responder]) {
57
+ return true;
58
+ }
59
+ from = next;
60
+ }
61
+ return contained;
62
+ }
63
+
64
+ } // namespace ngn::appkit
@@ -62,6 +62,8 @@ BOOL NGNControlShouldChangeText(
62
62
  - (const ngn::TextInputOptions &)options;
63
63
  // Drawn in the field's font; nil colour is the system's placeholder colour.
64
64
  - (void)setPlaceholder:(NSString *)placeholder color:(NSColor *)color;
65
+ // The alignment and base writing direction of the text, while editing too, and of the placeholder.
66
+ - (void)setTextAlignment:(NSTextAlignment)alignment writingDirection:(NSWritingDirection)direction;
65
67
  // Selects now while editing, or as editing starts.
66
68
  - (void)selectRange:(NSRange)range;
67
69
  @end
@@ -72,5 +74,8 @@ BOOL NGNControlShouldChangeText(
72
74
  @property (nonatomic, copy, readonly) NSAttributedString *placeholder;
73
75
  - (void)applyOptions:(const ngn::TextInputOptions &)options;
74
76
  - (void)setPlaceholder:(NSString *)placeholder color:(NSColor *)color;
77
+ // The alignment and base writing direction of the text, typed or set later too, and of the
78
+ // placeholder.
79
+ - (void)setTextAlignment:(NSTextAlignment)alignment writingDirection:(NSWritingDirection)direction;
75
80
  - (void)selectRange:(NSRange)range;
76
81
  @end