@craft-native/ios 0.0.278 → 0.0.290

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@craft-native/ios",
3
- "version": "0.0.278",
3
+ "version": "0.0.290",
4
4
  "homepage": "https://github.com/craft-native/craft/tree/main/packages/ios#readme",
5
5
  "repository": {
6
6
  "type": "git",
@@ -903,6 +903,9 @@ final class CraftChrome: ObservableObject {
903
903
  func tap(_ tab: Tab) {
904
904
  let reselect = tab.id == selected
905
905
  if !reselect {
906
+ // A tab's screen not made yet is made first, so the pill moves on
907
+ // a free main thread rather than stalling behind its mount.
908
+ CraftHybrid.shared.mountTabBeforeSelecting(tab.id)
906
909
  withAnimation(Self.motion(.spring(response: 0.32, dampingFraction: 0.86))) { selected = tab.id }
907
910
  }
908
911
  // A hybrid app shows the tab's own stack first; the page hears the
@@ -1162,11 +1165,7 @@ struct CraftTabBarView: View {
1162
1165
  VStack(spacing: 3) {
1163
1166
  ZStack(alignment: .topTrailing) {
1164
1167
  if let image = tab.image, let url = URL(string: image) {
1165
- AsyncImage(url: url) { image in
1166
- image.resizable().scaledToFill()
1167
- } placeholder: {
1168
- Image(systemName: symbol(tab.symbol, selected: isSelected))
1169
- }
1168
+ CraftTabPicture(url: url, placeholder: symbol(tab.symbol, selected: isSelected))
1170
1169
  .frame(width: iconSize + 4, height: iconSize + 4)
1171
1170
  .clipShape(Circle())
1172
1171
  .frame(height: iconSize + 6)
@@ -1258,6 +1257,46 @@ struct CraftTabBarView: View {
1258
1257
  }
1259
1258
  }
1260
1259
 
1260
+ /// A tab's picture (HQ's Me: the account's photo). It is kept once loaded,
1261
+ /// so a tab item drawn again (its screen mounting, the bar coming back) shows
1262
+ /// it at once: AsyncImage keeps nothing, so the photo went back to the
1263
+ /// person symbol, loaded again and arrived inside whatever animation was
1264
+ /// running, sliding in from the side on the first visit to Me.
1265
+ private struct CraftTabPicture: View {
1266
+ let url: URL
1267
+ let placeholder: String
1268
+ @State private var image: UIImage?
1269
+
1270
+ private static let cache: NSCache<NSURL, UIImage> = {
1271
+ let cache = NSCache<NSURL, UIImage>()
1272
+ cache.countLimit = 16
1273
+ return cache
1274
+ }()
1275
+
1276
+ var body: some View {
1277
+ Group {
1278
+ if let shown = image ?? Self.cache.object(forKey: url as NSURL) {
1279
+ Image(uiImage: shown).resizable().scaledToFill()
1280
+ } else {
1281
+ Image(systemName: placeholder)
1282
+ }
1283
+ }
1284
+ // A picture arriving never animates, whatever transaction it lands in.
1285
+ .transaction { $0.animation = nil }
1286
+ .task(id: url) {
1287
+ if let cached = Self.cache.object(forKey: url as NSURL) {
1288
+ image = cached
1289
+ return
1290
+ }
1291
+ guard let (data, response) = try? await URLSession.shared.data(from: url),
1292
+ (response as? HTTPURLResponse).map({ (200..<300).contains($0.statusCode) }) ?? true,
1293
+ let loaded = UIImage(data: data) else { return }
1294
+ Self.cache.setObject(loaded, forKey: url as NSURL)
1295
+ image = loaded
1296
+ }
1297
+ }
1298
+ }
1299
+
1261
1300
  private struct CraftTabSelectionShape: Shape {
1262
1301
  let cornerRadius: CGFloat?
1263
1302
 
@@ -880,6 +880,13 @@ final class CraftHybrid: NSObject {
880
880
  return controller.tabTapped(id, reselect: reselect)
881
881
  }
882
882
 
883
+ /// A native tab not prepared yet mounts its screen now, hidden, before
884
+ /// the bar's selection moves (CraftChrome.tap).
885
+ func mountTabBeforeSelecting(_ id: String) {
886
+ guard isEnabled else { return }
887
+ controller?.mountTabBeforeSelecting(id)
888
+ }
889
+
883
890
  /// The page chose a tab itself (its `selectTab`, or a tab link).
884
891
  func pageSelectedTab(_ id: String) {
885
892
  guard isEnabled else { return }
@@ -1731,6 +1738,20 @@ final class CraftHybridController: UIViewController, UINavigationControllerDeleg
1731
1738
 
1732
1739
  private var preparation: Timer?
1733
1740
 
1741
+ /// A native tab tapped before the shell prepared it (HQ's Me, prepared
1742
+ /// last, tapped right after launch) mounts its screen hidden first. Mounted
1743
+ /// in the tap's own turn after the selection began, it held the main
1744
+ /// thread while SwiftUI's spring should have moved the pill, which then
1745
+ /// stalled and jumped in from the side.
1746
+ func mountTabBeforeSelecting(_ id: String) {
1747
+ guard stacks[id] == nil, id != currentTab, activeTabSwipeTarget == nil,
1748
+ hybrid.tabIds.contains(id), hybrid.routes.match(id) != nil, view.window != nil else { return }
1749
+ let nav = makeStack(tab: id, rootPath: id, hidden: true)
1750
+ nav.view.layoutIfNeeded()
1751
+ Self.drawContents(of: nav.view.layer)
1752
+ (visibleStack?.topViewController as? CraftNativeScreenController)?.screenVisibilityChanged(true)
1753
+ }
1754
+
1734
1755
  /// When the shell last moved between screens: a tab, a push or a pop.
1735
1756
  private var lastNavigation: CFTimeInterval = 0
1736
1757
 
@@ -5101,6 +5101,17 @@ private final class CraftNativeTextView: UITextView {
5101
5101
 
5102
5102
  override func layoutSubviews() {
5103
5103
  super.layoutSubviews()
5104
+ // A multiline input grows with its text up to its `maxHeight`, then
5105
+ // scrolls inside itself. Never scrolling, the text past that height
5106
+ // was cut off and could not be reached (a long workout comment).
5107
+ // With scrolling off, contentSize is the view's own height: measure
5108
+ // the text itself.
5109
+ let needed = isEditable ? sizeThatFits(CGSize(width: bounds.width, height: .greatestFiniteMagnitude)).height : 0
5110
+ let overflows = isEditable && bounds.height > 0 && needed > bounds.height + 1
5111
+ if isScrollEnabled != overflows {
5112
+ isScrollEnabled = overflows
5113
+ if overflows, isFirstResponder { scrollRangeToVisible(selectedRange) }
5114
+ }
5104
5115
  let inset = textContainerInset
5105
5116
  let padding = textContainer.lineFragmentPadding
5106
5117
  placeholderLabel.frame = CGRect(
@@ -5117,6 +5128,7 @@ private final class CraftNativeTextView: UITextView {
5117
5128
  }
5118
5129
 
5119
5130
  private func configurePlaceholder() {
5131
+ isScrollEnabled = false
5120
5132
  placeholderLabel.numberOfLines = 0
5121
5133
  placeholderLabel.font = font
5122
5134
  placeholderLabel.textColor = .placeholderText
@@ -5623,6 +5635,7 @@ final class CraftNativeScreenController: UIViewController, UIScrollViewDelegate,
5623
5635
  // A tab prepared in a hidden stack "appears" to UIKit as well; the
5624
5636
  // shell says when it really shows (screenVisibilityChanged).
5625
5637
  if isShownOnScreen {
5638
+ hasAppeared = true
5626
5639
  screenVisibilityChanged(true)
5627
5640
  // A navigation breadcrumb for the crash reporter: the trail a
5628
5641
  // crash report carries is the screens that led to it.
@@ -5694,6 +5707,14 @@ final class CraftNativeScreenController: UIViewController, UIScrollViewDelegate,
5694
5707
  // preview also restores a screen that never lost focus, so restore its
5695
5708
  // chrome even when no new SCREEN_FOCUS event is needed.
5696
5709
  if visible { restoreShellChrome() }
5710
+ // A prepared tab the shell now shows has appeared once its frame is
5711
+ // drawn; what its script asks for until then is there from the start.
5712
+ if visible, !hasAppeared {
5713
+ DispatchQueue.main.async { [weak self] in
5714
+ guard let self, self.isViewLoaded, self.isShownOnScreen else { return }
5715
+ self.hasAppeared = true
5716
+ }
5717
+ }
5697
5718
  if visible, frameWanted { CraftNativeFrameClock.shared.request(self) { [weak self] in self?.fireFrame() } }
5698
5719
  if !visible { CraftNativeFrameClock.shared.cancel(self) }
5699
5720
  guard visible != reportedVisible else { return }
@@ -5780,6 +5801,27 @@ final class CraftNativeScreenController: UIViewController, UIScrollViewDelegate,
5780
5801
  super.viewDidLayoutSubviews()
5781
5802
  positionModals()
5782
5803
  emitLayoutEvents()
5804
+ sendViewportIfChanged()
5805
+ }
5806
+
5807
+ /// The width the bundle last heard, so a layout pass at the same size says nothing.
5808
+ private var reportedViewportWidth: CGFloat = -1
5809
+
5810
+ /// The screen's own width once laid out, else its window's: what a web
5811
+ /// page's media queries would measure.
5812
+ private func currentViewportWidth() -> CGFloat {
5813
+ let width = view.bounds.width > 0 ? view.bounds.width : (view.window ?? CraftPresenter.keyWindow())?.bounds.width ?? 0
5814
+ reportedViewportWidth = width
5815
+ return width
5816
+ }
5817
+
5818
+ /// A rotation, iPad split view or a resized Stage Manager window: the
5819
+ /// bundle's width classes (`md:`, `lg:`) follow the screen's new width.
5820
+ private func sendViewportIfChanged() {
5821
+ let width = view.bounds.width
5822
+ guard width > 0, abs(width - reportedViewportWidth) >= 0.5 else { return }
5823
+ reportedViewportWidth = width
5824
+ send(type: "VIEWPORT", payload: ["width": Double(width), "height": Double(view.bounds.height)])
5783
5825
  }
5784
5826
 
5785
5827
  /// Modals cover the screen; their content starts below its bars (the
@@ -5864,6 +5906,8 @@ final class CraftNativeScreenController: UIViewController, UIScrollViewDelegate,
5864
5906
  let appearanceJSON: String
5865
5907
  let sessionTokenKey: String
5866
5908
  let focused: Bool?
5909
+ /// The screen's width in points, for the bundle's `sm:`/`md:`/`lg:` classes.
5910
+ let viewportWidth: Double
5867
5911
  }
5868
5912
 
5869
5913
  private func scriptInputs() -> ScriptInputs {
@@ -5875,7 +5919,8 @@ final class CraftNativeScreenController: UIViewController, UIScrollViewDelegate,
5875
5919
  colorScheme: colorScheme,
5876
5920
  appearanceJSON: appearanceJSON(),
5877
5921
  sessionTokenKey: Self.jsonString(CraftSession.tokenKey(config)),
5878
- focused: reportedVisible
5922
+ focused: reportedVisible,
5923
+ viewportWidth: Double(currentViewportWidth())
5879
5924
  )
5880
5925
  }
5881
5926
 
@@ -5977,6 +6022,7 @@ final class CraftNativeScreenController: UIViewController, UIScrollViewDelegate,
5977
6022
  capabilities: \(capabilityJSON),
5978
6023
  initialAppState: "\(inputs.appState)",
5979
6024
  colorScheme: "\(inputs.colorScheme)",
6025
+ viewportWidth: \(inputs.viewportWidth),
5980
6026
  appearance: \(inputs.appearanceJSON),
5981
6027
  appURL: \(appURL),
5982
6028
  appVersion: \(appVersion),
@@ -6546,6 +6592,14 @@ final class CraftNativeScreenController: UIViewController, UIScrollViewDelegate,
6546
6592
  /// own horizontal pager or chart gesture.
6547
6593
  private(set) var tabSwipeEnabled = true
6548
6594
  private var prefersLargeTitle: Bool?
6595
+ /// Whether the screen has been on screen. Until then, a bar its script
6596
+ /// asks for is simply there in its first frame: a tab opened for the
6597
+ /// first time (HQ's Me) mounts in the window, and its bar and large title
6598
+ /// slid in from the side as the screen appeared.
6599
+ private var hasAppeared = false
6600
+ /// A bar shown or hidden after the screen has appeared animates; before,
6601
+ /// it is in place from the start.
6602
+ var animatesNavigationBarChanges: Bool { hasAppeared && view.window != nil }
6549
6603
  /// Whether this screen opts into an opaque navigation bar filled with its
6550
6604
  /// own background colour. The default keeps UIKit's material appearance.
6551
6605
  private var navigationBarSolid = false
@@ -6624,7 +6678,7 @@ final class CraftNativeScreenController: UIViewController, UIScrollViewDelegate,
6624
6678
  if let hidden = options["hidden"] as? Bool {
6625
6679
  navigationBarHidden = hidden
6626
6680
  if navigationController?.topViewController === self {
6627
- navigationController?.setNavigationBarHidden(hidden, animated: view.window != nil)
6681
+ navigationController?.setNavigationBarHidden(hidden, animated: animatesNavigationBarChanges)
6628
6682
  }
6629
6683
  }
6630
6684
  if options.keys.contains("leftButtons") {
@@ -6642,7 +6696,7 @@ final class CraftNativeScreenController: UIViewController, UIScrollViewDelegate,
6642
6696
  // Asked for after the bar was hidden for a bare root screen.
6643
6697
  if navigationBarHidden == nil, wantsNavigationBar, navigationController?.topViewController === self,
6644
6698
  navigationController?.isNavigationBarHidden == true {
6645
- navigationController?.setNavigationBarHidden(false, animated: view.window != nil)
6699
+ navigationController?.setNavigationBarHidden(false, animated: animatesNavigationBarChanges)
6646
6700
  }
6647
6701
  }
6648
6702
 
@@ -7104,13 +7158,39 @@ final class CraftNativeScreenController: UIViewController, UIScrollViewDelegate,
7104
7158
  /// `input`, scrolled into the visible part of the scroll view it is in.
7105
7159
  /// Internal for XCTest.
7106
7160
  func scrollIntoView(_ input: UIView, animated: Bool) {
7107
- var ancestor = input.superview
7108
- while let current = ancestor, !(current is CraftNativeScrollView) { ancestor = current.superview }
7109
- guard let scroll = ancestor as? CraftNativeScrollView, scroll.isVertical else { return }
7110
- let rect = input.convert(input.bounds, to: scroll).insetBy(dx: 0, dy: -16)
7161
+ guard let scroll = enclosingVerticalScroll(of: input) else { return }
7162
+ var rect = input.convert(input.bounds, to: scroll).insetBy(dx: 0, dy: -16)
7163
+ // A field taller than the part the keyboard leaves (a long comment
7164
+ // with no max height) shows its caret rather than its top: scrolled
7165
+ // to its top, the caret at the end of the text stayed under the
7166
+ // keyboard with no way to scroll it out.
7167
+ let visible = scroll.bounds.inset(by: scroll.adjustedContentInset)
7168
+ if rect.height > visible.height, let caret = caretRect(of: input, in: scroll) { rect = caret }
7111
7169
  scroll.scrollRectToVisible(rect, animated: animated)
7112
7170
  }
7113
7171
 
7172
+ /// The vertical scroll view `view` sits in, if any.
7173
+ private func enclosingVerticalScroll(of view: UIView) -> CraftNativeScrollView? {
7174
+ var ancestor = view.superview
7175
+ while let current = ancestor, !(current is CraftNativeScrollView) { ancestor = current.superview }
7176
+ guard let scroll = ancestor as? CraftNativeScrollView, scroll.isVertical else { return nil }
7177
+ return scroll
7178
+ }
7179
+
7180
+ /// A focused input's caret in `scroll`'s coordinates, with a little room
7181
+ /// around it, kept within the input (one scrolling inside itself has its
7182
+ /// caret beyond its own frame until it scrolls to it).
7183
+ private func caretRect(of input: UIView, in scroll: UIScrollView) -> CGRect? {
7184
+ guard input.isFirstResponder, let text = input as? UITextInput,
7185
+ let end = text.selectedTextRange?.end else { return nil }
7186
+ let caret = text.caretRect(for: end)
7187
+ guard caret.minY.isFinite, caret.height > 0 else { return nil }
7188
+ let frame = input.convert(input.bounds, to: scroll)
7189
+ var rect = input.convert(caret, to: scroll)
7190
+ rect.origin.y = min(max(rect.minY, frame.minY), max(frame.minY, frame.maxY - rect.height))
7191
+ return rect.insetBy(dx: 0, dy: -16)
7192
+ }
7193
+
7114
7194
  private func sendAppState(_ state: String) {
7115
7195
  // A screen hears it while it is on top of its stack; the app script,
7116
7196
  // which is on no stack, always.
@@ -8977,7 +9057,8 @@ final class CraftNativeScreenController: UIViewController, UIScrollViewDelegate,
8977
9057
  if textView.textContainerInset != padding { textView.textContainerInset = padding }
8978
9058
  textView.textContainer.lineFragmentPadding = 0
8979
9059
  }
8980
- textView.isScrollEnabled = false
9060
+ // An input's own scrolling follows its height (CraftNativeTextView).
9061
+ if !(textView is CraftNativeTextView) { textView.isScrollEnabled = false }
8981
9062
  let numberOfLines = Int(number(props["numberOfLines"]) ?? 0)
8982
9063
  if numberOfLines > 0 {
8983
9064
  textView.textContainer.maximumNumberOfLines = numberOfLines
@@ -9846,11 +9927,38 @@ final class CraftNativeScreenController: UIViewController, UIScrollViewDelegate,
9846
9927
  let id = ObjectIdentifier(textView)
9847
9928
  inputIdentities[id].map { inputDrafts[$0] = textView.text ?? "" }
9848
9929
  (textView as? CraftNativeTextView)?.refreshPlaceholder()
9930
+ keepCaretVisible(in: textView)
9849
9931
  guard let handler = handlers[id] else { return }
9850
9932
  noteSentText(textView.text ?? "", for: textView)
9851
9933
  send(type: "EVENT", payload: ["handlerName": handler, "nativeEvent": ["text": textView.text ?? ""]])
9852
9934
  }
9853
9935
 
9936
+ func textViewDidChangeSelection(_ textView: UITextView) {
9937
+ guard textView.isFirstResponder else { return }
9938
+ keepCaretVisible(in: textView)
9939
+ }
9940
+
9941
+ /// The caret, kept where it can be seen as a multiline input grows or the
9942
+ /// caret moves: in the part of the screen's scroll view the keyboard
9943
+ /// leaves. Only the field was scrolled into view, when editing began, so
9944
+ /// a long comment typed at the bottom of a screen went under the keyboard.
9945
+ private func keepCaretVisible(in textView: UITextView) {
9946
+ DispatchQueue.main.async { [weak self, weak textView] in
9947
+ guard let self, let textView, textView.isFirstResponder else { return }
9948
+ // The input grows with its text first: a text view's new lines
9949
+ // are news to the flex layout around it, which sized the scroll
9950
+ // view's content to the old height.
9951
+ textView.invalidateIntrinsicContentSize()
9952
+ CraftNativeFlexLayout.invalidateAncestors(of: textView)
9953
+ self.view.layoutIfNeeded()
9954
+ guard let scroll = self.enclosingVerticalScroll(of: textView),
9955
+ let rect = self.caretRect(of: textView, in: scroll) else { return }
9956
+ let visible = scroll.bounds.inset(by: scroll.adjustedContentInset)
9957
+ guard !visible.contains(rect) else { return }
9958
+ scroll.scrollRectToVisible(rect, animated: false)
9959
+ }
9960
+ }
9961
+
9854
9962
  func textViewDidBeginEditing(_ textView: UITextView) {
9855
9963
  lastFocusedInput = textView
9856
9964
  if keyboardScreenFrame != nil { DispatchQueue.main.async { self.scrollFocusedInputIntoView(animated: true) } }