Skip to content

Commit

Permalink
Merge pull request #67 from JetBrains/schedule-redraw-onresize
Browse files Browse the repository at this point in the history
Schedule redraw on resize and initialization
  • Loading branch information
igordmn authored Feb 20, 2021
2 parents f1ce1d4 + 9b9bea5 commit 324a21c
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions skiko/src/jvmMain/kotlin/org/jetbrains/skiko/SkiaLayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ import org.jetbrains.skija.PictureRecorder
import org.jetbrains.skija.Rect
import org.jetbrains.skiko.context.ContextHandler
import org.jetbrains.skiko.context.createContextHandler
import org.jetbrains.skiko.context.SoftwareContextHandler
import org.jetbrains.skiko.redrawer.SoftwareRedrawer
import org.jetbrains.skiko.redrawer.Redrawer
import java.awt.Graphics
import javax.swing.SwingUtilities.invokeLater
import javax.swing.SwingUtilities.isEventDispatchThread
import kotlin.collections.MutableList
import kotlin.collections.toMutableList

interface SkiaRenderer {
fun onRender(canvas: Canvas, width: Int, height: Int, nanoTime: Long)
Expand Down Expand Up @@ -45,7 +42,7 @@ open class SkiaLayer(
contextHandler = createContextHandler(this, initialRenderApi)
redrawer = platformOperations.createRedrawer(this, initialRenderApi, properties)
redrawer?.syncSize()
redrawer?.redrawImmediately()
redraw()
}

override fun dispose() {
Expand All @@ -62,15 +59,35 @@ open class SkiaLayer(
override fun setBounds(x: Int, y: Int, width: Int, height: Int) {
super.setBounds(x, y, width, height)
redrawer?.syncSize()
redrawer?.redrawImmediately()
redraw()
}

override fun paint(g: Graphics) {
super.paint(g)
redrawer?.syncSize()
needRedraw()
redrawer?.redrawImmediately()
}

private var redrawScheduled = false

/**
* Redraw as soon as possible (but not right now)
*/
fun redraw() {
if (!redrawScheduled) {
redrawScheduled = true
invokeLater {
redrawScheduled = false
if (!isDisposed) {
redrawer?.redrawImmediately()
}
}
}
}

/**
* Redraw on the next animation Frame (on vsync signal if vsync is enabled).
*/
fun needRedraw() {
check(!isDisposed)
check(isEventDispatchThread())
Expand Down

0 comments on commit 324a21c

Please sign in to comment.