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

Add ImageSize #23929

Merged
merged 2 commits into from
Dec 30, 2024
Merged

Add ImageSize #23929

merged 2 commits into from
Dec 30, 2024

Conversation

kean
Copy link
Contributor

@kean kean commented Dec 30, 2024

Adds ImageSize to clarify that ImageDownloader uses pixels as a parameter and make sure you can't blindly pass something like view.frame.size to in, which is incorrect.

This PR also adds ImageRequest support to AsyncImageView.

Regression Notes

  1. Potential unintended areas of impact

  2. What I did to test those areas of impact (or what existing automated tests I relied on)

  3. What automated tests I added (or what prevented me from doing so)

PR submission checklist:

  • I have completed the Regression Notes.
  • I have considered adding unit tests for my changes.
  • I have considered adding accessibility improvements for my changes.
  • I have considered if this change warrants user-facing release notes and have added them to RELEASE-NOTES.txt if necessary.

Testing checklist:

  • WordPress.com sites and self-hosted Jetpack sites.
  • Portrait and landscape orientations.
  • Light and dark modes.
  • Fonts: Larger, smaller and bold text.
  • High contrast.
  • VoiceOver.
  • Languages with large words or with letters/accents not frequently used in English.
  • Right-to-left languages. (Even if translation isn’t complete, formatting should still respect the right-to-left layout)
  • iPhone and iPad.
  • Multi-tasking: Split view and Slide over. (iPad)

@kean kean merged commit bc6f5ec into christmas-feature-branch Dec 30, 2024
1 of 18 checks passed
@kean kean deleted the task/optimize-image-prefetcher branch December 30, 2024 21:33
@dangermattic
Copy link
Collaborator

1 Error
🚫 PR requires at least one label.
1 Warning
⚠️ View files have been modified, but no screenshot or video is included in the pull request. Consider adding some for clarity.

Generated by 🚫 Danger

@kean kean mentioned this pull request Dec 30, 2024
14 tasks
@wpmobilebot
Copy link
Contributor

WordPress Alpha📲 You can test the changes from this Pull Request in WordPress Alpha by scanning the QR code below to install the corresponding build.
App NameWordPress Alpha WordPress Alpha
ConfigurationRelease-Alpha
Build Numberpr23929-3b036a0
Version25.6
Bundle IDorg.wordpress.alpha
Commit3b036a0
App Center BuildWPiOS - One-Offs #11237
Automatticians: You can use our internal self-serve MC tool to give yourself access to App Center if needed.

@wpmobilebot
Copy link
Contributor

Jetpack Alpha📲 You can test the changes from this Pull Request in Jetpack Alpha by scanning the QR code below to install the corresponding build.
App NameJetpack Alpha Jetpack Alpha
ConfigurationRelease-Alpha
Build Numberpr23929-3b036a0
Version25.6
Bundle IDcom.jetpack.alpha
Commit3b036a0
App Center Buildjetpack-installable-builds #10275
Automatticians: You can use our internal self-serve MC tool to give yourself access to App Center if needed.

/// Image size in **pixels**.
public struct ImageSize: Hashable, Sendable {
public let width: CGFloat
public let height: CGFloat
Copy link
Contributor

Choose a reason for hiding this comment

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

Should these properties be Int? There is no decimal pixel, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated in the upcoming PR.

self.height = height
}

public init(_ size: CGSize) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you thinking changing this API to init(pixels:)? IMHO, the name init(pixels:) matches the init(scaling...) ones to make it a bit clearer that this API accepts pixels sizes, and the "scaling" ones accept points sizes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense; updating in the upcoming PR.

/// Initializes `ImageSize` with the given size scaled for the current trait
/// collection display scale.
public init(scaling size: CGSize) {
self.init(size.scaled(by: UITraitCollection.current.displayScale))
Copy link
Contributor

Choose a reason for hiding this comment

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

Using UITraitCollection.current.displayScale as the default scale (even though that may be the correct value 90% of the time) may lead to misuse. What do you think about adding a non-optional scale argument here? Maybe, even include "points" in the name: ImageSize(points:scale:)?

Of course, you can add some convenient methods on top of it, like ImageSize(points: CGSize, in: UITraitCollection), to make it easier to use the app wide scale easier: ImageSize(points: ..., in: .current)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, you can misuse current. I'm not entirely sure I understand how it works. I'm chasing it to the following:

    /// A convenience initializer that creates `ImageSize` with the given size
    /// in **points** scaled for the given view.
    @MainActor
    public init(scaling size: CGSize, in view: UIView) {
        self.init(scaling: size, scale: view.traitCollection.displayScale)
    }

    /// Initializes `ImageSize` with the given size in **points** scaled for the
    /// current trait collection display scale.
    public init(scaling size: CGSize, scale: CGFloat) {
        self.init(pixels: size.scaled(by: max(1, scale)))
    }
  • It doesn't use current anymore
  • It enforces you to either provide a view (always has a valid trait collection) or a scale exclicitly
  • Add max(1, scale) just in case
  • Add documentation (**points**)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants