scoop:scoop.ui:0.1.0
src/text/editor/policy.scoop
Selected production source · src/text/editor/policy.scoop
1
package scoop.ui.text3
import scoop.graphics.text.*4
import scoop.ui.*5
import scoop.window.*7
public enum TextEditSource {8
NativeInput, Paste, EditCommand, Programmatic, Accessibility, DragDrop9
}11
public struct TextEditReplacement(val range: Utf8Range, val text: String)13
public struct TextEditSnapshot(14
val text: String,15
val anchor: TextPosition,16
val cursor: TextPosition,17
val markedRange: Utf8Range? = None18
)20
/** UTF-8 ranges refer to after.text, including changes made by earlier policy stages. */21
public 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>26
)28
public enum TextEditDecision {29
Accept, Reject, Transform(val replacements: Array<TextEditReplacement>)30
}32
public 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
}44
}46
public class TextEditPolicyBuilder internal constructor(initial: TextEditPolicy) {47
public var singleLine: Boolean = initial.singleLine48
public var maxLength: Long? = initial.maxLength49
private var pasteTransform: ((TextEditCandidate) -> TextEditDecision)? = initial.paste50
private var editTransform: ((TextEditCandidate) -> TextEditDecision)? = initial.transform51
private var insertedTextPredicate: ((String) -> Boolean)? = initial.acceptsInsertedText52
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
insertedTextPredicate73
)74
}75
}77
public fun textEditPolicy(configure: TextEditPolicyBuilder.() -> Unit = {}): TextEditPolicy {78
return TextEditPolicy(false, None, None, None, None).copy(configure)79
}81
internal fun editorSnapshot(value: EditorValue): TextEditSnapshot =82
TextEditSnapshot(83
value.input.surroundingText,84
value.endpoints.anchor,85
value.endpoints.cursor,86
value.input.markedRange87
)