Refactor details controller

This commit is contained in:
Marvin Elsen 2024-10-03 13:16:51 +02:00
parent 8ada0a5510
commit f6f2cfac5c
Signed by: marvinelsen
GPG Key ID: 820672408CC318C2
2 changed files with 94 additions and 93 deletions

View File

@ -25,36 +25,31 @@ import java.util.ResourceBundle
class DetailsController(private val model: Model, private val config: Config) { class DetailsController(private val model: Model, private val config: Config) {
@FXML @FXML
private lateinit var flowPaneSelectedEntry: FlowPane private lateinit var resources: ResourceBundle
@FXML
private lateinit var flowPaneHeader: FlowPane
@FXML
private lateinit var labelHeadword: Label
@FXML @FXML
private lateinit var labelPronunciation: Label private lateinit var labelPronunciation: Label
@FXML
private lateinit var resources: ResourceBundle
@FXML @FXML
private lateinit var tabPaneDetails: TabPane private lateinit var tabPaneDetails: TabPane
@FXML @FXML
private lateinit var webViewDefinition: WebView private lateinit var webViewDefinition: WebView
@FXML
@Suppress("UnusedPrivateProperty")
private lateinit var listviewSentences: ListView<DictionaryEntryFx>
@FXML @FXML
private lateinit var listViewWords: ListView<DictionaryEntryFx> private lateinit var listViewWords: ListView<DictionaryEntryFx>
@FXML @FXML
@Suppress("UnusedPrivateProperty")
private lateinit var listViewCharacters: ListView<DictionaryEntryFx> private lateinit var listViewCharacters: ListView<DictionaryEntryFx>
@FXML
private lateinit var labelHeadword: Label
@FXML @FXML
@Suppress("UnusedPrivateMember")
private fun initialize() { private fun initialize() {
initializeLabelHeadword() initializeLabelHeadword()
initializeLabelPronunciation() initializeLabelPronunciation()
@ -62,28 +57,74 @@ class DetailsController(private val model: Model, private val config: Config) {
initializeListViewWords() initializeListViewWords()
initializeListViewCharacters() initializeListViewCharacters()
initializeWebViewDefinition() initializeWebViewDefinition()
}
private fun initializeLabelHeadword() {
labelHeadword.apply {
textProperty().bind(
Bindings.createStringBinding(
{
val selectedEntry = model.selectedEntry.value
when (config.script.value!!) {
Script.SIMPLIFIED -> selectedEntry?.simplifiedProperty?.value
Script.TRADITIONAL -> selectedEntry?.traditionalProperty?.value
}
},
config.script,
model.selectedEntry
)
)
styleProperty().bind(
Bindings.concat(
"-fx-font-size: ",
config.details.headwordFontSize.asString(),
"px;"
)
)
}
}
private fun initializeLabelPronunciation() {
labelPronunciation.apply {
textProperty().bind(
Bindings.createStringBinding(
{
val selectedEntry = model.selectedEntry.value
when (config.details.pronunciation.value!!) {
Pronunciation.PINYIN_WITH_TONE_MARKS -> selectedEntry?.pinyinWithToneMarksProperty?.value
Pronunciation.PINYIN_WITH_TONE_NUMBERS -> selectedEntry?.pinyinWithToneNumbersProperty?.value
Pronunciation.ZHUYIN -> selectedEntry?.zhuyinProperty?.value
}
},
config.details.pronunciation,
model.selectedEntry
)
)
styleProperty().bind(
Bindings.concat(
"-fx-font-size: ",
config.details.pronunciationFontSize.asString(),
"px;"
)
)
}
}
private fun initializeTabPaneDetails() {
tabPaneDetails.apply {
disableProperty().bind(Bindings.isNull(model.selectedEntry))
selectionModel.selectedItemProperty().addListener { _, _, selectedTab ->
if (model.selectedEntry.value == null) return@addListener
lazyUpdateTabContent(selectedTab.id)
}
}
model.selectedEntry.addListener { _, _, newEntry -> model.selectedEntry.addListener { _, _, newEntry ->
if (newEntry == null) return@addListener if (newEntry == null) return@addListener
when (tabPaneDetails.selectionModel.selectedItem.id) { lazyUpdateTabContent(tabPaneDetails.selectionModel.selectedItem.id)
"tabWords" -> {
model.findWords()
}
"tabCharacters" -> {
model.findCharacters()
}
else -> {}
}
webViewDefinition.engine.loadContent(newEntry.createCedictDefinitionHtml())
}
}
private fun initializeWebViewDefinition() {
webViewDefinition.apply {
engine.userStyleSheetLocation = this::class.java.getResource("/css/definitions.css")!!.toExternalForm()
} }
} }
@ -101,68 +142,15 @@ class DetailsController(private val model: Model, private val config: Config) {
} }
} }
private fun initializeTabPaneDetails() { private fun initializeWebViewDefinition() {
tabPaneDetails.apply { webViewDefinition.apply {
disableProperty().bind(Bindings.isNull(model.selectedEntry)) engine.userStyleSheetLocation = this::class.java.getResource("/css/definitions.css")!!.toExternalForm()
selectionModel.selectedItemProperty().addListener { _, _, selectedTab ->
if (model.selectedEntry.value == null) return@addListener
when (selectedTab.id) {
"tabWords" -> {
model.findWords()
}
"tabCharacters" -> {
model.findCharacters()
}
else -> {}
}
}
} }
}
private fun initializeLabelHeadword() { model.selectedEntry.addListener { _, _, newEntry ->
labelHeadword.apply { if (newEntry == null) return@addListener
textProperty().bind(
Bindings.createStringBinding(
{
when (config.script.value!!) {
Script.SIMPLIFIED -> model.selectedEntry.value?.simplifiedProperty?.value
Script.TRADITIONAL -> model.selectedEntry.value?.traditionalProperty?.value
}
},
config.script,
model.selectedEntry
)
)
styleProperty().bind(Bindings.concat("-fx-font-size: ", config.details.headwordFontSize.asString(), "px;"))
}
}
private fun initializeLabelPronunciation() { webViewDefinition.engine.loadContent(newEntry.createCedictDefinitionHtml())
labelPronunciation.apply {
textProperty().bind(
Bindings.createStringBinding(
{
when (config.details.pronunciation.value!!) {
Pronunciation.PINYIN_WITH_TONE_MARKS -> model.selectedEntry.value?.pinyinWithToneMarksProperty?.value
Pronunciation.PINYIN_WITH_TONE_NUMBERS -> model.selectedEntry.value?.pinyinWithToneNumbersProperty?.value
Pronunciation.ZHUYIN -> model.selectedEntry.value?.zhuyinProperty?.value
}
},
config.details.pronunciation,
model.selectedEntry
)
)
styleProperty().bind(
Bindings.concat(
"-fx-font-size: ",
config.details.pronunciationFontSize.asString(),
"px;"
)
)
} }
} }
@ -181,13 +169,26 @@ class DetailsController(private val model: Model, private val config: Config) {
} }
} }
fun showSelectedEntryContextMenu(contextMenuEvent: ContextMenuEvent) { @FXML
private fun headerOnContextMenuRequested(contextMenuEvent: ContextMenuEvent) {
if (model.selectedEntry.value == null) return if (model.selectedEntry.value == null) return
createContextMenuForEntry(model.selectedEntry.value, resources).show( createContextMenuForEntry(model.selectedEntry.value, resources).show(
flowPaneSelectedEntry.scene.window, flowPaneHeader.scene.window,
contextMenuEvent.screenX, contextMenuEvent.screenX,
contextMenuEvent.screenY contextMenuEvent.screenY
) )
} }
private fun lazyUpdateTabContent(selectedTabId: String?) = when (selectedTabId) {
"tabWords" -> {
model.findWords()
}
"tabCharacters" -> {
model.findCharacters()
}
else -> {}
}
} }

View File

@ -8,7 +8,7 @@
<VBox xmlns="http://javafx.com/javafx/23" xmlns:fx="http://javafx.com/fxml/1" <VBox xmlns="http://javafx.com/javafx/23" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.marvinelsen.willow.ui.controllers.DetailsController" fx:controller="com.marvinelsen.willow.ui.controllers.DetailsController"
stylesheets="/css/details.css"> stylesheets="/css/details.css">
<FlowPane fx:id="flowPaneSelectedEntry" hgap="8.0" onContextMenuRequested="#showSelectedEntryContextMenu" <FlowPane fx:id="flowPaneHeader" hgap="8.0" onContextMenuRequested="#headerOnContextMenuRequested"
prefHeight="38.0" prefWidth="412.0" rowValignment="BASELINE" vgap="8.0" VBox.vgrow="NEVER"> prefHeight="38.0" prefWidth="412.0" rowValignment="BASELINE" vgap="8.0" VBox.vgrow="NEVER">
<padding> <padding>
<Insets bottom="6.0" left="6.0" right="6.0" top="6.0"/> <Insets bottom="6.0" left="6.0" right="6.0" top="6.0"/>
@ -26,7 +26,7 @@
<WebView fx:id="webViewDefinition" minHeight="-1.0" minWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0"/> <WebView fx:id="webViewDefinition" minHeight="-1.0" minWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0"/>
</Tab> </Tab>
<Tab id="tabSentences" closable="false" text="%tab.sentences"> <Tab id="tabSentences" closable="false" text="%tab.sentences">
<ListView fx:id="listviewSentences"/> <ListView fx:id="listViewSentences"/>
</Tab> </Tab>
<Tab id="tabWords" closable="false" text="%tab.words"> <Tab id="tabWords" closable="false" text="%tab.words">
<ListView fx:id="listViewWords"/> <ListView fx:id="listViewWords"/>