Browse Source

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
pull/2183/head
Ilya Ryzhenkov 2 years ago committed by GitHub
parent
commit
010576557f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 51
      components/SplitPane/library/src/commonMain/kotlin/org/jetbrains/compose/splitpane/SplitPane.kt
  2. 23
      components/SplitPane/library/src/desktopMain/kotlin/org/jetbrains/compose/splitpane/DesktopSplitPane.kt

51
components/SplitPane/library/src/commonMain/kotlin/org/jetbrains/compose/splitpane/SplitPane.kt

@ -28,20 +28,15 @@ fun VerticalSplitPane(
content: SplitPaneScope.() -> Unit content: SplitPaneScope.() -> Unit
) { ) {
with(SplitPaneScopeImpl(isHorizontal = false, splitPaneState).apply(content)) { with(SplitPaneScopeImpl(isHorizontal = false, splitPaneState).apply(content)) {
if (firstPlaceableContent != null && secondPlaceableContent != null) { SplitPane(
SplitPane( modifier = modifier,
modifier = modifier, isHorizontal = false,
isHorizontal = false, splitPaneState = splitPaneState,
splitPaneState = splitPaneState, minimalSizesConfiguration = minimalSizes,
minimalSizesConfiguration = minimalSizes, first = firstPlaceableContent,
first = firstPlaceableContent!!, second = secondPlaceableContent,
second = secondPlaceableContent!!, splitter = splitter
splitter = splitter )
)
} else {
firstPlaceableContent?.invoke()
secondPlaceableContent?.invoke()
}
} }
} }
@ -63,20 +58,15 @@ fun HorizontalSplitPane(
content: SplitPaneScope.() -> Unit content: SplitPaneScope.() -> Unit
) { ) {
with(SplitPaneScopeImpl(isHorizontal = true, splitPaneState).apply(content)) { with(SplitPaneScopeImpl(isHorizontal = true, splitPaneState).apply(content)) {
if (firstPlaceableContent != null && secondPlaceableContent != null) { SplitPane(
SplitPane( modifier = modifier,
modifier = modifier, isHorizontal = true,
isHorizontal = true, splitPaneState = splitPaneState,
splitPaneState = splitPaneState, minimalSizesConfiguration = minimalSizes,
minimalSizesConfiguration = minimalSizes, first = firstPlaceableContent!!,
first = firstPlaceableContent!!, second = secondPlaceableContent!!,
second = secondPlaceableContent!!, splitter = splitter
splitter = splitter )
)
} else {
firstPlaceableContent?.invoke()
secondPlaceableContent?.invoke()
}
} }
} }
@ -105,12 +95,13 @@ internal expect fun defaultSplitter(
* @param splitter separator composable, by default [Splitter] is used * @param splitter separator composable, by default [Splitter] is used
* */ * */
@Composable @Composable
@OptIn(ExperimentalSplitPaneApi::class)
internal expect fun SplitPane( internal expect fun SplitPane(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
isHorizontal: Boolean = true, isHorizontal: Boolean = true,
splitPaneState: SplitPaneState, splitPaneState: SplitPaneState,
minimalSizesConfiguration: MinimalSizes = MinimalSizes(0.dp, 0.dp), minimalSizesConfiguration: MinimalSizes = MinimalSizes(0.dp, 0.dp),
first: @Composable () -> Unit, first: (@Composable () -> Unit)?,
second: @Composable () -> Unit, second: (@Composable () -> Unit)?,
splitter: Splitter splitter: Splitter
) )

23
components/SplitPane/library/src/desktopMain/kotlin/org/jetbrains/compose/splitpane/DesktopSplitPane.kt

@ -19,10 +19,16 @@ internal actual fun SplitPane(
isHorizontal: Boolean, isHorizontal: Boolean,
splitPaneState: SplitPaneState, splitPaneState: SplitPaneState,
minimalSizesConfiguration: MinimalSizes, minimalSizesConfiguration: MinimalSizes,
first: @Composable () -> Unit, first: (@Composable () -> Unit)?,
second: @Composable () -> Unit, second: (@Composable () -> Unit)?,
splitter: Splitter splitter: Splitter
) { ) {
if (first == null || second == null) {
first?.let { Box(modifier) { it() } }
second?.let { Box(modifier) { it() } }
return
}
Layout( Layout(
{ {
Box { Box {
@ -41,12 +47,13 @@ internal actual fun SplitPane(
modifier, modifier,
) { measurables, constraints -> ) { measurables, constraints ->
with(minimalSizesConfiguration) { with(minimalSizesConfiguration) {
with(splitPaneState) { val firstMinSizePx = firstPlaceableMinimalSize.value * density
val secondMinSizePx = secondPlaceableMinimalSize.value * density
val constrainedMin = constraints.minByDirection(isHorizontal) + firstPlaceableMinimalSize.value
with(splitPaneState) {
val constrainedMin = constraints.minByDirection(isHorizontal) + firstMinSizePx
val constrainedMax = val constrainedMax =
(constraints.maxByDirection(isHorizontal).toFloat() - secondPlaceableMinimalSize.value).let { (constraints.maxByDirection(isHorizontal).toFloat() - secondMinSizePx).let {
if (it <= 0 || it <= constrainedMin) { if (it <= 0 || it <= constrainedMin) {
constraints.maxByDirection(isHorizontal).toFloat() constraints.maxByDirection(isHorizontal).toFloat()
} else { } else {
@ -60,7 +67,7 @@ internal actual fun SplitPane(
if (maxPosition != constrainedMax) { if (maxPosition != constrainedMax) {
maxPosition = maxPosition =
if ((firstPlaceableMinimalSize + secondPlaceableMinimalSize).value < constraints.maxByDirection(isHorizontal)) { if (firstMinSizePx + secondMinSizePx < constraints.maxByDirection(isHorizontal)) {
constrainedMax constrainedMax
} else { } else {
minPosition minPosition
@ -68,7 +75,7 @@ internal actual fun SplitPane(
} }
val constrainedPosition = val constrainedPosition =
(constraints.maxByDirection(isHorizontal) - (firstPlaceableMinimalSize + secondPlaceableMinimalSize).value).let { (constraints.maxByDirection(isHorizontal) - (firstMinSizePx + secondMinSizePx)).let {
if (it > 0f) { if (it > 0f) {
(it * positionPercentage).coerceIn(constrainedMin, constrainedMax).roundToInt() (it * positionPercentage).coerceIn(constrainedMin, constrainedMax).roundToInt()
} else { } else {

Loading…
Cancel
Save