Merge pull request #86322 from bruvzg/mac_global_index

[MenuBar] Store and use global menu item indices.
This commit is contained in:
Rémi Verschelde 2024-02-13 17:23:14 +01:00
commit f5cfc7f203
No known key found for this signature in database
GPG key ID: C3336907360768E1
2 changed files with 28 additions and 19 deletions

View file

@ -234,10 +234,13 @@ String MenuBar::bind_global_menu() {
String submenu_name = popups[i]->bind_global_menu();
if (!popups[i]->is_system_menu()) {
int index = ds->global_menu_add_submenu_item("_main", menu_cache[i].name, submenu_name, global_start_idx + i);
menu_cache.write[i].global_index = index;
ds->global_menu_set_item_tag("_main", index, global_menu_name + "#" + itos(i));
ds->global_menu_set_item_hidden("_main", index, menu_cache[i].hidden);
ds->global_menu_set_item_disabled("_main", index, menu_cache[i].disabled);
ds->global_menu_set_item_tooltip("_main", index, menu_cache[i].tooltip);
} else {
menu_cache.write[i].global_index = -1;
}
}
@ -250,12 +253,14 @@ void MenuBar::unbind_global_menu() {
}
DisplayServer *ds = DisplayServer::get_singleton();
int global_start = _find_global_start_index();
Vector<PopupMenu *> popups = _get_popups();
for (int i = menu_cache.size() - 1; i >= 0; i--) {
if (!popups[i]->is_system_menu()) {
popups[i]->unbind_global_menu();
ds->global_menu_remove_item("_main", global_start + i);
if (menu_cache[i].global_index >= 0) {
ds->global_menu_remove_item("_main", menu_cache[i].global_index);
}
menu_cache.write[i].global_index = -1;
}
}
@ -283,11 +288,10 @@ void MenuBar::_notification(int p_what) {
case NOTIFICATION_TRANSLATION_CHANGED: {
DisplayServer *ds = DisplayServer::get_singleton();
bool is_global = !global_menu_name.is_empty();
int global_start = _find_global_start_index();
for (int i = 0; i < menu_cache.size(); i++) {
shape(menu_cache.write[i]);
if (is_global) {
ds->global_menu_set_item_text("_main", global_start + i, atr(menu_cache[i].name));
if (is_global && menu_cache[i].global_index >= 0) {
ds->global_menu_set_item_text("_main", menu_cache[i].global_index, atr(menu_cache[i].name));
}
}
} break;
@ -487,15 +491,14 @@ void MenuBar::shape(Menu &p_menu) {
void MenuBar::_refresh_menu_names() {
DisplayServer *ds = DisplayServer::get_singleton();
bool is_global = !global_menu_name.is_empty();
int global_start = _find_global_start_index();
Vector<PopupMenu *> 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();
shape(menu_cache.write[i]);
if (is_global) {
ds->global_menu_set_item_text("_main", global_start + i, atr(menu_cache[i].name));
if (is_global && menu_cache[i].global_index >= 0) {
ds->global_menu_set_item_text("_main", menu_cache[i].global_index, atr(menu_cache[i].name));
}
}
}
@ -546,6 +549,7 @@ void MenuBar::add_child_notify(Node *p_child) {
String submenu_name = pm->bind_global_menu();
if (!pm->is_system_menu()) {
int index = DisplayServer::get_singleton()->global_menu_add_submenu_item("_main", atr(menu.name), submenu_name, _find_global_start_index() + menu_cache.size() - 1);
menu_cache.write[menu_cache.size() - 1].global_index = index;
DisplayServer::get_singleton()->global_menu_set_item_tag("_main", index, global_menu_name + "#" + itos(menu_cache.size() - 1));
}
}
@ -577,12 +581,13 @@ void MenuBar::move_child_notify(Node *p_child) {
if (!global_menu_name.is_empty()) {
if (!pm->is_system_menu()) {
int global_start = _find_global_start_index();
if (old_idx != -1) {
DisplayServer::get_singleton()->global_menu_remove_item("_main", global_start + old_idx);
if (menu.global_index >= 0) {
DisplayServer::get_singleton()->global_menu_remove_item("_main", menu.global_index);
}
if (new_idx != -1) {
String submenu_name = pm->bind_global_menu();
int index = DisplayServer::get_singleton()->global_menu_add_submenu_item("_main", atr(menu.name), submenu_name, global_start + new_idx);
menu_cache.write[new_idx].global_index = index;
DisplayServer::get_singleton()->global_menu_set_item_tag("_main", index, global_menu_name + "#" + itos(new_idx));
}
}
@ -604,7 +609,10 @@ void MenuBar::remove_child_notify(Node *p_child) {
if (!global_menu_name.is_empty()) {
if (!pm->is_system_menu()) {
pm->unbind_global_menu();
DisplayServer::get_singleton()->global_menu_remove_item("_main", _find_global_start_index() + idx);
if (menu_cache[idx].global_index >= 0) {
DisplayServer::get_singleton()->global_menu_remove_item("_main", menu_cache[idx].global_index);
menu_cache.write[idx].global_index = -1;
}
}
}
@ -800,8 +808,8 @@ void MenuBar::set_menu_title(int p_menu, const String &p_title) {
}
menu_cache.write[p_menu].name = p_title;
shape(menu_cache.write[p_menu]);
if (!global_menu_name.is_empty()) {
DisplayServer::get_singleton()->global_menu_set_item_text("_main", _find_global_start_index() + p_menu, atr(menu_cache[p_menu].name));
if (!global_menu_name.is_empty() && menu_cache[p_menu].global_index >= 0) {
DisplayServer::get_singleton()->global_menu_set_item_text("_main", menu_cache[p_menu].global_index, atr(menu_cache[p_menu].name));
}
update_minimum_size();
}
@ -816,8 +824,8 @@ void MenuBar::set_menu_tooltip(int p_menu, const String &p_tooltip) {
PopupMenu *pm = get_menu_popup(p_menu);
pm->set_meta("_menu_tooltip", p_tooltip);
menu_cache.write[p_menu].tooltip = p_tooltip;
if (!global_menu_name.is_empty()) {
DisplayServer::get_singleton()->global_menu_set_item_tooltip("_main", _find_global_start_index() + p_menu, p_tooltip);
if (!global_menu_name.is_empty() && menu_cache[p_menu].global_index >= 0) {
DisplayServer::get_singleton()->global_menu_set_item_tooltip("_main", menu_cache[p_menu].global_index, p_tooltip);
}
}
@ -829,8 +837,8 @@ String MenuBar::get_menu_tooltip(int p_menu) const {
void MenuBar::set_menu_disabled(int p_menu, bool p_disabled) {
ERR_FAIL_INDEX(p_menu, menu_cache.size());
menu_cache.write[p_menu].disabled = p_disabled;
if (!global_menu_name.is_empty()) {
DisplayServer::get_singleton()->global_menu_set_item_disabled("_main", _find_global_start_index() + p_menu, p_disabled);
if (!global_menu_name.is_empty() && menu_cache[p_menu].global_index >= 0) {
DisplayServer::get_singleton()->global_menu_set_item_disabled("_main", menu_cache[p_menu].global_index, p_disabled);
}
}
@ -842,8 +850,8 @@ bool MenuBar::is_menu_disabled(int p_menu) const {
void MenuBar::set_menu_hidden(int p_menu, bool p_hidden) {
ERR_FAIL_INDEX(p_menu, menu_cache.size());
menu_cache.write[p_menu].hidden = p_hidden;
if (!global_menu_name.is_empty()) {
DisplayServer::get_singleton()->global_menu_set_item_hidden("_main", _find_global_start_index() + p_menu, p_hidden);
if (!global_menu_name.is_empty() && menu_cache[p_menu].global_index >= 0) {
DisplayServer::get_singleton()->global_menu_set_item_hidden("_main", menu_cache[p_menu].global_index, p_hidden);
}
update_minimum_size();
}

View file

@ -55,6 +55,7 @@ class MenuBar : public Control {
Ref<TextLine> text_buf;
bool hidden = false;
bool disabled = false;
int global_index = -1;
Menu(const String &p_name) {
name = p_name;