Scoop API

scoop:scoop.ui.component:0.1.0

src/resizable/values.scoop

Selected production source · src/resizable/values.scoop

1package scoop.ui.component
3import scoop.collections.HashMap
4import scoop.ui.*
5import scoop.ui.style.*
7public struct ResizableSize(val key: ViewKey, val fraction: Double, val collapsed: Boolean = false)
9public class ResizablePanelProps public constructor(
10 public val key: ViewKey,
11 public val content: View,
12 public val minimum: Double = 0.0,
13 public val maximum: Double? = None,
14 public val collapsible: Boolean = false,
15 public val collapsedSize: Double = 0.0,
16 public val style: StyleOverride = styleOverride()
17) {
18 init {
19 requireComponent(key != ViewKey.Position, "resizable panel requires an explicit key")
20 nonnegative(minimum, "invalid panel minimum")
21 nonnegative(collapsedSize, "invalid collapsed panel size")
22 when (maximum) {
23 case Some(val value) -> {
24 nonnegative(value, "invalid panel maximum")
25 requireComponent(value >= minimum, "panel maximum is less than minimum")
26 }
27 case None -> {}
28 }
29 requireComponent(
30 !collapsible || collapsedSize <= minimum,
31 "collapsed panel size exceeds its minimum"
32 )
33 }
36public class ResizableHandleOptions public constructor(
37 public val afterKey: ViewKey,
38 public val withHandle: Boolean? = None,
39 public val enabled: Boolean = true,
40 public val focus: FocusOptions = FocusOptions(),
41 public val style: StyleOverride = styleOverride(),
42 public val hitWidth: Double = 20.0,
43 public val semantics: ComponentSemantics = ComponentSemantics()
44) {
45 init {
46 requireComponent(
47 hitWidth.isFinite() && hitWidth > 0.0,
48 "resizable handle hit width must be finite and positive"
49 )
50 }
53public class ResizableProps public constructor(
54 public val sizes: Binding<Array<ResizableSize>>,
55 public val panels: Array<ResizablePanelProps>,
56 public val handles: Array<ResizableHandleOptions> = [],
57 public val orientation: Orientation = Orientation.Horizontal,
58 public val enabled: Boolean = true,
59 public val withHandle: Boolean = false,
60 public val length: Double? = None,
61 public val keyboardStep: Double = 10.0,
62 public val dragThreshold: Double = 4.0,
63 public val theme: ThemeSource? = None,
64 public val style: StyleOverride = styleOverride(),
65 public val clip: Boolean = false,
66 public val onSizesChange: (Array<ResizableSize>) -> Unit = {},
67 public val onSizesCommit: (Array<ResizableSize>) -> Unit = {},
68 public val onSizesCancel: (Array<ResizableSize>, DragCancelReason) -> Unit = { _, _ -> }
69) {
70 init {
71 when (length) {
72 case Some(val value) -> nonnegative(value, "invalid resizable length")
73 case None -> {}
74 }
75 requireComponent(
76 keyboardStep.isFinite() && keyboardStep > 0.0,
77 "resizable keyboard step must be finite and positive"
78 )
79 nonnegative(dragThreshold, "invalid resizable drag threshold")
80 val keys = HashMap<ViewKey, Long>()
81 for (index in 0L ..< panels.size) {
82 val key = panels[index].key
83 requireComponent(keys[key] == None, "duplicate resizable panel key")
84 keys[key] = index
85 }
86 val configured = HashMap<ViewKey, Boolean>()
87 for (handle in handles) {
88 val index = keys[handle.afterKey]
89 requireComponent(
90 index != None && index!! + 1L < panels.size,
91 "resizable handle requires an adjacent panel pair"
92 )
93 requireComponent(configured[handle.afterKey] == None, "duplicate resizable handle")
94 configured[handle.afterKey] = true
95 }
96 }
99internal fun resizableHandle(input: ResizableProps, index: Long): ResizableHandleOptions {
100 val key = input.panels[index].key
101 for (handle in input.handles)
102 if (handle.afterKey == key)
103 return handle
104 return ResizableHandleOptions(key)
107internal fun sameResizableSizes(a: Array<ResizableSize>, b: Array<ResizableSize>): Boolean {
108 if (a === b)
109 return true
110 if (a.size != b.size)
111 return false
112 for (index in 0L ..< a.size)
113 if (
114 a[index].key !=
115 b[index].key ||
116 a[index].fraction !=
117 b[index].fraction ||
118 a[index].collapsed !=
119 b[index].collapsed
120 )
121 return false
122 return true
125internal fun resizableValues(input: Array<ResizableSize>): HashMap<ViewKey, ResizableSize> {
126 val values = HashMap<ViewKey, ResizableSize>()
127 for (value in input) {
128 requireComponent(value.key != ViewKey.Position, "resizable size requires an explicit key")
129 requireComponent(
130 value.fraction.isFinite() && value.fraction >= 0.0 && value.fraction <= 1.0,
131 "resizable fraction must be between zero and one"
132 )
133 requireComponent(values[value.key] == None, "duplicate resizable size key")
134 values[value.key] = value
135 }
136 return values