Split preferences dialog into Dialog and Controller

This commit is contained in:
Marvin Elsen 2024-10-03 21:33:07 +02:00
parent d48c1b2bf2
commit ee989b8997
Signed by: marvinelsen
GPG Key ID: 820672408CC318C2
4 changed files with 142 additions and 116 deletions

View File

@ -8,9 +8,13 @@ import javafx.beans.binding.Bindings
import javafx.fxml.FXML import javafx.fxml.FXML
import javafx.scene.control.MenuBar import javafx.scene.control.MenuBar
import javafx.scene.control.MenuItem import javafx.scene.control.MenuItem
import java.util.ResourceBundle
@Suppress("UnusedPrivateMember") @Suppress("UnusedPrivateMember")
class MenuController(private val model: Model, private val config: Config) { class MenuController(private val model: Model, private val config: Config) {
@FXML
private lateinit var resources: ResourceBundle
@FXML @FXML
private lateinit var menuBar: MenuBar private lateinit var menuBar: MenuBar
@ -28,10 +32,10 @@ class MenuController(private val model: Model, private val config: Config) {
@FXML @FXML
private fun onMenuItemPreferencesAction() { private fun onMenuItemPreferencesAction() {
PreferencesDialog(menuBar.scene.window, config).showAndWait().ifPresent { result -> PreferencesDialog(menuBar.scene.window, config, resources).showAndWait().ifPresent { result ->
when (result) { when (result) {
PreferencesDialog.Result.CHANGES -> config.save() PreferencesDialog.Result.SAVE_CHANGES -> config.save()
PreferencesDialog.Result.NO_CHANGES -> config.load() PreferencesDialog.Result.DO_NOT_SAVE_CHANGES -> config.load()
} }
} }
} }

View File

@ -0,0 +1,108 @@
package com.marvinelsen.willow.ui.controllers.dialogs
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 javafx.fxml.FXML
import javafx.scene.control.CheckBox
import javafx.scene.control.ComboBox
import javafx.scene.control.Spinner
import java.util.Locale
@Suppress("UnusedPrivateMember")
class PreferencesDialogController(private val config: Config) {
@FXML
private lateinit var comboBoxLocale: ComboBox<Locale>
@FXML
private lateinit var comboBoxTheme: ComboBox<Theme>
@FXML
private lateinit var comboBoxScript: ComboBox<Script>
@FXML
private lateinit var comboBoxPronunciationSearchResults: ComboBox<Pronunciation>
@FXML
private lateinit var comboBoxPronunciationDetails: ComboBox<Pronunciation>
@FXML
private lateinit var checkBoxShowPronunciationSearchResults: CheckBox
@FXML
private lateinit var checkBoxShowDefinitionSearchResults: CheckBox
@FXML
private lateinit var spinnerHeadwordFontSizeDetails: Spinner<Int>
@FXML
private lateinit var spinnerPronunciationFontSizeDetails: Spinner<Int>
@FXML
private lateinit var spinnerHeadwordFontSizeSearchResults: Spinner<Int>
@FXML
private lateinit var spinnerPronunciationFontSizeSearchResults: Spinner<Int>
@FXML
private lateinit var spinnerDefinitionFontSizeSearchResults: Spinner<Int>
private val entryHeadwordFontSizeObjectProperty = config.details.headwordFontSize.asObject()
private val entryPronunciationFontSizeObjectProperty = config.details.pronunciationFontSize.asObject()
private val searchResultHeadwordFontSizeObjectProperty = config.searchResults.headwordFontSize.asObject()
private val searchResultPronunciationFontSizeObjectProperty = config.searchResults.pronunciationFontSize.asObject()
private val searchResultDefinitionFontSizeObjectProperty = config.searchResults.definitionFontSize.asObject()
@FXML
private fun initialize() {
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)
comboBoxPronunciationDetails.valueProperty().bindBidirectional(config.details.pronunciation)
checkBoxShowDefinitionSearchResults
.selectedProperty()
.bindBidirectional(config.searchResults.shouldShowDefinition)
checkBoxShowPronunciationSearchResults
.selectedProperty()
.bindBidirectional(config.searchResults.shouldShowPronunciation)
with(spinnerHeadwordFontSizeDetails) {
editor.textFormatter = FontSizeTextFormatter()
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)
}
}
}

View File

@ -1,82 +1,42 @@
package com.marvinelsen.willow.ui.dialogs package com.marvinelsen.willow.ui.dialogs
import com.marvinelsen.willow.WillowApplication
import com.marvinelsen.willow.config.Config import com.marvinelsen.willow.config.Config
import com.marvinelsen.willow.config.Pronunciation import com.marvinelsen.willow.ui.controllers.dialogs.PreferencesDialogController
import com.marvinelsen.willow.config.Script
import com.marvinelsen.willow.config.Theme
import com.marvinelsen.willow.ui.formatters.FontSizeTextFormatter
import javafx.fxml.FXML
import javafx.fxml.FXMLLoader import javafx.fxml.FXMLLoader
import javafx.scene.control.ButtonType import javafx.scene.control.ButtonType
import javafx.scene.control.CheckBox
import javafx.scene.control.ComboBox
import javafx.scene.control.Dialog import javafx.scene.control.Dialog
import javafx.scene.control.DialogPane import javafx.scene.control.DialogPane
import javafx.scene.control.Spinner
import javafx.stage.Modality 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 import java.util.ResourceBundle
class PreferencesDialog(owner: Window?, config: Config) : Dialog<PreferencesDialog.Result>() { class PreferencesDialog(
owner: Window?,
config: Config,
resources: ResourceBundle,
) : Dialog<PreferencesDialog.Result>() {
companion object { companion object {
private const val DIALOG_MIN_HEIGHT = 400.0 private const val MIN_HEIGHT = 400.0
private const val DIALOG_MIN_WIDTH = 400.0 private const val MIN_WIDTH = 400.0
} }
enum class Result { enum class Result {
CHANGES, NO_CHANGES SAVE_CHANGES, DO_NOT_SAVE_CHANGES
} }
@FXML
private lateinit var comboBoxLocale: ComboBox<Locale>
@FXML
private lateinit var comboBoxTheme: ComboBox<Theme>
@FXML
private lateinit var comboBoxScript: ComboBox<Script>
@FXML
private lateinit var comboBoxPronunciationSearchResults: ComboBox<Pronunciation>
@FXML
private lateinit var comboBoxPronunciationDetails: ComboBox<Pronunciation>
@FXML
private lateinit var checkBoxShowPronunciationSearchResults: CheckBox
@FXML
private lateinit var checkBoxShowDefinitionSearchResults: CheckBox
@FXML
private lateinit var spinnerHeadwordFontSizeDetails: Spinner<Int>
@FXML
private lateinit var spinnerPronunciationFontSizeDetails: Spinner<Int>
@FXML
private lateinit var spinnerHeadwordFontSizeSearchResults: Spinner<Int>
@FXML
private lateinit var spinnerPronunciationFontSizeSearchResults: Spinner<Int>
@FXML
private lateinit var spinnerDefinitionFontSizeSearchResults: Spinner<Int>
private val entryHeadwordFontSizeObjectProperty = config.details.headwordFontSize.asObject()
private val entryPronunciationFontSizeObjectProperty = config.details.pronunciationFontSize.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/dialogs/preferences.fxml")) val fxmlLoader = FXMLLoader()
loader.setController(this) fxmlLoader.resources = resources
val root: DialogPane = loader.load() fxmlLoader.controllerFactory = Callback { type ->
when (type) {
PreferencesDialogController::class.java -> PreferencesDialogController(config)
else -> error("Trying to instantiate unknown controller type $type")
}
}
val root = fxmlLoader.load(javaClass.getResourceAsStream("/fxml/dialogs/preferences.fxml")) as DialogPane
dialogPane = root dialogPane = root
title = "Settings" title = "Settings"
@ -86,55 +46,8 @@ class PreferencesDialog(owner: Window?, config: Config) : Dialog<PreferencesDial
initModality(Modality.APPLICATION_MODAL) initModality(Modality.APPLICATION_MODAL)
(dialogPane.scene.window as Stage).apply { (dialogPane.scene.window as Stage).apply {
minWidth = DIALOG_MIN_WIDTH minWidth = MIN_WIDTH
minHeight = DIALOG_MIN_HEIGHT minHeight = 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)
comboBoxPronunciationDetails.valueProperty().bindBidirectional(config.details.pronunciation)
checkBoxShowDefinitionSearchResults
.selectedProperty()
.bindBidirectional(config.searchResults.shouldShowDefinition)
checkBoxShowPronunciationSearchResults
.selectedProperty()
.bindBidirectional(config.searchResults.shouldShowPronunciation)
with(spinnerHeadwordFontSizeDetails) {
editor.textFormatter = FontSizeTextFormatter()
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)
@ -142,7 +55,7 @@ class PreferencesDialog(owner: Window?, config: Config) : Dialog<PreferencesDial
private fun convertToResult(buttonType: ButtonType) = private fun convertToResult(buttonType: ButtonType) =
when (buttonType) { when (buttonType) {
ButtonType.APPLY -> Result.CHANGES ButtonType.APPLY -> Result.SAVE_CHANGES
else -> Result.NO_CHANGES else -> Result.DO_NOT_SAVE_CHANGES
} }
} }

View File

@ -7,7 +7,8 @@
<?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/preferences.css"> stylesheets="/css/preferences.css"
fx:controller="com.marvinelsen.willow.ui.controllers.dialogs.PreferencesDialogController">
<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"/>
@ -169,6 +170,6 @@
</Tab> </Tab>
</TabPane> </TabPane>
</content> </content>
<ButtonType fx:id="buttonTypeApply" fx:constant="APPLY"/> <ButtonType fx:constant="APPLY"/>
<ButtonType fx:constant="CANCEL"/> <ButtonType fx:constant="CANCEL"/>
</DialogPane> </DialogPane>