From cb5eb95b046bb54e0a8fdfb3e3867e80a1772e7b Mon Sep 17 00:00:00 2001 From: Marvin Elsen Date: Sat, 28 Sep 2024 16:12:11 +0200 Subject: [PATCH] Implement preferences --- .../kotlin/com/marvinelsen/willow/Config.kt | 32 ---- .../marvinelsen/willow/WillowApplication.kt | 6 +- .../com/marvinelsen/willow/config/Config.kt | 146 ++++++++++++++++++ .../willow/config/Pronunciation.kt | 7 + .../com/marvinelsen/willow/config/Script.kt | 5 + .../com/marvinelsen/willow/config/Theme.kt | 5 + .../marvinelsen/willow/ui/Configuration.kt | 15 -- .../willow/ui/DictionaryEntryFx.kt | 1 + .../willow/ui/cells/EntryCellFactory.kt | 67 +++++++- .../ui/controllers/DetailsController.kt | 21 ++- .../willow/ui/controllers/ListController.kt | 5 +- .../willow/ui/controllers/MenuController.kt | 2 +- .../ui/converters/LocaleStringConverter.kt | 9 ++ .../PronunciationStringConverter.kt | 2 +- .../ui/converters/ScriptStringConverter.kt | 13 ++ .../ui/converters/ThemeStringConverter.kt | 14 ++ .../willow/ui/dialogs/PreferencesDialog.kt | 63 +++++++- src/main/resources/css/preferences.css | 3 + .../resources/fxml/preferences-dialog.fxml | 43 +++++- 19 files changed, 384 insertions(+), 75 deletions(-) delete mode 100644 src/main/kotlin/com/marvinelsen/willow/Config.kt create mode 100644 src/main/kotlin/com/marvinelsen/willow/config/Config.kt create mode 100644 src/main/kotlin/com/marvinelsen/willow/config/Pronunciation.kt create mode 100644 src/main/kotlin/com/marvinelsen/willow/config/Script.kt create mode 100644 src/main/kotlin/com/marvinelsen/willow/config/Theme.kt delete mode 100644 src/main/kotlin/com/marvinelsen/willow/ui/Configuration.kt create mode 100644 src/main/kotlin/com/marvinelsen/willow/ui/converters/LocaleStringConverter.kt create mode 100644 src/main/kotlin/com/marvinelsen/willow/ui/converters/ScriptStringConverter.kt create mode 100644 src/main/kotlin/com/marvinelsen/willow/ui/converters/ThemeStringConverter.kt create mode 100644 src/main/resources/css/preferences.css diff --git a/src/main/kotlin/com/marvinelsen/willow/Config.kt b/src/main/kotlin/com/marvinelsen/willow/Config.kt deleted file mode 100644 index b3b13b4..0000000 --- a/src/main/kotlin/com/marvinelsen/willow/Config.kt +++ /dev/null @@ -1,32 +0,0 @@ -package com.marvinelsen.willow - -import javafx.beans.property.IntegerProperty -import javafx.beans.property.SimpleIntegerProperty -import java.util.prefs.Preferences - -class Config { - companion object { - private const val DETAIL_HEADWORD_FONT_SIZE_KEY = "detailHeadwordFontSize" - - private const val DEFAULT_DETAIL_HEADWORD_FONT_SIZE = 16 - } - - private val preferences = Preferences.userNodeForPackage(this::class.java) - - val detailHeadwordFontSize: IntegerProperty = SimpleIntegerProperty(DEFAULT_DETAIL_HEADWORD_FONT_SIZE) - - fun save() { - preferences.putInt(DETAIL_HEADWORD_FONT_SIZE_KEY, detailHeadwordFontSize.value) - } - - fun load() { - detailHeadwordFontSize.value = preferences.getInt( - DETAIL_HEADWORD_FONT_SIZE_KEY, - DEFAULT_DETAIL_HEADWORD_FONT_SIZE - ) - } - - fun reset() { - detailHeadwordFontSize.value = DEFAULT_DETAIL_HEADWORD_FONT_SIZE - } -} diff --git a/src/main/kotlin/com/marvinelsen/willow/WillowApplication.kt b/src/main/kotlin/com/marvinelsen/willow/WillowApplication.kt index 08cdbaf..a47f176 100644 --- a/src/main/kotlin/com/marvinelsen/willow/WillowApplication.kt +++ b/src/main/kotlin/com/marvinelsen/willow/WillowApplication.kt @@ -1,5 +1,6 @@ package com.marvinelsen.willow +import com.marvinelsen.willow.config.Config import com.marvinelsen.willow.domain.SqliteDictionary import com.marvinelsen.willow.ui.controllers.DetailsController import com.marvinelsen.willow.ui.controllers.ListController @@ -16,7 +17,6 @@ import javafx.scene.text.Font import javafx.stage.Stage import javafx.util.Callback import java.sql.DriverManager -import java.util.Locale import java.util.ResourceBundle class WillowApplication : Application() { @@ -48,14 +48,14 @@ class WillowApplication : Application() { config.load() val fxmlLoader = FXMLLoader() - fxmlLoader.resources = ResourceBundle.getBundle("i18n/willow", Locale.getDefault()) + fxmlLoader.resources = ResourceBundle.getBundle("i18n/willow", config.locale.value) fxmlLoader.controllerFactory = Callback { type -> when (type) { MainController::class.java -> MainController(model) MenuController::class.java -> MenuController(model, config) DetailsController::class.java -> DetailsController(model, config) SearchController::class.java -> SearchController(model) - ListController::class.java -> ListController(model) + ListController::class.java -> ListController(model, config) else -> error("Trying to instantiate unknown controller type $type") } } diff --git a/src/main/kotlin/com/marvinelsen/willow/config/Config.kt b/src/main/kotlin/com/marvinelsen/willow/config/Config.kt new file mode 100644 index 0000000..e8c4b4e --- /dev/null +++ b/src/main/kotlin/com/marvinelsen/willow/config/Config.kt @@ -0,0 +1,146 @@ +package com.marvinelsen.willow.config + +import javafx.beans.property.BooleanProperty +import javafx.beans.property.IntegerProperty +import javafx.beans.property.ObjectProperty +import javafx.beans.property.SimpleBooleanProperty +import javafx.beans.property.SimpleIntegerProperty +import javafx.beans.property.SimpleObjectProperty +import java.util.Locale +import java.util.prefs.Preferences + +class Config { + companion object { + private const val LOCALE_KEY = "locale" + private const val THEME_KEY = "theme" + private const val SCRIPT_KEY = "script" + private const val DETAIL_HEADWORD_FONT_SIZE_KEY = "detailHeadwordFontSize" + private const val DETAIL_PRONUNCIATION_FONT_SIZE_KEY = "detailPronunciationFontSize" + + private const val DEFAULT_DETAIL_HEADWORD_FONT_SIZE = 40 + private const val DEFAULT_DETAIL_PRONUNCIATION_FONT_SIZE = 16 + private val DEFAULT_THEME = Theme.SYSTEM + private val DEFAULT_SCRIPT = Script.SIMPLIFIED + private val DEFAULT_LOCALE = Locale.ENGLISH + } + + private val preferences = Preferences.userNodeForPackage(this::class.java) + val searchResults = SearchResultsConfig(preferences) + + val locale: ObjectProperty = SimpleObjectProperty(DEFAULT_LOCALE) + val theme: ObjectProperty = SimpleObjectProperty(DEFAULT_THEME) + val script: ObjectProperty