scoop:scoop.ui.component:0.1.0
src/media.scoop
Selected production source · src/media.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.ui.*5
import scoop.window.accessibility.*7
public 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? = None16
)18
/** Borrows pixels from its caller and has no input or loading behavior. */19
public class ImageView public constructor(20
private val props: Props<ImageViewProps>,21
scope: ComponentScope22
): Element<Unit>(scope) {23
private var semantics: SemanticsHandle? = None24
public override fun update(): UiInvalidation {25
val input = props.value26
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.Layout34
}35
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<36
Unit37
> = LayoutResult(measureContent(props.value.naturalSize, constraints), Unit)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
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.layoutDirection52
)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.opacity64
)65
}66
Unit67
}68
}69
}71
public 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? = None80
)82
/** Fits an explicit view box; drawing, stroke and borrowed resource ownership stay in graphics. */83
public class PathView public constructor(84
private val props: Props<PathViewProps>,85
scope: ComponentScope86
): Element<Unit>(scope) {87
private var semantics: SemanticsHandle? = None88
public override fun update(): UiInvalidation {89
val input = props.value90
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.Layout102
}103
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<104
Unit105
> {106
val input = props.value107
return LayoutResult(108
measureContent(input.naturalSize ?: input.viewBox.size, constraints),109
Unit110
)111
}112
public override fun scene(context: SceneContext, measured: Unit) {113
val input = props.value114
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.layoutDirection126
)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.height139
)140
canvas.translate(-input.viewBox.x, -input.viewBox.y)141
canvas.drawPath(input.path, input.paint)142
}143
}144
Unit145
}146
}147
}149
private fun updateImageSemantics(150
scope: ComponentScope,151
current: SemanticsHandle?,152
input: ComponentSemantics?153
): SemanticsHandle? {154
when (input) {155
case None -> {156
current?.close();157
return None158
}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
else167
current168
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 result178
}