Scoop API

scoop:scoop.ui.component:0.1.0

src/otp/model.scoop

Selected production source · src/otp/model.scoop

1package scoop.ui.component
3import scoop.graphics.text.*
4import scoop.time.Duration
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.ui.text.*
8import scoop.window.TextInputPurpose
10public struct InputOtpSlot(val index: Long, val invalid: Boolean = false)
12public class InputOtpGroup public constructor(public val slots: Array<InputOtpSlot>) {
13 init {
14 requireComponent(slots.size > 0L, "OTP groups must contain a slot")
15 }
18public enum InputOtpPart {
19 Group(val group: InputOtpGroup), Separator(val content: View)
22public enum InputOtpPattern {
23 Digits, Alphanumeric, Custom(val policy: TextEditPolicy)
26public class InputOtpProps public constructor(
27 public val state: TextEditorState,
28 public val length: Long,
29 public val parts: Array<InputOtpPart> = [],
30 public val pattern: InputOtpPattern = InputOtpPattern.Digits,
31 public val enabled: Boolean = true,
32 public val readOnly: Boolean = false,
33 public val invalid: Boolean = false,
34 public val fonts: FontCollection? = None,
35 public val theme: ThemeSource? = None,
36 public val labelTarget: FieldTarget? = None,
37 public val focus: FocusOptions = FocusOptions(),
38 public val style: InputOtpStyle = inputOtpStyle(),
39 public val caretBlink: Duration? = None,
40 public val onEdited: (TextEditorState) -> Unit = { _ -> Unit },
41 public val onComplete: (TextEditorState) -> Unit = { _ -> Unit },
42 public val contextMenu: TextContextMenuOptions = TextContextMenuOptions(),
43 public val protectedText: Boolean = false,
44 public val acceptDrops: Boolean = true,
45 public val drag: TextDragOptions = TextDragOptions(),
46 public val touch: TextTouchOptions = TextTouchOptions(),
47 public val touchScroll: TouchScrollOptions = TouchScrollOptions(),
48 public val scrollPhysics: ScrollPhysicsOptions = ScrollPhysicsOptions(),
49 public val semantics: ComponentSemantics = ComponentSemantics(),
50 public val inputPurpose: TextInputPurpose? = None,
51 public val softwareKeyboard: Boolean = true,
52 public val primarySelection: TextPrimarySelectionOptions = TextPrimarySelectionOptions(),
53 public val automaticTextReplacement: Boolean = false
54) {
55 init {
56 requireComponent(length > 0L, "OTP length must be positive")
57 if (parts.size > 0L) {
58 var next = 0L
59 var afterGroup = false
60 for (part in parts) {
61 when (part) {
62 case Group(val group) -> {
63 for (slot in group.slots) {
64 requireComponent(
65 slot.index == next,
66 "OTP slots must follow logical order"
67 )
68 next += 1L
69 }
70 afterGroup = true
71 }
72 case Separator(_) -> {
73 requireComponent(afterGroup, "OTP separators must be between groups")
74 afterGroup = false
75 }
76 }
77 }
78 requireComponent(afterGroup && next == length, "OTP groups must cover every slot")
79 }
80 }
81 internal fun groups(): Array<InputOtpPart> =
82 if (parts.size == 0L)
83 [
84 InputOtpPart.Group(
85 InputOtpGroup(Array<InputOtpSlot>(length) { index -> InputOtpSlot(index) })
86 )
87 ]
88 else
89 parts
92public class InputOtp public constructor(
93 private val props: Props<InputOtpProps>,
94 scope: ComponentScope
95): Component(scope) {
96 private val themes = ThemeAccess(scope)
97 private val body = ActionTextStyle()
98 private val visual = state(TextEditInteraction())
99 private val interactionChanged: (TextEditInteraction) -> Unit = { next -> visual.value = next }
100 public override fun render(): View {
101 val input = props.value
102 val snapshot = input.state.inputState
103 val interaction = visual.value
104 val theme = themes.read(input.theme)
105 val children = ArrayList<View>()
106 for (part in input.parts)
107 when (part) {
108 case Separator(val content) -> children.add(content)
109 case Group(_) -> {}
110 }
111 return element(
112 OtpInkProps(
113 input,
114 theme,
115 interaction,
116 body.resolve(
117 theme.tokens.typography.body,
118 theme.tokens.palette.background.foreground,
119 input.style.text
120 ),
121 interactionChanged,
122 input.fonts ?: scope.defaultFonts
123 ),
124 { current, mount -> OtpInk(current, scope = mount) },
125 children.toArray()
126 )
127 }
130internal class OtpInkProps(
131 val input: InputOtpProps,
132 val theme: Theme,
133 val interaction: TextEditInteraction,
134 val text: TextStyle,
135 val onInteractionChanged: (TextEditInteraction) -> Unit,
136 val fonts: FontCollection
139public fun inputOtp(
140 state: TextEditorState,
141 length: Long,
142 configure: InputOtpBuilder.() -> Unit = {}
143): View = makeInputOtp(state, length, configure)