blob: d9284890cb0c805dc7c24752bfa3b1deac8e14a5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_VIEWS_TOOLBAR_TOOLBAR_ACTION_VIEW_H_
#define CHROME_BROWSER_UI_VIEWS_TOOLBAR_TOOLBAR_ACTION_VIEW_H_
#include "base/callback.h"
#include "base/macros.h"
#include "chrome/browser/ui/views/toolbar/toolbar_action_view_delegate_views.h"
#include "ui/views/context_menu_controller.h"
#include "ui/views/controls/button/menu_button.h"
#include "ui/views/controls/button/menu_button_listener.h"
#include "ui/views/controls/menu/menu_model_adapter.h"
#include "ui/views/drag_controller.h"
#include "ui/views/view.h"
class ExtensionAction;
namespace extensions {
class Extension;
}
namespace gfx {
class Image;
}
namespace views {
class MenuItemView;
class MenuModelAdapter;
class MenuRunner;
}
////////////////////////////////////////////////////////////////////////////////
// ToolbarActionView
// A wrapper around a ToolbarActionViewController to display a toolbar action
// action in the BrowserActionsContainer.
class ToolbarActionView : public views::MenuButton,
public ToolbarActionViewDelegateViews,
public views::MenuButtonListener,
public views::ContextMenuController {
public:
// Need DragController here because ToolbarActionView could be
// dragged/dropped.
class Delegate : public views::DragController {
public:
// Returns the current web contents.
virtual content::WebContents* GetCurrentWebContents() = 0;
// Whether the container for this button is shown inside a menu.
virtual bool ShownInsideMenu() const = 0;
// Notifies that a drag completed.
virtual void OnToolbarActionViewDragDone() = 0;
// Returns the view of the toolbar actions overflow menu to use as a
// reference point for a popup when this view isn't visible.
virtual views::MenuButton* GetOverflowReferenceView() = 0;
// Notifies the delegate that the mouse entered the view.
virtual void OnMouseEnteredToolbarActionView() = 0;
protected:
~Delegate() override {}
};
// Callback type used for testing.
using ContextMenuCallback = base::Callback<void(ToolbarActionView*)>;
ToolbarActionView(ToolbarActionViewController* view_controller,
Delegate* delegate);
~ToolbarActionView() override;
// views::MenuButton:
void GetAccessibleState(ui::AXViewState* state) override;
scoped_ptr<views::LabelButtonBorder> CreateDefaultBorder() const override;
void OnMouseEntered(const ui::MouseEvent& event) override;
bool IsTriggerableEvent(const ui::Event& event) override;
SkColor GetInkDropBaseColor() const override;
bool ShouldShowInkDropHover() const override;
// ToolbarActionViewDelegateViews:
content::WebContents* GetCurrentWebContents() const override;
void UpdateState() override;
// views::MenuButtonListener:
void OnMenuButtonClicked(views::MenuButton* source,
const gfx::Point& point,
const ui::Event* event) override;
ToolbarActionViewController* view_controller() {
return view_controller_;
}
// Returns button icon so it can be accessed during tests.
gfx::ImageSkia GetIconForTest();
bool wants_to_run_for_testing() const { return wants_to_run_; }
// Set a callback to be called directly before the context menu is shown.
// The toolbar action opening the menu will be passed in.
static void set_context_menu_callback_for_testing(
ContextMenuCallback* callback);
views::MenuItemView* menu_for_testing() { return menu_; }
private:
// views::MenuButton:
gfx::Size GetPreferredSize() const override;
bool OnMousePressed(const ui::MouseEvent& event) override;
void OnGestureEvent(ui::GestureEvent* event) override;
void OnDragDone() override;
void ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) override;
// ToolbarActionViewDelegateViews:
views::View* GetAsView() override;
views::FocusManager* GetFocusManagerForAccelerator() override;
views::View* GetReferenceViewForPopup() override;
bool IsMenuRunning() const override;
void OnPopupShown(bool by_user) override;
void OnPopupClosed() override;
// views::ContextMenuController:
void ShowContextMenuForView(views::View* source,
const gfx::Point& point,
ui::MenuSourceType source_type) override;
// Shows the context menu (if one exists) for the toolbar action.
void DoShowContextMenu(ui::MenuSourceType source_type);
// Closes the currently-active menu, if needed. This is the case when there
// is an active menu that wouldn't close automatically when a new one is
// opened.
// Returns true if a menu was closed, false otherwise.
bool CloseActiveMenuIfNeeded();
// Callback for MenuModelAdapter.
void OnMenuClosed();
// A lock to keep the MenuButton pressed when a menu or popup is visible.
scoped_ptr<views::MenuButton::PressedLock> pressed_lock_;
// The controller for this toolbar action view.
ToolbarActionViewController* view_controller_;
// Delegate that usually represents a container for ToolbarActionView.
Delegate* delegate_;
// Used to make sure we only register the command once.
bool called_register_command_;
// The cached value of whether or not the action wants to run on the current
// tab.
bool wants_to_run_;
// Responsible for converting the context menu model into |menu_|.
scoped_ptr<views::MenuModelAdapter> menu_adapter_;
// Responsible for running the menu.
scoped_ptr<views::MenuRunner> menu_runner_;
// The root MenuItemView for the context menu, or null if no menu is being
// shown.
views::MenuItemView* menu_;
// The time the popup was last closed.
base::TimeTicks popup_closed_time_;
scoped_ptr<views::InkDropDelegate> ink_drop_delegate_;
base::WeakPtrFactory<ToolbarActionView> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(ToolbarActionView);
};
#endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_TOOLBAR_ACTION_VIEW_H_
|