Scoop API

scoop:scoop.ui.component:0.1.0

src/otp/layout.scoop

Selected production source · src/otp/layout.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.ui.*
5import scoop.ui.style.*
6import scoop.ui.text.*
8internal class OtpSlotDrawing(val geometry: SurfaceGeometry, val visual: SurfaceVisual)
10internal class OtpDrawing(
11 val slots: Array<OtpSlotDrawing>,
12 val active: Long,
13 val origin: Point,
14 val editor: TextEditMeasurement
17/** Converts the public groups into one text geometry; separators remain ordinary children. */
18internal fun measureOtp(
19 ink: OtpInk,
20 context: MeasureContext,
21 constraints: Constraints
22): LayoutResult<OtpDrawing> {
23 val value = ink.props.value
24 val input = value.input
25 val theme = value.theme
26 val density = theme.tokens.typography.body.fontSize / 14.0
27 val cellSize = input.style.cellSize ?: Size(32.0 * density, 32.0 * density)
28 val gap = input.style.gap
29 val gaps = MutableArray<Double>(input.length - 1L) { _ -> 0.0 }
30 val positions = ArrayList<GroupPosition>()
31 val invalid = ArrayList<Boolean>()
32 val separators = ArrayList<Size>()
33 var height = cellSize.height
34 for (index in 0L ..< context.childCount) {
35 val child = context.measureChild(index, Constraints()).size
36 separators.add(child)
37 height = lowerBound(height, child.height)
38 }
39 var child = 0L
40 var next = 0L
41 var pendingGap = 0.0
42 for (part in input.groups()) {
43 when (part) {
44 case Group(val group) -> {
45 if (next > 0L)
46 gaps[next - 1L] = pendingGap + gap
47 for (index in 0L ..< group.slots.size) {
48 positions.add(
49 GroupPosition(
50 FlexAxis.Horizontal,
51 index == 0L,
52 index == group.slots.size - 1L
53 )
54 )
55 invalid.add(input.invalid || group.slots[index].invalid)
56 next += 1L
57 }
58 pendingGap = 0.0
59 }
60 case Separator(_) -> {
61 pendingGap = separators[child].width + gap
62 child += 1L
63 }
64 }
65 }
66 val plan =
67 TextEditCells(
68 input.length,
69 cellSize,
70 Array<Double>(gaps.size) { index -> gaps[index] },
71 context.layoutDirection
72 )
73 val baseVisual = SurfaceVisual(SurfaceKind.Plain)
74 val baseOptions = otpOptions(theme, input.style, baseVisual.interaction)
75 val margin = theme.surfaceMetrics(baseVisual, baseOptions, context.layoutDirection).focusOutset
76 var naturalWidth = cellSize.width * input.length.toDouble()
77 for (distance in plan.gaps)
78 naturalWidth += distance
79 val size = constraints.constrain(Size(naturalWidth + 2.0 * margin, height + 2.0 * margin))
80 val contentSize =
81 Size(
82 lowerBound(size.width - 2.0 * margin, 0.0),
83 lowerBound(size.height - 2.0 * margin, 0.0)
84 )
85 val viewport = Size(contentSize.width, upperBound(contentSize.height, cellSize.height))
86 val origin =
87 Point(
88 upperBound(margin, size.width / 2.0),
89 upperBound(margin, size.height / 2.0) + (contentSize.height - viewport.height) / 2.0
90 )
91 val text = ink.host.measure(Constraints.tight(viewport), Some(plan))
92 try {
93 val layout = text.layout
94 val rects = layout.cellRects
95 val active = layout.cellIndex(input.state.cursor) ?: 0L
96 val slots = ArrayList<OtpSlotDrawing>()
97 for (index in 0L ..< rects.size) {
98 val position =
99 if (index < input.length - 1L)
100 positions[index]
101 else
102 GroupPosition(
103 FlexAxis.Horizontal,
104 index < input.length && positions[index].first,
105 index == rects.size - 1L
106 )
107 val visual =
108 SurfaceVisual(
109 SurfaceKind.Plain,
110 InteractionVisual(
111 focusVisible =
112 input.enabled && value.interaction.focusVisible && active == index,
113 invalid = input.invalid || (index < invalid.size && invalid[index])
114 )
115 )
116 val options = otpOptions(theme, input.style, visual.interaction)
117 val metrics =
118 groupSurfaceMetrics(
119 theme.surfaceMetrics(visual, options, context.layoutDirection),
120 Some(position),
121 None,
122 context.layoutDirection
123 )
124 val original =
125 theme.surfaceGeometry(rects[index], visual, options, context.layoutDirection)
126 slots.add(
127 OtpSlotDrawing(
128 groupSurfaceGeometry(
129 original,
130 metrics,
131 Some(position),
132 context.layoutDirection
133 ),
134 visual
135 )
136 )
137 }
138 child = 0L
139 next = 0L
140 for (part in input.groups()) {
141 when (part) {
142 case Group(val group) -> next += group.slots.size
143 case Separator(_) -> {
144 val previous = rects[next - 1L]
145 val extent = separators[child]
146 val x =
147 if (context.layoutDirection == LayoutDirection.Ltr)
148 previous.x + previous.width + gap
149 else
150 previous.x - gap - extent.width
151 context.placeChild(child, Point(x, (cellSize.height - extent.height) / 2.0))
152 child += 1L
153 }
154 }
155 }
156 val drawing = OtpDrawing(slots.toArray(), active, origin, text)
157 return LayoutResult(offsetMetrics(text.metrics, size, origin.y), drawing, { text.close() })
158 } catch (error: Throwable) {
159 try {
160 text.close()
161 } catch (cleanup: Throwable) {
162 throw ResourceUseException(error, cleanup)
163 }
164 throw error
165 }