Scoop API

scoop:scoop.ui.component:0.1.0

src/selectable_text.scoop

Selected production source · src/selectable_text.scoop

1package scoop.ui.component
3import scoop.graphics.Rect
4import scoop.graphics.text.*
5import scoop.ui.*
6import scoop.ui.text.*
7import scoop.window.accessibility.SemanticRole
9public class SelectableStyledTextProps public constructor(
10 public val content: StyledText,
11 public val fonts: FontCollection? = None,
12 public val settings: ParagraphStyle? = None,
13 public val inlines: Array<InlineView> = [],
14 public val options: SelectableTextOptions = SelectableTextOptions(),
15 public val focus: FocusOptions = FocusOptions(),
16 public val actions: Array<TextRangeAction> = [],
17 public val decorations: Array<TextRangeDecoration> = [],
18 public val onSelectionChanged: (TextPosition, TextPosition) -> Unit = { _, _ -> Unit },
19 public val semantics: ComponentSemantics = ComponentSemantics()
22/** Styled selection delegates layout, input and lifetime to the public text host. */
23public class SelectableStyledText public constructor(
24 private val props: Props<SelectableStyledTextProps>,
25 scope: ComponentScope
26): Component(scope) {
27 public override fun render(): View {
28 val input = props.value
29 return element(
30 input,
31 { current, mount -> SelectableStyledTextInk(current, scope = mount) },
32 inlineChildren(input.content, input.inlines)
33 )
34 }
37public fun selectableStyledText(
38 content: StyledText,
39 configure: SelectableStyledTextBuilder.() -> Unit = {}
40): View = makeSelectableStyledText(content, configure)
42internal fun makeSelectableStyledText(
43 content: StyledText,
44 configure: SelectableStyledTextBuilder.() -> Unit = {}
45): View {
46 val builder = SelectableStyledTextBuilder()
47 builder.configure()
48 return component(
49 builder.snapshot(content),
50 { props, mount -> SelectableStyledText(props, scope = mount) },
51 builder.key
52 )
55private class SelectableStyledTextInk(
56 private val props: Props<SelectableStyledTextProps>,
57 scope: ComponentScope
58): Element<ParagraphMeasurement>(scope) {
59 private fun fonts(): FontCollection = props.value.fonts ?: scope.defaultFonts
60 private val host =
61 scope.resource {
62 SelectableTextHost(scope, fonts(), props.value.content, props.value.options)
63 }
64 private val semantics = createComponentSemantics(scope, SemanticRole.Text)
65 private val selectionChanged: (TextPosition, TextPosition) -> Unit =
66 {
67 anchor, cursor ->
68 updateSemantics()
69 props.value.onSelectionChanged(anchor, cursor)
70 }
71 init {
72 scope.routeSemanticsChildren(semantics)
73 }
74 private fun updateSemantics() {
75 val input = props.value
76 semantics?.bindReference(input.semantics.reference)
77 semantics?.updateSelectableText(
78 host,
79 input.semantics.properties(),
80 input.semantics.relations
81 )
82 }
83 public override fun update(): UiInvalidation {
84 val input = props.value
85 host.update(
86 fonts(),
87 input.content,
88 input.options,
89 input.focus,
90 input.actions,
91 input.decorations,
92 selectionChanged
93 )
94 updateSemantics()
95 return UiInvalidation.Layout
96 }
97 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
98 ParagraphMeasurement
99 > {
100 val input = props.value
101 val measured = host.measure(context, constraints, input.settings, input.inlines)
102 return LayoutResult(measured.metrics, measured, { measured.close() })
103 }
104 public override fun scene(context: SceneContext, measured: ParagraphMeasurement) {
105 when (semantics) {
106 case Some(val handle) -> context.semantics(
107 handle,
108 Rect(0.0, 0.0, context.size.width, context.size.height)
109 )
110 case None -> {}
111 }
112 host.scene(context, measured)
113 }