scoop:scoop.ui.component:0.1.0
src/text_field/label.scoop
Selected production source · src/text_field/label.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.ui.LayoutDirection6
import scoop.ui.style.*8
internal enum FieldLabelPosition {9
Above, Inline, Notched10
}12
internal class TextFieldLabelAppearance(13
val position: FieldLabelPosition,14
val text: TextStyle,15
val compact: TextStyle16
)18
internal 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: Boolean28
): TextFieldLabelAppearance? {29
if (input.label.byteLength == 0L) return None30
val outlined = input.chrome ==31
TextFieldChrome.Framed &&32
theme.tokens.textField ==33
TextFieldTreatment.Outlined34
val position = if (!outlined)35
FieldLabelPosition.Above36
else if (focused || populated) FieldLabelPosition.Notched else FieldLabelPosition.Inline37
val colors = theme.tokens.palette38
val foreground = if (!visual.enabled && theme.tokens.disabledOpacity >= 1.0)39
colors.disabledText40
else if (visual.enabled && visual.invalid)41
colors.destructive.background42
else if (outlined && visual.enabled && focused)43
colors.primary.background44
else if (!outlined || (visual.enabled && visual.hovered))45
colors.background.foreground46
else47
colors.muted.foreground48
val small = compact49
.resolve(50
if (outlined) theme.tokens.typography.caption else theme.tokens.typography.body,51
foreground, input.style.label52
)53
val text = if (position == FieldLabelPosition.Inline)54
inline.resolve(theme.tokens.typography.input, foreground, input.style.label)55
else56
small57
val opacity = if (visual.enabled) 1.0 else theme.tokens.disabledOpacity58
return Some(59
TextFieldLabelAppearance(60
position,61
if (opacity < 1.0)62
text.copy { color = text.color.withAlpha(text.color.alpha * opacity) }63
else64
text,65
small66
)67
)68
}69
}71
internal fun textFieldLabelBounds(72
appearance: TextFieldLabelAppearance,73
size: Size,74
geometry: SurfaceGeometry,75
multiline: Boolean,76
direction: LayoutDirection77
): Rect {78
val content = geometry.content79
val above = appearance.position == FieldLabelPosition.Above80
val x = if (direction == LayoutDirection.Rtl)81
if (above)82
geometry.bounds.x + geometry.bounds.width - size.width83
else84
content.x + content.width - size.width85
else if (above) geometry.bounds.x else content.x86
val y = when (appearance.position) {87
case Above -> 0.088
case Notched -> geometry.bounds.y - size.height / 2.089
case Inline ->90
if (multiline)91
content.y92
else93
geometry.bounds.y + (geometry.bounds.height - size.height) / 2.094
}95
return Rect(x, y, size.width, size.height)96
}98
internal fun textFieldLabelClip(99
geometry: SurfaceGeometry,100
label: Rect,101
gap: Double102
): Path {103
val bounds = geometry.ring?.outline?.bounds ?: geometry.bounds104
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
}108
}