scoop:scoop.ui.component:0.1.0
src/otp/builder.scoop
Selected production source · src/otp/builder.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.time.Duration6
import scoop.ui.*7
import scoop.ui.style.*8
import scoop.ui.text.*9
import scoop.window.TextInputPurpose11
public class InputOtpGroupBuilder internal constructor() {12
private val slots = ArrayList<InputOtpSlot>()13
public fun slot(index: Long, invalid: Boolean = false) {14
slots.add(InputOtpSlot(index, invalid))15
}16
internal fun snapshot(): InputOtpGroup = InputOtpGroup(slots.toArray())17
}19
public class InputOtpBuilder internal constructor() {20
public var pattern: InputOtpPattern = InputOtpPattern.Digits21
public var enabled: Boolean = true22
public var readOnly: Boolean = false23
public var protectedText: Boolean = false24
public var inputPurpose: TextInputPurpose? = None25
public var softwareKeyboard: Boolean = true26
public var primarySelection: TextPrimarySelectionOptions = TextPrimarySelectionOptions()27
public var automaticTextReplacement: Boolean = false28
public var acceptDrops: Boolean = true29
public var drag: TextDragOptions = TextDragOptions()30
public var touch: TextTouchOptions = TextTouchOptions()31
public var touchScroll: TouchScrollOptions = TouchScrollOptions()32
public var scrollPhysics: ScrollPhysicsOptions = ScrollPhysicsOptions()33
public var invalid: Boolean = false34
public var fonts: FontCollection? = None35
public var theme: ThemeSource? = None36
public var labelTarget: FieldTarget? = None37
public var focus: FocusOptions = FocusOptions()38
public var style: InputOtpStyle = inputOtpStyle()39
public var caretBlink: Duration? = None40
public var contextMenu: TextContextMenuOptions = TextContextMenuOptions()41
public var onEdited: (TextEditorState) -> Unit = { _ -> Unit }42
public var onComplete: (TextEditorState) -> Unit = { _ -> Unit }43
public var semantics: ComponentSemantics = ComponentSemantics()44
public var key: ViewKey = ViewKey.Position45
private val parts = ArrayList<(ThemeSource?) -> InputOtpPart>()46
public fun group(configure: InputOtpGroupBuilder.() -> Unit) {47
val builder = InputOtpGroupBuilder()48
builder.configure()49
val group = builder.snapshot()50
parts.add { _ -> InputOtpPart.Group(group) }51
}52
public fun separator(content: View) {53
parts.add { _ -> InputOtpPart.Separator(content) }54
}55
public fun separator() {56
parts.add {57
source -> InputOtpPart.Separator(58
component(source, { current, mount -> OtpSeparator(current, scope = mount) })59
)60
}61
}62
public fun style(configure: InputOtpStyleBuilder.() -> Unit) {63
style = style.copy(configure)64
}65
public fun focus(configure: FocusOptionsBuilder.() -> Unit) {66
focus = focus.copy(configure)67
}68
public fun contextMenu(configure: TextContextMenuOptionsBuilder.() -> Unit) {69
contextMenu = contextMenu.copy(configure)70
}71
internal fun snapshot(state: TextEditorState, length: Long): InputOtpProps =72
InputOtpProps(73
state,74
length,75
Array<InputOtpPart>(parts.size) { index -> parts[index](theme) },76
pattern,77
enabled,78
readOnly,79
invalid,80
fonts,81
theme,82
labelTarget,83
focus,84
style,85
caretBlink,86
onEdited,87
onComplete,88
contextMenu,89
protectedText,90
acceptDrops,91
drag,92
touch,93
touchScroll,94
scrollPhysics,95
semantics,96
inputPurpose,97
softwareKeyboard,98
primarySelection,99
automaticTextReplacement100
)101
}103
internal fun makeInputOtp(104
state: TextEditorState,105
length: Long,106
configure: InputOtpBuilder.() -> Unit107
): View {108
val builder = InputOtpBuilder()109
builder.configure()110
return component(111
builder.snapshot(state, length),112
{ props, mount -> InputOtp(props, scope = mount) },113
builder.key114
)115
}117
private class OtpSeparator(118
private val source: Props<ThemeSource?>,119
scope: ComponentScope120
): Component(scope) {121
private val themes = ThemeAccess(scope)122
public override fun onMount() {123
themes.mount(source.value)124
}125
public override fun render(): View {126
val theme = themes.read(source.value)127
return frame(128
makeSeparator {129
this.length = Some(10.0)130
this.thickness = 2.0131
this.color = Some(theme.tokens.palette.background.foreground)132
}133
) {134
this.width = Some(16.0);135
this.height = Some(16.0)136
}137
}138
}