Scoop API

scoop:scoop.ui.component:0.1.0

src/text_field/layout.scoop

Selected production source · src/text_field/layout.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.ui.*
6import scoop.ui.text.TextEditMeasurement
7import scoop.ui.style.*
9internal 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? = None
18/** Only the scalar font metric is cached; both temporary native objects close immediately. */
19internal class TextFieldLineHeight {
20 private var fonts: FontCollection? = None
21 private var revision = -1L
22 private var style: TextStyle? = None
23 private var height = 0.0
24 fun measure(fonts: FontCollection, style: TextStyle): Double {
25 val sameFonts = when (this.fonts) {
26 case Some(val previous) -> previous === fonts
27 case None -> false
28 }
29 val sameStyle = when (this.style) {
30 case Some(val previous) -> previous === style
31 case None -> false
32 }
33 if (sameFonts && sameStyle && revision == fonts.revision) return height
34 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.revision
39 height = next
40 return next
41 }
44internal fun measureTextField(
45 ink: TextFieldInk,
46 context: MeasureContext,
47 constraints: Constraints
48): LayoutResult<TextFieldDrawing> {
49 val value = ink.props.value
50 val input = value.input
51 val base = value.theme.surfaceMetrics(value.visual, value.surface, context.layoutDirection)
52 val metrics = if (input.chrome == TextFieldChrome.Bare)
53 bareTextFieldMetrics(base)
54 else
55 groupSurfaceMetrics(
56 base,
57 input.group,
58 groupEndRadius(value.theme, value.surface.style),
59 context.layoutDirection
60 )
61 val insets = metrics.contentInsets.totals()
62 val naturalWidth = value.surface.suggestedSize?.width ?: 240.0
63 val width = constraints
64 .constrain(
65 Size(constraints.maxWidth ?: lowerBound(naturalWidth, metrics.minimumSize.width), 0.0)
66 )
67 .width
68 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.0
74 )
75 Some(
76 if (natural.size.width > available)
77 context.measureChild(0L, Constraints(maxWidth = Some(available)))
78 else
79 natural
80 )
81 }
83 case None -> None
84 }
85 val descriptionIndex = if (label == None) 0L else 1L
86 val outlined = value.theme.tokens.textField == TextFieldTreatment.Outlined
87 val support = if (outlined) metrics.contentInsets else Insets()
88 val description = if (input.description.byteLength > 0L)
89 Some(
90 context
91 .measureChild(
92 descriptionIndex,
93 Constraints(
94 maxWidth = Some(lowerBound(width - support.totals().width, 0.0))
95 )
96 )
97 )
98 else
99 None
100 val gap = if (description != None) value.theme.tokens.spacing.small else 0.0
101 nonnegative(gap, "invalid text field description spacing")
102 val after = (description?.size?.height ?: 0.0) + gap
103 val before = when (value.label) {
104 case Some(val appearance) ->
105 if (appearance.position == FieldLabelPosition.Above)
106 label!!.size.height + value.theme.tokens.spacing.small
107 else
108 ink.labelHeight.measure(ink.fonts, appearance.compact) / 2.0
109 case None -> 0.0
110 }
111 val axes = if (input.options.wrap) ScrollAxes.Vertical else ScrollAxes.Both
112 val visibility = if (input.options.multiline) input.scrollbars else ScrollbarVisibility.Hidden
113 val behavior = ink.scrollbarFeedback.options
114 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 = value
123 .theme
124 .surfaceGeometry(
125 Rect(0.0, before, size.width, fieldHeight),
126 value.visual,
127 value.surface,
128 context.layoutDirection
129 )
130 val geometry = if (input.chrome == TextFieldChrome.Bare)
131 bareTextFieldGeometry(original, metrics)
132 else
133 groupSurfaceGeometry(original, metrics, input.group, context.layoutDirection)
134 val content = geometry.content
135 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.layoutDirection
143 )
144 context.placeChild(0L, bounds.origin)
145 Some(bounds)
146 }
148 case None -> None
149 }
150 val text = ink
151 .host
152 .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.layoutDirection
163 )
164 }
165 )
166 val snapshot = text.scrollSnapshot
167 val bars = ScrollBarsLayout(
168 outer,
169 snapshot,
170 axes,
171 visibility,
172 behavior.thickness,
173 context.layoutDirection,
174 behavior.input.placement
175 )
176 val outlineClip = try {
177 if (value.label?.position == Some(FieldLabelPosition.Notched))
178 Some(
179 textFieldLabelClip(
180 geometry, labelBounds!!, 4.0 * value.theme.preferences.textScaleFactor
181 )
182 )
183 else
184 None
185 } catch (error: Throwable) {
186 text.close()
187 throw error
188 }
189 val drawing = TextFieldDrawing(
190 geometry,
191 viewport,
192 bars.horizontal,
193 bars.vertical,
194 text,
195 outlineClip
196 )
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 )