Scoop API

scoop:scoop.ui.component:0.1.0

src/actions/icon.scoop

Selected production source · src/actions/icon.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.svg.*
5import scoop.ui.*
6import scoop.ui.style.*
8public class IconProps public constructor(
9 public val asset: SvgAsset,
10 public val size: Double = 24.0,
11 public val color: Color? = None,
12 public val strokeWidth: Double = 2.0,
13 public val absoluteStrokeWidth: Boolean = false,
14 public val theme: ThemeSource? = None
15) {
16 init {
17 nonnegative(size, "invalid icon size")
18 nonnegative(strokeWidth, "invalid icon stroke width")
19 }
22/** An owned SVG icon whose default color follows the current theme. */
23public class Icon public constructor(
24 private val props: Props<IconProps>,
25 scope: ComponentScope
26): Component(scope) {
27 private val themes = ThemeAccess(scope)
28 public override fun onMount() {
29 if (props.value.color == None)
30 themes.mount(props.value.theme)
31 }
32 public override fun render(): View {
33 val input = props.value
34 return element(
35 IconInkProps(
36 input.asset,
37 input.size,
38 input.color ?: themes.read(input.theme).tokens.typography.action.color,
39 input.strokeWidth,
40 input.absoluteStrokeWidth
41 ),
42 { current, mount -> IconInk(current, mount) }
43 )
44 }
47private class IconInkProps(
48 val asset: SvgAsset,
49 val size: Double,
50 val color: Color,
51 val strokeWidth: Double,
52 val absoluteStrokeWidth: Boolean
55private class IconInk(private val props: Props<IconInkProps>, scope: ComponentScope): Element<Unit>(
56 scope
57) {
58 private var owned: IconDocument? = None
59 private var previousSize: Double? = None
60 public override fun onMount() {
61 owned = Some(scope.resource { IconDocument() })
62 }
63 public override fun update(): UiInvalidation {
64 val input = props.value
65 val document = owned!!.update(input.asset)
66 svgNaturalSize(document)
67 if (input.absoluteStrokeWidth)
68 iconStrokeCoordinates(document)
69 val resized = previousSize != Some(input.size)
70 previousSize = Some(input.size)
71 return if (resized)
72 UiInvalidation.Layout
73 else
74 UiInvalidation.Scene
75 }
76 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
77 Unit
78 > =
79 LayoutResult(
80 LayoutMetrics(constraints.constrain(Size(props.value.size, props.value.size))),
81 Unit
82 )
84 public override fun scene(context: SceneContext, measured: Unit) {
85 val input = props.value
86 val document = owned!!.document
87 val destination =
88 fitContent(
89 svgNaturalSize(document),
90 Rect(0.0, 0.0, context.size.width, context.size.height)
91 )
92 if (destination.width > 0.0 && destination.height > 0.0) {
93 context.draw {
94 paint -> paint.canvas.drawIcon(
95 document,
96 destination,
97 input.color,
98 input.strokeWidth,
99 input.absoluteStrokeWidth
100 )
101 }
102 }
103 }
106internal fun Canvas.drawIcon(
107 document: SvgDocument,
108 destination: Rect,
109 color: Color,
110 strokeWidth: Double,
111 absoluteStrokeWidth: Boolean
112) {
113 if (destination.width <= 0.0 || destination.height <= 0.0)
114 return
115 val stroke =
116 if (absoluteStrokeWidth)
117 strokeWidth * iconStrokeCoordinates(document).width / destination.width
118 else
119 strokeWidth
120 drawSvg(document, destination, SvgDrawOptions(Some(color), Some(stroke)))
123internal fun iconStrokeCoordinates(document: SvgDocument): Size {
124 val natural =
125 document.intrinsicSize ?:
126 throw IllegalArgumentException(
127 Some("absolute icon stroke requires fixed root dimensions")
128 )
129 val coordinates =
130 when (document.viewBox) {
131 case Some(val box) -> box.size
132 case None -> natural
133 }
134 requireComponent(
135 natural.width * coordinates.height == natural.height * coordinates.width,
136 "absolute icon stroke requires matching viewport and viewBox aspect ratios"
137 )
138 return coordinates