All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jvmMain.vis.canvas.awt.AwtRepaintTimer.kt Maven / Gradle / Ivy

There is a newer version: 4.5.3-alpha1
Show newest version
/*
 * 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