scoop:scoop.ui.component:0.1.0
src/drawer/events.scoop
Selected production source · src/drawer/events.scoop
1
package scoop.ui.component3
import scoop.math.*4
import scoop.surface.*5
import scoop.ui.*6
import scoop.window.*8
internal fun drawerSwipe(control: DrawerControl, sample: SwipeGestureSample) {9
control.cancel(DragCancelReason.Explicit)10
if (!control.available() || !control.read().props.nativeSwipe)11
return12
val shape = control.geometry!!13
val positive =14
sample.direction == SwipeDirection.Right || sample.direction == SwipeDirection.Down15
val expand = positive != (shape.outward > 0.0)16
when (drawerAdjacent(shape.snaps, control.current(), expand)) {17
case Some(val index) -> control.select(index, true)18
case None -> if (!expand && control.read().props.swipeToClose)19
control.close()20
}21
}23
internal fun drawerDrag(control: DrawerControl, event: DragEvent) {24
when (event) {25
case Started(val sample) -> {26
if (!control.available()) {27
control.cancel(DragCancelReason.GeometryUnavailable);28
return29
}30
control.motion.cancel()31
control.gesture = Some(DrawerGesture(control.position, control.geometry!!))32
control.focus.handle.requestFocus(FocusOrigin.Pointer)33
if (!control.active)34
return35
drawerDragged(control, sample);36
control.publish()37
}38
case Moved(val sample) -> {39
drawerDragged(control, sample);40
control.publish()41
}42
case Ended(val sample) -> {43
val gesture = control.gesture ?: return44
drawerDragged(control, sample)45
if (!control.active)46
return47
control.gesture = None48
val input = control.read()49
val target =50
drawerReleased(51
gesture.geometry,52
control.position,53
-sample.velocity * gesture.geometry.outward,54
input.props.projectionSeconds,55
input.props.swipeToClose56
)57
when (target) {58
case Some(val value) -> if (value.index < 0L)59
control.close()60
else61
control.select(value.index, true)62
case None -> {}63
}64
control.publish()65
}66
case Cancelled(_, val reason) -> control.cancel(reason)67
}68
}70
private fun drawerDragged(control: DrawerControl, sample: DragSample) {71
val gesture = control.gesture ?: return72
val extent = gesture.geometry.extent73
var next = gesture.origin - sample.displacement * gesture.geometry.outward74
if (!next.isFinite())75
return76
next = max(0.0, next)77
if (next > extent)78
next = extent + min(extent / 3.0, (next - extent) / 3.0)79
if (control.motion.value != next)80
control.motion.snapTo(next)81
}83
internal fun drawerKeyboard(84
control: DrawerControl,85
context: RoutedEventContext,86
event: KeyEvent,87
handle: Boolean88
) {89
if (!control.active || !sliderKeyAllowed(event))90
return91
if (event.logicalKey == LogicalKey.Named(NamedKey.Escape)) {92
when (control.gesture) {93
case Some(_) -> {94
context.consume();95
control.cancel(DragCancelReason.Explicit)96
}97
case None -> {}98
}99
return100
}101
if (!handle || !control.available())102
return103
val shape = control.geometry!!104
val key = event.logicalKey105
val expand: Boolean? =106
when (key) {107
case Named(NamedKey.ArrowUp) -> if (shape.vertical)108
Some(shape.outward > 0.0)109
else110
None111
case Named(NamedKey.ArrowDown) -> if (shape.vertical)112
Some(shape.outward < 0.0)113
else114
None115
case Named(NamedKey.ArrowLeft) -> if (!shape.vertical)116
Some(shape.outward > 0.0)117
else118
None119
case Named(NamedKey.ArrowRight) -> if (!shape.vertical)120
Some(shape.outward < 0.0)121
else122
None123
else -> None124
}125
val endpoint = key == LogicalKey.Named(NamedKey.Home) || key == LogicalKey.Named(NamedKey.End)126
if (expand == None && !endpoint)127
return128
context.consume()129
var selected = control.current()130
if (endpoint) {131
for (index in 0L ..< shape.snaps.size) {132
if (133
if (key == LogicalKey.Named(NamedKey.End))134
shape.snaps[index] > shape.snaps[selected]135
else136
shape.snaps[index] < shape.snaps[selected]137
)138
selected = index139
}140
} else141
selected = drawerAdjacent(shape.snaps, selected, expand!!) ?: selected142
control.select(selected, true)143
}