menu_delegate.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright (c) 2014 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/ui/views/menu_delegate.h"
  5. #include "atom/browser/ui/views/menu_bar.h"
  6. #include "atom/browser/ui/views/menu_model_adapter.h"
  7. #include "content/public/browser/browser_thread.h"
  8. #include "ui/views/controls/button/menu_button.h"
  9. #include "ui/views/controls/menu/menu_item_view.h"
  10. #include "ui/views/controls/menu/menu_runner.h"
  11. #include "ui/views/widget/widget.h"
  12. namespace atom {
  13. MenuDelegate::MenuDelegate(MenuBar* menu_bar)
  14. : menu_bar_(menu_bar),
  15. id_(-1) {
  16. }
  17. MenuDelegate::~MenuDelegate() {
  18. }
  19. void MenuDelegate::RunMenu(AtomMenuModel* model, views::MenuButton* button) {
  20. gfx::Point screen_loc;
  21. views::View::ConvertPointToScreen(button, &screen_loc);
  22. // Subtract 1 from the height to make the popup flush with the button border.
  23. gfx::Rect bounds(screen_loc.x(), screen_loc.y(), button->width(),
  24. button->height() - 1);
  25. id_ = button->tag();
  26. adapter_.reset(new MenuModelAdapter(model));
  27. views::MenuItemView* item = new views::MenuItemView(this);
  28. static_cast<MenuModelAdapter*>(adapter_.get())->BuildMenu(item);
  29. menu_runner_.reset(new views::MenuRunner(
  30. item,
  31. views::MenuRunner::CONTEXT_MENU | views::MenuRunner::HAS_MNEMONICS));
  32. ignore_result(menu_runner_->RunMenuAt(
  33. button->GetWidget()->GetTopLevelWidget(),
  34. button,
  35. bounds,
  36. views::MENU_ANCHOR_TOPRIGHT,
  37. ui::MENU_SOURCE_MOUSE));
  38. }
  39. void MenuDelegate::ExecuteCommand(int id) {
  40. adapter_->ExecuteCommand(id);
  41. }
  42. void MenuDelegate::ExecuteCommand(int id, int mouse_event_flags) {
  43. adapter_->ExecuteCommand(id, mouse_event_flags);
  44. }
  45. bool MenuDelegate::IsTriggerableEvent(views::MenuItemView* source,
  46. const ui::Event& e) {
  47. return adapter_->IsTriggerableEvent(source, e);
  48. }
  49. bool MenuDelegate::GetAccelerator(int id, ui::Accelerator* accelerator) const {
  50. return adapter_->GetAccelerator(id, accelerator);
  51. }
  52. base::string16 MenuDelegate::GetLabel(int id) const {
  53. return adapter_->GetLabel(id);
  54. }
  55. const gfx::FontList* MenuDelegate::GetLabelFontList(int id) const {
  56. return adapter_->GetLabelFontList(id);
  57. }
  58. bool MenuDelegate::IsCommandEnabled(int id) const {
  59. return adapter_->IsCommandEnabled(id);
  60. }
  61. bool MenuDelegate::IsCommandVisible(int id) const {
  62. return adapter_->IsCommandVisible(id);
  63. }
  64. bool MenuDelegate::IsItemChecked(int id) const {
  65. return adapter_->IsItemChecked(id);
  66. }
  67. void MenuDelegate::SelectionChanged(views::MenuItemView* menu) {
  68. adapter_->SelectionChanged(menu);
  69. }
  70. void MenuDelegate::WillShowMenu(views::MenuItemView* menu) {
  71. adapter_->WillShowMenu(menu);
  72. }
  73. void MenuDelegate::WillHideMenu(views::MenuItemView* menu) {
  74. adapter_->WillHideMenu(menu);
  75. }
  76. views::MenuItemView* MenuDelegate::GetSiblingMenu(
  77. views::MenuItemView* menu,
  78. const gfx::Point& screen_point,
  79. views::MenuAnchorPosition* anchor,
  80. bool* has_mnemonics,
  81. views::MenuButton**) {
  82. views::MenuButton* button;
  83. AtomMenuModel* model;
  84. if (menu_bar_->GetMenuButtonFromScreenPoint(screen_point, &model, &button) &&
  85. button->tag() != id_) {
  86. DCHECK(menu_runner_->IsRunning());
  87. menu_runner_->Cancel();
  88. // After canceling the menu, we need to wait until next tick
  89. // so we are out of nested message loop.
  90. content::BrowserThread::PostTask(
  91. content::BrowserThread::UI, FROM_HERE,
  92. base::Bind(base::IgnoreResult(&views::MenuButton::Activate),
  93. base::Unretained(button), nullptr));
  94. }
  95. return nullptr;
  96. }
  97. } // namespace atom