Implement preferences

This commit is contained in:
Marvin Elsen 2024-09-28 16:12:11 +02:00
parent 6446c2d9c8
commit cb5eb95b04
Signed by: marvinelsen
GPG Key ID: 820672408CC318C2
19 changed files with 384 additions and 75 deletions

View File

@ -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
}
}

View File

@ -1,5 +1,6 @@
package com.marvinelsen.willow package com.marvinelsen.willow
import com.marvinelsen.willow.config.Config
import com.marvinelsen.willow.domain.SqliteDictionary import com.marvinelsen.willow.domain.SqliteDictionary
import com.marvinelsen.willow.ui.controllers.DetailsController import com.marvinelsen.willow.ui.controllers.DetailsController
import com.marvinelsen.willow.ui.controllers.ListController import com.marvinelsen.willow.ui.controllers.ListController
@ -16,7 +17,6 @@ import javafx.scene.text.Font
import javafx.stage.Stage import javafx.stage.Stage
import javafx.util.Callback import javafx.util.Callback
import java.sql.DriverManager import java.sql.DriverManager
import java.util.Locale
import java.util.ResourceBundle import java.util.ResourceBundle
class WillowApplication : Application() { class WillowApplication : Application() {
@ -48,14 +48,14 @@ class WillowApplication : Application() {
config.load() config.load()
val fxmlLoader = FXMLLoader() val fxmlLoader = FXMLLoader()
fxmlLoader.resources = ResourceBundle.getBundle("i18n/willow", Locale.getDefault()) fxmlLoader.resources = ResourceBundle.getBundle("i18n/willow", config.locale.value)
fxmlLoader.controllerFactory = Callback { type -> fxmlLoader.controllerFactory = Callback { type ->
when (type) { when (type) {
MainController::class.java -> MainController(model) MainController::class.java -> MainController(model)
MenuController::class.java -> MenuController(model, config) MenuController::class.java -> MenuController(model, config)
DetailsController::class.java -> DetailsController(model, config) DetailsController::class.java -> DetailsController(model, config)
SearchController::class.java -> SearchController(model) 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") else -> error("Trying to instantiate unknown controller type $type")
} }
} }

View File

@ -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<Locale> = SimpleObjectProperty(DEFAULT_LOCALE)
val theme: ObjectProperty<Theme> = SimpleObjectProperty(DEFAULT_THEME)
val script: ObjectProperty<Script> = SimpleObjectProperty(DEFAULT_SCRIPT)
val detailHeadwordFontSize: IntegerProperty = SimpleIntegerProperty(DEFAULT_DETAIL_HEADWORD_FONT_SIZE)
val detailPronunciationFontSize: IntegerProperty = SimpleIntegerProperty(DEFAULT_DETAIL_PRONUNCIATION_FONT_SIZE)
fun save() {
preferences.put(LOCALE_KEY, locale.value.toLanguageTag())
preferences.put(THEME_KEY, theme.value.name)
preferences.put(SCRIPT_KEY, script.value.name)
preferences.putInt(DETAIL_HEADWORD_FONT_SIZE_KEY, detailHeadwordFontSize.value)
preferences.putInt(DETAIL_PRONUNCIATION_FONT_SIZE_KEY, detailPronunciationFontSize.value)
searchResults.save()
preferences.flush()
}
fun load() {
preferences.sync()
detailHeadwordFontSize.value = preferences.getInt(
DETAIL_HEADWORD_FONT_SIZE_KEY,
DEFAULT_DETAIL_HEADWORD_FONT_SIZE
)
detailPronunciationFontSize.value = preferences.getInt(
DETAIL_PRONUNCIATION_FONT_SIZE_KEY,
DEFAULT_DETAIL_PRONUNCIATION_FONT_SIZE
)
theme.value = Theme.valueOf(
preferences.get(
THEME_KEY,
DEFAULT_THEME.name
)
)
script.value = Script.valueOf(
preferences.get(
SCRIPT_KEY,
DEFAULT_SCRIPT.name
)
)
locale.value = Locale.forLanguageTag(preferences.get(LOCALE_KEY, DEFAULT_LOCALE.toLanguageTag()))
searchResults.load()
}
fun reset() {
detailHeadwordFontSize.value = DEFAULT_DETAIL_HEADWORD_FONT_SIZE
}
}
class SearchResultsConfig(private val preferences: Preferences) {
companion object {
private const val PRONUNCIATION_KEY = "searchResultsPronunciation"
private const val HEADWORD_FONT_SIZE_KEY = "searchResultsHeadwordFontSize"
private const val PRONUNCIATION_FONT_SIZE_KEY = "searchResultsPronunciationFontSize"
private const val DEFINITION_FONT_SIZE_KEY = "searchResultsDefinitionFontSize"
private const val SHOULD_SHOW_PRONUNCIATION_KEY = "searchResultsShouldShowPronunciation"
private const val SHOULD_SHOW_DEFINITION_KEY = "searchResultsShouldShowDefinition"
private val DEFAULT_PRONUNCIATION = Pronunciation.PINYIN_WITH_TONE_MARKS
private const val DEFAULT_HEADWORD_FONT_SIZE = 20
private const val DEFAULT_PRONUNCIATION_FONT_SIZE = 14
private const val DEFAULT_DEFINITION_FONT_SIZE = 14
private const val DEFAULT_SHOULD_SHOW_PRONUNCIATION = true
private const val DEFAULT_SHOULD_SHOW_DEFINITION = true
}
val pronunciation: ObjectProperty<Pronunciation> = SimpleObjectProperty(DEFAULT_PRONUNCIATION)
val headwordFontSize: IntegerProperty = SimpleIntegerProperty(DEFAULT_HEADWORD_FONT_SIZE)
val pronunciationFontSize: IntegerProperty = SimpleIntegerProperty(DEFAULT_PRONUNCIATION_FONT_SIZE)
val definitionFontSize: IntegerProperty = SimpleIntegerProperty(DEFAULT_DEFINITION_FONT_SIZE)
val shouldShowPronunciation: BooleanProperty = SimpleBooleanProperty(DEFAULT_SHOULD_SHOW_PRONUNCIATION)
val shouldShowDefinition: BooleanProperty = SimpleBooleanProperty(DEFAULT_SHOULD_SHOW_DEFINITION)
fun save() {
preferences.put(PRONUNCIATION_KEY, pronunciation.value.name)
preferences.putInt(HEADWORD_FONT_SIZE_KEY, headwordFontSize.value)
preferences.putInt(PRONUNCIATION_FONT_SIZE_KEY, pronunciationFontSize.value)
preferences.putInt(DEFINITION_FONT_SIZE_KEY, definitionFontSize.value)
preferences.putBoolean(SHOULD_SHOW_PRONUNCIATION_KEY, shouldShowPronunciation.value)
preferences.putBoolean(SHOULD_SHOW_DEFINITION_KEY, shouldShowDefinition.value)
}
fun load() {
headwordFontSize.value = preferences.getInt(
HEADWORD_FONT_SIZE_KEY,
DEFAULT_HEADWORD_FONT_SIZE
)
pronunciationFontSize.value = preferences.getInt(
PRONUNCIATION_FONT_SIZE_KEY,
DEFAULT_PRONUNCIATION_FONT_SIZE
)
definitionFontSize.value = preferences.getInt(
DEFINITION_FONT_SIZE_KEY,
DEFAULT_DEFINITION_FONT_SIZE
)
shouldShowPronunciation.value = preferences.getBoolean(
SHOULD_SHOW_PRONUNCIATION_KEY,
DEFAULT_SHOULD_SHOW_PRONUNCIATION
)
shouldShowDefinition.value = preferences.getBoolean(
SHOULD_SHOW_DEFINITION_KEY,
DEFAULT_SHOULD_SHOW_DEFINITION
)
pronunciation.value = Pronunciation.valueOf(
preferences.get(
PRONUNCIATION_KEY,
DEFAULT_PRONUNCIATION.name
)
)
}
}

View File

@ -0,0 +1,7 @@
package com.marvinelsen.willow.config
enum class Pronunciation {
PINYIN_WITH_TONE_MARKS,
PINYIN_WITH_TONE_NUMBERS,
ZHUYIN
}

View File

@ -0,0 +1,5 @@
package com.marvinelsen.willow.config
enum class Script {
SIMPLIFIED, TRADITIONAL
}

View File

@ -0,0 +1,5 @@
package com.marvinelsen.willow.config
enum class Theme {
SYSTEM, LIGHT, DARK
}

View File

@ -1,15 +0,0 @@
package com.marvinelsen.willow.ui
data class Configuration(
val preferredScript: Script = Script.SIMPLIFIED,
)
enum class Script {
SIMPLIFIED, TRADITIONAL
}
enum class Pronunciation {
PINYIN_WITH_TONE_MARKS,
PINYIN_WITH_TONE_NUMBERS,
ZHUYIN
}

View File

@ -6,6 +6,7 @@ import javafx.beans.property.StringProperty
import javafx.collections.FXCollections import javafx.collections.FXCollections
import javafx.collections.ObservableList import javafx.collections.ObservableList
// TODO: Consider removing this class completely if no property is ever used directly
data class DictionaryEntryFx( data class DictionaryEntryFx(
val traditionalProperty: StringProperty, val traditionalProperty: StringProperty,
val simplifiedProperty: StringProperty, val simplifiedProperty: StringProperty,

View File

@ -1,7 +1,11 @@
package com.marvinelsen.willow.ui.cells package com.marvinelsen.willow.ui.cells
import com.marvinelsen.willow.config.Config
import com.marvinelsen.willow.config.Pronunciation
import com.marvinelsen.willow.config.Script
import com.marvinelsen.willow.ui.DictionaryEntryFx import com.marvinelsen.willow.ui.DictionaryEntryFx
import com.marvinelsen.willow.ui.util.createContextMenuForEntry import com.marvinelsen.willow.ui.util.createContextMenuForEntry
import javafx.beans.binding.Bindings
import javafx.geometry.VPos import javafx.geometry.VPos
import javafx.scene.control.Label import javafx.scene.control.Label
import javafx.scene.control.ListCell import javafx.scene.control.ListCell
@ -11,10 +15,10 @@ import javafx.scene.layout.VBox
import javafx.util.Callback import javafx.util.Callback
import java.util.ResourceBundle import java.util.ResourceBundle
class DictionaryEntryCellFactory(private val resources: ResourceBundle) : class DictionaryEntryCellFactory(private val resources: ResourceBundle, private val config: Config) :
Callback<ListView<DictionaryEntryFx?>, ListCell<DictionaryEntryFx?>> { Callback<ListView<DictionaryEntryFx?>, ListCell<DictionaryEntryFx?>> {
override fun call(listView: ListView<DictionaryEntryFx?>): ListCell<DictionaryEntryFx?> { override fun call(listView: ListView<DictionaryEntryFx?>): ListCell<DictionaryEntryFx?> {
val entryCell = EntryCell(resources) val entryCell = EntryCell(resources, config)
entryCell.prefWidthProperty().bind(listView.widthProperty().subtract(CELL_PADDING)) entryCell.prefWidthProperty().bind(listView.widthProperty().subtract(CELL_PADDING))
return entryCell return entryCell
} }
@ -24,17 +28,43 @@ class DictionaryEntryCellFactory(private val resources: ResourceBundle) :
} }
} }
internal class EntryCell(private val resources: ResourceBundle) : ListCell<DictionaryEntryFx?>() { internal class EntryCell(private val resources: ResourceBundle, private val config: Config) :
ListCell<DictionaryEntryFx?>() {
private val labelHeadword = Label().apply { private val labelHeadword = Label().apply {
styleClass.add("list-view-entry") styleClass.add("headword")
styleProperty().bind(
Bindings.concat(
"-fx-font-size: ",
config.searchResults.headwordFontSize.asString(),
"px;"
)
)
} }
private val labelDefinition = Label().apply { private val labelDefinition = Label().apply {
styleClass.add("list-view-definition") styleClass.add("definition")
styleProperty().bind(
Bindings.concat(
"-fx-font-size: ",
config.searchResults.definitionFontSize.asString(),
"px;"
)
)
visibleProperty().bind(config.searchResults.shouldShowDefinition)
managedProperty().bind(config.searchResults.shouldShowDefinition)
} }
private val labelPronunciation = Label().apply { private val labelPronunciation = Label().apply {
styleClass.add("list-view-pronunciation") styleClass.add("pronunciation")
styleProperty().bind(
Bindings.concat(
"-fx-font-size: ",
config.searchResults.pronunciationFontSize.asString(),
"px;"
)
)
visibleProperty().bind(config.searchResults.shouldShowPronunciation)
managedProperty().bind(config.searchResults.shouldShowPronunciation)
} }
private val flowPane = FlowPane(labelHeadword, labelPronunciation).apply { private val flowPane = FlowPane(labelHeadword, labelPronunciation).apply {
@ -54,8 +84,29 @@ internal class EntryCell(private val resources: ResourceBundle) : ListCell<Dicti
graphic = null graphic = null
contextMenu = null contextMenu = null
} else { } else {
labelHeadword.text = entry.traditionalProperty.value labelHeadword.textProperty().bind(
labelPronunciation.text = entry.pinyinWithToneMarksProperty.value Bindings.createStringBinding(
{
when (config.script.value!!) {
Script.SIMPLIFIED -> entry.simplifiedProperty.value
Script.TRADITIONAL -> entry.traditionalProperty.value
}
},
config.script
)
)
labelPronunciation.textProperty().bind(
Bindings.createStringBinding(
{
when (config.searchResults.pronunciation.value!!) {
Pronunciation.PINYIN_WITH_TONE_MARKS -> entry.pinyinWithToneMarksProperty.value
Pronunciation.PINYIN_WITH_TONE_NUMBERS -> entry.pinyinWithToneNumbersProperty.value
Pronunciation.ZHUYIN -> entry.zhuyinProperty.value
}
},
config.searchResults.pronunciation
)
)
val definition = entry.definitions.joinToString(separator = " / ") { it.joinToString(separator = "; ") } val definition = entry.definitions.joinToString(separator = " / ") { it.joinToString(separator = "; ") }
labelDefinition.text = definition labelDefinition.text = definition

View File

@ -1,7 +1,8 @@
package com.marvinelsen.willow.ui.controllers package com.marvinelsen.willow.ui.controllers
import com.marvinelsen.willow.Config
import com.marvinelsen.willow.Model import com.marvinelsen.willow.Model
import com.marvinelsen.willow.config.Config
import com.marvinelsen.willow.config.Script
import com.marvinelsen.willow.ui.DictionaryEntryFx import com.marvinelsen.willow.ui.DictionaryEntryFx
import com.marvinelsen.willow.ui.cells.DictionaryEntryCellFactory import com.marvinelsen.willow.ui.cells.DictionaryEntryCellFactory
import javafx.beans.binding.Bindings import javafx.beans.binding.Bindings
@ -45,10 +46,18 @@ class DetailsController(private val model: Model, private val config: Config) {
@FXML @FXML
@Suppress("UnusedPrivateMember") @Suppress("UnusedPrivateMember")
private fun initialize() { private fun initialize() {
val headwordObjectBinding = labelHeadword.textProperty().bind(
Bindings.createStringBinding({ model.selectedEntry.value?.traditionalProperty?.value }, model.selectedEntry) Bindings.createStringBinding(
{
labelHeadword.textProperty().bind(headwordObjectBinding) when (config.script.value!!) {
Script.SIMPLIFIED -> model.selectedEntry.value?.simplifiedProperty?.value
Script.TRADITIONAL -> model.selectedEntry.value?.traditionalProperty?.value
}
},
config.script,
model.selectedEntry
)
)
labelHeadword labelHeadword
.styleProperty() .styleProperty()
.bind( .bind(
@ -61,7 +70,7 @@ class DetailsController(private val model: Model, private val config: Config) {
tabPaneDetails.disableProperty().bind(Bindings.isNull(model.selectedEntry)) tabPaneDetails.disableProperty().bind(Bindings.isNull(model.selectedEntry))
listViewWords.cellFactory = DictionaryEntryCellFactory(resources) listViewWords.cellFactory = DictionaryEntryCellFactory(resources, config)
listViewWords.items = model.wordsContaining listViewWords.items = model.wordsContaining
tabPaneDetails.selectionModel.selectedItemProperty().addListener { _, _, selectedTab -> tabPaneDetails.selectionModel.selectedItemProperty().addListener { _, _, selectedTab ->
if (model.selectedEntry.value == null) return@addListener if (model.selectedEntry.value == null) return@addListener

View File

@ -1,6 +1,7 @@
package com.marvinelsen.willow.ui.controllers package com.marvinelsen.willow.ui.controllers
import com.marvinelsen.willow.Model import com.marvinelsen.willow.Model
import com.marvinelsen.willow.config.Config
import com.marvinelsen.willow.ui.DictionaryEntryFx import com.marvinelsen.willow.ui.DictionaryEntryFx
import com.marvinelsen.willow.ui.cells.DictionaryEntryCellFactory import com.marvinelsen.willow.ui.cells.DictionaryEntryCellFactory
import javafx.beans.binding.Bindings import javafx.beans.binding.Bindings
@ -10,7 +11,7 @@ import javafx.scene.control.ListView
import javafx.scene.control.ProgressIndicator import javafx.scene.control.ProgressIndicator
import java.util.ResourceBundle import java.util.ResourceBundle
class ListController(private val model: Model) { class ListController(private val model: Model, private val config: Config) {
@FXML @FXML
private lateinit var resources: ResourceBundle private lateinit var resources: ResourceBundle
@ -27,7 +28,7 @@ class ListController(private val model: Model) {
@FXML @FXML
@Suppress("UnusedPrivateMember") @Suppress("UnusedPrivateMember")
private fun initialize() { private fun initialize() {
listViewSearchResults.cellFactory = DictionaryEntryCellFactory(resources) listViewSearchResults.cellFactory = DictionaryEntryCellFactory(resources, config)
listViewSearchResults.items = model.searchResults listViewSearchResults.items = model.searchResults
listViewSearchResults listViewSearchResults
.disableProperty() .disableProperty()

View File

@ -1,7 +1,7 @@
package com.marvinelsen.willow.ui.controllers package com.marvinelsen.willow.ui.controllers
import com.marvinelsen.willow.Config
import com.marvinelsen.willow.Model import com.marvinelsen.willow.Model
import com.marvinelsen.willow.config.Config
import com.marvinelsen.willow.ui.dialogs.PreferencesDialog import com.marvinelsen.willow.ui.dialogs.PreferencesDialog
import javafx.application.Platform import javafx.application.Platform
import javafx.beans.binding.Bindings import javafx.beans.binding.Bindings

View File

@ -0,0 +1,9 @@
package com.marvinelsen.willow.ui.converters
import javafx.util.StringConverter
import java.util.Locale
class LocaleStringConverter : StringConverter<Locale>() {
override fun toString(locale: Locale): String = locale.displayName
override fun fromString(string: String) = null
}

View File

@ -1,6 +1,6 @@
package com.marvinelsen.willow.ui.converters package com.marvinelsen.willow.ui.converters
import com.marvinelsen.willow.ui.Pronunciation import com.marvinelsen.willow.config.Pronunciation
import javafx.util.StringConverter import javafx.util.StringConverter
class PronunciationStringConverter : StringConverter<Pronunciation>() { class PronunciationStringConverter : StringConverter<Pronunciation>() {

View File

@ -0,0 +1,13 @@
package com.marvinelsen.willow.ui.converters
import com.marvinelsen.willow.config.Script
import javafx.util.StringConverter
class ScriptStringConverter : StringConverter<Script>() {
override fun toString(script: Script): String = when (script) {
Script.SIMPLIFIED -> "Simplified"
Script.TRADITIONAL -> "Traditional"
}
override fun fromString(string: String) = null
}

View File

@ -0,0 +1,14 @@
package com.marvinelsen.willow.ui.converters
import com.marvinelsen.willow.config.Theme
import javafx.util.StringConverter
class ThemeStringConverter : StringConverter<Theme>() {
override fun toString(theme: Theme): String = when (theme) {
Theme.SYSTEM -> "System"
Theme.LIGHT -> "Light"
Theme.DARK -> "Dark"
}
override fun fromString(string: String) = null
}

View File

@ -1,8 +1,10 @@
package com.marvinelsen.willow.ui.dialogs package com.marvinelsen.willow.ui.dialogs
import com.marvinelsen.willow.Config
import com.marvinelsen.willow.WillowApplication import com.marvinelsen.willow.WillowApplication
import com.marvinelsen.willow.ui.Pronunciation import com.marvinelsen.willow.config.Config
import com.marvinelsen.willow.config.Pronunciation
import com.marvinelsen.willow.config.Script
import com.marvinelsen.willow.config.Theme
import com.marvinelsen.willow.ui.formatters.FontSizeTextFormatter import com.marvinelsen.willow.ui.formatters.FontSizeTextFormatter
import javafx.fxml.FXML import javafx.fxml.FXML
import javafx.fxml.FXMLLoader import javafx.fxml.FXMLLoader
@ -16,6 +18,7 @@ import javafx.stage.Modality
import javafx.stage.Stage import javafx.stage.Stage
import javafx.stage.Window import javafx.stage.Window
import javafx.util.Callback import javafx.util.Callback
import java.util.Locale
class PreferencesDialog(owner: Window?, config: Config) : Dialog<PreferencesDialog.Result>() { class PreferencesDialog(owner: Window?, config: Config) : Dialog<PreferencesDialog.Result>() {
companion object { companion object {
@ -30,6 +33,15 @@ class PreferencesDialog(owner: Window?, config: Config) : Dialog<PreferencesDial
@FXML @FXML
private lateinit var comboBoxPronunciationDetails: ComboBox<Pronunciation> private lateinit var comboBoxPronunciationDetails: ComboBox<Pronunciation>
@FXML
private lateinit var comboBoxLocale: ComboBox<Locale>
@FXML
private lateinit var comboBoxTheme: ComboBox<Theme>
@FXML
private lateinit var comboBoxScript: ComboBox<Script>
@FXML @FXML
private lateinit var comboBoxPronunciationSearchResults: ComboBox<Pronunciation> private lateinit var comboBoxPronunciationSearchResults: ComboBox<Pronunciation>
@ -55,6 +67,11 @@ class PreferencesDialog(owner: Window?, config: Config) : Dialog<PreferencesDial
private lateinit var spinnerDefinitionFontSizeSearchResults: Spinner<Int> private lateinit var spinnerDefinitionFontSizeSearchResults: Spinner<Int>
private val entryHeadwordFontSizeObjectProperty = config.detailHeadwordFontSize.asObject() private val entryHeadwordFontSizeObjectProperty = config.detailHeadwordFontSize.asObject()
private val entryPronunciationFontSizeObjectProperty = config.detailPronunciationFontSize.asObject()
private val searchResultHeadwordFontSizeObjectProperty = config.searchResults.headwordFontSize.asObject()
private val searchResultPronunciationFontSizeObjectProperty = config.searchResults.pronunciationFontSize.asObject()
private val searchResultDefinitionFontSizeObjectProperty = config.searchResults.definitionFontSize.asObject()
init { init {
val loader = FXMLLoader(WillowApplication::class.java.getResource("/fxml/preferences-dialog.fxml")) val loader = FXMLLoader(WillowApplication::class.java.getResource("/fxml/preferences-dialog.fxml"))
@ -62,6 +79,7 @@ class PreferencesDialog(owner: Window?, config: Config) : Dialog<PreferencesDial
val root: DialogPane = loader.load() val root: DialogPane = loader.load()
dialogPane = root dialogPane = root
// TODO: Use resource bundle string
title = "Settings" title = "Settings"
isResizable = true isResizable = true
@ -73,11 +91,52 @@ class PreferencesDialog(owner: Window?, config: Config) : Dialog<PreferencesDial
minHeight = DIALOG_MIN_HEIGHT minHeight = DIALOG_MIN_HEIGHT
} }
comboBoxLocale.items.addAll(
setOf(
Locale.ENGLISH,
Locale.GERMAN,
Locale.CHINA,
Locale.TAIWAN
)
)
comboBoxTheme.valueProperty().bindBidirectional(config.theme)
comboBoxScript.valueProperty().bindBidirectional(config.script)
comboBoxLocale.valueProperty().bindBidirectional(config.locale)
comboBoxPronunciationSearchResults.valueProperty().bindBidirectional(config.searchResults.pronunciation)
checkBoxShowDefinitionSearchResults
.selectedProperty()
.bindBidirectional(config.searchResults.shouldShowDefinition)
checkBoxShowPronunciationSearchResults
.selectedProperty()
.bindBidirectional(config.searchResults.shouldShowPronunciation)
with(spinnerHeadwordFontSizeDetails) { with(spinnerHeadwordFontSizeDetails) {
editor.textFormatter = FontSizeTextFormatter() editor.textFormatter = FontSizeTextFormatter()
valueFactory.valueProperty().bindBidirectional(entryHeadwordFontSizeObjectProperty) valueFactory.valueProperty().bindBidirectional(entryHeadwordFontSizeObjectProperty)
} }
with(spinnerPronunciationFontSizeDetails) {
editor.textFormatter = FontSizeTextFormatter()
valueFactory.valueProperty().bindBidirectional(entryPronunciationFontSizeObjectProperty)
}
with(spinnerHeadwordFontSizeSearchResults) {
editor.textFormatter = FontSizeTextFormatter()
valueFactory.valueProperty().bindBidirectional(searchResultHeadwordFontSizeObjectProperty)
}
with(spinnerPronunciationFontSizeSearchResults) {
editor.textFormatter = FontSizeTextFormatter()
valueFactory.valueProperty().bindBidirectional(searchResultPronunciationFontSizeObjectProperty)
}
with(spinnerDefinitionFontSizeSearchResults) {
editor.textFormatter = FontSizeTextFormatter()
valueFactory.valueProperty().bindBidirectional(searchResultDefinitionFontSizeObjectProperty)
}
resultConverter = Callback(::convertToResult) resultConverter = Callback(::convertToResult)
} }

View File

@ -0,0 +1,3 @@
.preferences-dialog .content {
-fx-padding: 0;
}

View File

@ -1,19 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?import com.marvinelsen.willow.ui.converters.PronunciationStringConverter?> <?import com.marvinelsen.willow.config.*?>
<?import com.marvinelsen.willow.ui.*?> <?import com.marvinelsen.willow.ui.converters.*?>
<?import javafx.collections.FXCollections?> <?import javafx.collections.FXCollections?>
<?import javafx.geometry.Insets?> <?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?> <?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?> <?import javafx.scene.layout.*?>
<DialogPane styleClass="preferences-dialog" xmlns="http://javafx.com/javafx/23" xmlns:fx="http://javafx.com/fxml/1" <DialogPane styleClass="preferences-dialog" xmlns="http://javafx.com/javafx/23" xmlns:fx="http://javafx.com/fxml/1"
stylesheets="/css/main.css"> stylesheets="/css/preferences.css">
<fx:define> <fx:define>
<FXCollections fx:factory="observableArrayList" fx:id="phoneticAlphabets"> <FXCollections fx:factory="observableArrayList" fx:id="phoneticAlphabets">
<Pronunciation fx:value="PINYIN_WITH_TONE_MARKS"/> <Pronunciation fx:value="PINYIN_WITH_TONE_MARKS"/>
<Pronunciation fx:value="PINYIN_WITH_TONE_NUMBERS"/> <Pronunciation fx:value="PINYIN_WITH_TONE_NUMBERS"/>
<Pronunciation fx:value="ZHUYIN"/> <Pronunciation fx:value="ZHUYIN"/>
</FXCollections> </FXCollections>
<FXCollections fx:factory="observableArrayList" fx:id="themes">
<Theme fx:value="SYSTEM"/>
<Theme fx:value="LIGHT"/>
<Theme fx:value="DARK"/>
</FXCollections>
<FXCollections fx:factory="observableArrayList" fx:id="scripts">
<Script fx:value="SIMPLIFIED"/>
<Script fx:value="TRADITIONAL"/>
</FXCollections>
</fx:define> </fx:define>
<content> <content>
<TabPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" <TabPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
@ -26,12 +35,36 @@
</columnConstraints> </columnConstraints>
<rowConstraints> <rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" valignment="BASELINE" vgrow="NEVER"/> <RowConstraints minHeight="10.0" prefHeight="30.0" valignment="BASELINE" vgrow="NEVER"/>
<RowConstraints minHeight="10.0" prefHeight="30.0" valignment="BASELINE" vgrow="NEVER"/>
</rowConstraints> </rowConstraints>
<padding> <padding>
<Insets bottom="12.0" left="12.0" right="12.0" top="12.0"/> <Insets bottom="12.0" left="12.0" right="12.0" top="12.0"/>
</padding> </padding>
<Label text="Script:"/> <Label text="Script:" GridPane.rowIndex="3"/>
<ComboBox fx:id="comboBoxScript" GridPane.columnIndex="1"/> <ComboBox fx:id="comboBoxScript" items="$scripts" GridPane.columnIndex="1" GridPane.rowIndex="3">
<value>
<Script fx:value="SIMPLIFIED"/>
</value>
<converter>
<ScriptStringConverter/>
</converter>
</ComboBox>
<Separator prefWidth="200.0" GridPane.columnSpan="2147483647" GridPane.rowIndex="2"/>
<Label text="Language:" GridPane.rowIndex="0"/>
<ComboBox fx:id="comboBoxLocale" GridPane.rowIndex="0" GridPane.columnIndex="1">
<converter>
<LocaleStringConverter/>
</converter>
</ComboBox>
<Label text="Theme:" GridPane.rowIndex="1"/>
<ComboBox fx:id="comboBoxTheme" items="$themes" GridPane.rowIndex="1" GridPane.columnIndex="1">
<value>
<Theme fx:value="SYSTEM"/>
</value>
<converter>
<ThemeStringConverter/>
</converter>
</ComboBox>
</GridPane> </GridPane>
</Tab> </Tab>
<Tab closable="false" text="Details View"> <Tab closable="false" text="Details View">