From 7af0aeafa759c30cc71c04c8964efa4a930553ff Mon Sep 17 00:00:00 2001 From: death-claw <53543762+death-claw@users.noreply.github.com> Date: Sat, 25 Nov 2023 18:25:57 +0100 Subject: [PATCH] fixes #137 (#138) --- .../kotlin/me/vripper/gui/view/Preview.kt | 23 +++++++++++++------ .../me/vripper/gui/view/PreviewCache.kt | 2 +- .../vripper/gui/view/tables/PostsTableView.kt | 19 +++++++-------- .../view/tables/ThreadSelectionTableView.kt | 19 +++++++-------- 4 files changed, 37 insertions(+), 26 deletions(-) diff --git a/vripper-gui/src/main/kotlin/me/vripper/gui/view/Preview.kt b/vripper-gui/src/main/kotlin/me/vripper/gui/view/Preview.kt index 506556c5..75df9891 100644 --- a/vripper-gui/src/main/kotlin/me/vripper/gui/view/Preview.kt +++ b/vripper-gui/src/main/kotlin/me/vripper/gui/view/Preview.kt @@ -1,5 +1,6 @@ package me.vripper.gui.view +import javafx.event.EventHandler import javafx.scene.control.ProgressIndicator import javafx.scene.image.Image import javafx.scene.image.ImageView @@ -7,13 +8,15 @@ import javafx.scene.layout.HBox import javafx.stage.Popup import javafx.stage.Stage import kotlinx.coroutines.* +import me.vripper.delegate.LoggerDelegate import me.vripper.gui.view.PreviewCache.previewDispatcher import tornadofx.add import tornadofx.runLater import java.io.ByteArrayInputStream -class Preview(private val owner: Stage, val images: List) { +class Preview(private val owner: Stage, private val images: List) { + private val log by LoggerDelegate() private val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.IO) private var previewLoadJob: Job? = null val previewPopup = Popup() @@ -43,6 +46,9 @@ class Preview(private val owner: Stage, val images: List) { yield() runLater { val hBox = HBox() + hBox.onMouseEntered = EventHandler { + hide() + } imageViewList.forEach { hBox.add(it) } previewPopup.content.clear() previewPopup.content.add(hBox) @@ -54,16 +60,19 @@ class Preview(private val owner: Stage, val images: List) { private fun previewLoading(url: String): Deferred { return coroutineScope.async(previewDispatcher) { val imageView = try { - ImageView(Image(ByteArrayInputStream(PreviewCache.cache[url]))).apply { - isPreserveRatio = true + ByteArrayInputStream(PreviewCache.cache[url]).use { + ImageView(Image(it)).apply { + isPreserveRatio = true - fitWidth = if (image.width > 200.0) { - if (image.width > image.height) 200.0 * image.width / image.height else 200.0 - } else { - 200.0 + fitWidth = if (image.width > 200.0) { + if (image.width > image.height) 200.0 * image.width / image.height else 200.0 + } else { + 200.0 + } } } } catch (e: Exception) { + log.warn("Failed to load preview $url") null } imageView diff --git a/vripper-gui/src/main/kotlin/me/vripper/gui/view/PreviewCache.kt b/vripper-gui/src/main/kotlin/me/vripper/gui/view/PreviewCache.kt index e78095d9..4132748b 100644 --- a/vripper-gui/src/main/kotlin/me/vripper/gui/view/PreviewCache.kt +++ b/vripper-gui/src/main/kotlin/me/vripper/gui/view/PreviewCache.kt @@ -28,7 +28,7 @@ object PreviewCache : KoinComponent { .maximumWeight(1024 * 1024 * 100) .build(::load) - fun load(url: String): ByteArray { + private fun load(url: String): ByteArray { val path = cachePath.resolve(url.hash256()) if (Files.exists(path) && Files.isRegularFile(path)) { return Files.readAllBytes(path) diff --git a/vripper-gui/src/main/kotlin/me/vripper/gui/view/tables/PostsTableView.kt b/vripper-gui/src/main/kotlin/me/vripper/gui/view/tables/PostsTableView.kt index 2455d836..82271a08 100644 --- a/vripper-gui/src/main/kotlin/me/vripper/gui/view/tables/PostsTableView.kt +++ b/vripper-gui/src/main/kotlin/me/vripper/gui/view/tables/PostsTableView.kt @@ -163,23 +163,24 @@ class PostsTableView : View() { cellFactory = Callback { val cell = PreviewTableCell() cell.onMouseEntered = EventHandler { mouseEvent -> + preview?.hide() if (cell.tableRow.item != null && cell.tableRow.item.previewList.isNotEmpty()) { preview = Preview(currentStage!!, cell.tableRow.item.previewList) preview?.previewPopup?.apply { x = mouseEvent.screenX + 20 y = mouseEvent.screenY + 10 } + cell.onMouseMoved = EventHandler { + preview?.previewPopup?.apply { + x = it.screenX + 20 + y = it.screenY + 10 + } + } + cell.onMouseExited = EventHandler { + preview?.hide() + } } } - cell.onMouseMoved = EventHandler { - preview?.previewPopup?.apply { - x = it.screenX + 20 - y = it.screenY + 10 - } - } - cell.onMouseExited = EventHandler { - preview?.hide() - } cell } } diff --git a/vripper-gui/src/main/kotlin/me/vripper/gui/view/tables/ThreadSelectionTableView.kt b/vripper-gui/src/main/kotlin/me/vripper/gui/view/tables/ThreadSelectionTableView.kt index 23fc786c..c02b158d 100644 --- a/vripper-gui/src/main/kotlin/me/vripper/gui/view/tables/ThreadSelectionTableView.kt +++ b/vripper-gui/src/main/kotlin/me/vripper/gui/view/tables/ThreadSelectionTableView.kt @@ -84,23 +84,24 @@ class ThreadSelectionTableView : Fragment("Thread") { val cell = PreviewTableCell() cell.alignment = Pos.CENTER cell.onMouseEntered = EventHandler { mouseEvent -> + preview?.hide() if (cell.tableRow.item != null && cell.tableRow.item.previewList.isNotEmpty()) { preview = Preview(currentStage!!, cell.tableRow.item.previewList) preview?.previewPopup?.apply { x = mouseEvent.screenX + 20 y = mouseEvent.screenY + 10 } + cell.onMouseMoved = EventHandler { + preview?.previewPopup?.apply { + x = it.screenX + 20 + y = it.screenY + 10 + } + } + cell.onMouseExited = EventHandler { + preview?.hide() + } } } - cell.onMouseMoved = EventHandler { - preview?.previewPopup?.apply { - x = it.screenX + 20 - y = it.screenY + 10 - } - } - cell.onMouseExited = EventHandler { - preview?.hide() - } cell } }