Scoop API

scoop:scoop.ui:0.1.0

src/text/selectable/scene.scoop

Selected production source · src/text/selectable/scene.scoop

1package scoop.ui.text
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.surface.FrameSubmission
6import scoop.ui.*
8internal class SelectableFrame(
9 private val retained: RetainedParagraphLayout,
10 val content: StyledText,
11 val ranges: Array<SelectableRange>
12): Closeable {
13 val paths = ArrayList<Path>()
14 val layout: TextLayout get() = retained.layout
15 public override fun close() {
16 for (value in paths)
17 value.close()
18 paths.clear()
19 retained.close()
20 }
23internal fun selectableScene(
24 host: SelectableTextHost,
25 context: SceneContext,
26 measurement: ParagraphMeasurement
27) {
28 val ranges =
29 Array<SelectableRange>(host.actions.size) {
30 index ->
31 val input = host.actions[index].input
32 SelectableRange(input.key, input.range)
33 }
34 val frame = SelectableFrame(measurement.retainLayout(), host.content, ranges)
35 host.staged?.close()
36 host.staged = Some(frame)
37 val bounds = Rect(0.0, 0.0, context.size.width, context.size.height)
38 val selected = host.selection.range
39 val boxes = frame.layout.selection(TextRange(selected.start, selected.end))
40 val active = host.scope.nativeWindow.isFocused && host.area.scope.activity.hasFocus
41 val color =
42 if (active)
43 host.options.selectionColor
44 else
45 host.options.inactiveSelectionColor
46 val textColor =
47 if (active)
48 host.options.selectionTextColor
49 else
50 host.options.inactiveSelectionTextColor
51 val ink =
52 when (textColor) {
53 case Some(val value) -> if (boxes.size > 0L)
54 Some(TextSelectionInk(TextRange(selected.start, selected.end), value))
55 else
56 None
57 case None -> None
58 }
59 context.withState {
60 scene ->
61 if (host.options.clip)
62 scene.clipRect(bounds)
63 scene.place(host.area, bounds)
64 selectableSemantics(host, scene, frame, bounds)
65 for (decoration in host.decorations) {
66 val regions = frame.layout.selection(decoration.range)
67 when (decoration.background) {
68 case Some(val value) -> scene.draw {
69 paint -> for (box in regions)
70 paint.canvas.drawRect(box.rect, Paint(value))
71 }
72 case None -> {}
73 }
74 }
75 scene.draw {
76 paint -> for (box in boxes)
77 paint.canvas.drawRect(box.rect, Paint(color))
78 }
79 for (action in host.actions) {
80 val regions = frame.layout.selection(action.input.range)
81 val shape =
82 path {
83 for (box in regions)
84 addRect(box.rect)
85 }
86 frame.paths.add(shape)
87 scene.place(action.area, shape)
88 when (action.semantics) {
89 case Some(val handle) -> scene.semantics(handle, shape)
90 case None -> {}
91 }
92 }
93 host.paragraph.scene(scene, measurement, ink, boxes)
94 for (decoration in host.decorations) {
95 val regions = frame.layout.selection(decoration.range)
96 when (decoration.underline) {
97 case Some(val value) -> scene.draw {
98 paint -> for (box in regions)
99 paint.canvas.drawRect(
100 Rect(
101 box.rect.x,
102 box.rect.y + box.rect.height - 1.0,
103 box.rect.width,
104 1.0
105 ),
106 Paint(value)
107 )
108 }
109 case None -> {}
110 }
111 }
112 selectableSelectionHandles(host, scene, frame, bounds)
113 }
114 context.onFrameComplete {
115 completion ->
116 when (host.staged) {
117 case Some(val value) -> if (value === frame)
118 host.staged = None
119 case None -> {}
120 }
121 if (host.closed || completion.submission == FrameSubmission.Discarded) {
122 frame.close()
123 return@onFrameComplete
124 }
125 val previous = host.accepted
126 host.accepted = Some(frame)
127 previous?.close()
128 host.refreshAdmission()
129 host.touchHandles.accepted()
130 }