Skip to content

Commit fc230ae

Browse files
committed
example: make the VerticalDivider in widescreen draggable
1 parent e944337 commit fc230ae

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

example/src/commonMain/kotlin/UITest.kt

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import androidx.compose.animation.fadeIn
99
import androidx.compose.animation.fadeOut
1010
import androidx.compose.animation.shrinkHorizontally
1111
import androidx.compose.animation.shrinkVertically
12+
import androidx.compose.foundation.gestures.Orientation
13+
import androidx.compose.foundation.gestures.draggable
14+
import androidx.compose.foundation.gestures.rememberDraggableState
1215
import androidx.compose.foundation.layout.Arrangement
1316
import androidx.compose.foundation.layout.Box
1417
import androidx.compose.foundation.layout.BoxWithConstraints
@@ -38,6 +41,7 @@ import androidx.compose.runtime.CompositionLocalProvider
3841
import androidx.compose.runtime.MutableState
3942
import androidx.compose.runtime.compositionLocalOf
4043
import androidx.compose.runtime.getValue
44+
import androidx.compose.runtime.mutableFloatStateOf
4145
import androidx.compose.runtime.mutableStateOf
4246
import androidx.compose.runtime.remember
4347
import androidx.compose.runtime.rememberCoroutineScope
@@ -49,6 +53,7 @@ import androidx.compose.ui.Modifier
4953
import androidx.compose.ui.graphics.Color
5054
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
5155
import androidx.compose.ui.input.nestedscroll.nestedScroll
56+
import androidx.compose.ui.platform.LocalDensity
5257
import androidx.compose.ui.platform.LocalHapticFeedback
5358
import androidx.compose.ui.platform.LocalLayoutDirection
5459
import androidx.compose.ui.platform.LocalUriHandler
@@ -165,7 +170,10 @@ fun UITest(
165170

166171
val handlePageChange: (Int) -> Unit = remember(pagerState, coroutineScope) {
167172
{ page ->
168-
coroutineScope.launch { pagerState.animateScrollToPage(page) }
173+
coroutineScope.launch {
174+
if (uiState.isWideScreen) pagerState.scrollToPage(page)
175+
else pagerState.animateScrollToPage(page)
176+
}
169177
}
170178
}
171179

@@ -229,12 +237,31 @@ private fun WideScreenLayout(
229237
windowSize: WindowSize,
230238
colorMode: MutableState<Int>
231239
) {
240+
val density = LocalDensity.current
232241
val layoutDirection = LocalLayoutDirection.current
233242

243+
val windowWidth = getWindowSize().width
244+
val defaultWeight = remember(windowWidth, density) {
245+
val minPx = with(density) { UIConstants.WIDE_SCREEN_THRESHOLD.toPx() }
246+
val maxPx = with(density) { 1920.dp.toPx() }
247+
val t = (windowWidth - minPx) / 2500
248+
0.3f - 0.2f * t
249+
}
250+
var weight by remember(windowWidth) { mutableStateOf(defaultWeight) }
251+
var potentialWeight by remember { mutableFloatStateOf(weight) }
252+
val dragState = rememberDraggableState { delta ->
253+
val nextPotentialWeight = potentialWeight + delta / windowWidth
254+
potentialWeight = nextPotentialWeight
255+
val clampedWeight = nextPotentialWeight.coerceIn(0.2f, 0.5f)
256+
if (clampedWeight == nextPotentialWeight) {
257+
weight = clampedWeight
258+
}
259+
}
260+
234261
Scaffold {
235262
val barScrollBehavior = MiuixScrollBehavior()
236263
Row {
237-
Box(modifier = Modifier.weight(0.4f)) {
264+
Box(modifier = Modifier.weight(weight)) {
238265
WideScreenPanel(
239266
barScrollBehavior = barScrollBehavior,
240267
uiState = uiState,
@@ -243,9 +270,14 @@ private fun WideScreenLayout(
243270
)
244271
}
245272
VerticalDivider(
246-
modifier = Modifier.padding(horizontal = 6.dp)
273+
modifier = Modifier
274+
.draggable(
275+
state = dragState,
276+
orientation = Orientation.Horizontal
277+
)
278+
.padding(horizontal = 6.dp)
247279
)
248-
Box(modifier = Modifier.weight(0.6f)) {
280+
Box(modifier = Modifier.weight(1f - weight)) {
249281
WideScreenContent(
250282
navigationItems = navigationItems,
251283
uiState = uiState,

iosApp/iosApp/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<key>CFBundleShortVersionString</key>
1818
<string>1.0.4</string>
1919
<key>CFBundleVersion</key>
20-
<string>532</string>
20+
<string>533</string>
2121
<key>LSRequiresIPhoneOS</key>
2222
<true/>
2323
<key>CADisableMinimumFrameDurationOnPhone</key>

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/basic/ColorPicker.kt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -855,13 +855,7 @@ private fun ColorSlider(
855855
detectHorizontalDragGestures(
856856
onDragStart = { offset ->
857857
val newValue =
858-
handleSliderInteraction(
859-
offset.x,
860-
size.width.toFloat(),
861-
with(density) { sliderHeightDp.toPx() }).coerceIn(
862-
0f,
863-
1f
864-
)
858+
handleSliderInteraction(offset.x, size.width.toFloat(), with(density) { sliderHeightDp.toPx() })
865859
onValueChanged(newValue)
866860
hapticState.reset(newValue)
867861
},

0 commit comments

Comments
 (0)