menu_bar.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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_bar.h"
  5. #include "atom/browser/ui/views/menu_delegate.h"
  6. #include "atom/browser/ui/views/submenu_button.h"
  7. #include "ui/base/models/menu_model.h"
  8. #include "ui/views/background.h"
  9. #include "ui/views/layout/box_layout.h"
  10. #if defined(OS_WIN)
  11. #include "ui/gfx/color_utils.h"
  12. #endif
  13. namespace atom {
  14. namespace {
  15. // Default color of the menu bar.
  16. const SkColor kDefaultColor = SkColorSetARGB(255, 233, 233, 233);
  17. } // namespace
  18. const char MenuBar::kViewClassName[] = "ElectronMenuBar";
  19. MenuBar::MenuBar(views::View* window)
  20. : background_color_(kDefaultColor), window_(window) {
  21. RefreshColorCache();
  22. UpdateViewColors();
  23. SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal));
  24. window_->GetFocusManager()->AddFocusChangeListener(this);
  25. }
  26. MenuBar::~MenuBar() {
  27. window_->GetFocusManager()->RemoveFocusChangeListener(this);
  28. }
  29. void MenuBar::SetMenu(AtomMenuModel* model) {
  30. menu_model_ = model;
  31. RebuildChildren();
  32. }
  33. void MenuBar::SetAcceleratorVisibility(bool visible) {
  34. for (auto* child : GetChildrenInZOrder())
  35. static_cast<SubmenuButton*>(child)->SetAcceleratorVisibility(visible);
  36. }
  37. MenuBar::View* MenuBar::FindAccelChild(base::char16 key) {
  38. for (auto* child : GetChildrenInZOrder()) {
  39. if (static_cast<SubmenuButton*>(child)->accelerator() == key)
  40. return child;
  41. }
  42. return nullptr;
  43. }
  44. bool MenuBar::HasAccelerator(base::char16 key) {
  45. return FindAccelChild(key) != nullptr;
  46. }
  47. void MenuBar::ActivateAccelerator(base::char16 key) {
  48. auto* child = FindAccelChild(key);
  49. if (child)
  50. static_cast<SubmenuButton*>(child)->Activate(nullptr);
  51. }
  52. int MenuBar::GetItemCount() const {
  53. return menu_model_ ? menu_model_->GetItemCount() : 0;
  54. }
  55. bool MenuBar::GetMenuButtonFromScreenPoint(const gfx::Point& screenPoint,
  56. AtomMenuModel** menu_model,
  57. views::MenuButton** button) {
  58. if (!GetBoundsInScreen().Contains(screenPoint))
  59. return false;
  60. auto children = GetChildrenInZOrder();
  61. for (int i = 0, n = children.size(); i < n; ++i) {
  62. if (children[i]->GetBoundsInScreen().Contains(screenPoint) &&
  63. (menu_model_->GetTypeAt(i) == AtomMenuModel::TYPE_SUBMENU)) {
  64. *menu_model = menu_model_->GetSubmenuModelAt(i);
  65. *button = static_cast<views::MenuButton*>(children[i]);
  66. return true;
  67. }
  68. }
  69. return false;
  70. }
  71. const char* MenuBar::GetClassName() const {
  72. return kViewClassName;
  73. }
  74. void MenuBar::OnMenuButtonClicked(views::MenuButton* source,
  75. const gfx::Point& point,
  76. const ui::Event* event) {
  77. // Hide the accelerator when a submenu is activated.
  78. SetAcceleratorVisibility(false);
  79. if (!menu_model_)
  80. return;
  81. if (!window_->HasFocus())
  82. window_->RequestFocus();
  83. int id = source->tag();
  84. AtomMenuModel::ItemType type = menu_model_->GetTypeAt(id);
  85. if (type != AtomMenuModel::TYPE_SUBMENU) {
  86. menu_model_->ActivatedAt(id, 0);
  87. return;
  88. }
  89. // Deleted in MenuDelegate::OnMenuClosed
  90. MenuDelegate* menu_delegate = new MenuDelegate(this);
  91. menu_delegate->RunMenu(menu_model_->GetSubmenuModelAt(id), source);
  92. }
  93. void MenuBar::RefreshColorCache(const ui::NativeTheme* theme) {
  94. if (!theme)
  95. theme = ui::NativeTheme::GetInstanceForNativeUi();
  96. if (theme) {
  97. background_color_ =
  98. theme->GetSystemColor(ui::NativeTheme::kColorId_MenuBackgroundColor);
  99. #if defined(USE_X11)
  100. enabled_color_ = theme->GetSystemColor(
  101. ui::NativeTheme::kColorId_EnabledMenuItemForegroundColor);
  102. disabled_color_ = theme->GetSystemColor(
  103. ui::NativeTheme::kColorId_DisabledMenuItemForegroundColor);
  104. #endif
  105. }
  106. #if defined(OS_WIN)
  107. background_color_ = color_utils::GetSysSkColor(COLOR_MENUBAR);
  108. #endif
  109. }
  110. void MenuBar::OnNativeThemeChanged(const ui::NativeTheme* theme) {
  111. RefreshColorCache(theme);
  112. UpdateViewColors();
  113. }
  114. void MenuBar::OnDidChangeFocus(View* focused_before, View* focused_now) {
  115. // if we've changed focus, update our view
  116. const auto had_focus = has_focus_;
  117. has_focus_ = focused_now != nullptr;
  118. if (has_focus_ != had_focus)
  119. UpdateViewColors();
  120. }
  121. void MenuBar::RebuildChildren() {
  122. RemoveAllChildViews(true);
  123. for (int i = 0, n = GetItemCount(); i < n; ++i) {
  124. auto* button =
  125. new SubmenuButton(menu_model_->GetLabelAt(i), this, background_color_);
  126. button->set_tag(i);
  127. AddChildView(button);
  128. }
  129. UpdateViewColors();
  130. }
  131. void MenuBar::UpdateViewColors() {
  132. // set menubar background color
  133. SetBackground(views::CreateSolidBackground(background_color_));
  134. // set child colors
  135. if (menu_model_ == nullptr)
  136. return;
  137. #if defined(USE_X11)
  138. const auto& textColor = has_focus_ ? enabled_color_ : disabled_color_;
  139. for (auto* child : GetChildrenInZOrder()) {
  140. auto* button = static_cast<SubmenuButton*>(child);
  141. button->SetTextColor(views::Button::STATE_NORMAL, textColor);
  142. button->SetTextColor(views::Button::STATE_DISABLED, disabled_color_);
  143. button->SetTextColor(views::Button::STATE_PRESSED, enabled_color_);
  144. button->SetTextColor(views::Button::STATE_HOVERED, textColor);
  145. button->SetUnderlineColor(textColor);
  146. }
  147. #elif defined(OS_WIN)
  148. for (auto* child : GetChildrenInZOrder()) {
  149. auto button = static_cast<SubmenuButton*>(child);
  150. button->SetUnderlineColor(color_utils::GetSysSkColor(COLOR_MENUTEXT));
  151. }
  152. #endif
  153. }
  154. } // namespace atom