From f71cbc247560bc45f988ca2e5288266788194ec5 Mon Sep 17 00:00:00 2001 From: ItsEthra <107059409+ItsEthra@users.noreply.github.com> Date: Mon, 31 Oct 2022 19:57:15 +0300 Subject: [PATCH] Fixed menu popups going outside of the screen (#2191) * Fixed menu popups going outside of the screen * Made menu appear above above the button instead --- crates/egui/src/menu.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/egui/src/menu.rs b/crates/egui/src/menu.rs index 1950cb83..a697e2da 100644 --- a/crates/egui/src/menu.rs +++ b/crates/egui/src/menu.rs @@ -288,7 +288,22 @@ impl MenuRoot { { // menu not open and button clicked // or button hovered while other menu is open - let pos = response.rect.left_bottom(); + drop(input); + + let mut pos = response.rect.left_bottom(); + if let Some(root) = root.inner.as_mut() { + let menu_rect = root.menu_state.read().rect; + let screen_rect = response.ctx.input().screen_rect; + + if pos.y + menu_rect.height() > screen_rect.max.y { + pos.y = screen_rect.max.y - menu_rect.height() - response.rect.height(); + } + + if pos.x + menu_rect.width() > screen_rect.max.x { + pos.x = screen_rect.max.x - menu_rect.width(); + } + } + return MenuResponse::Create(pos, id); } else if input.pointer.any_pressed() && input.pointer.primary_down() { if let Some(pos) = input.pointer.interact_pos() {