menu_bar.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #ifndef ATOM_BROWSER_UI_VIEWS_MENU_BAR_H_
  5. #define ATOM_BROWSER_UI_VIEWS_MENU_BAR_H_
  6. #include <memory>
  7. #include "atom/browser/ui/atom_menu_model.h"
  8. #include "atom/browser/ui/views/menu_delegate.h"
  9. #include "atom/browser/ui/views/root_view.h"
  10. #include "ui/views/accessible_pane_view.h"
  11. #include "ui/views/controls/button/menu_button_listener.h"
  12. #include "ui/views/focus/focus_manager.h"
  13. #include "ui/views/view.h"
  14. namespace views {
  15. class Button;
  16. class MenuButton;
  17. } // namespace views
  18. namespace atom {
  19. class MenuBarColorUpdater : public views::FocusChangeListener {
  20. public:
  21. explicit MenuBarColorUpdater(MenuBar* menu_bar);
  22. ~MenuBarColorUpdater() override;
  23. void OnDidChangeFocus(views::View* focused_before,
  24. views::View* focused_now) override;
  25. void OnWillChangeFocus(views::View* focused_before,
  26. views::View* focused_now) override {}
  27. private:
  28. MenuBar* menu_bar_;
  29. };
  30. class MenuBar : public views::AccessiblePaneView,
  31. public views::MenuButtonListener,
  32. public atom::MenuDelegate::Observer {
  33. public:
  34. static const char kViewClassName[];
  35. explicit MenuBar(RootView* window);
  36. ~MenuBar() override;
  37. // Replaces current menu with a new one.
  38. void SetMenu(AtomMenuModel* menu_model);
  39. // Shows underline under accelerators.
  40. void SetAcceleratorVisibility(bool visible);
  41. // Returns true if the submenu has accelerator |key|
  42. bool HasAccelerator(base::char16 key);
  43. // Shows the submenu whose accelerator is |key|.
  44. void ActivateAccelerator(base::char16 key);
  45. // Returns there are how many items in the root menu.
  46. int GetItemCount() const;
  47. // Get the menu under specified screen point.
  48. bool GetMenuButtonFromScreenPoint(const gfx::Point& point,
  49. AtomMenuModel** menu_model,
  50. views::MenuButton** button);
  51. // atom::MenuDelegate::Observer:
  52. void OnBeforeExecuteCommand() override;
  53. void OnMenuClosed() override;
  54. // views::AccessiblePaneView:
  55. bool AcceleratorPressed(const ui::Accelerator& accelerator) override;
  56. bool SetPaneFocus(views::View* initial_focus) override;
  57. void RemovePaneFocus() override;
  58. protected:
  59. // views::View:
  60. const char* GetClassName() const override;
  61. // views::MenuButtonListener:
  62. void OnMenuButtonClicked(views::Button* source,
  63. const gfx::Point& point,
  64. const ui::Event* event) override;
  65. void OnThemeChanged() override;
  66. private:
  67. friend class MenuBarColorUpdater;
  68. void RebuildChildren();
  69. void UpdateViewColors();
  70. void RefreshColorCache();
  71. SkColor background_color_;
  72. #if defined(USE_X11)
  73. SkColor enabled_color_;
  74. SkColor disabled_color_;
  75. #endif
  76. RootView* window_ = nullptr;
  77. AtomMenuModel* menu_model_ = nullptr;
  78. View* FindAccelChild(base::char16 key);
  79. bool has_focus_ = true;
  80. std::unique_ptr<MenuBarColorUpdater> color_updater_;
  81. DISALLOW_COPY_AND_ASSIGN(MenuBar);
  82. };
  83. } // namespace atom
  84. #endif // ATOM_BROWSER_UI_VIEWS_MENU_BAR_H_