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
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<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 var previewLoadJob: Job? = null
val previewPopup = Popup()
@@ -43,6 +46,9 @@ class Preview(private val owner: Stage, val images: List<String>) {
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<String>) {
private fun previewLoading(url: String): Deferred<ImageView?> {
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
@@ -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)
@@ -163,23 +163,24 @@ class PostsTableView : View() {
cellFactory = Callback {
val cell = PreviewTableCell<PostModel>()
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
}
}
@@ -84,23 +84,24 @@ class ThreadSelectionTableView : Fragment("Thread") {
val cell = PreviewTableCell<ThreadSelectionModel>()
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
}
}