Skip to content

Commit

Permalink
improve closing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
latidoremi committed Feb 23, 2024
1 parent 6f7b7a6 commit dd632e8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 56 deletions.
8 changes: 8 additions & 0 deletions src/native/menu/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ pub(super) enum Axis {

pub(super) type Index = Option<usize>;

/// Should be returned from the recursive event processing function,
/// tells the caller which type of event has been processed
pub(super) enum RecEvent {
Event,
Close,
None,
}

pub fn pad_rectangle(rect: Rectangle, padding: Padding) -> Rectangle {
Rectangle {
x: rect.x - padding.left,
Expand Down
2 changes: 1 addition & 1 deletion src/native/menu/menu_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ where
self.check_bounds_width = check_bounds_width;
self
}

/// Sets the draw path option of the [`MenuBar`]
pub fn draw_path(mut self, draw_path: DrawPath) -> Self {
self.draw_path = draw_path;
Expand Down
86 changes: 32 additions & 54 deletions src/native/menu/menu_bar_overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ use iced::{
use super::{common::*, menu_bar::MenuBarState, menu_tree::*};
use crate::style::menu_bar::*;

/// Should be returned from the recursive event processing function,
/// tells the caller which type of event has been processed
enum RecEvent {
Event,
Close,
None,
}

pub(super) struct MenuBarOverlay<'a, 'b, Message, Theme, Renderer>
where
Theme: StyleSheet,
Expand Down Expand Up @@ -247,21 +239,22 @@ where

let menu_state = menu_tree.state.downcast_mut::<MenuState>();

if let Some(active) = menu_state.active {
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;
};

layout.bounds()
};

let re = rec(
rec(
next_tree,
next_item,
event,
Expand All @@ -274,55 +267,40 @@ where
viewport,
prev_bounds_list,
&mut menu_state.active,
);

prev_bounds_list.pop();

match re {
RecEvent::Event => RecEvent::Event,
RecEvent::Close => {
if cursor.is_over(prescroll) {
menu.on_event(menu_tree, event, menu_layout, cursor, renderer, clipboard, shell, viewport);
menu.open_event(menu_tree, menu_layout, cursor);
RecEvent::Event
} else if cursor.is_over(offset_bounds) {
RecEvent::Event
} else {
menu.close_event(menu_tree, menu_layout, cursor, parent_bounds, prev_bounds_list, prev);
if prev.is_some() {
RecEvent::None
} else {
RecEvent::Close
}
}
}
RecEvent::None => {
if cursor.is_over(prescroll) {
menu.on_event(menu_tree, event, menu_layout, cursor, renderer, clipboard, shell, viewport);
menu.open_event(menu_tree, menu_layout, cursor);
RecEvent::Event
} else if cursor.is_over(offset_bounds) {
RecEvent::Event
} else {
)
} else {
RecEvent::Close
};

prev_bounds_list.pop();

match rec_event {
RecEvent::Event => RecEvent::Event,
RecEvent::Close => {
if menu_state.pressed || cursor.is_over(prescroll){
menu.on_event(menu_tree, event, menu_layout, cursor, renderer, clipboard, shell, viewport);
menu.open_event(menu_tree, menu_layout, cursor);
RecEvent::Event
} else if cursor.is_over(offset_bounds) {
RecEvent::Event
} else {
menu.close_event(menu_tree, menu_layout, cursor, parent_bounds, prev_bounds_list, prev);
if prev.is_some() {
RecEvent::None
} else {
RecEvent::Close
}
}
}
} else {
prev_bounds_list.pop();

if cursor.is_over(prescroll) {
menu.on_event(menu_tree, event, menu_layout, cursor, renderer, clipboard, shell, viewport);
menu.open_event(menu_tree, menu_layout, cursor);
RecEvent::Event
} else if cursor.is_over(offset_bounds) {
RecEvent::Event
} else {
menu.close_event(menu_tree, menu_layout, cursor, parent_bounds, prev_bounds_list, prev);
if prev.is_some() {
RecEvent::None
RecEvent::None => {
if menu_state.pressed || cursor.is_over(prescroll){
menu.on_event(menu_tree, event, menu_layout, cursor, renderer, clipboard, shell, viewport);
menu.open_event(menu_tree, menu_layout, cursor);
RecEvent::Event
} else if cursor.is_over(offset_bounds) {
RecEvent::Event
} else {
RecEvent::Close
RecEvent::None
}
}
}
Expand Down
20 changes: 19 additions & 1 deletion src/native/menu/menu_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub(super) struct MenuState {
scroll_offset: f32,
pub(super) active: Index,
pub(super) slice: MenuSlice,
pub(super) pressed: bool,
}
impl Default for MenuState {
fn default() -> Self {
Expand All @@ -73,6 +74,7 @@ impl Default for MenuState {
lower_bound_rel: 0.0,
upper_bound_rel: f32::MAX,
},
pressed: false,
}
}
}
Expand Down Expand Up @@ -346,6 +348,16 @@ where
.fold(Ignored, event::Status::merge);

match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
if cursor.is_over(prescroll) {
menu_state.pressed = true;
}
Ignored
}
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => {
menu_state.pressed = false;
Ignored
}
Event::Mouse(mouse::Event::WheelScrolled { delta }) => {
if cursor.is_over(prescroll) {
process_scroll_event(menu_state, prescroll, *delta, viewport.size());
Expand Down Expand Up @@ -546,6 +558,12 @@ where
let offset_bounds = lc.next().unwrap().bounds();
let check_bounds = lc.next().unwrap().bounds();

let menu_state = tree.state.downcast_mut::<MenuState>();

if menu_state.pressed {
return;
}

let open = {
if cursor.is_over(prescroll)
|| cursor.is_over(parent_bounds)
Expand All @@ -561,9 +579,9 @@ where

if !open {
*prev = None;
let menu_state = tree.state.downcast_mut::<MenuState>();
menu_state.scroll_offset = 0.0;
menu_state.active = None;
menu_state.pressed = false;
}
}
}
Expand Down

0 comments on commit dd632e8

Please sign in to comment.