Skip to content

Commit a7d06d4

Browse files
committed
fix: ui crashes related to settings
1 parent 4f26116 commit a7d06d4

3 files changed

Lines changed: 20 additions & 5 deletions

File tree

modules/internal/src/main/kotlin/org/polyfrost/oneconfig/internal/ui/components/settings/NumberOption.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ import org.polyfrost.oneconfig.internal.ui.components.rememberInteractionSource
3737
import org.polyfrost.oneconfig.internal.ui.themes.LocalTheme
3838
import kotlin.math.roundToInt
3939

40+
fun Float.toNumberType(type: Class<*>): Number = when (type) {
41+
Int::class.java, java.lang.Integer::class.java, Integer.TYPE -> toInt()
42+
Long::class.java, java.lang.Long::class.java, java.lang.Long.TYPE -> toLong()
43+
Double::class.java, java.lang.Double::class.java, java.lang.Double.TYPE -> toDouble()
44+
Short::class.java, java.lang.Short::class.java, java.lang.Short.TYPE -> toInt().toShort()
45+
Byte::class.java, java.lang.Byte::class.java, java.lang.Byte.TYPE -> toInt().toByte()
46+
else -> this
47+
}
48+
4049
fun filterNumberInput(input: String): String {
4150
val sb = StringBuilder()
4251
var hasDot = false
@@ -139,7 +148,7 @@ fun NumberOption(data: NumberOptionData) {
139148

140149
NumberSpinner(
141150
value = value,
142-
onValueChange = { value = it; data.numProp.set(it) },
151+
onValueChange = { value = it; data.numProp.set(it.toNumberType(data.prop.type)) },
143152
min = data.min,
144153
max = data.max,
145154
step = step,

modules/internal/src/main/kotlin/org/polyfrost/oneconfig/internal/ui/components/settings/Options.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ import org.polyfrost.oneconfig.internal.ui.themes.LocalTheme
1414

1515
@Composable
1616
fun Option(prop: Property<*>) {
17-
val vis = remember(prop) { prop.getMetadata<Visualizer>("visualizer") }
17+
val vis = remember(prop) {
18+
when (val raw = prop.getMetadata<Any>("visualizer")) {
19+
is Visualizer -> raw
20+
is Class<*> -> (raw.getDeclaredConstructor().newInstance() as? Visualizer)
21+
else -> null
22+
}
23+
}
1824
if (vis != null) {
1925
vis.visualize(prop)
2026
return

modules/internal/src/main/kotlin/org/polyfrost/oneconfig/internal/ui/components/settings/SliderOption.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ fun SliderOption(data: SliderOptionData) {
6767
awaitEachGesture {
6868
val down = awaitFirstDown()
6969
value = xToValue(down.position.x)
70-
data.numProp.set(value)
70+
data.numProp.set(value.toNumberType(data.prop.type))
7171
do {
7272
val event = awaitPointerEvent()
7373
event.changes.forEach { ch ->
7474
if (ch.pressed) {
7575
value = xToValue(ch.position.x)
76-
data.numProp.set(value)
76+
data.numProp.set(value.toNumberType(data.prop.type))
7777
ch.consume()
7878
}
7979
}
@@ -113,7 +113,7 @@ fun SliderOption(data: SliderOptionData) {
113113
}
114114
NumberSpinner(
115115
value = value,
116-
onValueChange = { value = it; data.numProp.set(it) },
116+
onValueChange = { value = it; data.numProp.set(it.toNumberType(data.prop.type)) },
117117
min = data.min,
118118
max = data.max,
119119
step = if (data.step > 0f) data.step else 1f,

0 commit comments

Comments
 (0)