Skip to content

Commit 549591a

Browse files
committed
Temp: wave start flicker reproducer
1 parent 9e60813 commit 549591a

File tree

3 files changed

+24
-74
lines changed
  • library/src/commonMain/kotlin/ir/mahozad/multiplatform/wavyslider
  • showcase/shared/src/commonMain/kotlin/showcase

3 files changed

+24
-74
lines changed

library/src/commonMain/kotlin/ir/mahozad/multiplatform/wavyslider/Core.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package ir.mahozad.multiplatform.wavyslider
22

33
import androidx.compose.animation.core.*
44
import androidx.compose.runtime.*
5-
import androidx.compose.ui.geometry.*
65
import androidx.compose.ui.geometry.Offset
76
import androidx.compose.ui.graphics.Path
87
import androidx.compose.ui.graphics.drawscope.DrawScope
@@ -142,7 +141,7 @@ internal inline fun animateWaveShift(
142141
val startTime = withFrameNanos { it }
143142
while (true /* Android itself uses true instead of isActive */) {
144143
val playTime = (withFrameNanos { it } - startTime) / 1_000_000_000f
145-
shift.value = startShift + (amount * playTime)
144+
shift.value = (startShift + (amount * playTime)).coerceAtLeast(-10.dp)
146145
}
147146
}
148147
return shift
@@ -183,8 +182,8 @@ internal inline fun DrawScope.createWavyPath(
183182
val waveShiftPx = waveShift.toPx()
184183
val waveLengthPx = waveLength.toPx()
185184
val waveHeightPx = waveHeight.toPx().absoluteValue
186-
val startRadians = waveSpread * (waveShiftPx) / waveLengthPx * (2 * PI)
187-
val startHeightFactor = if (incremental) 0f else 1f
185+
val startRadians = waveSpread * waveShiftPx / waveLengthPx * (2 * PI) //
186+
val startHeightFactor = if (incremental) 0f else 1f //
188187
val startY = (sin(startRadians) * startHeightFactor * waveHeightPx + size.height) / 2
189188
moveTo(startOffset.x, startY.toFloat())
190189
val range = startOffset.x.toInt()..valueOffset.x.toInt()
@@ -193,7 +192,9 @@ internal inline fun DrawScope.createWavyPath(
193192
val radians = waveSpread * (x - range.first + waveShiftPx) / waveLengthPx * (2 * PI)
194193
val y = (sin(radians) * heightFactor * waveHeightPx + size.height) / 2
195194
lineTo(x.toFloat(), y.toFloat())
195+
println("${x.toFloat()} ${y.toFloat()}")
196196
}
197+
println("============================")
197198
}
198199

199200
// Scale x1 from a1..b1 range to a2..b2 range

library/src/commonMain/kotlin/ir/mahozad/multiplatform/wavyslider/material3/WavySlider.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -931,8 +931,8 @@ private inline fun DrawScope.drawTrackActivePart(
931931
} else {
932932
drawPath(
933933
path = createWavyPath(
934-
startOffset, // Do not modify the offset x because it will break wave (the one in demo visual test)
935-
endOffset.copy(x = endOffset.x - trackInsideCornerSize.toPx() / 2),
934+
startOffset.copy(x = startOffset.x + waveThickness.toPx() / 2),
935+
endOffset.copy(x = endOffset.x - waveThickness.toPx() / 2),
936936
waveLength,
937937
waveHeight,
938938
waveSpread,

showcase/shared/src/commonMain/kotlin/showcase/App.kt

Lines changed: 17 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,38 @@
11
package showcase
22

33
import androidx.compose.animation.core.*
4-
import androidx.compose.foundation.background
5-
import androidx.compose.foundation.layout.Box
64
import androidx.compose.foundation.layout.Column
7-
import androidx.compose.foundation.layout.size
85
import androidx.compose.material3.ExperimentalMaterial3Api
96
import androidx.compose.material3.SliderDefaults
10-
import androidx.compose.material3.SliderState
117
import androidx.compose.runtime.*
12-
import androidx.compose.ui.Modifier
13-
import androidx.compose.ui.graphics.Color
8+
import androidx.compose.ui.platform.LocalDensity
9+
import androidx.compose.ui.unit.Density
1410
import androidx.compose.ui.unit.dp
1511
import ir.mahozad.multiplatform.wavyslider.WaveAnimationSpecs
12+
import ir.mahozad.multiplatform.wavyslider.WaveDirection
1613
import ir.mahozad.multiplatform.wavyslider.WaveDirection.RIGHT
1714
import ir.mahozad.multiplatform.wavyslider.material3.Track
18-
import androidx.compose.material.Slider as Slider2
19-
import androidx.compose.material.SliderDefaults as SliderDefaults2
20-
import androidx.compose.material3.Slider as Slider3
15+
import ir.mahozad.multiplatform.wavyslider.material3.WaveAnimationSpecs
2116
import androidx.compose.material3.SliderDefaults as SliderDefaults3
22-
import ir.mahozad.multiplatform.wavyslider.material.WavySlider as WavySlider2
2317
import ir.mahozad.multiplatform.wavyslider.material3.WavySlider as WavySlider3
2418

2519
@OptIn(ExperimentalMaterial3Api::class)
2620
@Composable
2721
fun App() {
28-
val state = remember { SliderState(value = 0.67f) }
29-
Column {
30-
Slider2(value = state.value, onValueChange = { state.value = it })
31-
WavySlider2(
32-
value = state.value,
33-
onValueChange = { state.value = it }
34-
)
35-
WavySlider2(
36-
value = state.value,
37-
onValueChange = { state.value = it },
38-
waveLength = 12.dp,
39-
waveHeight = 24.dp,
40-
waveVelocity = 14.dp to RIGHT,
41-
waveThickness = 1.dp,
42-
trackThickness = 5.dp,
43-
incremental = true,
44-
colors = SliderDefaults2.colors(activeTrackColor = Color.Red)
45-
)
46-
WavySlider2(
47-
value = state.value,
48-
onValueChange = { state.value = it },
49-
waveHeight = 0.dp
50-
)
51-
52-
WavyDivider()
53-
54-
// FIXME: Some of the sliders have broken layout because all the sliders are using the same state instance
55-
// Is it a bug at all or is it intended?
56-
// It happens because there are `state.something = ...` assignments in the material3.WavySlider
57-
58-
Slider3(state)
59-
Slider3(
60-
state = state,
61-
thumb = { Box(modifier = Modifier.size(64.dp).background(Color.Yellow)) }
62-
)
63-
WavySlider3(
64-
state = state,
65-
thumb = { Box(modifier = Modifier.size(64.dp).background(Color.Red)) }
66-
)
67-
WavySlider3(state,
68-
track = {
69-
SliderDefaults3.Track(
70-
sliderState = state,
71-
waveLength = 20.dp,
72-
thumbTrackGapSize = 0.dp
22+
CompositionLocalProvider(LocalDensity provides Density(1.23f)) {
23+
Column {
24+
var waveHeight by remember { mutableStateOf(10.dp) }
25+
WavySlider3(
26+
value = 0.02f,
27+
onValueChange = {},
28+
waveLength = 24.dp,
29+
waveHeight = waveHeight,
30+
waveVelocity = 4.dp to WaveDirection.HEAD,
31+
animationSpecs = SliderDefaults3.WaveAnimationSpecs.copy(
32+
waveStartSpreadAnimationSpec = snap()
7333
)
74-
}
75-
)
76-
WavySlider3(
77-
state = state,
78-
waveLength = 12.dp,
79-
waveHeight = 24.dp,
80-
waveVelocity = 14.dp to RIGHT,
81-
waveThickness = 1.dp,
82-
trackThickness = 2.dp,
83-
incremental = true,
84-
colors = SliderDefaults3.colors(activeTrackColor = Color.Red)
85-
)
86-
WavySlider3(state = state, waveHeight = 0.dp)
34+
)
35+
}
8736
}
8837
}
8938

0 commit comments

Comments
 (0)