Scoop API

scoop:scoop.ui:0.1.0

src/text/editor/policy.scoop

Selected production source · src/text/editor/policy.scoop

1package scoop.ui.text
3import scoop.graphics.text.*
4import scoop.ui.*
5import scoop.window.*
7public enum TextEditSource {
8 NativeInput, Paste, EditCommand, Programmatic, Accessibility, DragDrop
9}
11public struct TextEditReplacement(val range: Utf8Range, val text: String)
13public struct TextEditSnapshot(
14 val text: String,
15 val anchor: TextPosition,
16 val cursor: TextPosition,
17 val markedRange: Utf8Range? = None
20/** UTF-8 ranges refer to after.text, including changes made by earlier policy stages. */
21public class TextEditCandidate internal constructor(
22 public val before: TextEditSnapshot,
23 public val after: TextEditSnapshot,
24 public val source: TextEditSource,
25 public val insertedRanges: Array<Utf8Range>
28public enum TextEditDecision {
29 Accept, Reject, Transform(val replacements: Array<TextEditReplacement>)
32public class TextEditPolicy internal constructor(
33 public val singleLine: Boolean,
34 public val maxLength: Long?,
35 internal val paste: ((TextEditCandidate) -> TextEditDecision)?,
36 internal val transform: ((TextEditCandidate) -> TextEditDecision)?,
37 internal val acceptsInsertedText: ((String) -> Boolean)?
38) {
39 public fun copy(configure: TextEditPolicyBuilder.() -> Unit = {}): TextEditPolicy {
40 val builder = TextEditPolicyBuilder(this)
41 builder.configure()
42 return builder.snapshot()
43 }
46public class TextEditPolicyBuilder internal constructor(initial: TextEditPolicy) {
47 public var singleLine: Boolean = initial.singleLine
48 public var maxLength: Long? = initial.maxLength
49 private var pasteTransform: ((TextEditCandidate) -> TextEditDecision)? = initial.paste
50 private var editTransform: ((TextEditCandidate) -> TextEditDecision)? = initial.transform
51 private var insertedTextPredicate: ((String) -> Boolean)? = initial.acceptsInsertedText
52 public fun paste(value: (TextEditCandidate) -> TextEditDecision) {
53 pasteTransform = Some(value)
54 }
55 public fun transform(value: (TextEditCandidate) -> TextEditDecision) {
56 editTransform = Some(value)
57 }
58 /** Checks each contiguous inserted fragment after transforms and before length limiting. */
59 public fun acceptsInsertedText(value: (String) -> Boolean) {
60 insertedTextPredicate = Some(value)
61 }
62 internal fun snapshot(): TextEditPolicy {
63 when (maxLength) {
64 case Some(val value) -> requireUi(value >= 0L, "negative text length limit")
65 case None -> {}
66 }
67 return TextEditPolicy(
68 singleLine,
69 maxLength,
70 pasteTransform,
71 editTransform,
72 insertedTextPredicate
73 )
74 }
77public fun textEditPolicy(configure: TextEditPolicyBuilder.() -> Unit = {}): TextEditPolicy {
78 return TextEditPolicy(false, None, None, None, None).copy(configure)
81internal fun editorSnapshot(value: EditorValue): TextEditSnapshot =
82 TextEditSnapshot(
83 value.input.surroundingText,
84 value.endpoints.anchor,
85 value.endpoints.cursor,
86 value.input.markedRange
87 )