Scoop API

scoop:scoop.ui:0.1.0

src/text/selection_ink.scoop

Selected production source · src/text/selection_ink.scoop

1package scoop.ui.text
3import scoop.graphics.*
4import scoop.graphics.text.*
6internal struct TextSelectionInk(val range: TextRange, val color: Color)
8/** Selection affects cached glyph paint only when it overrides the original foreground. */
9internal class TextInkVersion {
10 private var initialized = false
11 private var source = 0L
12 private var selection: TextSelectionInk? = None
13 private var revision = 0L
15 fun update(generation: Long, ink: TextSelectionInk?): Long {
16 if (!initialized || source != generation || selection != ink) {
17 initialized = true
18 source = generation
19 selection = ink
20 revision += 1L
21 }
22 return revision
23 }
26internal fun drawSelectionText(
27 canvas: Canvas,
28 bounds: Rect,
29 boxes: Array<TextBox>,
30 ink: TextSelectionInk?,
31 draw: (Canvas) -> Unit
32) {
33 val selected =
34 when (ink) {
35 case Some(val value) -> value
36 case None -> {
37 draw(canvas)
38 return
39 }
40 }
41 canvas.saveLayer(bounds = Some(bounds))
42 try {
43 draw(canvas)
44 selectionMask(canvas, bounds, boxes, true)
45 } finally {
46 canvas.restore()
47 }
48 // A zero-radius alpha tint preserves the already shaped glyph coverage.
49 ImageFilter.dropShadowOnly(Point(0.0, 0.0), 0.0, 0.0, selected.color).use {
50 filter ->
51 canvas.saveLayer(filter = Some(filter), bounds = Some(bounds))
52 try {
53 draw(canvas)
54 selectionMask(canvas, bounds, boxes, false)
55 } finally {
56 canvas.restore()
57 }
58 }
61private fun selectionMask(canvas: Canvas, bounds: Rect, boxes: Array<TextBox>, inverse: Boolean) {
62 canvas.saveLayer(blendMode = BlendMode.DstIn, bounds = Some(bounds))
63 try {
64 if (inverse) {
65 canvas.clear(Color.White)
66 for (box in boxes) {
67 canvas.save()
68 try {
69 canvas.clipRect(box.rect)
70 canvas.clear(Color.Transparent)
71 } finally {
72 canvas.restore()
73 }
74 }
75 } else {
76 for (box in boxes)
77 canvas.drawRect(box.rect, Paint(Color.White))
78 }
79 } finally {
80 canvas.restore()
81 }