scoop:scoop.ui.component:0.1.0
src/text_field/layout.scoop
Selected production source · src/text_field/layout.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.ui.*6
import scoop.ui.text.TextEditMeasurement7
import scoop.ui.style.*9
internal class TextFieldDrawing(10
val geometry: SurfaceGeometry,11
val viewport: Rect,12
val horizontal: Rect?,13
val vertical: Rect?,14
val editor: TextEditMeasurement,15
val outlineClip: Path? = None16
)18
/** Only the scalar font metric is cached; both temporary native objects close immediately. */19
internal class TextFieldLineHeight {20
private var fonts: FontCollection? = None21
private var revision = -1L22
private var style: TextStyle? = None23
private var height = 0.024
fun measure(fonts: FontCollection, style: TextStyle): Double {25
val sameFonts = when (this.fonts) {26
case Some(val previous) -> previous === fonts27
case None -> false28
}29
val sameStyle = when (this.style) {30
case Some(val previous) -> previous === style31
case None -> false32
}33
if (sameFonts && sameStyle && revision == fonts.revision) return height34
val next = Paragraph(fonts, "", style)35
.use { paragraph -> paragraph.layoutUnwrapped().use { layout -> layout.size.height } }36
this.fonts = Some(fonts)37
this.style = Some(style)38
revision = fonts.revision39
height = next40
return next41
}42
}44
internal fun measureTextField(45
ink: TextFieldInk,46
context: MeasureContext,47
constraints: Constraints48
): LayoutResult<TextFieldDrawing> {49
val value = ink.props.value50
val input = value.input51
val base = value.theme.surfaceMetrics(value.visual, value.surface, context.layoutDirection)52
val metrics = if (input.chrome == TextFieldChrome.Bare)53
bareTextFieldMetrics(base)54
else55
groupSurfaceMetrics(56
base,57
input.group,58
groupEndRadius(value.theme, value.surface.style),59
context.layoutDirection60
)61
val insets = metrics.contentInsets.totals()62
val naturalWidth = value.surface.suggestedSize?.width ?: 240.063
val width = constraints64
.constrain(65
Size(constraints.maxWidth ?: lowerBound(naturalWidth, metrics.minimumSize.width), 0.0)66
)67
.width68
val label = when (value.label) {69
case Some(val appearance) -> {70
val natural = context.measureChild(0L, Constraints())71
val available = lowerBound(72
width - if (appearance.position == FieldLabelPosition.Above) 0.0 else insets.width,73
0.074
)75
Some(76
if (natural.size.width > available)77
context.measureChild(0L, Constraints(maxWidth = Some(available)))78
else79
natural80
)81
}83
case None -> None84
}85
val descriptionIndex = if (label == None) 0L else 1L86
val outlined = value.theme.tokens.textField == TextFieldTreatment.Outlined87
val support = if (outlined) metrics.contentInsets else Insets()88
val description = if (input.description.byteLength > 0L)89
Some(90
context91
.measureChild(92
descriptionIndex,93
Constraints(94
maxWidth = Some(lowerBound(width - support.totals().width, 0.0))95
)96
)97
)98
else99
None100
val gap = if (description != None) value.theme.tokens.spacing.small else 0.0101
nonnegative(gap, "invalid text field description spacing")102
val after = (description?.size?.height ?: 0.0) + gap103
val before = when (value.label) {104
case Some(val appearance) ->105
if (appearance.position == FieldLabelPosition.Above)106
label!!.size.height + value.theme.tokens.spacing.small107
else108
ink.labelHeight.measure(ink.fonts, appearance.compact) / 2.0109
case None -> 0.0110
}111
val axes = if (input.options.wrap) ScrollAxes.Vertical else ScrollAxes.Both112
val visibility = if (input.options.multiline) input.scrollbars else ScrollbarVisibility.Hidden113
val behavior = ink.scrollbarFeedback.options114
val gutter = scrollbarGutter(axes, visibility, behavior.thickness, behavior.input.placement)115
val rowHeight = ink.lineHeight.measure(ink.fonts, value.text)116
val naturalHeight = lowerBound(117
rowHeight * input.rows.toDouble() + insets.height + gutter.height,118
lowerBound(metrics.minimumSize.height, metrics.suggestedSize.height)119
)120
val size = constraints.constrain(Size(width, before + naturalHeight + after))121
val fieldHeight = lowerBound(size.height - before - after, 0.0)122
val original = value123
.theme124
.surfaceGeometry(125
Rect(0.0, before, size.width, fieldHeight),126
value.visual,127
value.surface,128
context.layoutDirection129
)130
val geometry = if (input.chrome == TextFieldChrome.Bare)131
bareTextFieldGeometry(original, metrics)132
else133
groupSurfaceGeometry(original, metrics, input.group, context.layoutDirection)134
val content = geometry.content135
val outer = Size(content.width, content.height)136
val viewport = scrollbarViewport(outer, gutter, context.layoutDirection)137
if (description != None)138
context.placeChild(descriptionIndex, Point(support.left, before + fieldHeight + gap))139
val labelBounds = when (value.label) {140
case Some(val appearance) -> {141
val bounds = textFieldLabelBounds(142
appearance, label!!.size, geometry, input.options.multiline, context.layoutDirection143
)144
context.placeChild(0L, bounds.origin)145
Some(bounds)146
}148
case None -> None149
}150
val text = ink151
.host152
.measure(153
Constraints.tight(Size(viewport.width, viewport.height)),154
scrollPadding = { size, body ->155
scrollbarPadding(156
size,157
body,158
axes,159
visibility,160
behavior.thickness,161
behavior.input.placement,162
context.layoutDirection163
)164
}165
)166
val snapshot = text.scrollSnapshot167
val bars = ScrollBarsLayout(168
outer,169
snapshot,170
axes,171
visibility,172
behavior.thickness,173
context.layoutDirection,174
behavior.input.placement175
)176
val outlineClip = try {177
if (value.label?.position == Some(FieldLabelPosition.Notched))178
Some(179
textFieldLabelClip(180
geometry, labelBounds!!, 4.0 * value.theme.preferences.textScaleFactor181
)182
)183
else184
None185
} catch (error: Throwable) {186
text.close()187
throw error188
}189
val drawing = TextFieldDrawing(190
geometry,191
viewport,192
bars.horizontal,193
bars.vertical,194
text,195
outlineClip196
)197
return LayoutResult(198
offsetMetrics(text.metrics, size, content.y + viewport.y),199
drawing,200
{201
try {202
outlineClip?.close()203
} finally {204
text.close()205
}206
}207
)208
}