Scoop API

scoop:scoop.ui.style:0.1.0

src/borders.scoop

Selected production source · src/borders.scoop

1package scoop.ui.style
3import scoop.graphics.*
4import scoop.ui.LayoutDirection
6/** One inward edge in logical units. */
7public struct BorderEdge(val width: Double = 0.0, val color: Color = Color.Transparent)
9/** Physical edges after direction has been resolved. */
10public struct BorderEdges(
11 val left: BorderEdge = BorderEdge(),
12 val top: BorderEdge = BorderEdge(),
13 val right: BorderEdge = BorderEdge(),
14 val bottom: BorderEdge = BorderEdge()
15) {
16 public companion object {
17 public fun all(width: Double, color: Color): BorderEdges =
18 BorderEdges(
19 BorderEdge(width, color),
20 BorderEdge(width, color),
21 BorderEdge(width, color),
22 BorderEdge(width, color)
23 )
24 }
25 public val insets: Insets get() {
26 val result = Insets(left.width, top.width, right.width, bottom.width)
27 result.totals()
28 return result
29 }
30 internal fun values(): Array<BorderEdge> = [left, top, right, bottom]
31 internal fun uniformColor(): Color? {
32 var result: Color? = None
33 for (edge in values()) {
34 if (edge.width == 0.0)
35 continue
36 when (result) {
37 case Some(val color) -> if (color != edge.color)
38 return None
39 case None -> result = Some(edge.color)
40 }
41 }
42 return result
43 }
46/** Physical and logical descriptions never share ambiguous edge names. */
47public enum Border {
48 Physical(val edges: BorderEdges),
49 Logical(
50 val start: BorderEdge = BorderEdge(),
51 val top: BorderEdge = BorderEdge(),
52 val end: BorderEdge = BorderEdge(),
53 val bottom: BorderEdge = BorderEdge()
54 )
56 public companion object {
57 public fun all(width: Double, color: Color): Border =
58 Border.Physical(BorderEdges.all(width, color))
59 }
60 public fun resolve(direction: LayoutDirection): BorderEdges =
61 when (this) {
62 case Physical(val edges) -> edges
63 case Logical(val start, val top, val end, val bottom) -> if (direction == LayoutDirection.Ltr)
64 BorderEdges(start, top, end, bottom)
65 else
66 BorderEdges(end, top, start, bottom)
67 }
70/** Directional corners are converted before entering graphics geometry. */
71public enum Corners {
72 Physical(val radii: CornerRadii),
73 Logical(
74 val topStart: Size = Size(0.0, 0.0),
75 val topEnd: Size = Size(0.0, 0.0),
76 val bottomEnd: Size = Size(0.0, 0.0),
77 val bottomStart: Size = Size(0.0, 0.0)
78 )
80 public companion object {
81 public fun all(radius: Double): Corners = Corners.Physical(CornerRadii.all(radius))
82 }
83 public fun resolve(direction: LayoutDirection): CornerRadii =
84 when (this) {
85 case Physical(val radii) -> radii
86 case Logical(val topStart, val topEnd, val bottomEnd, val bottomStart) -> if (
87 direction == LayoutDirection.Ltr
88 )
89 CornerRadii(topStart, topEnd, bottomEnd, bottomStart)
90 else
91 CornerRadii(topEnd, topStart, bottomStart, bottomEnd)
92 }