Scoop API

scoop:scoop.ui.style:0.1.0

src/theme/painter.scoop

Selected production source · src/theme/painter.scoop

1package scoop.ui.style
3import scoop.graphics.*
4import scoop.platform.concurrent.currentThreadId
6internal class ShapeThemePainter(
7 private val scrollbar: ScrollbarPart,
8 private val disabledOpacity: Double
9) : ThemePainter {
10 private val owner = currentThreadId()
11 private var closed = false
12 private var painting = false
13 private val background = StylePainter()
14 private val foreground = StylePainter()
15 private val marker = StylePainter()
16 private val focusPainter = StylePainter()
17 public override val isClosed: Boolean get() = closed
19 private fun checkThread() {
20 if (owner != currentThreadId())
21 throw IllegalStateException(Some("theme painter requires its owner thread"))
22 }
23 private fun draw(canvas: Canvas, block: (Canvas) -> Unit) {
24 checkThread()
25 if (closed || painting)
26 throw IllegalStateException(Some("theme painter is closed or has an active draw"))
27 painting = true
28 try {
29 canvas.withState(block)
30 } finally {
31 painting = false
32 }
33 }
34 public override fun drawSurface(
35 canvas: Canvas,
36 geometry: SurfaceGeometry,
37 visual: SurfaceVisual,
38 layer: SurfaceLayer
39 ) {
40 draw(canvas) { local ->
41 if (layer == SurfaceLayer.All && geometry.opacity < 1.0)
42 local.saveLayer(geometry.opacity)
43 val style = geometry.style
44 if (layer != SurfaceLayer.Foreground)
45 background
46 .paint(
47 local,
48 geometry.outline.bounds,
49 Style(
50 background = style.background,
51 corners = Corners.Physical(geometry.outline.radii),
52 boxShadows = style.boxShadows
53 )
54 ) { _ -> }
55 if (layer != SurfaceLayer.Background) {
56 foreground
57 .paint(
58 local,
59 geometry.outline.bounds,
60 Style(
61 border = style.border,
62 corners = Corners.Physical(geometry.outline.radii)
63 )
64 ) { _ -> }
65 drawFocus(local, geometry.ring, focusPainter)
66 }
67 if (layer == SurfaceLayer.All && geometry.opacity < 1.0) local.restore()
68 }
69 }
70 public override fun drawCheckbox(
71 canvas: Canvas,
72 geometry: CheckboxGeometry,
73 visual: CheckboxVisual
74 ) {
75 draw(canvas) { local ->
76 if (geometry.opacity < 1.0) local.saveLayer(geometry.opacity)
77 drawMarkerLayer(local, geometry.stateLayer)
78 marker
79 .paint(local, geometry.outline.bounds, geometry.style) { content ->
80 if (geometry.selection > 0.0) {
81 val color = geometry
82 .markColor
83 .withAlpha(geometry.markColor.alpha * geometry.selection)
84 when (visual.mark) {
85 case Mixed -> drawRound(content, geometry.mixed, color)
86 else -> drawStroke(content, geometry.tick, color)
87 }
88 }
89 }
90 drawFocus(local, geometry.ring, focusPainter)
91 if (geometry.opacity < 1.0) local.restore()
92 }
93 }
94 public override fun drawRadio(canvas: Canvas, geometry: RadioGeometry, visual: RadioVisual) {
95 draw(canvas) { local ->
96 if (geometry.opacity < 1.0) local.saveLayer(geometry.opacity)
97 drawMarkerLayer(local, geometry.stateLayer)
98 marker
99 .paint(local, geometry.outer.bounds, geometry.style) { content ->
100 if (geometry.dot.radius > 0.0)
101 content
102 .drawCircle(
103 geometry.dot.center,
104 geometry.dot.radius,
105 Paint(
106 geometry
107 .dotColor
108 .withAlpha(
109 geometry.dotColor.alpha * geometry.selection
110 )
111 )
112 )
113 }
114 drawFocus(local, geometry.ring, focusPainter)
115 if (geometry.opacity < 1.0) local.restore()
116 }
117 }
118 public override fun drawScrollbar(
119 canvas: Canvas,
120 geometry: ScrollbarGeometry,
121 visual: ScrollbarVisual
122 ) {
123 draw(canvas) { local ->
124 val opacity = themeUnit(visual.opacity) * if (visual.enabled) 1.0 else disabledOpacity
125 if (opacity < 1.0) local.saveLayer(opacity)
126 if (visual.showTrack)
127 drawRound(
128 local,
129 geometry.track,
130 scrollbar.track.resolve(scrollbarVisual(visual, ScrollbarRegion.Track))
131 )
132 drawRound(
133 local,
134 geometry.thumb,
135 scrollbar.thumb.resolve(scrollbarVisual(visual, ScrollbarRegion.Thumb))
136 )
137 drawButton(
138 local,
139 geometry.decrement,
140 scrollbarVisual(visual, ScrollbarRegion.Decrement)
141 )
142 drawButton(
143 local,
144 geometry.increment,
145 scrollbarVisual(visual, ScrollbarRegion.Increment)
146 )
147 if (opacity < 1.0) local.restore()
148 }
149 }
150 private fun drawButton(
151 canvas: Canvas,
152 geometry: ScrollbarButtonGeometry?,
153 visual: InteractionVisual
154 ) {
155 when (geometry) {
156 case Some(val value) -> {
157 drawRound(canvas, value.outline, scrollbar.button.resolve(visual))
158 drawStroke(canvas, value.arrow, scrollbar.arrow.resolve(visual))
159 }
161 case None -> {}
162 }
163 }
164 public override fun close() {
165 checkThread()
166 if (painting) throw IllegalStateException(Some("theme painter has an active draw"))
167 if (closed) return
168 closed = true
169 var failure = cleanupStyle(None) { focusPainter.close() }
170 failure = cleanupStyle(failure) { marker.close() }
171 failure = cleanupStyle(failure) { foreground.close() }
172 failure = cleanupStyle(failure) { background.close() }
173 throwStyle(failure)
174 }
177private fun scrollbarVisual(visual: ScrollbarVisual, part: ScrollbarRegion): InteractionVisual =
178 InteractionVisual(
179 enabled = visual.enabled,
180 hovered = visual.hoveredPart == Some(part),
181 pressed = visual.pressedPart == Some(part)
182 )
184private fun drawRound(canvas: Canvas, outline: RoundedOutline, color: Color) {
185 if (outline.bounds.width > 0.0 && outline.bounds.height > 0.0)
186 canvas.drawRoundRect(outline.bounds, outline.radii, Paint(color))
189private fun drawMarkerLayer(canvas: Canvas, layer: MarkerStateLayerGeometry?) {
190 val geometry = layer ?: return
191 canvas.drawCircle(geometry.circle.center, geometry.circle.radius, Paint(geometry.color))
194private fun drawStroke(canvas: Canvas, stroke: StrokeSegment, color: Color) {
195 if (stroke.width <= 0.0) return
196 val paint = Paint(color, PaintingStyle.Stroke, stroke.width, StrokeCap.Round, StrokeJoin.Round)
197 canvas.drawLine(stroke.start, stroke.bend, paint)
198 canvas.drawLine(stroke.bend, stroke.end, paint)
201private fun drawFocus(canvas: Canvas, focus: OutlineRing?, painter: StylePainter) {
202 when (focus) {
203 case Some(val value) ->
204 painter
205 .paint(
206 canvas,
207 value.outline.bounds,
208 Style(
209 border = Some(Border.all(value.border.width, value.border.color)),
210 corners = Corners.Physical(value.outline.radii)
211 )
212 ) { _ -> }
213 case None -> {}
214 }