Skip to content

Commit

Permalink
Fix expansion behavior of ZStack content
Browse files Browse the repository at this point in the history
  • Loading branch information
aabewhite committed Mar 19, 2024
1 parent 1d30c54 commit 30f2db6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
14 changes: 11 additions & 3 deletions Sources/SkipUI/SkipUI/Compose/ComposeContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import androidx.compose.ui.Modifier
/// behind our `Modifier.fillWidth` and `Modifier.fillHeight` extension functions.
///
/// Having to explicitly set a certain modifier in order to expand within a parent is problematic for containers that want to
/// fit content. The container only wants to expand if it has content that wants to expand. It cant't know this until it composes
/// fit content. The container only wants to expand if it has content that wants to expand. It can't know this until it composes
/// its content. The code in this function sets triggers on the environment values that we use in 'fillWidth' and 'fillHeight' so
/// that if the container content uses them, the container itself can recompose with the appropriate expansion to match its
/// content. Note that this generally only affects final layout when an expanding child is in a container that is itself in a
Expand Down Expand Up @@ -100,7 +100,11 @@ import androidx.compose.ui.Modifier
isNonExpandingFillWidth.value = true
}
}
return fillModifier ?? Modifier.fillMaxWidth()
if let fillModifier, fillModifier != Modifier {
return fillModifier
} else {
return Modifier.fillMaxWidth()
}
}
$0.set_fillHeight { expandContainer in
let fillModifier = EnvironmentValues.shared._fillHeightModifier
Expand All @@ -115,7 +119,11 @@ import androidx.compose.ui.Modifier
isNonExpandingFillHeight.value = true
}
}
return fillModifier ?? Modifier.fillMaxHeight()
if let fillModifier, fillModifier != Modifier {
return fillModifier
} else {
return Modifier.fillMaxHeight()
}
}
} in: {
// Render the container content with the above environment setup
Expand Down
34 changes: 23 additions & 11 deletions Sources/SkipUI/SkipUI/Containers/ZStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ public struct ZStack : View {
let contentContext = context.content()
ComposeContainer(eraseAxis: true, modifier: context.modifier) { modifier in
Box(modifier: modifier, contentAlignment: alignment.asComposeAlignment()) {
views.forEach { $0.Compose(context: contentContext) }
EnvironmentValues.shared.setValues {
// The ComposeContainer uses the presence of these modifiers to influence container expansion behavior
$0.set_fillWidthModifier(Modifier)
$0.set_fillHeightModifier(Modifier)
} in: {
views.forEach { $0.Compose(context: contentContext) }
}
}
}
} else {
Expand All @@ -62,17 +68,23 @@ public struct ZStack : View {
rememberedNewIds.clear()
}
Box(contentAlignment: alignment.asComposeAlignment()) {
for view in state {
let id = idMap(view)
var modifier: Modifier = Modifier
if let animation, newIds.contains(id) || rememberedNewIds.contains(id) || !ids.contains(id) {
let transition = TransitionModifierView.transition(for: view) ?? OpacityTransition.shared
let spec = animation.asAnimationSpec()
let enter = transition.asEnterTransition(spec: spec)
let exit = transition.asExitTransition(spec: spec)
modifier = modifier.animateEnterExit(enter: enter, exit: exit)
EnvironmentValues.shared.setValues {
// The ComposeContainer uses the presence of these modifiers to influence container expansion behavior
$0.set_fillWidthModifier(Modifier)
$0.set_fillHeightModifier(Modifier)
} in: {
for view in state {
let id = idMap(view)
var modifier: Modifier = Modifier
if let animation, newIds.contains(id) || rememberedNewIds.contains(id) || !ids.contains(id) {
let transition = TransitionModifierView.transition(for: view) ?? OpacityTransition.shared
let spec = animation.asAnimationSpec()
let enter = transition.asEnterTransition(spec: spec)
let exit = transition.asExitTransition(spec: spec)
modifier = modifier.animateEnterExit(enter: enter, exit: exit)
}
view.Compose(context: context.content(modifier: modifier))
}
view.Compose(context: context.content(modifier: modifier))
}
}
}, label: "ZStack")
Expand Down

0 comments on commit 30f2db6

Please sign in to comment.