@momo-kits/native-kits 0.300.7 → 0.300.8-beta.1

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 (107) hide show
  1. package/CLAUDE.md +12 -8
  2. package/README.md +8 -7
  3. package/ios/Application/ApplicationEnvironment.swift +6 -4
  4. package/ios/Application/DesignSystemKits.swift +59 -13
  5. package/ios/Application/DesignSystemKitsPayload.swift +0 -8
  6. package/ios/Application/LanguageSource.swift +2 -11
  7. package/ios/Application/Localize.swift +8 -35
  8. package/ios/Application/LocalizeModifier.swift +2 -11
  9. package/ios/Application/MaxApi.swift +3 -11
  10. package/ios/Avatar/Avatar.swift +0 -1
  11. package/ios/Badge/Badge.swift +5 -8
  12. package/ios/Badge/BadgeRibbon.swift +2 -5
  13. package/ios/Button/Button.swift +4 -8
  14. package/ios/CalculatorKeyboard/CalculatorKeyboard.swift +5 -7
  15. package/ios/Carousel/Carousel.swift +4 -17
  16. package/ios/Checkbox/Checkbox.swift +0 -2
  17. package/ios/Collapse/Collapse.swift +17 -11
  18. package/ios/Colors+Radius+Spacing/Theme.swift +0 -7
  19. package/ios/DateTimePicker/DateTimePicker.swift +4 -10
  20. package/ios/DateTimePicker/DateTimePickerTypes.swift +1 -3
  21. package/ios/DateTimePicker/DateTimePickerUtils.swift +0 -3
  22. package/ios/DateTimePicker/WheelPicker.swift +2 -4
  23. package/ios/Extensions/ActiveOpacityButtonStyle.swift +2 -3
  24. package/ios/Extensions/Color++.swift +2 -4
  25. package/ios/Extensions/SkeletonUICompat.swift +0 -8
  26. package/ios/Icon/Icon.swift +3 -6
  27. package/ios/IconButton/IconButton.swift +2 -5
  28. package/ios/Input/ErrorView.swift +0 -7
  29. package/ios/Input/Input.swift +9 -20
  30. package/ios/Input/InputPhoneNumber.swift +37 -20
  31. package/ios/Input/InputSearch.swift +23 -12
  32. package/ios/Input/InputTextArea.swift +4 -13
  33. package/ios/InputDropDown/InputDropDown.swift +1 -11
  34. package/ios/InputMoney/InputMoney.swift +9 -28
  35. package/ios/InputOTP/InputOTP.swift +2 -9
  36. package/ios/Layout/HeaderLayout.swift +72 -0
  37. package/ios/Layout/Item.swift +1 -2
  38. package/ios/Layout/ModalLayout.swift +144 -0
  39. package/ios/Layout/ScreenLayout.swift +48 -31
  40. package/ios/Layout/Section.swift +2 -3
  41. package/ios/Loader/Loader.swift +3 -12
  42. package/ios/Lottie/LottieView.swift +0 -11
  43. package/ios/Navigation/BottomSheet.swift +73 -94
  44. package/ios/Navigation/BottomTab/BottomTab.swift +58 -73
  45. package/ios/Navigation/BottomTab/BottomTabBar.swift +286 -121
  46. package/ios/Navigation/BottomTab/CurvedContainer.swift +52 -54
  47. package/ios/Navigation/BottomTab/DuoBarCompression.swift +5 -12
  48. package/ios/Navigation/BottomTab/DuoTabBar.swift +67 -202
  49. package/ios/Navigation/BottomTab/TabBarMetrics.swift +97 -0
  50. package/ios/Navigation/Component/FloatingButton.swift +4 -11
  51. package/ios/Navigation/Component/Header.swift +35 -54
  52. package/ios/Navigation/Component/HeaderBackground.swift +2 -8
  53. package/ios/Navigation/Component/HeaderJourney.swift +4 -8
  54. package/ios/Navigation/Component/HeaderLocation.swift +4 -8
  55. package/ios/Navigation/Component/HeaderRight.swift +20 -22
  56. package/ios/Navigation/Component/HeaderTitle.swift +1 -6
  57. package/ios/Navigation/Component/HeaderUser.swift +4 -10
  58. package/ios/Navigation/Component/SideBar.swift +6 -21
  59. package/ios/Navigation/Component/SnackBar.swift +9 -13
  60. package/ios/Navigation/HostBackGestureBridge.swift +2 -9
  61. package/ios/Navigation/ModalScreen.swift +24 -13
  62. package/ios/Navigation/Navigation.swift +35 -19
  63. package/ios/Navigation/NavigationContainer.swift +4 -15
  64. package/ios/Navigation/Navigator.swift +21 -39
  65. package/ios/Navigation/StackScreen.swift +64 -51
  66. package/ios/Navigation/StackTransition.swift +7 -14
  67. package/ios/Navigation/SwipeBackHaptic.swift +2 -7
  68. package/ios/Navigation/Tracking/ScreenTracker.swift +2 -7
  69. package/ios/Pagination/PaginationDot.swift +0 -9
  70. package/ios/Pagination/PaginationNumber.swift +1 -9
  71. package/ios/Pagination/PaginationScroll.swift +2 -14
  72. package/ios/Pagination/PaginationWhiteDot.swift +1 -10
  73. package/ios/Popup/PopupDisplay.swift +7 -5
  74. package/ios/Popup/PopupInput.swift +6 -4
  75. package/ios/Popup/PopupLayout.swift +17 -9
  76. package/ios/Popup/PopupNotify.swift +32 -25
  77. package/ios/PopupView/FullscreenPopup.swift +3 -4
  78. package/ios/PopupView/PopupView.swift +2 -2
  79. package/ios/PopupView/Utils++.swift +1 -2
  80. package/ios/ProgressInfo/ProgressInfo.swift +2 -13
  81. package/ios/Skeleton/Skeleton.swift +5 -16
  82. package/ios/Slider/Slider.swift +5 -12
  83. package/ios/StatusBarTap/StatusBarTap.h +2 -3
  84. package/ios/StatusBarTap/StatusBarTap.m +3 -16
  85. package/ios/Stepper/Stepper.swift +8 -27
  86. package/ios/Steps/Steps.swift +29 -26
  87. package/ios/Swipeable/SwipeCell.swift +2 -2
  88. package/ios/Swipeable/SwipeCellModel.swift +3 -6
  89. package/ios/TabView/TabView.swift +14 -28
  90. package/ios/Template/TrustBanner/TrustBanner.swift +2 -9
  91. package/ios/Title/Title.swift +1 -3
  92. package/ios/Tooltip/Tooltip.swift +4 -9
  93. package/ios/Typography/FontScale.swift +79 -24
  94. package/ios/Typography/Text.swift +6 -12
  95. package/ios/Typography/Typography.swift +8 -11
  96. package/ios/Uploader/Uploader.swift +2 -17
  97. package/ios-demo/Resources/tabbar_scan_qr_button.json +1 -0
  98. package/ios-demo/Resources/tabbar_scan_qr_label.json +1 -0
  99. package/ios-demo/Sources/MoMoUIKitsDemo/SampleDemos/AnimatedHeaderDemo.swift +11 -2
  100. package/ios-demo/Sources/MoMoUIKitsDemo/SampleDemos/BottomTabDemo.swift +296 -59
  101. package/ios-demo/Sources/MoMoUIKitsDemo/SampleDemos/InputPhoneNumberDemo.swift +15 -4
  102. package/ios-demo/Sources/MoMoUIKitsDemo/SampleDemos/LocalizeDemo.swift +0 -8
  103. package/ios-demo/Sources/MoMoUIKitsDemo/SampleDemos/NavigationDemo.swift +114 -0
  104. package/ios-demo/Sources/MoMoUIKitsDemo/SampleDemos/PopupNotifyDemo.swift +29 -1
  105. package/ios-demo/Sources/MoMoUIKitsDemo/SampleDemos/TypographyDemo.swift +16 -0
  106. package/package.json +1 -1
  107. package/publish.sh +8 -2
@@ -1,12 +1,5 @@
1
- //
2
- // InputTextArea.swift
3
- // Pods
4
- //
5
- // Created by dung.pham2 on 2/12/24.
6
- //
7
1
  import SwiftUI
8
2
 
9
- // Make these constants public
10
3
  public let MAX_LENGTH = 300
11
4
  public let DEFAULT_HEIGHT: CGFloat = 104
12
5
 
@@ -92,7 +85,6 @@ struct UITextViewWrapper: UIViewRepresentable {
92
85
  }
93
86
  }
94
87
 
95
- // Make the struct public
96
88
  public struct InputTextArea: View {
97
89
  @Environment(\.appTheme) private var theme
98
90
  @Environment(\.momoFontScale) private var fontScale
@@ -120,7 +112,6 @@ public struct InputTextArea: View {
120
112
 
121
113
  @State private var isFocused = false
122
114
 
123
- // Make the initializer public
124
115
  public init(
125
116
  text: Binding<String>,
126
117
  maxLength: Int = MAX_LENGTH,
@@ -204,7 +195,6 @@ public struct InputTextArea: View {
204
195
  }
205
196
  .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
206
197
 
207
- // Clear button (mirrors Compose: focused + non-empty).
208
198
  if isFocused && !text.isEmpty {
209
199
  SwiftUI.Button(action: {
210
200
  text = ""
@@ -215,8 +205,7 @@ public struct InputTextArea: View {
215
205
  .padding(.leading, Spacing.XS)
216
206
  }
217
207
 
218
- // Loading indicator and right icon (independent, mirrors
219
- // Compose's RenderRightIcon: both can render at once).
208
+ // Not if/else: Compose's RenderRightIcon can show the spinner and the icon together.
220
209
  if loading {
221
210
  ActivityIndicator(isAnimating: .constant(true), style: .medium, color: iconTintColor)
222
211
  .frame(width: 16, height: 16)
@@ -279,7 +268,9 @@ public struct InputTextArea: View {
279
268
  }
280
269
  .padding(.horizontal, Spacing.S)
281
270
  .background(theme.colors.background.surface)
282
- .alignmentGuide(.top) { $0[VerticalAlignment.center] }
271
+ // A zero-height frame on the top edge centers the label on the border, re-laid out with font scale.
272
+ .fixedSize(horizontal: false, vertical: true)
273
+ .frame(height: 0)
283
274
  .offset(x: Spacing.S)
284
275
  }
285
276
  }
@@ -1,10 +1,3 @@
1
- //
2
- // InputDropDown.swift
3
- // Pods
4
- //
5
- // Hand-written SwiftUI reimplementation of Compose `InputDropDown`.
6
- //
7
-
8
1
  import SwiftUI
9
2
 
10
3
  // MARK: - RenderRightIcon (mirrors Compose RenderRightIcon for InputDropDown)
@@ -16,8 +9,7 @@ private struct RenderRightIcon: View {
16
9
  let onPressed: () -> Void
17
10
 
18
11
  var body: some View {
19
- // Mirrors Compose's two independent `if` blocks: loading spinner and icon
20
- // can both render at once, not exclusively (not an if/else).
12
+ // Not if/else, as in Compose: the spinner and the icon can show together.
21
13
  if loading {
22
14
  ActivityIndicator(isAnimating: .constant(true), style: .medium, color: color)
23
15
  .frame(width: 16, height: 16)
@@ -100,7 +92,6 @@ public struct InputDropDown: View {
100
92
  public var body: some View {
101
93
  VStack(alignment: .leading, spacing: 0) {
102
94
  ZStack(alignment: .topLeading) {
103
- // Floating label
104
95
  if !floatingValue.isEmpty || !floatingIcon.isEmpty {
105
96
  HStack(spacing: 0) {
106
97
  MomoText(floatingValue, typography: .labelSMedium, color: floatingTitleColor)
@@ -118,7 +109,6 @@ public struct InputDropDown: View {
118
109
  .zIndex(10)
119
110
  }
120
111
 
121
- // Input container
122
112
  HStack(alignment: .center, spacing: 0) {
123
113
  if !leadingIcon.isEmpty {
124
114
  Icon(source: leadingIcon, size: size == .small ? 24 : 32, color: leadingIconTintColor)
@@ -1,12 +1,3 @@
1
- //
2
- // InputMoney.swift
3
- // MoMoUIKits
4
- //
5
- // Native SwiftUI reimplementation of Compose `InputMoney`.
6
- // The bound `text` holds RAW DIGITS only, as in Compose; the field shows
7
- // the thousands-separated currency and edits apply at the caret.
8
- //
9
-
10
1
  import Foundation
11
2
  import SwiftUI
12
3
  import UIKit
@@ -35,7 +26,6 @@ public func formatNumberToMoney(_ digits: String, currency: String = "đ") -> St
35
26
  for i in 0..<reversed.count {
36
27
  count += 1
37
28
  chars.append(reversed[i])
38
- // i counts from the least significant digit; original index = len-1-i.
39
29
  let originalIndex = reversed.count - 1 - i
40
30
  if count == 3 && originalIndex >= 1 {
41
31
  chars.append(".")
@@ -47,9 +37,8 @@ public func formatNumberToMoney(_ digits: String, currency: String = "đ") -> St
47
37
 
48
38
  private func isMoneyDigit(_ char: Character) -> Bool { char.isASCII && char.isNumber }
49
39
 
50
- /// Applies an edit of `display[start..<end]` → `inserted` to the digits of `display`.
51
- /// Separators and currency are never deleted: removing a separator deletes the
52
- /// digit before it instead, removing only the currency leaves the digits untouched. Returns the new digits and the caret as a digit index.
40
+ /// Applies the edit `display[start..<end]` → `inserted` to the digits of `display`. Separators and
41
+ /// currency are never deleted: removing a separator deletes the digit before it instead.
53
42
  private func applyMoneyEdit(_ display: String, start: Int, end: Int, inserted: String) -> (digits: String, caretDigit: Int) {
54
43
  let chars = Array(display)
55
44
  let start = min(start, chars.count)
@@ -83,6 +72,8 @@ private func moneyCaret(_ display: String, digitIndex: Int) -> Int {
83
72
 
84
73
  // MARK: - InputMoney
85
74
 
75
+ /// Compose `InputMoney`: the bound `text` holds raw digits only; the field shows the
76
+ /// thousands-separated currency and edits apply at the caret.
86
77
  public struct InputMoney: View {
87
78
  @Environment(\.appTheme) private var theme
88
79
  @Environment(\.momoFontScale) private var fontScale
@@ -166,7 +157,6 @@ public struct InputMoney: View {
166
157
  public var body: some View {
167
158
  VStack(alignment: .leading, spacing: 0) {
168
159
  ZStack(alignment: .topLeading) {
169
- // Floating label
170
160
  if !floatingValue.isEmpty || !floatingIcon.isEmpty {
171
161
  HStack(spacing: 0) {
172
162
  MomoText(floatingValue, typography: .labelSMedium, color: floatingTitleColor)
@@ -185,24 +175,18 @@ public struct InputMoney: View {
185
175
  }
186
176
  )
187
177
  .background(theme.colors.background.surface)
188
- // Kotlin centers the label within the box-height-tall decorationBox by
189
- // default, then applies `offsetY = -size.values.height / 2` — net effect:
190
- // the label ends up centered exactly on the box's top border. Our ZStack
191
- // anchors top-leading instead, so matching that requires offsetting by
192
- // half the label's OWN measured height, not half the box height.
178
+ // Compose centers the label in the box and offsets it by -box height / 2, onto the
179
+ // top border; this ZStack anchors top-leading, so it offsets by half the label height.
193
180
  .offset(x: Spacing.S, y: -floatingLabelHeight / 2)
194
181
  .zIndex(10)
195
182
  }
196
183
 
197
- // Input container
198
184
  HStack(alignment: .center, spacing: 0) {
199
- // Leading icon
200
185
  if !leadingIcon.isEmpty {
201
186
  Icon(source: leadingIcon, size: size == .small ? 24 : 32, color: leadingIconTintColor)
202
187
  .padding(.trailing, Spacing.XS)
203
188
  }
204
189
 
205
- // Money field
206
190
  ZStack(alignment: .leading) {
207
191
  if !hasDigits {
208
192
  MomoText(
@@ -226,7 +210,6 @@ public struct InputMoney: View {
226
210
  }
227
211
  .frame(maxWidth: .infinity, alignment: .leading)
228
212
 
229
- // Clear button (focused + has value)
230
213
  if isFocused && hasDigits {
231
214
  SwiftUI.Button(action: {
232
215
  text = ""
@@ -238,8 +221,7 @@ public struct InputMoney: View {
238
221
  }
239
222
  }
240
223
 
241
- // Loading indicator and right icon can both render (mirrors
242
- // Compose's two independent `if` blocks, not if/else).
224
+ // Not if/else, as in Compose: the spinner and the icon can show together.
243
225
  if loading {
244
226
  ActivityIndicator(isAnimating: .constant(true), style: .medium, color: iconTintColor)
245
227
  .frame(width: 16, height: 16)
@@ -303,9 +285,8 @@ public struct InputMoney: View {
303
285
 
304
286
  // MARK: - MoneyTextField (UIViewRepresentable)
305
287
 
306
- /// A UITextField that shows live thousands-separated currency while the bound
307
- /// `digits` string holds only the raw numeric value. The caret can move freely
308
- /// but never past the trailing currency symbol; edits apply at the caret.
288
+ /// Shows the formatted currency while `digits` holds the raw value; the caret never passes the
289
+ /// currency suffix.
309
290
  private struct MoneyTextField: UIViewRepresentable {
310
291
  @Binding var digits: String
311
292
  var keyboardType: UIKeyboardType
@@ -27,11 +27,8 @@ private struct OTPCaret: View {
27
27
 
28
28
  // MARK: - InputOTP
29
29
 
30
- /// Native SwiftUI port of Compose `InputOTP`.
31
- ///
32
- /// A row of OTP cells driven by a hidden `TextField`. When `length` is provided
33
- /// it renders fixed-count slots (`-` placeholders + blinking caret); otherwise it
34
- /// renders the typed characters followed by a trailing caret while focused.
30
+ /// Compose `InputOTP`: cells driven by a hidden `TextField`. With `length`, fixed slots (`-`
31
+ /// placeholders + blinking caret); without, the typed characters and a trailing caret while focused.
35
32
  public struct InputOTP: View {
36
33
  @Environment(\.appTheme) private var theme
37
34
  @Binding public var value: String
@@ -80,10 +77,8 @@ public struct InputOTP: View {
80
77
  public var body: some View {
81
78
  VStack(alignment: .leading, spacing: 0) {
82
79
  ZStack {
83
- // Hidden text field that owns the keyboard / cursor.
84
80
  hiddenField
85
81
 
86
- // Floating label over the border.
87
82
  if !floatingValue.isEmpty {
88
83
  HStack {
89
84
  MomoText(floatingValue, typography: .labelSMedium, color: theme.colors.text.hint)
@@ -94,7 +89,6 @@ public struct InputOTP: View {
94
89
  .zIndex(10)
95
90
  }
96
91
 
97
- // Bordered container with cells.
98
92
  cells
99
93
  .frame(maxWidth: .infinity)
100
94
  .frame(height: 56)
@@ -103,7 +97,6 @@ public struct InputOTP: View {
103
97
  .stroke(borderColor, lineWidth: 1)
104
98
  )
105
99
 
106
- // Trailing clear button.
107
100
  HStack {
108
101
  Spacer()
109
102
  SwiftUI.Button(action: clearText) {
@@ -0,0 +1,72 @@
1
+ // Top navigation spec (Figma "Top Navigation · Màn hình gập"). Mirrors Compose `layout/HeaderLayout.kt`
2
+ // and RN; keep the three in sync.
3
+
4
+ import SwiftUI
5
+
6
+ /// Stretch frame on hold until design confirms it: `.stretch` lays out like `.grid`.
7
+ private let isStretchFrameEnabled = false
8
+
9
+ /// Header spec values, in points. Public so custom header rights and extensions can match them.
10
+ public enum HeaderLayoutMetrics {
11
+ public static let buttonSize: CGFloat = 32
12
+ public static let iconSize: CGFloat = 20
13
+ public static let pillWidth: CGFloat = 72
14
+ public static let searchHeight: CGFloat = 32
15
+ public static let titleGap: CGFloat = Spacing.S
16
+ public static let sidePadding: CGFloat = Spacing.M
17
+ /// shortcut + gap + help/home pill
18
+ public static let clusterWidth: CGFloat = buttonSize + titleGap + pillWidth
19
+ public static let frameMaxWidth: CGFloat = 600
20
+ /// Banner image width on every device: wide windows keep the phone size, centered.
21
+ public static let bannerWidth: CGFloat = 375
22
+ }
23
+
24
+ /// How a screen lays out its content on wide windows; the header row follows it.
25
+ public enum ContentLayout: Equatable {
26
+ /// 12 column grid: the header keeps the 12pt side padding.
27
+ case grid
28
+ /// One column of at most `HeaderLayoutMetrics.frameMaxWidth`, centered: the header row sits in the same frame.
29
+ /// On hold: same as `.grid` for now.
30
+ case stretch
31
+ }
32
+
33
+ /// Header geometry for a window; see `ScreenLayout.headerLayout(_:)`.
34
+ public struct HeaderLayout: Equatable {
35
+ /// Short side >= 600, iPhone Duo excluded (it has its own layout).
36
+ public let isWide: Bool
37
+ /// Banner kept at `HeaderLayoutMetrics.bannerWidth`, centered: wide windows plus the iPhone Duo inner display.
38
+ public let isBannerCentered: Bool
39
+ /// Padding from each side of the safe area to the header row (back, title / search, right cluster).
40
+ public let frameInset: CGFloat
41
+ /// Width of the header row, so tabs and other header extensions can line up with it.
42
+ public let frameWidth: CGFloat
43
+ }
44
+
45
+ public extension ScreenLayout {
46
+ func headerLayout(_ contentLayout: ContentLayout = .grid) -> HeaderLayout {
47
+ let isLargeWindow = min(width, height) >= ModalMetrics.foldableMinWidth
48
+ let isWide = !isIPhoneDuo && !isDuoDevice && isLargeWindow
49
+ let safeWidth = width - insets.leading - insets.trailing
50
+ let isStretch = isStretchFrameEnabled && contentLayout == .stretch
51
+ let frameInset = isWide && isStretch
52
+ ? max(HeaderLayoutMetrics.sidePadding, (safeWidth - HeaderLayoutMetrics.frameMaxWidth) / 2)
53
+ : HeaderLayoutMetrics.sidePadding
54
+ return HeaderLayout(
55
+ isWide: isWide,
56
+ isBannerCentered: isLargeWindow,
57
+ frameInset: frameInset,
58
+ frameWidth: safeWidth - frameInset * 2)
59
+ }
60
+ }
61
+
62
+ private struct ContentLayoutKey: EnvironmentKey {
63
+ static let defaultValue: ContentLayout = .grid
64
+ }
65
+
66
+ public extension EnvironmentValues {
67
+ /// The enclosing screen's `NavigationOptions.contentLayout`, set by `StackScreen`.
68
+ var contentLayout: ContentLayout {
69
+ get { self[ContentLayoutKey.self] }
70
+ set { self[ContentLayoutKey.self] = newValue }
71
+ }
72
+ }
@@ -19,8 +19,7 @@ public struct ItemView<Content: View>: View {
19
19
  // MARK: Public
20
20
 
21
21
  public var body: some View {
22
- // A VStack (not a ZStack) so multiple children flow VERTICALLY instead of stacking
23
- // on top of each other — matches the React Item's flex-column content.
22
+ // VStack, not ZStack: children flow vertically like RN Item's flex-column content.
24
23
  VStack(alignment: .leading, spacing: 0) { content() }
25
24
  .frame(width: width, height: height, alignment: .topLeading)
26
25
  }
@@ -0,0 +1,144 @@
1
+ // Foldable spec for Popup and BottomSheet (Figma "Modal - Foldable Guideline"). Mirrors Compose
2
+ // `layout/ModalLayout.kt` and RN; keep the three in sync.
3
+
4
+ import SwiftUI
5
+
6
+ /// Foldable modal spec values, in points. Public so host apps can size their own popups and sheets the same way.
7
+ public enum ModalMetrics {
8
+ public static let popupMaxWidth: CGFloat = 450
9
+ public static let popupMinWidth: CGFloat = 280
10
+ public static let popupMaxHeight: CGFloat = 600
11
+ public static let sheetMaxWidth: CGFloat = 653
12
+ public static let sheetMinWidth: CGFloat = 296
13
+ public static let sheetMinHeight: CGFloat = 230
14
+ public static let foldableMinWidth: CGFloat = 600
15
+ public static let iconButtonSize: CGFloat = 32
16
+ public static let iconSize: CGFloat = 20
17
+ public static let phoneScrimOpacity: CGFloat = 0.6
18
+ public static let foldableScrimOpacity: CGFloat = 0.2
19
+ }
20
+
21
+ public struct PopupGeometry: Equatable {
22
+ public let minWidth: CGFloat
23
+ public let maxWidth: CGFloat
24
+ /// nil: as tall as the content, phones keep their own caps
25
+ public let maxHeight: CGFloat?
26
+ public let margin: CGFloat
27
+ public let radius: CGFloat
28
+ }
29
+
30
+ public struct SheetGeometry: Equatable {
31
+ /// nil: spans the window
32
+ public let width: CGFloat?
33
+ /// nil: phones keep their own cap
34
+ public let maxHeight: CGFloat?
35
+ public let minHeight: CGFloat
36
+ public let margin: CGFloat
37
+ public let radius: CGFloat
38
+ }
39
+
40
+ /// Popup / bottom sheet geometry for a window; see `ModalLayout.resolve` and `ScreenLayout.modalLayout`.
41
+ public struct ModalLayout: Equatable {
42
+ public let isFoldable: Bool
43
+ /// Window coordinates of the area popups center in and sheets rest on the bottom of.
44
+ public let placement: CGRect
45
+ public let popup: PopupGeometry
46
+ public let sheet: SheetGeometry
47
+ public let scrimOpacity: CGFloat
48
+ }
49
+
50
+ public extension ScreenLayout {
51
+ /// Foldable = iPhone Duo or a window whose short side is at least 600 (RN parity), so phones
52
+ /// in landscape keep their look.
53
+ var modalLayout: ModalLayout {
54
+ ModalLayout.resolve(width: width, height: height, safeInsets: insets, isDuoDevice: isDuoDevice, hinge: hinge)
55
+ }
56
+ }
57
+
58
+ public extension ModalLayout {
59
+ /// For hosts without a kits screen (`ScreenLayout.current.modalLayout` covers the key window); window points.
60
+ /// On iPhone Duo `safeInsets` includes the control column and `hinge` is the half-fold hinge (nil while flat).
61
+ static func resolve(
62
+ width: CGFloat,
63
+ height: CGFloat,
64
+ safeInsets insets: EdgeInsets = EdgeInsets(),
65
+ isDuoDevice: Bool = false,
66
+ hinge: CGRect? = nil
67
+ ) -> ModalLayout {
68
+ let isFoldable = isDuoDevice || min(width, height) >= ModalMetrics.foldableMinWidth
69
+ guard isFoldable else {
70
+ let placement = CGRect(x: 0, y: 0, width: width, height: height)
71
+ return ModalLayout(
72
+ isFoldable: false,
73
+ placement: placement,
74
+ popup: popupGeometry(placementWidth: width, maxHeight: nil, margin: Spacing.XL, radius: Radius.XL),
75
+ sheet: SheetGeometry(width: nil, maxHeight: nil, minHeight: 0, margin: 0, radius: Radius.XL),
76
+ scrimOpacity: ModalMetrics.phoneScrimOpacity
77
+ )
78
+ }
79
+
80
+ // the safe area keeps overlays clear of the iPhone Duo control column; half folded they move to the right half
81
+ let hinge = hinge.flatMap { isDuoDevice && $0.minX > 0 && $0.maxX < width ? $0 : nil }
82
+ let left = max(insets.leading, hinge?.maxX ?? 0)
83
+ let placement = CGRect(
84
+ x: left,
85
+ y: insets.top,
86
+ width: max(width - insets.trailing - left, 0),
87
+ height: max(height - insets.top - insets.bottom, 0)
88
+ )
89
+ // outer display and half fold leave little room, so the margins tighten
90
+ let isCompact = isDuoDevice && (hinge != nil || min(width, height) < ModalMetrics.foldableMinWidth)
91
+ let sheetMargin = Spacing.M
92
+ let sheetMaxHeight = height - insets.top - sheetMargin
93
+ let sheetMinWidth = min(ModalMetrics.sheetMinWidth, placement.width)
94
+ return ModalLayout(
95
+ isFoldable: true,
96
+ placement: placement,
97
+ popup: popupGeometry(
98
+ placementWidth: placement.width,
99
+ maxHeight: min(ModalMetrics.popupMaxHeight, placement.height - Spacing.XL * 2),
100
+ margin: isCompact ? Spacing.M : Spacing.XL,
101
+ radius: Radius.XL
102
+ ),
103
+ sheet: SheetGeometry(
104
+ width: max(min(placement.width - sheetMargin * 2, ModalMetrics.sheetMaxWidth), sheetMinWidth),
105
+ maxHeight: sheetMaxHeight,
106
+ minHeight: min(ModalMetrics.sheetMinHeight, sheetMaxHeight),
107
+ margin: sheetMargin,
108
+ radius: Radius.XL
109
+ ),
110
+ scrimOpacity: ModalMetrics.foldableScrimOpacity
111
+ )
112
+ }
113
+
114
+ /// clamp(placement - 2 margin, 280, 450); the 280 floor gives way to a narrower placement.
115
+ private static func popupGeometry(placementWidth: CGFloat, maxHeight: CGFloat?, margin: CGFloat, radius: CGFloat) -> PopupGeometry {
116
+ let minWidth = min(ModalMetrics.popupMinWidth, placementWidth)
117
+ let maxWidth = min(max(placementWidth - margin * 2, minWidth), max(ModalMetrics.popupMaxWidth, minWidth))
118
+ return PopupGeometry(minWidth: minWidth, maxWidth: maxWidth, maxHeight: maxHeight, margin: margin, radius: radius)
119
+ }
120
+ }
121
+
122
+ /// Popup close and sheet header back / close: 32 circle on the surface with a border, 20 icon.
123
+ public struct ModalIconButton: View {
124
+ @Environment(\.appTheme) private var theme
125
+ let icon: String
126
+ let accessibilityId: String
127
+ let action: () -> Void
128
+
129
+ public init(icon: String, accessibilityId: String, action: @escaping () -> Void) {
130
+ self.icon = icon
131
+ self.accessibilityId = accessibilityId
132
+ self.action = action
133
+ }
134
+
135
+ public var body: some View {
136
+ Icon(source: icon, size: ModalMetrics.iconSize, color: theme.colors.text.default)
137
+ .frame(width: ModalMetrics.iconButtonSize, height: ModalMetrics.iconButtonSize)
138
+ .background(Circle().fill(theme.colors.background.surface))
139
+ .overlay(Circle().strokeBorder(theme.colors.border.default, lineWidth: 1))
140
+ .contentShape(Circle())
141
+ .onTapGesture(perform: action)
142
+ .accessibilityIdentifier(accessibilityId)
143
+ }
144
+ }
@@ -1,10 +1,5 @@
1
- //
2
- // ScreenLayout.swift
3
- // MoMoUIKits
4
- //
5
- // Port of momo-kits RN `useScreenLayout` (Layout/utils.ts) and `Application/device.ts`:
6
- // window size, safe-area insets, content width, header geometry and the iPhone Duo side bar.
7
- //
1
+ // Port of momo-kits RN `useScreenLayout` (Layout/utils.ts) and `Application/device.ts`:
2
+ // window size, safe-area insets, content width, header geometry and the iPhone Duo side bar.
8
3
 
9
4
  import SwiftUI
10
5
  import UIKit
@@ -32,7 +27,8 @@ public struct ScreenLayout: Equatable {
32
27
  public let width: CGFloat
33
28
  public let height: CGFloat
34
29
  public let isIPhoneDuo: Bool
35
- /// The device has a hinge, folded or not, whatever the window size; drives popups, modal and bottom sheet.
30
+ /// iPhone Duo folded or not, whatever the window size (its hinge, or the outer display's one-sided strip);
31
+ /// drives popups, modal and bottom sheet.
36
32
  public let isDuoDevice: Bool
37
33
  public let insets: EdgeInsets
38
34
  public let insetLeft: CGFloat
@@ -45,6 +41,8 @@ public struct ScreenLayout: Equatable {
45
41
  public let sideBar: SideBarLayout?
46
42
  /// iPhone Duo inner display in portrait: the header row shares the status bar band, between its clusters.
47
43
  public let statusBarClearance: StatusBarClearance?
44
+ /// Half-folded iPhone Duo: the hinge in window coordinates, running top to bottom; nil while flat.
45
+ public let hinge: CGRect?
48
46
 
49
47
  public var headerBarHeight: CGFloat { headerHeight - headerStatusBarHeight }
50
48
  }
@@ -110,11 +108,14 @@ extension ScreenLayout {
110
108
  insets: EdgeInsets,
111
109
  reservedRegions: [CGRect] = ReservedRegions.current,
112
110
  isPhone: Bool = isPhoneIdiom,
113
- isFoldable: Bool = FoldableDevice.isFoldable(measuredIn: keyWindow)
111
+ isFoldable: Bool = FoldableDevice.isFoldable(measuredIn: keyWindow),
112
+ division: CGRect? = FoldableDevice.halfFoldedHinge
114
113
  ) -> ScreenLayout {
115
114
  let width = size.width
116
115
  let height = size.height
117
116
  let isDuoDevice = isPhone && isFoldable
117
+ // only a hinge splitting the window into left and right halves moves overlays
118
+ let hinge = division.flatMap { isPhone && $0.height > $0.width ? $0 : nil }
118
119
  var (headerHeight, headerStatusBarHeight) = headerGeometry(topInset: insets.top, isPhone: isPhone)
119
120
  let isDuo = isPhone && isIPhoneDuoWindow(width: width, height: height, insets: insets)
120
121
  guard isDuo else {
@@ -122,7 +123,7 @@ extension ScreenLayout {
122
123
  width: width, height: height, isIPhoneDuo: false, isDuoDevice: isDuoDevice, insets: insets,
123
124
  insetLeft: 0, insetRight: 0, contentWidth: width,
124
125
  headerHeight: headerHeight, headerStatusBarHeight: headerStatusBarHeight, sideBar: nil,
125
- statusBarClearance: nil
126
+ statusBarClearance: nil, hinge: hinge
126
127
  )
127
128
  }
128
129
 
@@ -146,9 +147,10 @@ extension ScreenLayout {
146
147
  // Activities; region frames already include the system's margin
147
148
  let occlusion = stripOcclusion(reservedRegions, stripLeft: stripLeft, stripRight: stripLeft + sideInset, windowHeight: height)
148
149
 
149
- var top = statusBarClearance != nil
150
- ? headerHeight + Spacing.M
151
- : insets.top + HeaderMetrics.barHeight + Spacing.M
150
+ var top = insets.top + HeaderMetrics.barHeight + Spacing.M
151
+ if statusBarClearance != nil {
152
+ top = headerHeight + Spacing.M
153
+ }
152
154
  if insets.top < HeaderMetrics.statusBarMinInset {
153
155
  // status bar lives in the strip: start under its cluster, as the native bar does
154
156
  if width <= height {
@@ -178,12 +180,14 @@ extension ScreenLayout {
178
180
  )
179
181
  }
180
182
 
183
+ // the outer display reports no hinge; its one-sided strip still gives it away
181
184
  return ScreenLayout(
182
- width: width, height: height, isIPhoneDuo: true, isDuoDevice: isDuoDevice, insets: insets,
185
+ width: width, height: height, isIPhoneDuo: true,
186
+ isDuoDevice: isDuoDevice || insets.leading != insets.trailing, insets: insets,
183
187
  insetLeft: insets.leading, insetRight: insets.trailing,
184
188
  contentWidth: width - insets.leading - insets.trailing,
185
189
  headerHeight: headerHeight, headerStatusBarHeight: headerStatusBarHeight, sideBar: sideBar,
186
- statusBarClearance: statusBarClearance
190
+ statusBarClearance: statusBarClearance, hinge: hinge
187
191
  )
188
192
  }
189
193
 
@@ -203,9 +207,8 @@ extension ScreenLayout {
203
207
  return (height, max(0, centerY * 2 - height))
204
208
  }
205
209
 
206
- /// How far system regions reach into a side strip from its top and from its bottom end
207
- /// (e.g. the camera, which sits at either end depending on the rotation), and the center y
208
- /// of the region reaching furthest from the top.
210
+ /// How far system regions (e.g. the camera, at either end by rotation) reach into a side strip from its top and
211
+ /// bottom ends, and the center y of the one reaching furthest from the top.
209
212
  private static func stripOcclusion(
210
213
  _ regions: [CGRect],
211
214
  stripLeft: CGFloat,
@@ -256,20 +259,28 @@ extension ScreenLayout {
256
259
  public var duoTopInset: CGFloat {
257
260
  max(insets.top, height > width ? max(insets.leading, insets.trailing) * 2 : 0)
258
261
  }
262
+
259
263
  }
260
264
 
261
265
  /// Snapshot for call sites without `\.screenLayout`; not reactive. `windowSize` is ignored,
262
266
  /// kept for host call sites.
263
267
  public func isIPhoneDuoDevice(windowSize: CGSize? = nil) -> Bool {
264
- isPhoneIdiom && FoldableDevice.isFoldable(measuredIn: keyWindow)
268
+ guard isPhoneIdiom else { return false }
269
+ let insets = keyWindow?.safeAreaInsets
270
+ return FoldableDevice.isFoldable(measuredIn: keyWindow) || insets.map { $0.left != $0.right } == true
265
271
  }
266
272
 
267
- /// Apple's foldable check (as the host's `SideTabRailView.isFoldableDevice`): only a device with a
268
- /// hinge reports a `.division` region, inactive while flat but still there. Screen size cannot tell
269
- /// it apart from a large iPhone in landscape or a narrowed window.
273
+ /// Apple's foldable check (host `SideTabRailView.isFoldableDevice`): only a hinged device reports a `.division`
274
+ /// region, even inactive while flat. Screen size can't tell it from a landscape iPhone or a narrowed window.
270
275
  enum FoldableDevice {
271
276
  // a hinge never goes away, so a positive answer is kept
272
277
  private static var isKnownFoldable = false
278
+ private static var isPartiallyOpen = false
279
+
280
+ /// The active division region while the hinge reports partially open; nil while flat.
281
+ static var halfFoldedHinge: CGRect? {
282
+ isPartiallyOpen ? ReservedRegions.division : nil
283
+ }
273
284
 
274
285
  static func isFoldable(measuredIn view: UIView?) -> Bool {
275
286
  if isKnownFoldable { return true }
@@ -286,8 +297,11 @@ enum FoldableDevice {
286
297
  #if canImport(UIKit, _version: 9127.0.85.28)
287
298
  guard #available(iOS 27.1, *) else { return }
288
299
  view.addInteraction(UIHingeInteraction { _, update in
289
- guard update.hinge != nil, !isKnownFoldable else { return }
300
+ guard let hinge = update.hinge else { return }
301
+ let partiallyOpen = hinge.status == .partiallyOpen
302
+ guard !isKnownFoldable || partiallyOpen != isPartiallyOpen else { return }
290
303
  isKnownFoldable = true
304
+ isPartiallyOpen = partiallyOpen
291
305
  NotificationCenter.default.post(name: ReservedRegions.didChange, object: nil)
292
306
  })
293
307
  #endif
@@ -296,16 +310,18 @@ enum FoldableDevice {
296
310
 
297
311
  // MARK: - Reserved regions
298
312
 
299
- /// System occlusion regions (iOS 27.1+) in window coordinates: the iPhone Duo camera, and the
300
- /// status bar cluster as it grows for a call or a Live Activity. Empty before iOS 27.1.
313
+ /// System occlusion regions (iOS 27.1+, else empty) in window coordinates: the iPhone Duo camera and the status bar
314
+ /// cluster, which grows for a call or a Live Activity. `division` is the hinge, there only while half folded.
301
315
  enum ReservedRegions {
302
316
  // last report of a probe, so `ScreenLayout.current` includes it
303
317
  private(set) static var current: [CGRect] = []
318
+ private(set) static var division: CGRect?
304
319
  static let didChange = Notification.Name("MoMoUIKitsReservedRegionsDidChange")
305
320
 
306
- fileprivate static func publish(_ regions: [CGRect]) {
307
- guard regions != current else { return }
321
+ fileprivate static func publish(_ regions: [CGRect], division: CGRect?) {
322
+ guard regions != current || division != self.division else { return }
308
323
  current = regions
324
+ self.division = division
309
325
  NotificationCenter.default.post(name: didChange, object: nil)
310
326
  }
311
327
  }
@@ -324,12 +340,13 @@ private final class ReservedRegionsProbe: UIView {
324
340
  override func layoutSubviews() {
325
341
  super.layoutSubviews()
326
342
  // read even when not publishing, so UIKit keeps re-running this when they change
327
- let regions = occlusionRegions()
343
+ let regions = activeRegions(kind: "occlusionRegionKind")
344
+ let division = activeRegions(kind: "divisionRegionKind").first
328
345
  // every screen has a probe but the regions are window-wide: one sliding in on a push or
329
346
  // a present, or off the window, would overwrite them with a partial read
330
347
  guard spansWindow else { return }
331
348
  // layout pass: publish on the next turn instead of mutating SwiftUI state mid-update
332
- DispatchQueue.main.async { ReservedRegions.publish(regions) }
349
+ DispatchQueue.main.async { ReservedRegions.publish(regions, division: division) }
333
350
  }
334
351
 
335
352
  private var spansWindow: Bool {
@@ -344,11 +361,11 @@ private final class ReservedRegionsProbe: UIView {
344
361
  }
345
362
 
346
363
  // The API is newer than the SDKs hosts may build with, so it is reached through the runtime.
347
- private func occlusionRegions() -> [CGRect] {
364
+ private func activeRegions(kind kindName: String) -> [CGRect] {
348
365
  let regionsOfKind = NSSelectorFromString("reservedRegionsOfKind:")
349
366
  guard window != nil, responds(to: regionsOfKind),
350
367
  let kindType = NSClassFromString("UIViewReservedRegionKind") as? NSObject.Type,
351
- let kind = (kindType as AnyObject).perform(NSSelectorFromString("occlusionRegionKind"))?.takeUnretainedValue(),
368
+ let kind = (kindType as AnyObject).perform(NSSelectorFromString(kindName))?.takeUnretainedValue(),
352
369
  let regions = perform(regionsOfKind, with: kind)?.takeUnretainedValue() as? [NSObject]
353
370
  else { return [] }
354
371
  return regions.compactMap { ($0.value(forKey: "frame") as? NSValue)?.cgRectValue }