-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Image sizing to respect aspect ratio if possible
- Loading branch information
Showing
4 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// File.swift | ||
// DataCachingTest.swift | ||
// UIComponent | ||
// | ||
// Created by Luke Zhao on 11/8/24. | ||
|
This file contains 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,37 @@ | ||
// | ||
// SizingTest.swift | ||
// UIComponent | ||
// | ||
// Created by Luke Zhao on 11/8/24. | ||
// | ||
|
||
import Testing | ||
@testable import UIComponent | ||
import UIKit | ||
|
||
public struct AspectSpace: Component { | ||
let size: CGSize | ||
|
||
public init(width: CGFloat = 0, height: CGFloat = 0) { | ||
size = CGSize(width: width, height: height) | ||
} | ||
|
||
public func layout(_ constraint: Constraint) -> some RenderNode { | ||
SpaceRenderNode(size: size.boundWithAspectRatio(to: constraint)) | ||
} | ||
} | ||
|
||
@Suite("Sizing") | ||
@MainActor | ||
struct SizingTest { | ||
|
||
@Test func testAspectRatioSizing() throws { | ||
let base = AspectSpace(width: 100, height: 100) | ||
#expect(base.layout(Constraint(maxSize: CGSize(width: 200.0, height: .infinity))).size == CGSize(width: 100, height: 100)) | ||
#expect(base.layout(Constraint(maxSize: CGSize(width: 50.0, height: .infinity))).size == CGSize(width: 50, height: 50)) | ||
#expect(base.layout(Constraint(maxSize: CGSize(width: 50.0, height: 40.0))).size == CGSize(width: 40, height: 40)) | ||
|
||
#expect(base.size(width: .fill).layout(Constraint(maxSize: CGSize(width: 200.0, height: .infinity))).size == CGSize(width: 200, height: 200)) | ||
#expect(base.size(width: .fill).layout(Constraint(maxSize: CGSize(width: 50.0, height: .infinity))).size == CGSize(width: 50, height: 50)) | ||
} | ||
} |