Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / toolbar / toolbar_action_view.h
blobbf19e1e6649686e70451fd8121d4b57208b5246f
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_UI_VIEWS_TOOLBAR_TOOLBAR_ACTION_VIEW_H_
6 #define CHROME_BROWSER_UI_VIEWS_TOOLBAR_TOOLBAR_ACTION_VIEW_H_
8 #include "chrome/browser/ui/views/toolbar/toolbar_action_view_delegate_views.h"
9 #include "content/public/browser/notification_observer.h"
10 #include "content/public/browser/notification_registrar.h"
11 #include "ui/views/context_menu_controller.h"
12 #include "ui/views/controls/button/menu_button.h"
13 #include "ui/views/controls/button/menu_button_listener.h"
14 #include "ui/views/drag_controller.h"
15 #include "ui/views/view.h"
17 class ExtensionAction;
18 class Profile;
20 namespace extensions {
21 class Extension;
24 namespace gfx {
25 class Image;
28 namespace views {
29 class MenuRunner;
32 ////////////////////////////////////////////////////////////////////////////////
33 // ToolbarActionView
34 // A wrapper around a ToolbarActionViewController to display a toolbar action
35 // action in the BrowserActionsContainer.
36 class ToolbarActionView : public views::MenuButton,
37 public ToolbarActionViewDelegateViews,
38 public views::ButtonListener,
39 public views::ContextMenuController,
40 public content::NotificationObserver {
41 public:
42 // Need DragController here because ToolbarActionView could be
43 // dragged/dropped.
44 class Delegate : public views::DragController {
45 public:
46 // Returns the current web contents.
47 virtual content::WebContents* GetCurrentWebContents() = 0;
49 // Whether the container for this button is shown inside a menu.
50 virtual bool ShownInsideMenu() const = 0;
52 // Notifies that a drag completed.
53 virtual void OnToolbarActionViewDragDone() = 0;
55 // Returns the view of the toolbar actions overflow menu to use as a
56 // reference point for a popup when this view isn't visible.
57 virtual views::MenuButton* GetOverflowReferenceView() = 0;
59 // Notifies the delegate that the mouse entered the view.
60 virtual void OnMouseEnteredToolbarActionView() = 0;
62 protected:
63 ~Delegate() override {}
66 ToolbarActionView(ToolbarActionViewController* view_controller,
67 Profile* profile,
68 Delegate* delegate);
69 ~ToolbarActionView() override;
71 // views::MenuButton:
72 void GetAccessibleState(ui::AXViewState* state) override;
74 // views::ButtonListener:
75 void ButtonPressed(views::Button* sender, const ui::Event& event) override;
77 // content::NotificationObserver:
78 void Observe(int type,
79 const content::NotificationSource& source,
80 const content::NotificationDetails& details) override;
82 // MenuButton behavior overrides. These methods all default to LabelButton
83 // behavior unless this button is a popup. In that case, it uses MenuButton
84 // behavior. MenuButton has the notion of a child popup being shown where the
85 // button will stay in the pushed state until the "menu" (a popup in this
86 // case) is dismissed.
87 // TODO(devlin): This is a good idea, but it has some funny UI side-effects,
88 // like the fact that label buttons enter a pressed state immediately, but
89 // menu buttons only enter a pressed state on release (if they're draggable).
90 // We should probably just pick a behavior, and stick to it.
91 bool Activate() override;
92 void OnMouseEntered(const ui::MouseEvent& event) override;
93 bool OnMousePressed(const ui::MouseEvent& event) override;
94 void OnMouseReleased(const ui::MouseEvent& event) override;
95 void OnMouseExited(const ui::MouseEvent& event) override;
96 bool OnKeyReleased(const ui::KeyEvent& event) override;
97 void OnGestureEvent(ui::GestureEvent* event) override;
98 scoped_ptr<views::LabelButtonBorder> CreateDefaultBorder() const override;
99 bool ShouldEnterPushedState(const ui::Event& event) override;
101 // ToolbarActionViewDelegate: (public because called by others).
102 void UpdateState() override;
103 content::WebContents* GetCurrentWebContents() const override;
105 ToolbarActionViewController* view_controller() {
106 return view_controller_;
109 // Returns button icon so it can be accessed during tests.
110 gfx::ImageSkia GetIconForTest();
112 bool wants_to_run_for_testing() const { return wants_to_run_; }
114 private:
115 // views::MenuButton:
116 gfx::Size GetPreferredSize() const override;
117 void OnDragDone() override;
118 void ViewHierarchyChanged(
119 const ViewHierarchyChangedDetails& details) override;
121 // ToolbarActionViewDelegateViews:
122 views::View* GetAsView() override;
123 views::FocusManager* GetFocusManagerForAccelerator() override;
124 views::View* GetReferenceViewForPopup() override;
125 bool IsMenuRunning() const override;
126 void OnPopupShown(bool by_user) override;
127 void OnPopupClosed() override;
129 // views::ContextMenuController:
130 void ShowContextMenuForView(views::View* source,
131 const gfx::Point& point,
132 ui::MenuSourceType source_type) override;
134 // Shows the context menu (if one exists) for the toolbar action.
135 void DoShowContextMenu(ui::MenuSourceType source_type);
137 // Closes the currently-active menu, if needed. This is the case when there
138 // is an active menu that wouldn't close automatically when a new one is
139 // opened.
140 // Returns true if a menu was closed, false otherwise.
141 bool CloseActiveMenuIfNeeded();
143 // Unfortunately, due to the dual-nature of a ToolbarActionView as both a
144 // label button and a menu button, activation can happen as part of either
145 // ButtonPressed() or Activate(). Handle both in this function.
146 void HandleActivation(const gfx::Point& menu_point,
147 ui::MenuSourceType source_type);
149 // A lock to keep the MenuButton pressed when a menu or popup is visible.
150 scoped_ptr<views::MenuButton::PressedLock> pressed_lock_;
152 // The controller for this toolbar action view.
153 ToolbarActionViewController* view_controller_;
155 // The associated profile.
156 Profile* profile_;
158 // Delegate that usually represents a container for ToolbarActionView.
159 Delegate* delegate_;
161 // Used to make sure we only register the command once.
162 bool called_register_command_;
164 // The cached value of whether or not the action wants to run on the current
165 // tab.
166 bool wants_to_run_;
168 // Responsible for running the menu.
169 scoped_ptr<views::MenuRunner> menu_runner_;
171 // If non-null, this is the next toolbar action context menu that wants to run
172 // once the current owner (this one) is done.
173 base::Closure followup_context_menu_task_;
175 content::NotificationRegistrar registrar_;
177 base::WeakPtrFactory<ToolbarActionView> weak_factory_;
179 DISALLOW_COPY_AND_ASSIGN(ToolbarActionView);
182 #endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_TOOLBAR_ACTION_VIEW_H_