@ng-native/desktop 0.24.0 → 0.26.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 (85) hide show
  1. package/bin/package.mjs +65 -9
  2. package/bin/prebuilt.mjs +25 -4
  3. package/native/CMakeLists.txt +51 -4
  4. package/native/appkit/CMakeLists.txt +16 -2
  5. package/native/appkit/appkit-backend.mm +28 -3
  6. package/native/appkit/cg-canvas.h +5 -0
  7. package/native/appkit/cg-canvas.mm +21 -6
  8. package/native/appkit/desktop-expo.h +6 -0
  9. package/native/appkit/desktop-expo.mm +5 -0
  10. package/native/appkit/host-view.mm +32 -0
  11. package/native/appkit/image-request.h +2 -10
  12. package/native/appkit/image-request.mm +0 -22
  13. package/native/appkit/image-view.mm +121 -4
  14. package/native/appkit/main.mm +130 -185
  15. package/native/appkit/scroll-view.mm +57 -16
  16. package/native/appkit/scrollers.h +22 -2
  17. package/native/appkit/scrollers.mm +221 -2
  18. package/native/appkit/split-view.h +4 -0
  19. package/native/appkit/split-view.mm +60 -12
  20. package/native/appkit/system-access.mm +3 -0
  21. package/native/appkit/system.mm +33 -48
  22. package/native/appkit/text-layout.h +6 -0
  23. package/native/appkit/text-layout.mm +147 -24
  24. package/native/appkit/view-effects.h +27 -0
  25. package/native/appkit/view-effects.mm +90 -0
  26. package/native/cmake/react-native.cmake +9 -0
  27. package/native/cmake/runtime-dependencies.cmake +32 -1
  28. package/native/core/include/ngn/app-info.h +3 -2
  29. package/native/core/include/ngn/app-menus.h +5 -11
  30. package/native/core/include/ngn/default-menu.h +16 -6
  31. package/native/core/include/ngn/desktop-expo.h +0 -5
  32. package/native/core/include/ngn/desktop-modules.h +144 -0
  33. package/native/core/include/ngn/dev-server.h +12 -3
  34. package/native/core/include/ngn/host-os.h +45 -0
  35. package/native/core/include/ngn/host-views.h +25 -0
  36. package/native/core/include/ngn/image-source.h +18 -0
  37. package/native/core/include/ngn/launch-settings.h +9 -7
  38. package/native/core/include/ngn/module-registry.h +2 -0
  39. package/native/core/include/ngn/notifications.h +6 -0
  40. package/native/core/include/ngn/runtime.h +4 -1
  41. package/native/core/include/ngn/scrollers.h +5 -0
  42. package/native/core/include/ngn/spawn-rules.h +3 -2
  43. package/native/core/include/ngn/split-view.h +12 -0
  44. package/native/core/include/ngn/system.h +47 -0
  45. package/native/core/include/ngn/text-measurer.h +11 -0
  46. package/native/core/include/ngn/undo.h +27 -0
  47. package/native/core/include/ngn/updates.h +3 -2
  48. package/native/core/include/ngn/view-filter.h +33 -0
  49. package/native/core/src/app-menus.cpp +9 -11
  50. package/native/core/src/default-menu.cpp +6 -2
  51. package/native/core/src/dev-server-helper.cpp +3 -2
  52. package/native/core/src/dev-server.cpp +22 -5
  53. package/native/core/src/host-os.cpp +20 -0
  54. package/native/core/src/host-views.cpp +25 -0
  55. package/native/core/src/image-source.cpp +30 -0
  56. package/native/core/src/launch-settings.cpp +10 -6
  57. package/native/core/src/module-registry.cpp +9 -0
  58. package/native/core/src/modules/app-info.cpp +4 -4
  59. package/native/core/src/modules/app-modules.cpp +4 -2
  60. package/native/core/src/modules/desktop-expo.cpp +0 -5
  61. package/native/core/src/modules/desktop-modules.cpp +132 -0
  62. package/native/core/src/modules/notifications.cpp +4 -4
  63. package/native/core/src/runtime.cpp +2 -0
  64. package/native/core/src/scrollers.cpp +11 -0
  65. package/native/core/src/spawn-rules.cpp +1 -4
  66. package/native/core/src/split-view.cpp +10 -0
  67. package/native/core/src/system.cpp +52 -0
  68. package/native/core/src/text-layout-manager.cpp +13 -0
  69. package/native/core/src/undo-stacks.cpp +71 -0
  70. package/native/core/src/view-filter.cpp +114 -0
  71. package/native/scripts/build-hermes.sh +19 -7
  72. package/native/scripts/build-icu.sh +36 -0
  73. package/native/scripts/build-lock.sh +21 -0
  74. package/native/scripts/build.sh +46 -7
  75. package/native/scripts/fetch-hermes.sh +70 -0
  76. package/native/scripts/fetch.sh +24 -2
  77. package/native/scripts/hermes-built.sh +5 -4
  78. package/native/scripts/hermes-target.sh +18 -0
  79. package/native/scripts/icu-filter.json +22 -0
  80. package/native/scripts/make-prebuilt.mjs +13 -6
  81. package/native/scripts/prebuilt-host-cache.mjs +1 -1
  82. package/native/shim/openssl/sha.h +108 -3
  83. package/native/shim/text-platform/react/renderer/textlayoutmanager/TextLayoutManager.h +52 -0
  84. package/package.json +2 -2
  85. package/prebuilt.json +6 -6
@@ -1,31 +1,9 @@
1
1
  #import "image-request.h"
2
2
 
3
- #include <cmath>
4
-
5
3
  using facebook::react::ImageSource;
6
4
 
7
5
  namespace ngn::appkit {
8
6
 
9
- std::optional<ImageSource> pickImageSource(const std::vector<ImageSource> &sources, double screenScale) {
10
- std::optional<ImageSource> best;
11
- double distance = INFINITY;
12
- for (const auto &source : sources) {
13
- if (source.uri.empty()) {
14
- continue;
15
- }
16
- double d = std::abs(source.scale - screenScale);
17
- if (d < distance) {
18
- distance = d;
19
- best = source;
20
- }
21
- }
22
- return best;
23
- }
24
-
25
- bool sameImageRequest(const ImageSource &a, const ImageSource &b) {
26
- return a.uri == b.uri && a.cache == b.cache && a.method == b.method && a.headers == b.headers && a.body == b.body;
27
- }
28
-
29
7
  NSURLRequest *imageRequest(NSURL *url, const ImageSource &source) {
30
8
  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
31
9
  // As React Native iOS maps a source's `cache` (RCTImagePrimitivesConversions.h).
@@ -1,19 +1,71 @@
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"
7
+ #include "ngn/image-source.h"
5
8
 
6
9
  #include <react/renderer/components/image/ImageEventEmitter.h>
7
10
  #include <react/renderer/components/image/ImageProps.h>
8
11
 
9
12
  using namespace facebook::react;
10
13
 
14
+ namespace {
15
+
16
+ // The image's shape filled with `tint`, at the image's own pixels: what UIKit's template rendering
17
+ // makes of it, and what React Native iOS shows for `tintColor`. An image with no pixels to draw is
18
+ // handed back as it is.
19
+ NSImage *tintedImage(NSImage *image, const SharedColor &tint) {
20
+ CGImageRef source = [image CGImageForProposedRect:nullptr context:nil hints:nil];
21
+ size_t width = source ? CGImageGetWidth(source) : 0, height = source ? CGImageGetHeight(source) : 0;
22
+ if (width == 0 || height == 0) {
23
+ return image;
24
+ }
25
+ CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
26
+ CGContextRef context = CGBitmapContextCreate(nullptr, width, height, 8, 0, space, kCGImageAlphaPremultipliedLast);
27
+ CGColorSpaceRelease(space);
28
+ if (context == nullptr) {
29
+ return image;
30
+ }
31
+ CGRect rect = CGRectMake(0, 0, width, height);
32
+ CGContextDrawImage(context, rect, source);
33
+ // Source-in keeps the colour of the tint where the image has alpha, and the image's alpha with it.
34
+ CGContextSetBlendMode(context, kCGBlendModeSourceIn);
35
+ auto c = colorComponentsFromColor(tint);
36
+ CGContextSetRGBFillColor(context, c.red, c.green, c.blue, c.alpha);
37
+ CGContextFillRect(context, rect);
38
+ CGImageRef made = CGBitmapContextCreateImage(context);
39
+ CGContextRelease(context);
40
+ if (made == nullptr) {
41
+ return image;
42
+ }
43
+ NSImage *result = [[NSImage alloc] initWithCGImage:made size:image.size];
44
+ CGImageRelease(made);
45
+ return result;
46
+ }
47
+
48
+ } // namespace
49
+
11
50
  @implementation NGNImageView {
12
51
  NSString *_uri;
13
52
  // The source picked, whose method, headers and body the request carries.
14
53
  std::optional<ImageSource> _source;
15
54
  NSURLSessionDataTask *_task;
16
55
  NSImage *_image;
56
+ // resizeMode `repeat`: the image tiles at its own size from the top left, as React Native iOS
57
+ // tiles it, instead of being drawn once by the layer's contents gravity.
58
+ BOOL _repeat;
59
+ // The picked source's scale: how many of the picture's pixels make a point, as React Native iOS
60
+ // hands the source's scale to its loader. Sizes the repeat tile.
61
+ double _scale;
62
+ // The `tintColor` the image is shown in, if it has one. Present and transparent black (0, which
63
+ // converts to false) still tints: the picture goes clear, as it does in React Native iOS.
64
+ std::optional<SharedColor> _tint;
65
+ // Holds the image under the view's children. The view's own layer is no place for it: NGNView
66
+ // draws its box in drawRect:, and each redraw (every layout asks for one) replaces the layer's
67
+ // contents, so an image that loaded before the redraw was wiped.
68
+ CALayer *_imageLayer;
17
69
  // Moved on by each load: a completion already queued for an earlier one, even of the same uri,
18
70
  // is ignored.
19
71
  uint64_t _load;
@@ -23,6 +75,24 @@ using namespace facebook::react;
23
75
  [_task cancel];
24
76
  }
25
77
 
78
+ - (CALayer *)imageLayer {
79
+ if (_imageLayer == nil) {
80
+ _imageLayer = [CALayer layer];
81
+ _imageLayer.contentsGravity = self.layer.contentsGravity;
82
+ _imageLayer.frame = self.layer.bounds;
83
+ [self.layer insertSublayer:_imageLayer atIndex:0];
84
+ }
85
+ return _imageLayer;
86
+ }
87
+
88
+ - (void)updateLayout:(const LayoutMetrics &)metrics {
89
+ [super updateLayout:metrics];
90
+ [CATransaction begin];
91
+ [CATransaction setDisableActions:YES];
92
+ _imageLayer.frame = self.layer.bounds;
93
+ [CATransaction commit];
94
+ }
95
+
26
96
  - (std::shared_ptr<const ImageEventEmitter>)imageEmitter {
27
97
  return std::dynamic_pointer_cast<const ImageEventEmitter>(self.eventEmitter);
28
98
  }
@@ -48,10 +118,22 @@ using namespace facebook::react;
48
118
  break;
49
119
  }
50
120
  self.layer.masksToBounds = YES;
121
+ _imageLayer.contentsGravity = self.layer.contentsGravity;
122
+ BOOL repeat = p->resizeMode == ImageResizeMode::Repeat;
123
+ if (p->tintColor != _tint || repeat != _repeat) {
124
+ _tint = p->tintColor;
125
+ _repeat = repeat;
126
+ [self showImage];
127
+ }
51
128
  // The source whose scale is nearest the screen's, as React Native iOS picks.
52
129
  double screenScale = self.window ? self.window.backingScaleFactor : NSScreen.mainScreen.backingScaleFactor;
53
- auto source = ngn::appkit::pickImageSource(p->sources, screenScale);
54
- if (source.has_value() == _source.has_value() && (!source || ngn::appkit::sameImageRequest(*source, *_source))) {
130
+ auto source = ngn::pickImageSource(p->sources, screenScale);
131
+ double scale = source && source->scale > 0 ? source->scale : 1;
132
+ if (scale != _scale) {
133
+ _scale = scale;
134
+ [self showImage];
135
+ }
136
+ if (source.has_value() == _source.has_value() && (!source || ngn::sameImageRequest(*source, *_source))) {
55
137
  return;
56
138
  }
57
139
  _source = source;
@@ -63,7 +145,8 @@ using namespace facebook::react;
63
145
  [_task cancel];
64
146
  _task = nil;
65
147
  _load++;
66
- self.layer.contents = nil;
148
+ _image = nil;
149
+ [self showImage];
67
150
  // A file is drawn only inside the file boundary, and exactly the file checked.
68
151
  std::shared_ptr<ngn::FileAccess::Use> use;
69
152
  NSString *load = _uri;
@@ -116,13 +199,47 @@ using namespace facebook::react;
116
199
  [_task resume];
117
200
  }
118
201
 
202
+ // `shown`, the loaded picture or its tinted copy, at the loaded picture's pixel size divided by the
203
+ // source's scale, in points: the size one tile takes. A copy, so the image that other drawing uses
204
+ // keeps its own size. The tinted copy is drawn at the screen's scale, so its own pixels are no guide.
205
+ - (NSImage *)tile:(NSImage *)shown {
206
+ NSImageRep *rep = _image.representations.firstObject;
207
+ NSSize pixels = rep && rep.pixelsWide > 0 && rep.pixelsHigh > 0 ? NSMakeSize(rep.pixelsWide, rep.pixelsHigh) : _image.size;
208
+ NSImage *tile = [shown copy];
209
+ tile.size = NSMakeSize(pixels.width / _scale, pixels.height / _scale);
210
+ return tile;
211
+ }
212
+
213
+ // The image on its layer, in the tint if there is one: tiled as that layer's background, or as its
214
+ // contents.
215
+ - (void)showImage {
216
+ if (_image == nil && _imageLayer == nil) {
217
+ return;
218
+ }
219
+ CALayer *layer = [self imageLayer];
220
+ NSImage *shown = _image != nil && _tint.has_value() ? tintedImage(_image, *_tint) : _image;
221
+ [CATransaction begin];
222
+ [CATransaction setDisableActions:YES];
223
+ layer.contentsGravity = self.layer.contentsGravity;
224
+ if (shown != nil && _repeat) {
225
+ layer.contents = nil;
226
+ layer.backgroundColor = [NSColor colorWithPatternImage:[self tile:shown]].CGColor;
227
+ } else {
228
+ layer.backgroundColor = nil;
229
+ layer.contents = shown;
230
+ }
231
+ [CATransaction commit];
232
+ }
233
+
119
234
  - (void)loaded:(NSImage *)image {
120
235
  _image = image;
121
- self.layer.contents = image;
236
+ [self showImage];
122
237
  if (auto e = [self imageEmitter]) {
123
238
  NSImageRep *rep = image.representations.firstObject;
124
239
  ImageSource source;
125
240
  source.uri = std::string(_uri.UTF8String);
241
+ // In pixels, at a scale of 1: ImageSource's default scale of 3 would report three times the size.
242
+ source.scale = 1;
126
243
  source.size = {static_cast<Float>(rep ? rep.pixelsWide : image.size.width),
127
244
  static_cast<Float>(rep ? rep.pixelsHigh : image.size.height)};
128
245
  e->onLoad(source);
@@ -13,7 +13,7 @@
13
13
 
14
14
  #import "animation-frames.h"
15
15
  #import "input-replay.h"
16
- #include "ngn/gestures.h"
16
+ #include "ngn/host-views.h"
17
17
  #include "ngn/worklet-host.h"
18
18
  #import "notifications.h"
19
19
  #import "undo.h"
@@ -75,6 +75,7 @@
75
75
  #include "ngn/app-menus.h"
76
76
  #include "ngn/back-swipe.h"
77
77
  #include "ngn/core-modules.h"
78
+ #include "ngn/desktop-modules.h"
78
79
  #include "ngn/default-menu.h"
79
80
  #include "ngn/desktop-modal.h"
80
81
  #include "ngn/input-dispatcher.h"
@@ -823,6 +824,8 @@ void showAppWindow() {
823
824
 
824
825
  int main(int argc, const char *argv[]) {
825
826
  @autoreleasepool {
827
+ // The values the core takes from the OS it runs on (host-os.h).
828
+ const ngn::HostOs os = ngn::macOS();
826
829
  // What to run: argv and the environment in a dev run, and only the Info.plist in a packaged
827
830
  // app (launch-settings.h).
828
831
  NSDictionary *info = NSBundle.mainBundle.infoDictionary;
@@ -838,7 +841,7 @@ int main(int argc, const char *argv[]) {
838
841
  return [value isKindOfClass:NSString.class] ? std::optional<std::string>(value.UTF8String) : std::nullopt;
839
842
  },
840
843
  .resourcePath = NSBundle.mainBundle.resourcePath != nil ? NSBundle.mainBundle.resourcePath.UTF8String : "",
841
- });
844
+ }, os);
842
845
  if (auto *usage = std::get_if<std::string>(&launch)) {
843
846
  fprintf(stderr, "%s\n", usage->c_str());
844
847
  return 2;
@@ -860,13 +863,10 @@ int main(int argc, const char *argv[]) {
860
863
  app.delegate = delegate;
861
864
 
862
865
  gHost.backend = std::make_shared<ngn::appkit::AppKitBackend>();
863
- gHost.mounting = std::make_shared<ngn::MountingManager>(gHost.backend, gHost.backend);
864
- gHost.input = std::make_unique<ngn::InputDispatcher>(gHost.mounting->tree());
865
- ngn::appkit::installWorkletHost(gHost.mounting);
866
- // react-native-gesture-handler's recognisers over the view tree, fed by the input dispatcher,
867
- // their timers on the main queue.
868
- static auto gestures = std::make_shared<ngn::Gestures>(
869
- gHost.mounting->tree(), [](double ms, std::function<void()> fn) -> std::function<void()> {
866
+ // The mounting manager, the input dispatcher and the gesture recognisers over the backend
867
+ // (host-views.h), the recognisers' timers on the main queue.
868
+ auto views = ngn::makeHostViews(
869
+ gHost.backend, gHost.backend, [](double ms, std::function<void()> fn) -> std::function<void()> {
870
870
  auto cancelled = std::make_shared<std::atomic<bool>>(false);
871
871
  auto shared = std::make_shared<std::function<void()>>(std::move(fn));
872
872
  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, static_cast<int64_t>(ms * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
@@ -876,13 +876,9 @@ int main(int argc, const char *argv[]) {
876
876
  });
877
877
  return [cancelled] { *cancelled = true; };
878
878
  });
879
- gHost.input->setGestures(gestures.get());
880
- // A worklet gesture's events go to Reanimated's handlers on the UI runtime, and a worklet's
881
- // state manager comes back to the recognisers, while Reanimated is running.
882
- gestures->onWorkletEvent = [](const std::string &name, Tag view, const folly::dynamic &body) {
883
- ngn::workletHost().reanimatedEvent.call(name, view, body);
884
- };
885
- ngn::workletHost().setGestureState = [](int handlerTag, int state) { gestures->setState(handlerTag, state); };
879
+ gHost.mounting = views.mounting;
880
+ gHost.input = std::move(views.input);
881
+ ngn::appkit::installWorkletHost(gHost.mounting);
886
882
  // Each commit's time, for the performance budget (pnpm bench:macos), when asked for: every
887
883
  // frame of an animation commits, which would flood a dev run's terminal.
888
884
  if (settings.logCommits) {
@@ -1058,7 +1054,7 @@ int main(int argc, const char *argv[]) {
1058
1054
  });
1059
1055
  },
1060
1056
  .fakeSystemUI = settings.test && settings.test->fakeSystemUI,
1061
- .gestures = gestures,
1057
+ .gestures = views.gestures,
1062
1058
  .runOnUiThread = [](std::function<void()> task) {
1063
1059
  auto shared = std::make_shared<std::function<void()>>(std::move(task));
1064
1060
  dispatch_async(dispatch_get_main_queue(), ^{
@@ -1096,13 +1092,13 @@ int main(int argc, const char *argv[]) {
1096
1092
  });
1097
1093
  gHost.windowHost = std::make_shared<ngn::WindowHost>(gHost.windows, system);
1098
1094
  gHost.windows->setHost(gHost.windowHost);
1099
- ngn::addModule(*modules, "NgNativeWindows", [] { return ngn::makeWindowsModule(gHost.windowHost); });
1100
1095
 
1101
1096
  gHost.menus = std::make_shared<ngn::MenuHost>(
1102
1097
  std::make_shared<AppKitMenus>(),
1103
1098
  ngn::DefaultMenuOptions{
1104
- .appName = std::string(title.UTF8String), .developer = ngn::DevServer::fromBundleUrl(bundle).has_value()});
1105
- ngn::addModule(*modules, "NgNativeMenus", [] { return ngn::makeMenusModule(gHost.menus); });
1099
+ .appName = std::string(title.UTF8String),
1100
+ .developer = ngn::DevServer::fromBundleUrl(bundle).has_value(),
1101
+ .os = os});
1106
1102
  // Each window's history, as the app's router reports it, for its toolbar's chevrons.
1107
1103
  gHost.menus->historyChanged = [](SurfaceId surface) {
1108
1104
  dispatch_async(dispatch_get_main_queue(), ^{
@@ -1125,177 +1121,125 @@ int main(int argc, const char *argv[]) {
1125
1121
  gHost.tray = std::make_shared<ngn::TrayHost>(std::make_shared<ngn::appkit::AppKitTray>(
1126
1122
  [](const std::string &icon) { gHost.tray->press(icon); },
1127
1123
  [](const std::string &icon, const std::string &id) { gHost.tray->menuSelect(icon, id); }));
1128
- ngn::addModule(*modules, "NgNativeTray", [] { return ngn::makeTrayModule(gHost.tray); });
1129
1124
  gHost.dock = std::make_shared<ngn::appkit::AppKitDock>([](const std::string &id) { gHost.dockHost->menuSelect(id); });
1130
1125
  gHost.dockHost = std::make_shared<ngn::DockHost>(gHost.dock);
1131
- ngn::addModule(*modules, "NgNativeDock", [] { return ngn::makeDockModule(gHost.dockHost); });
1132
- {
1133
- // One launcher and one declaration for the process: a reload gets a new module over them.
1134
- static auto launcher = ngn::makePosixLauncher();
1135
- gHost.launcher = launcher;
1136
- static auto opener = ngn::appkit::makeShellOpener();
1137
- ngn::addModule(*modules, "NgNativeProcesses", [] { return ngn::makeProcessesModule(launcher, processes); });
1138
- ngn::addModule(*modules, "NgNativeShell", [] { return ngn::makeShellModule(opener, fileAccess); });
1139
- // The desktop Expo modules (DESKTOP_MODULES in @ng-native/expo).
1140
- static std::shared_ptr<ngn::SecretStore> keychain = ngn::appkit::makeKeychain();
1141
- static auto keychainService = appId;
1142
- if (settings.test) {
1143
- // The run's own items, removed as it ends, and any a crashed run left.
1144
- ngn::appkit::removeStaleTestItems(*settings.test);
1145
- auto prefixed = std::make_shared<ngn::PrefixedSecretStore>(keychain, settings.test->keychainPrefix());
1146
- ngn::appkit::onTestLaunchEnd([prefixed] { prefixed->clear(); });
1147
- keychain = prefixed;
1148
- }
1149
- static auto expoSystem = system;
1150
- static auto awake = std::make_shared<ngn::KeepAwakeTags>(ngn::appkit::makeSleepPreventer());
1151
- static auto haptics = ngn::appkit::makeHapticPlayer();
1152
- ngn::addModule(*modules, "NgNativeSecureStore", [] { return ngn::makeSecureStoreModule(keychain, keychainService); });
1153
- ngn::addModule(*modules, "NgNativeClipboard", [] { return ngn::makeDesktopClipboardModule(expoSystem, gHost.state); });
1154
- static auto notificationEvents = std::make_shared<ngn::NotificationEvents>();
1155
- static auto notifications = ngn::appkit::makeNotificationCenter(notificationEvents);
1156
- static auto pushTokens = std::make_shared<ngn::PushTokens>(ngn::appkit::makePushRegistrar(), notificationEvents);
1157
- gHost.push = pushTokens;
1158
- gHost.notificationEvents = notificationEvents;
1159
- ngn::addModule(*modules, "NgNativeNotifications", [] {
1160
- return ngn::makeNotificationsModule(notifications, notificationEvents, pushTokens);
1161
- });
1162
- static auto undo = ngn::appkit::makeUndoManager();
1163
- ngn::addModule(*modules, "NgNativeUndo", [] { return ngn::makeUndoModule(undo); });
1164
- ngn::addModule(*modules, "NgNativeKeepAwake", [] { return ngn::makeKeepAwakeModule(awake); });
1165
- ngn::addModule(*modules, "NgNativeHaptics", [] { return ngn::makeHapticsModule(haptics); });
1166
- static auto dialogs = settings.test && settings.test->fakeSystemUI ? ngn::makeDismissedFileDialogs()
1167
- : ngn::appkit::makeFileDialogs();
1168
- static auto fileWatcher = ngn::appkit::makeFileWatcher();
1169
- ngn::addModule(*modules, "NgNativeFileSystem", [] { return ngn::makeFileSystemModule(fileAccess, fileWatcher); });
1170
- ngn::addModule(*modules, "NgNativeDocumentPicker", [] { return ngn::makeDocumentPickerModule(dialogs, caches, fileAccess); });
1171
- // expo-sqlite keeps its databases in the app's documents, under SQLite; here, in its data.
1172
- static auto databases = system->appDataDirectory() + "/SQLite";
1173
- ngn::addModule(*modules, "NgNativeSqlite", [] { return ngn::makeSqliteModule(databases, fileAccess); });
1174
- static auto location = ngn::appkit::makeLocationSource();
1175
- static bool noPrompts = settings.noPrompts;
1176
- static auto geocoder = ngn::appkit::makeGeocoder();
1177
- ngn::addModule(*modules, "NgNativeLocation", [] { return ngn::makeLocationModule(location, noPrompts, geocoder); });
1178
- static auto authenticator = ngn::appkit::makeAuthenticator(noPrompts);
1179
- ngn::addModule(*modules, "NgNativeLocalAuthentication", [] { return ngn::makeLocalAuthenticationModule(authenticator); });
1180
- static auto bundleFacts = ngn::appkit::mainBundleFacts();
1181
- static auto appName = std::string(title.UTF8String);
1182
- ngn::addModule(*modules, "NgNativeApplication", [] { return ngn::makeApplicationModule(bundleFacts, keychainService, appName); });
1183
- static auto machine = ngn::appkit::machineFacts();
1184
- ngn::addModule(*modules, "NgNativeDevice", [] { return ngn::makeDeviceModule(machine); });
1185
- static auto pathMonitor = ngn::appkit::makePathMonitor();
1186
- ngn::addModule(*modules, "NgNativeNetwork", [] { return ngn::makeNetworkModule(pathMonitor); });
1187
- static auto power = ngn::appkit::makePowerSource();
1188
- ngn::addModule(*modules, "NgNativeBattery", [] { return ngn::makeBatteryModule(power); });
1189
- // The fonts loaded stay registered for the process, so their names outlast a reload too.
1190
- static auto fontLibrary = std::make_shared<ngn::FontLibrary>();
1191
- static auto fontRegistrar = ngn::appkit::makeFontRegistrar();
1192
- static auto fontHttp = network.http;
1193
- ngn::useFontLibrary(fontLibrary);
1194
- ngn::addModule(*modules, "NgNativeFont", [] {
1195
- return ngn::makeFontModule(fontRegistrar, fontLibrary, fontHttp, caches + "/fonts", fileAccess);
1196
- });
1197
- static auto authSession = ngn::appkit::makeAuthSession();
1198
- ngn::addModule(*modules, "NgNativeWebBrowser", [] { return ngn::makeWebBrowserModule(expoSystem, authSession, noPrompts); });
1199
- static auto textReader = ngn::appkit::makeTextReader();
1200
- ngn::addModule(*modules, "NgNativeTextRecognition", [] { return ngn::makeTextRecognitionModule(textReader, fileAccess); });
1201
- static auto calendars = ngn::appkit::makeCalendarStore();
1202
- ngn::addModule(*modules, "NgNativeCalendar", [] { return ngn::makeCalendarModule(calendars, noPrompts); });
1203
- // Players and recorders end with the runtime: a reload stops what the app played.
1204
- static auto audioOutput = ngn::appkit::makeAudioOutput();
1205
- static auto audioInput = ngn::appkit::makeAudioInput();
1206
- ngn::addModule(*modules, "NgNativeAudio", [] { return ngn::makeAudioModule(audioOutput, audioInput, caches + "/Audio", noPrompts, fileAccess); });
1207
- static auto videoOutput = ngn::appkit::makeVideoOutput();
1208
- ngn::addModule(*modules, "NgNativeVideo", [] { return ngn::makeVideoModule(videoOutput, fileAccess); });
1209
- static auto languageModel = ngn::appkit::makeLanguageModelEngine();
1210
- ngn::addModule(*modules, "NgNativeLocalLlm", [] { return ngn::makeLocalLlmModule(languageModel); });
1211
- ngn::addModule(*modules, "NgNativeScreenCapture", [] { return ngn::makeScreenCaptureModule(ngn::appkit::captureShield()); });
1212
- ngn::addModule(*modules, "NgNativeSplashScreen", [] { return ngn::makeSplashScreenModule(gSplash); });
1213
- static auto photos = ngn::appkit::makePhotoLibrary(caches);
1214
- ngn::addModule(*modules, "NgNativeMediaLibrary", [] { return ngn::makeMediaLibraryModule(photos, noPrompts, fileAccess); });
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
- });
1220
- static auto tracking = ngn::appkit::makeTrackingAuthority();
1221
- ngn::addModule(*modules, "NgNativeTrackingTransparency", [] { return ngn::makeTrackingModule(tracking, noPrompts); });
1222
- static auto imageHttp = network.http;
1223
- static auto addressLookup = ngn::appkit::makeAddressLookup();
1224
- ngn::addModule(*modules, "NgNativeAddressSearch", [] { return ngn::makeAddressSearchModule(addressLookup); });
1225
- ngn::addModule(*modules, "NgNativeAsset", [] { return ngn::makeAssetModule(imageHttp, caches, fileAccess); });
1226
- static auto symbolMeasurer = ngn::appkit::makeSymbolMeasurer();
1227
- ngn::addModule(*modules, "NgNativeSymbol", [] { return ngn::makeSymbolModule(symbolMeasurer); });
1228
- static auto sharePicker = ngn::appkit::makeSharePicker();
1229
- ngn::addModule(*modules, "NgNativeSharing", [] { return ngn::makeSharingModule(sharePicker, noPrompts, fileAccess); });
1230
- static auto imageCodec = ngn::appkit::makeImageCodec();
1231
- ngn::addModule(*modules, "NgNativeImageManipulator", [] {
1232
- return ngn::makeImageManipulatorModule(imageCodec, imageHttp, caches + "/ImageManipulator", fileAccess);
1233
- });
1234
- // <expo-image>'s bytes: 64 MB of them in memory, the rest on disk in the caches.
1235
- [NGNExpoImageView useHttp:imageHttp cache:std::make_shared<ngn::ImageCache>(caches + "/ExpoImage", 64 << 20)];
1236
- static auto cameraViews = std::make_shared<ngn::CameraViews>();
1237
- [NGNCameraView useHardware:cameraHardware views:cameraViews noPrompts:noPrompts];
1238
- ngn::addModule(*modules, "NgNativeCamera", [] { return ngn::makeCameraModule(cameraHardware, cameraViews, caches + "/Camera", noPrompts); });
1239
- static auto mapViews = std::make_shared<ngn::MapViews>();
1240
- [NGNMapView useViews:mapViews location:location];
1241
- ngn::addModule(*modules, "NgNativeMaps", [] { return ngn::makeMapsModule(mapViews); });
1242
- // Sign in with Apple: available only to an app signed with its entitlement.
1243
- static auto appleId = ngn::appkit::makeAppleIdProvider();
1244
- ngn::addModule(*modules, "NgNativeAppleAuthentication", [] { return ngn::makeAppleAuthenticationModule(appleId, noPrompts); });
1245
- static auto printSystem = ngn::appkit::makePrintSystem([](SurfaceId surface) -> NSWindow * {
1246
- return surface == kAppSurface ? gHost.appWindow : gHost.windows->window(surface);
1247
- });
1248
- ngn::addModule(*modules, "NgNativePrint", [] { return ngn::makePrintModule(printSystem, imageHttp, caches + "/Print", fileAccess, noPrompts); });
1249
- // react-native-webview's navigation answers; harmless in a host built without the library.
1250
- ngn::addModule(*modules, "RNCWebViewModule", [] { return ngn::makeWebViewModule(ngn::sharedNavigationLocks()); });
1251
- // A rating prompt only in a Mac App Store build: one sandboxed but shipped another way has none.
1252
- static auto packaged = ngn::appkit::packagedApp();
1253
- static auto reviewPrompt = ngn::appkit::makeReviewPrompt(packaged && packaged->distribution == "mas");
1254
- ngn::addModule(*modules, "NgNativeStoreReview", [] { return ngn::makeStoreReviewModule(reviewPrompt, noPrompts); });
1255
- static auto crypto = ngn::appkit::makeCryptoSource();
1256
- ngn::addModule(*modules, "NgNativeCrypto", [] { return ngn::makeCryptoModule(crypto); });
1257
- static auto locales = ngn::appkit::makeLocaleSource();
1258
- ngn::addModule(*modules, "NgNativeLocalization", [] {
1259
- return ngn::makeLocalizationModule(locales, ngn::appkit::systemRegionCode());
1260
- });
1126
+ // The secrets' store: a test launch's own items, removed as it ends, and any a crashed run left.
1127
+ std::shared_ptr<ngn::SecretStore> keychain = ngn::appkit::makeKeychain();
1128
+ if (settings.test) {
1129
+ ngn::appkit::removeStaleTestItems(*settings.test);
1130
+ auto prefixed = std::make_shared<ngn::PrefixedSecretStore>(keychain, settings.test->keychainPrefix());
1131
+ ngn::appkit::onTestLaunchEnd([prefixed] { prefixed->clear(); });
1132
+ keychain = prefixed;
1261
1133
  }
1262
-
1263
1134
  // A bundle on disk runs the newest update the Expo Updates client keeps, when the app has an
1264
1135
  // updates URL; one from a dev server never does.
1265
- {
1266
- bool fromDevServer = ngn::DevServer::fromBundleUrl(bundle).has_value();
1267
- gHost.updates = std::make_shared<ngn::UpdatesClient>(
1268
- ngn::updatesConfigFrom(settings, system->appDataDirectory(), bundle), network.http, ngn::appkit::makeUpdateCrypto());
1269
- if (!fromDevServer && gHost.updates->enabled()) {
1270
- bundle = gHost.updates->launch();
1271
- if (auto running = gHost.updates->running()) {
1272
- fprintf(stderr, "[ngn] running update %s\n", running->id.c_str());
1273
- }
1136
+ bool fromDevServer = ngn::DevServer::fromBundleUrl(bundle).has_value();
1137
+ gHost.updates = std::make_shared<ngn::UpdatesClient>(
1138
+ ngn::updatesConfigFrom(settings, os, system->appDataDirectory(), bundle), network.http, ngn::appkit::makeUpdateCrypto());
1139
+ if (!fromDevServer && gHost.updates->enabled()) {
1140
+ bundle = gHost.updates->launch();
1141
+ if (auto running = gHost.updates->running()) {
1142
+ fprintf(stderr, "[ngn] running update %s\n", running->id.c_str());
1274
1143
  }
1275
- // Sparkle starts with the app, as its own updater does: one for the process.
1276
- static auto appUpdater = ngn::appkit::makeSparkleUpdater();
1277
- ngn::addModule(*modules, "NgNativeUpdates", [fromDevServer] {
1278
- return ngn::makeUpdatesModule(gHost.updates, fromDevServer, [](const std::string &next, std::function<void(bool)> done) {
1279
- setRunningBundle(next);
1280
- // The launch the module's beginReload just began.
1281
- uint64_t generation = gHost.updates->launchGeneration();
1282
- gHost.runtime->reload(next, [next, done, generation](bool loaded) {
1283
- if (!loaded) {
1284
- fprintf(stderr, "ngn-macos: the update's bundle %s did not load\n", next.c_str());
1285
- }
1286
- // A bundle that did not load never counts as a launch that succeeded; one that did counts
1287
- // from the first frame its own instance mounts on screen.
1288
- if (loaded) {
1289
- uint64_t instance = gHost.mounting->instance();
1290
- dispatch_async(dispatch_get_main_queue(), ^{
1291
- reloadLaunched(generation, instance);
1292
- });
1293
- }
1294
- done(loaded);
1295
- });
1296
- }, appUpdater);
1297
- });
1298
1144
  }
1145
+ // The map and camera views reach the same location service and camera as their modules.
1146
+ auto location = ngn::appkit::makeLocationSource();
1147
+ auto cameraHardware = ngn::appkit::makeCameraHardware();
1148
+ // A rating prompt only in a Mac App Store build: one sandboxed but shipped another way has none.
1149
+ auto packaged = ngn::appkit::packagedApp();
1150
+ // The desktop host's own modules over AppKit's sources (desktop-modules.h).
1151
+ auto desktop = ngn::addDesktopModules(
1152
+ *modules,
1153
+ {.os = os,
1154
+ .system = system,
1155
+ .state = gHost.state,
1156
+ .http = network.http,
1157
+ .files = fileAccess,
1158
+ .appId = appId,
1159
+ .appName = std::string(title.UTF8String),
1160
+ .caches = caches,
1161
+ .noPrompts = settings.noPrompts,
1162
+ .windows = gHost.windowHost,
1163
+ .menus = gHost.menus,
1164
+ .tray = gHost.tray,
1165
+ .dock = gHost.dockHost,
1166
+ .processes = processes,
1167
+ .shell = ngn::appkit::makeShellOpener(),
1168
+ .secrets = keychain,
1169
+ .sleep = ngn::appkit::makeSleepPreventer(),
1170
+ .haptics = ngn::appkit::makeHapticPlayer(),
1171
+ .notifications = ngn::appkit::makeNotificationCenter,
1172
+ .push = ngn::appkit::makePushRegistrar(),
1173
+ .undo = ngn::appkit::makeUndoManager(),
1174
+ .fileDialogs = settings.test && settings.test->fakeSystemUI ? ngn::makeDismissedFileDialogs()
1175
+ : ngn::appkit::makeFileDialogs(),
1176
+ .fileWatcher = ngn::appkit::makeFileWatcher(),
1177
+ .location = location,
1178
+ .geocoder = ngn::appkit::makeGeocoder(),
1179
+ .authenticator = ngn::appkit::makeAuthenticator(settings.noPrompts),
1180
+ .bundle = ngn::appkit::mainBundleFacts(),
1181
+ .machine = ngn::appkit::machineFacts(),
1182
+ .pathMonitor = ngn::appkit::makePathMonitor(),
1183
+ .power = ngn::appkit::makePowerSource(),
1184
+ .fontRegistrar = ngn::appkit::makeFontRegistrar(),
1185
+ .authSession = ngn::appkit::makeAuthSession(),
1186
+ .textReader = ngn::appkit::makeTextReader(),
1187
+ .calendars = ngn::appkit::makeCalendarStore(),
1188
+ .audioOutput = ngn::appkit::makeAudioOutput(),
1189
+ .audioInput = ngn::appkit::makeAudioInput(),
1190
+ .videoOutput = ngn::appkit::makeVideoOutput(),
1191
+ .languageModel = ngn::appkit::makeLanguageModelEngine(),
1192
+ .captureShield = ngn::appkit::captureShield(),
1193
+ .splash = gSplash,
1194
+ .photos = ngn::appkit::makePhotoLibrary(caches),
1195
+ .cameraHardware = cameraHardware,
1196
+ .cameraSheet = ngn::appkit::makeCameraSheet(),
1197
+ .tracking = ngn::appkit::makeTrackingAuthority(),
1198
+ .addressLookup = ngn::appkit::makeAddressLookup(),
1199
+ .symbolMeasurer = ngn::appkit::makeSymbolMeasurer(),
1200
+ .sharePicker = ngn::appkit::makeSharePicker(),
1201
+ .imageCodec = ngn::appkit::makeImageCodec(),
1202
+ // Sign in with Apple: available only to an app signed with its entitlement.
1203
+ .appleId = ngn::appkit::makeAppleIdProvider(),
1204
+ .printSystem = ngn::appkit::makePrintSystem([](SurfaceId surface) -> NSWindow * {
1205
+ return surface == kAppSurface ? gHost.appWindow : gHost.windows->window(surface);
1206
+ }),
1207
+ .reviewPrompt = ngn::appkit::makeReviewPrompt(packaged && packaged->distribution == "mas"),
1208
+ .crypto = ngn::appkit::makeCryptoSource(),
1209
+ .locales = ngn::appkit::makeLocaleSource(),
1210
+ .regionCode = ngn::appkit::systemRegionCode,
1211
+ .updates = gHost.updates,
1212
+ .fromDevServer = fromDevServer,
1213
+ .reload =
1214
+ [](const std::string &next, std::function<void(bool)> done) {
1215
+ setRunningBundle(next);
1216
+ // The launch the module's beginReload just began.
1217
+ uint64_t generation = gHost.updates->launchGeneration();
1218
+ gHost.runtime->reload(next, [next, done, generation](bool loaded) {
1219
+ if (!loaded) {
1220
+ fprintf(stderr, "ngn-macos: the update's bundle %s did not load\n", next.c_str());
1221
+ }
1222
+ // A bundle that did not load never counts as a launch that succeeded; one that did
1223
+ // counts from the first frame its own instance mounts on screen.
1224
+ if (loaded) {
1225
+ uint64_t instance = gHost.mounting->instance();
1226
+ dispatch_async(dispatch_get_main_queue(), ^{
1227
+ reloadLaunched(generation, instance);
1228
+ });
1229
+ }
1230
+ done(loaded);
1231
+ });
1232
+ },
1233
+ // Sparkle starts with the app, as its own updater does: one for the process.
1234
+ .appUpdater = ngn::appkit::makeSparkleUpdater()});
1235
+ gHost.launcher = desktop.launcher;
1236
+ gHost.push = desktop.push;
1237
+ gHost.notificationEvents = desktop.notificationEvents;
1238
+ ngn::useFontLibrary(desktop.fonts);
1239
+ // <expo-image>'s bytes: 64 MB of them in memory, the rest on disk in the caches.
1240
+ [NGNExpoImageView useHttp:network.http cache:std::make_shared<ngn::ImageCache>(caches + "/ExpoImage", 64 << 20)];
1241
+ [NGNCameraView useHardware:cameraHardware views:desktop.cameraViews noPrompts:settings.noPrompts];
1242
+ [NGNMapView useViews:desktop.mapViews location:location];
1299
1243
 
1300
1244
  setRunningBundle(bundle);
1301
1245
  gHost.runtime = std::make_unique<ngn::Runtime>(
@@ -1305,7 +1249,8 @@ int main(int argc, const char *argv[]) {
1305
1249
  modules, network, bundle,
1306
1250
  ngn::DebuggerIdentity{
1307
1251
  .appId = appId,
1308
- .deviceName = NSHost.currentHost.localizedName ? NSHost.currentHost.localizedName.UTF8String : "Mac"});
1252
+ .deviceName = NSHost.currentHost.localizedName ? NSHost.currentHost.localizedName.UTF8String : "Mac",
1253
+ .os = os.id});
1309
1254
  // Each surface lays out the way I18nManager.isRTL says for the instance running, read again
1310
1255
  // as a reload starts the next.
1311
1256
  gHost.runtime->rightToLeft = [system] { return ngn::appRightToLeft(*system); };