@blotch/angular-lynx-native 22.3.0 → 22.3.2

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 (100) hide show
  1. package/README.md +41 -15
  2. package/dist/android/project.d.ts +90 -1
  3. package/dist/android/project.d.ts.map +1 -1
  4. package/dist/android/project.js +369 -5
  5. package/dist/eject.d.ts +15 -1
  6. package/dist/eject.d.ts.map +1 -1
  7. package/dist/eject.js +13 -2
  8. package/dist/features.d.ts +70 -0
  9. package/dist/features.d.ts.map +1 -0
  10. package/dist/features.js +115 -0
  11. package/dist/generate.d.ts +17 -1
  12. package/dist/generate.d.ts.map +1 -1
  13. package/dist/generate.js +12 -4
  14. package/dist/index.d.ts +1 -0
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +8 -1
  17. package/dist/ios/build.d.ts.map +1 -1
  18. package/dist/ios/build.js +8 -2
  19. package/dist/ios/frameworks.d.ts +6 -5
  20. package/dist/ios/frameworks.d.ts.map +1 -1
  21. package/dist/ios/frameworks.js +46 -15
  22. package/dist/ios/project.d.ts +173 -11
  23. package/dist/ios/project.d.ts.map +1 -1
  24. package/dist/ios/project.js +767 -33
  25. package/dist/options.d.ts +143 -2
  26. package/dist/options.d.ts.map +1 -1
  27. package/dist/options.js +127 -0
  28. package/dist/permissions.d.ts +18 -1
  29. package/dist/permissions.d.ts.map +1 -1
  30. package/dist/permissions.js +63 -3
  31. package/package.json +1 -1
  32. package/templates/android/README.md +37 -2
  33. package/templates/android/angular-lynx.properties +2 -0
  34. package/templates/android/app/build.gradle.kts +70 -1
  35. package/templates/android/app/src/main/AndroidManifest.xml +24 -3
  36. package/templates/android/app/src/main/java/dev/angularlynx/shell/BarcodeScannerModule.kt +144 -0
  37. package/templates/android/app/src/main/java/dev/angularlynx/shell/BiometricsModule.kt +433 -0
  38. package/templates/android/app/src/main/java/dev/angularlynx/shell/BitmapPresenter.kt +98 -0
  39. package/templates/android/app/src/main/java/dev/angularlynx/shell/BitmapRendererContext.kt +175 -0
  40. package/templates/android/app/src/main/java/dev/angularlynx/shell/BluetoothModule.kt +1288 -0
  41. package/templates/android/app/src/main/java/dev/angularlynx/shell/CalendarModule.kt +127 -0
  42. package/templates/android/app/src/main/java/dev/angularlynx/shell/Canvas2DContext.kt +1557 -0
  43. package/templates/android/app/src/main/java/dev/angularlynx/shell/Canvas2DOps.kt +63 -0
  44. package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasFilter.kt +357 -0
  45. package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasUI.kt +4 -3
  46. package/templates/android/app/src/main/java/dev/angularlynx/shell/FilePickerModule.kt +263 -0
  47. package/templates/android/app/src/main/java/dev/angularlynx/shell/HapticsModule.kt +82 -0
  48. package/templates/android/app/src/main/java/dev/angularlynx/shell/MainActivity.kt +376 -6
  49. package/templates/android/app/src/main/java/dev/angularlynx/shell/ModuleBytes.kt +58 -0
  50. package/templates/android/app/src/main/java/dev/angularlynx/shell/MotionModule.kt +332 -0
  51. package/templates/android/app/src/main/java/dev/angularlynx/shell/NfcModule.kt +274 -0
  52. package/templates/android/app/src/main/java/dev/angularlynx/shell/PermissionRequests.kt +25 -0
  53. package/templates/android/app/src/main/java/dev/angularlynx/shell/PhotoLibraryModule.kt +168 -0
  54. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShareTargetModule.kt +234 -0
  55. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellApplication.kt +6 -0
  56. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellModule.kt +47 -3
  57. package/templates/android/app/src/main/java/dev/angularlynx/shell/StoreReviewModule.kt +66 -0
  58. package/templates/android/app/src/main/java/dev/angularlynx/shell/SystemFontFamilies.kt +68 -0
  59. package/templates/android/app/src/main/java/dev/angularlynx/shell/TrackingModule.kt +74 -0
  60. package/templates/android/app/src/main/java/dev/angularlynx/shell/VideoFrameTarget.kt +429 -0
  61. package/templates/android/app/src/main/java/dev/angularlynx/shell/VideoUI.kt +676 -0
  62. package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLContext.kt +128 -13
  63. package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLModule.kt +493 -65
  64. package/templates/android/app/src/main/java/dev/angularlynx/shell/WifiModule.kt +429 -0
  65. package/templates/android/app/src/main/res/values/biometrics.xml +9 -0
  66. package/templates/android/app/src/main/res/values/share_target.xml +11 -0
  67. package/templates/android/app/src/main/res/xml/backup_rules.xml +5 -3
  68. package/templates/android/app/src/main/res/xml/data_extraction_rules.xml +6 -3
  69. package/templates/android/build.gradle.kts +1 -1
  70. package/templates/android/gradle/wrapper/gradle-wrapper.properties +2 -2
  71. package/templates/ios/App/AngularLynxBitmapRendererContext.m +188 -0
  72. package/templates/ios/App/AngularLynxCanvas2DContext.m +2467 -0
  73. package/templates/ios/App/AngularLynxCanvas2DOps.h +61 -0
  74. package/templates/ios/App/AngularLynxVideo.h +37 -0
  75. package/templates/ios/App/AngularLynxVideo.m +435 -0
  76. package/templates/ios/App/AngularLynxWebGL.h +69 -7
  77. package/templates/ios/App/AngularLynxWebGL.m +538 -29
  78. package/templates/ios/App/AngularLynxWebGLContext.m +148 -46
  79. package/templates/ios/App/AppDelegate.swift +34 -2
  80. package/templates/ios/App/AppleSignInButton.swift +106 -0
  81. package/templates/ios/App/AppleSignInModule.swift +222 -0
  82. package/templates/ios/App/BarcodeScannerModule.swift +284 -0
  83. package/templates/ios/App/BiometricsModule.swift +256 -0
  84. package/templates/ios/App/BluetoothModule.swift +1086 -0
  85. package/templates/ios/App/CalendarModule.swift +158 -0
  86. package/templates/ios/App/FilePickerModule.swift +240 -0
  87. package/templates/ios/App/HapticsModule.swift +90 -0
  88. package/templates/ios/App/Info.plist +2 -0
  89. package/templates/ios/App/LynxViewController.swift +210 -6
  90. package/templates/ios/App/MotionModule.swift +430 -0
  91. package/templates/ios/App/NfcModule.swift +346 -0
  92. package/templates/ios/App/PhotoLibraryModule.swift +169 -0
  93. package/templates/ios/App/ShareTargetModule.swift +150 -0
  94. package/templates/ios/App/ShellModule.swift +17 -0
  95. package/templates/ios/App/StoreReviewModule.swift +61 -0
  96. package/templates/ios/App/TrackingModule.swift +121 -0
  97. package/templates/ios/App/WifiModule.swift +237 -0
  98. package/templates/ios/App/XElementRegistry.swift +13 -3
  99. package/templates/ios/ShareExtension/Info.plist +42 -0
  100. package/templates/ios/ShareExtension/ShareViewController.swift +387 -0
@@ -16,6 +16,8 @@
16
16
  #pragma clang diagnostic ignored "-Wdeprecated-declarations"
17
17
 
18
18
  NSData *AngularLynxBytesFromWords(NSArray *words);
19
+ NSDictionary *AngularLynxEncodeImage(NSData *pixels, size_t width, size_t height,
20
+ CGBitmapInfo alphaInfo, NSString *type, double quality);
19
21
 
20
22
  // WebGL's names for the default framebuffer's buffers, which GLES gives to
21
23
  // window surfaces only. The default framebuffer here is an ordinary one.
@@ -171,6 +173,7 @@ static NSUInteger componentsOf(GLenum format) {
171
173
  BOOL _depth;
172
174
  BOOL _stencil;
173
175
  BOOL _preserveDrawingBuffer;
176
+ BOOL _premultipliedAlpha;
174
177
  GLint _samples;
175
178
 
176
179
  // The default framebuffer, multisampled when antialias is on, and the
@@ -258,6 +261,7 @@ static NSUInteger componentsOf(GLenum format) {
258
261
  _depth = ![attributes[@"depth"] isEqual:@NO];
259
262
  _stencil = [attributes[@"stencil"] isEqual:@YES];
260
263
  _preserveDrawingBuffer = [attributes[@"preserveDrawingBuffer"] isEqual:@YES];
264
+ _premultipliedAlpha = ![attributes[@"premultipliedAlpha"] isEqual:@NO];
261
265
  _syncs = [NSMutableDictionary dictionary];
262
266
  _heldErrors = [NSMutableOrderedSet orderedSet];
263
267
  _namesCapacity = 256;
@@ -404,6 +408,13 @@ static NSUInteger componentsOf(GLenum format) {
404
408
  GLboolean discard = glIsEnabled(GL_RASTERIZER_DISCARD);
405
409
 
406
410
  glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _framebuffer);
411
+ // The app may have drawn to no color buffer (drawBuffers([NONE]) on the
412
+ // default framebuffer), which glClear would then skip: the color
413
+ // attachment is selected for the clear, and the app's choice put back.
414
+ GLint drawBuffer = GL_COLOR_ATTACHMENT0;
415
+ glGetIntegerv(GL_DRAW_BUFFER0, &drawBuffer);
416
+ GLenum colorAttachment = GL_COLOR_ATTACHMENT0;
417
+ glDrawBuffers(1, &colorAttachment);
407
418
  glDisable(GL_SCISSOR_TEST);
408
419
  glDisable(GL_RASTERIZER_DISCARD);
409
420
  glClearColor(0, 0, 0, 0);
@@ -414,6 +425,8 @@ static NSUInteger componentsOf(GLenum format) {
414
425
  glStencilMask(0xffffffff);
415
426
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
416
427
 
428
+ GLenum appDrawBuffer = (GLenum)drawBuffer;
429
+ glDrawBuffers(1, &appDrawBuffer);
417
430
  glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
418
431
  glClearDepthf(clearDepth);
419
432
  glClearStencil(clearStencil);
@@ -541,26 +554,16 @@ static NSUInteger componentsOf(GLenum format) {
541
554
 
542
555
  #pragma mark - Image files
543
556
 
544
- /// The drawing buffer as an image file, for the runtime's canvas.toBlob() and
545
- /// toDataURL(): `{ base64, type }`, or `{ error }`.
546
- ///
547
- /// It reads what the drawing buffer holds now, as a browser does: what the
548
- /// app has drawn since the last frame was shown, or, once a frame has been
549
- /// shown without preserveDrawingBuffer, the cleared buffer. The pixels are
550
- /// encoded here rather than sent to the runtime, which would have to carry
551
- /// 11 MB of RGBA for a 2048 by 1365 photo across the bridge as numbers, and
552
- /// encode it in JavaScript on PrimJS, which has no JIT. The file crosses as
553
- /// base64: the bridge can't return bytes (see webgl-bridge.ts).
554
- - (NSDictionary *)encodeImageOfType:(NSString *)type
555
- quality:(double)quality
556
- premultiplied:(BOOL)premultiplied {
557
- if (_context == EGL_NO_CONTEXT) return @{@"error" : @"The WebGL context is gone"};
557
+ /// The drawing buffer's pixels, RGBA with rows top to bottom, read whatever
558
+ /// the app has bound and set, or nil when they couldn't be read.
559
+ - (nullable NSMutableData *)readDrawingBuffer {
560
+ if (_context == EGL_NO_CONTEXT) return nil;
558
561
  [self makeCurrent];
559
562
  size_t width = (size_t)_width;
560
563
  size_t height = (size_t)_height;
561
564
  size_t stride = width * 4;
562
565
  NSMutableData *pixels = [NSMutableData dataWithLength:stride * height];
563
- if (!pixels) return @{@"error" : @"Could not allocate the canvas's pixels"};
566
+ if (!pixels) return nil;
564
567
 
565
568
  // A plain read of the whole default framebuffer, whatever the app has
566
569
  // bound and set: its pixel pack buffer and pack parameters would move or
@@ -575,6 +578,12 @@ static NSUInteger componentsOf(GLenum format) {
575
578
  glPixelStorei(GL_PACK_ROW_LENGTH, 0);
576
579
  glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
577
580
  glPixelStorei(GL_PACK_SKIP_ROWS, 0);
581
+ // Its color is read whatever the app selected to read (NONE, say), and
582
+ // the selection is put back after.
583
+ glBindFramebuffer(GL_READ_FRAMEBUFFER, _framebuffer);
584
+ GLint readBuffer = GL_COLOR_ATTACHMENT0;
585
+ glGetIntegerv(GL_READ_BUFFER, &readBuffer);
586
+ glReadBuffer(GL_COLOR_ATTACHMENT0);
578
587
  GLuint source = _framebuffer;
579
588
  if (_samples > 0) {
580
589
  // GLES can't read a multisampled framebuffer: its samples are resolved
@@ -614,13 +623,58 @@ static NSUInteger componentsOf(GLenum format) {
614
623
  glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
615
624
  }
616
625
  glDeleteBuffers(1, &pack);
626
+ glBindFramebuffer(GL_READ_FRAMEBUFFER, _framebuffer);
627
+ glReadBuffer((GLenum)readBuffer);
617
628
  glBindBuffer(GL_PIXEL_PACK_BUFFER, (GLuint)packBuffer);
618
629
  glPixelStorei(GL_PACK_ALIGNMENT, alignment);
619
630
  glPixelStorei(GL_PACK_ROW_LENGTH, rowLength);
620
631
  glPixelStorei(GL_PACK_SKIP_PIXELS, skipPixels);
621
632
  glPixelStorei(GL_PACK_SKIP_ROWS, skipRows);
622
633
  [self restoreBindings];
623
- if (!read) return @{@"error" : @"Could not read the canvas's pixels"};
634
+ return read ? pixels : nil;
635
+ }
636
+
637
+ /// The canvas's pixels now, for another context to read: drawn on a 2D
638
+ /// canvas or uploaded as a texture. It makes this context current; the
639
+ /// caller makes its own current again.
640
+ - (AngularLynxDecodedImage *)snapshot {
641
+ NSMutableData *pixels = [self readDrawingBuffer];
642
+ if (!pixels) return nil;
643
+ return [[AngularLynxDecodedImage alloc] initWithPixels:pixels
644
+ width:_width
645
+ height:_height
646
+ premultiplied:_premultipliedAlpha || !_alpha];
647
+ }
648
+
649
+ /// After transferToImageBitmap() took the drawing buffer, it's cleared, even
650
+ /// with preserveDrawingBuffer: the HTML spec gives the canvas a new bitmap,
651
+ /// and says preserveDrawingBuffer has no effect on a transfer.
652
+ - (void)clearForTransfer {
653
+ if (_context == EGL_NO_CONTEXT) return;
654
+ [self makeCurrent];
655
+ [self clearDrawingBuffer];
656
+ }
657
+
658
+ /// The drawing buffer as an image file, for the runtime's canvas.toBlob() and
659
+ /// toDataURL(): `{ base64, type }`, or `{ error }`.
660
+ ///
661
+ /// It reads what the drawing buffer holds now, as a browser does: what the
662
+ /// app has drawn since the last frame was shown, or, once a frame has been
663
+ /// shown without preserveDrawingBuffer, the cleared buffer. The pixels are
664
+ /// encoded here rather than sent to the runtime, which would have to carry
665
+ /// 11 MB of RGBA for a 2048 by 1365 photo across the bridge as numbers, and
666
+ /// encode it in JavaScript on PrimJS, which has no JIT. The file crosses as
667
+ /// base64: the bridge can't return bytes (see webgl-bridge.ts).
668
+ - (NSDictionary *)encodeImageOfType:(NSString *)type
669
+ quality:(double)quality
670
+ premultiplied:(BOOL)premultiplied {
671
+ if (_context == EGL_NO_CONTEXT) return @{@"error" : @"The WebGL context is gone"};
672
+ NSMutableData *pixels = [self readDrawingBuffer];
673
+ if (!pixels) return @{@"error" : @"Could not read the canvas's pixels"};
674
+ size_t width = (size_t)_width;
675
+ size_t height = (size_t)_height;
676
+ size_t stride = width * 4;
677
+ uint8_t *bytes = pixels.mutableBytes;
624
678
 
625
679
  BOOL jpeg = [type isEqualToString:@"image/jpeg"];
626
680
  CGBitmapInfo alphaInfo;
@@ -646,31 +700,7 @@ static NSUInteger componentsOf(GLenum format) {
646
700
  // colors from either.
647
701
  alphaInfo = (CGBitmapInfo)(premultiplied ? kCGImageAlphaPremultipliedLast : kCGImageAlphaLast);
648
702
  }
649
- CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)pixels);
650
- CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
651
- CGImageRef image =
652
- CGImageCreate(width, height, 8, 32, stride, colorSpace,
653
- alphaInfo | kCGBitmapByteOrder32Big, provider, NULL, false,
654
- kCGRenderingIntentDefault);
655
- CGColorSpaceRelease(colorSpace);
656
- CGDataProviderRelease(provider);
657
- if (!image) return @{@"error" : @"Could not make an image of the canvas's pixels"};
658
- NSMutableData *file = [NSMutableData data];
659
- NSString *fileType = jpeg ? @"public.jpeg" : @"public.png";
660
- CGImageDestinationRef destination = CGImageDestinationCreateWithData(
661
- (__bridge CFMutableDataRef)file, (__bridge CFStringRef)fileType, 1, NULL);
662
- BOOL written = NO;
663
- if (destination) {
664
- NSDictionary *properties =
665
- jpeg ? @{(__bridge NSString *)kCGImageDestinationLossyCompressionQuality : @(quality)}
666
- : @{};
667
- CGImageDestinationAddImage(destination, image, (__bridge CFDictionaryRef)properties);
668
- written = CGImageDestinationFinalize(destination);
669
- CFRelease(destination);
670
- }
671
- CGImageRelease(image);
672
- if (!written) return @{@"error" : @"Could not encode the canvas's pixels"};
673
- return @{@"base64" : [file base64EncodedStringWithOptions:0], @"type" : type};
703
+ return AngularLynxEncodeImage(pixels, width, height, alphaInfo, type, quality);
674
704
  }
675
705
 
676
706
  #pragma mark - Errors
@@ -762,25 +792,92 @@ static NSUInteger componentsOf(GLenum format) {
762
792
  return (*holder).bytes;
763
793
  }
764
794
  if (![source isKindOfClass:NSDictionary.class]) return NULL;
765
- AngularLynxDecodedImage *image =
766
- [AngularLynxDecodedImage imageWithId:[source[@"image"] integerValue]];
795
+ AngularLynxDecodedImage *image;
796
+ BOOL blank = NO;
797
+ NSString *canvasId = source[@"canvas"];
798
+ if (![canvasId isKindOfClass:NSString.class]) canvasId = nil;
799
+ if (canvasId) {
800
+ // A canvas's pixels, which its context reads now: a 2D canvas's bitmap,
801
+ // or a WebGL canvas's drawing buffer, which makes that context current.
802
+ image = [[AngularLynxCanvasRegistry contextForCanvasId:canvasId] snapshot];
803
+ [self makeCurrent];
804
+ // A canvas with no pixels to read, one the app never drew on, is
805
+ // transparent black, as in a browser.
806
+ if (!image) {
807
+ image = [[AngularLynxDecodedImage alloc]
808
+ initWithPixels:[NSMutableData dataWithLength:(NSUInteger)width * height * 4]
809
+ width:width
810
+ height:height
811
+ premultiplied:YES];
812
+ blank = YES;
813
+ }
814
+ } else {
815
+ image = [AngularLynxDecodedImage imageWithId:[source[@"image"] integerValue]];
816
+ }
817
+ // The part of the image the unpack skips and the size name (a blank one is
818
+ // already that size).
819
+ if (!blank) {
820
+ image = [self cropped:image width:width height:height flipY:[source[@"flipY"] boolValue]];
821
+ }
767
822
  if (!image) return NULL;
768
823
  *isImage = YES;
769
824
  *holder = [self convertImage:image
770
825
  format:format
771
826
  type:type
772
827
  flipY:[source[@"flipY"] boolValue]
773
- premultiply:[source[@"premultiply"] boolValue]];
828
+ premultiply:[source[@"premultiply"] boolValue]
829
+ fromCanvas:canvasId != nil];
774
830
  return (*holder).bytes;
775
831
  }
776
832
 
833
+ /// The part of an image an upload of `width` by `height` takes: from
834
+ /// UNPACK_SKIP_PIXELS and UNPACK_SKIP_ROWS, counted in the image as
835
+ /// UNPACK_FLIP_Y_WEBGL leaves it, as WebGL2 reads a sub-rectangle of an
836
+ /// image source. The whole image for a plain upload; nil for a rectangle
837
+ /// outside it.
838
+ - (AngularLynxDecodedImage *)cropped:(AngularLynxDecodedImage *)image
839
+ width:(GLsizei)width
840
+ height:(GLsizei)height
841
+ flipY:(BOOL)flipY {
842
+ if (!image) return nil;
843
+ GLint skipPixels = 0, skipRows = 0;
844
+ glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &skipPixels);
845
+ glGetIntegerv(GL_UNPACK_SKIP_ROWS, &skipRows);
846
+ if (skipPixels == 0 && skipRows == 0 && width == image.width && height == image.height) {
847
+ return image;
848
+ }
849
+ // In NSInteger, 64 bits, so a skip near INT_MAX can't wrap round past the
850
+ // check.
851
+ if (width <= 0 || height <= 0 || skipPixels < 0 || skipRows < 0 ||
852
+ (NSInteger)skipPixels + width > image.width || (NSInteger)skipRows + height > image.height) {
853
+ return nil;
854
+ }
855
+ // Rows counted from the top of the flipped image are counted from the
856
+ // bottom of the image as it is, which convertImage then flips.
857
+ NSInteger y = flipY ? image.height - skipRows - height : skipRows;
858
+ NSMutableData *out = [NSMutableData dataWithLength:(NSUInteger)width * height * 4];
859
+ const uint8_t *from = image.pixels.bytes;
860
+ uint8_t *to = out.mutableBytes;
861
+ for (GLsizei r = 0; r < height; r++) {
862
+ memcpy(to + (NSUInteger)r * width * 4, from + ((y + r) * image.width + skipPixels) * 4,
863
+ (NSUInteger)width * 4);
864
+ }
865
+ return [[AngularLynxDecodedImage alloc] initWithPixels:out
866
+ width:width
867
+ height:height
868
+ premultiplied:image.premultiplied];
869
+ }
870
+
777
871
  /// A decoded image's RGBA bytes as the format and type an upload names,
778
- /// tightly packed, flipped and premultiplied as asked.
872
+ /// tightly packed, flipped and premultiplied as asked. A canvas's pixels,
873
+ /// premultiplied, are divided back when the upload doesn't premultiply, as
874
+ /// WebGL uploads a canvas; an ImageBitmap's are uploaded as it was decoded.
779
875
  - (NSData *)convertImage:(AngularLynxDecodedImage *)image
780
876
  format:(GLenum)format
781
877
  type:(GLenum)type
782
878
  flipY:(BOOL)flipY
783
- premultiply:(BOOL)premultiply {
879
+ premultiply:(BOOL)premultiply
880
+ fromCanvas:(BOOL)fromCanvas {
784
881
  NSInteger width = image.width;
785
882
  NSInteger height = image.height;
786
883
  const uint8_t *rgba = image.pixels.bytes;
@@ -792,6 +889,7 @@ static NSUInteger componentsOf(GLenum format) {
792
889
  NSMutableData *out = [NSMutableData dataWithLength:width * height * pixelSize];
793
890
  uint8_t *bytes = out.mutableBytes;
794
891
  BOOL multiply = premultiply && !image.premultiplied;
892
+ BOOL divide = fromCanvas && !premultiply && image.premultiplied;
795
893
  for (NSInteger y = 0; y < height; y++) {
796
894
  const uint8_t *row = rgba + (flipY ? height - 1 - y : y) * width * 4;
797
895
  uint8_t *outRow = bytes + y * width * pixelSize;
@@ -801,6 +899,10 @@ static NSUInteger componentsOf(GLenum format) {
801
899
  r = r * a / 255;
802
900
  g = g * a / 255;
803
901
  b = b * a / 255;
902
+ } else if (divide && a > 0 && a < 255) {
903
+ r = MIN(255, r * 255 / a);
904
+ g = MIN(255, g * 255 / a);
905
+ b = MIN(255, b * 255 / a);
804
906
  }
805
907
  float channels[4];
806
908
  switch (format) {
@@ -71,8 +71,10 @@ final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
71
71
  // scene(_:openURLContexts:) or scene(_:continue:) too, and before the
72
72
  // LynxView exists, so it goes out with the first global props
73
73
  // (LinkCenter).
74
+ // The Share Extension's link isn't one: the share waits in the App Group,
75
+ // and the runtime's LynxShareTarget takes it as it starts.
74
76
  LinkCenter.shared.initialURL =
75
- (connectionOptions.urlContexts.first?.url
77
+ (connectionOptions.urlContexts.lazy.map(\.url).first { !Self.isShareLink($0) }
76
78
  ?? connectionOptions.userActivities.lazy.compactMap(Self.universalLink).first)?
77
79
  .absoluteString
78
80
  let window = UIWindow(windowScene: windowScene)
@@ -86,10 +88,40 @@ final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
86
88
  // navigates to it.
87
89
  func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
88
90
  for context in URLContexts {
89
- LinkCenter.shared.open(context.url)
91
+ if Self.isShareLink(context.url) {
92
+ Self.shareArrived()
93
+ } else {
94
+ LinkCenter.shared.open(context.url)
95
+ }
90
96
  }
91
97
  }
92
98
 
99
+ // The Share Extension may not have managed to open the app, which then
100
+ // comes to the front later, by hand: whatever was shared meanwhile is
101
+ // waiting.
102
+ func sceneWillEnterForeground(_ scene: UIScene) {
103
+ if Self.shareScheme != nil {
104
+ Self.shareArrived()
105
+ }
106
+ }
107
+
108
+ /// The scheme the app's Share Extension opens it with once it has left a
109
+ /// share in the App Group (Info.plist's `AngularLynxShareScheme`, which
110
+ /// only an app with angular.json's `shareTarget` has).
111
+ private static let shareScheme =
112
+ (Bundle.main.object(forInfoDictionaryKey: "AngularLynxShareScheme") as? String)?.lowercased()
113
+
114
+ private static func isShareLink(_ url: URL) -> Bool {
115
+ shareScheme != nil && url.scheme?.lowercased() == shareScheme
116
+ }
117
+
118
+ /// Tells the runtime's LynxShareTarget a share is waiting: it takes it
119
+ /// from ShareTargetModule. Before the LynxView exists there's nobody to
120
+ /// tell, and the runtime takes the waiting shares as it starts anyway.
121
+ private static func shareArrived() {
122
+ LinkCenter.shared.lynxView?.sendGlobalEvent("angularLynxShare", withParams: [])
123
+ }
124
+
93
125
  // A Universal Link while the app is open: the same as a link with one of
94
126
  // its schemes.
95
127
  func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
@@ -0,0 +1,106 @@
1
+ import AuthenticationServices
2
+ import Lynx
3
+ import UIKit
4
+
5
+ /// `<apple-sign-in-button>`: iOS's own Sign in with Apple button
6
+ /// (`ASAuthorizationAppleIDButton`), which draws the logo, the title in the
7
+ /// system's language, the colors and the corners as Apple's guidelines ask,
8
+ /// so App Review finds it as Apple drew it. The app taps it to call
9
+ /// `LynxAppleSignIn.signIn()`: Lynx's `bindtap` reaches the element as on any
10
+ /// other, and the button shows its own highlight while it's pressed.
11
+ ///
12
+ /// - `type`: `signIn` ("Sign in with Apple"), `continue` or `signUp`.
13
+ /// - `button-style`: `black`, `white` or `whiteOutline`.
14
+ /// - `corner-radius`: in points.
15
+ ///
16
+ /// UIKit sets a button's type and style only when it makes it, so a change
17
+ /// to either makes a new one, at the end of the update (propsDidUpdate).
18
+ ///
19
+ /// The generator keeps this file only in an app whose angular.json sets
20
+ /// `signInWithApple`, with AppleSignInModule, whose `button` method tells
21
+ /// the runtime the element is here. XElementRegistry registers it by its
22
+ /// Objective-C name only when it is, hence the `@objc` name. Elsewhere the
23
+ /// runtime draws the same button itself.
24
+ @objc(AngularLynxAppleSignInButton)
25
+ final class AppleSignInButton: LynxUI<UIView> {
26
+ private var type: ASAuthorizationAppleIDButton.ButtonType = .signIn
27
+ private var style: ASAuthorizationAppleIDButton.Style = .black
28
+ private var cornerRadius: CGFloat?
29
+ /// The button needs making again: a new element, or a new type or style.
30
+ private var stale = true
31
+ private var button: ASAuthorizationAppleIDButton?
32
+
33
+ /// The attributes, as Lynx finds a Swift element's: an Objective-C one
34
+ /// declares them with LYNX_PROP_SETTER.
35
+ @objc static func propSetterLookUp() -> [[String]] {
36
+ [
37
+ ["type", NSStringFromSelector(#selector(setType(_:requestReset:)))],
38
+ ["button-style", NSStringFromSelector(#selector(setButtonStyle(_:requestReset:)))],
39
+ ["corner-radius", NSStringFromSelector(#selector(setCornerRadius(_:requestReset:)))],
40
+ ]
41
+ }
42
+
43
+ override func createView() -> UIView? {
44
+ let view = UIView()
45
+ view.clipsToBounds = false
46
+ return view
47
+ }
48
+
49
+ /// Lynx passes the value as it is (an `id`), and `NSNull` with
50
+ /// `requestReset` when the attribute is removed.
51
+ @objc func setType(_ value: Any?, requestReset: Bool) {
52
+ let type: ASAuthorizationAppleIDButton.ButtonType
53
+ switch requestReset ? nil : value as? String {
54
+ case "continue": type = .continue
55
+ case "signUp": type = .signUp
56
+ default: type = .signIn
57
+ }
58
+ if type != self.type {
59
+ self.type = type
60
+ stale = true
61
+ }
62
+ }
63
+
64
+ @objc func setButtonStyle(_ value: Any?, requestReset: Bool) {
65
+ let style: ASAuthorizationAppleIDButton.Style
66
+ switch requestReset ? nil : value as? String {
67
+ case "white": style = .white
68
+ case "whiteOutline": style = .whiteOutline
69
+ default: style = .black
70
+ }
71
+ if style != self.style {
72
+ self.style = style
73
+ stale = true
74
+ }
75
+ }
76
+
77
+ @objc func setCornerRadius(_ value: Any?, requestReset: Bool) {
78
+ let radius = requestReset ? nil : (value as? NSNumber).map { CGFloat($0.doubleValue) }
79
+ ?? (value as? String).flatMap(Double.init).map { CGFloat($0) }
80
+ cornerRadius = radius
81
+ if let radius {
82
+ button?.cornerRadius = radius
83
+ } else if button != nil {
84
+ // UIKit has no way back to the button's own radius once one is set,
85
+ // so a removed `corner-radius` makes a new button.
86
+ stale = true
87
+ }
88
+ }
89
+
90
+ override func propsDidUpdate() {
91
+ super.propsDidUpdate()
92
+ guard stale else { return }
93
+ let container = view()
94
+ stale = false
95
+ button?.removeFromSuperview()
96
+ let button = ASAuthorizationAppleIDButton(authorizationButtonType: type, authorizationButtonStyle: style)
97
+ if let cornerRadius {
98
+ button.cornerRadius = cornerRadius
99
+ }
100
+ button.frame = container.bounds
101
+ // Lynx sizes the container; the button follows it.
102
+ button.autoresizingMask = [.flexibleWidth, .flexibleHeight]
103
+ container.addSubview(button)
104
+ self.button = button
105
+ }
106
+ }
@@ -0,0 +1,222 @@
1
+ import AuthenticationServices
2
+ import Lynx
3
+ import UIKit
4
+
5
+ /// The native module behind the runtime's `LynxAppleSignIn`, which calls it as
6
+ /// `NativeModules.AngularLynxAppleSignIn` (from the background thread, where
7
+ /// NativeModules lives).
8
+ ///
9
+ /// `signIn` opens Sign in with Apple's own sheet
10
+ /// (`ASAuthorizationAppleIDProvider`) over the app's key window, the usual
11
+ /// way to meet App Review's guideline 4.8 beside other social logins, and
12
+ /// calls back once with the credential: the stable user identifier, the identity token
13
+ /// and the authorization code the app's server checks with Apple, and the
14
+ /// email and name, which Apple gives only on the person's first sign-in.
15
+ /// `credentialState` asks Apple whether a user identifier is still signed in,
16
+ /// which Apple asks apps to check at launch, `available` says the module
17
+ /// is there, and `button` that `<apple-sign-in-button>` is (AppleSignInButton).
18
+ ///
19
+ /// The sheet needs the `com.apple.developer.applesignin` entitlement, which a
20
+ /// free Personal Team can't sign, so the generator writes it and keeps this
21
+ /// file only in an app whose angular.json sets `signInWithApple`.
22
+ /// LynxViewController registers the module by its Objective-C name only when
23
+ /// it's there, hence the `@objc` name.
24
+ @objc(AngularLynxAppleSignInModule)
25
+ final class AppleSignInModule: NSObject, LynxModule, ASAuthorizationControllerDelegate,
26
+ ASAuthorizationControllerPresentationContextProviding
27
+ {
28
+ static var name: String { "AngularLynxAppleSignIn" }
29
+
30
+ static var methodLookup: [String: String] {
31
+ [
32
+ "available": NSStringFromSelector(#selector(available(_:))),
33
+ "signIn": NSStringFromSelector(#selector(signIn(_:callback:))),
34
+ "credentialState": NSStringFromSelector(#selector(credentialState(_:callback:))),
35
+ "button": NSStringFromSelector(#selector(button)),
36
+ ]
37
+ }
38
+
39
+ /// The open sign-in and its callback. Held until it ends: the controller's
40
+ /// delegate is weak, and a released controller never calls back.
41
+ private var controller: ASAuthorizationController?
42
+ private var callback: LynxCallbackBlock?
43
+
44
+ override init() {
45
+ super.init()
46
+ }
47
+
48
+ // Swift can't import an optional initializer as optional, so Lynx's
49
+ // initWithParam: is required here. Registered without a param, the module
50
+ // ignores it.
51
+ init(param: Any) {
52
+ super.init()
53
+ }
54
+
55
+ /// Calls back `{ available: true }`: the shell supports iOS 15 and later,
56
+ /// all of which have the sheet, so an app with the module can always open
57
+ /// it. Without the module, the runtime's call fails, which it reads as
58
+ /// unavailable.
59
+ @objc func available(_ callback: @escaping LynxCallbackBlock) {
60
+ callback(["available": true])
61
+ }
62
+
63
+ /// Returns `{ available: true }`: the shell registers `<apple-sign-in-button>`
64
+ /// (AppleSignInButton) with this module. The runtime calls it from the main
65
+ /// thread, where Angular makes elements, to choose between iOS's button and
66
+ /// the one it draws itself; without the module it gets no answer.
67
+ @objc func button() -> [String: Any] {
68
+ ["available": true]
69
+ }
70
+
71
+ /// Opens the sheet with `request`'s `scopes` (`email`, `fullName`), `nonce`
72
+ /// and `state`, and calls back once with the credential, or with
73
+ /// `{ error, message }`, where `error` is `canceled` or `failed`.
74
+ @objc func signIn(_ request: [String: Any], callback: @escaping LynxCallbackBlock) {
75
+ // Lynx calls modules off the main thread, and the sheet is UI.
76
+ DispatchQueue.main.async {
77
+ self.open(request, callback)
78
+ }
79
+ }
80
+
81
+ private func open(_ request: [String: Any], _ callback: @escaping LynxCallbackBlock) {
82
+ guard controller == nil else {
83
+ callback(["error": "failed", "message": "A sign-in is already open."])
84
+ return
85
+ }
86
+ let appleIDRequest = ASAuthorizationAppleIDProvider().createRequest()
87
+ let scopes = request["scopes"] as? [String] ?? []
88
+ appleIDRequest.requestedScopes = scopes.compactMap { scope in
89
+ switch scope {
90
+ case "email": return .email
91
+ case "fullName": return .fullName
92
+ default: return nil
93
+ }
94
+ }
95
+ if let nonce = request["nonce"] as? String {
96
+ appleIDRequest.nonce = nonce
97
+ }
98
+ if let state = request["state"] as? String {
99
+ appleIDRequest.state = state
100
+ }
101
+ let controller = ASAuthorizationController(authorizationRequests: [appleIDRequest])
102
+ controller.delegate = self
103
+ controller.presentationContextProvider = self
104
+ self.controller = controller
105
+ self.callback = callback
106
+ controller.performRequests()
107
+ }
108
+
109
+ /// Calls back the open sign-in's callback once, and lets the next one open.
110
+ private func finish(_ result: [String: Any]) {
111
+ let callback = self.callback
112
+ controller = nil
113
+ self.callback = nil
114
+ callback?(result)
115
+ }
116
+
117
+ func authorizationController(
118
+ controller: ASAuthorizationController,
119
+ didCompleteWithAuthorization authorization: ASAuthorization
120
+ ) {
121
+ guard let credential = authorization.credential as? ASAuthorizationAppleIDCredential else {
122
+ finish(["error": "failed", "message": "Apple gave no Apple ID credential."])
123
+ return
124
+ }
125
+ var result: [String: Any] = [
126
+ "user": credential.user,
127
+ "realUserStatus": Self.statusName(credential.realUserStatus),
128
+ ]
129
+ // Both are UTF-8 text: the token a JWT, the code a short string. They're
130
+ // missing only when Apple couldn't make them, which the runtime reports.
131
+ if let token = credential.identityToken.flatMap({ String(data: $0, encoding: .utf8) }) {
132
+ result["identityToken"] = token
133
+ }
134
+ if let code = credential.authorizationCode.flatMap({ String(data: $0, encoding: .utf8) }) {
135
+ result["authorizationCode"] = code
136
+ }
137
+ if let email = credential.email {
138
+ result["email"] = email
139
+ }
140
+ if let state = credential.state {
141
+ result["state"] = state
142
+ }
143
+ // Only on the first sign-in with the fullName scope, and then with only
144
+ // the parts the person filled in.
145
+ if let name = credential.fullName {
146
+ let parts: [String: String?] = [
147
+ "givenName": name.givenName,
148
+ "familyName": name.familyName,
149
+ "middleName": name.middleName,
150
+ "nickname": name.nickname,
151
+ "namePrefix": name.namePrefix,
152
+ "nameSuffix": name.nameSuffix,
153
+ ]
154
+ let given = parts.compactMapValues { $0 }
155
+ if !given.isEmpty {
156
+ result["fullName"] = given
157
+ }
158
+ }
159
+ finish(result)
160
+ }
161
+
162
+ func authorizationController(
163
+ controller: ASAuthorizationController,
164
+ didCompleteWithError error: Error
165
+ ) {
166
+ // The person closed the sheet.
167
+ if let error = error as? ASAuthorizationError, error.code == .canceled {
168
+ finish(["error": "canceled", "message": "The sign-in was canceled."])
169
+ return
170
+ }
171
+ // Code 1000 (unknown) covers more than one cause: a phone with no Apple
172
+ // Account, where iOS offers Settings and closing that alert ends the
173
+ // request (AuthKit's -7022 underneath, seen on the Simulator), and an
174
+ // app signed without the entitlement. The code goes in the message so
175
+ // the app's logs can tell them apart from the others.
176
+ finish([
177
+ "error": "failed",
178
+ "message": "Sign in with Apple failed: \(error.localizedDescription) (\((error as NSError).code))",
179
+ ])
180
+ }
181
+
182
+ /// The sheet opens over the app's window.
183
+ func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
184
+ UIApplication.shared.connectedScenes
185
+ .compactMap { $0 as? UIWindowScene }
186
+ .flatMap(\.windows)
187
+ .first(where: \.isKeyWindow) ?? ASPresentationAnchor()
188
+ }
189
+
190
+ /// Calls back `{ state }` for `user`, an identifier a sign-in returned:
191
+ /// `authorized`, `revoked` (the person stopped using Apple ID with the app),
192
+ /// `notFound` or `transferred` (the app moved to another team), or
193
+ /// `{ error, message }`.
194
+ @objc func credentialState(_ user: String, callback: @escaping LynxCallbackBlock) {
195
+ ASAuthorizationAppleIDProvider().getCredentialState(forUserID: user) { state, error in
196
+ if let error {
197
+ callback(["error": "failed", "message": error.localizedDescription])
198
+ return
199
+ }
200
+ callback(["state": Self.stateName(state)])
201
+ }
202
+ }
203
+
204
+ private static func stateName(_ state: ASAuthorizationAppleIDProvider.CredentialState) -> String {
205
+ switch state {
206
+ case .authorized: return "authorized"
207
+ case .revoked: return "revoked"
208
+ case .notFound: return "notFound"
209
+ case .transferred: return "transferred"
210
+ @unknown default: return "notFound"
211
+ }
212
+ }
213
+
214
+ private static func statusName(_ status: ASUserDetectionStatus) -> String {
215
+ switch status {
216
+ case .likelyReal: return "likelyReal"
217
+ case .unknown: return "unknown"
218
+ case .unsupported: return "unsupported"
219
+ @unknown default: return "unknown"
220
+ }
221
+ }
222
+ }