This commit is contained in:
death-claw
2023-11-25 18:25:57 +01:00
committed by GitHub
parent 90625736e9
commit 7af0aeafa7
4 changed files with 37 additions and 26 deletions
@@ -1,5 +1,6 @@
package me.vripper.gui.view package me.vripper.gui.view
import javafx.event.EventHandler
import javafx.scene.control.ProgressIndicator import javafx.scene.control.ProgressIndicator
import javafx.scene.image.Image import javafx.scene.image.Image
import javafx.scene.image.ImageView import javafx.scene.image.ImageView
@@ -7,13 +8,15 @@ import javafx.scene.layout.HBox
import javafx.stage.Popup import javafx.stage.Popup
import javafx.stage.Stage import javafx.stage.Stage
import kotlinx.coroutines.* import kotlinx.coroutines.*
import me.vripper.delegate.LoggerDelegate
import me.vripper.gui.view.PreviewCache.previewDispatcher import me.vripper.gui.view.PreviewCache.previewDispatcher
import tornadofx.add import tornadofx.add
import tornadofx.runLater import tornadofx.runLater
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
class Preview(private val owner: Stage, val images: List<String>) { class Preview(private val owner: Stage, private val images: List<String>) {
private val log by LoggerDelegate()
private val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.IO) private val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
private var previewLoadJob: Job? = null private var previewLoadJob: Job? = null
val previewPopup = Popup() val previewPopup = Popup()
@@ -43,6 +46,9 @@ class Preview(private val owner: Stage, val images: List<String>) {
yield() yield()
runLater { runLater {
val hBox = HBox() val hBox = HBox()
hBox.onMouseEntered = EventHandler {
hide()
}
imageViewList.forEach { hBox.add(it) } imageViewList.forEach { hBox.add(it) }
previewPopup.content.clear() previewPopup.content.clear()
previewPopup.content.add(hBox) previewPopup.content.add(hBox)
@@ -54,16 +60,19 @@ class Preview(private val owner: Stage, val images: List<String>) {
private fun previewLoading(url: String): Deferred<ImageView?> { private fun previewLoading(url: String): Deferred<ImageView?> {
return coroutineScope.async(previewDispatcher) { return coroutineScope.async(previewDispatcher) {
val imageView = try { val imageView = try {
ImageView(Image(ByteArrayInputStream(PreviewCache.cache[url]))).apply { ByteArrayInputStream(PreviewCache.cache[url]).use {
isPreserveRatio = true ImageView(Image(it)).apply {
isPreserveRatio = true
fitWidth = if (image.width > 200.0) { fitWidth = if (image.width > 200.0) {
if (image.width > image.height) 200.0 * image.width / image.height else 200.0 if (image.width > image.height) 200.0 * image.width / image.height else 200.0
} else { } else {
200.0 200.0
}
} }
} }
} catch (e: Exception) { } catch (e: Exception) {
log.warn("Failed to load preview $url")
null null
} }
imageView imageView
@@ -28,7 +28,7 @@ object PreviewCache : KoinComponent {
.maximumWeight(1024 * 1024 * 100) .maximumWeight(1024 * 1024 * 100)
.build(::load) .build(::load)
fun load(url: String): ByteArray { private fun load(url: String): ByteArray {
val path = cachePath.resolve(url.hash256()) val path = cachePath.resolve(url.hash256())
if (Files.exists(path) && Files.isRegularFile(path)) { if (Files.exists(path) && Files.isRegularFile(path)) {
return Files.readAllBytes(path) return Files.readAllBytes(path)
@@ -163,23 +163,24 @@ class PostsTableView : View() {
cellFactory = Callback { cellFactory = Callback {
val cell = PreviewTableCell<PostModel>() val cell = PreviewTableCell<PostModel>()
cell.onMouseEntered = EventHandler { mouseEvent -> cell.onMouseEntered = EventHandler { mouseEvent ->
preview?.hide()
if (cell.tableRow.item != null && cell.tableRow.item.previewList.isNotEmpty()) { if (cell.tableRow.item != null && cell.tableRow.item.previewList.isNotEmpty()) {
preview = Preview(currentStage!!, cell.tableRow.item.previewList) preview = Preview(currentStage!!, cell.tableRow.item.previewList)
preview?.previewPopup?.apply { preview?.previewPopup?.apply {
x = mouseEvent.screenX + 20 x = mouseEvent.screenX + 20
y = mouseEvent.screenY + 10 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 cell
} }
} }
@@ -84,23 +84,24 @@ class ThreadSelectionTableView : Fragment("Thread") {
val cell = PreviewTableCell<ThreadSelectionModel>() val cell = PreviewTableCell<ThreadSelectionModel>()
cell.alignment = Pos.CENTER cell.alignment = Pos.CENTER
cell.onMouseEntered = EventHandler { mouseEvent -> cell.onMouseEntered = EventHandler { mouseEvent ->
preview?.hide()
if (cell.tableRow.item != null && cell.tableRow.item.previewList.isNotEmpty()) { if (cell.tableRow.item != null && cell.tableRow.item.previewList.isNotEmpty()) {
preview = Preview(currentStage!!, cell.tableRow.item.previewList) preview = Preview(currentStage!!, cell.tableRow.item.previewList)
preview?.previewPopup?.apply { preview?.previewPopup?.apply {
x = mouseEvent.screenX + 20 x = mouseEvent.screenX + 20
y = mouseEvent.screenY + 10 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 cell
} }
} }