@ng-native/desktop 0.23.0 → 0.24.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 (54) hide show
  1. package/native/CMakeLists.txt +3 -2
  2. package/native/appkit/CMakeLists.txt +25 -4
  3. package/native/appkit/camera-sheet.h +59 -0
  4. package/native/appkit/camera-sheet.mm +344 -0
  5. package/native/appkit/camera-view.mm +5 -1
  6. package/native/appkit/camera.h +22 -0
  7. package/native/appkit/camera.mm +203 -69
  8. package/native/appkit/cg-canvas.h +8 -1
  9. package/native/appkit/cg-canvas.mm +166 -21
  10. package/native/appkit/header-toolbar.mm +4 -1
  11. package/native/appkit/host-view.mm +29 -1
  12. package/native/appkit/main.mm +29 -69
  13. package/native/appkit/photo-library.mm +6 -6
  14. package/native/appkit/surface-view.mm +5 -1
  15. package/native/appkit/svg-view.h +2 -1
  16. package/native/appkit/svg-view.mm +49 -3
  17. package/native/appkit/tab-walk.h +31 -0
  18. package/native/appkit/tab-walk.mm +64 -0
  19. package/native/appkit/text-input-field.h +5 -0
  20. package/native/appkit/text-input-field.mm +101 -20
  21. package/native/appkit/text-layout.h +1 -1
  22. package/native/appkit/text-layout.mm +16 -8
  23. package/native/appkit/web-view-component.h +4 -3
  24. package/native/appkit/web-view-component.mm +8 -2
  25. package/native/appkit/web-view.mm +7 -0
  26. package/native/core/include/ngn/app-menus.h +8 -3
  27. package/native/core/include/ngn/back-swipe.h +16 -7
  28. package/native/core/include/ngn/camera.h +103 -9
  29. package/native/core/include/ngn/core-modules.h +3 -0
  30. package/native/core/include/ngn/dom-web-view.h +53 -0
  31. package/native/core/include/ngn/photo-library.h +7 -3
  32. package/native/core/include/ngn/runtime.h +10 -8
  33. package/native/core/include/ngn/safe-areas.h +5 -0
  34. package/native/core/include/ngn/surface-metrics.h +51 -0
  35. package/native/core/include/ngn/svg.h +49 -8
  36. package/native/core/include/ngn/text-input.h +9 -0
  37. package/native/core/include/ngn/web-view.h +6 -2
  38. package/native/core/src/app-menus.cpp +10 -3
  39. package/native/core/src/back-swipe.cpp +12 -5
  40. package/native/core/src/dom-web-view.cpp +45 -0
  41. package/native/core/src/libraries/rnsvg.cpp +13 -0
  42. package/native/core/src/modules/app-modules.cpp +20 -13
  43. package/native/core/src/modules/camera.cpp +203 -23
  44. package/native/core/src/modules/photo-library.cpp +22 -8
  45. package/native/core/src/mounting-manager.cpp +4 -0
  46. package/native/core/src/runtime.cpp +6 -8
  47. package/native/core/src/safe-areas.cpp +7 -0
  48. package/native/core/src/svg.cpp +324 -17
  49. package/native/core/src/text-input.cpp +13 -0
  50. package/native/core/src/web-view.cpp +1 -1
  51. package/native/scripts/build.sh +5 -1
  52. package/native/scripts/launch-probe.sh +60 -0
  53. package/package.json +2 -2
  54. package/prebuilt.json +6 -6
@@ -86,6 +86,7 @@ add_library(ngn_core STATIC
86
86
  core/src/desktop-view-props.cpp
87
87
  core/src/default-menu.cpp
88
88
  core/src/dev-server.cpp
89
+ core/src/dom-web-view.cpp
89
90
  core/src/focus-order.cpp
90
91
  core/src/gestures.cpp
91
92
  core/src/glass.cpp
@@ -210,7 +211,7 @@ if(NGN_TESTS)
210
211
  appkit/web-view.mm appkit/tests/web-view.test.mm
211
212
  appkit/expo-image-drawing.mm appkit/tests/expo-image.test.mm
212
213
  appkit/expo-ui-renderer.mm appkit/tests/expo-ui.test.mm
213
- appkit/camera.mm appkit/tests/camera.test.mm
214
+ appkit/camera.mm appkit/tests/camera.test.mm appkit/camera-sheet.mm appkit/system-access.mm appkit/tests/camera-sheet.test.mm
214
215
  appkit/maps.mm appkit/tests/maps.test.mm
215
216
  appkit/apple-authentication.mm appkit/tests/apple-authentication.test.mm
216
217
  appkit/video.mm appkit/tests/video.test.mm
@@ -218,7 +219,7 @@ if(NGN_TESTS)
218
219
  appkit/text-input-field.mm appkit/tests/text-input-field.test.mm
219
220
  appkit/search-field.mm appkit/tests/search-field.test.mm
220
221
  appkit/search-toolbar-item.mm appkit/tests/search-toolbar-item.test.mm
221
- appkit/cg-canvas.mm appkit/tests/svg-effects.test.mm
222
+ appkit/cg-canvas.mm appkit/tests/svg-effects.test.mm appkit/tests/svg-text.test.mm
222
223
  appkit/scrollers.mm appkit/tests/scrollers.test.mm
223
224
  appkit/bookmarks.mm appkit/tests/bookmarks.test.mm
224
225
  appkit/push.mm appkit/tests/push.test.mm
@@ -1,6 +1,9 @@
1
1
  # The AppKit backend and the ngn-macos binary. Everything links statically: the binary loads
2
2
  # nothing from outside itself but the system's frameworks.
3
- add_executable(ngn-macos
3
+ #
4
+ # Every source but main.mm, compiled once into objects the binary and the AppKit view tests
5
+ # (tests/views) both link whole.
6
+ add_library(ngn_appkit OBJECT
4
7
  animation-frames.mm
5
8
  app-info.mm
6
9
  app-windows.mm
@@ -25,6 +28,7 @@ add_executable(ngn-macos
25
28
  expo-ui-renderer.mm
26
29
  expo-ui-view.mm
27
30
  camera.mm
31
+ camera-sheet.mm
28
32
  camera-view.mm
29
33
  maps.mm
30
34
  map-view.mm
@@ -47,7 +51,6 @@ add_executable(ngn-macos
47
51
  symbol-view.mm
48
52
  red-box.mm
49
53
  share-picker.mm
50
- main.mm
51
54
  menu-bar.mm
52
55
  glass-effect.mm
53
56
  glass-view.mm
@@ -72,6 +75,7 @@ add_executable(ngn-macos
72
75
  store-review.mm
73
76
  system.mm
74
77
  system-access.mm
78
+ tab-walk.mm
75
79
  test-launch.mm
76
80
  text-input-field.mm
77
81
  text-layout.mm
@@ -80,10 +84,16 @@ add_executable(ngn-macos
80
84
  updates.mm
81
85
  web-browser.mm
82
86
  app-updater.mm)
83
- target_link_libraries(ngn-macos PRIVATE ngn_core_runtime ngn_libraries ${NGN_LINKED}
87
+ target_link_libraries(ngn_appkit PUBLIC ngn_core_runtime ngn_libraries)
88
+ target_compile_options(ngn_appkit PUBLIC -fobjc-arc -Wall -Wno-unused-parameter)
89
+ set(NGN_APPKIT_FRAMEWORKS
84
90
  "-framework AppKit" "-framework Foundation" "-framework CoreText" "-framework ImageIO" "-framework QuartzCore" "-framework Security" "-framework IOKit" "-framework UniformTypeIdentifiers" "-framework UserNotifications" "-framework LocalAuthentication" "-framework CoreLocation" "-framework CoreServices" "-framework SystemConfiguration" "-framework Network" "-framework AuthenticationServices" "-framework Vision" "-framework EventKit" "-framework StoreKit" "-framework AVFoundation" "-framework CoreMedia" "-framework Photos" "-framework PhotosUI" "-framework AppTrackingTransparency" "-framework AdSupport" "-framework MapKit" "-framework WebKit" "-framework PDFKit" "-framework CoreImage" "-framework AVKit")
91
+
92
+ add_executable(ngn-macos
93
+ main.mm)
94
+ # The autolinked code (cmake/linked-modules.cmake), which only a target linking it directly takes whole.
95
+ target_link_libraries(ngn-macos PRIVATE ngn_appkit ${NGN_LINKED} ${NGN_APPKIT_FRAMEWORKS})
85
96
  ngn_link_swift(ngn-macos)
86
- target_compile_options(ngn-macos PRIVATE -fobjc-arc -Wall -Wno-unused-parameter)
87
97
  # A Release build is what a packaged app ships: the linker drops the code nothing reaches.
88
98
  target_link_options(ngn-macos PRIVATE $<$<CONFIG:Release>:-Wl,-dead_strip>)
89
99
  # The React Native the host is built from, which PlatformConstants reports and React Native's
@@ -107,3 +117,14 @@ if(EXISTS ${_sparkle})
107
117
  COMMAND ditto ${_sparkle} $<TARGET_FILE_DIR:ngn-macos>/Sparkle.framework
108
118
  VERBATIM)
109
119
  endif()
120
+
121
+ # The AppKit view tests: views mounted from Fabric mutations into an offscreen window, through the
122
+ # backend the binary uses. Built with the core tests, and run beside them.
123
+ if(NGN_TESTS)
124
+ add_executable(ngn_appkit_tests ../core/tests/main.cpp tests/views/accessibility.test.mm tests/views/svg-foreign-object.test.mm
125
+ tests/views/tab-walk.test.mm tests/views/camera-view.test.mm tests/views/text-input-alignment.test.mm)
126
+ target_include_directories(ngn_appkit_tests PRIVATE ../core/tests)
127
+ target_link_libraries(ngn_appkit_tests PRIVATE ngn_appkit doctest ${NGN_APPKIT_FRAMEWORKS})
128
+ ngn_link_swift(ngn_appkit_tests)
129
+ add_test(NAME appkit COMMAND ngn_appkit_tests)
130
+ endif()
@@ -0,0 +1,59 @@
1
+ // The host's camera for NgNativeImagePicker's launchCameraAsync (the core's CameraSheet): macOS has
2
+ // no system camera screen, so a sheet on the key window shows the camera's preview, mirrored as
3
+ // Photo Booth and FaceTime show it, with Cancel (Escape) and the shutter (Return) below it. The
4
+ // photo or movie itself is not mirrored, as on iOS. The sheet is a picker's size (pickerSheet).
5
+ //
6
+ // In photo mode the shutter is Take Photo. In video mode it is Record, then Stop, with the time
7
+ // recorded beside it; the recording also stops at the request's limit. A request for both shows a
8
+ // Photo and Video switch, in photo mode first, as iOS's camera opens; it is fixed while recording.
9
+ #pragma once
10
+
11
+ #include "ngn/camera.h"
12
+
13
+ #include <memory>
14
+
15
+ namespace ngn::appkit {
16
+
17
+ std::shared_ptr<CameraSheet> makeCameraSheet();
18
+
19
+ } // namespace ngn::appkit
20
+
21
+ #ifdef __OBJC__
22
+ #import <AVFoundation/AVFoundation.h>
23
+ #import <AppKit/AppKit.h>
24
+
25
+ #import "camera.h"
26
+
27
+ namespace ngn::appkit {
28
+ // The window the sheet shows on: the key window, the main one, or, in an app that is not active
29
+ // (as just after an OS permission prompt), its front window that can hold a sheet; nil for none.
30
+ NSWindow *windowForSheet();
31
+ } // namespace ngn::appkit
32
+
33
+ // What the sheet answers: a photo written as the request asks, with its size and the camera's EXIF
34
+ // (nil when it has none); a movie, with its size and length; or neither, with an error or, when
35
+ // the user cancelled, none.
36
+ using CameraSheetDone = std::function<void(ngn::CameraTake take, NSDictionary *exif, const std::string &error)>;
37
+
38
+ @interface NGNCameraSheet : NSViewController
39
+ - (instancetype)initWithCapture:(std::shared_ptr<ngn::appkit::SessionCapture>)capture
40
+ request:(ngn::CameraSheetRequest)request
41
+ done:(CameraSheetDone)done;
42
+ @property(nonatomic, readonly) NSButton *shutter;
43
+ @property(nonatomic, readonly) NSButton *cancelButton;
44
+ @property(nonatomic, readonly) AVCaptureVideoPreviewLayer *preview;
45
+ // Photo (0) and Video (1), for a request that takes either; nil otherwise.
46
+ @property(nonatomic, readonly) NSSegmentedControl *modeSwitch;
47
+ // The time recorded, shown in video mode.
48
+ @property(nonatomic, readonly) NSTextField *elapsed;
49
+ @property(nonatomic, readonly) BOOL videoMode;
50
+ // The camera is running: the shutter can take a photo or start a recording.
51
+ - (void)cameraRunning;
52
+ - (void)takePhoto:(id)sender;
53
+ // Starts a recording, or stops the one running.
54
+ - (void)record:(id)sender;
55
+ // Switches to the mode modeSwitch shows.
56
+ - (void)switchMode:(id)sender;
57
+ - (void)cancel:(id)sender;
58
+ @end
59
+ #endif
@@ -0,0 +1,344 @@
1
+ #import "camera-sheet.h"
2
+
3
+ #import "photo-library.h"
4
+ #import "system-access.h"
5
+
6
+ #import <ImageIO/ImageIO.h>
7
+ #import <QuartzCore/QuartzCore.h>
8
+
9
+ #include <cmath>
10
+
11
+ namespace {
12
+
13
+ // The bar under the preview that holds the buttons, and the room around them.
14
+ constexpr CGFloat kBar = 52;
15
+ constexpr CGFloat kInset = 20;
16
+
17
+ // The time recorded as iOS's camera shows it, hours, minutes and seconds.
18
+ NSString *elapsedText(double seconds) {
19
+ auto whole = static_cast<long>(std::floor(std::max(0.0, seconds)));
20
+ return [NSString stringWithFormat:@"%02ld:%02ld:%02ld", whole / 3600, whole / 60 % 60, whole % 60];
21
+ }
22
+
23
+ } // namespace
24
+
25
+ @implementation NGNCameraSheet {
26
+ std::shared_ptr<ngn::appkit::SessionCapture> _capture;
27
+ ngn::CameraSheetRequest _request;
28
+ CameraSheetDone _done;
29
+ BOOL _running;
30
+ // A photo is being taken, a recording runs, or one is being stopped.
31
+ BOOL _taking;
32
+ BOOL _recording;
33
+ BOOL _stopping;
34
+ CFTimeInterval _recordingSince;
35
+ NSTimer *_clock;
36
+ }
37
+
38
+ - (instancetype)initWithCapture:(std::shared_ptr<ngn::appkit::SessionCapture>)capture
39
+ request:(ngn::CameraSheetRequest)request
40
+ done:(CameraSheetDone)done {
41
+ if ((self = [super initWithNibName:nil bundle:nil])) {
42
+ _capture = std::move(capture);
43
+ _request = std::move(request);
44
+ _done = std::move(done);
45
+ // Photo mode first, as iOS's camera opens, unless only videos are asked for.
46
+ _videoMode = _request.videos && !_request.photos;
47
+ }
48
+ return self;
49
+ }
50
+
51
+ - (void)loadView {
52
+ NSView *view = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 780, 560)];
53
+
54
+ _preview = [AVCaptureVideoPreviewLayer layerWithSession:_capture->session()];
55
+ _preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
56
+ _preview.backgroundColor = NSColor.blackColor.CGColor;
57
+ // Mirrored, as a camera facing the user is shown on a Mac; the photo and the movie are not.
58
+ AVCaptureConnection *connection = _preview.connection;
59
+ if (connection.isVideoMirroringSupported) {
60
+ connection.automaticallyAdjustsVideoMirroring = NO;
61
+ connection.videoMirrored = YES;
62
+ }
63
+ NSView *preview = [[NSView alloc] initWithFrame:NSMakeRect(0, kBar, 780, 560 - kBar)];
64
+ preview.layer = _preview;
65
+ preview.wantsLayer = YES;
66
+ // Filled, the picture overflows the preview on one side, and must not cover the buttons' bar.
67
+ // AppKit sets the layer's masksToBounds from this.
68
+ preview.clipsToBounds = YES;
69
+ preview.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
70
+ [preview setAccessibilityElement:YES];
71
+ [preview setAccessibilityRole:NSAccessibilityImageRole];
72
+ [preview setAccessibilityLabel:@"Camera preview"];
73
+ [view addSubview:preview];
74
+
75
+ // The shutter is the default button, rightmost, as a dialog's action is; Cancel is beside it.
76
+ // Both are as wide as the widest title either takes, so they stay put as the shutter's changes.
77
+ _shutter = [NSButton buttonWithTitle:@"" target:self action:nil];
78
+ _shutter.keyEquivalent = @"\r";
79
+ _shutter.enabled = NO;
80
+ _cancelButton = [NSButton buttonWithTitle:@"Cancel" target:self action:@selector(cancel:)];
81
+ _cancelButton.keyEquivalent = @"\x1b";
82
+ CGFloat width = 0;
83
+ for (NSString *title in @[ @"Take Photo", @"Record", @"Stop", @"Cancel" ]) {
84
+ _shutter.title = title;
85
+ [_shutter sizeToFit];
86
+ width = std::max(width, _shutter.frame.size.width);
87
+ }
88
+ [_cancelButton sizeToFit];
89
+ CGFloat y = (kBar - _shutter.frame.size.height) / 2;
90
+ _shutter.frame = NSMakeRect(780 - kInset - width, y, width, _shutter.frame.size.height);
91
+ _cancelButton.frame = NSMakeRect(NSMinX(_shutter.frame) - 12 - width, y, width, _cancelButton.frame.size.height);
92
+ for (NSButton *button in @[ _shutter, _cancelButton ]) {
93
+ button.autoresizingMask = NSViewMinXMargin | NSViewMaxYMargin;
94
+ [view addSubview:button];
95
+ }
96
+
97
+ _elapsed = [NSTextField labelWithString:elapsedText(0)];
98
+ _elapsed.font = [NSFont monospacedDigitSystemFontOfSize:NSFont.systemFontSize weight:NSFontWeightRegular];
99
+ [_elapsed sizeToFit];
100
+ _elapsed.frame = NSMakeRect((780 - _elapsed.frame.size.width) / 2, (kBar - _elapsed.frame.size.height) / 2, _elapsed.frame.size.width,
101
+ _elapsed.frame.size.height);
102
+ _elapsed.autoresizingMask = NSViewMinXMargin | NSViewMaxXMargin | NSViewMaxYMargin;
103
+ [_elapsed setAccessibilityLabel:@"Time recorded"];
104
+ [view addSubview:_elapsed];
105
+
106
+ if (_request.photos && _request.videos) {
107
+ _modeSwitch = [NSSegmentedControl segmentedControlWithLabels:@[ @"Photo", @"Video" ]
108
+ trackingMode:NSSegmentSwitchTrackingSelectOne
109
+ target:self
110
+ action:@selector(switchMode:)];
111
+ [_modeSwitch sizeToFit];
112
+ _modeSwitch.frame = NSMakeRect(kInset, (kBar - _modeSwitch.frame.size.height) / 2, _modeSwitch.frame.size.width, _modeSwitch.frame.size.height);
113
+ _modeSwitch.autoresizingMask = NSViewMaxXMargin | NSViewMaxYMargin;
114
+ [_modeSwitch setAccessibilityLabel:@"Camera mode"];
115
+ [view addSubview:_modeSwitch];
116
+ }
117
+ self.view = view;
118
+ [self refresh];
119
+ if (_videoMode) {
120
+ _capture->prepareRecording(_request.video);
121
+ }
122
+ }
123
+
124
+ // Puts the controls in step with the mode and what is under way.
125
+ - (void)refresh {
126
+ if (!self.viewLoaded) {
127
+ return;
128
+ }
129
+ BOOL answered = !_done;
130
+ if (_videoMode) {
131
+ _shutter.title = _recording ? @"Stop" : @"Record";
132
+ _shutter.action = @selector(record:);
133
+ } else {
134
+ _shutter.title = @"Take Photo";
135
+ _shutter.action = @selector(takePhoto:);
136
+ }
137
+ _shutter.enabled = _running && !answered && !_taking && !_stopping;
138
+ _elapsed.hidden = !_videoMode;
139
+ _modeSwitch.selectedSegment = _videoMode ? 1 : 0;
140
+ _modeSwitch.enabled = !answered && !_taking && !_recording && !_stopping;
141
+ }
142
+
143
+ - (void)cameraRunning {
144
+ _running = YES;
145
+ [self refresh];
146
+ }
147
+
148
+ - (void)switchMode:(id)sender {
149
+ // Fixed while recording or once answered: the switch goes back to the mode in use.
150
+ BOOL video = _modeSwitch.selectedSegment == 1;
151
+ if (_modeSwitch.enabled && video != _videoMode) {
152
+ _videoMode = video;
153
+ // The camera is set up for video as the mode changes, as iOS's is, so Record starts at once.
154
+ if (_videoMode) {
155
+ _capture->prepareRecording(_request.video);
156
+ } else {
157
+ _capture->unprepareRecording();
158
+ }
159
+ }
160
+ [self refresh];
161
+ }
162
+
163
+ - (void)takePhoto:(id)sender {
164
+ // Disabled until the camera runs, while a photo is taken, and once answered.
165
+ if (_videoMode || !_shutter.enabled) {
166
+ return;
167
+ }
168
+ _taking = YES;
169
+ [self refresh];
170
+ NGNCameraSheet *sheet = self;
171
+ ngn::CameraPictureRequest request = _request.photo;
172
+ _capture->capturePhoto([sheet, request](CGImageRef image, NSDictionary *metadata, const std::string &error) {
173
+ std::optional<ngn::CameraShot> shot;
174
+ std::string failure = error;
175
+ if (image != nullptr) {
176
+ if (ngn::appkit::writePicture(image, request)) {
177
+ shot = ngn::CameraShot{.width = static_cast<int>(CGImageGetWidth(image)), .height = static_cast<int>(CGImageGetHeight(image))};
178
+ } else {
179
+ failure = "the picture could not be saved";
180
+ }
181
+ }
182
+ NSDictionary *exif = metadata[(__bridge NSString *)kCGImagePropertyExifDictionary];
183
+ // Decided on the main queue, where a cancel is too: a photo answered after a cancel is not
184
+ // kept.
185
+ std::string path = request.path;
186
+ dispatch_async(dispatch_get_main_queue(), ^{
187
+ if (![sheet finish:{.photo = shot} exif:exif error:failure] && shot) {
188
+ [NSFileManager.defaultManager removeItemAtPath:@(path.c_str()) error:nil];
189
+ }
190
+ });
191
+ });
192
+ }
193
+
194
+ - (void)record:(id)sender {
195
+ if (!_videoMode || !_shutter.enabled) {
196
+ return;
197
+ }
198
+ if (_recording) {
199
+ _stopping = YES;
200
+ [self refresh];
201
+ _capture->stopRecording();
202
+ return;
203
+ }
204
+ _recording = YES;
205
+ _recordingSince = CACurrentMediaTime();
206
+ _elapsed.stringValue = elapsedText(0);
207
+ __weak NGNCameraSheet *weakSheet = self;
208
+ _clock = [NSTimer scheduledTimerWithTimeInterval:0.25
209
+ repeats:YES
210
+ block:^(NSTimer *timer) {
211
+ [weakSheet showElapsed];
212
+ }];
213
+ [self refresh];
214
+ NGNCameraSheet *sheet = self;
215
+ _capture->startRecording(_request.video, [sheet](const std::string &error) {
216
+ std::string failure = error;
217
+ dispatch_async(dispatch_get_main_queue(), ^{
218
+ [sheet recordingEnded:failure];
219
+ });
220
+ });
221
+ }
222
+
223
+ - (void)showElapsed {
224
+ _elapsed.stringValue = elapsedText(CACurrentMediaTime() - _recordingSince);
225
+ }
226
+
227
+ // The movie is finished: at Stop, at the request's limit, or as the sheet went another way.
228
+ - (void)recordingEnded:(std::string)error {
229
+ _recording = NO;
230
+ _stopping = NO;
231
+ [_clock invalidate];
232
+ _clock = nil;
233
+ NSString *path = @(_request.video.path.c_str());
234
+ if (!_done) {
235
+ // Cancelled while it recorded: nothing answers for the movie.
236
+ [NSFileManager.defaultManager removeItemAtPath:path error:nil];
237
+ return;
238
+ }
239
+ if (!error.empty()) {
240
+ // Whatever of it was written is no movie.
241
+ [NSFileManager.defaultManager removeItemAtPath:path error:nil];
242
+ [self finish:{} exif:nil error:error];
243
+ return;
244
+ }
245
+ auto clip = ngn::appkit::movieClip(_request.video.path);
246
+ if (!clip) {
247
+ [NSFileManager.defaultManager removeItemAtPath:path error:nil];
248
+ [self finish:{} exif:nil error:"the video could not be read"];
249
+ return;
250
+ }
251
+ [self finish:{.video = clip} exif:nil error:""];
252
+ }
253
+
254
+ - (void)cancel:(id)sender {
255
+ [self finish:{} exif:nil error:""];
256
+ }
257
+
258
+ // The window going another way (its parent closed, say) is a cancel.
259
+ - (void)viewDidDisappear {
260
+ [super viewDidDisappear];
261
+ [self finish:{} exif:nil error:""];
262
+ }
263
+
264
+ // Answers once, stops the camera (and with it a recording) and lets the sheet go; NO when it was
265
+ // answered already.
266
+ - (BOOL)finish:(ngn::CameraTake)take exif:(NSDictionary *)exif error:(std::string)error {
267
+ if (!_done) {
268
+ return NO;
269
+ }
270
+ auto done = std::move(_done);
271
+ _done = nullptr;
272
+ [self refresh];
273
+ _capture->stop();
274
+ NSWindow *window = self.viewLoaded ? self.view.window : nil;
275
+ if (window.sheetParent != nil) {
276
+ [window.sheetParent endSheet:window];
277
+ }
278
+ done(std::move(take), exif, error);
279
+ return YES;
280
+ }
281
+
282
+ @end
283
+
284
+ namespace ngn::appkit {
285
+
286
+ NSWindow *windowForSheet() {
287
+ if (NSApp.keyWindow != nil) {
288
+ return NSApp.keyWindow;
289
+ }
290
+ if (NSApp.mainWindow != nil) {
291
+ return NSApp.mainWindow;
292
+ }
293
+ // An app that is not active has neither, as just after the OS asked for the microphone in a
294
+ // window of its own: its front window that can hold a sheet.
295
+ for (NSWindow *window in NSApp.orderedWindows) {
296
+ if (window.visible && window.sheetParent == nil && window.attachedSheet == nil && window.canBecomeMainWindow) {
297
+ return window;
298
+ }
299
+ }
300
+ return nil;
301
+ }
302
+
303
+ namespace {
304
+
305
+ class SheetCamera final : public CameraSheet {
306
+ public:
307
+ void shoot(const CameraSheetRequest &request, std::function<void(CameraTake, const std::string &)> done) override {
308
+ auto answer = std::make_shared<std::function<void(CameraTake, const std::string &)>>(std::move(done));
309
+ CameraSheetRequest asked = request;
310
+ dispatch_async(dispatch_get_main_queue(), ^{
311
+ NSWindow *window = windowForSheet();
312
+ if (window == nil) {
313
+ (*answer)({}, "");
314
+ return;
315
+ }
316
+ auto controller = std::make_shared<NGNCameraSheet *>(nil);
317
+ std::string error;
318
+ auto capture = startCapture(cameraFor("front"), error, [controller] { [*controller cameraRunning]; });
319
+ if (!capture) {
320
+ (*answer)({}, error);
321
+ return;
322
+ }
323
+ NGNCameraSheet *sheet = [[NGNCameraSheet alloc] initWithCapture:capture
324
+ request:asked
325
+ done:[answer, controller](CameraTake take, NSDictionary *exif, const std::string &failure) {
326
+ *controller = nil;
327
+ if (take.photo) {
328
+ take.photo->exif = dynamicFrom(exif);
329
+ }
330
+ (*answer)(std::move(take), failure);
331
+ }];
332
+ *controller = sheet;
333
+ [window beginSheet:pickerSheet(sheet, window) completionHandler:nil];
334
+ });
335
+ }
336
+ };
337
+
338
+ } // namespace
339
+
340
+ std::shared_ptr<CameraSheet> makeCameraSheet() {
341
+ return std::make_shared<SheetCamera>();
342
+ }
343
+
344
+ } // namespace ngn::appkit
@@ -51,9 +51,13 @@ bool gNoPrompts = false;
51
51
  _preview.frame = self.bounds;
52
52
  }
53
53
 
54
- // Starts or stops the camera as `active` and the permission say.
54
+ // Starts or stops the camera as `active` and the permission say, and passes `mute` on for the
55
+ // recordings NgNativeCamera starts. Set first, so the camera is never attached without it.
55
56
  - (void)follow {
56
57
  auto props = std::dynamic_pointer_cast<const ngn::CameraProps>(self.props);
58
+ if (props && gViews) {
59
+ gViews->mute(static_cast<int>(self.reactTag), props->mute);
60
+ }
57
61
  if (!props || !gHardware || !self.eventEmitter) {
58
62
  return;
59
63
  }
@@ -9,12 +9,20 @@
9
9
 
10
10
  #include <functional>
11
11
  #include <memory>
12
+ #include <optional>
12
13
  #include <string>
13
14
 
14
15
  namespace ngn::appkit {
15
16
 
16
17
  std::shared_ptr<CameraHardware> makeCameraHardware();
17
18
 
19
+ // Writes `image` to the request's path as its format asks: a JPEG at its quality, or a PNG.
20
+ bool writePicture(CGImageRef image, const CameraPictureRequest &request);
21
+
22
+ // A movie's size as it plays and its length in milliseconds, read as expo-image-picker reads a
23
+ // video's on iOS; nothing for a file with no video.
24
+ std::optional<CameraClip> movieClip(const std::string &path);
25
+
18
26
  // The camera a view facing `facing` uses: the built-in one, else the first there is; nil on a Mac
19
27
  // with none.
20
28
  AVCaptureDevice *cameraFor(const std::string &facing);
@@ -22,13 +30,27 @@ AVCaptureDevice *cameraFor(const std::string &facing);
22
30
  // A capture session with a photo output. Starting and stopping run on a queue of the capture's
23
31
  // own, since AVCaptureSession's startRunning and stopRunning block while the camera comes up or
24
32
  // goes down, which can take a second (a Continuity Camera longer).
33
+ // A still's image (owned by the capture, valid during the call) and the camera's metadata, or
34
+ // a null image and why. On whatever thread the photo output answers on.
35
+ using PhotoDone = std::function<void(CGImageRef image, NSDictionary *metadata, const std::string &error)>;
36
+
25
37
  class SessionCapture : public CameraCapture {
26
38
  public:
27
39
  virtual AVCaptureSession *session() const = 0;
40
+ // Takes a still from the running session.
41
+ virtual void capturePhoto(PhotoDone done) = 0;
42
+ // A still written as `request` asks (writePicture).
43
+ void takePicture(const CameraPictureRequest &request, std::function<void(int, int, const std::string &)> done) final;
28
44
  // Starts the session, then calls `running` on the main thread.
29
45
  virtual void start(std::function<void()> running) = 0;
30
46
  // Stops it, without waiting. Going away stops it too.
31
47
  virtual void stop() = 0;
48
+ // Sets the session up for startRecording(request) ahead of it (its preset, the movie output and,
49
+ // with sound, the microphone), since the camera takes a moment to reconfigure. Nothing while a
50
+ // recording runs; a recording takes the setup down when it ends.
51
+ virtual void prepareRecording(const CameraRecordingRequest &request) = 0;
52
+ // Takes a prepared setup down, putting the session's preset back. Nothing while a recording runs.
53
+ virtual void unprepareRecording() = 0;
32
54
  };
33
55
  // A capture session on `device`, started (`running` is called on the main thread once it is), or
34
56
  // null with `error` set: never without the camera allowed, so it never prompts.