Scoop API

scoop:scoop.ui.component:0.1.0

src/actions/group_text.scoop

Selected production source · src/actions/group_text.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.ui.*
6import scoop.ui.style.*
8internal fun groupText(
9 content: PanelContent,
10 fonts: FontCollection?,
11 style: StyleOverride,
12 group: GroupPosition?,
13 theme: Theme
14): View {
15 val density = theme.tokens.typography.body.fontSize / 14.0
16 val base =
17 theme.tokens.typography.body.copy {
18 this.lineHeight = Some(20.0 / 14.0)
19 this.weight = 500
20 this.color = theme.tokens.palette.background.foreground
21 }
22 val options =
23 surfaceOptions {
24 this.suggestedSize = Some(Size(0.0, 0.0))
25 this.minimumSize = Some(Size(0.0, 0.0))
26 this.padding = Some(Insets(10.0 * density, 0.0, 10.0 * density, 0.0))
27 this.style =
28 styleOverride {
29 this.background(Some(Paint(theme.tokens.palette.muted.background)))
30 this.border(Some(Border.all(1.0, theme.tokens.palette.border)))
31 this.corners(Corners.all(theme.tokens.corners.large))
32 }.then(style)
33 }
34 return element(
35 GroupTextProps(theme, options, group, groupEndRadius(theme, style)),
36 { current, mount -> GroupTextInk(current, scope = mount) },
37 [panelCaption(content, base, fonts)]
38 )
41private class GroupTextProps(
42 val theme: Theme,
43 val options: SurfaceOptions,
44 val group: GroupPosition?,
45 val endRadius: Double?
48private class GroupTextInk(
49 private val props: Props<GroupTextProps>,
50 scope: ComponentScope
51): Element<SurfaceGeometry>(scope) {
52 private var painters: ThemePainterCache? = None
53 private var content: StylePainter? = None
54 public override fun onMount() {
55 painters = Some(scope.resource { ThemePainterCache() })
56 content = Some(scope.resource { StylePainter() })
57 }
58 public override fun update(): UiInvalidation {
59 painters!!.get(props.value.theme)
60 return UiInvalidation.Layout
61 }
62 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
63 SurfaceGeometry
64 > {
65 val input = props.value
66 val visual = SurfaceVisual(SurfaceKind.Plain)
67 val metrics =
68 groupSurfaceMetrics(
69 input.theme.surfaceMetrics(visual, input.options, context.layoutDirection),
70 input.group,
71 input.endRadius,
72 context.layoutDirection
73 )
74 val insets = metrics.contentInsets.totals()
75 val natural = context.measureChild(0L, Constraints())
76 val width = constraints.constrain(Size(natural.size.width + insets.width, 0.0)).width
77 val child =
78 context.measureChild(
79 0L,
80 Constraints(
81 maxWidth =
82 Some(upperBound(natural.size.width, lowerBound(width - insets.width, 0.0))),
83 maxHeight = insetMaximum(constraints.maxHeight, insets.height)
84 )
85 )
86 val size = constraints.constrain(Size(width, child.size.height + insets.height))
87 val bounds = Rect(0.0, 0.0, size.width, size.height)
88 val surface =
89 groupSurfaceGeometry(
90 input.theme.surfaceGeometry(bounds, visual, input.options, context.layoutDirection),
91 metrics,
92 input.group,
93 context.layoutDirection
94 )
95 val origin =
96 Point(
97 surface.content.x + (surface.content.width - child.size.width) / 2.0,
98 surface.content.y + (surface.content.height - child.size.height) / 2.0
99 )
100 context.placeChild(0L, origin)
101 return LayoutResult(offsetMetrics(child, size, origin.y), surface)
102 }
103 public override fun scene(context: SceneContext, surface: SurfaceGeometry) {
104 val visual = SurfaceVisual(SurfaceKind.Plain)
105 val painter = painters!!.get(props.value.theme)
106 if (surface.opacity < 1.0)
107 context.saveLayer(surface.opacity)
108 context.draw {
109 paint -> painter.drawSurface(paint.canvas, surface, visual, SurfaceLayer.Background)
110 }
111 content!!.scene(context, surface.bounds, surface.contentStyle) {
112 children -> children.children()
113 }
114 context.draw {
115 paint -> painter.drawSurface(paint.canvas, surface, visual, SurfaceLayer.Foreground)
116 }
117 if (surface.opacity < 1.0)
118 context.restore()
119 }
122internal fun groupSeparator(axis: FlexAxis, color: Color): View =
123 element(
124 GroupSeparatorProps(axis, color),
125 { current, mount -> GroupSeparator(current, scope = mount) }
126 )
128private struct GroupSeparatorProps(val axis: FlexAxis, val color: Color)
130private class GroupSeparator(
131 private val props: Props<GroupSeparatorProps>,
132 scope: ComponentScope
133): Element<Unit>(scope) {
134 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
135 Unit
136 > =
137 LayoutResult(
138 LayoutMetrics(
139 constraints.constrain(
140 if (props.value.axis == FlexAxis.Horizontal)
141 Size(1.0, constraints.minHeight)
142 else
143 Size(constraints.minWidth, 1.0)
144 )
145 ),
146 Unit
147 )
148 public override fun scene(context: SceneContext, measured: Unit) {
149 val input = props.value
150 val size = context.size
151 val line =
152 if (input.axis == FlexAxis.Horizontal)
153 Rect(
154 0.0,
155 upperBound(1.0, size.height / 2.0),
156 size.width,
157 lowerBound(size.height - 2.0, 0.0)
158 )
159 else
160 Rect(
161 upperBound(1.0, size.width / 2.0),
162 0.0,
163 lowerBound(size.width - 2.0, 0.0),
164 size.height
165 )
166 context.draw { paint -> paint.canvas.drawRect(line, Paint(input.color)) }
167 }