Skip to content

Commit 9083b65

Browse files
emilkhacknus
authored andcommitted
Give each menu Area an id distinct from the id of what was clicked (emilk#4114)
* Closes emilk#4113 Previously the `Id` of the menu `Area` was using the same id as the thing that was clicked (i.e. the button opening menu), which lead to id clashes
1 parent 17e90e3 commit 9083b65

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

crates/egui/src/menu.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ impl BarState {
3939
}
4040

4141
/// Show a menu at pointer if primary-clicked response.
42+
///
4243
/// Should be called from [`Context`] on a [`Response`]
4344
pub fn bar_menu<R>(
4445
&mut self,
4546
response: &Response,
4647
add_contents: impl FnOnce(&mut Ui) -> R,
4748
) -> Option<InnerResponse<R>> {
48-
MenuRoot::stationary_click_interaction(response, &mut self.open_menu, response.id);
49+
MenuRoot::stationary_click_interaction(response, &mut self.open_menu);
4950
self.open_menu.show(response, add_contents)
5051
}
5152

@@ -134,7 +135,7 @@ pub(crate) fn submenu_button<R>(
134135
/// wrapper for the contents of every menu.
135136
pub(crate) fn menu_ui<'c, R>(
136137
ctx: &Context,
137-
menu_id: impl Into<Id>,
138+
menu_id: Id,
138139
menu_state_arc: &Arc<RwLock<MenuState>>,
139140
add_contents: impl FnOnce(&mut Ui) -> R + 'c,
140141
) -> InnerResponse<R> {
@@ -144,7 +145,7 @@ pub(crate) fn menu_ui<'c, R>(
144145
menu_state.rect.min
145146
};
146147

147-
let area = Area::new(menu_id)
148+
let area = Area::new(menu_id.with("__menu"))
148149
.order(Order::Foreground)
149150
.fixed_pos(pos)
150151
.constrain_to(ctx.screen_rect())
@@ -222,7 +223,7 @@ pub(crate) fn context_menu(
222223
let menu_id = Id::new(CONTEXT_MENU_ID_STR);
223224
let mut bar_state = BarState::load(&response.ctx, menu_id);
224225

225-
MenuRoot::context_click_interaction(response, &mut bar_state, response.id);
226+
MenuRoot::context_click_interaction(response, &mut bar_state);
226227
let inner_response = bar_state.show(response, add_contents);
227228

228229
bar_state.store(&response.ctx, menu_id);
@@ -237,6 +238,7 @@ pub(crate) struct MenuRootManager {
237238

238239
impl MenuRootManager {
239240
/// Show a menu at pointer if right-clicked response.
241+
///
240242
/// Should be called from [`Context`] on a [`Response`]
241243
pub fn show<R>(
242244
&mut self,
@@ -308,11 +310,9 @@ impl MenuRoot {
308310
/// Interaction with a stationary menu, i.e. fixed in another Ui.
309311
///
310312
/// Responds to primary clicks.
311-
fn stationary_interaction(
312-
response: &Response,
313-
root: &mut MenuRootManager,
314-
id: Id,
315-
) -> MenuResponse {
313+
fn stationary_interaction(response: &Response, root: &mut MenuRootManager) -> MenuResponse {
314+
let id = response.id;
315+
316316
if (response.clicked() && root.is_menu_open(id))
317317
|| response.ctx.input(|i| i.key_pressed(Key::Escape))
318318
{
@@ -357,8 +357,8 @@ impl MenuRoot {
357357
MenuResponse::Stay
358358
}
359359

360-
/// Interaction with a context menu (secondary clicks).
361-
fn context_interaction(response: &Response, root: &mut Option<Self>, id: Id) -> MenuResponse {
360+
/// Interaction with a context menu (secondary click).
361+
fn context_interaction(response: &Response, root: &mut Option<Self>) -> MenuResponse {
362362
let response = response.interact(Sense::click());
363363
response.ctx.input(|input| {
364364
let pointer = &input.pointer;
@@ -371,7 +371,7 @@ impl MenuRoot {
371371
}
372372
if !in_old_menu {
373373
if response.hovered() && response.secondary_clicked() {
374-
return MenuResponse::Create(pos, id);
374+
return MenuResponse::Create(pos, response.id);
375375
} else if (response.hovered() && pointer.primary_down()) || destroy {
376376
return MenuResponse::Close;
377377
}
@@ -392,14 +392,14 @@ impl MenuRoot {
392392
}
393393

394394
/// Respond to secondary (right) clicks.
395-
pub fn context_click_interaction(response: &Response, root: &mut MenuRootManager, id: Id) {
396-
let menu_response = Self::context_interaction(response, root, id);
395+
pub fn context_click_interaction(response: &Response, root: &mut MenuRootManager) {
396+
let menu_response = Self::context_interaction(response, root);
397397
Self::handle_menu_response(root, menu_response);
398398
}
399399

400400
// Responds to primary clicks.
401-
pub fn stationary_click_interaction(response: &Response, root: &mut MenuRootManager, id: Id) {
402-
let menu_response = Self::stationary_interaction(response, root, id);
401+
pub fn stationary_click_interaction(response: &Response, root: &mut MenuRootManager) {
402+
let menu_response = Self::stationary_interaction(response, root);
403403
Self::handle_menu_response(root, menu_response);
404404
}
405405
}

crates/egui/src/widget_rect.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,17 @@ impl WidgetRects {
105105
// e.g. calling `response.interact(…)` to add more interaction.
106106
let (idx_in_layer, existing) = entry.get_mut();
107107

108+
egui_assert!(
109+
existing.layer_id == widget_rect.layer_id,
110+
"Widget changed layer_id during the frame"
111+
);
112+
108113
// Update it:
109114
existing.rect = widget_rect.rect; // last wins
110115
existing.interact_rect = widget_rect.interact_rect; // last wins
111116
existing.sense |= widget_rect.sense;
112117
existing.enabled |= widget_rect.enabled;
113118

114-
egui_assert!(
115-
existing.layer_id == widget_rect.layer_id,
116-
"Widget changed layer_id during the frame"
117-
);
118-
119119
if existing.layer_id == widget_rect.layer_id {
120120
layer_widgets[*idx_in_layer] = *existing;
121121
}

0 commit comments

Comments
 (0)