Skip to content

Commit

Permalink
Support .position modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
aabewhite committed Jul 15, 2024
1 parent 5a2af9e commit 05000e9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,10 @@ Support levels:
</details>
</td>
</tr>
<tr>
<td>✅</td>
<td><code>.position</code></td>
</tr>
<tr>
<td>✅</td>
<td>
Expand Down
27 changes: 27 additions & 0 deletions Sources/SkipUI/SkipUI/Compose/ComposeLayouts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,33 @@ import androidx.compose.ui.unit.dp
}
}

/// Layout the given view with the given position.
@Composable func PositionLayout(view: View, x: CGFloat, y: CGFloat, context: ComposeContext) {
PositionLayout(x: x, y: y, context: context) { view.Compose($0) }
}

@Composable func PositionLayout(x: CGFloat, y: CGFloat, context: ComposeContext, target: @Composable (ComposeContext) -> Void) {
// SwiftUI expands to fill the available space and places within that
Box(modifier: context.modifier.fillSize(expandContainer: false)) {
let density = LocalDensity.current
let xPx = with(density) { x.dp.roundToPx() }
let yPx = with(density) { y.dp.roundToPx() }
Layout(content = {
target(context.content())
}) { measurables, constraints in
guard !measurables.isEmpty() else {
return layout(width: 0, height: 0) {}
}
let targetPlaceables = measurables.map { $0.measure(constraints) }
layout(width: targetPlaceables[0].width, height: targetPlaceables[0].height) {
for targetPlaceable in targetPlaceables {
targetPlaceable.placeRelative(x: xPx - targetPlaceable.width / 2, y: yPx - targetPlaceable.height / 2)
}
}
}
}
}

private func constraint(_ value: Int, subtracting: Int) -> Int {
guard value != Int.MAX_VALUE else {
return value
Expand Down
17 changes: 12 additions & 5 deletions Sources/SkipUI/SkipUI/View/View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -822,16 +822,23 @@ extension View {
return self
}

@available(*, unavailable)
public func position(_ position: CGPoint) -> some View {
// NOTE: animatable property
return self
return self.position(x: position.x, y: position.y)
}

@available(*, unavailable)
public func position(x: CGFloat = 0.0, y: CGFloat = 0.0) -> some View {
// NOTE: animatable
#if SKIP
return ComposeModifierView(contentView: self) { view, context in
let density = LocalDensity.current
let animatable = (Float(x), Float(y)).asAnimatable(context: context)
let positionPx = with(density) {
IntOffset(animatable.value.0.dp.roundToPx(), animatable.value.1.dp.roundToPx())
}
PositionLayout(view: view, x: Double(animatable.value.0), y: Double(animatable.value.1), context: context)
}
#else
return self
#endif
}

@available(*, unavailable)
Expand Down

0 comments on commit 05000e9

Please sign in to comment.