Skip to content

Commit

Permalink
Merge pull request #79361 from bruvzg/mac_gl_menu_signals
Browse files Browse the repository at this point in the history
[macOS] Add `about_to_open` and `popup_hide` callback for the global menus.
  • Loading branch information
akien-mga committed Oct 4, 2023
2 parents 7a0fc7e + d1aaa91 commit 425b000
Show file tree
Hide file tree
Showing 11 changed files with 877 additions and 130 deletions.
40 changes: 40 additions & 0 deletions doc/classes/DisplayServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,16 @@
[b]Note:[/b] This method is implemented only on macOS.
</description>
</method>
<method name="global_menu_is_item_hidden" qualifiers="const">
<return type="bool" />
<param index="0" name="menu_root" type="String" />
<param index="1" name="idx" type="int" />
<description>
Returns [code]true[/code] if the item at index [param idx] is hidden.
See [method global_menu_set_item_hidden] for more info on how to hide an item.
[b]Note:[/b] This method is implemented only on macOS.
</description>
</method>
<method name="global_menu_is_item_radio_checkable" qualifiers="const">
<return type="bool" />
<param index="0" name="menu_root" type="String" />
Expand Down Expand Up @@ -648,6 +658,27 @@
[b]Note:[/b] This method is implemented only on macOS.
</description>
</method>
<method name="global_menu_set_item_hidden">
<return type="void" />
<param index="0" name="menu_root" type="String" />
<param index="1" name="idx" type="int" />
<param index="2" name="hidden" type="bool" />
<description>
Hides/shows the item at index [param idx]. When it is hidden, an item does not appear in a menu and its action cannot be invoked.
[b]Note:[/b] This method is implemented only on macOS.
</description>
</method>
<method name="global_menu_set_item_hover_callbacks">
<return type="void" />
<param index="0" name="menu_root" type="String" />
<param index="1" name="idx" type="int" />
<param index="2" name="callback" type="Callable" />
<description>
Sets the callback of the item at index [param idx]. The callback is emitted when an item is hovered.
[b]Note:[/b] The [param callback] Callable needs to accept exactly one Variant parameter, the parameter passed to the Callable will be the value passed to the [code]tag[/code] parameter when the menu item was created.
[b]Note:[/b] This method is implemented only on macOS.
</description>
</method>
<method name="global_menu_set_item_icon">
<return type="void" />
<param index="0" name="menu_root" type="String" />
Expand Down Expand Up @@ -751,6 +782,15 @@
[b]Note:[/b] This method is implemented only on macOS.
</description>
</method>
<method name="global_menu_set_popup_callbacks">
<return type="void" />
<param index="0" name="menu_root" type="String" />
<param index="1" name="open_callback" type="Callable" />
<param index="2" name="close_callback" type="Callable" />
<description>
Registers callables to emit when the menu is respectively about to show or closed.
</description>
</method>
<method name="has_feature" qualifiers="const">
<return type="bool" />
<param index="0" name="feature" type="int" enum="DisplayServer.Feature" />
Expand Down
17 changes: 16 additions & 1 deletion platform/macos/display_server_macos.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,14 @@ class DisplayServerMacOS : public DisplayServer {

NSMenu *apple_menu = nullptr;
NSMenu *dock_menu = nullptr;
HashMap<String, NSMenu *> submenu;
struct MenuData {
Callable open;
Callable close;
NSMenu *menu = nullptr;
bool is_open = false;
};
HashMap<String, MenuData> submenu;
HashMap<NSMenu *, String> submenu_inv;

struct WarpEvent {
NSTimeInterval timestamp;
Expand Down Expand Up @@ -197,6 +204,7 @@ class DisplayServerMacOS : public DisplayServer {

const NSMenu *_get_menu_root(const String &p_menu_root) const;
NSMenu *_get_menu_root(const String &p_menu_root);
bool _is_menu_opened(NSMenu *p_menu) const;

WindowID _create_window(WindowMode p_mode, VSyncMode p_vsync_mode, const Rect2i &p_rect);
void _update_window_style(WindowData p_wd);
Expand All @@ -223,6 +231,8 @@ class DisplayServerMacOS : public DisplayServer {
public:
NSMenu *get_dock_menu() const;
void menu_callback(id p_sender);
void menu_open(NSMenu *p_menu);
void menu_close(NSMenu *p_menu);

bool has_window(WindowID p_window) const;
WindowData &get_window(WindowID p_window);
Expand Down Expand Up @@ -254,6 +264,8 @@ class DisplayServerMacOS : public DisplayServer {
virtual bool has_feature(Feature p_feature) const override;
virtual String get_name() const override;

virtual void global_menu_set_popup_callbacks(const String &p_menu_root, const Callable &p_open_callback = Callable(), const Callable &p_close_callback = Callable()) override;

virtual int global_menu_add_submenu_item(const String &p_menu_root, const String &p_label, const String &p_submenu, int p_index = -1) override;
virtual int global_menu_add_item(const String &p_menu_root, const String &p_label, const Callable &p_callback = Callable(), const Callable &p_key_callback = Callable(), const Variant &p_tag = Variant(), Key p_accel = Key::NONE, int p_index = -1) override;
virtual int global_menu_add_check_item(const String &p_menu_root, const String &p_label, const Callable &p_callback = Callable(), const Callable &p_key_callback = Callable(), const Variant &p_tag = Variant(), Key p_accel = Key::NONE, int p_index = -1) override;
Expand All @@ -277,6 +289,7 @@ class DisplayServerMacOS : public DisplayServer {
virtual String global_menu_get_item_submenu(const String &p_menu_root, int p_idx) const override;
virtual Key global_menu_get_item_accelerator(const String &p_menu_root, int p_idx) const override;
virtual bool global_menu_is_item_disabled(const String &p_menu_root, int p_idx) const override;
virtual bool global_menu_is_item_hidden(const String &p_menu_root, int p_idx) const override;
virtual String global_menu_get_item_tooltip(const String &p_menu_root, int p_idx) const override;
virtual int global_menu_get_item_state(const String &p_menu_root, int p_idx) const override;
virtual int global_menu_get_item_max_states(const String &p_menu_root, int p_idx) const override;
Expand All @@ -288,11 +301,13 @@ class DisplayServerMacOS : public DisplayServer {
virtual void global_menu_set_item_radio_checkable(const String &p_menu_root, int p_idx, bool p_checkable) override;
virtual void global_menu_set_item_callback(const String &p_menu_root, int p_idx, const Callable &p_callback) override;
virtual void global_menu_set_item_key_callback(const String &p_menu_root, int p_idx, const Callable &p_key_callback) override;
virtual void global_menu_set_item_hover_callbacks(const String &p_menu_root, int p_idx, const Callable &p_callback) override;
virtual void global_menu_set_item_tag(const String &p_menu_root, int p_idx, const Variant &p_tag) override;
virtual void global_menu_set_item_text(const String &p_menu_root, int p_idx, const String &p_text) override;
virtual void global_menu_set_item_submenu(const String &p_menu_root, int p_idx, const String &p_submenu) override;
virtual void global_menu_set_item_accelerator(const String &p_menu_root, int p_idx, Key p_keycode) override;
virtual void global_menu_set_item_disabled(const String &p_menu_root, int p_idx, bool p_disabled) override;
virtual void global_menu_set_item_hidden(const String &p_menu_root, int p_idx, bool p_hidden) override;
virtual void global_menu_set_item_tooltip(const String &p_menu_root, int p_idx, const String &p_tooltip) override;
virtual void global_menu_set_item_state(const String &p_menu_root, int p_idx, int p_state) override;
virtual void global_menu_set_item_max_states(const String &p_menu_root, int p_idx, int p_max_states) override;
Expand Down
Loading

0 comments on commit 425b000

Please sign in to comment.