Scoop API

scoop:scoop.ui.component:0.1.0

src/media.scoop

Selected production source · src/media.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.ui.*
5import scoop.window.accessibility.*
7public class ImageViewProps public constructor(
8 public val image: Image,
9 public val naturalSize: Size,
10 public val fit: ContentFit = ContentFit.Contain,
11 public val alignment: Alignment = Alignment(),
12 public val clip: Boolean = true,
13 public val sampling: ImageSampling = ImageSampling.Linear,
14 public val opacity: Double = 1.0,
15 public val semantics: ComponentSemantics? = None
18/** Borrows pixels from its caller and has no input or loading behavior. */
19public class ImageView public constructor(
20 private val props: Props<ImageViewProps>,
21 scope: ComponentScope
22): Element<Unit>(scope) {
23 private var semantics: SemanticsHandle? = None
24 public override fun update(): UiInvalidation {
25 val input = props.value
26 requireComponent(!input.image.isClosed, "image view requires an open image")
27 checkContentSize(input.naturalSize)
28 requireComponent(
29 input.opacity.isFinite() && input.opacity >= 0.0 && input.opacity <= 1.0,
30 "invalid image opacity"
31 )
32 semantics = updateImageSemantics(scope, semantics, input.semantics)
33 return UiInvalidation.Layout
34 }
35 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
36 Unit
37 > = LayoutResult(measureContent(props.value.naturalSize, constraints), Unit)
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 when (semantics) {
42 case Some(val handle) -> context.semantics(handle, bounds)
43 case None -> {}
44 }
45 val destination =
46 fitContent(
47 input.naturalSize,
48 bounds,
49 input.fit,
50 input.alignment,
51 context.layoutDirection
52 )
53 context.withState {
54 scene ->
55 if (input.clip)
56 scene.clipRect(bounds)
57 if (destination.width > 0.0 && destination.height > 0.0)
58 scene.draw {
59 paint -> paint.canvas.drawImage(
60 input.image,
61 destination,
62 input.sampling,
63 input.opacity
64 )
65 }
66 Unit
67 }
68 }
71public class PathViewProps public constructor(
72 public val path: Path,
73 public val viewBox: Rect,
74 public val naturalSize: Size? = None,
75 public val paint: Paint = Paint(),
76 public val fit: ContentFit = ContentFit.Contain,
77 public val alignment: Alignment = Alignment(),
78 public val clip: Boolean = true,
79 public val semantics: ComponentSemantics? = None
82/** Fits an explicit view box; drawing, stroke and borrowed resource ownership stay in graphics. */
83public class PathView public constructor(
84 private val props: Props<PathViewProps>,
85 scope: ComponentScope
86): Element<Unit>(scope) {
87 private var semantics: SemanticsHandle? = None
88 public override fun update(): UiInvalidation {
89 val input = props.value
90 requireComponent(!input.path.isClosed, "path view requires an open path")
91 checkContentSize(input.viewBox.size)
92 requireComponent(
93 input.viewBox.x.isFinite() &&
94 input.viewBox.y.isFinite() &&
95 (input.viewBox.x + input.viewBox.width).isFinite() &&
96 (input.viewBox.y + input.viewBox.height).isFinite(),
97 "invalid path view box"
98 )
99 checkContentSize(input.naturalSize ?: input.viewBox.size)
100 semantics = updateImageSemantics(scope, semantics, input.semantics)
101 return UiInvalidation.Layout
102 }
103 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
104 Unit
105 > {
106 val input = props.value
107 return LayoutResult(
108 measureContent(input.naturalSize ?: input.viewBox.size, constraints),
109 Unit
110 )
111 }
112 public override fun scene(context: SceneContext, measured: Unit) {
113 val input = props.value
114 val bounds = Rect(0.0, 0.0, context.size.width, context.size.height)
115 when (semantics) {
116 case Some(val handle) -> context.semantics(handle, bounds)
117 case None -> {}
118 }
119 val destination =
120 fitContent(
121 input.naturalSize ?: input.viewBox.size,
122 bounds,
123 input.fit,
124 input.alignment,
125 context.layoutDirection
126 )
127 context.withState {
128 scene ->
129 if (input.clip)
130 scene.clipRect(bounds)
131 if (destination.width > 0.0 && destination.height > 0.0)
132 scene.draw {
133 paint -> paint.canvas.withState {
134 canvas ->
135 canvas.translate(destination.x, destination.y)
136 canvas.scale(
137 destination.width / input.viewBox.width,
138 destination.height / input.viewBox.height
139 )
140 canvas.translate(-input.viewBox.x, -input.viewBox.y)
141 canvas.drawPath(input.path, input.paint)
142 }
143 }
144 Unit
145 }
146 }
149private fun updateImageSemantics(
150 scope: ComponentScope,
151 current: SemanticsHandle?,
152 input: ComponentSemantics?
153): SemanticsHandle? {
154 when (input) {
155 case None -> {
156 current?.close();
157 return None
158 }
159 case Some(_) -> {}
160 }
161 val options = input!!
162 val result =
163 when (current) {
164 case Some(val previous) -> if (previous.isClosed)
165 createComponentSemantics(scope, SemanticRole.Image)
166 else
167 current
168 case None -> createComponentSemantics(scope, SemanticRole.Image)
169 }
170 when (result) {
171 case Some(val handle) -> {
172 handle.bindReference(options.reference)
173 handle.update(options.properties(), relations = options.relations)
174 }
175 case None -> {}
176 }
177 return result