From 9604e98a5264f079106fc4f7e089956e639c06f1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pa=CC=84vels=20Nadtoc=CC=8Cajevs?=
<7645683+bruvzg@users.noreply.github.com>
Date: Fri, 20 Dec 2024 23:35:07 +0200
Subject: [PATCH] [MenuBar] Use PopupMenu title property as a menu name.
---
doc/classes/MenuBar.xml | 2 +-
doc/classes/Window.xml | 5 +++++
scene/gui/menu_bar.cpp | 35 ++++++++++++++++++++++++++++++-----
scene/gui/menu_bar.h | 2 ++
scene/main/window.cpp | 2 ++
5 files changed, 40 insertions(+), 6 deletions(-)
diff --git a/doc/classes/MenuBar.xml b/doc/classes/MenuBar.xml
index bcf63f061038..d2303622bfa2 100644
--- a/doc/classes/MenuBar.xml
+++ b/doc/classes/MenuBar.xml
@@ -4,7 +4,7 @@
A horizontal menu bar that creates a menu for each [PopupMenu] child.
- A horizontal menu bar that creates a menu for each [PopupMenu] child. New items are created by adding [PopupMenu]s to this node.
+ 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].
diff --git a/doc/classes/Window.xml b/doc/classes/Window.xml
index f779da8af9dc..f9b3a8944bf8 100644
--- a/doc/classes/Window.xml
+++ b/doc/classes/Window.xml
@@ -768,6 +768,11 @@
Emitted when the [constant NOTIFICATION_THEME_CHANGED] notification is sent.
+
+
+ Emitted when window title bar text is changed.
+
+
Emitted when window title bar decorations are changed, e.g. macOS window enter/exit full screen mode, or extend-to-title flag is changed.
diff --git a/scene/gui/menu_bar.cpp b/scene/gui/menu_bar.cpp
index 9853d699d40b..0c5e42c4c82b 100644
--- a/scene/gui/menu_bar.cpp
+++ b/scene/gui/menu_bar.cpp
@@ -507,8 +507,9 @@ void MenuBar::_refresh_menu_names() {
Vector popups = _get_popups();
for (int i = 0; i < popups.size(); i++) {
- if (!popups[i]->has_meta("_menu_name") && String(popups[i]->get_name()) != get_menu_title(i)) {
- menu_cache.write[i].name = popups[i]->get_name();
+ String menu_name = popups[i]->get_title().is_empty() ? String(popups[i]->get_name()) : popups[i]->get_title();
+ if (!popups[i]->has_meta("_menu_name") && menu_name != get_menu_title(i)) {
+ menu_cache.write[i].name = menu_name;
shape(menu_cache.write[i]);
queue_redraw();
if (is_global && menu_cache[i].submenu_rid.is_valid()) {
@@ -547,6 +548,24 @@ int MenuBar::get_menu_idx_from_control(PopupMenu *p_child) const {
return -1;
}
+void MenuBar::_popup_changed(ObjectID p_menu) {
+ PopupMenu *pm = Object::cast_to(ObjectDB::get_instance(p_menu));
+ if (!pm) {
+ return;
+ }
+
+ int idx = get_menu_idx_from_control(pm);
+
+ String menu_name = pm->get_title().is_empty() ? String(pm->get_name()) : pm->get_title();
+ menu_name = String(pm->get_meta("_menu_name", menu_name));
+
+ menu_cache.write[idx].name = menu_name;
+ shape(menu_cache.write[idx]);
+
+ update_minimum_size();
+ queue_redraw();
+}
+
void MenuBar::add_child_notify(Node *p_child) {
Control::add_child_notify(p_child);
@@ -554,9 +573,12 @@ void MenuBar::add_child_notify(Node *p_child) {
if (!pm) {
return;
}
- Menu menu = Menu(p_child->get_name());
+ String menu_name = pm->get_title().is_empty() ? String(pm->get_name()) : pm->get_title();
+ Menu menu = Menu(menu_name);
shape(menu);
+ pm->connect("title_changed", callable_mp(this, &MenuBar::_popup_changed).bind(pm->get_instance_id()), CONNECT_REFERENCE_COUNTED);
+
menu_cache.push_back(menu);
p_child->connect("renamed", callable_mp(this, &MenuBar::_refresh_menu_names));
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) {
}
int old_idx = -1;
- String menu_name = String(pm->get_meta("_menu_name", pm->get_name()));
+ String menu_name = pm->get_title().is_empty() ? String(pm->get_name()) : pm->get_title();
+ menu_name = String(pm->get_meta("_menu_name", menu_name));
// Find the previous menu index of the control.
for (int i = 0; i < get_menu_count(); i++) {
if (get_menu_title(i) == menu_name) {
@@ -640,6 +663,7 @@ void MenuBar::remove_child_notify(Node *p_child) {
}
}
+ pm->disconnect("title_changed", callable_mp(this, &MenuBar::_popup_changed));
menu_cache.remove_at(idx);
p_child->remove_meta("_menu_name");
@@ -827,7 +851,8 @@ int MenuBar::get_menu_count() const {
void MenuBar::set_menu_title(int p_menu, const String &p_title) {
ERR_FAIL_INDEX(p_menu, menu_cache.size());
PopupMenu *pm = get_menu_popup(p_menu);
- if (p_title == pm->get_name()) {
+ String menu_name = pm->get_title().is_empty() ? String(pm->get_name()) : pm->get_title();
+ if (p_title == menu_name) {
pm->remove_meta("_menu_name");
} else {
pm->set_meta("_menu_name", p_title);
diff --git a/scene/gui/menu_bar.h b/scene/gui/menu_bar.h
index 04f6afc2fa7d..c9f2550cc732 100644
--- a/scene/gui/menu_bar.h
+++ b/scene/gui/menu_bar.h
@@ -135,6 +135,8 @@ class MenuBar : public Control {
return -1;
}
+ void _popup_changed(ObjectID p_menu);
+
void bind_global_menu();
void unbind_global_menu();
diff --git a/scene/main/window.cpp b/scene/main/window.cpp
index 154d4d199d8a..28fea61e5a83 100644
--- a/scene/main/window.cpp
+++ b/scene/main/window.cpp
@@ -303,6 +303,7 @@ void Window::set_title(const String &p_title) {
}
}
}
+ emit_signal("title_changed");
}
String Window::get_title() const {
@@ -3044,6 +3045,7 @@ void Window::_bind_methods() {
ADD_SIGNAL(MethodInfo("theme_changed"));
ADD_SIGNAL(MethodInfo("dpi_changed"));
ADD_SIGNAL(MethodInfo("titlebar_changed"));
+ ADD_SIGNAL(MethodInfo("title_changed"));
BIND_CONSTANT(NOTIFICATION_VISIBILITY_CHANGED);
BIND_CONSTANT(NOTIFICATION_THEME_CHANGED);