Skip to content

Commit

Permalink
Fix split panel (#2175)
Browse files Browse the repository at this point in the history
* Fix ignoring modifier when one of the parts is missing

* Fix ignoring density for min size when measuring

* Fix a copypasta for secondMinSizePx
  • Loading branch information
orangy authored Jul 7, 2022
1 parent 7fbbdd8 commit 0105765
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,15 @@ fun VerticalSplitPane(
content: SplitPaneScope.() -> Unit
) {
with(SplitPaneScopeImpl(isHorizontal = false, splitPaneState).apply(content)) {
if (firstPlaceableContent != null && secondPlaceableContent != null) {
SplitPane(
modifier = modifier,
isHorizontal = false,
splitPaneState = splitPaneState,
minimalSizesConfiguration = minimalSizes,
first = firstPlaceableContent!!,
second = secondPlaceableContent!!,
splitter = splitter
)
} else {
firstPlaceableContent?.invoke()
secondPlaceableContent?.invoke()
}
SplitPane(
modifier = modifier,
isHorizontal = false,
splitPaneState = splitPaneState,
minimalSizesConfiguration = minimalSizes,
first = firstPlaceableContent,
second = secondPlaceableContent,
splitter = splitter
)
}
}

Expand All @@ -63,20 +58,15 @@ fun HorizontalSplitPane(
content: SplitPaneScope.() -> Unit
) {
with(SplitPaneScopeImpl(isHorizontal = true, splitPaneState).apply(content)) {
if (firstPlaceableContent != null && secondPlaceableContent != null) {
SplitPane(
modifier = modifier,
isHorizontal = true,
splitPaneState = splitPaneState,
minimalSizesConfiguration = minimalSizes,
first = firstPlaceableContent!!,
second = secondPlaceableContent!!,
splitter = splitter
)
} else {
firstPlaceableContent?.invoke()
secondPlaceableContent?.invoke()
}
SplitPane(
modifier = modifier,
isHorizontal = true,
splitPaneState = splitPaneState,
minimalSizesConfiguration = minimalSizes,
first = firstPlaceableContent!!,
second = secondPlaceableContent!!,
splitter = splitter
)
}

}
Expand Down Expand Up @@ -105,12 +95,13 @@ internal expect fun defaultSplitter(
* @param splitter separator composable, by default [Splitter] is used
* */
@Composable
@OptIn(ExperimentalSplitPaneApi::class)
internal expect fun SplitPane(
modifier: Modifier = Modifier,
isHorizontal: Boolean = true,
splitPaneState: SplitPaneState,
minimalSizesConfiguration: MinimalSizes = MinimalSizes(0.dp, 0.dp),
first: @Composable () -> Unit,
second: @Composable () -> Unit,
first: (@Composable () -> Unit)?,
second: (@Composable () -> Unit)?,
splitter: Splitter
)
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ internal actual fun SplitPane(
isHorizontal: Boolean,
splitPaneState: SplitPaneState,
minimalSizesConfiguration: MinimalSizes,
first: @Composable () -> Unit,
second: @Composable () -> Unit,
first: (@Composable () -> Unit)?,
second: (@Composable () -> Unit)?,
splitter: Splitter
) {
if (first == null || second == null) {
first?.let { Box(modifier) { it() } }
second?.let { Box(modifier) { it() } }
return
}

Layout(
{
Box {
Expand All @@ -41,12 +47,13 @@ internal actual fun SplitPane(
modifier,
) { measurables, constraints ->
with(minimalSizesConfiguration) {
with(splitPaneState) {

val constrainedMin = constraints.minByDirection(isHorizontal) + firstPlaceableMinimalSize.value
val firstMinSizePx = firstPlaceableMinimalSize.value * density
val secondMinSizePx = secondPlaceableMinimalSize.value * density

with(splitPaneState) {
val constrainedMin = constraints.minByDirection(isHorizontal) + firstMinSizePx
val constrainedMax =
(constraints.maxByDirection(isHorizontal).toFloat() - secondPlaceableMinimalSize.value).let {
(constraints.maxByDirection(isHorizontal).toFloat() - secondMinSizePx).let {
if (it <= 0 || it <= constrainedMin) {
constraints.maxByDirection(isHorizontal).toFloat()
} else {
Expand All @@ -60,15 +67,15 @@ internal actual fun SplitPane(

if (maxPosition != constrainedMax) {
maxPosition =
if ((firstPlaceableMinimalSize + secondPlaceableMinimalSize).value < constraints.maxByDirection(isHorizontal)) {
if (firstMinSizePx + secondMinSizePx < constraints.maxByDirection(isHorizontal)) {
constrainedMax
} else {
minPosition
}
}

val constrainedPosition =
(constraints.maxByDirection(isHorizontal) - (firstPlaceableMinimalSize + secondPlaceableMinimalSize).value).let {
(constraints.maxByDirection(isHorizontal) - (firstMinSizePx + secondMinSizePx)).let {
if (it > 0f) {
(it * positionPercentage).coerceIn(constrainedMin, constrainedMax).roundToInt()
} else {
Expand Down

0 comments on commit 0105765

Please sign in to comment.