forked from processing/processing4
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOther.kt
More file actions
97 lines (91 loc) · 4.46 KB
/
Other.kt
File metadata and controls
97 lines (91 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package processing.app.ui.preferences
import androidx.compose.foundation.layout.widthIn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Lightbulb
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Switch
import androidx.compose.runtime.DisposableEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import processing.app.LocalPreferences
import processing.app.ui.PDEPreference
import processing.app.ui.PDEPreferencePane
import processing.app.ui.PDEPreferencePanes
import processing.app.ui.PDEPreferences
import processing.app.ui.preferences.Sketches.Companion.sketches
import processing.app.ui.theme.LocalLocale
class Other {
companion object{
val other = PDEPreferencePane(
nameKey = "preferences.pane.other",
icon = {
Icon(Icons.Default.Lightbulb, contentDescription = "Other Preferences")
},
after = sketches
)
fun register(panes: PDEPreferencePanes) {
PDEPreferences.register(
PDEPreference(
key = "preferences.show_other",
descriptionKey = "preferences.other",
pane = other,
control = { preference, setPreference ->
val showOther = preference?.toBoolean() ?: false
Switch(
checked = showOther,
onCheckedChange = {
setPreference(it.toString())
}
)
if (!showOther) {
return@PDEPreference
}
val prefs = LocalPreferences.current
val locale = LocalLocale.current
DisposableEffect(Unit) {
// add all the other options to the same group as the current one
val group =
panes[other]?.find { group -> group.any { preference -> preference.key == "preferences.show_other" } } as? MutableList<PDEPreference>
val existing = panes.values.flatten().flatten().map { preference -> preference.key }
val keys = prefs.keys.mapNotNull { it as? String }.filter { it !in existing }.sorted()
for (prefKey in keys) {
val descriptionKey = "preferences.$prefKey"
val preference = PDEPreference(
key = prefKey,
descriptionKey = if (locale.containsKey(descriptionKey)) descriptionKey else prefKey,
pane = other,
control = { preference, updatePreference ->
if (preference?.toBooleanStrictOrNull() != null) {
Switch(
checked = preference.toBoolean(),
onCheckedChange = {
updatePreference(it.toString())
}
)
return@PDEPreference
}
OutlinedTextField(
modifier = Modifier.widthIn(max = 300.dp),
value = preference ?: "",
singleLine = true,
onValueChange = {
updatePreference(it)
}
)
}
)
group?.add(preference)
}
onDispose {
group?.apply {
removeIf { it.key != "preferences.show_other" }
}
}
}
}
)
)
}
}
}