dymokomi/luce-ui
Source of release 0.19.5
0.19.5 / src / widgets / drag_gesture.lucb
## A press that may become a drag of a list's selection (`Layout.start_drag`): where it
## began, whether it is still held, and whether its click should still select the
## pressed row alone. A press on a row already selected among several keeps them all
## selected, so they can be dragged together, and selects that row alone only when the
## button comes up without a drag, as Finder does.
import memory
## How far, in points, the pointer moves from the press before the press is a drag.
let threshold: f64 = 4.0
pub struct DragGesture:
## The kind the selection is dragged as, a heap copy; "" when it is not dragged.
var kind: str
var held: bool
var x: f64
var y: f64
## The row to select alone on release (`collapse`), or -1.
var collapse: i64
pub func init():
self.kind = ""
self.held = false
self.x = 0.0
self.y = 0.0
self.collapse = -1
## Drag the selection as items of `kind`; "" stops it.
pub func set_kind(kind: str) -> !:
let copy = try copy_text(kind)
release_text(self.kind)
self.kind = copy
pub func enabled() -> bool:
return self.kind.length > 0
## The kind the selection is dragged as.
pub func dragged_kind() -> str:
return self.kind
## A primary press at (x, y). `collapse` is the row to select alone should the
## button come up without a drag, or -1.
pub func press(x: f64, y: f64, collapse: i64):
self.held = self.kind.length > 0
self.x = x
self.y = y
self.collapse = collapse
## Whether a move to (x, y) starts the drag: the press is held and has moved past
## the threshold. It is held no longer then.
pub func moved(x: f64, y: f64) -> bool:
if not self.held:
return false
let dx = x - self.x
let dy = y - self.y
if dx * dx + dy * dy < threshold * threshold:
return false
self.held = false
self.collapse = -1
return true
## The button came up: the row to select alone now, or -1.
pub func release() -> i64:
let row = self.collapse
self.held = false
self.collapse = -1
return row
## The press was taken away (a drag began, focus went): nothing more to do.
pub func cancel():
self.held = false
self.collapse = -1
pub func close():
release_text(self.kind)
self.kind = ""
func copy_text(text: str) -> str!:
if text.length == 0:
return ""
let copy = try new u8[text.length + 1] --- in memory.heap
for index in 0..<text.length:
copy[index] = text.bytes[index]
copy[text.length] = 0
return (str)copy[..<text.length]
func release_text(text: str):
if text.length > 0:
free(u8[]((u8*)text.bytes.data, text.length + 1)) in memory.heap