godot_menu_delegate.mm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #include "godot_menu_delegate.h"
  31. #include "display_server_macos.h"
  32. #include "godot_menu_item.h"
  33. #include "key_mapping_macos.h"
  34. #include "native_menu_macos.h"
  35. @implementation GodotMenuDelegate
  36. - (void)doNothing:(id)sender {
  37. }
  38. - (void)menuWillOpen:(NSMenu *)menu {
  39. if (NativeMenu::get_singleton()) {
  40. NativeMenuMacOS *nmenu = (NativeMenuMacOS *)NativeMenu::get_singleton();
  41. nmenu->_menu_open(menu);
  42. }
  43. }
  44. - (void)menuNeedsUpdate:(NSMenu *)menu {
  45. if (NativeMenu::get_singleton()) {
  46. NativeMenuMacOS *nmenu = (NativeMenuMacOS *)NativeMenu::get_singleton();
  47. nmenu->_menu_need_update(menu);
  48. }
  49. }
  50. - (void)menuDidClose:(NSMenu *)menu {
  51. if (NativeMenu::get_singleton()) {
  52. NativeMenuMacOS *nmenu = (NativeMenuMacOS *)NativeMenu::get_singleton();
  53. nmenu->_menu_close(menu);
  54. }
  55. }
  56. - (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item {
  57. if (item) {
  58. GodotMenuItem *value = [item representedObject];
  59. if (value && value->hover_callback.is_valid()) {
  60. // If custom callback is set, use it.
  61. Variant ret;
  62. Callable::CallError ce;
  63. const Variant *args[1] = { &value->meta };
  64. value->hover_callback.callp(args, 1, ret, ce);
  65. if (ce.error != Callable::CallError::CALL_OK) {
  66. ERR_PRINT(vformat("Failed to execute menu hover callback: %s.", Variant::get_callable_error_text(value->hover_callback, args, 1, ce)));
  67. }
  68. }
  69. }
  70. }
  71. - (BOOL)menuHasKeyEquivalent:(NSMenu *)menu forEvent:(NSEvent *)event target:(id *)target action:(SEL *)action {
  72. NSString *ev_key = [[event charactersIgnoringModifiers] lowercaseString];
  73. NSUInteger ev_modifiers = [event modifierFlags] & NSEventModifierFlagDeviceIndependentFlagsMask;
  74. for (int i = 0; i < [menu numberOfItems]; i++) {
  75. const NSMenuItem *menu_item = [menu itemAtIndex:i];
  76. if ([menu_item isEnabled] && [[menu_item keyEquivalent] compare:ev_key] == NSOrderedSame) {
  77. NSUInteger item_modifiers = [menu_item keyEquivalentModifierMask];
  78. if (ev_modifiers == item_modifiers) {
  79. GodotMenuItem *value = [menu_item representedObject];
  80. if (value) {
  81. if (value->key_callback.is_valid()) {
  82. // If custom callback is set, use it.
  83. Variant ret;
  84. Callable::CallError ce;
  85. const Variant *args[1] = { &value->meta };
  86. value->key_callback.callp(args, 1, ret, ce);
  87. if (ce.error != Callable::CallError::CALL_OK) {
  88. ERR_PRINT(vformat("Failed to execute menu key callback: %s.", Variant::get_callable_error_text(value->key_callback, args, 1, ce)));
  89. }
  90. } else {
  91. // Otherwise redirect event to the engine.
  92. if (DisplayServer::get_singleton()) {
  93. if ([[NSApplication sharedApplication] keyWindow].sheet) {
  94. [[[[NSApplication sharedApplication] keyWindow] sheetParent] sendEvent:event];
  95. } else {
  96. [[[NSApplication sharedApplication] keyWindow] sendEvent:event];
  97. }
  98. }
  99. }
  100. // Suppress default menu action.
  101. *target = self;
  102. *action = @selector(doNothing:);
  103. }
  104. return YES;
  105. }
  106. }
  107. }
  108. return NO;
  109. }
  110. @end