menu_bar.cc 7.6 KB

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