craft-native 0.0.262 → 0.0.276

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.
@@ -240,8 +240,10 @@ enum CraftHybridKeychain {
240
240
  static let nativeService = "dev.craft.native.secure"
241
241
 
242
242
  static func set(_ key: String, value: String?) {
243
+ defer { CraftKeychain.forget(account: key) }
243
244
  // Every item for the account, whatever its service.
244
245
  SecItemDelete([kSecClass as String: kSecClassGenericPassword, kSecAttrAccount as String: key] as CFDictionary)
246
+ CraftKeychain.forget(account: key)
245
247
  guard let value else { return }
246
248
  for service in [nil, nativeService] {
247
249
  var add: [String: Any] = [
@@ -355,6 +357,7 @@ final class CraftHybrid: NSObject {
355
357
  isConfigured = true
356
358
  self.config = config
357
359
  CraftNativeScriptThread.isBackground = config.nativeScriptThread == "background"
360
+ if CraftNativeScriptThread.isBackground { CraftNativeBundle.prewarm() }
358
361
  hasWeb = config.renderer != "native"
359
362
  siteURL = config.devServerURL.flatMap(URL.init(string:)).flatMap { url in
360
363
  ["http", "https"].contains((url.scheme ?? "").lowercased()) ? url : nil
@@ -2034,6 +2037,8 @@ final class CraftHybridController: UIViewController, UINavigationControllerDeleg
2034
2037
  func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
2035
2038
  lastNavigation = CACurrentMediaTime()
2036
2039
  CraftFramePacing.shared.end("navigation")
2040
+ // A page shows no native accessory; one a screen gave up is taken off.
2041
+ if viewController is CraftHybridWebSlot { CraftChrome.shared.clearUnclaimedTabAccessory() }
2037
2042
  let key = ObjectIdentifier(navigationController)
2038
2043
  let before = shownStacks[key] ?? []
2039
2044
  let now = navigationController.viewControllers
@@ -2291,16 +2296,34 @@ final class CraftFramePacing: NSObject {
2291
2296
  }
2292
2297
  }
2293
2298
 
2294
- /// One main-thread block, timed: a screen mount, a bundle evaluation.
2299
+ /// One block, timed: a screen mount, a bundle evaluation. Labelled by the
2300
+ /// thread it ran on, since a screen's script now runs off the main thread
2301
+ /// and a bundle's time there costs no frames.
2295
2302
  @discardableResult
2296
2303
  func measure<T>(_ name: String, _ work: () -> T) -> T {
2297
2304
  guard enabled else { return work() }
2298
2305
  let started = CACurrentMediaTime()
2299
2306
  let result = work()
2300
- write(String(format: "%@ main=%.1fms", name, (CACurrentMediaTime() - started) * 1000))
2307
+ write(String(format: "%@ %@=%.1fms", name, Thread.isMainThread ? "main" : "script", (CACurrentMediaTime() - started) * 1000))
2301
2308
  return result
2302
2309
  }
2303
2310
 
2311
+ /// One piece of the screens' JavaScript (CraftNativeScriptThread): how
2312
+ /// long it waited behind other screens' work, and how long it ran. The
2313
+ /// screens share one serial script thread, so a screen in front can sit
2314
+ /// behind hidden tabs reloading; `waited` is where that shows.
2315
+ func scriptWork(_ name: String, waited: CFTimeInterval, ran: CFTimeInterval) {
2316
+ guard enabled else { return }
2317
+ write(String(format: "script %@ waited=%.1fms ran=%.1fms", name, waited * 1000, ran * 1000))
2318
+ }
2319
+
2320
+ /// A capability a screen asked for (a request, the keychain), from the
2321
+ /// ask to the answer. Logged from 4ms, as script work is.
2322
+ func capability(_ name: String, took: CFTimeInterval) {
2323
+ guard enabled, took >= 0.004 else { return }
2324
+ write(String(format: "capability %@ took=%.1fms", name, took * 1000))
2325
+ }
2326
+
2304
2327
  /// Each line to the log and to `Library/Caches/craft-frame-pacing.log`
2305
2328
  /// in the app's container, which a test run reads back whole (a
2306
2329
  /// simulator's unified log can drop an app's lines under load). Written
@@ -219,11 +219,13 @@ enum CraftNativeActions {
219
219
  }
220
220
  // A write that moves the sign-in token is the account's change:
221
221
  // every screen hears it (CraftSession). Reads skip the check.
222
- if ["set", "remove", "delete", "clear"].contains(method) {
223
- sync = CraftSession.observe(key: method == "clear" ? nil : args.first as? String, config: config, write)
224
- } else {
225
- sync = write()
222
+ // Off the main thread, in order (CraftKeychain.queue).
223
+ let isWrite = ["set", "remove", "delete", "clear"].contains(method)
224
+ CraftKeychain.queue.async {
225
+ let result = isWrite ? CraftSession.observe(key: method == "clear" ? nil : args.first as? String, config: config, write) : write()
226
+ complete(requestToken, result, completion)
226
227
  }
228
+ return
227
229
  case ("Biometrics", "isAvailable"):
228
230
  sync = config.enableBiometric ? .success(biometricContext().canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil)) : failure("CAPABILITY_DISABLED", "Biometric authentication is disabled")
229
231
  case ("Biometrics", "getBiometricType"):
@@ -957,6 +959,7 @@ enum CraftNativeActions {
957
959
  if method == "clear" {
958
960
  let query: [String: Any] = [kSecClass as String: kSecClassGenericPassword, kSecAttrService as String: service]
959
961
  let status = SecItemDelete(query as CFDictionary)
962
+ CraftKeychain.forget(account: nil)
960
963
  guard status == errSecSuccess || status == errSecItemNotFound else {
961
964
  return failure("SECURE_STORAGE_ERROR", "Secure storage could not be cleared")
962
965
  }
@@ -977,24 +980,38 @@ enum CraftNativeActions {
977
980
  query[kSecValueData as String] = Data(value.utf8)
978
981
  query[kSecAttrAccessible as String] = kSecAttrAccessibleWhenUnlockedThisDeviceOnly
979
982
  let removed = SecItemDelete(base as CFDictionary)
983
+ CraftKeychain.forget(account: key)
980
984
  guard removed == errSecSuccess || removed == errSecItemNotFound else {
981
985
  return failure("SECURE_STORAGE_ERROR", "Secure storage could not be replaced (\(removed))")
982
986
  }
983
987
  let status = SecItemAdd(query as CFDictionary, nil)
988
+ CraftKeychain.forget(account: key)
984
989
  return status == errSecSuccess ? .success(true) : failure("SECURE_STORAGE_ERROR", "Secure storage could not be written (\(status))")
985
990
  case "get":
986
- var query = base
987
- query[kSecReturnData as String] = true
988
- query[kSecMatchLimit as String] = kSecMatchLimitOne
989
- var result: AnyObject?
990
- let status = SecItemCopyMatching(query as CFDictionary, &result)
991
- if status == errSecItemNotFound { return .success(NSNull()) }
992
- guard status == errSecSuccess, let data = result as? Data else {
991
+ // A failed read is not remembered: it is answered as a failure,
992
+ // and the next read asks the Keychain again.
993
+ var failed = false
994
+ let value = CraftKeychain.value(account: key, service: service) { () -> String? in
995
+ var query = base
996
+ query[kSecReturnData as String] = true
997
+ query[kSecMatchLimit as String] = kSecMatchLimitOne
998
+ var result: AnyObject?
999
+ let status = SecItemCopyMatching(query as CFDictionary, &result)
1000
+ if status == errSecItemNotFound { return nil }
1001
+ guard status == errSecSuccess, let data = result as? Data else {
1002
+ failed = true
1003
+ return nil
1004
+ }
1005
+ return String(decoding: data, as: UTF8.self)
1006
+ }
1007
+ if failed {
1008
+ CraftKeychain.forget(account: key)
993
1009
  return failure("SECURE_STORAGE_ERROR", "Secure storage could not be read")
994
1010
  }
995
- return .success(String(decoding: data, as: UTF8.self))
1011
+ return .success(value.map { $0 as Any } ?? NSNull())
996
1012
  case "remove":
997
1013
  let status = SecItemDelete(base as CFDictionary)
1014
+ CraftKeychain.forget(account: key)
998
1015
  return status == errSecSuccess || status == errSecItemNotFound ? .success(true) : failure("SECURE_STORAGE_ERROR", "Secure storage could not be removed")
999
1016
  default:
1000
1017
  return failure("UNKNOWN_ACTION", "Unsupported SecureStorage method")
@@ -1500,15 +1517,85 @@ extension CraftNativeActions {
1500
1517
  /// `secureStore` writes it. Nil when there is none.
1501
1518
  static func webSecureValue(forKey key: String) -> String? {
1502
1519
  guard let key = validKey(key) else { return nil }
1503
- let query: [String: Any] = [
1504
- kSecClass as String: kSecClassGenericPassword,
1505
- kSecAttrAccount as String: key,
1506
- kSecReturnData as String: true,
1507
- kSecMatchLimit as String: kSecMatchLimitOne,
1508
- ]
1509
- var result: AnyObject?
1510
- guard SecItemCopyMatching(query as CFDictionary, &result) == errSecSuccess, let data = result as? Data else { return nil }
1511
- return String(data: data, encoding: .utf8)
1520
+ return CraftKeychain.value(account: key, service: nil) {
1521
+ let query: [String: Any] = [
1522
+ kSecClass as String: kSecClassGenericPassword,
1523
+ kSecAttrAccount as String: key,
1524
+ kSecReturnData as String: true,
1525
+ kSecMatchLimit as String: kSecMatchLimitOne,
1526
+ ]
1527
+ var result: AnyObject?
1528
+ guard SecItemCopyMatching(query as CFDictionary, &result) == errSecSuccess, let data = result as? Data else { return nil }
1529
+ return String(data: data, encoding: .utf8)
1530
+ }
1531
+ }
1532
+ }
1533
+
1534
+ /// The app's own Keychain items, read once and then remembered.
1535
+ ///
1536
+ /// A Keychain read is `SecItemCopyMatching`, which took 100-900ms a read on a
1537
+ /// simulator and is not free on a phone, and every screen reads the sign-in
1538
+ /// token and the account beside it when it opens. They ran on the main
1539
+ /// thread, one after another, so a sign-in that made each open screen check
1540
+ /// the account again held the interface for seconds.
1541
+ ///
1542
+ /// This process is the only writer of these items, through `secureStorage`,
1543
+ /// `CraftHybridKeychain` and the web bridge's secure storage, and each of
1544
+ /// them forgets what it changed. An item another process could write (a
1545
+ /// shared access group, an extension) is not remembered, and coming back to
1546
+ /// the foreground forgets everything, so a value written while the app was
1547
+ /// away is read fresh.
1548
+ enum CraftKeychain {
1549
+ /// Where native screens' secure storage runs: off the main thread, one
1550
+ /// call at a time, so a write is always seen by the read after it.
1551
+ static let queue = DispatchQueue(label: "craft.keychain", qos: .userInitiated)
1552
+ private static let lock = NSLock()
1553
+ private static var values: [String: String?] = [:]
1554
+ /// Moves on every forget, so a read that raced a write is not kept.
1555
+ private static var generation = 0
1556
+ private static var observing: NSObjectProtocol?
1557
+
1558
+ private static func cacheKey(_ account: String, _ service: String?) -> String {
1559
+ "\(service ?? "*")\u{1F}\(account)"
1560
+ }
1561
+
1562
+ /// `account`'s value under `service` (nil: whichever service holds it),
1563
+ /// from memory when it was read before, else from `read`.
1564
+ static func value(account: String, service: String?, read: () -> String?) -> String? {
1565
+ let key = cacheKey(account, service)
1566
+ lock.lock()
1567
+ if observing == nil { observeForeground() }
1568
+ if let known = values[key] {
1569
+ lock.unlock()
1570
+ return known
1571
+ }
1572
+ let started = generation
1573
+ lock.unlock()
1574
+ let value = read()
1575
+ lock.lock()
1576
+ if generation == started { values[key] = .some(value) }
1577
+ lock.unlock()
1578
+ return value
1579
+ }
1580
+
1581
+ /// Drop what is remembered for `account`, under every service; nil drops all.
1582
+ static func forget(account: String?) {
1583
+ lock.lock()
1584
+ generation += 1
1585
+ if let account {
1586
+ let suffix = "\u{1F}\(account)"
1587
+ values = values.filter { !$0.key.hasSuffix(suffix) }
1588
+ } else {
1589
+ values.removeAll()
1590
+ }
1591
+ lock.unlock()
1592
+ }
1593
+
1594
+ /// Called with `lock` held.
1595
+ private static func observeForeground() {
1596
+ observing = NotificationCenter.default.addObserver(forName: UIApplication.willEnterForegroundNotification, object: nil, queue: nil) { _ in
1597
+ forget(account: nil)
1598
+ }
1512
1599
  }
1513
1600
  }
1514
1601
 
@@ -49,6 +49,8 @@ final class CraftNativeFlatList: UICollectionView, UICollectionViewDelegateFlowL
49
49
 
50
50
  private(set) var representedIdentity: String?
51
51
  private(set) var hostedView: UIView?
52
+ /// Top, bottom, leading and trailing: the row's own margins inset it.
53
+ private var hostedConstraints: [NSLayoutConstraint] = []
52
54
  var onRecycle: ((String) -> Void)?
53
55
  override func prepareForReuse() {
54
56
  super.prepareForReuse()
@@ -74,7 +76,7 @@ final class CraftNativeFlatList: UICollectionView, UICollectionViewDelegateFlowL
74
76
  representedIdentity = identity
75
77
  // The same view, already here. A view that left for another cell
76
78
  // and is asked for again is put back below.
77
- guard hostedView !== view || view.superview !== contentView else { return }
79
+ guard hostedView !== view || view.superview !== contentView else { return applyHostedMargins() }
78
80
  // Only a view still in this cell is taken out of it: one that has
79
81
  // since moved to another cell is that cell's now, and pulling it
80
82
  // from there blanked a row on screen.
@@ -82,12 +84,27 @@ final class CraftNativeFlatList: UICollectionView, UICollectionViewDelegateFlowL
82
84
  hostedView = view
83
85
  view.translatesAutoresizingMaskIntoConstraints = false
84
86
  contentView.addSubview(view)
85
- NSLayoutConstraint.activate([
87
+ hostedConstraints = [
86
88
  view.topAnchor.constraint(equalTo: contentView.topAnchor),
87
- view.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
89
+ contentView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
88
90
  view.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
89
- view.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
90
- ])
91
+ contentView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
92
+ ]
93
+ applyHostedMargins()
94
+ NSLayoutConstraint.activate(hostedConstraints)
95
+ }
96
+
97
+ /// A row's margins (`mx-4` on a grouped row) inset it in its cell, as
98
+ /// a column inset it: pinned edge to edge, HQ's Discover results ran
99
+ /// to the screen's sides. Read again on every pass, since a style
100
+ /// update changes the row in place.
101
+ private func applyHostedMargins() {
102
+ guard let view = hostedView, view.superview === contentView, hostedConstraints.count == 4 else { return }
103
+ let margins = view.craftLayoutMargins
104
+ let constants = [margins.top, margins.bottom, margins.left, margins.right]
105
+ for (constraint, constant) in zip(hostedConstraints, constants) where constraint.constant != constant {
106
+ constraint.constant = constant
107
+ }
91
108
  }
92
109
 
93
110
  func releaseHostedView() {
@@ -106,6 +123,8 @@ final class CraftNativeFlatList: UICollectionView, UICollectionViewDelegateFlowL
106
123
  _ layoutAttributes: UICollectionViewLayoutAttributes
107
124
  ) -> UICollectionViewLayoutAttributes {
108
125
  guard let hostedView = hostedView else { return layoutAttributes }
126
+ applyHostedMargins()
127
+ let margins = hostedView.craftLayoutMargins
109
128
  let attributes = layoutAttributes.copy() as! UICollectionViewLayoutAttributes
110
129
  // A cell has no collectionView property; the list is its nearest
111
130
  // collection view ancestor.
@@ -121,24 +140,26 @@ final class CraftNativeFlatList: UICollectionView, UICollectionViewDelegateFlowL
121
140
  let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout,
122
141
  flowLayout.scrollDirection == .horizontal {
123
142
  let height = fittingExtent ?? layoutAttributes.size.height
143
+ let inner = max(0, height - margins.top - margins.bottom)
124
144
  let measured = CraftNativeFlexLayout.measure(
125
145
  hostedView,
126
146
  width: nil,
127
- height: height,
147
+ height: inner,
128
148
  maxWidth: CraftNativeFlexLayout.unbounded,
129
- maxHeight: height
149
+ maxHeight: inner
130
150
  )
131
- attributes.size = CGSize(width: max(1, ceil(measured.width)), height: max(1, height))
151
+ attributes.size = CGSize(width: max(1, ceil(measured.width + margins.left + margins.right)), height: max(1, height))
132
152
  } else {
133
153
  let width = fittingExtent ?? layoutAttributes.size.width
154
+ let inner = max(0, width - margins.left - margins.right)
134
155
  let measured = CraftNativeFlexLayout.measure(
135
156
  hostedView,
136
- width: width,
157
+ width: inner,
137
158
  height: nil,
138
- maxWidth: width,
159
+ maxWidth: inner,
139
160
  maxHeight: CraftNativeFlexLayout.unbounded
140
161
  )
141
- attributes.size = CGSize(width: max(1, width), height: max(1, ceil(measured.height)))
162
+ attributes.size = CGSize(width: max(1, width), height: max(1, ceil(measured.height + margins.top + margins.bottom)))
142
163
  }
143
164
  return attributes
144
165
  }
@@ -184,6 +205,14 @@ final class CraftNativeFlatList: UICollectionView, UICollectionViewDelegateFlowL
184
205
  private var keyboardShouldPersistTaps = "never"
185
206
  private var scrollTargetKey: String?
186
207
  private var pendingScrollTarget: (id: String, animated: Bool, attempts: Int)?
208
+ /// A target that ends at the list's end, held to the true bottom while
209
+ /// the rows near it measure themselves. Rows self-size from a 44pt
210
+ /// estimate, so an offset worked out before they are measured stops short
211
+ /// of tall rows (multi-paragraph replies, cards). Cleared by a drag.
212
+ private var endSettlePasses: Int?
213
+ private var endSettleGeneration = 0
214
+ private var isAnimatingToEnd = false
215
+ private var isMeasuringEnd = false
187
216
 
188
217
  init() {
189
218
  let layout = CraftNativeFlatListLayout()
@@ -339,6 +368,7 @@ final class CraftNativeFlatList: UICollectionView, UICollectionViewDelegateFlowL
339
368
  guard key != scrollTargetKey else { return }
340
369
  scrollTargetKey = key
341
370
  pendingScrollTarget = (id, animated, 0)
371
+ cancelEndSettle()
342
372
  scrollToPendingTarget()
343
373
  }
344
374
 
@@ -538,6 +568,7 @@ final class CraftNativeFlatList: UICollectionView, UICollectionViewDelegateFlowL
538
568
  pendingThemeRefresh = false
539
569
  scrollTargetKey = nil
540
570
  pendingScrollTarget = nil
571
+ cancelEndSettle()
541
572
  }
542
573
 
543
574
  /// Releases callbacks and UIKit ownership that belong to a permanently
@@ -569,15 +600,28 @@ final class CraftNativeFlatList: UICollectionView, UICollectionViewDelegateFlowL
569
600
  }
570
601
 
571
602
  func scrollViewDidScroll(_ scrollView: UIScrollView) {
603
+ // The end is measured by jumping there and back in one pass, before
604
+ // anything draws: the screen hears only where the scroll goes.
605
+ guard !isMeasuringEnd else { return }
572
606
  onScrollEvent?(scrollView)
573
607
  onLayoutChanged?()
574
608
  evaluateEndReached()
575
609
  }
576
610
 
577
611
  func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
612
+ // The finger has the list now: stop holding it to its end.
613
+ cancelEndSettle()
578
614
  onScrollBeginDrag?(scrollView)
579
615
  }
580
616
 
617
+ func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
618
+ guard isAnimatingToEnd else { return }
619
+ isAnimatingToEnd = false
620
+ // Rows passed on the way measured themselves and moved the end.
621
+ settleAtEnd()
622
+ scheduleEndSettle()
623
+ }
624
+
581
625
  func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
582
626
  onScrollEndDrag?(scrollView)
583
627
  }
@@ -636,12 +680,16 @@ final class CraftNativeFlatList: UICollectionView, UICollectionViewDelegateFlowL
636
680
 
637
681
  private func scrollToPendingTarget() {
638
682
  guard let target = pendingScrollTarget else { return }
683
+ // A snapshot that is landing, or queued behind the one landing, can
684
+ // add rows before the target or move it: an index read now is stale
685
+ // (it scrolled to the old end). finishApplyingSnapshot() aims again
686
+ // once the rows are in, and so does a list not laid out yet.
687
+ guard !isApplyingSnapshot, pendingApply == nil, bounds.width > 0, bounds.height > 0 else { return }
639
688
  guard let index = targetIndex(for: target.id), index < numberOfItems(inSection: 0) else {
689
+ // No row has this id once the list is idle: try a few more
690
+ // passes, then let it go.
640
691
  if target.attempts >= 4 {
641
- // A diffable snapshot may still be applying. Keep the target
642
- // for finishApplyingSnapshot(), but discard an invalid target
643
- // once the list is otherwise idle.
644
- if !isApplyingSnapshot { pendingScrollTarget = nil }
692
+ pendingScrollTarget = nil
645
693
  return
646
694
  }
647
695
  pendingScrollTarget = (target.id, target.animated, target.attempts + 1)
@@ -649,8 +697,100 @@ final class CraftNativeFlatList: UICollectionView, UICollectionViewDelegateFlowL
649
697
  return
650
698
  }
651
699
  pendingScrollTarget = nil
700
+ let indexPath = IndexPath(item: index, section: 0)
701
+ let identity = diffableDataSource.itemIdentifier(for: indexPath) ?? items[index].identity
702
+ if index == numberOfItems(inSection: 0) - 1 {
703
+ scrollToEnd(animated: target.animated)
704
+ return
705
+ }
652
706
  let position: UICollectionView.ScrollPosition = flowLayout.scrollDirection == .horizontal ? .left : .top
653
- scrollToItem(at: IndexPath(item: index, section: 0), at: position, animated: target.animated)
707
+ scrollToItem(at: indexPath, at: position, animated: target.animated)
708
+ guard !target.animated else { return }
709
+ // A row near the end cannot come to the top: the list stops at its
710
+ // end, which is only true once the rows there are measured.
711
+ layoutIfNeeded()
712
+ if rowReachesPastEnd(identity) { scrollToEnd(animated: false) }
713
+ }
714
+
715
+ /// The offset that shows the end of the content, along the scroll axis.
716
+ private var endOffset: CGPoint {
717
+ if flowLayout.scrollDirection == .horizontal {
718
+ return CGPoint(
719
+ x: max(-adjustedContentInset.left, contentSize.width + adjustedContentInset.right - bounds.width),
720
+ y: contentOffset.y
721
+ )
722
+ }
723
+ return CGPoint(
724
+ x: contentOffset.x,
725
+ y: max(-adjustedContentInset.top, contentSize.height + adjustedContentInset.bottom - bounds.height)
726
+ )
727
+ }
728
+
729
+ /// Whether the row's leading edge lies past where the list can scroll to.
730
+ private func rowReachesPastEnd(_ identity: String) -> Bool {
731
+ guard let indexPath = diffableDataSource.indexPath(for: identity),
732
+ let frame = layoutAttributesForItem(at: indexPath)?.frame else { return false }
733
+ if flowLayout.scrollDirection == .horizontal {
734
+ return frame.minX - adjustedContentInset.left >= endOffset.x - 0.5
735
+ }
736
+ return frame.minY - adjustedContentInset.top >= endOffset.y - 0.5
737
+ }
738
+
739
+ private func scrollToEnd(animated: Bool) {
740
+ endSettlePasses = 2
741
+ endSettleGeneration += 1
742
+ if animated {
743
+ // Measure the rows at the end where they will be, then animate
744
+ // there from where the list was, so the animation lands on the
745
+ // true end rather than on an estimate.
746
+ let start = contentOffset
747
+ isMeasuringEnd = true
748
+ settleAtEnd()
749
+ let end = contentOffset
750
+ setContentOffset(start, animated: false)
751
+ isMeasuringEnd = false
752
+ if abs(end.x - start.x) > 0.5 || abs(end.y - start.y) > 0.5 {
753
+ isAnimatingToEnd = true
754
+ setContentOffset(end, animated: true)
755
+ }
756
+ } else {
757
+ settleAtEnd()
758
+ }
759
+ scheduleEndSettle()
760
+ }
761
+
762
+ /// Moves to the end until it stops moving: each move shows rows that
763
+ /// measure themselves and change the content size.
764
+ private func settleAtEnd() {
765
+ guard endSettlePasses != nil, !isDragging, !isTracking else { return cancelEndSettle() }
766
+ for _ in 0..<6 {
767
+ layoutIfNeeded()
768
+ let end = endOffset
769
+ guard abs(end.x - contentOffset.x) > 0.5 || abs(end.y - contentOffset.y) > 0.5 else { break }
770
+ setContentOffset(end, animated: false)
771
+ }
772
+ }
773
+
774
+ /// Checks the end again on the next runloop passes, while late rows and
775
+ /// the content size settle.
776
+ private func scheduleEndSettle() {
777
+ let generation = endSettleGeneration
778
+ DispatchQueue.main.async { [weak self] in
779
+ guard let self, self.endSettleGeneration == generation, let passes = self.endSettlePasses,
780
+ // An animation on its way there settles, and checks
781
+ // again, when it ends.
782
+ !self.isAnimatingToEnd else { return }
783
+ if passes <= 0 { return self.cancelEndSettle() }
784
+ self.endSettlePasses = passes - 1
785
+ if !self.isApplyingSnapshot, self.pendingApply == nil { self.settleAtEnd() }
786
+ self.scheduleEndSettle()
787
+ }
788
+ }
789
+
790
+ private func cancelEndSettle() {
791
+ endSettlePasses = nil
792
+ endSettleGeneration += 1
793
+ isAnimatingToEnd = false
654
794
  }
655
795
 
656
796
  private func clamped(_ offset: CGPoint) -> CGPoint {
@@ -339,11 +339,13 @@ final class CraftNativeMutationDocument {
339
339
  /// `Svg`). Only View, SafeAreaView and ScrollView used to be here, so a
340
340
  /// drawing revealed after the first paint (an `:if` around an `<svg>`)
341
341
  /// had its shapes refused as INVALID_PARENT, and the whole screen was
342
- /// drawn again; Android's host already took the same list.
342
+ /// drawn again; Android's host already took the same list. A shape holds
343
+ /// its `Animate` (SMIL keyframes: HQ's moving exercise figures).
343
344
  static let containerTypes: Set<String> = [
344
345
  "View", "SafeAreaView", "ScrollView", "KeyboardAvoidingView", "Modal", "Pressable",
345
346
  "TouchableOpacity", "TouchableHighlight", "ImageBackground", "FlatList", "SectionList",
346
347
  "Fragment", "Svg", "G", "Map",
348
+ "Path", "Line", "Rect", "Circle", "Ellipse", "Polyline", "Polygon",
347
349
  ]
348
350
  private static let containerTypeKeys = Set(containerTypes.map { $0.lowercased() })
349
351