Skip to content

Commit 67da3ac

Browse files
committed
Welcome to Beta screen: Preferences
1 parent ed49b65 commit 67da3ac

File tree

5 files changed

+63
-56
lines changed

5 files changed

+63
-56
lines changed

app/src/processing/app/contrib/ui/Preferences.kt renamed to app/src/processing/app/Preferences.kt

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,37 @@
1-
package processing.app.contrib.ui
1+
package processing.app
22

33
import androidx.compose.runtime.*
44
import kotlinx.coroutines.Dispatchers
55
import kotlinx.coroutines.launch
6-
import processing.app.Base
7-
import processing.app.Platform
86
import java.io.File
7+
import java.io.InputStream
98
import java.nio.file.*
109
import java.util.Properties
1110

1211

1312
const val PREFERENCES_FILE_NAME = "preferences.txt"
1413
const val DEFAULTS_FILE_NAME = "defaults.txt"
1514

15+
fun PlatformStart(){
16+
Platform.inst ?: Platform.init()
17+
}
1618

1719
@Composable
1820
fun loadPreferences(): Properties{
19-
Platform.init()
21+
PlatformStart()
2022

2123
val settingsFolder = Platform.getSettingsFolder()
2224
val preferencesFile = settingsFolder.resolve(PREFERENCES_FILE_NAME)
2325

2426
if(!preferencesFile.exists()){
2527
preferencesFile.createNewFile()
2628
}
27-
val watched = watchFile(preferencesFile)
28-
29-
val preferences by remember {
30-
mutableStateOf(Properties())
31-
}
32-
33-
LaunchedEffect(watched){
34-
val defaults = Base::class.java.getResourceAsStream("/lib/${DEFAULTS_FILE_NAME}") ?: return@LaunchedEffect
29+
watchFile(preferencesFile)
3530

36-
preferences.load(defaults)
37-
preferences.load(preferencesFile.inputStream())
31+
return Properties().apply {
32+
load(ClassLoader.getSystemResourceAsStream(DEFAULTS_FILE_NAME) ?: InputStream.nullInputStream())
33+
load(preferencesFile.inputStream())
3834
}
39-
40-
return preferences
4135
}
4236

4337
@Composable
@@ -68,4 +62,12 @@ fun watchFile(file: File): Any? {
6862
}
6963
}
7064
return event
65+
}
66+
val LocalPreferences = compositionLocalOf<Properties> { error("No preferences provided") }
67+
@Composable
68+
fun PreferencesProvider(content: @Composable () -> Unit){
69+
val preferences = loadPreferences()
70+
CompositionLocalProvider(LocalPreferences provides preferences){
71+
content()
72+
}
7173
}

app/src/processing/app/contrib/ui/ContributionManager.kt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import androidx.compose.ui.Alignment
1212
import androidx.compose.ui.Modifier
1313
import androidx.compose.ui.awt.ComposePanel
1414
import androidx.compose.ui.graphics.Color
15-
import androidx.compose.ui.input.key.Key
16-
import androidx.compose.ui.input.key.key
1715
import androidx.compose.ui.input.pointer.PointerIcon
1816
import androidx.compose.ui.input.pointer.pointerHoverIcon
1917
import androidx.compose.ui.text.font.FontWeight
@@ -25,6 +23,7 @@ import com.charleskorn.kaml.Yaml
2523
import com.charleskorn.kaml.YamlConfiguration
2624
import kotlinx.serialization.Serializable
2725
import processing.app.Platform
26+
import processing.app.loadPreferences
2827
import java.net.URL
2928
import java.util.*
3029
import javax.swing.JFrame
@@ -33,16 +32,7 @@ import kotlin.io.path.*
3332

3433

3534
fun main() = application {
36-
val active = remember { mutableStateOf(true) }
37-
if(!active.value){
38-
Window(onCloseRequest = ::exitApplication) {
39-
40-
}
41-
return@application
42-
}
43-
Window(
44-
onCloseRequest = { active.value = false },
45-
) {
35+
Window(onCloseRequest = ::exitApplication) {
4636
contributionsManager()
4737
}
4838
}

app/src/processing/app/ui/WelcomeToBeta.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class WelcomeToBeta {
122122
}) {
123123
Text(
124124
text = locale["beta.button"],
125-
color = Color.White
125+
color = colors.onPrimary
126126
)
127127
}
128128
}

app/src/processing/app/ui/theme/Locale.kt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@ package processing.app.ui.theme
33
import androidx.compose.runtime.Composable
44
import androidx.compose.runtime.CompositionLocalProvider
55
import androidx.compose.runtime.compositionLocalOf
6+
import processing.app.LocalPreferences
7+
import processing.app.Platform
8+
import processing.app.PlatformStart
9+
import processing.app.watchFile
10+
import java.io.File
611
import java.io.InputStream
712
import java.util.*
813

9-
class Locale : Properties() {
14+
class Locale(language: String = "") : Properties() {
1015
init {
1116
val locale = java.util.Locale.getDefault()
1217
load(ClassLoader.getSystemResourceAsStream("PDE.properties"))
1318
load(ClassLoader.getSystemResourceAsStream("PDE_${locale.language}.properties") ?: InputStream.nullInputStream())
1419
load(ClassLoader.getSystemResourceAsStream("PDE_${locale.toLanguageTag()}.properties") ?: InputStream.nullInputStream())
20+
load(ClassLoader.getSystemResourceAsStream("PDE_${language}.properties") ?: InputStream.nullInputStream())
1521
}
1622

1723
@Deprecated("Use get instead", ReplaceWith("get(key)"))
@@ -23,8 +29,13 @@ class Locale : Properties() {
2329
val LocalLocale = compositionLocalOf { Locale() }
2430
@Composable
2531
fun LocaleProvider(content: @Composable () -> Unit) {
26-
val locale = Locale()
27-
// TODO: Listen for languages changes
32+
PlatformStart()
33+
34+
val settingsFolder = Platform.getSettingsFolder()
35+
val languageFile = File(settingsFolder, "language.txt")
36+
watchFile(languageFile)
37+
38+
val locale = Locale(languageFile.readText().substring(0, 2))
2839
CompositionLocalProvider(LocalLocale provides locale) {
2940
content()
3041
}

app/src/processing/app/ui/theme/Theme.kt

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import androidx.compose.runtime.Composable
77
import androidx.compose.runtime.CompositionLocalProvider
88
import androidx.compose.runtime.compositionLocalOf
99
import androidx.compose.ui.graphics.Color
10+
import processing.app.LocalPreferences
11+
import processing.app.PreferencesProvider
1012
import java.io.InputStream
1113
import java.util.Properties
1214

@@ -28,31 +30,33 @@ fun ProcessingTheme(
2830
darkTheme: Boolean = isSystemInDarkTheme(),
2931
content: @Composable() () -> Unit
3032
) {
33+
PreferencesProvider {
34+
val preferences = LocalPreferences.current
35+
val theme = Theme(preferences.getProperty("theme"))
36+
val colors = Colors(
37+
primary = theme.getColor("editor.gradient.top"),
38+
primaryVariant = theme.getColor("toolbar.button.pressed.field"),
39+
secondary = theme.getColor("editor.gradient.bottom"),
40+
secondaryVariant = theme.getColor("editor.scrollbar.thumb.pressed.color"),
41+
background = theme.getColor("editor.bgcolor"),
42+
surface = theme.getColor("editor.bgcolor"),
43+
error = theme.getColor("status.error.bgcolor"),
44+
onPrimary = theme.getColor("toolbar.button.enabled.field"),
45+
onSecondary = theme.getColor("toolbar.button.enabled.field"),
46+
onBackground = theme.getColor("editor.fgcolor"),
47+
onSurface = theme.getColor("editor.fgcolor"),
48+
onError = theme.getColor("status.error.fgcolor"),
49+
isLight = theme.getProperty("laf.mode").equals("light")
50+
)
3151

32-
val theme = Theme()
33-
val colors = Colors(
34-
primary = theme.getColor("editor.gradient.top"),
35-
primaryVariant = theme.getColor("toolbar.button.pressed.field"),
36-
secondary = theme.getColor("editor.gradient.bottom"),
37-
secondaryVariant = theme.getColor("editor.scrollbar.thumb.pressed.color"),
38-
background = theme.getColor("editor.bgcolor"),
39-
surface = theme.getColor("editor.bgcolor"),
40-
error = theme.getColor("status.error.bgcolor"),
41-
onPrimary = theme.getColor("toolbar.button.pressed.field"),
42-
onSecondary = theme.getColor("toolbar.button.pressed.field"),
43-
onBackground = theme.getColor("editor.fgcolor"),
44-
onSurface = theme.getColor("editor.fgcolor"),
45-
onError = theme.getColor("status.error.fgcolor"),
46-
isLight = theme.getProperty("laf.mode").equals("light")
47-
)
48-
49-
CompositionLocalProvider(LocalTheme provides theme) {
50-
LocaleProvider {
51-
MaterialTheme(
52-
colors = colors,
53-
typography = Typography,
54-
content = content
55-
)
52+
CompositionLocalProvider(LocalTheme provides theme) {
53+
LocaleProvider {
54+
MaterialTheme(
55+
colors = colors,
56+
typography = Typography,
57+
content = content
58+
)
59+
}
5660
}
5761
}
5862
}

0 commit comments

Comments
 (0)