1use crate::style::StyleModifier;
12use crate::{
13 Button, Color32, Context, Frame, Id, InnerResponse, IntoAtoms, Layout, Popup,
14 PopupCloseBehavior, Response, Style, Ui, UiBuilder, UiKind, UiStack, UiStackInfo, Widget as _,
15};
16use emath::{Align, RectAlign, Vec2, vec2};
17use epaint::Stroke;
18
19pub fn menu_style(style: &mut Style) {
23 style.spacing.button_padding = vec2(2.0, 0.0);
24 style.visuals.widgets.active.bg_stroke = Stroke::NONE;
25 style.visuals.widgets.open.bg_stroke = Stroke::NONE;
26 style.visuals.widgets.hovered.bg_stroke = Stroke::NONE;
27 style.visuals.widgets.inactive.weak_bg_fill = Color32::TRANSPARENT;
28 style.visuals.widgets.inactive.bg_stroke = Stroke::NONE;
29}
30
31pub fn find_menu_root(ui: &Ui) -> &UiStack {
33 ui.stack()
34 .iter()
35 .find(|stack| {
36 stack.is_root_ui()
37 || [Some(UiKind::Popup), Some(UiKind::Menu)].contains(&stack.kind())
38 || stack.info.tags.contains(MenuConfig::MENU_CONFIG_TAG)
39 })
40 .expect("We should always find the root")
41}
42
43pub fn is_in_menu(ui: &Ui) -> bool {
48 for stack in ui.stack().iter() {
49 if let Some(config) = stack
50 .info
51 .tags
52 .get_downcast::<MenuConfig>(MenuConfig::MENU_CONFIG_TAG)
53 {
54 return !config.bar;
55 }
56 if [Some(UiKind::Popup), Some(UiKind::Menu)].contains(&stack.kind()) {
57 return true;
58 }
59 }
60 false
61}
62
63#[derive(Clone, Debug)]
65pub struct MenuConfig {
66 bar: bool,
68
69 pub close_behavior: PopupCloseBehavior,
71
72 pub style: StyleModifier,
76}
77
78impl Default for MenuConfig {
79 fn default() -> Self {
80 Self {
81 close_behavior: PopupCloseBehavior::default(),
82 bar: false,
83 style: menu_style.into(),
84 }
85 }
86}
87
88impl MenuConfig {
89 pub const MENU_CONFIG_TAG: &'static str = "egui_menu_config";
91
92 pub fn new() -> Self {
93 Self::default()
94 }
95
96 #[inline]
98 pub fn close_behavior(mut self, close_behavior: PopupCloseBehavior) -> Self {
99 self.close_behavior = close_behavior;
100 self
101 }
102
103 #[inline]
107 pub fn style(mut self, style: impl Into<StyleModifier>) -> Self {
108 self.style = style.into();
109 self
110 }
111
112 fn from_stack(stack: &UiStack) -> Self {
113 stack
114 .info
115 .tags
116 .get_downcast(Self::MENU_CONFIG_TAG)
117 .cloned()
118 .unwrap_or_default()
119 }
120
121 pub fn find(ui: &Ui) -> Self {
125 find_menu_root(ui)
126 .info
127 .tags
128 .get_downcast(Self::MENU_CONFIG_TAG)
129 .cloned()
130 .unwrap_or_default()
131 }
132}
133
134#[derive(Clone)]
136pub struct MenuState {
137 pub open_item: Option<Id>,
139 last_visible_pass: u64,
140}
141
142impl MenuState {
143 pub const ID: &'static str = "menu_state";
144
145 pub fn from_ui<R>(ui: &Ui, f: impl FnOnce(&mut Self, &UiStack) -> R) -> R {
147 let stack = find_menu_root(ui);
148 Self::from_id(ui.ctx(), stack.id, |state| f(state, stack))
149 }
150
151 pub fn from_id<R>(ctx: &Context, id: Id, f: impl FnOnce(&mut Self) -> R) -> R {
153 let pass_nr = ctx.cumulative_pass_nr();
154 ctx.data_mut(|data| {
155 let state = data.get_temp_mut_or_insert_with(id.with(Self::ID), || Self {
156 open_item: None,
157 last_visible_pass: pass_nr,
158 });
159 if state.last_visible_pass + 1 < pass_nr {
161 state.open_item = None;
162 }
163 state.last_visible_pass = pass_nr;
164 f(state)
165 })
166 }
167
168 pub fn is_deepest_sub_menu(ctx: &Context, id: Id) -> bool {
170 Self::from_id(ctx, id, |state| state.open_item.is_none())
171 }
172}
173
174#[derive(Clone, Debug)]
193pub struct MenuBar {
194 config: MenuConfig,
195 style: StyleModifier,
196}
197
198#[deprecated = "Renamed to `egui::MenuBar`"]
199pub type Bar = MenuBar;
200
201impl Default for MenuBar {
202 fn default() -> Self {
203 Self {
204 config: MenuConfig::default(),
205 style: menu_style.into(),
206 }
207 }
208}
209
210impl MenuBar {
211 pub fn new() -> Self {
212 Self::default()
213 }
214
215 #[inline]
220 pub fn style(mut self, style: impl Into<StyleModifier>) -> Self {
221 self.style = style.into();
222 self
223 }
224
225 #[inline]
229 pub fn config(mut self, config: MenuConfig) -> Self {
230 self.config = config;
231 self
232 }
233
234 #[inline]
236 pub fn ui<R>(self, ui: &mut Ui, content: impl FnOnce(&mut Ui) -> R) -> InnerResponse<R> {
237 let Self { mut config, style } = self;
238 config.bar = true;
239 ui.horizontal(|ui| {
242 ui.scope_builder(
243 UiBuilder::new()
244 .layout(Layout::left_to_right(Align::Center))
245 .ui_stack_info(
246 UiStackInfo::new(UiKind::Menu)
247 .with_tag_value(MenuConfig::MENU_CONFIG_TAG, config),
248 ),
249 |ui| {
250 style.apply(ui.style_mut());
251
252 let height = ui.spacing().interact_size.y;
254 ui.set_min_size(vec2(ui.available_width(), height));
255
256 content(ui)
257 },
258 )
259 .inner
260 })
261 }
262}
263
264pub struct MenuButton<'a> {
270 pub button: Button<'a>,
271 pub config: Option<MenuConfig>,
272}
273
274impl<'a> MenuButton<'a> {
275 pub fn new(atoms: impl IntoAtoms<'a>) -> Self {
276 Self::from_button(Button::new(atoms.into_atoms()))
277 }
278
279 #[inline]
281 pub fn config(mut self, config: MenuConfig) -> Self {
282 self.config = Some(config);
283 self
284 }
285
286 #[inline]
288 pub fn from_button(button: Button<'a>) -> Self {
289 Self {
290 button,
291 config: None,
292 }
293 }
294
295 pub fn ui<R>(
297 self,
298 ui: &mut Ui,
299 content: impl FnOnce(&mut Ui) -> R,
300 ) -> (Response, Option<InnerResponse<R>>) {
301 let response = self.button.ui(ui);
302 let mut config = self.config.unwrap_or_else(|| MenuConfig::find(ui));
303 config.bar = false;
304 let inner = Popup::menu(&response)
305 .close_behavior(config.close_behavior)
306 .style(config.style.clone())
307 .info(
308 UiStackInfo::new(UiKind::Menu).with_tag_value(MenuConfig::MENU_CONFIG_TAG, config),
309 )
310 .show(content);
311 (response, inner)
312 }
313}
314
315pub struct SubMenuButton<'a> {
317 pub button: Button<'a>,
318 pub sub_menu: SubMenu,
319}
320
321impl<'a> SubMenuButton<'a> {
322 pub const RIGHT_ARROW: &'static str = "⏵";
324
325 pub fn new(atoms: impl IntoAtoms<'a>) -> Self {
326 Self::from_button(Button::new(atoms.into_atoms()).right_text("⏵"))
327 }
328
329 pub fn from_button(button: Button<'a>) -> Self {
334 Self {
335 button,
336 sub_menu: SubMenu::default(),
337 }
338 }
339
340 #[inline]
344 pub fn config(mut self, config: MenuConfig) -> Self {
345 self.sub_menu.config = Some(config);
346 self
347 }
348
349 pub fn ui<R>(
351 self,
352 ui: &mut Ui,
353 content: impl FnOnce(&mut Ui) -> R,
354 ) -> (Response, Option<InnerResponse<R>>) {
355 let my_id = ui.next_auto_id();
356 let open = MenuState::from_ui(ui, |state, _| {
357 state.open_item == Some(SubMenu::id_from_widget_id(my_id))
358 });
359 let inactive = ui.style().visuals.widgets.inactive;
360 if open {
362 ui.style_mut().visuals.widgets.inactive = ui.style().visuals.widgets.open;
363 }
364 let response = self.button.ui(ui);
365 ui.style_mut().visuals.widgets.inactive = inactive;
366
367 let popup_response = self.sub_menu.show(ui, &response, content);
368
369 (response, popup_response)
370 }
371}
372
373#[derive(Clone, Debug, Default)]
378pub struct SubMenu {
379 config: Option<MenuConfig>,
380}
381
382impl SubMenu {
383 pub fn new() -> Self {
384 Self::default()
385 }
386
387 #[inline]
391 pub fn config(mut self, config: MenuConfig) -> Self {
392 self.config = Some(config);
393 self
394 }
395
396 pub fn id_from_widget_id(widget_id: Id) -> Id {
398 widget_id.with("submenu")
399 }
400
401 pub fn show<R>(
403 self,
404 ui: &Ui,
405 button_response: &Response,
406 content: impl FnOnce(&mut Ui) -> R,
407 ) -> Option<InnerResponse<R>> {
408 let frame = Frame::menu(ui.style());
409
410 let id = Self::id_from_widget_id(button_response.id);
411
412 let (open_item, menu_id, parent_config) = MenuState::from_ui(ui, |state, stack| {
413 (state.open_item, stack.id, MenuConfig::from_stack(stack))
414 });
415
416 let mut menu_config = self.config.unwrap_or_else(|| parent_config.clone());
417 menu_config.bar = false;
418
419 let menu_root_response = ui
420 .ctx()
421 .read_response(menu_id)
422 .unwrap();
424
425 let hover_pos = ui.ctx().pointer_hover_pos();
426
427 let menu_rect = menu_root_response.rect - frame.total_margin();
429 let is_hovering_menu = hover_pos.is_some_and(|pos| {
430 ui.ctx().layer_id_at(pos) == Some(menu_root_response.layer_id)
431 && menu_rect.contains(pos)
432 });
433
434 let is_any_open = open_item.is_some();
435 let mut is_open = open_item == Some(id);
436 let mut set_open = None;
437
438 let button_rect = button_response
441 .rect
442 .expand2(ui.style().spacing.item_spacing / 2.0);
443
444 let is_hovered = hover_pos.is_some_and(|pos| button_rect.contains(pos));
447
448 if (!is_any_open && is_hovered) || button_response.clicked() {
450 set_open = Some(true);
451 is_open = true;
452 MenuState::from_id(ui.ctx(), id, |state| {
454 state.open_item = None;
455 });
456 }
457
458 let gap = frame.total_margin().sum().x / 2.0 + 2.0;
459
460 let mut response = button_response.clone();
461 let expand = Vec2::new(0.0, frame.total_margin().sum().y / 2.0);
463 response.interact_rect = response.interact_rect.expand2(expand);
464
465 let popup_response = Popup::from_response(&response)
466 .id(id)
467 .open(is_open)
468 .align(RectAlign::RIGHT_START)
469 .layout(Layout::top_down_justified(Align::Min))
470 .gap(gap)
471 .style(menu_config.style.clone())
472 .frame(frame)
473 .close_behavior(PopupCloseBehavior::IgnoreClicks)
475 .info(
476 UiStackInfo::new(UiKind::Menu)
477 .with_tag_value(MenuConfig::MENU_CONFIG_TAG, menu_config.clone()),
478 )
479 .show(|ui| {
480 if button_response.clicked() || button_response.is_pointer_button_down_on() {
482 ui.ctx().move_to_top(ui.layer_id());
483 }
484 content(ui)
485 });
486
487 if let Some(popup_response) = &popup_response {
488 let is_deepest_submenu = MenuState::is_deepest_sub_menu(ui.ctx(), id);
490
491 let clicked_outside = is_deepest_submenu
497 && popup_response.response.clicked_elsewhere()
498 && menu_root_response.clicked_elsewhere();
499
500 let submenu_button_clicked = button_response.clicked();
505
506 let clicked_inside = is_deepest_submenu
507 && !submenu_button_clicked
508 && response.ctx.input(|i| i.pointer.any_click())
509 && hover_pos.is_some_and(|pos| popup_response.response.interact_rect.contains(pos));
510
511 let click_close = match menu_config.close_behavior {
512 PopupCloseBehavior::CloseOnClick => clicked_outside || clicked_inside,
513 PopupCloseBehavior::CloseOnClickOutside => clicked_outside,
514 PopupCloseBehavior::IgnoreClicks => false,
515 };
516
517 if click_close {
518 set_open = Some(false);
519 ui.close();
520 }
521
522 let is_moving_towards_rect = ui.input(|i| {
523 i.pointer
524 .is_moving_towards_rect(&popup_response.response.rect)
525 });
526 if is_moving_towards_rect {
527 ui.ctx().request_repaint();
530 }
531 let hovering_other_menu_entry = is_open
532 && !is_hovered
533 && !popup_response.response.contains_pointer()
534 && !is_moving_towards_rect
535 && is_hovering_menu;
536
537 let close_called = popup_response.response.should_close();
538
539 if close_called {
541 ui.close();
542 }
543
544 if hovering_other_menu_entry {
545 set_open = Some(false);
546 }
547
548 if ui.will_parent_close() {
549 ui.data_mut(|data| data.remove_by_type::<MenuState>());
550 }
551 }
552
553 if let Some(set_open) = set_open {
554 MenuState::from_id(ui.ctx(), menu_id, |state| {
555 state.open_item = set_open.then_some(id);
556 });
557 }
558
559 popup_response
560 }
561}