From 010576557f828f5749f9672541335d216d8f2f79 Mon Sep 17 00:00:00 2001 From: Ilya Ryzhenkov Date: Thu, 7 Jul 2022 20:10:04 +0300 Subject: [PATCH] Fix split panel (#2175) * Fix ignoring modifier when one of the parts is missing * Fix ignoring density for min size when measuring * Fix a copypasta for secondMinSizePx --- .../jetbrains/compose/splitpane/SplitPane.kt | 51 ++++++++----------- .../compose/splitpane/DesktopSplitPane.kt | 23 ++++++--- 2 files changed, 36 insertions(+), 38 deletions(-) diff --git a/components/SplitPane/library/src/commonMain/kotlin/org/jetbrains/compose/splitpane/SplitPane.kt b/components/SplitPane/library/src/commonMain/kotlin/org/jetbrains/compose/splitpane/SplitPane.kt index e660cdced8..8691dff75b 100644 --- a/components/SplitPane/library/src/commonMain/kotlin/org/jetbrains/compose/splitpane/SplitPane.kt +++ b/components/SplitPane/library/src/commonMain/kotlin/org/jetbrains/compose/splitpane/SplitPane.kt @@ -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 + ) } } @@ -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 + ) } } @@ -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 ) \ No newline at end of file diff --git a/components/SplitPane/library/src/desktopMain/kotlin/org/jetbrains/compose/splitpane/DesktopSplitPane.kt b/components/SplitPane/library/src/desktopMain/kotlin/org/jetbrains/compose/splitpane/DesktopSplitPane.kt index 40f25249ae..50b372003b 100644 --- a/components/SplitPane/library/src/desktopMain/kotlin/org/jetbrains/compose/splitpane/DesktopSplitPane.kt +++ b/components/SplitPane/library/src/desktopMain/kotlin/org/jetbrains/compose/splitpane/DesktopSplitPane.kt @@ -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 { @@ -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 { @@ -60,7 +67,7 @@ internal actual fun SplitPane( if (maxPosition != constrainedMax) { maxPosition = - if ((firstPlaceableMinimalSize + secondPlaceableMinimalSize).value < constraints.maxByDirection(isHorizontal)) { + if (firstMinSizePx + secondMinSizePx < constraints.maxByDirection(isHorizontal)) { constrainedMax } else { minPosition @@ -68,7 +75,7 @@ internal actual fun SplitPane( } 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 {