scoop:scoop.ui.component:0.1.0
src/content/item_ink.scoop
Selected production source · src/content/item_ink.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.*5
import scoop.time.Duration6
import scoop.ui.*7
import scoop.ui.style.*8
import scoop.window.accessibility.*10
internal class ItemInkProps(11
val input: ItemProps,12
val theme: Theme,13
val visual: InteractionVisual,14
val behavior: ActionBehavior,15
val semantics: SemanticsHandle?16
)18
internal fun itemOptions(19
input: ItemProps,20
theme: Theme,21
visual: InteractionVisual,22
actionable: Boolean23
): SurfaceOptions {24
val colors = theme.tokens.palette25
val density = theme.tokens.typography.body.fontSize / 14.026
val fill =27
if (input.variant == ItemVariant.Muted)28
colors.muted.background.withAlpha(colors.muted.background.alpha * 0.5)29
else30
Color.Transparent31
val hover =32
if (actionable)33
colors.muted.background34
else35
fill36
val pressed =37
if (actionable)38
mixThemeColor(hover, colors.background.foreground, 0.08)39
else40
fill41
val background = StateColors(fill, hover, pressed, fill).resolve(visual)42
val border =43
if (visual.enabled && visual.focusVisible)44
when (input.focusOutline) {45
case Some(val value) -> value.borderColor ?: colors.ring46
case None -> colors.ring47
}48
else49
if (input.variant == ItemVariant.Outline)50
colors.border51
else52
Color.Transparent53
val local =54
styleOverride {55
this.background(Some(Paint(background)))56
this.border(Some(Border.all(1.0, border)))57
this.corners(Corners.all(theme.tokens.corners.large))58
this.boxShadows([])59
}.then(input.style)60
return surfaceOptions {61
this.suggestedSize = Some(Size(0.0, 0.0))62
this.minimumSize = Some(Size(0.0, 0.0))63
this.padding =64
Some(65
if (input.size == ItemSize.Xs)66
Insets(10.0 * density, 8.0 * density, 10.0 * density, 8.0 * density)67
else68
Insets(12.0 * density, 10.0 * density, 12.0 * density, 10.0 * density)69
)70
this.style = input.states.apply(local, visual)71
this.focus = input.focusOutline72
}73
}75
internal class ItemInk(private val props: Props<ItemInkProps>, scope: ComponentScope): Element<76
Unit77
>(scope) {78
private var previousMetrics: SurfaceMetrics? = None79
private var previousTheme: Theme? = None80
private var input: InputScope? = None81
private var motion: InteractionMotion? = None82
private var painters: ThemePainterCache? = None83
private var content: StylePainter? = None84
public override fun onMount() {85
input = Some(scope.input)86
motion = Some(InteractionMotion(scope, props.value.visual))87
painters = Some(scope.resource { ThemePainterCache() })88
content = Some(scope.resource { StylePainter() })89
}90
public override fun update(): UiInvalidation {91
val value = props.value92
when (value.behavior) {93
case Passive -> input!!.enabled = value.input.enabled94
case Press(val owner, val action, val focus) -> {95
val control = owner96
control.press.update(PressOptions(value.input.enabled), action)97
control.area.scope.enabled = value.input.enabled98
control.focus.options = focus99
}100
}101
value.semantics?.bindReference(value.input.semantics.reference)102
value.semantics?.update(103
value.input.semantics.properties(104
enabled = value.input.enabled,105
description = semanticCaption(value.input.description)106
).copy {107
if (name == None)108
name = semanticCaption(value.input.title)109
},110
actions =111
when (value.behavior) {112
case Press(_, val action, _) -> SemanticActions(113
invoke =114
Some {115
action();116
SemanticActionResult.Completed117
}118
)119
case Passive -> SemanticActions()120
},121
relations = value.input.semantics.relations122
)123
val transition =124
value.theme.tokens.motion.resolve(125
value.input.transition.resolve(126
Some(AnimationSpec.Tween(Duration.milliseconds(100L)))127
)128
)129
motion!!.update(value.visual, transition, transition)130
painters!!.get(value.theme)131
val actionable =132
when (value.behavior) {133
case Passive -> false134
case Press(_, _, _) -> true135
}136
val metrics =137
value.theme.surfaceMetrics(138
SurfaceVisual(SurfaceKind.Plain, value.visual),139
itemOptions(value.input, value.theme, value.visual, actionable),140
scope.layoutDirection141
)142
val changed = previousMetrics != Some(metrics) || !sameTheme(previousTheme, value.theme)143
previousMetrics = Some(metrics)144
previousTheme = Some(value.theme)145
return if (changed)146
UiInvalidation.Layout147
else148
UiInvalidation.Scene149
}150
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<151
Unit152
> {153
val value = props.value154
val visual = SurfaceVisual(SurfaceKind.Plain, value.visual)155
val actionable =156
when (value.behavior) {157
case Passive -> false;158
case Press(_, _, _) -> true159
}160
val options = itemOptions(value.input, value.theme, visual.interaction, actionable)161
val metrics = value.theme.surfaceMetrics(visual, options, context.layoutDirection)162
val inset = metrics.contentInsets.totals()163
val child = context.measureChild(0L, childConstraints(constraints, inset))164
val size =165
constraints.constrain(166
Size(167
constraints.maxWidth ?: (child.size.width + inset.width),168
child.size.height + inset.height169
)170
)171
val surface =172
value.theme.surfaceGeometry(173
Rect(0.0, 0.0, size.width, size.height),174
visual,175
options,176
context.layoutDirection177
)178
context.placeChild(0L, Point(surface.content.x, surface.content.y))179
return LayoutResult(offsetMetrics(child, size, surface.content.y), Unit)180
}181
public override fun scene(context: SceneContext, measured: Unit) {182
val value = props.value183
val state = SurfaceVisual(SurfaceKind.Plain, motion!!.sample(value.visual))184
val actionable =185
when (value.behavior) {186
case Passive -> false187
case Press(_, _, _) -> true188
}189
val surface =190
value.theme.surfaceGeometry(191
Rect(0.0, 0.0, context.size.width, context.size.height),192
state,193
itemOptions(value.input, value.theme, state.interaction, actionable),194
context.layoutDirection195
)196
when (value.semantics) {197
case Some(val handle) -> context.semantics(handle, surface.bounds)198
case None -> {}199
}200
val painter = painters!!.get(value.theme)201
if (surface.opacity < 1.0)202
context.saveLayer(surface.opacity)203
context.draw {204
paint -> painter.drawSurface(paint.canvas, surface, state, SurfaceLayer.Background)205
}206
when (value.behavior) {207
case Passive -> {}208
case Press(val owner, _, _) -> context.place(owner.area, surface.bounds)209
}210
content!!.scene(context, surface.bounds, surface.contentStyle) {211
children -> children.children()212
}213
context.draw {214
paint -> painter.drawSurface(paint.canvas, surface, state, SurfaceLayer.Foreground)215
}216
if (surface.opacity < 1.0)217
context.restore()218
}219
}