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

Make rect(toFit:...) and rect(toFill:...) have similar API surface #144

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
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
55 changes: 14 additions & 41 deletions Paralayout/AspectRatio.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,22 +219,20 @@ public struct AspectRatio: Comparable, CustomDebugStringConvertible, Sendable {
/// - parameter rect: The bounding rect.
/// - parameter position: The location within the bounding rect for the new rect, determining where margin(s) will
/// be if the aspect ratios do not match perfectly.
/// - parameter scaleFactor: The view/window/screen to use for pixel alignment.
/// - parameter layoutDirection: The effective layout direction of the view in which the `rect` is defined.
/// - parameter context: The view/window/screen that provides the scale factor and effective layout direction in
/// which the rect should be positioned.
/// - returns: A rect with the receiver's aspect ratio, strictly within the bounding rect.
@MainActor
public func rect(
toFit rect: CGRect,
at position: Position,
in scaleFactor: ScaleFactorProviding,
layoutDirection: UIUserInterfaceLayoutDirection
in context: (ScaleFactorProviding & LayoutDirectionProviding)
) -> CGRect {
CGRect(
size: size(toFit: rect.size, in: scaleFactor),
self.rect(
toFit: rect,
at: position,
of: rect,
in: scaleFactor,
layoutDirection: layoutDirection
in: context.pixelsPerPoint,
layoutDirection: context.effectiveUserInterfaceLayoutDirection
)
}

Expand Down Expand Up @@ -302,22 +300,20 @@ public struct AspectRatio: Comparable, CustomDebugStringConvertible, Sendable {
/// - parameter rect: The bounding rect.
/// - parameter position: The location within the bounding rect for the new rect, determining where margin(s) will
/// be if the aspect ratios do not match perfectly.
/// - parameter scaleFactor: The view/window/screen to use for pixel alignment.
/// - parameter layoutDirection: The effective layout direction of the view in which the `rect` is defined.
/// - parameter context: The view/window/screen that provides the scale factor and effective layout direction in
/// which the rect should be positioned.
/// - returns: A rect with the receiver's aspect ratio, strictly containing the bounding rect.
@MainActor
public func rect(
toFill rect: CGRect,
at position: Position,
in scaleFactor: ScaleFactorProviding,
layoutDirection: UIUserInterfaceLayoutDirection
in context: (ScaleFactorProviding & LayoutDirectionProviding)
) -> CGRect {
CGRect(
size: size(toFill: rect.size, in: scaleFactor),
self.rect(
toFill: rect,
at: position,
of: rect,
in: scaleFactor,
layoutDirection: layoutDirection
in: context.pixelsPerPoint,
layoutDirection: context.effectiveUserInterfaceLayoutDirection
)
}

Expand Down Expand Up @@ -345,29 +341,6 @@ public struct AspectRatio: Comparable, CustomDebugStringConvertible, Sendable {
)
}

/// An "aspect-fill" function that determines the smallest rect of the receiver's aspect ratio that fits a rect
/// within it.
///
/// - parameter rect: The bounding rect.
/// - parameter position: The location within the bounding rect for the new rect, determining where margin(s) will
/// be if the aspect ratios do not match perfectly.
/// - parameter context: The view/window/screen that provides the scale factor and effective layout direction in
/// which the rect should be positioned.
/// - returns: A rect with the receiver's aspect ratio, strictly containing the bounding rect.
@MainActor
public func rect(
toFill rect: CGRect,
at position: Position,
in context: (ScaleFactorProviding & LayoutDirectionProviding)
) -> CGRect {
self.rect(
toFill: rect,
at: position,
in: context,
layoutDirection: context.effectiveUserInterfaceLayoutDirection
)
}

}

// MARK: -
Expand Down
4 changes: 2 additions & 2 deletions ParalayoutTests/AspectRatioTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ final class AspectRatioTests: XCTestCase {

// An AspectRatio's size that fits a rect of the same aspect ratio should also be the same as the size of
// that rect.
let rectToFit = ratio.rect(toFit: rect, at: position, in: ScaleFactorProvider(scale), layoutDirection: layoutDirection)
let rectToFit = ratio.rect(toFit: rect, at: position, in: scale, layoutDirection: layoutDirection)
XCTAssert(rectToFit.size == ratio.size(toFit: rect.size, in: scale))

// The rect needs to be positioned as requested (within a pixel).
Expand Down Expand Up @@ -170,7 +170,7 @@ final class AspectRatioTests: XCTestCase {
let rectToFill = ratio.rect(
toFill: rect,
at: position,
in: ScaleFactorProvider(scale),
in: scale,
layoutDirection: layoutDirection
)
XCTAssert(rectToFill.size == ratio.size(toFill: rect.size, in: scale))
Expand Down
28 changes: 0 additions & 28 deletions ParalayoutTests/ScaleFactorProvider.swift

This file was deleted.