Skip to content

Commit a7a2a12

Browse files
committed
Merge pull request #100671 from bruvzg/get_menu
[MenuBar] Use PopupMenu title property as a menu name.
2 parents 91e7210 + 9604e98 commit a7a2a12

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed

doc/classes/MenuBar.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
A horizontal menu bar that creates a menu for each [PopupMenu] child.
55
</brief_description>
66
<description>
7-
A horizontal menu bar that creates a menu for each [PopupMenu] child. New items are created by adding [PopupMenu]s to this node.
7+
A horizontal menu bar that creates a menu for each [PopupMenu] child. New items are created by adding [PopupMenu]s to this node. Item title is determined by [member Window.title], or node name if [member Window.title] is empty. Item title can be overridden using [method set_menu_title].
88
</description>
99
<tutorials>
1010
</tutorials>

doc/classes/Window.xml

+5
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,11 @@
768768
Emitted when the [constant NOTIFICATION_THEME_CHANGED] notification is sent.
769769
</description>
770770
</signal>
771+
<signal name="title_changed">
772+
<description>
773+
Emitted when window title bar text is changed.
774+
</description>
775+
</signal>
771776
<signal name="titlebar_changed">
772777
<description>
773778
Emitted when window title bar decorations are changed, e.g. macOS window enter/exit full screen mode, or extend-to-title flag is changed.

scene/gui/menu_bar.cpp

+30-5
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,9 @@ void MenuBar::_refresh_menu_names() {
507507

508508
Vector<PopupMenu *> popups = _get_popups();
509509
for (int i = 0; i < popups.size(); i++) {
510-
if (!popups[i]->has_meta("_menu_name") && String(popups[i]->get_name()) != get_menu_title(i)) {
511-
menu_cache.write[i].name = popups[i]->get_name();
510+
String menu_name = popups[i]->get_title().is_empty() ? String(popups[i]->get_name()) : popups[i]->get_title();
511+
if (!popups[i]->has_meta("_menu_name") && menu_name != get_menu_title(i)) {
512+
menu_cache.write[i].name = menu_name;
512513
shape(menu_cache.write[i]);
513514
queue_redraw();
514515
if (is_global && menu_cache[i].submenu_rid.is_valid()) {
@@ -547,16 +548,37 @@ int MenuBar::get_menu_idx_from_control(PopupMenu *p_child) const {
547548
return -1;
548549
}
549550

551+
void MenuBar::_popup_changed(ObjectID p_menu) {
552+
PopupMenu *pm = Object::cast_to<PopupMenu>(ObjectDB::get_instance(p_menu));
553+
if (!pm) {
554+
return;
555+
}
556+
557+
int idx = get_menu_idx_from_control(pm);
558+
559+
String menu_name = pm->get_title().is_empty() ? String(pm->get_name()) : pm->get_title();
560+
menu_name = String(pm->get_meta("_menu_name", menu_name));
561+
562+
menu_cache.write[idx].name = menu_name;
563+
shape(menu_cache.write[idx]);
564+
565+
update_minimum_size();
566+
queue_redraw();
567+
}
568+
550569
void MenuBar::add_child_notify(Node *p_child) {
551570
Control::add_child_notify(p_child);
552571

553572
PopupMenu *pm = Object::cast_to<PopupMenu>(p_child);
554573
if (!pm) {
555574
return;
556575
}
557-
Menu menu = Menu(p_child->get_name());
576+
String menu_name = pm->get_title().is_empty() ? String(pm->get_name()) : pm->get_title();
577+
Menu menu = Menu(menu_name);
558578
shape(menu);
559579

580+
pm->connect("title_changed", callable_mp(this, &MenuBar::_popup_changed).bind(pm->get_instance_id()), CONNECT_REFERENCE_COUNTED);
581+
560582
menu_cache.push_back(menu);
561583
p_child->connect("renamed", callable_mp(this, &MenuBar::_refresh_menu_names));
562584
p_child->connect("about_to_popup", callable_mp(this, &MenuBar::_popup_visibility_changed).bind(true));
@@ -584,7 +606,8 @@ void MenuBar::move_child_notify(Node *p_child) {
584606
}
585607

586608
int old_idx = -1;
587-
String menu_name = String(pm->get_meta("_menu_name", pm->get_name()));
609+
String menu_name = pm->get_title().is_empty() ? String(pm->get_name()) : pm->get_title();
610+
menu_name = String(pm->get_meta("_menu_name", menu_name));
588611
// Find the previous menu index of the control.
589612
for (int i = 0; i < get_menu_count(); i++) {
590613
if (get_menu_title(i) == menu_name) {
@@ -640,6 +663,7 @@ void MenuBar::remove_child_notify(Node *p_child) {
640663
}
641664
}
642665

666+
pm->disconnect("title_changed", callable_mp(this, &MenuBar::_popup_changed));
643667
menu_cache.remove_at(idx);
644668

645669
p_child->remove_meta("_menu_name");
@@ -827,7 +851,8 @@ int MenuBar::get_menu_count() const {
827851
void MenuBar::set_menu_title(int p_menu, const String &p_title) {
828852
ERR_FAIL_INDEX(p_menu, menu_cache.size());
829853
PopupMenu *pm = get_menu_popup(p_menu);
830-
if (p_title == pm->get_name()) {
854+
String menu_name = pm->get_title().is_empty() ? String(pm->get_name()) : pm->get_title();
855+
if (p_title == menu_name) {
831856
pm->remove_meta("_menu_name");
832857
} else {
833858
pm->set_meta("_menu_name", p_title);

scene/gui/menu_bar.h

+2
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ class MenuBar : public Control {
135135
return -1;
136136
}
137137

138+
void _popup_changed(ObjectID p_menu);
139+
138140
void bind_global_menu();
139141
void unbind_global_menu();
140142

scene/main/window.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ void Window::set_title(const String &p_title) {
303303
}
304304
}
305305
}
306+
emit_signal("title_changed");
306307
}
307308

308309
String Window::get_title() const {
@@ -3051,6 +3052,7 @@ void Window::_bind_methods() {
30513052
ADD_SIGNAL(MethodInfo("theme_changed"));
30523053
ADD_SIGNAL(MethodInfo("dpi_changed"));
30533054
ADD_SIGNAL(MethodInfo("titlebar_changed"));
3055+
ADD_SIGNAL(MethodInfo("title_changed"));
30543056

30553057
BIND_CONSTANT(NOTIFICATION_VISIBILITY_CHANGED);
30563058
BIND_CONSTANT(NOTIFICATION_THEME_CHANGED);

0 commit comments

Comments
 (0)