@blotch/angular-lynx-native 22.2.25 → 22.2.26

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 (47) hide show
  1. package/README.md +5 -3
  2. package/dist/android/project.d.ts +2 -1
  3. package/dist/android/project.d.ts.map +1 -1
  4. package/dist/android/project.js +4 -1
  5. package/dist/generate.d.ts +1 -1
  6. package/dist/generate.js +8 -2
  7. package/dist/index.d.ts +3 -1
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +9 -0
  10. package/dist/ios/project.d.ts +3 -2
  11. package/dist/ios/project.d.ts.map +1 -1
  12. package/dist/ios/project.js +12 -2
  13. package/dist/options.d.ts +57 -0
  14. package/dist/options.d.ts.map +1 -1
  15. package/dist/options.js +51 -0
  16. package/dist/png.d.ts +5 -0
  17. package/dist/png.d.ts.map +1 -1
  18. package/dist/png.js +19 -5
  19. package/dist/splash.d.ts +70 -0
  20. package/dist/splash.d.ts.map +1 -0
  21. package/dist/splash.js +239 -0
  22. package/package.json +1 -1
  23. package/templates/android/README.md +8 -1
  24. package/templates/android/app/build.gradle.kts +4 -0
  25. package/templates/android/app/src/debug/java/dev/angularlynx/shell/DevServer.kt +6 -0
  26. package/templates/android/app/src/main/AndroidManifest.xml +3 -1
  27. package/templates/android/app/src/main/java/dev/angularlynx/shell/AssetsModule.kt +2 -1
  28. package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasUI.kt +16 -3
  29. package/templates/android/app/src/main/java/dev/angularlynx/shell/MainActivity.kt +57 -1
  30. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellModule.kt +10 -0
  31. package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLContext.kt +15 -4
  32. package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLModule.kt +10 -1
  33. package/templates/android/app/src/main/res/values/colors.xml +3 -2
  34. package/templates/android/app/src/main/res/values/splash.xml +9 -0
  35. package/templates/android/app/src/main/res/values/themes.xml +14 -0
  36. package/templates/android/app/src/release/java/dev/angularlynx/shell/DevServer.kt +2 -0
  37. package/templates/ios/App/AngularLynxWebGL.h +34 -6
  38. package/templates/ios/App/AngularLynxWebGL.m +58 -23
  39. package/templates/ios/App/AngularLynxWebGLContext.m +25 -21
  40. package/templates/ios/App/AnimationFrameRate.swift +214 -0
  41. package/templates/ios/App/AppDelegate.swift +2 -0
  42. package/templates/ios/App/AssetsModule.swift +3 -1
  43. package/templates/ios/App/DevServer.swift +3 -0
  44. package/templates/ios/App/Info.plist +12 -5
  45. package/templates/ios/App/LynxViewController.swift +19 -1
  46. package/templates/ios/App/ShellModule.swift +14 -1
  47. package/templates/ios/App/SplashScreen.swift +71 -0
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <resources>
3
+ <!--
4
+ The splash screen's image (themes.xml): the launcher icon, as Android 12
5
+ shows by default. With angular.json's splash.image, the generator
6
+ deletes this file and writes drawable-*/splash_icon.png instead.
7
+ -->
8
+ <drawable name="splash_icon">@mipmap/ic_launcher</drawable>
9
+ </resources>
@@ -10,4 +10,18 @@
10
10
  <item name="android:windowLightStatusBar">true</item>
11
11
  <item name="android:windowLightNavigationBar">true</item>
12
12
  </style>
13
+
14
+ <!--
15
+ The splash screen, from the tap on the icon until the app's first screen
16
+ is ready (MainActivity keeps it up until the runtime says so). AndroidX's
17
+ core-splashscreen draws it as Android 12 and later do on every version,
18
+ and switches to the theme above once it's installed. Its color is the
19
+ window's, and its image splash_icon: the launcher icon, or angular.json's
20
+ splash.image, which the generator writes in its place.
21
+ -->
22
+ <style name="Theme.AngularLynxShell.Starting" parent="Theme.SplashScreen">
23
+ <item name="windowSplashScreenBackground">@color/window_background</item>
24
+ <item name="windowSplashScreenAnimatedIcon">@drawable/splash_icon</item>
25
+ <item name="postSplashScreenTheme">@style/Theme.AngularLynxShell</item>
26
+ </style>
13
27
  </resources>
@@ -21,4 +21,6 @@ object DevServer {
21
21
  fun remoteUrl(url: String): String? = null
22
22
 
23
23
  fun loaded(url: String, error: Throwable?) = Unit
24
+
25
+ fun isShowingMessage(): Boolean = false
24
26
  }
@@ -9,6 +9,14 @@
9
9
  // (`enableMTSModule`). The runtime batches the calls into one `run` per frame;
10
10
  // see packages/runtime/src/lib/canvas/webgl-bridge.ts for the protocol.
11
11
  //
12
+ // Lynx's main thread is its engine thread, which the shell runs off the UI
13
+ // thread (LynxViewController's thread strategy), and the module's calls
14
+ // arrive there. So every GL call is made there: an EGL context is current on
15
+ // one thread at a time, and the context keeps it current between batches.
16
+ // The canvas's view lives on the UI thread, and hands the context what it
17
+ // draws into (AngularLynxCanvasTarget) through Lynx's runOnTasmThread:, so
18
+ // the registry and the contexts are the engine thread's alone.
19
+ //
12
20
  // The OpenGL ES 3.0 is ANGLE's, on Metal (libEGL and libGLESv2, prebuilt
13
21
  // with Lynx's frameworks by scripts/build-angle-ios-frameworks.sh), rather
14
22
  // than iOS's own OpenGL ES. Apple deprecated that in iOS 12, and the
@@ -18,18 +26,37 @@
18
26
  // 3.00, is WebGL2's, so three.js's shaders compile as they are.
19
27
 
20
28
  #import <Foundation/Foundation.h>
29
+ #import <Lynx/LynxContext.h>
21
30
  #import <Lynx/LynxModule.h>
22
31
  #import <Lynx/LynxUI.h>
32
+ #import <QuartzCore/QuartzCore.h>
23
33
  #import <UIKit/UIKit.h>
24
34
 
25
35
  NS_ASSUME_NONNULL_BEGIN
26
36
 
27
37
  @class AngularLynxCanvasView;
28
38
 
39
+ /// What a canvas's view gives its context to show frames in: the view's
40
+ /// CAMetalLayer, which the context makes its window surface on, and the
41
+ /// layer's size in pixels. The context reads it on the engine thread, where
42
+ /// a UIView mustn't be touched, nor released for the last time.
43
+ @interface AngularLynxCanvasTarget : NSObject
44
+ @property(nonatomic, readonly) CAMetalLayer *layer;
45
+ /// The layer's size in pixels as the view was last laid out: written on the
46
+ /// UI thread, read on the engine thread.
47
+ @property(atomic) CGSize pixelSize;
48
+ /// The view, for the UI thread alone (the context sets its opacity there).
49
+ @property(nonatomic, weak, readonly, nullable) AngularLynxCanvasView *view;
50
+ @end
51
+
29
52
  /// The drawing surface: a view backed by a CAMetalLayer, which the context
30
53
  /// copies each finished frame into (an EGL window surface on the layer).
31
54
  @interface AngularLynxCanvasView : UIView
32
55
  @property(nonatomic, copy, nullable) NSString *canvasId;
56
+ /// The LynxView's context, whose engine thread the view hands its target to
57
+ /// (runOnTasmThread:). The canvas element sets it before the canvas id.
58
+ @property(nonatomic, weak, nullable) LynxContext *lynxContext;
59
+ @property(nonatomic, readonly) AngularLynxCanvasTarget *target;
33
60
  @end
34
61
 
35
62
  /// The `canvas` element. Its `canvas-id` attribute, which the runtime sets,
@@ -63,24 +90,25 @@ NS_ASSUME_NONNULL_BEGIN
63
90
  - (NSDictionary *)encodeImageOfType:(NSString *)type
64
91
  quality:(double)quality
65
92
  premultiplied:(BOOL)premultiplied;
66
- /// Shows frames in this view, or nowhere while it's nil.
67
- - (void)attachView:(nullable AngularLynxCanvasView *)view;
93
+ /// Shows frames in this canvas's target, or nowhere while it's nil.
94
+ - (void)attachTarget:(nullable AngularLynxCanvasTarget *)target;
68
95
  /// The view was laid out: a frame that waited for it to have a size is shown.
69
- - (void)viewDidLayOut:(AngularLynxCanvasView *)view;
96
+ - (void)targetDidLayOut:(AngularLynxCanvasTarget *)target;
70
97
  - (void)destroy;
71
98
  @end
72
99
 
73
100
  /// Pairs canvases with contexts by canvas id: either can come first, as the
74
101
  /// runtime may make the context before Lynx has made the element's view.
102
+ /// Engine thread only, as the contexts are.
75
103
  @interface AngularLynxCanvasRegistry : NSObject
76
- + (void)setView:(nullable AngularLynxCanvasView *)view forCanvasId:(NSString *)canvasId;
77
- + (nullable AngularLynxCanvasView *)viewForCanvasId:(NSString *)canvasId;
104
+ + (void)setTarget:(nullable AngularLynxCanvasTarget *)target forCanvasId:(NSString *)canvasId;
105
+ + (nullable AngularLynxCanvasTarget *)targetForCanvasId:(NSString *)canvasId;
78
106
  + (void)setContext:(nullable AngularLynxWebGLContext *)context forCanvasId:(NSString *)canvasId;
79
107
  + (nullable AngularLynxWebGLContext *)contextForCanvasId:(NSString *)canvasId;
80
108
  @end
81
109
 
82
110
  /// The `AngularLynxWebGL` module. Registered on the LynxView's config, and
83
- /// called from the main thread.
111
+ /// called from Lynx's main thread, on its engine thread.
84
112
  @interface AngularLynxWebGLModule : NSObject <LynxModule>
85
113
  @end
86
114
 
@@ -8,8 +8,8 @@
8
8
 
9
9
  @implementation AngularLynxCanvasRegistry
10
10
 
11
- // Weak both ways: the view belongs to Lynx, the context to the module.
12
- static NSMapTable<NSString *, AngularLynxCanvasView *> *views(void) {
11
+ // Weak both ways: the target belongs to its view, the context to the module.
12
+ static NSMapTable<NSString *, AngularLynxCanvasTarget *> *targets(void) {
13
13
  static NSMapTable *table;
14
14
  static dispatch_once_t once;
15
15
  dispatch_once(&once, ^{
@@ -27,23 +27,23 @@ static NSMapTable<NSString *, AngularLynxWebGLContext *> *contexts(void) {
27
27
  return table;
28
28
  }
29
29
 
30
- + (void)setView:(AngularLynxCanvasView *)view forCanvasId:(NSString *)canvasId {
31
- if (view) {
32
- [views() setObject:view forKey:canvasId];
30
+ + (void)setTarget:(AngularLynxCanvasTarget *)target forCanvasId:(NSString *)canvasId {
31
+ if (target) {
32
+ [targets() setObject:target forKey:canvasId];
33
33
  } else {
34
- [views() removeObjectForKey:canvasId];
34
+ [targets() removeObjectForKey:canvasId];
35
35
  }
36
- [[contexts() objectForKey:canvasId] attachView:view];
36
+ [[contexts() objectForKey:canvasId] attachTarget:target];
37
37
  }
38
38
 
39
- + (AngularLynxCanvasView *)viewForCanvasId:(NSString *)canvasId {
40
- return [views() objectForKey:canvasId];
39
+ + (AngularLynxCanvasTarget *)targetForCanvasId:(NSString *)canvasId {
40
+ return [targets() objectForKey:canvasId];
41
41
  }
42
42
 
43
43
  + (void)setContext:(AngularLynxWebGLContext *)context forCanvasId:(NSString *)canvasId {
44
44
  if (context) {
45
45
  [contexts() setObject:context forKey:canvasId];
46
- [context attachView:[views() objectForKey:canvasId]];
46
+ [context attachTarget:[targets() objectForKey:canvasId]];
47
47
  } else {
48
48
  [contexts() removeObjectForKey:canvasId];
49
49
  }
@@ -289,6 +289,18 @@ static AngularLynxDecodedImage *decodeImage(NSData *bytes, NSDictionary *options
289
289
 
290
290
  #pragma mark - Canvas
291
291
 
292
+ @implementation AngularLynxCanvasTarget
293
+
294
+ - (instancetype)initWithView:(AngularLynxCanvasView *)view {
295
+ if ((self = [super init])) {
296
+ _layer = (CAMetalLayer *)view.layer;
297
+ _view = view;
298
+ }
299
+ return self;
300
+ }
301
+
302
+ @end
303
+
292
304
  @implementation AngularLynxCanvasView
293
305
 
294
306
  + (Class)layerClass {
@@ -305,40 +317,62 @@ static AngularLynxDecodedImage *decodeImage(NSData *bytes, NSDictionary *options
305
317
  self.contentScaleFactor = UIScreen.mainScreen.scale;
306
318
  // Lynx dispatches touches itself, from the LynxView.
307
319
  self.userInteractionEnabled = NO;
320
+ _target = [[AngularLynxCanvasTarget alloc] initWithView:self];
308
321
  }
309
322
  return self;
310
323
  }
311
324
 
325
+ /// Runs `block` on Lynx's engine thread, where the module's calls arrive and
326
+ /// every GL call is made: next on it, or at once when this is it (it's the UI
327
+ /// thread under Lynx's default thread strategy). Nothing runs once the
328
+ /// LynxView has gone, nor do its contexts.
329
+ - (void)onEngineThread:(dispatch_block_t)block {
330
+ [_lynxContext runOnTasmThread:block];
331
+ }
332
+
312
333
  // A frame drawn before Lynx gave the view a size waits for one (see
313
334
  // present). An app that draws only when something changes, such as a photo
314
335
  // editor drawing its photo once, would otherwise show nothing until it drew
315
336
  // again; one drawing every frame never noticed.
316
337
  - (void)layoutSubviews {
317
338
  [super layoutSubviews];
318
- if (_canvasId) {
319
- [[AngularLynxCanvasRegistry contextForCanvasId:_canvasId] viewDidLayOut:self];
320
- }
339
+ // ANGLE sizes the layer's next drawable from its bounds and scale, as here.
340
+ CGSize size = self.bounds.size;
341
+ CGFloat scale = self.layer.contentsScale;
342
+ _target.pixelSize = CGSizeMake(floor(size.width * scale), floor(size.height * scale));
343
+ NSString *canvasId = _canvasId;
344
+ AngularLynxCanvasTarget *target = _target;
345
+ if (!canvasId) return;
346
+ [self onEngineThread:^{
347
+ [[AngularLynxCanvasRegistry contextForCanvasId:canvasId] targetDidLayOut:target];
348
+ }];
321
349
  }
322
350
 
323
351
  - (void)setCanvasId:(NSString *)canvasId {
324
352
  if ([_canvasId isEqualToString:canvasId]) return;
325
- if (_canvasId && [AngularLynxCanvasRegistry viewForCanvasId:_canvasId] == self) {
326
- [AngularLynxCanvasRegistry setView:nil forCanvasId:_canvasId];
327
- }
353
+ NSString *old = _canvasId;
328
354
  _canvasId = [canvasId copy];
329
- if (_canvasId) [AngularLynxCanvasRegistry setView:self forCanvasId:_canvasId];
355
+ NSString *new = _canvasId;
356
+ AngularLynxCanvasTarget *target = _target;
357
+ [self onEngineThread:^{
358
+ if (old && [AngularLynxCanvasRegistry targetForCanvasId:old] == target) {
359
+ [AngularLynxCanvasRegistry setTarget:nil forCanvasId:old];
360
+ }
361
+ if (new) [AngularLynxCanvasRegistry setTarget:target forCanvasId:new];
362
+ }];
330
363
  }
331
364
 
332
365
  - (void)dealloc {
333
- // The registry holds the view weakly; a context still showing frames in it
334
- // stops.
366
+ // The context holds the target, and its layer, while it shows frames in
367
+ // it; it lets go here.
335
368
  NSString *canvasId = _canvasId;
369
+ AngularLynxCanvasTarget *target = _target;
336
370
  if (canvasId) {
337
- dispatch_async(dispatch_get_main_queue(), ^{
338
- if (![AngularLynxCanvasRegistry viewForCanvasId:canvasId]) {
339
- [[AngularLynxCanvasRegistry contextForCanvasId:canvasId] attachView:nil];
371
+ [_lynxContext runOnTasmThread:^{
372
+ if ([AngularLynxCanvasRegistry targetForCanvasId:canvasId] == target) {
373
+ [AngularLynxCanvasRegistry setTarget:nil forCanvasId:canvasId];
340
374
  }
341
- });
375
+ }];
342
376
  }
343
377
  }
344
378
 
@@ -351,6 +385,7 @@ static AngularLynxDecodedImage *decodeImage(NSData *bytes, NSDictionary *options
351
385
  }
352
386
 
353
387
  LYNX_PROP_SETTER("canvas-id", setCanvasId, NSString *) {
388
+ self.view.lynxContext = self.context.lynxContext;
354
389
  self.view.canvasId = requestReset || ![value isKindOfClass:NSString.class] ? nil : value;
355
390
  }
356
391
 
@@ -185,7 +185,7 @@ static NSUInteger componentsOf(GLenum format) {
185
185
 
186
186
  // The view's layer, as a window surface each frame is copied into. Made
187
187
  // when a frame is first shown in the view, as the surface's framebuffer 0.
188
- __weak AngularLynxCanvasView *_view;
188
+ AngularLynxCanvasTarget *_target;
189
189
  EGLSurface _surface;
190
190
  /// A frame was finished before there was a view to show it in.
191
191
  BOOL _framePending;
@@ -420,41 +420,45 @@ static NSUInteger componentsOf(GLenum format) {
420
420
  glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _drawFramebuffer);
421
421
  }
422
422
 
423
- - (void)attachView:(AngularLynxCanvasView *)view {
424
- // A view that went away has already turned _view (weak) to nil, but its
425
- // layer lives on in the surface until the surface is let go of here.
426
- if (_view == view && (view || _surface == EGL_NO_SURFACE)) return;
423
+ - (void)attachTarget:(AngularLynxCanvasTarget *)target {
424
+ if (_target == target) return;
425
+ // The surface holds the old target's layer until it's let go of here.
427
426
  [self releaseSurface];
428
- _view = view;
429
- if (view) {
430
- view.layer.opaque = !_alpha;
431
- view.opaque = !_alpha;
427
+ _target = target;
428
+ if (target) {
429
+ // The view's opacity is the UI thread's to set.
430
+ BOOL opaque = !_alpha;
431
+ dispatch_async(dispatch_get_main_queue(), ^{
432
+ AngularLynxCanvasView *view = target.view;
433
+ view.layer.opaque = opaque;
434
+ view.opaque = opaque;
435
+ });
432
436
  if (_framePending) [self present];
433
437
  }
434
438
  }
435
439
 
436
- - (void)viewDidLayOut:(AngularLynxCanvasView *)view {
437
- if (view == _view && _framePending) [self present];
440
+ - (void)targetDidLayOut:(AngularLynxCanvasTarget *)target {
441
+ if (target == _target && _framePending) [self present];
438
442
  }
439
443
 
440
444
  /// Shows the finished frame: copies the default framebuffer (resolving it
441
445
  /// when it's multisampled) into the view's layer, scaled to its size, and
442
446
  /// presents it. Without a laid-out view, the frame waits for one.
443
447
  - (void)present {
444
- AngularLynxCanvasView *view = _view;
445
- CGSize size = view.bounds.size;
446
- // ANGLE sizes the layer's next drawable from its bounds and scale, as
447
- // here, when the blit below first draws to it.
448
- CGFloat scale = view.layer.contentsScale;
449
- GLint layerWidth = (GLint)(size.width * scale);
450
- GLint layerHeight = (GLint)(size.height * scale);
451
- if (!view || layerWidth < 1 || layerHeight < 1) {
448
+ AngularLynxCanvasTarget *target = _target;
449
+ // The layer's size as the view was last laid out: ANGLE sizes the layer's
450
+ // next drawable from its bounds and scale, as the view measured it, when
451
+ // the blit below first draws to it.
452
+ CGSize size = target.pixelSize;
453
+ GLint layerWidth = (GLint)size.width;
454
+ GLint layerHeight = (GLint)size.height;
455
+ if (!target || layerWidth < 1 || layerHeight < 1) {
452
456
  _framePending = YES;
453
457
  return;
454
458
  }
455
459
  if (_surface == EGL_NO_SURFACE) {
456
- _surface = eglCreateWindowSurface(_display, _config, (__bridge EGLNativeWindowType)view.layer,
457
- NULL);
460
+ _surface = eglCreateWindowSurface(_display, _config,
461
+ (__bridge EGLNativeWindowType)target.layer, NULL);
458
462
  if (_surface == EGL_NO_SURFACE) {
459
463
  NSLog(@"[AngularLynxWebGL] No window surface for the canvas (EGL error 0x%x)", eglGetError());
460
464
  _framePending = YES;
@@ -0,0 +1,214 @@
1
+ import Foundation
2
+ import ObjectiveC
3
+ import QuartzCore
4
+
5
+ /// Asks iOS to run the app's Core Animation transitions at up to 120 fps on a
6
+ /// ProMotion iPhone.
7
+ ///
8
+ /// An element with `enable-new-animator="false"` hands its CSS transitions and
9
+ /// `@keyframes` to Core Animation, as `<lynx-stack-outlet>`'s pages and
10
+ /// `ui-bottom-sheet` do. Lynx makes those animations without a
11
+ /// `preferredFrameRateRange`, and iOS runs an animation that asks for no rate at
12
+ /// 60 fps: `CADisableMinimumFrameDurationOnPhone` in Info.plist only lets the
13
+ /// app ask for more. On an iPhone 14 Pro a stack push ran at a steady 60 fps
14
+ /// while the Home Screen's own animations ran at 120.
15
+ ///
16
+ /// Lynx adds its animations in many places (a transition, a keyframe
17
+ /// animation, and their copies on the element's background and border layers),
18
+ /// and they all go through `CALayer.add(_:forKey:)`, so the shell asks there,
19
+ /// for every animation added in the app: UIKit's in the shell's own views too.
20
+ /// With it, the push ran at 120 fps while it moved fast, then slower as it
21
+ /// eased to a stop, as the Home Screen's animations do.
22
+ ///
23
+ /// It leaves alone an animation that asks for a rate of its own, and a loop
24
+ /// that never ends, such as a spinner or a pulse: nobody follows those with
25
+ /// their eye, so they keep the system's rate, and the battery that saves.
26
+ enum AnimationFrameRate {
27
+ /// The range Apple's ProMotion guidance gives fast motion: 120 fps, and no
28
+ /// lower than 80.
29
+ static let fastMotion = CAFrameRateRange(minimum: 80, maximum: 120, preferred: 120)
30
+
31
+ /// The most repeats a finite animation has. Lynx writes CSS's `infinite` as
32
+ /// 10⁹ iterations (half that with `alternate`), and UIKit's loops repeat
33
+ /// forever (`.infinity`), so anything above this is a loop.
34
+ static let loopRepeatCount: Float = 1_000
35
+
36
+ /// Whether the shell asks for `fastMotion` on this animation.
37
+ static func asksForFastMotion(_ animation: CAAnimation) -> Bool {
38
+ // The default range, {0, 0, 0}, is an animation that asks for nothing.
39
+ animation.preferredFrameRateRange.maximum == 0
40
+ && animation.repeatCount <= loopRepeatCount
41
+ && animation.repeatDuration.isFinite
42
+ }
43
+
44
+ /// Swaps in `CALayer`'s replacement `add(_:forKey:)`, and has Lynx's own
45
+ /// animator's rate asked for from the main thread (see `LynxAnimatorFrameRate`).
46
+ /// Call it once, before the first LynxView exists.
47
+ static func install() {
48
+ _ = swapped
49
+ LynxAnimatorFrameRate.install()
50
+ }
51
+
52
+ /// A static's initializer runs once, however often `install()` is called:
53
+ /// swapping twice would put CALayer's own method back.
54
+ private static let swapped: Void = {
55
+ guard
56
+ let original = class_getInstanceMethod(
57
+ CALayer.self, #selector(CALayer.add(_:forKey:))),
58
+ let replacement = class_getInstanceMethod(
59
+ CALayer.self, #selector(CALayer.angularLynx_add(_:forKey:)))
60
+ else { return }
61
+ method_exchangeImplementations(original, replacement)
62
+ }()
63
+ }
64
+
65
+ /// Asks iOS, from the main thread, for the rate Lynx's own animator asks for,
66
+ /// while that animator runs.
67
+ ///
68
+ /// Lynx works out each frame of an element's CSS transitions and `animate()`
69
+ /// itself (every element without `enable-new-animator="false"`), from a
70
+ /// `CADisplayLink` in its `LynxVSyncPulse`. The build's `preferredFps: 'high'`
71
+ /// has Lynx ask that link for up to 120 fps. But the link is made on the thread
72
+ /// that first asks it for a frame, and the shell runs Lynx's engine off the
73
+ /// main thread (`MostOnTASM`), so it lives on the engine thread, and iOS doesn't
74
+ /// raise the display's rate for a display link there. On an iPhone 14 Pro a
75
+ /// transition on Lynx's animator drew a frame every 16.7 ms while the same one
76
+ /// in Core Animation drew one every 8.3 ms. With a display link on the main
77
+ /// thread asking for the same range, it drew one every 8.3 ms too: the engine
78
+ /// already ticked that fast, and only the display held it back.
79
+ ///
80
+ /// So the shell swaps in its own `requestPulse`, which Lynx calls on the engine
81
+ /// thread for each frame it wants, and while those frames come from a link that
82
+ /// asks for more than 60 fps, a display link on the main thread asks for the
83
+ /// same range. It pauses once Lynx has asked for no frame for a few frames, so
84
+ /// a screen with nothing moving keeps the system's rate. A `preferredFps` of
85
+ /// `'auto'` or `'low'` leaves Lynx's link asking for nothing, so nothing is
86
+ /// asked for here either.
87
+ final class LynxAnimatorFrameRate: NSObject {
88
+ static let shared = LynxAnimatorFrameRate()
89
+
90
+ /// How long the main thread keeps asking after Lynx's last frame request: a
91
+ /// few frames, so a frame Angular's work delays doesn't drop the rate between
92
+ /// two of an animation's frames.
93
+ static let linger: CFTimeInterval = 0.1
94
+
95
+ /// The highest rate a display link asks for before the main thread asks too:
96
+ /// what an app gets without asking.
97
+ static let defaultMaximum: Float = 60
98
+
99
+ /// Guards what both threads read: the engine thread writes the request, the
100
+ /// main thread reads it on its own frames.
101
+ private let lock = NSLock()
102
+ private var lastRequest: CFTimeInterval = 0
103
+ private var range = CAFrameRateRange.default
104
+ /// Whether the main thread's link runs, or is about to.
105
+ private var asking = false
106
+ /// The main thread's link. Only the main thread touches it.
107
+ private var link: CADisplayLink?
108
+
109
+ /// Swaps `LynxVSyncPulse`'s `requestPulse` for one that also asks from the
110
+ /// main thread. Does nothing if Lynx no longer has that class or method.
111
+ static func install() {
112
+ _ = swapped
113
+ }
114
+
115
+ private static let swapped: Void = {
116
+ let selector = NSSelectorFromString("requestPulse")
117
+ let replacementSelector = #selector(NSObject.angularLynx_requestPulse)
118
+ guard
119
+ let pulse = NSClassFromString("LynxVSyncPulse"),
120
+ let original = class_getInstanceMethod(pulse, selector),
121
+ let replacement = class_getInstanceMethod(NSObject.self, replacementSelector),
122
+ // Added to LynxVSyncPulse itself, so the swap changes only its methods,
123
+ // not NSObject's.
124
+ class_addMethod(
125
+ pulse, replacementSelector, method_getImplementation(replacement),
126
+ method_getTypeEncoding(replacement)),
127
+ let added = class_getInstanceMethod(pulse, replacementSelector)
128
+ else { return }
129
+ method_exchangeImplementations(original, added)
130
+ }()
131
+
132
+ /// Whether a link's range asks for more than an app gets by default.
133
+ static func asksForHighRate(_ range: CAFrameRateRange) -> Bool {
134
+ max(range.maximum, range.preferred ?? 0) > defaultMaximum
135
+ }
136
+
137
+ /// Called on the engine thread for each frame Lynx asks its link for.
138
+ func lynxRequestedFrame(_ range: CAFrameRateRange) {
139
+ lock.lock()
140
+ lastRequest = CACurrentMediaTime()
141
+ self.range = range
142
+ let start = !asking
143
+ asking = true
144
+ lock.unlock()
145
+ if start {
146
+ DispatchQueue.main.async { self.resume() }
147
+ }
148
+ }
149
+
150
+ private func resume() {
151
+ let link: CADisplayLink
152
+ if let existing = self.link {
153
+ link = existing
154
+ } else {
155
+ link = CADisplayLink(target: self, selector: #selector(tick(_:)))
156
+ link.add(to: .main, forMode: .common)
157
+ self.link = link
158
+ }
159
+ lock.lock()
160
+ let range = self.range
161
+ lock.unlock()
162
+ link.preferredFrameRateRange = range
163
+ link.isPaused = false
164
+ }
165
+
166
+ @objc private func tick(_ link: CADisplayLink) {
167
+ lock.lock()
168
+ let idle = link.timestamp - lastRequest > Self.linger
169
+ if idle {
170
+ // A request after this unlock finds `asking` false and resumes the link
171
+ // on a later turn of the main thread, after this pause.
172
+ asking = false
173
+ }
174
+ lock.unlock()
175
+ if idle {
176
+ link.isPaused = true
177
+ }
178
+ }
179
+ }
180
+
181
+ extension NSObject {
182
+ /// Takes the place of `LynxVSyncPulse`'s `requestPulse` once
183
+ /// `LynxAnimatorFrameRate.install()` has run, so `self` is a `LynxVSyncPulse`.
184
+ @objc fileprivate func angularLynx_requestPulse() {
185
+ // The implementations are swapped, so this runs Lynx's own requestPulse.
186
+ angularLynx_requestPulse()
187
+ let getter = NSSelectorFromString("displayLink")
188
+ guard responds(to: getter),
189
+ let link = perform(getter)?.takeUnretainedValue() as? CADisplayLink,
190
+ LynxAnimatorFrameRate.asksForHighRate(link.preferredFrameRateRange)
191
+ else { return }
192
+ LynxAnimatorFrameRate.shared.lynxRequestedFrame(link.preferredFrameRateRange)
193
+ }
194
+ }
195
+
196
+ extension CALayer {
197
+ /// Takes the place of `add(_:forKey:)` once `AnimationFrameRate.install()`
198
+ /// has run.
199
+ @objc fileprivate func angularLynx_add(_ animation: CAAnimation, forKey key: String?) {
200
+ // The implementations are swapped, so each `angularLynx_add` call below
201
+ // runs CALayer's own `add(_:forKey:)`.
202
+ guard AnimationFrameRate.asksForFastMotion(animation),
203
+ // Core Animation adds a copy of the animation anyway. The one passed in
204
+ // may be one a layer handed back from `animation(forKey:)`, which mustn't
205
+ // be changed, so the rate goes on a copy of our own.
206
+ let fast = animation.copy() as? CAAnimation
207
+ else {
208
+ angularLynx_add(animation, forKey: key)
209
+ return
210
+ }
211
+ fast.preferredFrameRateRange = AnimationFrameRate.fastMotion
212
+ angularLynx_add(fast, forKey: key)
213
+ }
214
+ }
@@ -14,6 +14,8 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
14
14
  didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
15
15
  ) -> Bool {
16
16
  XElementRegistry.register()
17
+ // Before any LynxView, so its first transition already asks for 120 fps.
18
+ AnimationFrameRate.install()
17
19
  let config = LynxConfig(provider: BundleResourceFetcher.shared)
18
20
  LynxEnv.sharedInstance().prepareConfig(config)
19
21
  // LynxService's image service decodes through SDWebImage, which only
@@ -9,7 +9,9 @@ import Lynx
9
9
  /// Lynx's own `<image>`.
10
10
  ///
11
11
  /// Called from the main thread, synchronously (`lynx.module(name)`), like
12
- /// the WebGL module: the runtime turns the answer into a Response.
12
+ /// the WebGL module: the runtime turns the answer into a Response. Lynx calls
13
+ /// it on its engine thread, which LynxViewController runs off the UI thread,
14
+ /// so a dev server download that waits blocks Angular, not UIKit.
13
15
  final class AssetsModule: NSObject, LynxModule {
14
16
  static var name: String { "AngularLynxAssets" }
15
17
 
@@ -185,6 +185,9 @@ final class DevServer {
185
185
  status.text = message
186
186
  statusScreen.isHidden = false
187
187
  statusScreen.superview?.bringSubviewToFront(statusScreen)
188
+ // The launch screen's copy makes way for the message, rather than
189
+ // hide it until it times out (SplashScreen.swift).
190
+ SplashScreen.shared.hide()
188
191
  }
189
192
  #endif
190
193
  }
@@ -2,6 +2,13 @@
2
2
  <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
3
  <plist version="1.0">
4
4
  <dict>
5
+ <!-- Lets the app draw at up to 120 fps on a ProMotion iPhone. Without it
6
+ iOS holds an app's CADisplayLink and Core Animation to 60 fps there,
7
+ while the system's own UI runs at 120, and Lynx animates through a
8
+ CADisplayLink: every transition and drag ran at half the phone's
9
+ rate. Lynx asks for 120 itself (the build's `preferredFps`). -->
10
+ <key>CADisableMinimumFrameDurationOnPhone</key>
11
+ <true/>
5
12
  <key>CFBundleDevelopmentRegion</key>
6
13
  <string>$(DEVELOPMENT_LANGUAGE)</string>
7
14
  <key>CFBundleDisplayName</key>
@@ -29,16 +36,16 @@
29
36
  <key>UIApplicationSupportsMultipleScenes</key>
30
37
  <false/>
31
38
  </dict>
32
- <key>UILaunchScreen</key>
33
- <dict>
34
- <key>UIColorName</key>
35
- <string>LaunchBackground</string>
36
- </dict>
37
39
  <key>UIRequiredDeviceCapabilities</key>
38
40
  <array>
39
41
  <string>arm64</string>
40
42
  </array>
41
43
  <!-- angular-lynx:info -->
44
+ <key>UILaunchScreen</key>
45
+ <dict>
46
+ <key>UIColorName</key>
47
+ <string>LaunchBackground</string>
48
+ </dict>
42
49
  <key>UISupportedInterfaceOrientations</key>
43
50
  <array>
44
51
  <string>UIInterfaceOrientationPortrait</string>