is_visible and scroll_to

pull/6/head
dvkt 5 years ago
parent a1cbff1614
commit c815c01df4

@ -83,7 +83,12 @@ impl Menu {
}
}
// is the given link visible on the screen right now?
/// Is the given link visible on screen?
fn is_visible(&self, link: usize) -> bool {
self.link_visibility(link) == Some(LinkPos::Visible)
}
/// Where is the given link relative to the screen?
fn link_visibility(&self, i: usize) -> Option<LinkPos> {
if let Some(&pos) = self.links.get(i) {
Some(if pos < self.scroll {
@ -301,12 +306,10 @@ impl Menu {
self.scroll -= 1;
}
// select it if it's visible now
if let Some(dir) = self.link_visibility(new_link) {
if dir == LinkPos::Visible {
if self.is_visible(new_link) {
self.link = new_link;
}
}
}
LinkPos::Below => {
// jump to link....
if let Some(&pos) = self.links.get(new_link) {
@ -403,12 +406,10 @@ impl Menu {
// scroll down by 1
self.scroll += 1;
// select it if it's visible now
if let Some(dir) = self.link_visibility(new_link) {
if dir == LinkPos::Visible {
if self.is_visible(new_link) {
self.link = new_link;
}
}
}
LinkPos::Visible => {
// select next link down
self.link = new_link;
@ -452,11 +453,9 @@ impl Menu {
self.action_open()
}
fn action_open(&mut self) -> Action {
// if the selected link isn't visible, jump to it:
if let Some(dir) = self.link_visibility(self.link) {
if dir != LinkPos::Visible {
if let Some(&pos) = self.links.get(self.link) {
fn scroll_to(&mut self, link: usize) -> Action {
if self.link_visibility(self.link) != Some(LinkPos::Visible) {
if let Some(&pos) = self.links.get(link) {
if pos > 5 {
self.scroll = pos - 5;
} else {
@ -465,6 +464,13 @@ impl Menu {
return Action::Redraw;
}
}
Action::None
}
fn action_open(&mut self) -> Action {
// if the selected link isn't visible, jump to it:
if !self.is_visible(self.link) {
return self.scroll_to(self.link);
}
self.searching = false;

Loading…
Cancel
Save