Scoop API

scoop:scoop.ui:0.1.0

src/layout/paint.scoop

Selected production source · src/layout/paint.scoop

1package scoop.ui
3import scoop.graphics.Canvas
4import scoop.graphics.Size
5import scoop.platform.time.MonotonicInstant
7/** A pure drawing borrow. Input and child placement belong to SceneContext. */
8public class PaintContext internal constructor(
9 private val root: UiRoot,
10 private val target: Canvas,
11 private val extent: Size,
12 private val sample: MonotonicInstant
13) {
14 private var active = true
15 private var borrowed = false
16 public val canvas: Canvas get() {
17 checkActive()
18 return target
19 }
20 public val size: Size get() {
21 checkActive()
22 return extent
23 }
24 public val time: MonotonicInstant get() {
25 checkActive()
26 observeFrameTime()
27 return sample
28 }
29 private fun checkActive() {
30 root.checkThread()
31 if (!active)
32 throw IllegalStateException(Some("paint context has ended"))
33 if (borrowed)
34 throw IllegalStateException(Some("paint context has an active child scope"))
35 }
36 /** Isolates pixel operations without changing the surrounding scene geometry. */
37 public fun <T> withState(block: (PaintContext) -> T): T {
38 checkActive()
39 borrowed = true
40 try {
41 return target.withState {
42 canvas ->
43 val child = PaintContext(root, canvas, extent, sample)
44 try {
45 block(child)
46 } finally {
47 child.end()
48 }
49 }
50 } finally {
51 borrowed = false
52 }
53 }
54 internal fun end() {
55 active = false
56 }