godot_menu_delegate.mm 4.7 KB

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