scoop:scoop.ui.component:0.1.0
src/svg.scoop
Selected production source · src/svg.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.svg.*5
import scoop.ui.*7
public 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()14
)16
/** Borrows a parsed document. The caller keeps it open for the view's lifetime. */17
public class SvgView public constructor(18
private val props: Props<SvgViewProps>,19
scope: ComponentScope20
): Element<Unit>(scope) {21
public override fun update(): UiInvalidation {22
val input = props.value23
requireComponent(!input.document.isClosed, "SVG view requires an open document")24
svgNaturalSize(input.document, input.naturalSize)25
return UiInvalidation.Layout26
}27
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<28
Unit29
> =30
LayoutResult(31
measureContent(32
svgNaturalSize(props.value.document, props.value.naturalSize),33
constraints34
),35
Unit36
)38
public override fun scene(context: SceneContext, measured: Unit) {39
val input = props.value40
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.layoutDirection48
)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
Unit58
}59
}60
}62
internal 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.size68
case None -> throw IllegalArgumentException(69
Some("SVG view requires an explicit natural size")70
)71
}72
checkContentSize(size)73
return size74
}76
/** One mounted icon's parsed resource; failed replacements keep the previous document. */77
internal class IconDocument: Closeable {78
private var source: SvgAsset? = None79
private var current: SvgDocument? = None80
private var closed = false81
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 document97
}99
public override fun close() {100
when (current) {101
case Some(val previous) -> previous.close()102
case None -> {}103
}104
current = None105
source = None106
closed = true107
}108
}