1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use std::fmt::Debug;
use kas::event::PressSource;
use kas::prelude::*;
#[handler(handle=noauto)]
#[derive(Clone, Debug, Default, Widget)]
pub struct DragHandle {
#[widget_core]
core: CoreData,
track: Rect,
press_source: Option<event::PressSource>,
press_offset: Coord,
}
impl DragHandle {
pub fn new() -> Self {
DragHandle {
core: Default::default(),
track: Default::default(),
press_source: None,
press_offset: Coord::ZERO,
}
}
pub fn set_size_and_offset(&mut self, size: Size, offset: Coord) -> TkAction {
self.core.rect.size = size;
self.set_offset(offset).1
}
#[inline]
pub fn track(&self) -> Rect {
self.track
}
#[inline]
pub fn offset(&self) -> Coord {
self.core.rect.pos - self.track.pos
}
#[inline]
pub fn max_offset(&self) -> Coord {
Coord::from(self.track.size) - Coord::from(self.core.rect.size)
}
pub fn set_offset(&mut self, offset: Coord) -> (Coord, TkAction) {
let offset = offset.clamp(Coord::ZERO, self.max_offset());
let handle_pos = self.track.pos + offset;
if handle_pos != self.core.rect.pos {
self.core.rect.pos = handle_pos;
(offset, TkAction::Redraw)
} else {
(offset, TkAction::None)
}
}
pub fn handle_press_on_track(
&mut self,
mgr: &mut Manager,
source: PressSource,
coord: Coord,
) -> Coord {
if !self.grab_press(mgr, source, coord) {
return self.offset();
}
self.press_offset = Coord::from(self.core.rect.size / 2) + self.track.pos;
let (offset, action) = self.set_offset(coord - self.press_offset);
debug_assert!(action == TkAction::Redraw);
mgr.send_action(action);
offset
}
fn grab_press(&mut self, mgr: &mut Manager, source: PressSource, coord: Coord) -> bool {
let cur = Some(event::CursorIcon::Grabbing);
if mgr.request_grab(self.id(), source, coord, event::GrabMode::Grab, cur) {
self.press_source = Some(source);
true
} else {
false
}
}
}
impl Layout for DragHandle {
fn size_rules(&mut self, _: &mut dyn SizeHandle, _: AxisInfo) -> SizeRules {
SizeRules::EMPTY
}
fn set_rect(&mut self, rect: Rect, _: AlignHints) {
self.track = rect;
}
fn draw(&self, _: &mut dyn DrawHandle, _: &event::ManagerState, _: bool) {}
}
impl event::Handler for DragHandle {
type Msg = Coord;
fn handle(&mut self, mgr: &mut Manager, event: Event) -> Response<Self::Msg> {
match event {
Event::PressStart { source, coord, .. } => {
if !self.grab_press(mgr, source, coord) {
return Response::None;
}
self.press_offset = coord - self.offset();
Response::None
}
Event::PressMove { source, coord, .. } if Some(source) == self.press_source => {
let offset = coord - self.press_offset;
let (offset, action) = self.set_offset(offset);
if action == TkAction::None {
Response::None
} else {
mgr.send_action(action);
Response::Msg(offset)
}
}
Event::PressEnd { source, .. } if Some(source) == self.press_source => {
self.press_source = None;
Response::None
}
event => Response::Unhandled(event),
}
}
}