scoop:scoop.ui.component:0.1.0
src/resizable/control.scoop
Selected production source · src/resizable/control.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.*5
import scoop.ui.*6
import scoop.ui.style.*7
import scoop.window.*8
import scoop.window.accessibility.*10
internal class ResizableGesture(11
val index: Long,12
val initial: Array<ResizableSize>,13
val lengths: Array<Double>,14
val geometry: ResizableGeometry,15
val drag: Drag16
) {17
var expected = initial18
}20
internal class ResizableControl(21
private val scope: ComponentScope,22
private val input: () -> ResizableInput23
) {24
private var geometry: ResizableGeometry? = None25
private val semanticRevision = state(0UL)26
private var gesture: ResizableGesture? = None27
private var configured = input().props28
private var direction = scope.layoutDirection29
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.layoutDirection37
else38
direction39
)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).enabled46
fun update() {47
val props = input().props48
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
else58
if (!enabled(active.index))59
cancel(DragCancelReason.Disabled)60
}61
case None -> {}62
}63
if (!scope.isMounted)64
return65
if (66
!sameResizablePanels(configured.panels, props.panels) ||67
configured.orientation !=68
props.orientation ||69
direction !=70
scope.layoutDirection71
)72
geometry = None73
configured = props74
direction = scope.layoutDirection75
}76
fun accept(shape: ResizableGeometry) {77
if (!scope.isMounted || !compatible(shape))78
return79
val previous = geometry80
geometry = Some(shape)81
if (previous?.target != Some(shape.target))82
semanticRevision.value += 1UL83
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.091
)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.0104
case None -> false105
}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
return112
}113
cancel(DragCancelReason.Explicit)114
if (!scope.isMounted)115
return116
if (!focus.handle.requestFocus(FocusOrigin.Pointer)) {117
drag.cancel();118
return119
}120
if (!scope.isMounted)121
return122
val shape = geometry!!123
val initial = input().props.sizes.value124
val lengths =125
resizableAllocation(126
input().props.panels,127
resizableValues(initial),128
shape.target129
)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 ?: return137
gesture = None138
val props = input().props139
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 ?: return147
val props = input().props148
if (149
!compatible(active.geometry) || !sameResizableSizes(active.expected, props.sizes.value)150
) {151
cancel(DragCancelReason.ConfigurationChanged);152
return153
}154
val index = active.index155
val delta =156
if (157
props.orientation ==158
Orientation.Horizontal &&159
scope.layoutDirection ==160
LayoutDirection.Rtl161
)162
-displacement163
else164
displacement165
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.first177
else178
if (i == index + 1L)179
pair.second180
else181
active.lengths[i]182
}183
val before =184
resizableAllocation(185
props.panels,186
resizableValues(props.sizes.value),187
active.geometry.target188
)189
if (sameSliderValues(before, next))190
return191
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().props195
val previous = props.sizes.value196
if (sameResizableSizes(previous, next))197
return198
props.sizes.value = next199
val accepted = props.sizes.value200
resizableValues(accepted)201
when (active) {202
case Some(val value) -> value.expected = accepted203
case None -> {}204
}205
if (scope.isMounted && !sameResizableSizes(previous, accepted))206
props.onSizesChange(accepted)207
}208
fun cancel(reason: DragCancelReason) {209
val active = gesture ?: return210
gesture = None211
active.drag.cancel()212
val props = input().props213
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
return227
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
return236
}237
val props = input().props238
val command =239
resizableKey(props.orientation, scope.layoutDirection, event.logicalKey) ?: return240
context.consume()241
adjust(index, command)242
}243
fun range(index: Long): SemanticValueRange? {244
if (semanticRevision.value == 0UL || !available(index))245
return None246
val props = input().props247
val shape = geometry ?: return None248
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.Unavailable278
if (!value.isFinite() || value < current.minimum || value > current.maximum)279
return SemanticActionResult.InvalidArgument280
if (current.step <= 0.0)281
return SemanticActionResult.Completed282
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.Rejected287
cancel(DragCancelReason.Explicit)288
if (!scope.isMounted)289
return SemanticActionResult.Unavailable290
val props = input().props291
val shape = geometry ?: return SemanticActionResult.Unavailable292
val before = props.sizes.value293
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.Completed299
else300
SemanticActionResult.Rejected301
}302
fun retire() {303
cancel(DragCancelReason.Closed);304
geometry = None305
}306
}