scoop:scoop.ui.component:0.1.0
src/otp/layout.scoop
Selected production source · src/otp/layout.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.ui.*5
import scoop.ui.style.*6
import scoop.ui.text.*8
internal class OtpSlotDrawing(val geometry: SurfaceGeometry, val visual: SurfaceVisual)10
internal class OtpDrawing(11
val slots: Array<OtpSlotDrawing>,12
val active: Long,13
val origin: Point,14
val editor: TextEditMeasurement15
)17
/** Converts the public groups into one text geometry; separators remain ordinary children. */18
internal fun measureOtp(19
ink: OtpInk,20
context: MeasureContext,21
constraints: Constraints22
): LayoutResult<OtpDrawing> {23
val value = ink.props.value24
val input = value.input25
val theme = value.theme26
val density = theme.tokens.typography.body.fontSize / 14.027
val cellSize = input.style.cellSize ?: Size(32.0 * density, 32.0 * density)28
val gap = input.style.gap29
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.height34
for (index in 0L ..< context.childCount) {35
val child = context.measureChild(index, Constraints()).size36
separators.add(child)37
height = lowerBound(height, child.height)38
}39
var child = 0L40
var next = 0L41
var pendingGap = 0.042
for (part in input.groups()) {43
when (part) {44
case Group(val group) -> {45
if (next > 0L)46
gaps[next - 1L] = pendingGap + gap47
for (index in 0L ..< group.slots.size) {48
positions.add(49
GroupPosition(50
FlexAxis.Horizontal,51
index == 0L,52
index == group.slots.size - 1L53
)54
)55
invalid.add(input.invalid || group.slots[index].invalid)56
next += 1L57
}58
pendingGap = 0.059
}60
case Separator(_) -> {61
pendingGap = separators[child].width + gap62
child += 1L63
}64
}65
}66
val plan =67
TextEditCells(68
input.length,69
cellSize,70
Array<Double>(gaps.size) { index -> gaps[index] },71
context.layoutDirection72
)73
val baseVisual = SurfaceVisual(SurfaceKind.Plain)74
val baseOptions = otpOptions(theme, input.style, baseVisual.interaction)75
val margin = theme.surfaceMetrics(baseVisual, baseOptions, context.layoutDirection).focusOutset76
var naturalWidth = cellSize.width * input.length.toDouble()77
for (distance in plan.gaps)78
naturalWidth += distance79
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.090
)91
val text = ink.host.measure(Constraints.tight(viewport), Some(plan))92
try {93
val layout = text.layout94
val rects = layout.cellRects95
val active = layout.cellIndex(input.state.cursor) ?: 0L96
val slots = ArrayList<OtpSlotDrawing>()97
for (index in 0L ..< rects.size) {98
val position =99
if (index < input.length - 1L)100
positions[index]101
else102
GroupPosition(103
FlexAxis.Horizontal,104
index < input.length && positions[index].first,105
index == rects.size - 1L106
)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.layoutDirection123
)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.layoutDirection133
),134
visual135
)136
)137
}138
child = 0L139
next = 0L140
for (part in input.groups()) {141
when (part) {142
case Group(val group) -> next += group.slots.size143
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 + gap149
else150
previous.x - gap - extent.width151
context.placeChild(child, Point(x, (cellSize.height - extent.height) / 2.0))152
child += 1L153
}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 error165
}166
}