egui/
menu.rs

1#![allow(deprecated)]
2//! Deprecated menu API - Use [`crate::containers::menu`] instead.
3//!
4//! Usage:
5//! ```
6//! fn show_menu(ui: &mut egui::Ui) {
7//!     use egui::{menu, Button};
8//!
9//!     menu::bar(ui, |ui| {
10//!         ui.menu_button("File", |ui| {
11//!             if ui.button("Open").clicked() {
12//!                 // …
13//!             }
14//!         });
15//!     });
16//! }
17//! ```
18
19use super::{
20    Align, Context, Id, InnerResponse, PointerState, Pos2, Rect, Response, Sense, TextStyle, Ui,
21    Vec2, style::WidgetVisuals,
22};
23use crate::{
24    Align2, Area, Color32, Frame, Key, LayerId, Layout, NumExt as _, Order, Stroke, Style,
25    TextWrapMode, UiKind, WidgetText, epaint, vec2,
26    widgets::{Button, ImageButton},
27};
28use epaint::mutex::RwLock;
29use std::sync::Arc;
30
31/// What is saved between frames.
32#[derive(Clone, Default)]
33pub struct BarState {
34    open_menu: MenuRootManager,
35}
36
37impl BarState {
38    pub fn load(ctx: &Context, bar_id: Id) -> Self {
39        ctx.data_mut(|d| d.get_temp::<Self>(bar_id).unwrap_or_default())
40    }
41
42    pub fn store(self, ctx: &Context, bar_id: Id) {
43        ctx.data_mut(|d| d.insert_temp(bar_id, self));
44    }
45
46    /// Show a menu at pointer if primary-clicked response.
47    ///
48    /// Should be called from [`Context`] on a [`Response`]
49    pub fn bar_menu<R>(
50        &mut self,
51        button: &Response,
52        add_contents: impl FnOnce(&mut Ui) -> R,
53    ) -> Option<InnerResponse<R>> {
54        MenuRoot::stationary_click_interaction(button, &mut self.open_menu);
55        self.open_menu.show(button, add_contents)
56    }
57
58    pub(crate) fn has_root(&self) -> bool {
59        self.open_menu.inner.is_some()
60    }
61}
62
63impl std::ops::Deref for BarState {
64    type Target = MenuRootManager;
65
66    fn deref(&self) -> &Self::Target {
67        &self.open_menu
68    }
69}
70
71impl std::ops::DerefMut for BarState {
72    fn deref_mut(&mut self) -> &mut Self::Target {
73        &mut self.open_menu
74    }
75}
76
77fn set_menu_style(style: &mut Style) {
78    if style.compact_menu_style {
79        style.spacing.button_padding = vec2(2.0, 0.0);
80        style.visuals.widgets.active.bg_stroke = Stroke::NONE;
81        style.visuals.widgets.hovered.bg_stroke = Stroke::NONE;
82        style.visuals.widgets.inactive.weak_bg_fill = Color32::TRANSPARENT;
83        style.visuals.widgets.inactive.bg_stroke = Stroke::NONE;
84    }
85}
86
87/// The menu bar goes well in a [`crate::TopBottomPanel::top`],
88/// but can also be placed in a [`crate::Window`].
89/// In the latter case you may want to wrap it in [`Frame`].
90#[deprecated = "Use `egui::MenuBar::new().ui(` instead"]
91pub fn bar<R>(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse<R> {
92    ui.horizontal(|ui| {
93        set_menu_style(ui.style_mut());
94
95        // Take full width and fixed height:
96        let height = ui.spacing().interact_size.y;
97        ui.set_min_size(vec2(ui.available_width(), height));
98
99        add_contents(ui)
100    })
101}
102
103/// Construct a top level menu in a menu bar. This would be e.g. "File", "Edit" etc.
104///
105/// Responds to primary clicks.
106///
107/// Returns `None` if the menu is not open.
108pub fn menu_button<R>(
109    ui: &mut Ui,
110    title: impl Into<WidgetText>,
111    add_contents: impl FnOnce(&mut Ui) -> R,
112) -> InnerResponse<Option<R>> {
113    stationary_menu_impl(ui, title, Box::new(add_contents))
114}
115
116/// Construct a top level menu with a custom button in a menu bar.
117///
118/// Responds to primary clicks.
119///
120/// Returns `None` if the menu is not open.
121pub fn menu_custom_button<R>(
122    ui: &mut Ui,
123    button: Button<'_>,
124    add_contents: impl FnOnce(&mut Ui) -> R,
125) -> InnerResponse<Option<R>> {
126    stationary_menu_button_impl(ui, button, Box::new(add_contents))
127}
128
129/// Construct a top level menu with an image in a menu bar. This would be e.g. "File", "Edit" etc.
130///
131/// Responds to primary clicks.
132///
133/// Returns `None` if the menu is not open.
134#[deprecated = "Use `menu_custom_button` instead"]
135pub fn menu_image_button<R>(
136    ui: &mut Ui,
137    image_button: ImageButton<'_>,
138    add_contents: impl FnOnce(&mut Ui) -> R,
139) -> InnerResponse<Option<R>> {
140    stationary_menu_button_impl(
141        ui,
142        Button::image(image_button.image),
143        Box::new(add_contents),
144    )
145}
146
147/// Construct a nested sub menu in another menu.
148///
149/// Opens on hover.
150///
151/// Returns `None` if the menu is not open.
152pub fn submenu_button<R>(
153    ui: &mut Ui,
154    parent_state: Arc<RwLock<MenuState>>,
155    title: impl Into<WidgetText>,
156    add_contents: impl FnOnce(&mut Ui) -> R,
157) -> InnerResponse<Option<R>> {
158    SubMenu::new(parent_state, title).show(ui, add_contents)
159}
160
161/// wrapper for the contents of every menu.
162fn menu_popup<'c, R>(
163    ctx: &Context,
164    parent_layer: LayerId,
165    menu_state_arc: &Arc<RwLock<MenuState>>,
166    menu_id: Id,
167    add_contents: impl FnOnce(&mut Ui) -> R + 'c,
168) -> InnerResponse<R> {
169    let pos = {
170        let mut menu_state = menu_state_arc.write();
171        menu_state.entry_count = 0;
172        menu_state.rect.min
173    };
174
175    let area_id = menu_id.with("__menu");
176
177    ctx.pass_state_mut(|fs| {
178        fs.layers
179            .entry(parent_layer)
180            .or_default()
181            .open_popups
182            .insert(area_id)
183    });
184
185    let area = Area::new(area_id)
186        .kind(UiKind::Menu)
187        .order(Order::Foreground)
188        .fixed_pos(pos)
189        .default_width(ctx.style().spacing.menu_width)
190        .sense(Sense::hover());
191
192    let mut sizing_pass = false;
193
194    let area_response = area.show(ctx, |ui| {
195        sizing_pass = ui.is_sizing_pass();
196
197        set_menu_style(ui.style_mut());
198
199        Frame::menu(ui.style())
200            .show(ui, |ui| {
201                ui.set_menu_state(Some(menu_state_arc.clone()));
202                ui.with_layout(Layout::top_down_justified(Align::LEFT), add_contents)
203                    .inner
204            })
205            .inner
206    });
207
208    let area_rect = area_response.response.rect;
209
210    menu_state_arc.write().rect = if sizing_pass {
211        // During the sizing pass we didn't know the size yet,
212        // so we might have just constrained the position unnecessarily.
213        // Therefore keep the original=desired position until the next frame.
214        Rect::from_min_size(pos, area_rect.size())
215    } else {
216        // We knew the size, and this is where it ended up (potentially constrained to screen).
217        // Remember it for the future:
218        area_rect
219    };
220
221    area_response
222}
223
224/// Build a top level menu with a button.
225///
226/// Responds to primary clicks.
227fn stationary_menu_impl<'c, R>(
228    ui: &mut Ui,
229    title: impl Into<WidgetText>,
230    add_contents: Box<dyn FnOnce(&mut Ui) -> R + 'c>,
231) -> InnerResponse<Option<R>> {
232    let title = title.into();
233    let bar_id = ui.id();
234    let menu_id = bar_id.with(title.text());
235
236    let mut bar_state = BarState::load(ui.ctx(), bar_id);
237
238    let mut button = Button::new(title);
239
240    if bar_state.open_menu.is_menu_open(menu_id) {
241        button = button.fill(ui.visuals().widgets.open.weak_bg_fill);
242        button = button.stroke(ui.visuals().widgets.open.bg_stroke);
243    }
244
245    let button_response = ui.add(button);
246    let inner = bar_state.bar_menu(&button_response, add_contents);
247
248    bar_state.store(ui.ctx(), bar_id);
249    InnerResponse::new(inner.map(|r| r.inner), button_response)
250}
251
252/// Build a top level menu with an image button.
253///
254/// Responds to primary clicks.
255fn stationary_menu_button_impl<'c, R>(
256    ui: &mut Ui,
257    button: Button<'_>,
258    add_contents: Box<dyn FnOnce(&mut Ui) -> R + 'c>,
259) -> InnerResponse<Option<R>> {
260    let bar_id = ui.id();
261
262    let mut bar_state = BarState::load(ui.ctx(), bar_id);
263    let button_response = ui.add(button);
264    let inner = bar_state.bar_menu(&button_response, add_contents);
265
266    bar_state.store(ui.ctx(), bar_id);
267    InnerResponse::new(inner.map(|r| r.inner), button_response)
268}
269
270pub(crate) const CONTEXT_MENU_ID_STR: &str = "__egui::context_menu";
271
272/// Response to secondary clicks (right-clicks) by showing the given menu.
273pub fn context_menu(
274    response: &Response,
275    add_contents: impl FnOnce(&mut Ui),
276) -> Option<InnerResponse<()>> {
277    let menu_id = Id::new(CONTEXT_MENU_ID_STR);
278    let mut bar_state = BarState::load(&response.ctx, menu_id);
279
280    MenuRoot::context_click_interaction(response, &mut bar_state);
281    let inner_response = bar_state.show(response, add_contents);
282
283    bar_state.store(&response.ctx, menu_id);
284    inner_response
285}
286
287/// Returns `true` if the context menu is opened for this widget.
288pub fn context_menu_opened(response: &Response) -> bool {
289    let menu_id = Id::new(CONTEXT_MENU_ID_STR);
290    let bar_state = BarState::load(&response.ctx, menu_id);
291    bar_state.is_menu_open(response.id)
292}
293
294/// Stores the state for the context menu.
295#[derive(Clone, Default)]
296pub struct MenuRootManager {
297    inner: Option<MenuRoot>,
298}
299
300impl MenuRootManager {
301    /// Show a menu at pointer if right-clicked response.
302    ///
303    /// Should be called from [`Context`] on a [`Response`]
304    pub fn show<R>(
305        &mut self,
306        button: &Response,
307        add_contents: impl FnOnce(&mut Ui) -> R,
308    ) -> Option<InnerResponse<R>> {
309        if let Some(root) = self.inner.as_mut() {
310            let (menu_response, inner_response) = root.show(button, add_contents);
311            if menu_response.is_close() {
312                self.inner = None;
313            }
314            inner_response
315        } else {
316            None
317        }
318    }
319
320    fn is_menu_open(&self, id: Id) -> bool {
321        self.inner.as_ref().map(|m| m.id) == Some(id)
322    }
323}
324
325impl std::ops::Deref for MenuRootManager {
326    type Target = Option<MenuRoot>;
327
328    fn deref(&self) -> &Self::Target {
329        &self.inner
330    }
331}
332
333impl std::ops::DerefMut for MenuRootManager {
334    fn deref_mut(&mut self) -> &mut Self::Target {
335        &mut self.inner
336    }
337}
338
339/// Menu root associated with an Id from a Response
340#[derive(Clone)]
341pub struct MenuRoot {
342    pub menu_state: Arc<RwLock<MenuState>>,
343    pub id: Id,
344}
345
346impl MenuRoot {
347    pub fn new(position: Pos2, id: Id) -> Self {
348        Self {
349            menu_state: Arc::new(RwLock::new(MenuState::new(position))),
350            id,
351        }
352    }
353
354    pub fn show<R>(
355        &self,
356        button: &Response,
357        add_contents: impl FnOnce(&mut Ui) -> R,
358    ) -> (MenuResponse, Option<InnerResponse<R>>) {
359        if self.id == button.id {
360            let inner_response = menu_popup(
361                &button.ctx,
362                button.layer_id,
363                &self.menu_state,
364                self.id,
365                add_contents,
366            );
367            let menu_state = self.menu_state.read();
368
369            let escape_pressed = button.ctx.input(|i| i.key_pressed(Key::Escape));
370            if menu_state.response.is_close()
371                || escape_pressed
372                || inner_response.response.should_close()
373            {
374                return (MenuResponse::Close, Some(inner_response));
375            }
376        }
377        (MenuResponse::Stay, None)
378    }
379
380    /// Interaction with a stationary menu, i.e. fixed in another Ui.
381    ///
382    /// Responds to primary clicks.
383    fn stationary_interaction(button: &Response, root: &mut MenuRootManager) -> MenuResponse {
384        let id = button.id;
385
386        if (button.clicked() && root.is_menu_open(id))
387            || button.ctx.input(|i| i.key_pressed(Key::Escape))
388        {
389            // menu open and button clicked or esc pressed
390            return MenuResponse::Close;
391        } else if (button.clicked() && !root.is_menu_open(id))
392            || (button.hovered() && root.is_some())
393        {
394            // menu not open and button clicked
395            // or button hovered while other menu is open
396            let mut pos = button.rect.left_bottom();
397
398            let menu_frame = Frame::menu(&button.ctx.style());
399            pos.x -= menu_frame.total_margin().left; // Make fist button in menu align with the parent button
400            pos.y += button.ctx.style().spacing.menu_spacing;
401
402            if let Some(root) = root.inner.as_mut() {
403                let menu_rect = root.menu_state.read().rect;
404                let content_rect = button.ctx.input(|i| i.content_rect());
405
406                if pos.y + menu_rect.height() > content_rect.max.y {
407                    pos.y = content_rect.max.y - menu_rect.height() - button.rect.height();
408                }
409
410                if pos.x + menu_rect.width() > content_rect.max.x {
411                    pos.x = content_rect.max.x - menu_rect.width();
412                }
413            }
414
415            if let Some(to_global) = button.ctx.layer_transform_to_global(button.layer_id) {
416                pos = to_global * pos;
417            }
418
419            return MenuResponse::Create(pos, id);
420        } else if button
421            .ctx
422            .input(|i| i.pointer.any_pressed() && i.pointer.primary_down())
423            && let Some(pos) = button.ctx.input(|i| i.pointer.interact_pos())
424            && let Some(root) = root.inner.as_mut()
425            && root.id == id
426        {
427            // pressed somewhere while this menu is open
428            let in_menu = root.menu_state.read().area_contains(pos);
429            if !in_menu {
430                return MenuResponse::Close;
431            }
432        }
433        MenuResponse::Stay
434    }
435
436    /// Interaction with a context menu (secondary click).
437    pub fn context_interaction(response: &Response, root: &mut Option<Self>) -> MenuResponse {
438        let response = response.interact(Sense::click());
439        let hovered = response.hovered();
440        let secondary_clicked = response.secondary_clicked();
441
442        response.ctx.input(|input| {
443            let pointer = &input.pointer;
444            if let Some(pos) = pointer.interact_pos() {
445                let (in_old_menu, destroy) = if let Some(root) = root {
446                    let in_old_menu = root.menu_state.read().area_contains(pos);
447                    let destroy = !in_old_menu && pointer.any_pressed() && root.id == response.id;
448                    (in_old_menu, destroy)
449                } else {
450                    (false, false)
451                };
452                if !in_old_menu {
453                    if hovered && secondary_clicked {
454                        return MenuResponse::Create(pos, response.id);
455                    } else if destroy || hovered && pointer.primary_down() {
456                        return MenuResponse::Close;
457                    }
458                }
459            }
460            MenuResponse::Stay
461        })
462    }
463
464    pub fn handle_menu_response(root: &mut MenuRootManager, menu_response: MenuResponse) {
465        match menu_response {
466            MenuResponse::Create(pos, id) => {
467                root.inner = Some(Self::new(pos, id));
468            }
469            MenuResponse::Close => root.inner = None,
470            MenuResponse::Stay => {}
471        }
472    }
473
474    /// Respond to secondary (right) clicks.
475    pub fn context_click_interaction(response: &Response, root: &mut MenuRootManager) {
476        let menu_response = Self::context_interaction(response, root);
477        Self::handle_menu_response(root, menu_response);
478    }
479
480    // Responds to primary clicks.
481    pub fn stationary_click_interaction(button: &Response, root: &mut MenuRootManager) {
482        let menu_response = Self::stationary_interaction(button, root);
483        Self::handle_menu_response(root, menu_response);
484    }
485}
486
487#[derive(Copy, Clone, PartialEq, Eq)]
488pub enum MenuResponse {
489    Close,
490    Stay,
491    Create(Pos2, Id),
492}
493
494impl MenuResponse {
495    pub fn is_close(&self) -> bool {
496        *self == Self::Close
497    }
498}
499
500pub struct SubMenuButton {
501    text: WidgetText,
502    icon: WidgetText,
503    index: usize,
504}
505
506impl SubMenuButton {
507    /// The `icon` can be an emoji (e.g. `⏵` right arrow), shown right of the label
508    fn new(text: impl Into<WidgetText>, icon: impl Into<WidgetText>, index: usize) -> Self {
509        Self {
510            text: text.into(),
511            icon: icon.into(),
512            index,
513        }
514    }
515
516    fn visuals<'a>(
517        ui: &'a Ui,
518        response: &Response,
519        menu_state: &MenuState,
520        sub_id: Id,
521    ) -> &'a WidgetVisuals {
522        if menu_state.is_open(sub_id) && !response.hovered() {
523            &ui.style().visuals.widgets.open
524        } else {
525            ui.style().interact(response)
526        }
527    }
528
529    #[inline]
530    pub fn icon(mut self, icon: impl Into<WidgetText>) -> Self {
531        self.icon = icon.into();
532        self
533    }
534
535    pub(crate) fn show(self, ui: &mut Ui, menu_state: &MenuState, sub_id: Id) -> Response {
536        let Self { text, icon, .. } = self;
537
538        let text_style = TextStyle::Button;
539        let sense = Sense::click();
540
541        let text_icon_gap = ui.spacing().item_spacing.x;
542        let button_padding = ui.spacing().button_padding;
543        let total_extra = button_padding + button_padding;
544        let text_available_width = ui.available_width() - total_extra.x;
545        let text_galley = text.into_galley(
546            ui,
547            Some(TextWrapMode::Wrap),
548            text_available_width,
549            text_style.clone(),
550        );
551
552        let icon_available_width = text_available_width - text_galley.size().x;
553        let icon_galley = icon.into_galley(
554            ui,
555            Some(TextWrapMode::Wrap),
556            icon_available_width,
557            text_style,
558        );
559        let text_and_icon_size = Vec2::new(
560            text_galley.size().x + text_icon_gap + icon_galley.size().x,
561            text_galley.size().y.max(icon_galley.size().y),
562        );
563        let mut desired_size = text_and_icon_size + 2.0 * button_padding;
564        desired_size.y = desired_size.y.at_least(ui.spacing().interact_size.y);
565
566        let (rect, response) = ui.allocate_at_least(desired_size, sense);
567        response.widget_info(|| {
568            crate::WidgetInfo::labeled(
569                crate::WidgetType::Button,
570                ui.is_enabled(),
571                text_galley.text(),
572            )
573        });
574
575        if ui.is_rect_visible(rect) {
576            let visuals = Self::visuals(ui, &response, menu_state, sub_id);
577            let text_pos = Align2::LEFT_CENTER
578                .align_size_within_rect(text_galley.size(), rect.shrink2(button_padding))
579                .min;
580            let icon_pos = Align2::RIGHT_CENTER
581                .align_size_within_rect(icon_galley.size(), rect.shrink2(button_padding))
582                .min;
583
584            if ui.visuals().button_frame {
585                ui.painter().rect_filled(
586                    rect.expand(visuals.expansion),
587                    visuals.corner_radius,
588                    visuals.weak_bg_fill,
589                );
590            }
591
592            let text_color = visuals.text_color();
593            ui.painter().galley(text_pos, text_galley, text_color);
594            ui.painter().galley(icon_pos, icon_galley, text_color);
595        }
596        response
597    }
598}
599
600pub struct SubMenu {
601    button: SubMenuButton,
602    parent_state: Arc<RwLock<MenuState>>,
603}
604
605impl SubMenu {
606    fn new(parent_state: Arc<RwLock<MenuState>>, text: impl Into<WidgetText>) -> Self {
607        let index = parent_state.write().next_entry_index();
608        Self {
609            button: SubMenuButton::new(text, "⏵", index),
610            parent_state,
611        }
612    }
613
614    pub fn show<R>(
615        self,
616        ui: &mut Ui,
617        add_contents: impl FnOnce(&mut Ui) -> R,
618    ) -> InnerResponse<Option<R>> {
619        let sub_id = ui.id().with(self.button.index);
620        let response = self.button.show(ui, &self.parent_state.read(), sub_id);
621        self.parent_state
622            .write()
623            .submenu_button_interaction(ui, sub_id, &response);
624        let inner =
625            self.parent_state
626                .write()
627                .show_submenu(ui.ctx(), ui.layer_id(), sub_id, add_contents);
628        InnerResponse::new(inner, response)
629    }
630}
631
632/// Components of menu state, public for advanced usage.
633///
634/// Usually you don't need to use it directly.
635pub struct MenuState {
636    /// The opened sub-menu and its [`Id`]
637    sub_menu: Option<(Id, Arc<RwLock<MenuState>>)>,
638
639    /// Bounding box of this menu (without the sub-menu),
640    /// including the frame and everything.
641    pub rect: Rect,
642
643    /// Used to check if any menu in the tree wants to close
644    pub response: MenuResponse,
645
646    /// Used to hash different [`Id`]s for sub-menus
647    entry_count: usize,
648}
649
650impl MenuState {
651    pub fn new(position: Pos2) -> Self {
652        Self {
653            rect: Rect::from_min_size(position, Vec2::ZERO),
654            sub_menu: None,
655            response: MenuResponse::Stay,
656            entry_count: 0,
657        }
658    }
659
660    /// Close menu hierarchy.
661    pub fn close(&mut self) {
662        self.response = MenuResponse::Close;
663    }
664
665    fn show_submenu<R>(
666        &mut self,
667        ctx: &Context,
668        parent_layer: LayerId,
669        id: Id,
670        add_contents: impl FnOnce(&mut Ui) -> R,
671    ) -> Option<R> {
672        let (sub_response, response) = self.submenu(id).map(|sub| {
673            let inner_response = menu_popup(ctx, parent_layer, sub, id, add_contents);
674            if inner_response.response.should_close() {
675                sub.write().close();
676            }
677            (sub.read().response, inner_response.inner)
678        })?;
679        self.cascade_close_response(sub_response);
680        Some(response)
681    }
682
683    /// Check if position is in the menu hierarchy's area.
684    pub fn area_contains(&self, pos: Pos2) -> bool {
685        self.rect.contains(pos)
686            || self
687                .sub_menu
688                .as_ref()
689                .is_some_and(|(_, sub)| sub.read().area_contains(pos))
690    }
691
692    fn next_entry_index(&mut self) -> usize {
693        self.entry_count += 1;
694        self.entry_count - 1
695    }
696
697    /// Sense button interaction opening and closing submenu.
698    fn submenu_button_interaction(&mut self, ui: &Ui, sub_id: Id, button: &Response) {
699        let pointer = ui.input(|i| i.pointer.clone());
700        let open = self.is_open(sub_id);
701        if self.moving_towards_current_submenu(&pointer) {
702            // We don't close the submenu if the pointer is on its way to hover it.
703            // ensure to repaint once even when pointer is not moving
704            ui.ctx().request_repaint();
705        } else if !open && button.hovered() {
706            // TODO(emilk): open menu to the left if there isn't enough space to the right
707            let mut pos = button.rect.right_top();
708            pos.x = self.rect.right() + ui.spacing().menu_spacing;
709            pos.y -= Frame::menu(ui.style()).total_margin().top; // align the first button in the submenu with the parent button
710
711            self.open_submenu(sub_id, pos);
712        } else if open
713            && ui.response().contains_pointer()
714            && !button.hovered()
715            && !self.hovering_current_submenu(&pointer)
716        {
717            // We are hovering something else in the menu, so close the submenu.
718            self.close_submenu();
719        }
720    }
721
722    /// Check if pointer is moving towards current submenu.
723    fn moving_towards_current_submenu(&self, pointer: &PointerState) -> bool {
724        if pointer.is_still() {
725            return false;
726        }
727
728        if let Some(sub_menu) = self.current_submenu()
729            && let Some(pos) = pointer.hover_pos()
730        {
731            let rect = sub_menu.read().rect;
732            return rect.intersects_ray(pos, pointer.direction().normalized());
733        }
734        false
735    }
736
737    /// Check if pointer is hovering current submenu.
738    fn hovering_current_submenu(&self, pointer: &PointerState) -> bool {
739        if let Some(sub_menu) = self.current_submenu()
740            && let Some(pos) = pointer.hover_pos()
741        {
742            return sub_menu.read().area_contains(pos);
743        }
744        false
745    }
746
747    /// Cascade close response to menu root.
748    fn cascade_close_response(&mut self, response: MenuResponse) {
749        if response.is_close() {
750            self.response = response;
751        }
752    }
753
754    fn is_open(&self, id: Id) -> bool {
755        self.sub_id() == Some(id)
756    }
757
758    fn sub_id(&self) -> Option<Id> {
759        self.sub_menu.as_ref().map(|(id, _)| *id)
760    }
761
762    fn current_submenu(&self) -> Option<&Arc<RwLock<Self>>> {
763        self.sub_menu.as_ref().map(|(_, sub)| sub)
764    }
765
766    fn submenu(&self, id: Id) -> Option<&Arc<RwLock<Self>>> {
767        let (k, sub) = self.sub_menu.as_ref()?;
768        if id == *k { Some(sub) } else { None }
769    }
770
771    /// Open submenu at position, if not already open.
772    fn open_submenu(&mut self, id: Id, pos: Pos2) {
773        if !self.is_open(id) {
774            self.sub_menu = Some((id, Arc::new(RwLock::new(Self::new(pos)))));
775        }
776    }
777
778    fn close_submenu(&mut self) {
779        self.sub_menu = None;
780    }
781}