Repository navigation
Keep list separators out of the side safe area #26220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
Modules/Sources/WordPressUI/Extensions/UIView+SeparatorInsets.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import UIKit | ||
|
|
||
| extension UIView { | ||
| /// Returns a `separatorInset` for a table view or cell that is measured from the edges of the | ||
| /// view's safe area instead of the view's own edges. | ||
| /// | ||
| /// Setting an explicit `separatorInset` replaces the inset UIKit picks by default, which keeps | ||
| /// the separator out of the side safe areas. Without this, the separator runs under the iPhone | ||
| /// Duo's side strip, or under the sensor housing in landscape. | ||
| /// | ||
| /// A separator inset follows the layout direction (its `left` is the leading side), but safe | ||
| /// area insets don't, so they are swapped in right-to-left layouts. | ||
| public func separatorInsets(leading: CGFloat, trailing: CGFloat = 0) -> UIEdgeInsets { | ||
| let isRightToLeft = effectiveUserInterfaceLayoutDirection == .rightToLeft | ||
| let leadingSafeArea = isRightToLeft ? safeAreaInsets.right : safeAreaInsets.left | ||
| let trailingSafeArea = isRightToLeft ? safeAreaInsets.left : safeAreaInsets.right | ||
| return UIEdgeInsets(top: 0, left: leading + leadingSafeArea, bottom: 0, right: trailing + trailingSafeArea) | ||
| } | ||
| } | ||
43 changes: 43 additions & 0 deletions
43
Modules/Tests/WordPressUIUnitTests/Extensions/UIView+SeparatorInsetsTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import Testing | ||
| import UIKit | ||
| import WordPressUI | ||
|
|
||
| @MainActor | ||
| struct UIViewSeparatorInsetsTests { | ||
| @Test func withoutSideSafeAreasTheInsetsAreUnchanged() { | ||
| let view = SafeAreaView(safeAreaInsets: .zero) | ||
|
|
||
| #expect(view.separatorInsets(leading: 68) == UIEdgeInsets(top: 0, left: 68, bottom: 0, right: 0)) | ||
| } | ||
|
|
||
| @Test func leftToRightAddsEachSideSafeArea() { | ||
| let view = SafeAreaView(safeAreaInsets: UIEdgeInsets(top: 82, left: 10, bottom: 34, right: 84)) | ||
|
|
||
| #expect(view.separatorInsets(leading: 68, trailing: 4) == UIEdgeInsets(top: 0, left: 78, bottom: 0, right: 88)) | ||
| } | ||
|
|
||
| @Test func rightToLeftSwapsTheSafeAreaSides() { | ||
| let view = SafeAreaView(safeAreaInsets: UIEdgeInsets(top: 82, left: 10, bottom: 34, right: 84)) | ||
| view.semanticContentAttribute = .forceRightToLeft | ||
|
|
||
| // The leading side is on the right, where the 84 pt inset is. | ||
| #expect(view.separatorInsets(leading: 68, trailing: 4) == UIEdgeInsets(top: 0, left: 152, bottom: 0, right: 14)) | ||
| } | ||
| } | ||
|
|
||
| private final class SafeAreaView: UIView { | ||
| private let stubbedSafeAreaInsets: UIEdgeInsets | ||
|
|
||
| init(safeAreaInsets: UIEdgeInsets) { | ||
| self.stubbedSafeAreaInsets = safeAreaInsets | ||
| super.init(frame: CGRect(x: 0, y: 0, width: 466, height: 678)) | ||
| } | ||
|
|
||
| required init?(coder: NSCoder) { | ||
| fatalError("init(coder:) has not been implemented") | ||
| } | ||
|
|
||
| override var safeAreaInsets: UIEdgeInsets { | ||
| stubbedSafeAreaInsets | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: probably can add a note to say using NSDirectionalEdgeInsets once the app's min os is 26.