jvmMain.vis.canvas.awt.AwtRepaintTimer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lets-plot-common Show documentation
Show all versions of lets-plot-common Show documentation
Lets-Plot JVM package without rendering part
/*
* Copyright (c) 2020. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
package jetbrains.datalore.vis.canvas.awt
import jetbrains.datalore.base.registration.Disposable
import java.awt.EventQueue
import java.awt.event.ActionListener
import javax.swing.Timer
class AwtRepaintTimer(
repaint: () -> Unit,
val executor: (() -> Unit) -> Unit = { f -> EventQueue.invokeLater { f() } },
) : Disposable {
private val myHandlers = ArrayList<(Long) -> Unit>()
private var actionListener = ActionListener {
myHandlers.forEach {
executor {
it(System.currentTimeMillis())
}
}
repaint()
}
private val myTimer: Timer = Timer(1000 / 60, actionListener)
fun addHandler(handler: (Long) -> Unit) {
synchronized(myHandlers) {
myHandlers.add(handler)
if (!myTimer.isRunning) {
myTimer.start()
}
}
}
fun removeHandler(handler: (Long) -> Unit) {
synchronized(myHandlers) {
myHandlers.remove(handler)
if (myHandlers.isEmpty() && myTimer.isRunning) {
myTimer.stop()
}
}
}
override fun dispose() {
myTimer.stop()
myTimer.removeActionListener(actionListener)
actionListener = ActionListener {}
myHandlers.clear()
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy