Focus follow mouse failed if closed TreeItems above are present
Focus follow mouse now handles correctly items which have siblings with hidden children above them.
@@ -16,6 +16,22 @@ enum State {
Undefined,
}
+fn verify_open_till_root(opt: &Option<fltk::tree::TreeItem>) -> bool {
+ let mut par = opt.clone();
+ loop {
+ match par.as_ref().unwrap().parent() {
+ Some(p) => {
+ if p.is_close() {
+ return false;
+ } else {
+ par = Some(p.clone());
+ }
+ }
+ None => return true,
+ }
+ }
+}
+
struct TreeMouseFocus {
t_widget: fltk::tree::Tree,
previous_focus: Option<TreeItem>,
@@ -34,6 +50,32 @@ impl TreeMouseFocus {
match &pf {
Some(item) => {
let item_y = item.y();
+ match state {
+ State::MovingUp => {
+ if verify_open_till_root(&pf) == true {
+ if mouse_y < item_y {
+ pf = pf.as_ref().unwrap().prev();
+ continue;
+ };
+ break;
+ } else {
+ pf = pf.as_ref().unwrap().prev();
+ continue;
+ }
+ }
+ State::MovingDown => {
+ if verify_open_till_root(&pf) == true {
+ if mouse_y > item_y + item.h() {
+ pf = pf.as_ref().unwrap().next();
+ continue;
+ };
+ break;
+ } else {
+ pf = pf.as_ref().unwrap().next();
+ continue;
+ }
+ }
+ State::Undefined => {
if mouse_y < item_y {
pf = pf.as_ref().unwrap().prev();
state = State::MovingUp;
@@ -44,12 +86,11 @@ impl TreeMouseFocus {
state = State::MovingDown;
continue;
};
- if state == State::Undefined {
return true; // If in same range, don't update 'previous_focus'
- };
- break;
}
- // En up here if y is outside tree boundaries, or no tree item is present
+ }
+ }
+ // End up here if y is outside tree boundaries, or no tree item is present
None => match &state {
State::MovingUp | State::MovingDown => return true,
State::Undefined => {
@@ -62,24 +103,7 @@ impl TreeMouseFocus {
},
};
}
- let mut open = true;
- let mut pa = pf.clone();
- loop {
- match &pa.as_ref().unwrap().parent() {
- Some(p) => {
- if p.is_close() {
- open = false;
- break;
- } else {
- pa = Some(p.clone());
- }
- }
- None => {
- break;
- }
- }
- }
- if open == true {
+ if verify_open_till_root(&pf) == true {
t.take_focus();
t.set_item_focus(&pf.as_ref().unwrap());
println!("Set focus to item: {:?}", pf.as_ref().unwrap().label());
