[NativeMenu] Add checks to avoid unnecessary warnings.

Added by Rémi: Add dummy NativeMenu to DisplayServerHeadless,
fixing crashes when using `--headless`.

Co-authored-by: Rémi Verschelde <rverschelde@gmail.com>
This commit is contained in:
bruvzg 2024-03-06 20:29:31 +02:00 committed by Rémi Verschelde
parent 9b94c80e9a
commit da02ca7e75
No known key found for this signature in database
GPG Key ID: C3336907360768E1
3 changed files with 17 additions and 4 deletions

View File

@ -1590,6 +1590,10 @@
<member name="Marshalls" type="Marshalls" setter="" getter=""> <member name="Marshalls" type="Marshalls" setter="" getter="">
The [Marshalls] singleton. The [Marshalls] singleton.
</member> </member>
<member name="NativeMenu" type="NativeMenu" setter="" getter="">
The [NativeMenu] singleton.
[b]Note:[/b] Only implemented on macOS.
</member>
<member name="NavigationMeshGenerator" type="NavigationMeshGenerator" setter="" getter=""> <member name="NavigationMeshGenerator" type="NavigationMeshGenerator" setter="" getter="">
The [NavigationMeshGenerator] singleton. The [NavigationMeshGenerator] singleton.
</member> </member>

View File

@ -288,8 +288,8 @@ void MenuBar::_notification(int p_what) {
} break; } break;
case NOTIFICATION_TRANSLATION_CHANGED: { case NOTIFICATION_TRANSLATION_CHANGED: {
NativeMenu *nmenu = NativeMenu::get_singleton(); NativeMenu *nmenu = NativeMenu::get_singleton();
RID main_menu = nmenu->get_system_menu(NativeMenu::MAIN_MENU_ID);
bool is_global = !global_menu_tag.is_empty(); bool is_global = !global_menu_tag.is_empty();
RID main_menu = is_global ? nmenu->get_system_menu(NativeMenu::MAIN_MENU_ID) : RID();
for (int i = 0; i < menu_cache.size(); i++) { for (int i = 0; i < menu_cache.size(); i++) {
shape(menu_cache.write[i]); shape(menu_cache.write[i]);
if (is_global && menu_cache[i].global_index >= 0) { if (is_global && menu_cache[i].global_index >= 0) {
@ -492,8 +492,8 @@ void MenuBar::shape(Menu &p_menu) {
void MenuBar::_refresh_menu_names() { void MenuBar::_refresh_menu_names() {
NativeMenu *nmenu = NativeMenu::get_singleton(); NativeMenu *nmenu = NativeMenu::get_singleton();
RID main_menu = nmenu->get_system_menu(NativeMenu::MAIN_MENU_ID);
bool is_global = !global_menu_tag.is_empty(); bool is_global = !global_menu_tag.is_empty();
RID main_menu = is_global ? nmenu->get_system_menu(NativeMenu::MAIN_MENU_ID) : RID();
Vector<PopupMenu *> popups = _get_popups(); Vector<PopupMenu *> popups = _get_popups();
for (int i = 0; i < popups.size(); i++) { for (int i = 0; i < popups.size(); i++) {

View File

@ -51,6 +51,8 @@ private:
return memnew(DisplayServerHeadless()); return memnew(DisplayServerHeadless());
} }
NativeMenu *native_menu = nullptr;
public: public:
bool has_feature(Feature p_feature) const override { return false; } bool has_feature(Feature p_feature) const override { return false; }
String get_name() const override { return "headless"; } String get_name() const override { return "headless"; }
@ -132,8 +134,15 @@ public:
void set_icon(const Ref<Image> &p_icon) override {} void set_icon(const Ref<Image> &p_icon) override {}
DisplayServerHeadless() {} DisplayServerHeadless() {
~DisplayServerHeadless() {} native_menu = memnew(NativeMenu);
}
~DisplayServerHeadless() {
if (native_menu) {
memdelete(native_menu);
native_menu = nullptr;
}
}
}; };
#endif // DISPLAY_SERVER_HEADLESS_H #endif // DISPLAY_SERVER_HEADLESS_H