进度条在复制时更新进度

时间:2019-08-22 09:32:24

标签: kotlin tornadofx

我想从TornadoFX获得一个进度条,该进度条知道其最小值和最大值。

应该使用复制线程的信息来更新进度条:

 Files.copy(
    sourcePath,
    destinationPath,
    ExtendedCopyOption.INTERRUPTIBLE,
    StandardCopyOption.REPLACE_EXISTING
)

为了计算最大进度和当前进度,我使用了以下代码:

var mb = 1024
var fileSize = sourceFile.length() / mb
for (index in 0..fileSize) {
    updateMessage("Copying")
    updateProgress(index, fileSize)
    if (index == fileSize ) {
        updateMessage("Done"))
    }
}

总的来说,我这样的事情大大缩短了。

button {
    action {
        runAsync {
            Files.copy(
               sourcePath,
               destinationPath,
               ExtendedCopyOption.INTERRUPTIBLE,
               StandardCopyOption.REPLACE_EXISTING
            )
            var mb = 1024
            var fileSize = sourceFile.length() / mb
            for (index in 0..fileSize) {
                updateMessage("Copying")
                updateProgress(index, fileSize)
                if (index == fileSize ) {
                    updateMessage("Done").get())
                }
            }
        }
    }
}

progressbar(status.progress) {
    progress = 0.0
    minWidth = 250.0
    minHeight = 30.0
}

1 个答案:

答案 0 :(得分:1)

每个异步操作都会更新默认的TaskStatus模型,因此基本上您可以将TaskStatus模型注入到视图中并将其绑定到ProgressBar。 TaskStatus具有runningcompletedmessagetitleprogress的可观察属性。

我写了一篇简短的文章,并在实施该示例时创建了一个屏幕录像,您可能会发现它很有趣:

https://edvin.town/tornadofx-runasync-with-taskstatus/

您还可以传入特定的TaskStatus实例,甚至在需要时甚至可以重用用于实现此目的的模式。有关实现的详细信息,请参见框架的源代码。