Kotlin Progressbar在复制时更新进度

Kotlin Progressbar在复制时更新进度,kotlin,tornadofx,Kotlin,Tornadofx,我想从TornadoFX获得一个进度条,它知道它的最小值和最大值 应使用复制线程的信息更新progressbar: Files.copy( sourcePath, destinationPath, ExtendedCopyOption.INTERRUPTIBLE, StandardCopyOption.REPLACE_EXISTING ) 为了计算最大值和当前进度,我使用了以下代码: var mb = 1024 var fileSize = sourceFil

我想从TornadoFX获得一个进度条,它知道它的最小值和最大值

应使用复制线程的信息更新progressbar:

 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
}

每个异步操作都将更新默认的
TaskStatus
模型,因此基本上您可以将
TaskStatus
模型插入视图中,并将其绑定到
ProgressBar
。TaskStatus具有以下可观察属性:
正在运行
已完成
消息
标题
进度

我写了一篇短文,并创建了一个屏幕回放,当这个实现时,您可能会发现它很有趣:

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