-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d969cf6
commit f715337
Showing
3 changed files
with
74 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
|
||
struct HStack { | ||
private let children: [any View] | ||
@Mutable private var sizes: [Size] = [] | ||
} | ||
|
||
extension HStack: Builtin, View { | ||
|
||
func size( | ||
for proposal: ProposedSize, | ||
environment: EnvironmentValues | ||
) -> Size { | ||
|
||
let flexibility = children.map { child in | ||
let min = ProposedSize(width: 0, height: proposal.height) | ||
let max = ProposedSize(width: .max, height: proposal.height) | ||
let smallest = child._size(for: min) | ||
let largest = child._size(for: max) | ||
return largest.width - smallest.width | ||
} | ||
|
||
var indices = children.indices | ||
.sorted { flexibility[$0] < flexibility[$1] } | ||
|
||
sizes = Array(repeating: .zero, count: children.count) | ||
var remaining = proposal.width | ||
|
||
while !indices.isEmpty { | ||
|
||
let proposal = ProposedSize( | ||
width: remaining / indices.count, | ||
height: proposal.height) | ||
|
||
let index = indices.removeFirst() | ||
|
||
let child = children[index] | ||
let size = child._size(for: proposal, environment: environment) | ||
sizes[index] = size | ||
remaining -= size.width | ||
if remaining < 0 { remaining = 0 } | ||
} | ||
|
||
let width = sizes.map(\.width).reduce(0, +) | ||
let height = sizes.map(\.height).reduce(0, +) | ||
return Size(width: width, height: height) | ||
} | ||
|
||
func render( | ||
in canvas: any Canvas, | ||
size: Size, | ||
environment: EnvironmentValues | ||
) { | ||
var x: Horizontal = 0 | ||
for (child, childSize) in zip(children, sizes) { | ||
let canvas = canvas.translateBy(x: x, y: 0) | ||
child._render(in: canvas, size: childSize, environment: environment) | ||
x += childSize.width | ||
} | ||
} | ||
} | ||
|
||
extension Horizontal { | ||
fileprivate static var max: Self { Self(Int.max) } | ||
} | ||
|
||
extension Size { | ||
fileprivate static var zero: Size { Size(width: 0, height: 0) } | ||
} |
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 was deleted.
Oops, something went wrong.