Scoop API

scoop:scoop.ui.component:0.1.0

src/svg.scoop

Selected production source · src/svg.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.svg.*
5import scoop.ui.*
7public class SvgViewProps public constructor(
8 public val document: SvgDocument,
9 public val naturalSize: Size? = None,
10 public val fit: ContentFit = ContentFit.Contain,
11 public val alignment: Alignment = Alignment(),
12 public val clip: Boolean = true,
13 public val options: SvgDrawOptions = SvgDrawOptions()
16/** Borrows a parsed document. The caller keeps it open for the view's lifetime. */
17public class SvgView public constructor(
18 private val props: Props<SvgViewProps>,
19 scope: ComponentScope
20): Element<Unit>(scope) {
21 public override fun update(): UiInvalidation {
22 val input = props.value
23 requireComponent(!input.document.isClosed, "SVG view requires an open document")
24 svgNaturalSize(input.document, input.naturalSize)
25 return UiInvalidation.Layout
26 }
27 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
28 Unit
29 > =
30 LayoutResult(
31 measureContent(
32 svgNaturalSize(props.value.document, props.value.naturalSize),
33 constraints
34 ),
35 Unit
36 )
38 public override fun scene(context: SceneContext, measured: Unit) {
39 val input = props.value
40 val bounds = Rect(0.0, 0.0, context.size.width, context.size.height)
41 val destination =
42 fitContent(
43 svgNaturalSize(input.document, input.naturalSize),
44 bounds,
45 input.fit,
46 input.alignment,
47 context.layoutDirection
48 )
49 context.withState {
50 scene ->
51 if (input.clip)
52 scene.clipRect(bounds)
53 if (destination.width > 0.0 && destination.height > 0.0)
54 scene.draw {
55 paint -> paint.canvas.drawSvg(input.document, destination, input.options)
56 }
57 Unit
58 }
59 }
62internal fun svgNaturalSize(document: SvgDocument, explicit: Size? = None): Size {
63 val size =
64 explicit ?:
65 document.intrinsicSize ?:
66 when (document.viewBox) {
67 case Some(val box) -> box.size
68 case None -> throw IllegalArgumentException(
69 Some("SVG view requires an explicit natural size")
70 )
71 }
72 checkContentSize(size)
73 return size
76/** One mounted icon's parsed resource; failed replacements keep the previous document. */
77internal class IconDocument: Closeable {
78 private var source: SvgAsset? = None
79 private var current: SvgDocument? = None
80 private var closed = false
81 val document: SvgDocument get() =
82 current ?: throw IllegalStateException(Some("icon has no document"))
84 fun update(asset: SvgAsset): SvgDocument {
85 if (closed)
86 throw IllegalStateException(Some("icon document is closed"))
87 if (source != Some(asset)) {
88 val next = SvgDocument.parse(asset.source)
89 when (current) {
90 case Some(val previous) -> previous.close()
91 case None -> {}
92 }
93 current = Some(next)
94 source = Some(asset)
95 }
96 return document
97 }
99 public override fun close() {
100 when (current) {
101 case Some(val previous) -> previous.close()
102 case None -> {}
103 }
104 current = None
105 source = None
106 closed = true
107 }