Scoop API

scoop:scoop.ui.component:0.1.0

src/content/item_ink.scoop

Selected production source · src/content/item_ink.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.time.Duration
6import scoop.ui.*
7import scoop.ui.style.*
8import scoop.window.accessibility.*
10internal class ItemInkProps(
11 val input: ItemProps,
12 val theme: Theme,
13 val visual: InteractionVisual,
14 val behavior: ActionBehavior,
15 val semantics: SemanticsHandle?
18internal fun itemOptions(
19 input: ItemProps,
20 theme: Theme,
21 visual: InteractionVisual,
22 actionable: Boolean
23): SurfaceOptions {
24 val colors = theme.tokens.palette
25 val density = theme.tokens.typography.body.fontSize / 14.0
26 val fill =
27 if (input.variant == ItemVariant.Muted)
28 colors.muted.background.withAlpha(colors.muted.background.alpha * 0.5)
29 else
30 Color.Transparent
31 val hover =
32 if (actionable)
33 colors.muted.background
34 else
35 fill
36 val pressed =
37 if (actionable)
38 mixThemeColor(hover, colors.background.foreground, 0.08)
39 else
40 fill
41 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.ring
46 case None -> colors.ring
47 }
48 else
49 if (input.variant == ItemVariant.Outline)
50 colors.border
51 else
52 Color.Transparent
53 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 else
68 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.focusOutline
72 }
75internal class ItemInk(private val props: Props<ItemInkProps>, scope: ComponentScope): Element<
76 Unit
77>(scope) {
78 private var previousMetrics: SurfaceMetrics? = None
79 private var previousTheme: Theme? = None
80 private var input: InputScope? = None
81 private var motion: InteractionMotion? = None
82 private var painters: ThemePainterCache? = None
83 private var content: StylePainter? = None
84 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.value
92 when (value.behavior) {
93 case Passive -> input!!.enabled = value.input.enabled
94 case Press(val owner, val action, val focus) -> {
95 val control = owner
96 control.press.update(PressOptions(value.input.enabled), action)
97 control.area.scope.enabled = value.input.enabled
98 control.focus.options = focus
99 }
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.Completed
117 }
118 )
119 case Passive -> SemanticActions()
120 },
121 relations = value.input.semantics.relations
122 )
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 -> false
134 case Press(_, _, _) -> true
135 }
136 val metrics =
137 value.theme.surfaceMetrics(
138 SurfaceVisual(SurfaceKind.Plain, value.visual),
139 itemOptions(value.input, value.theme, value.visual, actionable),
140 scope.layoutDirection
141 )
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.Layout
147 else
148 UiInvalidation.Scene
149 }
150 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
151 Unit
152 > {
153 val value = props.value
154 val visual = SurfaceVisual(SurfaceKind.Plain, value.visual)
155 val actionable =
156 when (value.behavior) {
157 case Passive -> false;
158 case Press(_, _, _) -> true
159 }
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.height
169 )
170 )
171 val surface =
172 value.theme.surfaceGeometry(
173 Rect(0.0, 0.0, size.width, size.height),
174 visual,
175 options,
176 context.layoutDirection
177 )
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.value
183 val state = SurfaceVisual(SurfaceKind.Plain, motion!!.sample(value.visual))
184 val actionable =
185 when (value.behavior) {
186 case Passive -> false
187 case Press(_, _, _) -> true
188 }
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.layoutDirection
195 )
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 }