MenuPopup now supports PageUp/Down and Home/End

This commit is contained in:
Alexandre Bury 2016-07-02 20:53:13 -07:00
parent 692c310656
commit d89c83f0ed
2 changed files with 40 additions and 25 deletions

View File

@ -15,7 +15,7 @@ fn main() {
MenuTree::new() MenuTree::new()
.leaf("New", |s| s.add_layer(Dialog::info("New file!"))) .leaf("New", |s| s.add_layer(Dialog::info("New file!")))
.subtree("Recent", MenuTree::new().with(|tree| { .subtree("Recent", MenuTree::new().with(|tree| {
for i in 1..6 { for i in 1..100 {
tree.add_leaf(&format!("Item {}", i), |_| ()) tree.add_leaf(&format!("Item {}", i), |_| ())
} }
})) }))

View File

@ -43,6 +43,37 @@ impl MenuPopup {
} }
} }
fn scroll_up(&mut self, mut n: usize, cycle: bool) {
while n > 0 {
if self.focus > 0 {
self.focus -= 1;
} else if cycle {
self.focus = self.menu.children.len() - 1;
} else {
break;
}
if !self.menu.children[self.focus].is_delimiter() {
n -= 1;
}
}
}
fn scroll_down(&mut self, mut n: usize, cycle: bool) {
while n > 0 {
if self.focus + 1 < self.menu.children.len() {
self.focus += 1;
} else if cycle {
self.focus = 0;
} else {
break;
}
if !self.menu.children[self.focus].is_delimiter() {
n -= 1;
}
}
}
/// Sets the alignment for this view. /// Sets the alignment for this view.
pub fn align(mut self, align: Align) -> Self { pub fn align(mut self, align: Align) -> Self {
@ -163,30 +194,14 @@ impl View for MenuPopup {
s.pop_layer(); s.pop_layer();
}); });
} }
Event::Key(Key::Up) => { Event::Key(Key::Up) => self.scroll_up(1, true),
loop { Event::Key(Key::PageUp) => self.scroll_up(5, false),
if self.focus > 0 { Event::Key(Key::Down) => self.scroll_down(1, true),
self.focus -= 1; Event::Key(Key::PageDown) => self.scroll_down(5, false),
} else {
self.focus = self.menu.children.len() - 1; Event::Key(Key::Home) => self.focus = 0,
} Event::Key(Key::End) => self.focus = self.menu.children.len() - 1,
if !self.menu.children[self.focus].is_delimiter() {
break;
}
}
}
Event::Key(Key::Down) => {
loop {
if self.focus + 1 < self.menu.children.len() {
self.focus += 1;
} else {
self.focus = 0;
}
if !self.menu.children[self.focus].is_delimiter() {
break;
}
}
}
Event::Key(Key::Right) if self.menu.children[self.focus] Event::Key(Key::Right) if self.menu.children[self.focus]
.is_subtree() => { .is_subtree() => {
return match self.menu.children[self.focus] { return match self.menu.children[self.focus] {