@ng-native/desktop 0.25.0 → 0.26.1

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 (66) hide show
  1. package/native/CMakeLists.txt +47 -4
  2. package/native/appkit/CMakeLists.txt +2 -1
  3. package/native/appkit/cg-canvas.h +5 -0
  4. package/native/appkit/cg-canvas.mm +21 -6
  5. package/native/appkit/desktop-expo.h +6 -0
  6. package/native/appkit/desktop-expo.mm +5 -0
  7. package/native/appkit/host-view.mm +32 -0
  8. package/native/appkit/image-request.h +2 -10
  9. package/native/appkit/image-request.mm +0 -22
  10. package/native/appkit/image-view.mm +94 -6
  11. package/native/appkit/main.mm +130 -185
  12. package/native/appkit/split-view.mm +3 -9
  13. package/native/appkit/system-access.mm +3 -0
  14. package/native/appkit/system.mm +33 -48
  15. package/native/appkit/view-effects.h +27 -0
  16. package/native/appkit/view-effects.mm +90 -0
  17. package/native/cmake/runtime-dependencies.cmake +11 -1
  18. package/native/core/include/ngn/app-info.h +3 -2
  19. package/native/core/include/ngn/app-menus.h +5 -11
  20. package/native/core/include/ngn/default-menu.h +16 -6
  21. package/native/core/include/ngn/desktop-expo.h +0 -5
  22. package/native/core/include/ngn/desktop-modules.h +144 -0
  23. package/native/core/include/ngn/dev-server.h +12 -3
  24. package/native/core/include/ngn/host-os.h +45 -0
  25. package/native/core/include/ngn/host-views.h +25 -0
  26. package/native/core/include/ngn/image-source.h +18 -0
  27. package/native/core/include/ngn/launch-settings.h +9 -7
  28. package/native/core/include/ngn/module-registry.h +2 -0
  29. package/native/core/include/ngn/notifications.h +6 -0
  30. package/native/core/include/ngn/runtime.h +4 -1
  31. package/native/core/include/ngn/spawn-rules.h +3 -2
  32. package/native/core/include/ngn/split-view.h +12 -0
  33. package/native/core/include/ngn/system.h +47 -0
  34. package/native/core/include/ngn/undo.h +27 -0
  35. package/native/core/include/ngn/updates.h +3 -2
  36. package/native/core/include/ngn/view-filter.h +33 -0
  37. package/native/core/src/app-menus.cpp +9 -11
  38. package/native/core/src/default-menu.cpp +6 -2
  39. package/native/core/src/dev-server-helper.cpp +3 -2
  40. package/native/core/src/dev-server.cpp +22 -5
  41. package/native/core/src/host-os.cpp +20 -0
  42. package/native/core/src/host-views.cpp +25 -0
  43. package/native/core/src/image-source.cpp +30 -0
  44. package/native/core/src/launch-settings.cpp +10 -6
  45. package/native/core/src/module-registry.cpp +9 -0
  46. package/native/core/src/modules/app-info.cpp +4 -4
  47. package/native/core/src/modules/desktop-expo.cpp +0 -5
  48. package/native/core/src/modules/desktop-modules.cpp +132 -0
  49. package/native/core/src/modules/notifications.cpp +4 -4
  50. package/native/core/src/runtime.cpp +2 -0
  51. package/native/core/src/spawn-rules.cpp +1 -4
  52. package/native/core/src/split-view.cpp +10 -0
  53. package/native/core/src/system.cpp +52 -0
  54. package/native/core/src/undo-stacks.cpp +71 -0
  55. package/native/core/src/view-filter.cpp +114 -0
  56. package/native/scripts/build-hermes.sh +19 -7
  57. package/native/scripts/build-icu.sh +36 -0
  58. package/native/scripts/build-lock.sh +21 -0
  59. package/native/scripts/build.sh +40 -7
  60. package/native/scripts/fetch.sh +24 -2
  61. package/native/scripts/hermes-built.sh +5 -4
  62. package/native/scripts/hermes-target.sh +18 -0
  63. package/native/scripts/icu-filter.json +22 -0
  64. package/native/shim/openssl/sha.h +108 -3
  65. package/package.json +2 -2
  66. package/prebuilt.json +6 -6
@@ -6,7 +6,8 @@
6
6
  #
7
7
  # scripts/build.sh is the way in: it fetches the pinned sources into the per-user cache and passes
8
8
  # their paths here. NGN_BACKEND=none builds only the core and its tests (any OS, no Hermes), which
9
- # is what proves the core never reaches for a toolkit.
9
+ # is what proves the core never reaches for a toolkit; with NGN_RUNTIME, the runtime half over
10
+ # Hermes too, and its tests, still with no toolkit.
10
11
  cmake_minimum_required(VERSION 3.25)
11
12
  project(ngn_desktop LANGUAGES C CXX)
12
13
 
@@ -18,6 +19,7 @@ endif()
18
19
  set(NGN_BACKEND ${_default_backend} CACHE STRING "appkit or none")
19
20
  set_property(CACHE NGN_BACKEND PROPERTY STRINGS appkit none)
20
21
  option(NGN_TESTS "Build the core tests" ON)
22
+ option(NGN_RUNTIME "With NGN_BACKEND=none, build the runtime half over Hermes too" OFF)
21
23
 
22
24
  set(RN_DIR "" CACHE PATH "The installed react-native package")
23
25
  set(NGN_SOURCES "" CACHE PATH "Sources fetched by scripts/fetch.sh (<cache>/src)")
@@ -52,7 +54,7 @@ endif()
52
54
  if(NGN_BACKEND STREQUAL "appkit")
53
55
  set(NGN_WITH_RUNTIME ON)
54
56
  elseif(NGN_BACKEND STREQUAL "none")
55
- set(NGN_WITH_RUNTIME OFF)
57
+ set(NGN_WITH_RUNTIME ${NGN_RUNTIME})
56
58
  else()
57
59
  message(FATAL_ERROR "NGN_BACKEND must be appkit or none, not ${NGN_BACKEND}")
58
60
  endif()
@@ -92,7 +94,10 @@ add_library(ngn_core STATIC
92
94
  core/src/gestures.cpp
93
95
  core/src/glass.cpp
94
96
  core/src/header-bar.cpp
97
+ core/src/host-os.cpp
95
98
  core/src/host-state.cpp
99
+ core/src/host-views.cpp
100
+ core/src/image-source.cpp
96
101
  core/src/input-dispatcher.cpp
97
102
  core/src/input-script.cpp
98
103
  core/src/key-value-store.cpp
@@ -107,6 +112,7 @@ add_library(ngn_core STATIC
107
112
  core/src/text-input.cpp
108
113
  core/src/text-layout-manager.cpp
109
114
  core/src/tray-and-dock.cpp
115
+ core/src/undo-stacks.cpp
110
116
  core/src/safe-areas.cpp
111
117
  core/src/scrollers.cpp
112
118
  core/src/posix-launcher.cpp
@@ -117,10 +123,12 @@ add_library(ngn_core STATIC
117
123
  core/src/split-view.cpp
118
124
  core/src/svg.cpp
119
125
  core/src/symbol.cpp
126
+ core/src/system.cpp
120
127
  core/src/tabs-host.cpp
121
128
  core/src/download.cpp
122
129
  core/src/updates.cpp
123
130
  core/src/view-box.cpp
131
+ core/src/view-filter.cpp
124
132
  core/src/view-transform.cpp
125
133
  core/src/red-box.cpp
126
134
  core/src/expo-image.cpp
@@ -143,6 +151,7 @@ add_library(ngn_core STATIC
143
151
  core/src/modules/core-modules.cpp
144
152
  core/src/modules/crypto.cpp
145
153
  core/src/modules/desktop-expo.cpp
154
+ core/src/modules/desktop-modules.cpp
146
155
  core/src/modules/file-system.cpp
147
156
  core/src/modules/fonts.cpp
148
157
  core/src/modules/image-loader.cpp
@@ -176,7 +185,8 @@ target_compile_options(ngn_core PRIVATE -Wall -Wextra -Werror -Wno-unused-parame
176
185
  if(NGN_TESTS)
177
186
  file(GLOB core_test_SRC CONFIGURE_DEPENDS core/tests/*.cpp)
178
187
  add_executable(ngn_core_tests ${core_test_SRC} core/testing/fake-backend.cpp core/testing/fake-network.cpp core/testing/fake-system.cpp)
179
- target_include_directories(ngn_core_tests PRIVATE core/testing core/tests)
188
+ # shim/ for the SHA-256 shim's own test.
189
+ target_include_directories(ngn_core_tests PRIVATE core/testing core/tests shim)
180
190
  target_link_libraries(ngn_core_tests PRIVATE ngn_core doctest)
181
191
  # doctest includes <ciso646> under clang, which libstdc++ 15 marks deprecated with a #warning;
182
192
  # keep it a warning rather than an error under -Werror (libc++, on Apple, has no such warning).
@@ -185,6 +195,15 @@ if(NGN_TESTS)
185
195
  target_compile_options(ngn_core_tests PRIVATE
186
196
  $<$<CXX_COMPILER_ID:Clang>:-Wno-error=\#warnings>
187
197
  $<$<CXX_COMPILER_ID:GNU>:-Wno-error=cpp>)
198
+ # clang 22's -Wpedantic calls doctest's __COUNTER__ a C2y extension. -Wpedantic comes from
199
+ # React Native's interface options, which follow the target's own, so the exception goes on
200
+ # the sources, whose options come last.
201
+ include(CheckCXXCompilerFlag)
202
+ check_cxx_compiler_flag(-Wc2y-extensions NGN_HAS_WC2Y_EXTENSIONS)
203
+ if(NGN_HAS_WC2Y_EXTENSIONS)
204
+ set(NGN_DOCTEST_SOURCE_OPTIONS -Wno-c2y-extensions)
205
+ set_source_files_properties(${core_test_SRC} PROPERTIES COMPILE_OPTIONS -Wno-c2y-extensions)
206
+ endif()
188
207
  endif()
189
208
  # The AppKit backend's own logic that needs no window: on macOS only, over the system's frameworks.
190
209
  if(APPLE)
@@ -233,8 +252,10 @@ if(NGN_TESTS)
233
252
  appkit/menu-bar.mm appkit/tests/menu-bar.test.mm
234
253
  appkit/system.mm appkit/tests/system.test.mm
235
254
  appkit/image-request.mm appkit/tests/image-request.test.mm
255
+ appkit/view-effects.mm appkit/tests/view-effects.test.mm
236
256
  appkit/tests/pointer-buttons.test.mm
237
- appkit/test-launch.mm appkit/desktop-expo.mm appkit/tests/test-launch.test.mm appkit/tests/locales.test.mm)
257
+ appkit/test-launch.mm appkit/desktop-expo.mm appkit/tests/test-launch.test.mm appkit/tests/locales.test.mm
258
+ appkit/tests/desktop-expo.test.mm)
238
259
  target_compile_options(ngn_core_tests PRIVATE $<$<COMPILE_LANGUAGE:OBJCXX>:-fobjc-arc>)
239
260
  target_link_libraries(ngn_core_tests PRIVATE "-framework Foundation" "-framework Security" "-framework IOKit" "-framework SystemConfiguration" "-framework Network" "-framework AppKit" "-framework CoreText" "-framework ImageIO" "-framework Vision" "-framework UniformTypeIdentifiers" "-framework EventKit" "-framework AVFoundation" "-framework CoreMedia" "-framework Photos" "-framework PhotosUI" "-framework AppTrackingTransparency" "-framework AdSupport" "-framework CoreLocation" "-framework MapKit" "-framework WebKit" "-framework PDFKit" "-framework CoreImage" "-framework AuthenticationServices" "-framework AVKit" "-framework UserNotifications")
240
261
  endif()
@@ -265,6 +286,28 @@ if(NGN_WITH_RUNTIME)
265
286
  file(CONFIGURE OUTPUT ${CMAKE_BINARY_DIR}/generated/red-box-errors.h CONTENT
266
287
  "// Generated by CMakeLists.txt from core/src/red-box-errors.js.\n#pragma once\nconstexpr const char *kRedBoxErrors = R\"ngn(@NGN_RED_BOX_ERRORS_JS@)ngn\";\n" @ONLY)
267
288
  target_include_directories(ngn_core_runtime PRIVATE ${CMAKE_BINARY_DIR}/generated)
289
+
290
+ # Hermes and the runtime half linked whole, as a host links them: what no backend links yet on
291
+ # Linux, so an unresolved symbol or a missing ICU fails here first.
292
+ if(NGN_TESTS)
293
+ file(GLOB runtime_test_SRC CONFIGURE_DEPENDS core/runtime-tests/*.cpp)
294
+ add_executable(ngn_runtime_tests ${runtime_test_SRC})
295
+ target_link_libraries(ngn_runtime_tests PRIVATE "$<LINK_LIBRARY:WHOLE_ARCHIVE,ngn_core_runtime>" doctest)
296
+ # doctest's warnings, as for the core tests above.
297
+ if(NOT APPLE)
298
+ target_compile_options(ngn_runtime_tests PRIVATE
299
+ $<$<CXX_COMPILER_ID:Clang>:-Wno-error=\#warnings>
300
+ $<$<CXX_COMPILER_ID:GNU>:-Wno-error=cpp>)
301
+ endif()
302
+ if(NGN_DOCTEST_SOURCE_OPTIONS)
303
+ set_source_files_properties(${runtime_test_SRC} PROPERTIES COMPILE_OPTIONS ${NGN_DOCTEST_SOURCE_OPTIONS})
304
+ endif()
305
+ # A prebuilt Hermes framework loads through @rpath, as for the core tests above.
306
+ if(NGN_HERMES_TEST_RPATH)
307
+ target_link_options(ngn_runtime_tests PRIVATE ${NGN_HERMES_TEST_RPATH})
308
+ endif()
309
+ add_test(NAME runtime COMMAND ngn_runtime_tests)
310
+ endif()
268
311
  endif()
269
312
 
270
313
  # --- libraries' components (react-native-screens, react-native-safe-area-context) ---------------
@@ -81,6 +81,7 @@ add_library(ngn_appkit OBJECT
81
81
  text-layout.mm
82
82
  text-recognition.mm
83
83
  undo.mm
84
+ view-effects.mm
84
85
  updates.mm
85
86
  web-browser.mm
86
87
  app-updater.mm)
@@ -132,7 +133,7 @@ if(NGN_TESTS)
132
133
  add_executable(ngn_appkit_tests ../core/tests/main.cpp tests/views/accessibility.test.mm tests/views/svg-foreign-object.test.mm
133
134
  tests/views/tab-walk.test.mm tests/views/camera-view.test.mm tests/views/text-input-alignment.test.mm
134
135
  tests/views/image-view.test.mm tests/views/modal-beside-split.test.mm
135
- tests/views/split-under-pushed-screen.test.mm)
136
+ tests/views/split-under-pushed-screen.test.mm tests/views/view-effects.test.mm)
136
137
  target_include_directories(ngn_appkit_tests PRIVATE ../core/tests)
137
138
  target_link_libraries(ngn_appkit_tests PRIVATE ngn_appkit doctest ${NGN_APPKIT_FRAMEWORKS})
138
139
  if(NGN_HERMES_TEST_RPATH)
@@ -7,6 +7,7 @@
7
7
  #include "ngn/svg.h"
8
8
 
9
9
  #include <memory>
10
+ #include <string>
10
11
 
11
12
  // Both answer a +1 reference the caller releases.
12
13
  CGColorRef NGNCreateColor(uint32_t argb, double opacity);
@@ -22,3 +23,7 @@ std::unique_ptr<ngn::SvgCanvas> NGNMakeCGCanvas(
22
23
  CGFloat scale,
23
24
  void (^imageLoaded)(void),
24
25
  NSView * (^viewFor)(ngn::Tag) = nil);
26
+
27
+ // Whether the load of an svg image's uri failed, or its data was no image, so that later draws
28
+ // skip it. Tests wait on it to know the failure was recorded.
29
+ bool NGNSvgImageLoadFailed(const std::string &uri);
@@ -78,6 +78,13 @@ NSCache<NSString *, NSImage *> *imageCache() {
78
78
  return cache;
79
79
  }
80
80
 
81
+ // The uris whose load failed or whose data is no image: not asked for again on each draw, like a
82
+ // browser's broken image, for as long as the app runs.
83
+ NSMutableSet<NSString *> *failedImages() {
84
+ static NSMutableSet<NSString *> *failed = [NSMutableSet set];
85
+ return failed;
86
+ }
87
+
81
88
  // The canvas drawSvg draws on: Core Graphics calls on the view's context, or on a mask's while
82
89
  // one is drawn. It tracks the user transform itself, so a mask, drawn in a context of its own,
83
90
  // starts where the drawing is.
@@ -369,7 +376,9 @@ class CGCanvas final : public ngn::SvgCanvas {
369
376
  NSString *key = @(uri.c_str());
370
377
  NSImage *image = [imageCache() objectForKey:key];
371
378
  if (image == nil) {
372
- load(key);
379
+ if (![failedImages() containsObject:key]) {
380
+ load(key);
381
+ }
373
382
  return;
374
383
  }
375
384
  NSSize natural = image.size;
@@ -622,11 +631,13 @@ class CGCanvas final : public ngn::SvgCanvas {
622
631
  dispatch_async(dispatch_get_main_queue(), ^{
623
632
  NSArray *waiters = loading[uri];
624
633
  [loading removeObjectForKey:uri];
625
- if (image != nil) {
626
- [imageCache() setObject:image forKey:uri];
627
- for (void (^waiter)(void) in waiters) {
628
- waiter();
629
- }
634
+ if (image == nil) {
635
+ [failedImages() addObject:uri];
636
+ return;
637
+ }
638
+ [imageCache() setObject:image forKey:uri];
639
+ for (void (^waiter)(void) in waiters) {
640
+ waiter();
630
641
  }
631
642
  });
632
643
  }] resume];
@@ -645,6 +656,10 @@ class CGCanvas final : public ngn::SvgCanvas {
645
656
 
646
657
  } // namespace
647
658
 
659
+ bool NGNSvgImageLoadFailed(const std::string &uri) {
660
+ return [failedImages() containsObject:@(uri.c_str())];
661
+ }
662
+
648
663
  CGColorRef NGNCreateColor(uint32_t argb, double opacity) {
649
664
  return createColorImpl(argb, opacity);
650
665
  }
@@ -7,10 +7,16 @@
7
7
  #include "ngn/launch-settings.h"
8
8
 
9
9
  #include <memory>
10
+ #include <set>
10
11
  #include <string>
11
12
 
12
13
  namespace ngn::appkit {
13
14
 
15
+ // Whether the app can use the Data Protection keychain: its signature has a Team ID, and an
16
+ // entitlement that gives it a keychain access group (an application identifier, as a Mac App Store
17
+ // build has, or keychain-access-groups). Without one the keychain refuses it (errSecMissingEntitlement).
18
+ bool usesDataProtectionKeychain(const std::string &teamId, const std::set<std::string> &entitlements);
19
+
14
20
  std::shared_ptr<SecretStore> makeKeychain();
15
21
  // Removes the file keychain items another test launch left when it ended without removing them.
16
22
  // Reads attributes only, so no item's data is asked for.
@@ -144,6 +144,11 @@ class Trackpad final : public HapticPlayer {
144
144
 
145
145
  } // namespace
146
146
 
147
+ bool usesDataProtectionKeychain(const std::string &teamId, const std::set<std::string> &entitlements) {
148
+ return !teamId.empty() &&
149
+ (entitlements.contains("com.apple.application-identifier") || entitlements.contains("keychain-access-groups"));
150
+ }
151
+
147
152
  std::shared_ptr<SecretStore> makeKeychain() {
148
153
  auto [team, entitlements] = ownSigning();
149
154
  if (!usesDataProtectionKeychain(team, entitlements)) {
@@ -13,6 +13,7 @@
13
13
  #import "scroll-view.h"
14
14
  #import "split-view.h"
15
15
  #import "svg-view.h"
16
+ #import "view-effects.h"
16
17
  #import "symbol-view.h"
17
18
  #import "glass-view.h"
18
19
 
@@ -211,6 +212,9 @@ static ngn::InputDispatcher *gViewInput;
211
212
  // drawRect draws the box (ngn/view-box.h), which the layer does not.
212
213
  BOOL _boxDrawn;
213
214
  NSMutableArray<CALayer *> *_shadowLayers;
215
+ // The `filter` and `mix-blend-mode` on the layer now, so a layout does not set them again.
216
+ std::vector<ngn::FilterStep> _filterSteps;
217
+ BlendMode _blendMode;
214
218
  // The transform from the props, as NGNViewLayer holds it.
215
219
  CATransform3D _transform;
216
220
  }
@@ -653,6 +657,7 @@ static BOOL focusFromKeyboard() {
653
657
  layer.masksToBounds = viewProps->yogaStyle.overflow() != facebook::yoga::Overflow::Visible;
654
658
  layer.mask = box.clip ? shapeLayer(*box.clip, NO, self.bounds) : nil;
655
659
  [self applyShadows:box.shadows];
660
+ [self applyEffects:*viewProps];
656
661
  // Its transform, about its centre on a layer anchored at its corner (view-transform.h).
657
662
  CATransform3D transform = CATransform3DIdentity;
658
663
  if (auto matrix = ngn::layerTransform(*viewProps, _layout)) {
@@ -673,6 +678,33 @@ static BOOL focusFromKeyboard() {
673
678
  _boxDrawn = box.drawn;
674
679
  }
675
680
 
681
+ // `filter` and `mix-blend-mode` as Core Animation filters on the layer, which apply to the view's
682
+ // children too, and to nothing at all when the props have none: a filter or blend removed is
683
+ // taken off.
684
+ - (void)applyEffects:(const ViewProps &)props {
685
+ auto steps = ngn::filterSteps(props.filter);
686
+ CALayer *layer = self.layer;
687
+ if (steps != _filterSteps) {
688
+ _filterSteps = steps;
689
+ NSArray<CIFilter *> *filters = ngn::appkit::layerFilters(steps);
690
+ if (filters != nil) {
691
+ self.layerUsesCoreImageFilters = YES;
692
+ }
693
+ layer.filters = filters;
694
+ }
695
+ if (props.mixBlendMode != _blendMode) {
696
+ _blendMode = props.mixBlendMode;
697
+ NSString *blend = ngn::appkit::blendFilterName(_blendMode);
698
+ if (blend != nil) {
699
+ self.layerUsesCoreImageFilters = YES;
700
+ }
701
+ layer.compositingFilter = blend;
702
+ }
703
+ if (layer.filters.count == 0 && layer.compositingFilter == nil) {
704
+ self.layerUsesCoreImageFilters = NO;
705
+ }
706
+ }
707
+
676
708
  // Each box shadow as a layer under the children, as RCTViewComponentView adds them.
677
709
  - (void)applyShadows:(const std::vector<ngn::BoxShadowLayer> &)shadows {
678
710
  for (CALayer *old in _shadowLayers) {
@@ -1,21 +1,13 @@
1
- // What an Image asks for (image-view.h): the source whose scale is nearest the screen's, and the
2
- // request it is loaded with, its method, headers and body included.
1
+ // The request an Image loads its source with (image-view.h), its method, headers and body included.
2
+ // Which source it loads is the core's (ngn/image-source.h).
3
3
  #pragma once
4
4
 
5
5
  #import <Foundation/Foundation.h>
6
6
 
7
7
  #include <react/renderer/imagemanager/primitives.h>
8
8
 
9
- #include <optional>
10
- #include <vector>
11
-
12
9
  namespace ngn::appkit {
13
10
 
14
- // The source with a uri whose scale is nearest `screenScale`, as React Native iOS picks, or none.
15
- std::optional<facebook::react::ImageSource> pickImageSource(
16
- const std::vector<facebook::react::ImageSource> &sources, double screenScale);
17
- // Whether two sources load the same thing: ImageSource's own == compares only the uri.
18
- bool sameImageRequest(const facebook::react::ImageSource &a, const facebook::react::ImageSource &b);
19
11
  // The request for `url`, which the source's uri gave, with the source's method, headers and body.
20
12
  NSURLRequest *imageRequest(NSURL *url, const facebook::react::ImageSource &source);
21
13
 
@@ -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).
@@ -4,18 +4,64 @@
4
4
  #import <QuartzCore/QuartzCore.h>
5
5
 
6
6
  #include "ngn/file-system.h"
7
+ #include "ngn/image-source.h"
7
8
 
8
9
  #include <react/renderer/components/image/ImageEventEmitter.h>
9
10
  #include <react/renderer/components/image/ImageProps.h>
10
11
 
11
12
  using namespace facebook::react;
12
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
+
13
50
  @implementation NGNImageView {
14
51
  NSString *_uri;
15
52
  // The source picked, whose method, headers and body the request carries.
16
53
  std::optional<ImageSource> _source;
17
54
  NSURLSessionDataTask *_task;
18
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;
19
65
  // Holds the image under the view's children. The view's own layer is no place for it: NGNView
20
66
  // draws its box in drawRect:, and each redraw (every layout asks for one) replaces the layer's
21
67
  // contents, so an image that loaded before the redraw was wiped.
@@ -73,10 +119,21 @@ using namespace facebook::react;
73
119
  }
74
120
  self.layer.masksToBounds = YES;
75
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
+ }
76
128
  // The source whose scale is nearest the screen's, as React Native iOS picks.
77
129
  double screenScale = self.window ? self.window.backingScaleFactor : NSScreen.mainScreen.backingScaleFactor;
78
- auto source = ngn::appkit::pickImageSource(p->sources, screenScale);
79
- 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))) {
80
137
  return;
81
138
  }
82
139
  _source = source;
@@ -88,7 +145,8 @@ using namespace facebook::react;
88
145
  [_task cancel];
89
146
  _task = nil;
90
147
  _load++;
91
- _imageLayer.contents = nil;
148
+ _image = nil;
149
+ [self showImage];
92
150
  // A file is drawn only inside the file boundary, and exactly the file checked.
93
151
  std::shared_ptr<ngn::FileAccess::Use> use;
94
152
  NSString *load = _uri;
@@ -141,11 +199,41 @@ using namespace facebook::react;
141
199
  [_task resume];
142
200
  }
143
201
 
144
- - (void)loaded:(NSImage *)image {
145
- _image = image;
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
+ }
146
219
  CALayer *layer = [self imageLayer];
220
+ NSImage *shown = _image != nil && _tint.has_value() ? tintedImage(_image, *_tint) : _image;
221
+ [CATransaction begin];
222
+ [CATransaction setDisableActions:YES];
147
223
  layer.contentsGravity = self.layer.contentsGravity;
148
- layer.contents = image;
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
+
234
+ - (void)loaded:(NSImage *)image {
235
+ _image = image;
236
+ [self showImage];
149
237
  if (auto e = [self imageEmitter]) {
150
238
  NSImageRep *rep = image.representations.firstObject;
151
239
  ImageSource source;