Scoop API

scoop:scoop.ui.component:0.1.0

src/otp/builder.scoop

Selected production source · src/otp/builder.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.time.Duration
6import scoop.ui.*
7import scoop.ui.style.*
8import scoop.ui.text.*
9import scoop.window.TextInputPurpose
11public 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())
19public class InputOtpBuilder internal constructor() {
20 public var pattern: InputOtpPattern = InputOtpPattern.Digits
21 public var enabled: Boolean = true
22 public var readOnly: Boolean = false
23 public var protectedText: Boolean = false
24 public var inputPurpose: TextInputPurpose? = None
25 public var softwareKeyboard: Boolean = true
26 public var primarySelection: TextPrimarySelectionOptions = TextPrimarySelectionOptions()
27 public var automaticTextReplacement: Boolean = false
28 public var acceptDrops: Boolean = true
29 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 = false
34 public var fonts: FontCollection? = None
35 public var theme: ThemeSource? = None
36 public var labelTarget: FieldTarget? = None
37 public var focus: FocusOptions = FocusOptions()
38 public var style: InputOtpStyle = inputOtpStyle()
39 public var caretBlink: Duration? = None
40 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.Position
45 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 automaticTextReplacement
100 )
103internal fun makeInputOtp(
104 state: TextEditorState,
105 length: Long,
106 configure: InputOtpBuilder.() -> Unit
107): 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.key
114 )
117private class OtpSeparator(
118 private val source: Props<ThemeSource?>,
119 scope: ComponentScope
120): 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.0
131 this.color = Some(theme.tokens.palette.background.foreground)
132 }
133 ) {
134 this.width = Some(16.0);
135 this.height = Some(16.0)
136 }
137 }