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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
use crate::id::Id;
use crate::path::SearchPath;
use crate::{Absolute, EventResult, Mux, Orientation, Vec2};
impl Mux {
// Handler for mouse events
pub(crate) fn clicked_pane(&self, mp: Vec2) -> Option<Id> {
if self.zoomed {
return None;
}
for node in self.root.descendants(&self.tree) {
if self.tree.get(node).unwrap().get().click(mp) {
return Some(node);
}
}
None
}
pub(crate) fn zoom_focus(&mut self) -> EventResult {
self.zoomed = !self.zoomed;
self.invalidated = true;
EventResult::Consumed(None)
}
pub(crate) fn move_focus(&mut self, direction: Absolute) -> EventResult {
if self.zoomed {
return EventResult::Ignored;
}
let prev_move = self.focus;
match self.move_focus_relative(direction, self.focus, self.focus) {
EventResult::Consumed(any) => {
self.history.push_back((prev_move, self.focus, direction));
if self.history.len() > self.history_length {
self.history.pop_front();
}
EventResult::Consumed(any)
}
EventResult::Ignored => EventResult::Ignored,
}
}
fn move_focus_relative(&mut self, direction: Absolute, node: Id, origin: Id) -> EventResult {
match self.search_focus_path(direction, node.ancestors(&self.tree).nth(1).unwrap(), node) {
Ok((path, turn_point)) => {
// Traverse the path down again
if let Some(focus) = self.traverse_search_path(path, turn_point, direction, origin)
{
if let Ok(result) = self.tree.get_mut(focus).unwrap().get_mut().take_focus() {
self.focus = focus;
EventResult::Consumed(None).and(result)
} else {
// rejected
self.move_focus_relative(direction, focus, origin)
}
} else {
EventResult::Ignored
}
}
Err(_) => EventResult::Ignored,
}
}
fn traverse_single_node(&self, action: SearchPath, turn_point: Id, cur_node: Id) -> Option<Id> {
let left = || -> Option<Id> { cur_node.children(&self.tree).next() };
let right = || -> Option<Id> { cur_node.children(&self.tree).last() };
let up = left;
let down = right;
match self.tree.get(turn_point).unwrap().get().orientation {
Orientation::Horizontal => {
match action {
// Switching Sides for Left & Right
SearchPath::Right
if self.tree.get(cur_node).unwrap().get().orientation
== Orientation::Horizontal =>
{
left()
}
SearchPath::Left
if self.tree.get(cur_node).unwrap().get().orientation
== Orientation::Horizontal =>
{
right()
}
// Remain for Up & Down
SearchPath::Up
if self.tree.get(cur_node).unwrap().get().orientation
== Orientation::Vertical =>
{
up()
}
SearchPath::Down
if self.tree.get(cur_node).unwrap().get().orientation
== Orientation::Vertical =>
{
down()
}
_ => None,
}
}
Orientation::Vertical => {
match action {
// Remain for Left & Right
SearchPath::Right
if self.tree.get(cur_node).unwrap().get().orientation
== Orientation::Horizontal =>
{
right()
}
SearchPath::Left
if self.tree.get(cur_node).unwrap().get().orientation
== Orientation::Horizontal =>
{
left()
}
// Switch for Up & Down
SearchPath::Up
if self.tree.get(cur_node).unwrap().get().orientation
== Orientation::Vertical =>
{
down()
}
SearchPath::Down
if self.tree.get(cur_node).unwrap().get().orientation
== Orientation::Vertical =>
{
up()
}
_ => None,
}
}
}
}
fn traverse_search_path(
&self,
mut path: Vec<SearchPath>,
turn_point: Id,
direction: Absolute,
origin: Id,
) -> Option<Id> {
let mut cur_node = turn_point;
while let Some(step) = path.pop() {
match self.traverse_single_node(step, turn_point, cur_node) {
Some(node) => {
cur_node = node;
}
None => {
// Truncate remaining path
// cur_node = cur_node.children(&self.tree).next().unwrap();
break;
}
}
}
let check = |comp: Absolute, cur_node: &mut Id| -> Result<(), ()> {
if direction == comp {
match cur_node.children(&self.tree).last() {
Some(node) => {
*cur_node = node;
Ok(())
}
None => Err(()),
}
} else {
match cur_node.children(&self.tree).next() {
Some(node) => {
*cur_node = node;
Ok(())
}
None => Err(()),
}
}
};
// Check if values exist in the history that specify this path
let goal_opt = {
let history = self.history.iter().rev();
for entry in history {
match entry {
(goal, past_origin, past_direction)
if *past_direction == direction.invert() && origin == *past_origin =>
{
return Some(*goal);
}
_ => {}
}
}
None
};
if let Some(goal) = goal_opt {
return Some(goal);
}
// Have to find nearest child here in case path is too short
while !self.tree.get(cur_node).unwrap().get().has_view() {
match self.tree.get(cur_node).unwrap().get().orientation {
Orientation::Horizontal
if direction == Absolute::Left || direction == Absolute::Right =>
{
if check(Absolute::Left, &mut cur_node).is_err() {
return None;
}
}
Orientation::Vertical
if direction == Absolute::Up || direction == Absolute::Down =>
{
if check(Absolute::Up, &mut cur_node).is_err() {
return None;
}
}
_ => match cur_node.children(&self.tree).next() {
Some(node) => cur_node = node,
None => return None,
},
}
}
Some(cur_node)
}
fn search_focus_path(
&self,
direction: Absolute,
nodeid: Id,
fromid: Id,
) -> Result<(Vec<SearchPath>, Id), ()> {
let mut cur_node = Some(nodeid);
let mut from_node = fromid;
let mut path = Vec::new();
while cur_node.is_some() {
// println!("Current node in search path: {}", cur_node.unwrap());
// println!("Originating from node: {}", from_node);
match self.tree.get(cur_node.unwrap()).unwrap().get().orientation {
Orientation::Horizontal
if direction == Absolute::Left || direction == Absolute::Right =>
{
if cur_node.unwrap().children(&self.tree).next().unwrap() == from_node {
// Originated from left
path.push(SearchPath::Left);
from_node = cur_node.unwrap();
if direction == Absolute::Left {
cur_node = cur_node.unwrap().ancestors(&self.tree).nth(1);
if cur_node.is_none() {
return Err(());
}
} else {
cur_node = None;
}
} else {
// Originated from right
path.push(SearchPath::Right);
from_node = cur_node.unwrap();
if direction == Absolute::Right {
cur_node = cur_node.unwrap().ancestors(&self.tree).nth(1);
if cur_node.is_none() {
return Err(());
}
} else {
cur_node = None;
}
}
}
Orientation::Vertical
if direction == Absolute::Up || direction == Absolute::Down =>
{
if cur_node.unwrap().children(&self.tree).next().unwrap() == from_node {
// Originated from up
path.push(SearchPath::Up);
from_node = cur_node.unwrap();
if direction == Absolute::Up {
cur_node = cur_node.unwrap().ancestors(&self.tree).nth(1);
if cur_node.is_none() {
return Err(());
}
} else {
cur_node = None;
}
} else {
// Originated from down
path.push(SearchPath::Down);
from_node = cur_node.unwrap();
if direction == Absolute::Down {
cur_node = cur_node.unwrap().ancestors(&self.tree).nth(1);
if cur_node.is_none() {
return Err(());
}
} else {
cur_node = None;
}
}
}
Orientation::Horizontal => {
if cur_node.unwrap().children(&self.tree).next().unwrap() == from_node {
path.push(SearchPath::Left);
from_node = cur_node.unwrap();
cur_node = cur_node.unwrap().ancestors(&self.tree).nth(1);
} else {
path.push(SearchPath::Right);
from_node = cur_node.unwrap();
cur_node = cur_node.unwrap().ancestors(&self.tree).nth(1);
}
}
Orientation::Vertical => {
if cur_node.unwrap().children(&self.tree).next().unwrap() == from_node {
path.push(SearchPath::Up);
from_node = cur_node.unwrap();
cur_node = cur_node.unwrap().ancestors(&self.tree).nth(1);
} else {
path.push(SearchPath::Down);
from_node = cur_node.unwrap();
cur_node = cur_node.unwrap().ancestors(&self.tree).nth(1);
}
}
}
}
match self.tree.get(from_node).unwrap().get().orientation {
Orientation::Horizontal if direction == Absolute::Up || direction == Absolute::Down => {
Err(())
}
Orientation::Vertical
if direction == Absolute::Left || direction == Absolute::Right =>
{
Err(())
}
_ => Ok((path, from_node)),
}
}
pub(crate) fn resize(&mut self, direction: Absolute) -> EventResult {
// TODO: Do not let children be resized to a lower amount then they said they could be
if self.zoomed {
return EventResult::Ignored;
}
let mut parent = self.focus.ancestors(&self.tree).nth(1);
while parent.is_some() {
if let Some(view) = self.tree.get_mut(parent.unwrap()) {
if view.get().orientation == direction.into() {
match view.get_mut().move_offset(direction) {
Ok(()) => {
self.invalidated = true;
return EventResult::Consumed(None);
}
Err(_) => break,
}
} else {
parent = parent.unwrap().ancestors(&self.tree).nth(1);
}
}
}
EventResult::Ignored
}
}
impl std::convert::From<Absolute> for Orientation {
fn from(direction: Absolute) -> Orientation {
match direction {
Absolute::Up | Absolute::Down => Orientation::Vertical,
Absolute::Left | Absolute::Right => Orientation::Horizontal,
// If no direction default to Horizontal
Absolute::None => Orientation::Horizontal,
}
}
}
trait Invertable {
fn invert(&self) -> Self;
}
impl Invertable for Absolute {
fn invert(&self) -> Absolute {
match self {
Absolute::Right => Absolute::Left,
Absolute::Left => Absolute::Right,
Absolute::Up => Absolute::Down,
Absolute::Down => Absolute::Up,
Absolute::None => Absolute::None,
}
}
}