scoop:scoop.ui.component:0.1.0
src/selectable_text.scoop
Selected production source · src/selectable_text.scoop
1
package scoop.ui.component3
import scoop.graphics.Rect4
import scoop.graphics.text.*5
import scoop.ui.*6
import scoop.ui.text.*7
import scoop.window.accessibility.SemanticRole9
public 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()20
)22
/** Styled selection delegates layout, input and lifetime to the public text host. */23
public class SelectableStyledText public constructor(24
private val props: Props<SelectableStyledTextProps>,25
scope: ComponentScope26
): Component(scope) {27
public override fun render(): View {28
val input = props.value29
return element(30
input,31
{ current, mount -> SelectableStyledTextInk(current, scope = mount) },32
inlineChildren(input.content, input.inlines)33
)34
}35
}37
public fun selectableStyledText(38
content: StyledText,39
configure: SelectableStyledTextBuilder.() -> Unit = {}40
): View = makeSelectableStyledText(content, configure)42
internal 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.key52
)53
}55
private class SelectableStyledTextInk(56
private val props: Props<SelectableStyledTextProps>,57
scope: ComponentScope58
): Element<ParagraphMeasurement>(scope) {59
private fun fonts(): FontCollection = props.value.fonts ?: scope.defaultFonts60
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.value76
semantics?.bindReference(input.semantics.reference)77
semantics?.updateSelectableText(78
host,79
input.semantics.properties(),80
input.semantics.relations81
)82
}83
public override fun update(): UiInvalidation {84
val input = props.value85
host.update(86
fonts(),87
input.content,88
input.options,89
input.focus,90
input.actions,91
input.decorations,92
selectionChanged93
)94
updateSemantics()95
return UiInvalidation.Layout96
}97
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<98
ParagraphMeasurement99
> {100
val input = props.value101
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
}114
}