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
#![allow(dead_code)]
use fltk::{
app,
button::Button,
enums::Event,
frame::Frame,
prelude::*,
tree::{Tree, TreeItem, TreeReason, TreeSelect},
window::Window,
};
use std::cell::RefCell;
use std::env;
use std::rc::Rc;
#[derive(PartialEq)]
enum State {
MovingUp,
MovingDown,
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: Rc<RefCell<Option<TreeItem>>>,
}
impl TreeMouseFocus {
fn new(x: i32, y: i32, width: i32, height: i32, title: &'static str) -> Self {
let mut t_widget = Tree::new(x, y, width, height, title);
let previous_focus = Rc::new(RefCell::new(None::<TreeItem>));
let pfr = Rc::clone(&previous_focus);
t_widget.set_callback_reason(TreeReason::Selected);
t_widget.set_callback(|_t| println!("clicked an item"));
t_widget.handle(move |t, e| match e {
Event::Move => {
let (_, mouse_y) = app::event_coords();
let mut state = State::Undefined;
let mut pf = pfr.borrow_mut();
loop {
match &*pf {
Some(item) => {
let item_y = item.y();
match state {
State::MovingUp => {
if verify_open_till_root(&pf) {
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) {
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;
continue;
};
if mouse_y > item_y + item.h() {
*pf = pf.as_ref().unwrap().next();
state = State::MovingDown;
continue;
};
return true; // If in same range, don't update 'previous_focus'
}
}
}
// 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 => {
*pf = t.first();
state = State::MovingDown;
if pf.is_none() {
return true;
}
continue;
}
},
};
}
if verify_open_till_root(&pf) {
t.take_focus().ok();
t.set_item_focus(pf.as_ref().unwrap());
println!("Set focus to item: {:?}", pf.as_ref().unwrap().label());
}
true
}
_ => false,
});
Self {
t_widget,
previous_focus,
}
}
fn add(&mut self, path: &str) -> Option<TreeItem> {
self.t_widget.add(path)
}
/// Caution, variable 'previous focus' must be set to None, as it
/// otherwise could try to refer to an already freed memory location,
/// when this TreeItem is removed.
fn remove(&mut self, item: &TreeItem) -> Result<(), FltkError> {
*self.previous_focus.borrow_mut() = None;
self.t_widget.remove(item)
}
fn get_items(&self) -> Option<Vec<TreeItem>> {
self.t_widget.get_items()
}
}
fn main() {
let path = env::current_dir().unwrap();
let path: String = path
.to_str()
.unwrap()
.chars()
.enumerate()
.map(|(_, c)| match c {
'\\' => '/', // change window paths to posix paths
_ => c,
})
.collect();
let app = app::App::default().with_scheme(app::Scheme::Gtk);
let mut wind = Window::default().with_size(400, 300);
let mut but = Button::new(260, 255, 80, 40, "Get Items");
let _frame = Frame::new(20, 255, 160, 40, "Focus follow mouse");
let mut tree = TreeMouseFocus::new(5, 10, 190, 240, "");
tree.add(&path);
let mut items = tree.get_items().unwrap();
items.as_mut_slice()[0].set_label("/");
let mut tree2 = Tree::new(205, 10, 190, 240, "");
tree2.set_select_mode(TreeSelect::Multi);
tree2.add("First");
tree2.add("First/1st");
tree2.add("First/2nd/3rd");
tree2.add("Second");
tree2.add("Third");
tree2.set_trigger(fltk::enums::CallbackTrigger::ReleaseAlways);
wind.make_resizable(true);
wind.show();
but.set_callback({
let tree2 = tree2.clone();
move |_| match tree2.get_selected_items() {
None => println!("No items selected"),
Some(vals) => print!(
"In total {} items selected:\n{}",
vals.len(),
vals.iter()
.map(|i| tree2.item_pathname(i).unwrap() + "\n")
.collect::<String>()
),
}
});
app.run().unwrap();
}