Debian Codegraph @

platform/macos/godot_menu_delegate.mm

raw
1 /**************************************************************************/
2 /* godot_menu_delegate.mm */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #import "godot_menu_delegate.h"
32
33 #import "display_server_macos.h"
34 #import "godot_menu_item.h"
35 #import "key_mapping_macos.h"
36 #import "native_menu_macos.h"
37
38 @implementation GodotMenuDelegate
39
40 - (void)doNothing:(id)sender {
41 }
42
43 - (void)menuWillOpen:(NSMenu *)menu {
44 if (NativeMenu::get_singleton()) {
45 NativeMenuMacOS *nmenu = (NativeMenuMacOS *)NativeMenu::get_singleton();
46 nmenu->_menu_open(menu);
47 }
48 }
49
50 - (void)menuNeedsUpdate:(NSMenu *)menu {
51 if (NativeMenu::get_singleton()) {
52 NativeMenuMacOS *nmenu = (NativeMenuMacOS *)NativeMenu::get_singleton();
53 nmenu->_menu_need_update(menu);
54 }
55 }
56
57 - (void)menuDidClose:(NSMenu *)menu {
58 if (NativeMenu::get_singleton()) {
59 NativeMenuMacOS *nmenu = (NativeMenuMacOS *)NativeMenu::get_singleton();
60 nmenu->_menu_close(menu);
61 }
62 }
63
64 - (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item {
65 if (item) {
66 GodotMenuItem *value = [item representedObject];
67 if (value && value->hover_callback.is_valid()) {
68 // If custom callback is set, use it.
69 Variant ret;
70 Callable::CallError ce;
71 const Variant *args[1] = { &value->meta };
72
73 value->hover_callback.callp(args, 1, ret, ce);
74 if (ce.error != Callable::CallError::CALL_OK) {
75 ERR_PRINT(vformat("Failed to execute menu hover callback: %s.", Variant::get_callable_error_text(value->hover_callback, args, 1, ce)));
76 }
77 }
78 }
79 }
80
81 - (BOOL)menuHasKeyEquivalent:(NSMenu *)menu forEvent:(NSEvent *)event target:(id *)target action:(SEL *)action {
82 NSString *ev_key = [[event charactersIgnoringModifiers] lowercaseString];
83 NSUInteger ev_modifiers = [event modifierFlags] & NSEventModifierFlagDeviceIndependentFlagsMask;
84 for (int i = 0; i < [menu numberOfItems]; i++) {
85 const NSMenuItem *menu_item = [menu itemAtIndex:i];
86 if ([menu_item isEnabled] && [[menu_item keyEquivalent] compare:ev_key] == NSOrderedSame) {
87 NSUInteger item_modifiers = [menu_item keyEquivalentModifierMask];
88
89 if (ev_modifiers == item_modifiers) {
90 GodotMenuItem *value = [menu_item representedObject];
91 if (value) {
92 if (value->key_callback.is_valid()) {
93 // If custom callback is set, use it.
94 Variant ret;
95 Callable::CallError ce;
96 const Variant *args[1] = { &value->meta };
97
98 value->key_callback.callp(args, 1, ret, ce);
99 if (ce.error != Callable::CallError::CALL_OK) {
100 ERR_PRINT(vformat("Failed to execute menu key callback: %s.", Variant::get_callable_error_text(value->key_callback, args, 1, ce)));
101 }
102 } else {
103 // Otherwise redirect event to the engine.
104 if (DisplayServer::get_singleton()) {
105 if ([[NSApplication sharedApplication] keyWindow].sheet) {
106 [[[[NSApplication sharedApplication] keyWindow] sheetParent] sendEvent:event];
107 } else {
108 [[[NSApplication sharedApplication] keyWindow] sendEvent:event];
109 }
110 }
111 }
112
113 // Suppress default menu action.
114 *target = self;
115 *action = @selector(doNothing:);
116 }
117 return YES;
118 }
119 }
120 }
121 return NO;
122 }
123
124 @end