From c08ed739ab0322ae51174add0deac7a90486a47c Mon Sep 17 00:00:00 2001 From: Andrew Mackenzie Date: Tue, 31 Dec 2024 17:30:10 +0100 Subject: [PATCH 1/2] Avoid index out of range panic --- src/widget/menu/menu_bar_overlay.rs | 46 +++++++++++++++-------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/src/widget/menu/menu_bar_overlay.rs b/src/widget/menu/menu_bar_overlay.rs index 40dcae3..dbadd52 100644 --- a/src/widget/menu/menu_bar_overlay.rs +++ b/src/widget/menu/menu_bar_overlay.rs @@ -105,29 +105,31 @@ where let menu_state = menu_tree.state.downcast_ref::(); if let Some(active) = menu_state.active { - let next_item = &menu.items[active]; - let next_tree = &mut menu_tree.children[active]; - let next_parent_bounds = { - let slice_node = &menu_nodes.last().unwrap().children()[0]; - let Some(node) = slice_node - .children() - .get(active - menu_state.slice.start_index) - else { - return; + if active < menu.items.len() { + let next_item = &menu.items[active]; + let next_tree = &mut menu_tree.children[active]; + let next_parent_bounds = { + let slice_node = &menu_nodes.last().unwrap().children()[0]; + let Some(node) = slice_node + .children() + .get(active - menu_state.slice.start_index) + else { + return; + }; + + node.bounds() + (slice_node.bounds().position() - Point::ORIGIN) }; - - node.bounds() + (slice_node.bounds().position() - Point::ORIGIN) - }; - rec( - renderer, - next_item, - next_tree, - menu_nodes, - check_bounds_width, - next_parent_bounds, - direction, - viewport, - ); + rec( + renderer, + next_item, + next_tree, + menu_nodes, + check_bounds_width, + next_parent_bounds, + direction, + viewport, + ); + } } } } From 557f1d1315fcaa9cadc17ead569a2e90895c611e Mon Sep 17 00:00:00 2001 From: Andrew Mackenzie Date: Wed, 1 Jan 2025 16:50:04 +0100 Subject: [PATCH 2/2] Avoid index out of range panic --- src/widget/menu/menu_bar_overlay.rs | 58 +++++++++++++++-------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/src/widget/menu/menu_bar_overlay.rs b/src/widget/menu/menu_bar_overlay.rs index dbadd52..b7d05a1 100644 --- a/src/widget/menu/menu_bar_overlay.rs +++ b/src/widget/menu/menu_bar_overlay.rs @@ -248,35 +248,39 @@ where let menu_state = menu_tree.state.downcast_mut::(); let rec_event = if let Some(active) = menu_state.active { - let next_tree = &mut menu_tree.children[active]; - let next_item = &mut menu.items[active]; - let next_parent_bounds = { - let Some(layout) = slice_layout - .children() - .nth(active - menu_state.slice.start_index) - else { - prev_bounds_list.pop(); - return RecEvent::Event; - }; + if active < menu_tree.children.len() { + let next_tree = &mut menu_tree.children[active]; + let next_item = &mut menu.items[active]; + let next_parent_bounds = { + let Some(layout) = slice_layout + .children() + .nth(active - menu_state.slice.start_index) + else { + prev_bounds_list.pop(); + return RecEvent::Event; + }; - layout.bounds() - }; + layout.bounds() + }; - rec( - next_tree, - next_item, - event, - layout_iter, - cursor, - renderer, - clipboard, - shell, - next_parent_bounds, - viewport, - prev_bounds_list, - &mut menu_state.active, - scroll_speed, - ) + rec( + next_tree, + next_item, + event, + layout_iter, + cursor, + renderer, + clipboard, + shell, + next_parent_bounds, + viewport, + prev_bounds_list, + &mut menu_state.active, + scroll_speed, + ) + } else { + RecEvent::Close + } } else { RecEvent::Close };