Welcome to Beta screen: Preferences

This commit is contained in:
Stef Tervelde
2025-02-06 20:53:15 +01:00
parent ed49b65c48
commit 67da3ac205
5 changed files with 63 additions and 56 deletions
@@ -1,11 +1,10 @@
package processing.app.contrib.ui
package processing.app
import androidx.compose.runtime.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import processing.app.Base
import processing.app.Platform
import java.io.File
import java.io.InputStream
import java.nio.file.*
import java.util.Properties
@@ -13,10 +12,13 @@ import java.util.Properties
const val PREFERENCES_FILE_NAME = "preferences.txt"
const val DEFAULTS_FILE_NAME = "defaults.txt"
fun PlatformStart(){
Platform.inst ?: Platform.init()
}
@Composable
fun loadPreferences(): Properties{
Platform.init()
PlatformStart()
val settingsFolder = Platform.getSettingsFolder()
val preferencesFile = settingsFolder.resolve(PREFERENCES_FILE_NAME)
@@ -24,20 +26,12 @@ fun loadPreferences(): Properties{
if(!preferencesFile.exists()){
preferencesFile.createNewFile()
}
val watched = watchFile(preferencesFile)
watchFile(preferencesFile)
val preferences by remember {
mutableStateOf(Properties())
return Properties().apply {
load(ClassLoader.getSystemResourceAsStream(DEFAULTS_FILE_NAME) ?: InputStream.nullInputStream())
load(preferencesFile.inputStream())
}
LaunchedEffect(watched){
val defaults = Base::class.java.getResourceAsStream("/lib/${DEFAULTS_FILE_NAME}") ?: return@LaunchedEffect
preferences.load(defaults)
preferences.load(preferencesFile.inputStream())
}
return preferences
}
@Composable
@@ -68,4 +62,12 @@ fun watchFile(file: File): Any? {
}
}
return event
}
val LocalPreferences = compositionLocalOf<Properties> { error("No preferences provided") }
@Composable
fun PreferencesProvider(content: @Composable () -> Unit){
val preferences = loadPreferences()
CompositionLocalProvider(LocalPreferences provides preferences){
content()
}
}
@@ -12,8 +12,6 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.awt.ComposePanel
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.pointer.PointerIcon
import androidx.compose.ui.input.pointer.pointerHoverIcon
import androidx.compose.ui.text.font.FontWeight
@@ -25,6 +23,7 @@ import com.charleskorn.kaml.Yaml
import com.charleskorn.kaml.YamlConfiguration
import kotlinx.serialization.Serializable
import processing.app.Platform
import processing.app.loadPreferences
import java.net.URL
import java.util.*
import javax.swing.JFrame
@@ -33,16 +32,7 @@ import kotlin.io.path.*
fun main() = application {
val active = remember { mutableStateOf(true) }
if(!active.value){
Window(onCloseRequest = ::exitApplication) {
}
return@application
}
Window(
onCloseRequest = { active.value = false },
) {
Window(onCloseRequest = ::exitApplication) {
contributionsManager()
}
}
+1 -1
View File
@@ -122,7 +122,7 @@ class WelcomeToBeta {
}) {
Text(
text = locale["beta.button"],
color = Color.White
color = colors.onPrimary
)
}
}
+14 -3
View File
@@ -3,15 +3,21 @@ package processing.app.ui.theme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.compositionLocalOf
import processing.app.LocalPreferences
import processing.app.Platform
import processing.app.PlatformStart
import processing.app.watchFile
import java.io.File
import java.io.InputStream
import java.util.*
class Locale : Properties() {
class Locale(language: String = "") : Properties() {
init {
val locale = java.util.Locale.getDefault()
load(ClassLoader.getSystemResourceAsStream("PDE.properties"))
load(ClassLoader.getSystemResourceAsStream("PDE_${locale.language}.properties") ?: InputStream.nullInputStream())
load(ClassLoader.getSystemResourceAsStream("PDE_${locale.toLanguageTag()}.properties") ?: InputStream.nullInputStream())
load(ClassLoader.getSystemResourceAsStream("PDE_${language}.properties") ?: InputStream.nullInputStream())
}
@Deprecated("Use get instead", ReplaceWith("get(key)"))
@@ -23,8 +29,13 @@ class Locale : Properties() {
val LocalLocale = compositionLocalOf { Locale() }
@Composable
fun LocaleProvider(content: @Composable () -> Unit) {
val locale = Locale()
// TODO: Listen for languages changes
PlatformStart()
val settingsFolder = Platform.getSettingsFolder()
val languageFile = File(settingsFolder, "language.txt")
watchFile(languageFile)
val locale = Locale(languageFile.readText().substring(0, 2))
CompositionLocalProvider(LocalLocale provides locale) {
content()
}
+28 -24
View File
@@ -7,6 +7,8 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.ui.graphics.Color
import processing.app.LocalPreferences
import processing.app.PreferencesProvider
import java.io.InputStream
import java.util.Properties
@@ -28,31 +30,33 @@ fun ProcessingTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
content: @Composable() () -> Unit
) {
PreferencesProvider {
val preferences = LocalPreferences.current
val theme = Theme(preferences.getProperty("theme"))
val colors = Colors(
primary = theme.getColor("editor.gradient.top"),
primaryVariant = theme.getColor("toolbar.button.pressed.field"),
secondary = theme.getColor("editor.gradient.bottom"),
secondaryVariant = theme.getColor("editor.scrollbar.thumb.pressed.color"),
background = theme.getColor("editor.bgcolor"),
surface = theme.getColor("editor.bgcolor"),
error = theme.getColor("status.error.bgcolor"),
onPrimary = theme.getColor("toolbar.button.enabled.field"),
onSecondary = theme.getColor("toolbar.button.enabled.field"),
onBackground = theme.getColor("editor.fgcolor"),
onSurface = theme.getColor("editor.fgcolor"),
onError = theme.getColor("status.error.fgcolor"),
isLight = theme.getProperty("laf.mode").equals("light")
)
val theme = Theme()
val colors = Colors(
primary = theme.getColor("editor.gradient.top"),
primaryVariant = theme.getColor("toolbar.button.pressed.field"),
secondary = theme.getColor("editor.gradient.bottom"),
secondaryVariant = theme.getColor("editor.scrollbar.thumb.pressed.color"),
background = theme.getColor("editor.bgcolor"),
surface = theme.getColor("editor.bgcolor"),
error = theme.getColor("status.error.bgcolor"),
onPrimary = theme.getColor("toolbar.button.pressed.field"),
onSecondary = theme.getColor("toolbar.button.pressed.field"),
onBackground = theme.getColor("editor.fgcolor"),
onSurface = theme.getColor("editor.fgcolor"),
onError = theme.getColor("status.error.fgcolor"),
isLight = theme.getProperty("laf.mode").equals("light")
)
CompositionLocalProvider(LocalTheme provides theme) {
LocaleProvider {
MaterialTheme(
colors = colors,
typography = Typography,
content = content
)
CompositionLocalProvider(LocalTheme provides theme) {
LocaleProvider {
MaterialTheme(
colors = colors,
typography = Typography,
content = content
)
}
}
}
}