Scoop API

scoop:scoop.ui.component:0.1.0

src/actions/ink.scoop

Selected production source · src/actions/ink.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.ui.*
5import scoop.ui.style.*
6import scoop.window.accessibility.*
8internal enum ActionBehavior {
9 Passive, Press(PressControl, () -> Unit, FocusOptions)
12internal class ActionInkProps(
13 val theme: Theme,
14 val shape: ActionShape,
15 val variant: ButtonVariant,
16 val style: ButtonStyle,
17 val visual: SurfaceVisual,
18 val behavior: ActionBehavior,
19 val group: GroupPosition? = None,
20 val labelTarget: FieldTarget? = None,
21 val actionable: Boolean = true,
22 val semantics: SemanticsHandle? = None,
23 val semanticOptions: ComponentSemantics = ComponentSemantics(),
24 val semanticToggle: (() -> SemanticActionResult)? = None,
25 val semanticState: SemanticProperties? = None,
26 val semanticActions: SemanticActions? = None,
27 val prepareSemantics: (() -> SemanticsHandle?)? = None,
28 val semanticFallbackName: String? = None
31/** Buttons and badges use one layout and surface renderer, with optional activation behavior. */
32internal class ActionInk(private val props: Props<ActionInkProps>, scope: ComponentScope): Element<
33 SurfaceMetrics
34>(scope) {
35 private var currentSemantics: SemanticsHandle? = None
36 private var field: FieldLink? = None
37 private var motion: InteractionMotion? = None
38 private var painters: ThemePainterCache? = None
39 private var content: StylePainter? = None
40 private var previousMetrics: SurfaceMetrics? = None
41 private var previousIcon = false
42 private var previousTheme: Theme? = None
43 public override fun onMount() {
44 painters = Some(scope.resource { ThemePainterCache() })
45 content = Some(scope.resource { StylePainter() })
46 motion = Some(InteractionMotion(scope, props.value.visual.interaction))
47 }
48 public override fun update(): UiInvalidation {
49 val value = props.value
50 currentSemantics =
51 when (value.prepareSemantics) {
52 case Some(val prepare) -> prepare()
53 case None -> value.semantics
54 }
55 when (value.behavior) {
56 case Press(val owner, val action, val focus) -> {
57 val control = owner
58 control.press.update(
59 PressOptions(value.visual.interaction.enabled && value.actionable),
60 action
61 )
62 control.area.focusable = value.actionable
63 control.area.scope.enabled = value.visual.interaction.enabled
64 control.focus.options = focus
65 if (panelPresent(value.labelTarget) && !panelPresent(field))
66 field =
67 Some(
68 scope.resource {
69 FieldLink(scope, control.area, control.focus, currentSemantics) {
70 updateSemantics()
71 }
72 }
73 )
74 field?.update(value.labelTarget, value.visual.interaction.enabled)
75 }
76 case Passive -> {}
77 }
78 updateSemantics()
79 val specs = value.theme.tokens.motion
80 motion!!.update(
81 value.visual.interaction,
82 specs.resolve(value.style.transition.resolve(specs.hover)),
83 specs.resolve(value.style.transition.resolve(specs.press))
84 )
85 painters!!.get(value.theme)
86 val metrics = metrics(value, scope.layoutDirection)
87 val icon = isIconAction(value.shape)
88 val changed =
89 previousMetrics !=
90 Some(metrics) ||
91 previousIcon !=
92 icon ||
93 !sameTheme(previousTheme, value.theme)
94 previousMetrics = Some(metrics)
95 previousIcon = icon
96 previousTheme = Some(value.theme)
97 return if (changed)
98 UiInvalidation.Layout
99 else
100 UiInvalidation.Scene
101 }
102 private fun updateSemantics() {
103 val value = props.value
104 val handle = currentSemantics ?: return
105 if (handle.isClosed)
106 return
107 handle.bindReference(value.semanticOptions.reference)
108 val relations =
109 field?.relations(handle, value.semanticOptions.relations) ?:
110 value.semanticOptions.relations
111 when (value.behavior) {
112 case Press(_, val action, _) -> handle.update(
113 value.semanticState?.copy {
114 required =
115 value.semanticState!!.required ||
116 (this @ActionInk.field?.isRequired(handle) ?: false)
117 if (name == None && relations.labelledBy.size == 0L)
118 name = value.semanticFallbackName
119 } ?:
120 value.semanticOptions.properties(
121 enabled = value.visual.interaction.enabled && value.actionable,
122 invalid = value.visual.interaction.invalid,
123 required = this @ActionInk.field?.isRequired(handle) ?: false,
124 checked =
125 when (value.shape) {
126 case Toggle(_, _, val pressed) -> Some(
127 if (pressed)
128 SemanticChecked.On
129 else
130 SemanticChecked.Off
131 )
132 else -> None
133 }
134 ),
135 value.semanticActions ?:
136 if (!value.actionable)
137 SemanticActions()
138 else
139 when (val toggle = value.semanticToggle) {
140 case Some(_) -> SemanticActions(toggle = toggle)
141 case None -> SemanticActions(
142 invoke =
143 Some {
144 action();
145 SemanticActionResult.Completed
146 }
147 )
148 },
149 relations
150 )
151 case Passive -> handle.update(
152 value.semanticState ?:
153 value.semanticOptions.properties(enabled = value.visual.interaction.enabled),
154 relations = value.semanticOptions.relations
155 )
156 }
157 }
158 private fun metrics(value: ActionInkProps, direction: LayoutDirection): SurfaceMetrics =
159 groupSurfaceMetrics(
160 value.theme.surfaceMetrics(
161 value.visual,
162 actionOptions(
163 value.theme,
164 value.shape,
165 value.variant,
166 value.style,
167 value.visual.interaction
168 ),
169 direction
170 ),
171 value.group,
172 groupEndRadius(
173 value.theme,
174 value.style.states.apply(value.style.surface.style, value.visual.interaction)
175 ),
176 direction
177 )
178 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
179 SurfaceMetrics
180 > {
181 val value = props.value
182 val visual = value.visual
183 val options =
184 actionOptions(value.theme, value.shape, value.variant, value.style, visual.interaction)
185 val metrics = metrics(value, context.layoutDirection)
186 val inset = metrics.contentInsets.totals()
187 val child = context.measureChild(0L, childConstraints(constraints, inset))
188 var width =
189 lowerBound(
190 child.size.width + inset.width,
191 lowerBound(metrics.minimumSize.width, metrics.suggestedSize.width)
192 )
193 var height =
194 lowerBound(
195 child.size.height + inset.height,
196 lowerBound(metrics.minimumSize.height, metrics.suggestedSize.height)
197 )
198 if (isIconAction(value.shape)) {
199 width = lowerBound(width, height)
200 height = width
201 }
202 val size = constraints.constrain(Size(width, height))
203 val geometry =
204 value.theme.surfaceGeometry(
205 Rect(0.0, 0.0, size.width, size.height),
206 visual,
207 options,
208 context.layoutDirection
209 )
210 val connected =
211 groupSurfaceGeometry(geometry, metrics, value.group, context.layoutDirection)
212 val origin =
213 Point(
214 connected.content.x + (connected.content.width - child.size.width) / 2.0,
215 connected.content.y + (connected.content.height - child.size.height) / 2.0
216 )
217 context.placeChild(0L, origin)
218 return LayoutResult(offsetMetrics(child, size, origin.y), metrics)
219 }
220 public override fun scene(context: SceneContext, metrics: SurfaceMetrics) {
221 val value = props.value
222 val visual = SurfaceVisual(value.visual.kind, motion!!.sample(value.visual.interaction))
223 val options =
224 actionOptions(value.theme, value.shape, value.variant, value.style, visual.interaction)
225 val geometry =
226 groupSurfaceGeometry(
227 value.theme.surfaceGeometry(
228 Rect(0.0, 0.0, context.size.width, context.size.height),
229 visual,
230 options,
231 context.layoutDirection
232 ),
233 metrics,
234 value.group,
235 context.layoutDirection
236 )
237 val painter = painters!!.get(value.theme)
238 when (val handle = currentSemantics) {
239 case Some(val semantic) -> context.semantics(semantic, geometry.bounds)
240 case None -> {}
241 }
242 if (geometry.opacity < 1.0)
243 context.saveLayer(geometry.opacity)
244 context.draw {
245 paint -> painter.drawSurface(paint.canvas, geometry, visual, SurfaceLayer.Background)
246 }
247 when (value.behavior) {
248 case Press(val owner, _, _) -> context.place(owner.area, geometry.bounds)
249 case Passive -> {}
250 }
251 content!!.scene(context, geometry.bounds, geometry.contentStyle) {
252 children -> children.children()
253 }
254 context.draw {
255 paint -> painter.drawSurface(paint.canvas, geometry, visual, SurfaceLayer.Foreground)
256 }
257 if (geometry.opacity < 1.0)
258 context.restore()
259 }