Scoop API

scoop:scoop.ui.component:0.1.0

src/resizable/control.scoop

Selected production source · src/resizable/control.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.window.*
8import scoop.window.accessibility.*
10internal class ResizableGesture(
11 val index: Long,
12 val initial: Array<ResizableSize>,
13 val lengths: Array<Double>,
14 val geometry: ResizableGeometry,
15 val drag: Drag
16) {
17 var expected = initial
20internal class ResizableControl(
21 private val scope: ComponentScope,
22 private val input: () -> ResizableInput
23) {
24 private var geometry: ResizableGeometry? = None
25 private val semanticRevision = state(0UL)
26 private var gesture: ResizableGesture? = None
27 private var configured = input().props
28 private var direction = scope.layoutDirection
29 private fun compatible(shape: ResizableGeometry): Boolean =
30 sameResizablePanels(shape.input.panels, input().props.panels) &&
31 shape.input.orientation ==
32 input().props.orientation &&
33 shape.direction ==
34 (
35 if (scope.isMounted)
36 scope.layoutDirection
37 else
38 direction
39 )
40 private fun enabled(index: Long): Boolean =
41 input().props.enabled &&
42 index +
43 1L <
44 input().props.panels.size &&
45 resizableHandle(input().props, index).enabled
46 fun update() {
47 val props = input().props
48 when (gesture) {
49 case Some(val active) -> {
50 if (
51 !compatible(active.geometry) ||
52 props.dragThreshold !=
53 active.geometry.input.dragThreshold ||
54 !sameResizableSizes(active.expected, props.sizes.value)
55 )
56 cancel(DragCancelReason.ConfigurationChanged)
57 else
58 if (!enabled(active.index))
59 cancel(DragCancelReason.Disabled)
60 }
61 case None -> {}
62 }
63 if (!scope.isMounted)
64 return
65 if (
66 !sameResizablePanels(configured.panels, props.panels) ||
67 configured.orientation !=
68 props.orientation ||
69 direction !=
70 scope.layoutDirection
71 )
72 geometry = None
73 configured = props
74 direction = scope.layoutDirection
75 }
76 fun accept(shape: ResizableGeometry) {
77 if (!scope.isMounted || !compatible(shape))
78 return
79 val previous = geometry
80 geometry = Some(shape)
81 if (previous?.target != Some(shape.target))
82 semanticRevision.value += 1UL
83 when (gesture) {
84 case Some(val active) -> if (
85 shape.size !=
86 active.geometry.size ||
87 shape.size.width ==
88 0.0 ||
89 shape.size.height ==
90 0.0
91 )
92 cancel(DragCancelReason.GeometryUnavailable)
93 case None -> {}
94 }
95 }
96 fun available(index: Long): Boolean =
97 when (geometry) {
98 case Some(val shape) -> enabled(index) &&
99 compatible(shape) &&
100 shape.size.width >
101 0.0 &&
102 shape.size.height >
103 0.0
104 case None -> false
105 }
106 fun handle(index: Long, drag: Drag, focus: FocusTarget, event: DragEvent) {
107 when (event) {
108 case Started(val sample) -> {
109 if (!available(index)) {
110 drag.cancel();
111 return
112 }
113 cancel(DragCancelReason.Explicit)
114 if (!scope.isMounted)
115 return
116 if (!focus.handle.requestFocus(FocusOrigin.Pointer)) {
117 drag.cancel();
118 return
119 }
120 if (!scope.isMounted)
121 return
122 val shape = geometry!!
123 val initial = input().props.sizes.value
124 val lengths =
125 resizableAllocation(
126 input().props.panels,
127 resizableValues(initial),
128 shape.target
129 )
130 gesture = Some(ResizableGesture(index, initial, lengths, shape, drag))
131 move(sample.displacement)
132 }
133 case Moved(val sample) -> move(sample.displacement)
134 case Ended(val sample) -> {
135 move(sample.displacement)
136 val active = gesture ?: return
137 gesture = None
138 val props = input().props
139 if (scope.isMounted && !sameResizableSizes(active.initial, props.sizes.value))
140 props.onSizesCommit(props.sizes.value)
141 }
142 case Cancelled(_, val reason) -> cancel(reason)
143 }
144 }
145 private fun move(displacement: Double) {
146 val active = gesture ?: return
147 val props = input().props
148 if (
149 !compatible(active.geometry) || !sameResizableSizes(active.expected, props.sizes.value)
150 ) {
151 cancel(DragCancelReason.ConfigurationChanged);
152 return
153 }
154 val index = active.index
155 val delta =
156 if (
157 props.orientation ==
158 Orientation.Horizontal &&
159 scope.layoutDirection ==
160 LayoutDirection.Rtl
161 )
162 -displacement
163 else
164 displacement
165 val pair =
166 resizablePair(
167 active.lengths[index],
168 active.lengths[index + 1L],
169 delta,
170 props.panels[index],
171 props.panels[index + 1L]
172 )
173 val next =
174 Array<Double>(active.lengths.size) {
175 i -> if (i == index)
176 pair.first
177 else
178 if (i == index + 1L)
179 pair.second
180 else
181 active.lengths[i]
182 }
183 val before =
184 resizableAllocation(
185 props.panels,
186 resizableValues(props.sizes.value),
187 active.geometry.target
188 )
189 if (sameSliderValues(before, next))
190 return
191 apply(resizableModel(props.panels, props.sizes.value, before, next), Some(active))
192 }
193 fun apply(next: Array<ResizableSize>, active: ResizableGesture? = None) {
194 val props = input().props
195 val previous = props.sizes.value
196 if (sameResizableSizes(previous, next))
197 return
198 props.sizes.value = next
199 val accepted = props.sizes.value
200 resizableValues(accepted)
201 when (active) {
202 case Some(val value) -> value.expected = accepted
203 case None -> {}
204 }
205 if (scope.isMounted && !sameResizableSizes(previous, accepted))
206 props.onSizesChange(accepted)
207 }
208 fun cancel(reason: DragCancelReason) {
209 val active = gesture ?: return
210 gesture = None
211 active.drag.cancel()
212 val props = input().props
213 if (compatible(active.geometry) && sameResizableSizes(props.sizes.value, active.expected))
214 apply(active.initial)
215 props.onSizesCancel(props.sizes.value, reason)
216 }
217 fun lostFocus(key: ViewKey) {
218 when (gesture) {
219 case Some(val active) -> if (active.geometry.input.panels[active.index].key == key)
220 cancel(DragCancelReason.Explicit)
221 case None -> {}
222 }
223 }
224 fun key(index: Long, context: RoutedEventContext, event: KeyEvent) {
225 if (!available(index) || !sliderKeyAllowed(event))
226 return
227 if (event.logicalKey == LogicalKey.Named(NamedKey.Escape)) {
228 when (gesture) {
229 case Some(_) -> {
230 context.consume();
231 cancel(DragCancelReason.Explicit)
232 }
233 case None -> {}
234 }
235 return
236 }
237 val props = input().props
238 val command =
239 resizableKey(props.orientation, scope.layoutDirection, event.logicalKey) ?: return
240 context.consume()
241 adjust(index, command)
242 }
243 fun range(index: Long): SemanticValueRange? {
244 if (semanticRevision.value == 0UL || !available(index))
245 return None
246 val props = input().props
247 val shape = geometry ?: return None
248 val sizes =
249 resizableAllocation(props.panels, resizableValues(props.sizes.value), shape.target)
250 val total = sizes[index] + sizes[index + 1L]
251 val lower =
252 resizablePair(
253 sizes[index],
254 sizes[index + 1L],
255 -total,
256 props.panels[index],
257 props.panels[index + 1L]
258 )
259 val upper =
260 resizablePair(
261 sizes[index],
262 sizes[index + 1L],
263 total,
264 props.panels[index],
265 props.panels[index + 1L]
266 )
267 return Some(
268 SemanticValueRange(
269 lower.first,
270 upper.first,
271 sizes[index],
272 upperBound(total, props.keyboardStep)
273 )
274 )
275 }
276 fun setNumber(index: Long, value: Double): SemanticActionResult {
277 val current = range(index) ?: return SemanticActionResult.Unavailable
278 if (!value.isFinite() || value < current.minimum || value > current.maximum)
279 return SemanticActionResult.InvalidArgument
280 if (current.step <= 0.0)
281 return SemanticActionResult.Completed
282 return adjust(index, ResizableKey.Step((value - current.value) / current.step))
283 }
284 fun adjust(index: Long, command: ResizableKey): SemanticActionResult {
285 if (!available(index))
286 return SemanticActionResult.Rejected
287 cancel(DragCancelReason.Explicit)
288 if (!scope.isMounted)
289 return SemanticActionResult.Unavailable
290 val props = input().props
291 val shape = geometry ?: return SemanticActionResult.Unavailable
292 val before = props.sizes.value
293 val next = resizableKeyboard(props, before, shape.target, index, command)
294 apply(next)
295 if (scope.isMounted && !sameResizableSizes(before, props.sizes.value))
296 props.onSizesCommit(props.sizes.value)
297 return if (sameResizableSizes(next, props.sizes.value))
298 SemanticActionResult.Completed
299 else
300 SemanticActionResult.Rejected
301 }
302 fun retire() {
303 cancel(DragCancelReason.Closed);
304 geometry = None
305 }