scoop:scoop.ui.component:0.1.0
src/actions/ink.scoop
Selected production source · src/actions/ink.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.ui.*5
import scoop.ui.style.*6
import scoop.window.accessibility.*8
internal enum ActionBehavior {9
Passive, Press(PressControl, () -> Unit, FocusOptions)10
}12
internal 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? = None29
)31
/** Buttons and badges use one layout and surface renderer, with optional activation behavior. */32
internal class ActionInk(private val props: Props<ActionInkProps>, scope: ComponentScope): Element<33
SurfaceMetrics34
>(scope) {35
private var currentSemantics: SemanticsHandle? = None36
private var field: FieldLink? = None37
private var motion: InteractionMotion? = None38
private var painters: ThemePainterCache? = None39
private var content: StylePainter? = None40
private var previousMetrics: SurfaceMetrics? = None41
private var previousIcon = false42
private var previousTheme: Theme? = None43
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.value50
currentSemantics =51
when (value.prepareSemantics) {52
case Some(val prepare) -> prepare()53
case None -> value.semantics54
}55
when (value.behavior) {56
case Press(val owner, val action, val focus) -> {57
val control = owner58
control.press.update(59
PressOptions(value.visual.interaction.enabled && value.actionable),60
action61
)62
control.area.focusable = value.actionable63
control.area.scope.enabled = value.visual.interaction.enabled64
control.focus.options = focus65
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.motion80
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 = icon96
previousTheme = Some(value.theme)97
return if (changed)98
UiInvalidation.Layout99
else100
UiInvalidation.Scene101
}102
private fun updateSemantics() {103
val value = props.value104
val handle = currentSemantics ?: return105
if (handle.isClosed)106
return107
handle.bindReference(value.semanticOptions.reference)108
val relations =109
field?.relations(handle, value.semanticOptions.relations) ?:110
value.semanticOptions.relations111
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.semanticFallbackName119
} ?: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.On129
else130
SemanticChecked.Off131
)132
else -> None133
}134
),135
value.semanticActions ?:136
if (!value.actionable)137
SemanticActions()138
else139
when (val toggle = value.semanticToggle) {140
case Some(_) -> SemanticActions(toggle = toggle)141
case None -> SemanticActions(142
invoke =143
Some {144
action();145
SemanticActionResult.Completed146
}147
)148
},149
relations150
)151
case Passive -> handle.update(152
value.semanticState ?:153
value.semanticOptions.properties(enabled = value.visual.interaction.enabled),154
relations = value.semanticOptions.relations155
)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.interaction168
),169
direction170
),171
value.group,172
groupEndRadius(173
value.theme,174
value.style.states.apply(value.style.surface.style, value.visual.interaction)175
),176
direction177
)178
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<179
SurfaceMetrics180
> {181
val value = props.value182
val visual = value.visual183
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 = width201
}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.layoutDirection209
)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.0216
)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.value222
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.layoutDirection232
),233
metrics,234
value.group,235
context.layoutDirection236
)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
}260
}