scoop:scoop.ui.style:0.1.0
src/painter.scoop
Selected production source · src/painter.scoop
1
package scoop.ui.style3
import scoop.graphics.*4
import scoop.platform.concurrent.currentThreadId5
import scoop.ui.SceneContext6
import scoop.ui.LayoutDirection8
private struct DecorationDrawing(9
val paths: DecorationPaths?,10
val edges: BorderEdges,11
val shadows: Array<ImageFilter>,12
val filter: ImageFilter?13
)15
/** Owns just the current filters and outlines; register once in the using element's scope. */16
public class StylePainter public constructor() : Closeable {17
private val owner = currentThreadId()18
private var closed = false19
private var painting = false20
private val contentFilters = ContentFilterCache()21
private val boxFilters = BoxFilterCache()22
private val geometry = DecorationGeometry()23
public val isClosed: Boolean get() = closed25
private fun checkOpen() {26
checkThread()27
if (closed) throw IllegalStateException(Some("style painter is closed"))28
if (painting) throw IllegalStateException(Some("style painter has an active draw"))29
}30
private fun checkThread() {31
if (owner != currentThreadId())32
throw IllegalStateException(Some("style painter requires its owner thread"))33
}35
public fun scene(context: SceneContext, style: Style, content: (SceneContext) -> Unit) {36
scene(context, style, None, content)37
}38
/** Draws the borrowed backdrop between the outer shadows and foreground fill. */39
public fun scene(40
context: SceneContext,41
style: Style,42
backdrop: ImageFilter?,43
content: (SceneContext) -> Unit44
) {45
val size = context.size46
scene(context, Rect(0.0, 0.0, size.width, size.height), style, backdrop, content)47
}48
public fun scene(49
context: SceneContext,50
bounds: Rect,51
style: Style,52
content: (SceneContext) -> Unit53
) {54
scene(context, bounds, style, None, content)55
}56
public fun scene(57
context: SceneContext,58
bounds: Rect,59
style: Style,60
backdrop: ImageFilter?,61
content: (SceneContext) -> Unit62
) {63
checkOpen()64
painting = true65
try {66
val drawing = prepare(bounds, style, context.layoutDirection, backdrop)67
context.draw { paint -> background(paint.canvas, bounds, style, drawing, backdrop) }68
context69
.withState { local ->70
if (style.clipContent) local.clipPath(drawing.paths!!.outline)71
when (drawing.filter) {72
case Some(val value) -> local.saveLayer(filter = Some(value))73
case None -> {}74
}75
local.withState(content)76
when (drawing.filter) {77
case Some(_) -> local.restore()78
case None -> {}79
}80
}81
context.draw { paint -> border(paint.canvas, bounds, drawing) }82
} finally {83
painting = false84
}85
}86
public fun paint(87
canvas: Canvas,88
bounds: Rect,89
style: Style,90
direction: LayoutDirection = LayoutDirection.Ltr,91
content: (Canvas) -> Unit92
) {93
paint(canvas, bounds, style, None, direction, content)94
}95
public fun paint(96
canvas: Canvas,97
bounds: Rect,98
style: Style,99
backdrop: ImageFilter?,100
direction: LayoutDirection = LayoutDirection.Ltr,101
content: (Canvas) -> Unit102
) {103
checkOpen()104
painting = true105
try {106
val drawing = prepare(bounds, style, direction, backdrop)107
canvas108
.withState { local ->109
background(local, bounds, style, drawing, backdrop)110
local.save()111
if (style.clipContent) local.clipPath(drawing.paths!!.outline)112
when (drawing.filter) {113
case Some(val value) -> local.saveLayer(filter = Some(value))114
case None -> {}115
}116
local.withState(content)117
when (drawing.filter) {118
case Some(_) -> local.restore()119
case None -> {}120
}121
local.restore()122
border(local, bounds, drawing)123
}124
} finally {125
painting = false126
}127
}129
private fun prepare(130
bounds: Rect,131
style: Style,132
direction: LayoutDirection,133
backdrop: ImageFilter?134
): DecorationDrawing {135
when (style.background) {136
case Some(val value) ->137
requireStyle(138
value.style == PaintingStyle.Fill,139
"style background must be a fill"140
)141
case None -> {}142
}143
return DecorationDrawing(144
geometry145
.update(146
bounds,147
style,148
direction,149
when (backdrop) {150
case Some(_) -> true;151
case None -> false152
}153
),154
physicalEdges(style, direction),155
boxFilters.update(style.boxShadows),156
contentFilters.update(style.contentEffects)157
)158
}159
private fun background(160
canvas: Canvas,161
bounds: Rect,162
style: Style,163
drawing: DecorationDrawing,164
backdrop: ImageFilter?165
) {166
if (bounds.width == 0.0 || bounds.height == 0.0) return167
for (shadow in drawing.shadows)168
canvas169
.withState { mask ->170
mask.saveLayer(filter = Some(shadow), bounds = Some(bounds))171
mask.drawPath(drawing.paths!!.outline, Paint(Color.White))172
mask.restore()173
}174
when (backdrop) {175
case Some(val filter) -> canvas.drawBackdrop(drawing.paths!!.outline, filter)176
case None -> {}177
}178
when (style.background) {179
case Some(val paint) -> canvas.drawPath(drawing.paths!!.outline, paint)180
case None -> {}181
}182
}183
private fun border(canvas: Canvas, bounds: Rect, drawing: DecorationDrawing) {184
val paths = drawing.paths ?: return185
val ring = paths.border ?: return186
when (drawing.edges.uniformColor()) {187
case Some(val color) -> canvas.drawPath(ring, Paint(color))188
case None ->189
canvas190
.withState { local ->191
local.clipRect(bounds)192
local.saveLayer(bounds = Some(bounds))193
val colors = drawing.edges.values()194
for (index in 0L..<paths.regions.size) {195
if (colors[index].width == 0.0) continue196
// Sum disjoint antialias coverage without darkening their shared joins.197
local.saveLayer(blendMode = BlendMode.Plus, bounds = Some(bounds))198
local.drawPath(paths.regions[index], Paint(colors[index].color))199
local.restore()200
}201
// Apply one common rounded ring to the assembled colors.202
local.saveLayer(blendMode = BlendMode.DstIn, bounds = Some(bounds))203
local.drawPath(ring, Paint(Color.White))204
local.restore()205
local.restore()206
}207
}208
}210
public override fun close() {211
checkThread()212
if (painting) throw IllegalStateException(Some("style painter has an active draw"))213
if (closed) return214
closed = true215
var failure = cleanupStyle(None) { contentFilters.close() }216
failure = cleanupStyle(failure) { boxFilters.close() }217
failure = cleanupStyle(failure) { geometry.close() }218
throwStyle(failure)219
}220
}