Skip to content

Commit

Permalink
Return early in MenuPopup::on_event if self.menu.children is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
gyscos committed Jun 20, 2024
1 parent 9333a54 commit cb42dc1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
18 changes: 9 additions & 9 deletions cursive-core/src/theme/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,17 +394,17 @@ impl AsRef<Style> for Style {
}
}

fn combine_styles<S: AsRef<Style>>(styles: impl IntoIterator<Item=S>) -> Style {
let mut color = ColorStyle::inherit_parent();
let mut effects = Effects::empty();
fn combine_styles<S: AsRef<Style>>(styles: impl IntoIterator<Item = S>) -> Style {
let mut color = ColorStyle::inherit_parent();
let mut effects = Effects::empty();

for style in styles {
let style = style.as_ref();
color = ColorStyle::merge(color, style.color);
effects = Effects::merge(effects, style.effects);
}
for style in styles {
let style = style.as_ref();
color = ColorStyle::merge(color, style.color);
effects = Effects::merge(effects, style.effects);
}

Style { effects, color }
Style { effects, color }
}

/// Creates a new `Style` by merging all given styles.
Expand Down
3 changes: 2 additions & 1 deletion cursive-core/src/utils/markup/cursup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ mod tests {
Span {
content: "bar",
width: 3,
attr: &Style::from_color_style(BaseColor::Red.dark().into()).combine(Effect::Bold)
attr: &Style::from_color_style(BaseColor::Red.dark().into())
.combine(Effect::Bold)
},
Span {
content: " baz",
Expand Down
24 changes: 23 additions & 1 deletion cursive-core/src/views/menu_popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ impl_scroller!(MenuPopup::scroll_core);

impl MenuPopup {
/// Creates a new `MenuPopup` using the given menu tree.
///
/// The menu tree cannot be modified after this view has been created.
pub fn new(menu: Arc<menu::Tree>) -> Self {
MenuPopup {
menu,
Expand Down Expand Up @@ -123,6 +125,11 @@ impl MenuPopup {
self.on_action = Some(Callback::from_fn(f));
}

// Scroll up by `n` rows.
//
// # Panics
//
// If `self.menu.children.is_empty()`.
fn scroll_up(&mut self, mut n: usize, mut cycle: bool) {
while n > 0 {
if self.focus > 0 {
Expand All @@ -141,6 +148,11 @@ impl MenuPopup {
}
}

// Scroll down by `n` rows.
//
// # Panics
//
// If `self.menu.children.is_empty()`.
fn scroll_down(&mut self, mut n: usize, mut cycle: bool) {
while n > 0 {
if self.focus + 1 < self.menu.children.len() {
Expand All @@ -160,6 +172,11 @@ impl MenuPopup {
}
}

// Prepare the callback for when an item has been picked.
//
// # Panics
//
// If `self.menu.children.is_empty()`.
fn submit(&mut self) -> EventResult {
match self.menu.children[self.focus] {
menu::Item::Leaf { ref cb, .. } => {
Expand Down Expand Up @@ -227,6 +244,11 @@ impl MenuPopup {
///
/// Here the event has already been relativized. This means `y=0` points to the first item.
fn inner_on_event(&mut self, event: Event) -> EventResult {
// If there is no item, nothing can be done.
if self.menu.children.is_empty() {
return EventResult::Ignored;
}

match event {
Event::Key(Key::Up) => self.scroll_up(1, true),
Event::Key(Key::PageUp) => self.scroll_up(5, false),
Expand Down Expand Up @@ -320,7 +342,7 @@ impl View for MenuPopup {
scroll::draw_box_frame(
self,
printer,
|s, y| s.menu.children[y].is_delimiter(),
|s, y| s.menu.children.get(y).map_or(false, |c| c.is_delimiter()),
|_s, _x| false,
);

Expand Down

0 comments on commit cb42dc1

Please sign in to comment.