Skip to content

Commit

Permalink
fixes #137 (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-claw authored Nov 25, 2023
1 parent 9062573 commit 7af0aea
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 26 deletions.
23 changes: 16 additions & 7 deletions vripper-gui/src/main/kotlin/me/vripper/gui/view/Preview.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package me.vripper.gui.view

import javafx.event.EventHandler
import javafx.scene.control.ProgressIndicator
import javafx.scene.image.Image
import javafx.scene.image.ImageView
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()
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit 7af0aea

Please sign in to comment.