From b8194f3ca602d6a48db6768d739b9a7ded45c2b6 Mon Sep 17 00:00:00 2001 From: Marvin Elsen Date: Sun, 3 Nov 2024 11:01:55 +0100 Subject: [PATCH] Add "Copy sentence" context menu for example sentences --- .../ui/cells/DictionaryEntryCellFactory.kt | 16 ++++++++++++---- .../willow/ui/cells/SentenceCellFactory.kt | 16 +++++++++++++--- .../willow/ui/controllers/DetailsController.kt | 2 +- .../willow/ui/util/ContextMenuUtils.kt | 10 ++++++++++ src/main/resources/i18n/willow.properties | 1 + src/main/resources/i18n/willow_de.properties | 1 + src/main/resources/i18n/willow_en.properties | 1 + src/main/resources/i18n/willow_zh_CN.properties | 1 + src/main/resources/i18n/willow_zh_TW.properties | 1 + 9 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/com/marvinelsen/willow/ui/cells/DictionaryEntryCellFactory.kt b/src/main/kotlin/com/marvinelsen/willow/ui/cells/DictionaryEntryCellFactory.kt index e6f40c1..b3fff59 100644 --- a/src/main/kotlin/com/marvinelsen/willow/ui/cells/DictionaryEntryCellFactory.kt +++ b/src/main/kotlin/com/marvinelsen/willow/ui/cells/DictionaryEntryCellFactory.kt @@ -15,8 +15,11 @@ import javafx.scene.layout.VBox import javafx.util.Callback import java.util.ResourceBundle -class DictionaryEntryCellFactory(private val resources: ResourceBundle, private val config: Config) : - Callback, ListCell> { +class DictionaryEntryCellFactory( + private val resources: ResourceBundle, + private val config: Config, +) : Callback, ListCell> { + override fun call(listView: ListView): ListCell { val entryCell = EntryCell(resources, config) entryCell.prefWidthProperty().bind(listView.widthProperty().subtract(CELL_PADDING)) @@ -28,8 +31,11 @@ class DictionaryEntryCellFactory(private val resources: ResourceBundle, private } } -private class EntryCell(private val resources: ResourceBundle, private val config: Config) : - ListCell() { +private class EntryCell( + private val resources: ResourceBundle, + private val config: Config, +) : ListCell() { + private val labelHeadword = Label().apply { styleClass.add("headword") styleProperty().bind( @@ -118,9 +124,11 @@ private class EntryCell(private val resources: ResourceBundle, private val confi entry.crossStraitsDefinitions.isNotEmpty() -> entry.crossStraitsDefinitions.joinToString( separator = " / " ) { it.definition } + entry.moedictDefinitions.isNotEmpty() -> entry.moedictDefinitions.joinToString( separator = " / " ) { it.definition } + else -> error("No definition for entry") } labelDefinition.text = definition diff --git a/src/main/kotlin/com/marvinelsen/willow/ui/cells/SentenceCellFactory.kt b/src/main/kotlin/com/marvinelsen/willow/ui/cells/SentenceCellFactory.kt index fa09266..dbfca87 100644 --- a/src/main/kotlin/com/marvinelsen/willow/ui/cells/SentenceCellFactory.kt +++ b/src/main/kotlin/com/marvinelsen/willow/ui/cells/SentenceCellFactory.kt @@ -3,17 +3,22 @@ package com.marvinelsen.willow.ui.cells import com.marvinelsen.willow.config.Config import com.marvinelsen.willow.config.Script import com.marvinelsen.willow.ui.SentenceFx +import com.marvinelsen.willow.ui.util.createContextMenuForSentence import javafx.beans.binding.Bindings import javafx.scene.control.Label import javafx.scene.control.ListCell import javafx.scene.control.ListView import javafx.scene.layout.VBox import javafx.util.Callback +import java.util.ResourceBundle -class SentenceCellFactory(private val config: Config) : Callback, ListCell> { +class SentenceCellFactory( + private val resources: ResourceBundle, + private val config: Config, +) : Callback, ListCell> { override fun call(listView: ListView): ListCell { - val sentenceCell = SentenceCell(config) + val sentenceCell = SentenceCell(resources, config) sentenceCell.prefWidthProperty().bind(listView.widthProperty().subtract(CELL_PADDING)) return sentenceCell } @@ -23,7 +28,11 @@ class SentenceCellFactory(private val config: Config) : Callback() { +private class SentenceCell( + private val resources: ResourceBundle, + private val config: Config, +) : ListCell() { + private val labelSentence = Label().apply { styleClass.add("sentence") isWrapText = true @@ -51,6 +60,7 @@ private class SentenceCell(private val config: Config) : ListCell() ) ) + contextMenu = createContextMenuForSentence(sentence, resources) graphic = root } } diff --git a/src/main/kotlin/com/marvinelsen/willow/ui/controllers/DetailsController.kt b/src/main/kotlin/com/marvinelsen/willow/ui/controllers/DetailsController.kt index 32619ce..56c47d1 100644 --- a/src/main/kotlin/com/marvinelsen/willow/ui/controllers/DetailsController.kt +++ b/src/main/kotlin/com/marvinelsen/willow/ui/controllers/DetailsController.kt @@ -192,7 +192,7 @@ class DetailsController(private val model: Model, private val config: Config) { private fun initializeListViewSentences() { listViewSentences.apply { - cellFactory = SentenceCellFactory(config) + cellFactory = SentenceCellFactory(resources, config) items = model.sentences disableProperty().bind(Bindings.or(model.isFindingSentences, Bindings.isEmpty(model.sentences))) diff --git a/src/main/kotlin/com/marvinelsen/willow/ui/util/ContextMenuUtils.kt b/src/main/kotlin/com/marvinelsen/willow/ui/util/ContextMenuUtils.kt index 6dcdcb6..20b4da6 100644 --- a/src/main/kotlin/com/marvinelsen/willow/ui/util/ContextMenuUtils.kt +++ b/src/main/kotlin/com/marvinelsen/willow/ui/util/ContextMenuUtils.kt @@ -1,6 +1,7 @@ package com.marvinelsen.willow.ui.util import com.marvinelsen.willow.ui.DictionaryEntryFx +import com.marvinelsen.willow.ui.SentenceFx import javafx.event.EventHandler import javafx.scene.control.ContextMenu import javafx.scene.control.MenuItem @@ -19,3 +20,12 @@ fun createContextMenuForEntry(entry: DictionaryEntryFx, resourceBundle: Resource items.addAll(menuItemCopyHeadword, menuItemCopyPronunciation) } + +fun createContextMenuForSentence(sentence: SentenceFx, resourceBundle: ResourceBundle) = ContextMenu().apply { + val menuItemCopySentence = + MenuItem(resourceBundle.getString("menubar.edit.copy.sentence")).apply { + onAction = EventHandler { ClipboardHelper.copyString(sentence.traditionalProperty.value) } + } + + items.addAll(menuItemCopySentence) +} diff --git a/src/main/resources/i18n/willow.properties b/src/main/resources/i18n/willow.properties index c417639..b65410d 100644 --- a/src/main/resources/i18n/willow.properties +++ b/src/main/resources/i18n/willow.properties @@ -25,6 +25,7 @@ menubar.file.preferences=_Preferences… menubar.edit=_Edit menubar.edit.copy.headword=Copy Headword menubar.edit.copy.pronunciation=Copy Pronunciation +menubar.edit.copy.sentence=Copy Sentence menubar.help=_Help menubar.help.about=_About… diff --git a/src/main/resources/i18n/willow_de.properties b/src/main/resources/i18n/willow_de.properties index 37de9d0..dd05813 100644 --- a/src/main/resources/i18n/willow_de.properties +++ b/src/main/resources/i18n/willow_de.properties @@ -25,6 +25,7 @@ menubar.file.preferences=_Einstellungen… menubar.edit=_Bearbeiten menubar.edit.copy.headword=Kopiere Wort menubar.edit.copy.pronunciation=Kopiere Aussprache +menubar.edit.copy.sentence=Copy Sentence menubar.help=_Hilfe menubar.help.about=_Über… diff --git a/src/main/resources/i18n/willow_en.properties b/src/main/resources/i18n/willow_en.properties index c417639..b65410d 100644 --- a/src/main/resources/i18n/willow_en.properties +++ b/src/main/resources/i18n/willow_en.properties @@ -25,6 +25,7 @@ menubar.file.preferences=_Preferences… menubar.edit=_Edit menubar.edit.copy.headword=Copy Headword menubar.edit.copy.pronunciation=Copy Pronunciation +menubar.edit.copy.sentence=Copy Sentence menubar.help=_Help menubar.help.about=_About… diff --git a/src/main/resources/i18n/willow_zh_CN.properties b/src/main/resources/i18n/willow_zh_CN.properties index 18c3466..88dc688 100644 --- a/src/main/resources/i18n/willow_zh_CN.properties +++ b/src/main/resources/i18n/willow_zh_CN.properties @@ -25,6 +25,7 @@ menubar.file.preferences=_設定… menubar.edit=_編輯 menubar.edit.copy.headword=複製 Wort menubar.edit.copy.pronunciation=複製 Aussprache +menubar.edit.copy.sentence=Copy Sentence menubar.help=_說明 menubar.help.about=_關於 Willow… diff --git a/src/main/resources/i18n/willow_zh_TW.properties b/src/main/resources/i18n/willow_zh_TW.properties index 59630a6..21ef796 100644 --- a/src/main/resources/i18n/willow_zh_TW.properties +++ b/src/main/resources/i18n/willow_zh_TW.properties @@ -25,6 +25,7 @@ menubar.file.preferences=_設定… menubar.edit=_編輯 menubar.edit.copy.headword=複製 Wort menubar.edit.copy.pronunciation=複製 Aussprache +menubar.edit.copy.sentence=Copy Sentence menubar.help=_說明 menubar.help.about=_關於 Willow…