From 892058c47e1914b833069156c89f2d2f49e1d18c Mon Sep 17 00:00:00 2001 From: Marvin Elsen Date: Wed, 6 Nov 2024 18:14:16 +0100 Subject: [PATCH] Lazyload listview cell elements --- .../ui/cells/DictionaryEntryCellFactory.kt | 76 +++++++++++-------- .../willow/ui/cells/SentenceCellFactory.kt | 14 +++- 2 files changed, 53 insertions(+), 37 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 0f7c817..403e9bf 100644 --- a/src/main/kotlin/com/marvinelsen/willow/ui/cells/DictionaryEntryCellFactory.kt +++ b/src/main/kotlin/com/marvinelsen/willow/ui/cells/DictionaryEntryCellFactory.kt @@ -39,50 +39,60 @@ private class EntryCell( private val hostServices: HostServices, ) : ListCell() { - private val labelHeadword = Label().apply { - styleClass.add("headword") - styleProperty().bind( - Bindings.concat( - "-fx-font-size: ", - config.searchResults.headwordFontSize.asString(), - "px;" + private val labelHeadword by lazy { + Label().apply { + styleClass.add("headword") + styleProperty().bind( + Bindings.concat( + "-fx-font-size: ", + config.searchResults.headwordFontSize.asString(), + "px;" + ) ) - ) + } } - private val labelDefinition = Label().apply { - styleClass.add("definition") - styleProperty().bind( - Bindings.concat( - "-fx-font-size: ", - config.searchResults.definitionFontSize.asString(), - "px;" + private val labelDefinition by lazy { + Label().apply { + 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) + visibleProperty().bind(config.searchResults.shouldShowDefinition) + managedProperty().bind(config.searchResults.shouldShowDefinition) + } } - private val labelPronunciation = Label().apply { - styleClass.add("pronunciation") - styleProperty().bind( - Bindings.concat( - "-fx-font-size: ", - config.searchResults.pronunciationFontSize.asString(), - "px;" + private val labelPronunciation by lazy { + Label().apply { + 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) + visibleProperty().bind(config.searchResults.shouldShowPronunciation) + managedProperty().bind(config.searchResults.shouldShowPronunciation) + } } - private val flowPane = FlowPane(labelHeadword, labelPronunciation).apply { - hgap = FLOW_PANE_HGAP - rowValignment = VPos.BASELINE + private val flowPane by lazy { + FlowPane(labelHeadword, labelPronunciation).apply { + hgap = FLOW_PANE_HGAP + rowValignment = VPos.BASELINE + } } - private val root = VBox(flowPane, labelDefinition).apply { - styleClass.add("search-result") + private val root by lazy { + VBox(flowPane, labelDefinition).apply { + styleClass.add("search-result") + } } init { 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 0025dfc..6883a76 100644 --- a/src/main/kotlin/com/marvinelsen/willow/ui/cells/SentenceCellFactory.kt +++ b/src/main/kotlin/com/marvinelsen/willow/ui/cells/SentenceCellFactory.kt @@ -36,11 +36,16 @@ private class SentenceCell( private val hostServices: HostServices, ) : ListCell() { - private val labelSentence = Label().apply { - styleClass.add("sentence") - isWrapText = true + private val labelSentence by lazy { + Label().apply { + styleClass.add("sentence") + isWrapText = true + } + } + + private val root by lazy { + VBox(labelSentence) } - private val root = VBox(labelSentence) init { text = null @@ -50,6 +55,7 @@ private class SentenceCell( super.updateItem(sentence, empty) if (empty || sentence == null) { graphic = null + contextMenu = null } else { labelSentence.textProperty().bind( Bindings.createStringBinding(