menu_bar.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 "shell/browser/ui/views/menu_bar.h"
  5. #include <memory>
  6. #include "shell/browser/native_window.h"
  7. #include "shell/browser/ui/views/submenu_button.h"
  8. #include "ui/base/mojom/menu_source_type.mojom.h"
  9. #include "ui/color/color_provider.h"
  10. #include "ui/native_theme/common_theme.h"
  11. #include "ui/views/background.h"
  12. #include "ui/views/layout/box_layout.h"
  13. #if BUILDFLAG(IS_LINUX)
  14. #include "ui/gtk/gtk_util.h" // nogncheck
  15. #endif
  16. #if BUILDFLAG(IS_WIN)
  17. #include "ui/gfx/color_utils.h"
  18. #endif
  19. namespace electron {
  20. namespace {
  21. // Default color of the menu bar.
  22. const SkColor kDefaultColor = SkColorSetARGB(255, 233, 233, 233);
  23. } // namespace
  24. MenuBar::MenuBar(NativeWindow* window, RootView* root_view)
  25. : background_color_(kDefaultColor), window_(window), root_view_(root_view) {
  26. const ui::NativeTheme* theme = root_view_->GetNativeTheme();
  27. #if BUILDFLAG(IS_WIN)
  28. SetBackground(views::CreateThemedSolidBackground(ui::kColorMenuBackground));
  29. #endif
  30. RefreshColorCache(theme);
  31. UpdateViewColors();
  32. SetFocusBehavior(FocusBehavior::ALWAYS);
  33. SetLayoutManager(std::make_unique<views::BoxLayout>(
  34. views::BoxLayout::Orientation::kHorizontal));
  35. window_->AddObserver(this);
  36. SetAccessibleName(std::u16string(),
  37. ax::mojom::NameFrom::kAttributeExplicitlyEmpty);
  38. SetAccessibleRole(ax::mojom::Role::kMenuBar);
  39. }
  40. MenuBar::~MenuBar() {
  41. window_->RemoveObserver(this);
  42. }
  43. void MenuBar::SetMenu(ElectronMenuModel* model) {
  44. menu_model_ = model;
  45. RebuildChildren();
  46. }
  47. void MenuBar::SetAcceleratorVisibility(bool visible) {
  48. for (views::View* child : GetChildrenInZOrder())
  49. static_cast<SubmenuButton*>(child)->SetAcceleratorVisibility(visible);
  50. }
  51. MenuBar::View* MenuBar::FindAccelChild(char16_t key) {
  52. if (key == 0)
  53. return nullptr;
  54. for (views::View* child : GetChildrenInZOrder()) {
  55. if (static_cast<SubmenuButton*>(child)->accelerator() == key)
  56. return child;
  57. }
  58. return nullptr;
  59. }
  60. bool MenuBar::HasAccelerator(char16_t key) {
  61. return FindAccelChild(key) != nullptr;
  62. }
  63. void MenuBar::ActivateAccelerator(char16_t key) {
  64. auto* child = FindAccelChild(key);
  65. if (child)
  66. static_cast<SubmenuButton*>(child)->Activate(nullptr);
  67. }
  68. size_t MenuBar::GetItemCount() const {
  69. return menu_model_ ? menu_model_->GetItemCount() : 0;
  70. }
  71. bool MenuBar::GetMenuButtonFromScreenPoint(const gfx::Point& screenPoint,
  72. ElectronMenuModel** menu_model,
  73. views::MenuButton** button) {
  74. if (!GetBoundsInScreen().Contains(screenPoint))
  75. return false;
  76. auto children = GetChildrenInZOrder();
  77. for (size_t i = 0, n = children.size(); i < n; ++i) {
  78. if (children[i]->GetBoundsInScreen().Contains(screenPoint) &&
  79. (menu_model_->GetTypeAt(i) == ElectronMenuModel::TYPE_SUBMENU)) {
  80. *menu_model = menu_model_->GetSubmenuModelAt(i);
  81. *button = static_cast<views::MenuButton*>(children[i]);
  82. return true;
  83. }
  84. }
  85. return false;
  86. }
  87. void MenuBar::OnBeforeExecuteCommand() {
  88. if (GetPaneFocusTraversable() != nullptr) {
  89. RemovePaneFocus();
  90. }
  91. root_view_->RestoreFocus();
  92. }
  93. void MenuBar::OnMenuClosed() {
  94. SetAcceleratorVisibility(pane_has_focus());
  95. }
  96. void MenuBar::OnWindowBlur() {
  97. UpdateViewColors();
  98. SetAcceleratorVisibility(pane_has_focus());
  99. }
  100. void MenuBar::OnWindowFocus() {
  101. UpdateViewColors();
  102. SetAcceleratorVisibility(pane_has_focus());
  103. }
  104. bool MenuBar::AcceleratorPressed(const ui::Accelerator& accelerator) {
  105. // Treat pressing Alt the same as pressing Esc.
  106. const ui::Accelerator& translated =
  107. accelerator.key_code() == ui::VKEY_MENU
  108. ? ui::Accelerator(ui::VKEY_ESCAPE, accelerator.modifiers(),
  109. accelerator.key_state(), accelerator.time_stamp())
  110. : accelerator;
  111. bool result = views::AccessiblePaneView::AcceleratorPressed(translated);
  112. if (result && !pane_has_focus())
  113. root_view_->RestoreFocus();
  114. return result;
  115. }
  116. bool MenuBar::SetPaneFocusAndFocusDefault() {
  117. bool result = views::AccessiblePaneView::SetPaneFocusAndFocusDefault();
  118. if (result && !accelerator_installed_) {
  119. // Listen to Alt key events.
  120. // Note that there is no need to unregister the accelerator.
  121. accelerator_installed_ = true;
  122. focus_manager()->RegisterAccelerator(
  123. ui::Accelerator(ui::VKEY_MENU, ui::EF_ALT_DOWN),
  124. ui::AcceleratorManager::kNormalPriority, this);
  125. }
  126. return result;
  127. }
  128. void MenuBar::OnThemeChanged() {
  129. views::AccessiblePaneView::OnThemeChanged();
  130. const ui::NativeTheme* theme = root_view_->GetNativeTheme();
  131. RefreshColorCache(theme);
  132. UpdateViewColors();
  133. }
  134. void MenuBar::OnDidChangeFocus(View* focused_before, View* focused_now) {
  135. views::AccessiblePaneView::OnDidChangeFocus(focused_before, focused_now);
  136. SetAcceleratorVisibility(pane_has_focus());
  137. if (!pane_has_focus())
  138. root_view_->RestoreFocus();
  139. }
  140. void MenuBar::ButtonPressed(size_t id, const ui::Event& event) {
  141. // Hide the accelerator when a submenu is activated.
  142. SetAcceleratorVisibility(pane_has_focus());
  143. if (!menu_model_)
  144. return;
  145. if (!root_view_->HasFocus())
  146. root_view_->RequestFocus();
  147. ElectronMenuModel::ItemType type = menu_model_->GetTypeAt(id);
  148. if (type != ElectronMenuModel::TYPE_SUBMENU) {
  149. menu_model_->ActivatedAt(id, 0);
  150. return;
  151. }
  152. SubmenuButton* source = nullptr;
  153. for (views::View* child : children()) {
  154. auto* button = static_cast<SubmenuButton*>(child);
  155. int button_id = button->GetID();
  156. if (button_id >= 0 && static_cast<size_t>(button_id) == id) {
  157. source = button;
  158. break;
  159. }
  160. }
  161. DCHECK(source);
  162. // Deleted in MenuDelegate::OnMenuClosed
  163. auto* menu_delegate = new MenuDelegate(this);
  164. menu_delegate->RunMenu(menu_model_->GetSubmenuModelAt(id), source,
  165. event.IsKeyEvent()
  166. ? ui::mojom::MenuSourceType::kKeyboard
  167. : ui::mojom::MenuSourceType::kMouse);
  168. menu_delegate->AddObserver(this);
  169. }
  170. void MenuBar::ViewHierarchyChanged(
  171. const views::ViewHierarchyChangedDetails& details) {
  172. views::AccessiblePaneView::ViewHierarchyChanged(details);
  173. #if BUILDFLAG(IS_WIN)
  174. background_color_ = GetBackground()->get_color();
  175. #endif
  176. }
  177. void MenuBar::RefreshColorCache(const ui::NativeTheme* theme) {
  178. if (theme) {
  179. #if BUILDFLAG(IS_LINUX)
  180. background_color_ = gtk::GetBgColor("GtkMenuBar#menubar");
  181. enabled_color_ =
  182. gtk::GetFgColor("GtkMenuBar#menubar GtkMenuItem#menuitem GtkLabel");
  183. disabled_color_ = gtk::GetFgColor(
  184. "GtkMenuBar#menubar GtkMenuItem#menuitem:disabled GtkLabel");
  185. #elif BUILDFLAG(IS_WIN)
  186. background_color_ = GetBackground()->get_color();
  187. #endif
  188. }
  189. }
  190. void MenuBar::RebuildChildren() {
  191. RemoveAllChildViews();
  192. for (size_t i = 0, n = GetItemCount(); i < n; ++i) {
  193. auto* button = new SubmenuButton(
  194. base::BindRepeating(&MenuBar::ButtonPressed, base::Unretained(this), i),
  195. menu_model_->GetLabelAt(i), background_color_);
  196. button->SetID(i);
  197. AddChildView(button);
  198. }
  199. UpdateViewColors();
  200. }
  201. void MenuBar::UpdateViewColors() {
  202. #if BUILDFLAG(IS_LINUX)
  203. // set menubar background color
  204. SetBackground(views::CreateSolidBackground(background_color_));
  205. #endif
  206. // set child colors
  207. if (menu_model_ == nullptr)
  208. return;
  209. #if BUILDFLAG(IS_LINUX)
  210. const auto& textColor =
  211. window_->IsFocused() ? enabled_color_ : disabled_color_;
  212. for (views::View* child : GetChildrenInZOrder()) {
  213. auto* button = static_cast<SubmenuButton*>(child);
  214. button->SetTextColor(views::Button::STATE_NORMAL, textColor);
  215. button->SetTextColor(views::Button::STATE_DISABLED, disabled_color_);
  216. button->SetTextColor(views::Button::STATE_PRESSED, enabled_color_);
  217. button->SetTextColor(views::Button::STATE_HOVERED, textColor);
  218. button->SetUnderlineColor(textColor);
  219. }
  220. #elif BUILDFLAG(IS_WIN)
  221. for (views::View* child : GetChildrenInZOrder()) {
  222. auto* button = static_cast<SubmenuButton*>(child);
  223. button->SetUnderlineColor(color_utils::GetSysSkColor(COLOR_MENUTEXT));
  224. }
  225. #endif
  226. }
  227. BEGIN_METADATA(MenuBar)
  228. END_METADATA
  229. } // namespace electron