menu_bar.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #if defined(USE_X11)
  6. #include "gtk/gtk.h"
  7. #endif
  8. #include "atom/browser/ui/views/menu_delegate.h"
  9. #include "atom/browser/ui/views/submenu_button.h"
  10. #include "ui/base/models/menu_model.h"
  11. #include "ui/views/background.h"
  12. #include "ui/views/layout/box_layout.h"
  13. #if defined(OS_WIN)
  14. #include "ui/gfx/color_utils.h"
  15. #elif defined(USE_X11)
  16. #include "chrome/browser/ui/libgtk2ui/skia_utils_gtk2.h"
  17. #endif
  18. namespace atom {
  19. namespace {
  20. const char kViewClassName[] = "ElectronMenuBar";
  21. // Default color of the menu bar.
  22. const SkColor kDefaultColor = SkColorSetARGB(255, 233, 233, 233);
  23. #if defined(USE_X11)
  24. void GetMenuBarColor(SkColor* enabled, SkColor* disabled, SkColor* highlight,
  25. SkColor* hover, SkColor* background) {
  26. GtkWidget* menu_bar = gtk_menu_bar_new();
  27. GtkStyle* style = gtk_rc_get_style(menu_bar);
  28. *enabled = libgtk2ui::GdkColorToSkColor(style->fg[GTK_STATE_NORMAL]);
  29. *disabled = libgtk2ui::GdkColorToSkColor(style->fg[GTK_STATE_INSENSITIVE]);
  30. *highlight = libgtk2ui::GdkColorToSkColor(style->fg[GTK_STATE_SELECTED]);
  31. *hover = libgtk2ui::GdkColorToSkColor(style->fg[GTK_STATE_PRELIGHT]);
  32. *background = libgtk2ui::GdkColorToSkColor(style->bg[GTK_STATE_NORMAL]);
  33. gtk_widget_destroy(menu_bar);
  34. }
  35. #endif
  36. } // namespace
  37. MenuBar::MenuBar(NativeWindow* window)
  38. : background_color_(kDefaultColor),
  39. menu_model_(NULL),
  40. window_(window) {
  41. UpdateMenuBarColor();
  42. SetLayoutManager(new views::BoxLayout(
  43. views::BoxLayout::kHorizontal, 0, 0, 0));
  44. }
  45. MenuBar::~MenuBar() {
  46. }
  47. void MenuBar::SetMenu(AtomMenuModel* model) {
  48. menu_model_ = model;
  49. RemoveAllChildViews(true);
  50. for (int i = 0; i < model->GetItemCount(); ++i) {
  51. SubmenuButton* button = new SubmenuButton(model->GetLabelAt(i),
  52. this,
  53. background_color_);
  54. button->set_tag(i);
  55. #if defined(USE_X11)
  56. button->SetTextColor(views::Button::STATE_NORMAL, enabled_color_);
  57. button->SetTextColor(views::Button::STATE_DISABLED, disabled_color_);
  58. button->SetTextColor(views::Button::STATE_PRESSED, highlight_color_);
  59. button->SetTextColor(views::Button::STATE_HOVERED, hover_color_);
  60. button->SetUnderlineColor(enabled_color_);
  61. #elif defined(OS_WIN)
  62. button->SetUnderlineColor(color_utils::GetSysSkColor(COLOR_GRAYTEXT));
  63. #endif
  64. AddChildView(button);
  65. }
  66. }
  67. void MenuBar::SetAcceleratorVisibility(bool visible) {
  68. for (int i = 0; i < child_count(); ++i)
  69. static_cast<SubmenuButton*>(child_at(i))->SetAcceleratorVisibility(visible);
  70. }
  71. int MenuBar::GetAcceleratorIndex(base::char16 key) {
  72. for (int i = 0; i < child_count(); ++i) {
  73. SubmenuButton* button = static_cast<SubmenuButton*>(child_at(i));
  74. if (button->accelerator() == key)
  75. return i;
  76. }
  77. return -1;
  78. }
  79. void MenuBar::ActivateAccelerator(base::char16 key) {
  80. int i = GetAcceleratorIndex(key);
  81. if (i != -1)
  82. static_cast<SubmenuButton*>(child_at(i))->Activate(nullptr);
  83. }
  84. int MenuBar::GetItemCount() const {
  85. return menu_model_->GetItemCount();
  86. }
  87. bool MenuBar::GetMenuButtonFromScreenPoint(const gfx::Point& point,
  88. AtomMenuModel** menu_model,
  89. views::MenuButton** button) {
  90. gfx::Point location(point);
  91. views::View::ConvertPointFromScreen(this, &location);
  92. if (location.x() < 0 || location.x() >= width() || location.y() < 0 ||
  93. location.y() >= height())
  94. return false;
  95. for (int i = 0; i < child_count(); ++i) {
  96. views::View* view = child_at(i);
  97. if (view->bounds().Contains(location) &&
  98. (menu_model_->GetTypeAt(i) == AtomMenuModel::TYPE_SUBMENU)) {
  99. *menu_model = menu_model_->GetSubmenuModelAt(i);
  100. *button = static_cast<views::MenuButton*>(view);
  101. return true;
  102. }
  103. }
  104. return false;
  105. }
  106. const char* MenuBar::GetClassName() const {
  107. return kViewClassName;
  108. }
  109. void MenuBar::OnMenuButtonClicked(views::MenuButton* source,
  110. const gfx::Point& point,
  111. const ui::Event* event) {
  112. // Hide the accelerator when a submenu is activated.
  113. SetAcceleratorVisibility(false);
  114. if (!menu_model_)
  115. return;
  116. if (!window_->IsFocused())
  117. window_->Focus(true);
  118. int id = source->tag();
  119. AtomMenuModel::ItemType type = menu_model_->GetTypeAt(id);
  120. if (type != AtomMenuModel::TYPE_SUBMENU) {
  121. menu_model_->ActivatedAt(id, 0);
  122. return;
  123. }
  124. MenuDelegate menu_delegate(this);
  125. menu_delegate.RunMenu(menu_model_->GetSubmenuModelAt(id), source);
  126. }
  127. void MenuBar::OnNativeThemeChanged(const ui::NativeTheme* theme) {
  128. UpdateMenuBarColor();
  129. }
  130. void MenuBar::UpdateMenuBarColor() {
  131. #if defined(OS_WIN)
  132. background_color_ = color_utils::GetSysSkColor(COLOR_MENUBAR);
  133. #elif defined(USE_X11)
  134. GetMenuBarColor(&enabled_color_, &disabled_color_, &highlight_color_,
  135. &hover_color_, &background_color_);
  136. #endif
  137. set_background(views::Background::CreateSolidBackground(background_color_));
  138. }
  139. } // namespace atom