Skip to content
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

Get scale factor for view from trait collection instead of screen #110

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Paralayout/PixelRounding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,17 @@ extension UIScreen: ScaleFactorProviding {
extension UIView: ScaleFactorProviding {

public var pixelsPerPoint: CGFloat {
let scaleFromTraits = traitCollection.displayScale

// The trait collection is the authority for display scale, but sometimes the trait collection does not have a
// value for the scale, in which case it will return 0, breaking all the things.
if scaleFromTraits > 0 {
return scaleFromTraits
}

#if os(iOS)
return (window?.screen ?? UIScreen.main).pixelsPerPoint
#endif
Comment on lines +45 to +47
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This OS check is a precursor to #111, where we can't use UIScreen

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might also be able to fall back to window?.traitCollection ?? UITraitCollection.current as an alternative way to get the scale instead of UIScreen

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's in a window, it should get the traits from the window automatically, so I don't think the first part is necessary. Falling back to the current global trait collection is a really interesting idea though. I think in practice it would probably be safe to do so, but UITraitCollection.current has some really weird behavior around when it's valid to read, so I'm a bit hesitant to use it here. I think for now using UIScreen is safer (and should give the same value).

}

}
Expand Down