Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(719)

Side by Side Diff: chrome/browser/ui/views/toolbar/browser_action_view.cc

Issue 673503002: Rename BrowserActionView to ToolbarActionView (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/ui/views/toolbar/browser_action_view.h"
6
7 #include <string>
8
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/sessions/session_tab_helper.h"
12 #include "chrome/browser/themes/theme_service.h"
13 #include "chrome/browser/themes/theme_service_factory.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h"
16 #include "chrome/browser/ui/view_ids.h"
17 #include "chrome/browser/ui/views/frame/browser_view.h"
18 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h"
19 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
20 #include "chrome/grit/generated_resources.h"
21 #include "grit/theme_resources.h"
22 #include "ui/accessibility/ax_view_state.h"
23 #include "ui/events/event.h"
24 #include "ui/gfx/image/image_skia.h"
25 #include "ui/gfx/image/image_skia_operations.h"
26 #include "ui/gfx/image/image_skia_source.h"
27 #include "ui/views/controls/button/label_button_border.h"
28
29 using views::LabelButtonBorder;
30
31 namespace {
32
33 // We have smaller insets than normal STYLE_TEXTBUTTON buttons so that we can
34 // fit user supplied icons in without clipping them.
35 const int kBorderInset = 4;
36
37 } // namespace
38
39 ////////////////////////////////////////////////////////////////////////////////
40 // BrowserActionView
41
42 BrowserActionView::BrowserActionView(
43 scoped_ptr<ToolbarActionViewController> view_controller,
44 Browser* browser,
45 BrowserActionView::Delegate* delegate)
46 : MenuButton(this, base::string16(), NULL, false),
47 view_controller_(view_controller.Pass()),
48 browser_(browser),
49 delegate_(delegate),
50 called_register_command_(false) {
51 set_id(VIEW_ID_BROWSER_ACTION);
52 view_controller_->SetDelegate(this);
53 SetHorizontalAlignment(gfx::ALIGN_CENTER);
54 if (view_controller_->CanDrag())
55 set_drag_controller(delegate_);
56
57 // We also listen for browser theme changes on linux because a switch from or
58 // to GTK requires that we regrab our browser action images.
59 registrar_.Add(
60 this,
61 chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
62 content::Source<ThemeService>(
63 ThemeServiceFactory::GetForProfile(browser->profile())));
64
65 UpdateState();
66 }
67
68 BrowserActionView::~BrowserActionView() {
69 }
70
71 void BrowserActionView::ViewHierarchyChanged(
72 const ViewHierarchyChangedDetails& details) {
73 if (details.is_add && !called_register_command_ && GetFocusManager()) {
74 view_controller_->RegisterCommand();
75 called_register_command_ = true;
76 }
77
78 MenuButton::ViewHierarchyChanged(details);
79 }
80
81 void BrowserActionView::OnDragDone() {
82 delegate_->OnBrowserActionViewDragDone();
83 }
84
85 gfx::Size BrowserActionView::GetPreferredSize() const {
86 return gfx::Size(BrowserActionsContainer::IconWidth(false),
87 BrowserActionsContainer::IconHeight());
88 }
89
90 void BrowserActionView::PaintChildren(gfx::Canvas* canvas,
91 const views::CullSet& cull_set) {
92 View::PaintChildren(canvas, cull_set);
93 view_controller_->PaintExtra(
94 canvas, GetLocalBounds(), GetCurrentWebContents());
95 }
96
97 void BrowserActionView::GetAccessibleState(ui::AXViewState* state) {
98 views::MenuButton::GetAccessibleState(state);
99 state->role = ui::AX_ROLE_BUTTON;
100 }
101
102 void BrowserActionView::ButtonPressed(views::Button* sender,
103 const ui::Event& event) {
104 view_controller_->ExecuteAction(true);
105 }
106
107 void BrowserActionView::UpdateState() {
108 content::WebContents* web_contents = GetCurrentWebContents();
109 if (SessionTabHelper::IdForTab(web_contents) < 0)
110 return;
111
112 bool enabled = view_controller_->IsEnabled(web_contents);
113 if (!enabled)
114 SetState(views::CustomButton::STATE_DISABLED);
115
116 gfx::ImageSkia icon = *view_controller_->GetIcon(web_contents).ToImageSkia();
117
118 if (!icon.isNull()) {
119 if (!enabled)
120 icon = gfx::ImageSkiaOperations::CreateTransparentImage(icon, .25);
121
122 ThemeService* theme =
123 ThemeServiceFactory::GetForProfile(browser_->profile());
124
125 gfx::ImageSkia bg = *theme->GetImageSkiaNamed(IDR_BROWSER_ACTION);
126 SetImage(views::Button::STATE_NORMAL,
127 gfx::ImageSkiaOperations::CreateSuperimposedImage(bg, icon));
128 }
129
130 SetTooltipText(view_controller_->GetTooltip(web_contents));
131 SetAccessibleName(view_controller_->GetAccessibleName(web_contents));
132
133 Layout(); // We need to layout since we may have added an icon as a result.
134 SchedulePaint();
135 }
136
137 void BrowserActionView::Observe(int type,
138 const content::NotificationSource& source,
139 const content::NotificationDetails& details) {
140 DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type);
141 UpdateState();
142 }
143
144 bool BrowserActionView::Activate() {
145 if (!view_controller_->HasPopup(GetCurrentWebContents()))
146 return true;
147
148 view_controller_->ExecuteAction(true);
149
150 // TODO(erikkay): Run a nested modal loop while the mouse is down to
151 // enable menu-like drag-select behavior.
152
153 // The return value of this method is returned via OnMousePressed.
154 // We need to return false here since we're handing off focus to another
155 // widget/view, and true will grab it right back and try to send events
156 // to us.
157 return false;
158 }
159
160 bool BrowserActionView::OnMousePressed(const ui::MouseEvent& event) {
161 if (!event.IsRightMouseButton()) {
162 return view_controller_->HasPopup(GetCurrentWebContents()) ?
163 MenuButton::OnMousePressed(event) : LabelButton::OnMousePressed(event);
164 }
165 return false;
166 }
167
168 void BrowserActionView::OnMouseReleased(const ui::MouseEvent& event) {
169 if (view_controller_->HasPopup(GetCurrentWebContents()) ||
170 view_controller_->IsMenuRunning()) {
171 // TODO(erikkay) this never actually gets called (probably because of the
172 // loss of focus).
173 MenuButton::OnMouseReleased(event);
174 } else {
175 LabelButton::OnMouseReleased(event);
176 }
177 }
178
179 void BrowserActionView::OnMouseExited(const ui::MouseEvent& event) {
180 if (view_controller_->HasPopup(GetCurrentWebContents()) ||
181 view_controller_->IsMenuRunning())
182 MenuButton::OnMouseExited(event);
183 else
184 LabelButton::OnMouseExited(event);
185 }
186
187 bool BrowserActionView::OnKeyReleased(const ui::KeyEvent& event) {
188 return view_controller_->HasPopup(GetCurrentWebContents()) ?
189 MenuButton::OnKeyReleased(event) : LabelButton::OnKeyReleased(event);
190 }
191
192 void BrowserActionView::OnGestureEvent(ui::GestureEvent* event) {
193 if (view_controller_->HasPopup(GetCurrentWebContents()))
194 MenuButton::OnGestureEvent(event);
195 else
196 LabelButton::OnGestureEvent(event);
197 }
198
199 scoped_ptr<LabelButtonBorder> BrowserActionView::CreateDefaultBorder() const {
200 scoped_ptr<LabelButtonBorder> border = LabelButton::CreateDefaultBorder();
201 border->set_insets(gfx::Insets(kBorderInset, kBorderInset,
202 kBorderInset, kBorderInset));
203 return border.Pass();
204 }
205
206 gfx::ImageSkia BrowserActionView::GetIconForTest() {
207 return GetImage(views::Button::STATE_NORMAL);
208 }
209
210 void BrowserActionView::OnIconUpdated() {
211 UpdateState();
212 }
213
214 views::View* BrowserActionView::GetAsView() {
215 return this;
216 }
217
218 bool BrowserActionView::IsShownInMenu() {
219 return delegate_->ShownInsideMenu();
220 }
221
222 views::FocusManager* BrowserActionView::GetFocusManagerForAccelerator() {
223 return GetFocusManager();
224 }
225
226 views::Widget* BrowserActionView::GetParentForContextMenu() {
227 // RunMenuAt expects a nested menu to be parented by the same widget as the
228 // already visible menu, in this case the Chrome menu.
229 return delegate_->ShownInsideMenu() ?
230 BrowserView::GetBrowserViewForBrowser(browser_)
231 ->toolbar()->app_menu()->GetWidget() :
232 GetWidget();
233 }
234
235 ToolbarActionViewController*
236 BrowserActionView::GetPreferredPopupViewController() {
237 return delegate_->ShownInsideMenu() ?
238 delegate_->GetMainViewForAction(this)->view_controller() :
239 view_controller();
240 }
241
242 views::View* BrowserActionView::GetReferenceViewForPopup() {
243 // Browser actions in the overflow menu can still show popups, so we may need
244 // a reference view other than this button's parent. If so, use the overflow
245 // view.
246 return visible() ? this : delegate_->GetOverflowReferenceView();
247 }
248
249 views::MenuButton* BrowserActionView::GetContextMenuButton() {
250 DCHECK(visible()); // We should never show a context menu for a hidden item.
251 return this;
252 }
253
254 content::WebContents* BrowserActionView::GetCurrentWebContents() const {
255 return delegate_->GetCurrentWebContents();
256 }
257
258 void BrowserActionView::HideActivePopup() {
259 delegate_->HideActivePopup();
260 }
261
262 void BrowserActionView::OnPopupShown(bool grant_tab_permissions) {
263 delegate_->SetPopupOwner(this);
264 // If this was through direct user action, we press the menu button.
265 if (grant_tab_permissions) {
266 // We set the state of the menu button we're using as a reference view,
267 // which is either this or the overflow reference view.
268 // This cast is safe because GetReferenceViewForPopup returns either |this|
269 // or delegate_->GetOverflowReferenceView(), which returns a MenuButton.
270 views::MenuButton* reference_view =
271 static_cast<views::MenuButton*>(GetReferenceViewForPopup());
272 pressed_lock_.reset(new views::MenuButton::PressedLock(reference_view));
273 }
274 }
275
276 void BrowserActionView::CleanupPopup() {
277 // We need to do these actions synchronously (instead of closing and then
278 // performing the rest of the cleanup in OnWidgetDestroyed()) because
279 // OnWidgetDestroyed() can be called asynchronously from Close(), and we need
280 // to keep the delegate's popup owner up-to-date.
281 delegate_->SetPopupOwner(NULL);
282 pressed_lock_.reset(); // Unpress the menu button if it was pressed.
283 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/toolbar/browser_action_view.h ('k') | chrome/browser/ui/views/toolbar/browser_actions_container.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698