Refactor ClipboardHelper

This commit is contained in:
Marvin Elsen 2024-09-26 10:29:30 +02:00
parent efde0dcdb4
commit 8350cb3b3d
Signed by: marvinelsen
GPG Key ID: 820672408CC318C2
3 changed files with 6 additions and 13 deletions

View File

@ -53,10 +53,10 @@ class Model(private val searchService: SearchService, private val findWordsServi
} }
fun copyHeadwordOfSelectedEntry() { fun copyHeadwordOfSelectedEntry() {
ClipboardHelper.copyHeadword(internalSelectedEntry.get()) ClipboardHelper.copyString(internalSelectedEntry.value.traditionalProperty.value)
} }
fun copyPronunciationOfSelectedEntry() { fun copyPronunciationOfSelectedEntry() {
ClipboardHelper.copyPronunciation(internalSelectedEntry.get()) ClipboardHelper.copyString(internalSelectedEntry.value.pinyinWithToneMarksProperty.value)
} }
} }

View File

@ -1,21 +1,14 @@
package com.marvinelsen.willow.ui.util package com.marvinelsen.willow.ui.util
import com.marvinelsen.willow.ui.DictionaryEntryFx
import javafx.scene.input.Clipboard import javafx.scene.input.Clipboard
import javafx.scene.input.ClipboardContent import javafx.scene.input.ClipboardContent
object ClipboardHelper { object ClipboardHelper {
private val systemClipboard = Clipboard.getSystemClipboard() private val systemClipboard = Clipboard.getSystemClipboard()
fun copyHeadword(entry: DictionaryEntryFx) { fun copyString(string: String) {
val clipboardContent = ClipboardContent() val clipboardContent = ClipboardContent()
clipboardContent.putString(entry.traditionalProperty.value) clipboardContent.putString(string)
systemClipboard.setContent(clipboardContent)
}
fun copyPronunciation(entry: DictionaryEntryFx) {
val clipboardContent = ClipboardContent()
clipboardContent.putString(entry.pinyinWithToneMarksProperty.value)
systemClipboard.setContent(clipboardContent) systemClipboard.setContent(clipboardContent)
} }
} }

View File

@ -9,12 +9,12 @@ import javafx.scene.control.MenuItem
fun createContextMenuForEntry(entry: DictionaryEntryFx) = ContextMenu().apply { fun createContextMenuForEntry(entry: DictionaryEntryFx) = ContextMenu().apply {
val menuItemCopyHeadword = val menuItemCopyHeadword =
MenuItem(WillowApplication.resourceBundle.getString("menubar.edit.copy.headword")).apply { MenuItem(WillowApplication.resourceBundle.getString("menubar.edit.copy.headword")).apply {
onAction = EventHandler { ClipboardHelper.copyHeadword(entry) } onAction = EventHandler { ClipboardHelper.copyString(entry.traditionalProperty.value) }
} }
val menuItemCopyPronunciation = val menuItemCopyPronunciation =
MenuItem(WillowApplication.resourceBundle.getString("menubar.edit.copy.pronunciation")).apply { MenuItem(WillowApplication.resourceBundle.getString("menubar.edit.copy.pronunciation")).apply {
onAction = EventHandler { ClipboardHelper.copyPronunciation(entry) } onAction = EventHandler { ClipboardHelper.copyString(entry.pinyinWithToneMarksProperty.value) }
} }
items.addAll(menuItemCopyHeadword, menuItemCopyPronunciation) items.addAll(menuItemCopyHeadword, menuItemCopyPronunciation)