Scoop API

scoop:scoop.ui.component:0.1.0

src/text_field/label.scoop

Selected production source · src/text_field/label.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.ui.LayoutDirection
6import scoop.ui.style.*
8internal enum FieldLabelPosition {
9 Above, Inline, Notched
12internal class TextFieldLabelAppearance(
13 val position: FieldLabelPosition,
14 val text: TextStyle,
15 val compact: TextStyle
18internal class TextFieldLabelStyle {
19 private val compact = ActionTextStyle()
20 private val inline = ActionTextStyle()
22 fun resolve(
23 input: TextFieldInput,
24 theme: Theme,
25 visual: InteractionVisual,
26 focused: Boolean,
27 populated: Boolean
28 ): TextFieldLabelAppearance? {
29 if (input.label.byteLength == 0L) return None
30 val outlined = input.chrome ==
31 TextFieldChrome.Framed &&
32 theme.tokens.textField ==
33 TextFieldTreatment.Outlined
34 val position = if (!outlined)
35 FieldLabelPosition.Above
36 else if (focused || populated) FieldLabelPosition.Notched else FieldLabelPosition.Inline
37 val colors = theme.tokens.palette
38 val foreground = if (!visual.enabled && theme.tokens.disabledOpacity >= 1.0)
39 colors.disabledText
40 else if (visual.enabled && visual.invalid)
41 colors.destructive.background
42 else if (outlined && visual.enabled && focused)
43 colors.primary.background
44 else if (!outlined || (visual.enabled && visual.hovered))
45 colors.background.foreground
46 else
47 colors.muted.foreground
48 val small = compact
49 .resolve(
50 if (outlined) theme.tokens.typography.caption else theme.tokens.typography.body,
51 foreground, input.style.label
52 )
53 val text = if (position == FieldLabelPosition.Inline)
54 inline.resolve(theme.tokens.typography.input, foreground, input.style.label)
55 else
56 small
57 val opacity = if (visual.enabled) 1.0 else theme.tokens.disabledOpacity
58 return Some(
59 TextFieldLabelAppearance(
60 position,
61 if (opacity < 1.0)
62 text.copy { color = text.color.withAlpha(text.color.alpha * opacity) }
63 else
64 text,
65 small
66 )
67 )
68 }
71internal fun textFieldLabelBounds(
72 appearance: TextFieldLabelAppearance,
73 size: Size,
74 geometry: SurfaceGeometry,
75 multiline: Boolean,
76 direction: LayoutDirection
77): Rect {
78 val content = geometry.content
79 val above = appearance.position == FieldLabelPosition.Above
80 val x = if (direction == LayoutDirection.Rtl)
81 if (above)
82 geometry.bounds.x + geometry.bounds.width - size.width
83 else
84 content.x + content.width - size.width
85 else if (above) geometry.bounds.x else content.x
86 val y = when (appearance.position) {
87 case Above -> 0.0
88 case Notched -> geometry.bounds.y - size.height / 2.0
89 case Inline ->
90 if (multiline)
91 content.y
92 else
93 geometry.bounds.y + (geometry.bounds.height - size.height) / 2.0
94 }
95 return Rect(x, y, size.width, size.height)
98internal fun textFieldLabelClip(
99 geometry: SurfaceGeometry,
100 label: Rect,
101 gap: Double
102): Path {
103 val bounds = geometry.ring?.outline?.bounds ?: geometry.bounds
104 return path(FillRule.EvenOdd) {
105 addRect(Rect(bounds.x - 1.0, bounds.y - 1.0, bounds.width + 2.0, bounds.height + 2.0))
106 addRect(Rect(label.x - gap, label.y, label.width + gap * 2.0, label.height))
107 }