Refactor Other preferences registration logic by Stefterv · Pull Request #1379 · processing/processing4

Expand Up @@ -6,6 +6,7 @@ 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.Composable import androidx.compose.runtime.DisposableEffect import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp Expand All @@ -27,7 +28,7 @@ class Other { after = sketches )
fun register(panes: PDEPreferencePanes) { fun register() { PDEPreferences.register( PDEPreference( key = "preferences.show_other", Expand All @@ -41,57 +42,61 @@ class Other { 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() @Composable fun handleOtherPreferences(panes: PDEPreferencePanes) { // This function can be used to handle any specific logic related to other preferences if needed val prefs = LocalPreferences.current val locale = LocalLocale.current if (prefs["preferences.show_other"]?.toBoolean() != true) { return } DisposableEffect(panes) { // 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>
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 } val existing = panes.values.flatten().flatten().map { preference -> preference.key } val keys = prefs.keys.mapNotNull { it as? String }.filter { it !in existing }.sorted()
OutlinedTextField( modifier = Modifier.widthIn(max = 300.dp), value = preference ?: "", singleLine = true, onValueChange = { updatePreference(it) } ) 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()) } ) group?.add(preference) return@PDEPreference } onDispose { group?.apply { removeIf { it.key != "preferences.show_other" }
OutlinedTextField( modifier = Modifier.widthIn(max = 300.dp), value = preference ?: "", onValueChange = { updatePreference(it) } } ) } ) group?.add(preference) } onDispose { group?.apply { removeIf { it.key != "preferences.show_other" } } ) ) } } } } }