Skip to content

Commit

Permalink
fix(segmented_button): close context menu when clicked outside
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonrhansen authored Dec 31, 2024
1 parent 3f2ba11 commit 51ede4b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/widget/menu/menu_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ where
.merge(menu_status)
}

Mouse(ButtonReleased(Left)) | Touch(FingerLifted { .. }) => {
Mouse(ButtonReleased(_)) | Touch(FingerLifted { .. }) => {
let state = self.tree.state.downcast_mut::<MenuBarState>();
state.pressed = false;

Expand All @@ -596,7 +596,13 @@ where
.iter()
.any(|ms| ms.menu_bounds.check_bounds.contains(overlay_cursor));

if self.close_condition.click_inside && is_inside {
if self.close_condition.click_inside
&& is_inside
&& matches!(
event,
Mouse(ButtonReleased(Left)) | Touch(FingerLifted { .. })
)
{
state.reset();
return Captured;
}
Expand Down
15 changes: 12 additions & 3 deletions src/widget/segmented_button/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,13 @@ where
}
}

if let Event::Mouse(mouse::Event::ButtonReleased(_))
| Event::Touch(touch::Event::FingerLifted { .. }) = event
{
state.focused = false;
state.focused_item = Item::None;
}

if let Some(on_activate) = self.on_activate.as_ref() {
if let Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
| Event::Touch(touch::Event::FingerLifted { .. }) = event
Expand All @@ -928,8 +935,6 @@ where
state.show_context = Some(key);
state.context_cursor =
cursor_position.position().unwrap_or_default();
state.focused = true;
state.focused_item = Item::Tab(key);

let menu_state =
tree.children[0].state.downcast_mut::<MenuBarState>();
Expand Down Expand Up @@ -1321,13 +1326,17 @@ where

let center_y = bounds.center_y();

let menu_open = !tree.children.is_empty()
&& tree.children[0].state.downcast_ref::<MenuBarState>().open;

let key_is_active = self.model.is_active(key);
let key_is_hovered = self.button_is_hovered(state, key);
let key_has_context_menu_open = menu_open && state.show_context == Some(key);
let status_appearance = if self.button_is_focused(state, key) {
appearance.focus
} else if key_is_active {
appearance.active
} else if key_is_hovered {
} else if key_is_hovered || key_has_context_menu_open {
appearance.hover
} else {
appearance.inactive
Expand Down

0 comments on commit 51ede4b

Please sign in to comment.