Scoop API

scoop:scoop.ui.style:0.1.0

src/cache/geometry.scoop

Selected production source · src/cache/geometry.scoop

1package scoop.ui.style
3import scoop.graphics.*
4import scoop.ui.LayoutDirection
6internal struct OutlineInput(
7 val outline: RoundedOutline,
8 val insets: Insets,
9 val partitions: Boolean
12internal class DecorationPaths(
13 val outline: Path,
14 val border: Path?,
15 val regions: Array<Path>
16) : Closeable {
17 public override fun close() {
18 var failure = cleanupStyle(None) { outline.close() }
19 failure = cleanupStyle(failure) { border?.close() }
20 for (region in regions)
21 failure = cleanupStyle(failure) { region.close() }
22 throwStyle(failure)
23 }
26internal class DecorationGeometry : Closeable {
27 private var input: OutlineInput? = None
28 private var current: DecorationPaths? = None
30 fun update(
31 bounds: Rect,
32 style: Style,
33 direction: LayoutDirection = LayoutDirection.Ltr,
34 hasBackdrop: Boolean = false
35 ): DecorationPaths? {
36 val edges = physicalEdges(style, direction)
37 val insets = edges.insets
38 val outline = RoundedOutline(bounds, style.corners.resolve(direction)).normalized()
39 val hasBorder = insets.left >
40 0.0 ||
41 insets.top >
42 0.0 ||
43 insets.right >
44 0.0 ||
45 insets.bottom >
46 0.0
47 val background = when (style.background) {
48 case Some(_) -> true;
49 case None -> false
50 }
51 if (!background &&
52 !hasBackdrop &&
53 !hasBorder &&
54 style.boxShadows.size ==
55 0L &&
56 !style.clipContent
57 ) {
58 close()
59 return None
60 }
61 val nextInput = OutlineInput(outline, insets, hasBorder && edges.uniformColor() == None)
62 if (input == Some(nextInput)) return current
63 val next = decorationPaths(nextInput, edges)
64 val previous = current
65 current = Some(next)
66 input = Some(nextInput)
67 previous?.close()
68 return Some(next)
69 }
70 public override fun close() {
71 val previous = current
72 current = None
73 input = None
74 previous?.close()
75 }
78private fun decorationPaths(input: OutlineInput, edges: BorderEdges): DecorationPaths {
79 val outer = input.outline
80 val widths = input.insets
81 val owned = ArrayList<Path>()
82 try {
83 val outline = path { addRoundRect(outer.bounds, outer.radii) }
84 owned.add(outline)
85 val ring = if ((
86 widths.left > 0.0 || widths.top > 0.0 || widths.right > 0.0 || widths.bottom > 0.0
87 ) &&
88 outer.bounds.width >
89 0.0 &&
90 outer.bounds.height >
91 0.0
92 ) {
93 val inner = insetOutline(outer, widths)
94 val value = path(FillRule.EvenOdd) {
95 addRoundRect(outer.bounds, outer.radii)
96 if (inner.bounds.width > 0.0 && inner.bounds.height > 0.0)
97 addRoundRect(inner.bounds, inner.radii)
98 }
99 owned.add(value)
100 Some(value)
101 } else
102 None
103 val regions = ArrayList<Path>()
104 if (input.partitions)
105 for (points in borderRegions(outer.bounds, edges)) {
106 val region = path {
107 if (points.size > 0L) {
108 moveTo(points[0L])
109 for (index in 1L..<points.size)
110 lineTo(points[index])
111 closeContour()
112 }
113 }
114 owned.add(region)
115 regions.add(region)
116 }
117 return DecorationPaths(outline, ring, regions.toArray())
118 } catch (error: Throwable) {
119 var failure: Throwable? = Some(error)
120 for (value in owned)
121 failure = cleanupStyle(failure) { value.close() }
122 throwStyle(failure)
123 throw error
124 }