atom_api_menu_mac.mm 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #import "atom/browser/api/atom_api_menu_mac.h"
  5. #include "atom/browser/native_window.h"
  6. #include "atom/browser/unresponsive_suppressor.h"
  7. #include "base/mac/scoped_sending_event.h"
  8. #include "base/message_loop/message_loop.h"
  9. #include "base/strings/sys_string_conversions.h"
  10. #include "content/public/browser/browser_thread.h"
  11. #include "content/public/browser/web_contents.h"
  12. #include "atom/common/node_includes.h"
  13. using content::BrowserThread;
  14. namespace {
  15. static scoped_nsobject<NSMenu> applicationMenu_;
  16. } // namespace
  17. namespace atom {
  18. namespace api {
  19. MenuMac::MenuMac(v8::Isolate* isolate, v8::Local<v8::Object> wrapper)
  20. : Menu(isolate, wrapper), weak_factory_(this) {}
  21. MenuMac::~MenuMac() = default;
  22. void MenuMac::PopupAt(TopLevelWindow* window,
  23. int x,
  24. int y,
  25. int positioning_item,
  26. const base::Closure& callback) {
  27. NativeWindow* native_window = window->window();
  28. if (!native_window)
  29. return;
  30. auto popup = base::Bind(&MenuMac::PopupOnUI, weak_factory_.GetWeakPtr(),
  31. native_window->GetWeakPtr(), window->weak_map_id(), x,
  32. y, positioning_item, callback);
  33. BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, popup);
  34. }
  35. void MenuMac::PopupOnUI(const base::WeakPtr<NativeWindow>& native_window,
  36. int32_t window_id,
  37. int x,
  38. int y,
  39. int positioning_item,
  40. base::Closure callback) {
  41. if (!native_window)
  42. return;
  43. NSWindow* nswindow = native_window->GetNativeWindow();
  44. auto close_callback = base::Bind(
  45. &MenuMac::OnClosed, weak_factory_.GetWeakPtr(), window_id, callback);
  46. popup_controllers_[window_id] = base::scoped_nsobject<AtomMenuController>([
  47. [AtomMenuController alloc] initWithModel:model()
  48. useDefaultAccelerator:NO]);
  49. NSMenu* menu = [popup_controllers_[window_id] menu];
  50. NSView* view = [nswindow contentView];
  51. // Which menu item to show.
  52. NSMenuItem* item = nil;
  53. if (positioning_item < [menu numberOfItems] && positioning_item >= 0)
  54. item = [menu itemAtIndex:positioning_item];
  55. // (-1, -1) means showing on mouse location.
  56. NSPoint position;
  57. if (x == -1 || y == -1) {
  58. position = [view convertPoint:[nswindow mouseLocationOutsideOfEventStream]
  59. fromView:nil];
  60. } else {
  61. position = NSMakePoint(x, [view frame].size.height - y);
  62. }
  63. // If no preferred item is specified, try to show all of the menu items.
  64. if (!positioning_item) {
  65. CGFloat windowBottom = CGRectGetMinY([view window].frame);
  66. CGFloat lowestMenuPoint = windowBottom + position.y - [menu size].height;
  67. CGFloat screenBottom = CGRectGetMinY([view window].screen.frame);
  68. CGFloat distanceFromBottom = lowestMenuPoint - screenBottom;
  69. if (distanceFromBottom < 0)
  70. position.y = position.y - distanceFromBottom + 4;
  71. }
  72. // Place the menu left of cursor if it is overflowing off right of screen.
  73. CGFloat windowLeft = CGRectGetMinX([view window].frame);
  74. CGFloat rightmostMenuPoint = windowLeft + position.x + [menu size].width;
  75. CGFloat screenRight = CGRectGetMaxX([view window].screen.frame);
  76. if (rightmostMenuPoint > screenRight)
  77. position.x = position.x - [menu size].width;
  78. [popup_controllers_[window_id] setCloseCallback:close_callback];
  79. // Make sure events can be pumped while the menu is up.
  80. base::MessageLoop::ScopedNestableTaskAllower allow(
  81. base::MessageLoop::current());
  82. // One of the events that could be pumped is |window.close()|.
  83. // User-initiated event-tracking loops protect against this by
  84. // setting flags in -[CrApplication sendEvent:], but since
  85. // web-content menus are initiated by IPC message the setup has to
  86. // be done manually.
  87. base::mac::ScopedSendingEvent sendingEventScoper;
  88. // Don't emit unresponsive event when showing menu.
  89. atom::UnresponsiveSuppressor suppressor;
  90. [menu popUpMenuPositioningItem:item atLocation:position inView:view];
  91. }
  92. void MenuMac::ClosePopupAt(int32_t window_id) {
  93. auto controller = popup_controllers_.find(window_id);
  94. if (controller != popup_controllers_.end()) {
  95. // Close the controller for the window.
  96. [controller->second cancel];
  97. } else if (window_id == -1) {
  98. // Or just close all opened controllers.
  99. for (auto it = popup_controllers_.begin();
  100. it != popup_controllers_.end();) {
  101. // The iterator is invalidated after the call.
  102. [(it++)->second cancel];
  103. }
  104. }
  105. }
  106. void MenuMac::OnClosed(int32_t window_id, base::Closure callback) {
  107. popup_controllers_.erase(window_id);
  108. callback.Run();
  109. }
  110. // static
  111. void Menu::SetApplicationMenu(Menu* base_menu) {
  112. MenuMac* menu = static_cast<MenuMac*>(base_menu);
  113. base::scoped_nsobject<AtomMenuController> menu_controller([
  114. [AtomMenuController alloc] initWithModel:menu->model_.get()
  115. useDefaultAccelerator:YES]);
  116. NSRunLoop* currentRunLoop = [NSRunLoop currentRunLoop];
  117. [currentRunLoop cancelPerformSelector:@selector(setMainMenu:)
  118. target:NSApp
  119. argument:applicationMenu_];
  120. applicationMenu_.reset([[menu_controller menu] retain]);
  121. [[NSRunLoop currentRunLoop]
  122. performSelector:@selector(setMainMenu:)
  123. target:NSApp
  124. argument:applicationMenu_
  125. order:0
  126. modes:[NSArray arrayWithObject:NSDefaultRunLoopMode]];
  127. // Ensure the menu_controller_ is destroyed after main menu is set.
  128. menu_controller.swap(menu->menu_controller_);
  129. }
  130. // static
  131. void Menu::SendActionToFirstResponder(const std::string& action) {
  132. SEL selector = NSSelectorFromString(base::SysUTF8ToNSString(action));
  133. [NSApp sendAction:selector to:nil from:[NSApp mainMenu]];
  134. }
  135. // static
  136. mate::WrappableBase* Menu::New(mate::Arguments* args) {
  137. return new MenuMac(args->isolate(), args->GetThis());
  138. }
  139. } // namespace api
  140. } // namespace atom