diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2022-02-07 08:19:58 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-07 08:19:58 +0100 |
commit | 863f2840a582e289612147be43d8cbededed668c (patch) | |
tree | ed045d835ef916e5e5622469d85bf76d6306780d | |
parent | c2a540de51e41e1dbfc3ae8d6cfc438061e3c28d (diff) | |
parent | 8bde86da10eacff9221a19a2806eb2ece789de03 (diff) |
Merge pull request #57735 from YeldhamDev/popups_and_mirrors
Make popups from `MenuButton`, `OptionButton`, and submenus obey the layout direction
-rw-r--r-- | scene/gui/menu_button.cpp | 3 | ||||
-rw-r--r-- | scene/gui/option_button.cpp | 5 | ||||
-rw-r--r-- | scene/gui/popup_menu.cpp | 34 |
3 files changed, 34 insertions, 8 deletions
diff --git a/scene/gui/menu_button.cpp b/scene/gui/menu_button.cpp index a985a9d031..94fa5d81d8 100644 --- a/scene/gui/menu_button.cpp +++ b/scene/gui/menu_button.cpp @@ -136,6 +136,9 @@ int MenuButton::get_item_count() const { void MenuButton::_notification(int p_what) { switch (p_what) { + case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: { + popup->set_layout_direction((Window::LayoutDirection)get_layout_direction()); + } break; case NOTIFICATION_VISIBILITY_CHANGED: { if (!is_visible_in_tree()) { popup->hide(); diff --git a/scene/gui/option_button.cpp b/scene/gui/option_button.cpp index 9984ab240a..31f3b306b7 100644 --- a/scene/gui/option_button.cpp +++ b/scene/gui/option_button.cpp @@ -92,7 +92,10 @@ void OptionButton::_notification(int p_what) { arrow->draw(ci, ofs, clr); } break; case NOTIFICATION_TRANSLATION_CHANGED: - case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: + case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: { + popup->set_layout_direction((Window::LayoutDirection)get_layout_direction()); + [[fallthrough]]; + } case NOTIFICATION_THEME_CHANGED: { if (has_theme_icon(SNAME("arrow"))) { if (is_layout_rtl()) { diff --git a/scene/gui/popup_menu.cpp b/scene/gui/popup_menu.cpp index 4798019720..6d43bbdcf3 100644 --- a/scene/gui/popup_menu.cpp +++ b/scene/gui/popup_menu.cpp @@ -203,15 +203,17 @@ void PopupMenu::_activate_submenu(int p_over) { float scroll_offset = control->get_position().y; - Point2 submenu_pos; + submenu_popup->set_as_minsize(); // Shrink the popup size to its contents. Size2 submenu_size = submenu_popup->get_size(); + + Point2 submenu_pos; if (control->is_layout_rtl()) { submenu_pos = this_pos + Point2(-submenu_size.width, items[p_over]._ofs_cache + scroll_offset); } else { submenu_pos = this_pos + Point2(this_rect.size.width, items[p_over]._ofs_cache + scroll_offset); } - // Fix pos if going outside parent rect + // Fix pos if going outside parent rect. if (submenu_pos.x < get_parent_rect().position.x) { submenu_pos.x = this_pos.x + submenu_size.width; } @@ -222,7 +224,6 @@ void PopupMenu::_activate_submenu(int p_over) { submenu_popup->set_close_on_parent_focus(false); submenu_popup->set_position(submenu_pos); - submenu_popup->set_as_minsize(); // Shrink the popup size to its contents. PopupMenu *submenu_pum = Object::cast_to<PopupMenu>(submenu_popup); if (!submenu_pum) { @@ -642,8 +643,8 @@ void PopupMenu::_draw_items() { if (items[i].separator) { if (!text.is_empty()) { Vector2 text_pos = Point2(separator_ofs, item_ofs.y + Math::floor((h - items[i].text_buf->get_size().y) / 2.0)); - if (!items[i].icon.is_null()) { - text_pos.x = rtl ? text_pos.x - (icon_size.width + hseparation) : text_pos.x + icon_size.width + hseparation; + if (!rtl && !items[i].icon.is_null()) { + text_pos.x += icon_size.width + hseparation; } if (outline_size > 0 && font_outline_color.a > 0) { @@ -747,13 +748,32 @@ void PopupMenu::_notification(int p_what) { case NOTIFICATION_ENTER_TREE: { PopupMenu *pm = Object::cast_to<PopupMenu>(get_parent()); if (pm) { - // Inherit submenu's popup delay time from parent menu + // Inherit submenu's popup delay time from parent menu. float pm_delay = pm->get_submenu_popup_delay(); set_submenu_popup_delay(pm_delay); } } break; case NOTIFICATION_THEME_CHANGED: - case Control::NOTIFICATION_LAYOUT_DIRECTION_CHANGED: + case Control::NOTIFICATION_LAYOUT_DIRECTION_CHANGED: { + // Pass the layout direction to all submenus. + for (int i = 0; i < items.size(); i++) { + if (items[i].submenu.is_empty()) { + continue; + } + + Node *n = get_node(items[i].submenu); + if (!n) { + continue; + } + + PopupMenu *pm = Object::cast_to<PopupMenu>(n); + if (pm) { + pm->set_layout_direction(get_layout_direction()); + } + } + + [[fallthrough]]; + } case NOTIFICATION_TRANSLATION_CHANGED: { for (int i = 0; i < items.size(); i++) { items.write[i].xl_text = atr(items[i].text); |