Skip to content

Commit 696407c

Browse files
committed
[ComposeView] add manual clippingBehavior
1 parent e6bc93c commit 696407c

2 files changed

Lines changed: 106 additions & 7 deletions

File tree

ComposeUI/Sources/ComposeUI/ComposeView/ComposeView.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ open class ComposeView: BaseScrollView {
395395
/// The view is scrollable if the content is larger than the view's bounds. Otherwise, the view is not scrollable.
396396
case auto
397397

398-
/// The view does not modify scroll settings. You are responsible for `isScrollEnabled` and bounce configuration.
398+
/// The view does not modify scroll settings. `isScrollable` and `alwaysBounceHorizontal`/`alwaysBounceVertical` are managed by you.
399399
case manual
400400

401401
/// The view is always scrollable. The view will always bounce.
@@ -455,6 +455,9 @@ open class ComposeView: BaseScrollView {
455455
/// The view clips the content to the bounds when the view is scrollable.
456456
case auto
457457

458+
/// The view does not modify `clipsToBounds`. It is managed by you.
459+
case manual
460+
458461
/// The view always clips the content to the bounds.
459462
case always
460463

@@ -858,14 +861,22 @@ open class ComposeView: BaseScrollView {
858861
switch clippingBehavior {
859862
case .auto:
860863
clipsToBounds = isScrollable
864+
#if canImport(AppKit)
865+
contentView.clipsToBounds = clipsToBounds
866+
#endif
867+
case .manual:
868+
break
861869
case .always:
862870
clipsToBounds = true
871+
#if canImport(AppKit)
872+
contentView.clipsToBounds = clipsToBounds
873+
#endif
863874
case .never:
864875
clipsToBounds = false
876+
#if canImport(AppKit)
877+
contentView.clipsToBounds = clipsToBounds
878+
#endif
865879
}
866-
#if canImport(AppKit)
867-
contentView.clipsToBounds = clipsToBounds
868-
#endif
869880

870881
#if DEBUG
871882
debug?.onEvent(.renderDidUpdateClippingBehavior(clipsToBounds: clipsToBounds))

ComposeUI/Tests/ComposeUITests/ComposeView/ComposeView+ClippingTests.swift

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class ComposeView_ClippingTests: XCTestCase {
4444
}
4545
contentView.refresh(animated: false)
4646

47-
// should not clip
47+
// should not clip by default (auto mode)
4848
expect(contentView.clipsToBounds) == false
4949
#if canImport(AppKit)
5050
expect(contentView.contentView.clipsToBounds) == false
@@ -53,6 +53,7 @@ class ComposeView_ClippingTests: XCTestCase {
5353
// when set to always clipping
5454
contentView.clippingBehavior = .always
5555
contentView.refresh(animated: false)
56+
// then it should clip
5657
expect(contentView.clipsToBounds) == true
5758
#if canImport(AppKit)
5859
expect(contentView.contentView.clipsToBounds) == true
@@ -61,12 +62,41 @@ class ComposeView_ClippingTests: XCTestCase {
6162
// when set to never clipping
6263
contentView.clippingBehavior = .never
6364
contentView.refresh(animated: false)
65+
// then it should not clip
66+
expect(contentView.clipsToBounds) == false
67+
#if canImport(AppKit)
68+
expect(contentView.contentView.clipsToBounds) == false
69+
#endif
70+
71+
// when set to manual mode
72+
contentView.clippingBehavior = .manual
73+
contentView.refresh(animated: false)
74+
// then it should not change
75+
expect(contentView.clipsToBounds) == false
76+
#if canImport(AppKit)
77+
expect(contentView.contentView.clipsToBounds) == false
78+
#endif
79+
80+
// when manually flip the clipsToBounds value
81+
contentView.clipsToBounds = true
82+
contentView.refresh(animated: false)
83+
// then it should follow the manual setting
84+
expect(contentView.clipsToBounds) == true
85+
#if canImport(AppKit)
86+
expect(contentView.contentView.clipsToBounds) == true
87+
#endif
88+
89+
// when manually set the clipsToBounds again
90+
contentView.clipsToBounds = false
91+
contentView.refresh(animated: false)
92+
// then it should follow the manual setting
6493
expect(contentView.clipsToBounds) == false
6594
#if canImport(AppKit)
6695
expect(contentView.contentView.clipsToBounds) == false
6796
#endif
6897
}
6998

99+
// when content size is equal to bounds size
70100
do {
71101
let contentView = ComposeView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
72102
contentView.setContent {
@@ -75,7 +105,7 @@ class ComposeView_ClippingTests: XCTestCase {
75105
}
76106
contentView.refresh(animated: false)
77107

78-
// should not clip
108+
// should not clip by default (auto mode)
79109
expect(contentView.clipsToBounds) == false
80110
#if canImport(AppKit)
81111
expect(contentView.contentView.clipsToBounds) == false
@@ -84,6 +114,7 @@ class ComposeView_ClippingTests: XCTestCase {
84114
// when set to always clipping
85115
contentView.clippingBehavior = .always
86116
contentView.refresh(animated: false)
117+
// then it should clip
87118
expect(contentView.clipsToBounds) == true
88119
#if canImport(AppKit)
89120
expect(contentView.contentView.clipsToBounds) == true
@@ -92,6 +123,34 @@ class ComposeView_ClippingTests: XCTestCase {
92123
// when set to never clipping
93124
contentView.clippingBehavior = .never
94125
contentView.refresh(animated: false)
126+
// then it should not clip
127+
expect(contentView.clipsToBounds) == false
128+
#if canImport(AppKit)
129+
expect(contentView.contentView.clipsToBounds) == false
130+
#endif
131+
132+
// when set to manual mode
133+
contentView.clippingBehavior = .manual
134+
contentView.refresh(animated: false)
135+
// then it should not change
136+
expect(contentView.clipsToBounds) == false
137+
#if canImport(AppKit)
138+
expect(contentView.contentView.clipsToBounds) == false
139+
#endif
140+
141+
// when manually flip the clipsToBounds value
142+
contentView.clipsToBounds = true
143+
contentView.refresh(animated: false)
144+
// then it should follow the manual setting
145+
expect(contentView.clipsToBounds) == true
146+
#if canImport(AppKit)
147+
expect(contentView.contentView.clipsToBounds) == true
148+
#endif
149+
150+
// when manually set the clipsToBounds again
151+
contentView.clipsToBounds = false
152+
contentView.refresh(animated: false)
153+
// then it should follow the manual setting
95154
expect(contentView.clipsToBounds) == false
96155
#if canImport(AppKit)
97156
expect(contentView.contentView.clipsToBounds) == false
@@ -107,7 +166,7 @@ class ComposeView_ClippingTests: XCTestCase {
107166
}
108167
contentView.refresh(animated: false)
109168

110-
// should clip
169+
// should clip by default (auto mode)
111170
expect(contentView.clipsToBounds) == true
112171
#if canImport(AppKit)
113172
expect(contentView.contentView.clipsToBounds) == true
@@ -116,6 +175,7 @@ class ComposeView_ClippingTests: XCTestCase {
116175
// when set to always clipping
117176
contentView.clippingBehavior = .always
118177
contentView.refresh(animated: false)
178+
// then it should clip
119179
expect(contentView.clipsToBounds) == true
120180
#if canImport(AppKit)
121181
expect(contentView.contentView.clipsToBounds) == true
@@ -124,6 +184,34 @@ class ComposeView_ClippingTests: XCTestCase {
124184
// when set to never clipping
125185
contentView.clippingBehavior = .never
126186
contentView.refresh(animated: false)
187+
// then it should not clip
188+
expect(contentView.clipsToBounds) == false
189+
#if canImport(AppKit)
190+
expect(contentView.contentView.clipsToBounds) == false
191+
#endif
192+
193+
// when set to manual mode
194+
contentView.clippingBehavior = .manual
195+
contentView.refresh(animated: false)
196+
// then it should not change
197+
expect(contentView.clipsToBounds) == false
198+
#if canImport(AppKit)
199+
expect(contentView.contentView.clipsToBounds) == false
200+
#endif
201+
202+
// when manually flip the clipsToBounds value
203+
contentView.clipsToBounds = true
204+
contentView.refresh(animated: false)
205+
// then it should follow the manual setting
206+
expect(contentView.clipsToBounds) == true
207+
#if canImport(AppKit)
208+
expect(contentView.contentView.clipsToBounds) == true
209+
#endif
210+
211+
// when manually set the clipsToBounds again
212+
contentView.clipsToBounds = false
213+
contentView.refresh(animated: false)
214+
// then it should follow the manual setting
127215
expect(contentView.clipsToBounds) == false
128216
#if canImport(AppKit)
129217
expect(contentView.contentView.clipsToBounds) == false

0 commit comments

Comments
 (0)