Skip to content

Commit 41c1fd7

Browse files
committed
Refactor CutoutAccessoryView layout calculations
Extracted top padding logic into a separate variable for clarity and added section comments to improve code readability. No functional changes were made to the layout behavior ! ! !
1 parent 7a5f5c7 commit 41c1fd7

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

Sources/NotchMyProblem/CutoutAccessoryView.swift

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,15 @@ public struct CutoutAccessoryView<LeadingContent: View, TrailingContent: View>:
9292

9393
public var body: some View {
9494
GeometryReader { geometry in
95-
// Detect device topology based on safe area height
95+
// MARK: - Device Detection
9696
let statusBarHeight = geometry.safeAreaInsets.top
9797
let hasTopCutout = statusBarHeight > 40
9898

99-
let exclusionWidth = getAdjustedExclusionRect()?.width ?? geometry.size.width - 160
100-
99+
// MARK: - Cutout Dimensions
100+
let exclusionWidth = getAdjustedExclusionRect()?.width ?? (geometry.size.width - 160) // Fallback for non-cutout devices (iPad, iPhone SE)
101101
let exclusionHeight = notchMyProblem.exclusionRect?.height ?? 0
102-
102+
103+
// MARK: - Padding Calculations
103104
let (cutoutPadding, contentPadding, verticalPadding): (CGFloat, CGFloat, CGFloat) = {
104105
switch padding {
105106
case .auto:
@@ -113,37 +114,40 @@ public struct CutoutAccessoryView<LeadingContent: View, TrailingContent: View>:
113114
return (cutout(exclusionWidth), content(exclusionWidth), vertical(exclusionHeight))
114115
}
115116
}()
117+
118+
// MARK: - Top Positioning
119+
let topPadding: CGFloat = {
120+
let minY = notchMyProblem.exclusionRect?.minY ?? (hasTopCutout ? 0 : 5)
121+
122+
if minY == 0 {
123+
// Notched device - cutout touches bezel, need spacing
124+
return hasTopCutout ? (exclusionHeight / 3) : 5
125+
} else {
126+
// Island device - cutout floats, align to its Y position
127+
return minY
128+
}
129+
}()
116130

131+
// MARK: - Layout
117132
HStack(spacing: 0) {
118-
// Leading content, aligned appropriately
133+
// Leading content
119134
leadingContent
120135
.frame(maxWidth: .infinity, alignment: hasTopCutout ? .center : .leading)
121136

122-
// Space for the device's top cutout if present
137+
// Space for the device's top cutout
123138
if exclusionWidth > 0 {
124139
Color.clear
125140
.frame(width: exclusionWidth)
126141
.padding(.horizontal, cutoutPadding)
127142
}
128143

129-
// Trailing content, aligned appropriately
144+
// Trailing content
130145
trailingContent
131146
.frame(maxWidth: .infinity, alignment: hasTopCutout ? .center : .trailing)
132147
}
133148
.padding(.vertical, verticalPadding)
134149
.frame(height: hasTopCutout ? notchMyProblem.exclusionRect?.height ?? statusBarHeight : 30)
135-
.padding(.top, {
136-
let minY = notchMyProblem.exclusionRect?.minY ?? (hasTopCutout ? 0 : 5)
137-
138-
if minY == 0 {
139-
// Notched device - cutout touches bezel, need spacing
140-
return hasTopCutout ? (exclusionHeight / 3) : 5
141-
} else {
142-
// Island device - cutout floats, align to its Y position
143-
return minY
144-
}
145-
}())
146-
150+
.padding(.top, topPadding)
147151
.padding(.horizontal, hasTopCutout ? contentPadding : 5)
148152
.edgesIgnoringSafeArea(.all)
149153
}

0 commit comments

Comments
 (0)