scoop:scoop.ui.component:0.1.0
src/otp/model.scoop
Selected production source · src/otp/model.scoop
1
package scoop.ui.component3
import scoop.graphics.text.*4
import scoop.time.Duration5
import scoop.ui.*6
import scoop.ui.style.*7
import scoop.ui.text.*8
import scoop.window.TextInputPurpose10
public struct InputOtpSlot(val index: Long, val invalid: Boolean = false)12
public class InputOtpGroup public constructor(public val slots: Array<InputOtpSlot>) {13
init {14
requireComponent(slots.size > 0L, "OTP groups must contain a slot")15
}16
}18
public enum InputOtpPart {19
Group(val group: InputOtpGroup), Separator(val content: View)20
}22
public enum InputOtpPattern {23
Digits, Alphanumeric, Custom(val policy: TextEditPolicy)24
}26
public 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 = false54
) {55
init {56
requireComponent(length > 0L, "OTP length must be positive")57
if (parts.size > 0L) {58
var next = 0L59
var afterGroup = false60
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 += 1L69
}70
afterGroup = true71
}72
case Separator(_) -> {73
requireComponent(afterGroup, "OTP separators must be between groups")74
afterGroup = false75
}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
else89
parts90
}92
public class InputOtp public constructor(93
private val props: Props<InputOtpProps>,94
scope: ComponentScope95
): 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.value102
val snapshot = input.state.inputState103
val interaction = visual.value104
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.text120
),121
interactionChanged,122
input.fonts ?: scope.defaultFonts123
),124
{ current, mount -> OtpInk(current, scope = mount) },125
children.toArray()126
)127
}128
}130
internal 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: FontCollection137
)139
public fun inputOtp(140
state: TextEditorState,141
length: Long,142
configure: InputOtpBuilder.() -> Unit = {}143
): View = makeInputOtp(state, length, configure)