OLD | NEW |
| (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 views::MenuButton::OnDragDone(); | |
83 delegate_->OnBrowserActionViewDragDone(); | |
84 } | |
85 | |
86 gfx::Size BrowserActionView::GetPreferredSize() const { | |
87 return gfx::Size(BrowserActionsContainer::IconWidth(false), | |
88 BrowserActionsContainer::IconHeight()); | |
89 } | |
90 | |
91 void BrowserActionView::PaintChildren(gfx::Canvas* canvas, | |
92 const views::CullSet& cull_set) { | |
93 View::PaintChildren(canvas, cull_set); | |
94 view_controller_->PaintExtra( | |
95 canvas, GetLocalBounds(), GetCurrentWebContents()); | |
96 } | |
97 | |
98 void BrowserActionView::GetAccessibleState(ui::AXViewState* state) { | |
99 views::MenuButton::GetAccessibleState(state); | |
100 state->role = ui::AX_ROLE_BUTTON; | |
101 } | |
102 | |
103 void BrowserActionView::ButtonPressed(views::Button* sender, | |
104 const ui::Event& event) { | |
105 view_controller_->ExecuteAction(true); | |
106 } | |
107 | |
108 void BrowserActionView::UpdateState() { | |
109 content::WebContents* web_contents = GetCurrentWebContents(); | |
110 if (SessionTabHelper::IdForTab(web_contents) < 0) | |
111 return; | |
112 | |
113 bool enabled = view_controller_->IsEnabled(web_contents); | |
114 if (!enabled) | |
115 SetState(views::CustomButton::STATE_DISABLED); | |
116 | |
117 gfx::ImageSkia icon = *view_controller_->GetIcon(web_contents).ToImageSkia(); | |
118 | |
119 if (!icon.isNull()) { | |
120 if (!enabled) | |
121 icon = gfx::ImageSkiaOperations::CreateTransparentImage(icon, .25); | |
122 | |
123 ThemeService* theme = | |
124 ThemeServiceFactory::GetForProfile(browser_->profile()); | |
125 | |
126 gfx::ImageSkia bg = *theme->GetImageSkiaNamed(IDR_BROWSER_ACTION); | |
127 SetImage(views::Button::STATE_NORMAL, | |
128 gfx::ImageSkiaOperations::CreateSuperimposedImage(bg, icon)); | |
129 } | |
130 | |
131 SetTooltipText(view_controller_->GetTooltip(web_contents)); | |
132 SetAccessibleName(view_controller_->GetAccessibleName(web_contents)); | |
133 | |
134 Layout(); // We need to layout since we may have added an icon as a result. | |
135 SchedulePaint(); | |
136 } | |
137 | |
138 void BrowserActionView::Observe(int type, | |
139 const content::NotificationSource& source, | |
140 const content::NotificationDetails& details) { | |
141 DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type); | |
142 UpdateState(); | |
143 } | |
144 | |
145 bool BrowserActionView::Activate() { | |
146 if (!view_controller_->HasPopup(GetCurrentWebContents())) | |
147 return true; | |
148 | |
149 view_controller_->ExecuteAction(true); | |
150 | |
151 // TODO(erikkay): Run a nested modal loop while the mouse is down to | |
152 // enable menu-like drag-select behavior. | |
153 | |
154 // The return value of this method is returned via OnMousePressed. | |
155 // We need to return false here since we're handing off focus to another | |
156 // widget/view, and true will grab it right back and try to send events | |
157 // to us. | |
158 return false; | |
159 } | |
160 | |
161 bool BrowserActionView::OnMousePressed(const ui::MouseEvent& event) { | |
162 if (!event.IsRightMouseButton()) { | |
163 return view_controller_->HasPopup(GetCurrentWebContents()) ? | |
164 MenuButton::OnMousePressed(event) : LabelButton::OnMousePressed(event); | |
165 } | |
166 return false; | |
167 } | |
168 | |
169 void BrowserActionView::OnMouseReleased(const ui::MouseEvent& event) { | |
170 if (view_controller_->HasPopup(GetCurrentWebContents()) || | |
171 view_controller_->IsMenuRunning()) { | |
172 // TODO(erikkay) this never actually gets called (probably because of the | |
173 // loss of focus). | |
174 MenuButton::OnMouseReleased(event); | |
175 } else { | |
176 LabelButton::OnMouseReleased(event); | |
177 } | |
178 } | |
179 | |
180 void BrowserActionView::OnMouseExited(const ui::MouseEvent& event) { | |
181 if (view_controller_->HasPopup(GetCurrentWebContents()) || | |
182 view_controller_->IsMenuRunning()) | |
183 MenuButton::OnMouseExited(event); | |
184 else | |
185 LabelButton::OnMouseExited(event); | |
186 } | |
187 | |
188 bool BrowserActionView::OnKeyReleased(const ui::KeyEvent& event) { | |
189 return view_controller_->HasPopup(GetCurrentWebContents()) ? | |
190 MenuButton::OnKeyReleased(event) : LabelButton::OnKeyReleased(event); | |
191 } | |
192 | |
193 void BrowserActionView::OnGestureEvent(ui::GestureEvent* event) { | |
194 if (view_controller_->HasPopup(GetCurrentWebContents())) | |
195 MenuButton::OnGestureEvent(event); | |
196 else | |
197 LabelButton::OnGestureEvent(event); | |
198 } | |
199 | |
200 scoped_ptr<LabelButtonBorder> BrowserActionView::CreateDefaultBorder() const { | |
201 scoped_ptr<LabelButtonBorder> border = LabelButton::CreateDefaultBorder(); | |
202 border->set_insets(gfx::Insets(kBorderInset, kBorderInset, | |
203 kBorderInset, kBorderInset)); | |
204 return border.Pass(); | |
205 } | |
206 | |
207 gfx::ImageSkia BrowserActionView::GetIconForTest() { | |
208 return GetImage(views::Button::STATE_NORMAL); | |
209 } | |
210 | |
211 void BrowserActionView::OnIconUpdated() { | |
212 UpdateState(); | |
213 } | |
214 | |
215 views::View* BrowserActionView::GetAsView() { | |
216 return this; | |
217 } | |
218 | |
219 bool BrowserActionView::IsShownInMenu() { | |
220 return delegate_->ShownInsideMenu(); | |
221 } | |
222 | |
223 views::FocusManager* BrowserActionView::GetFocusManagerForAccelerator() { | |
224 return GetFocusManager(); | |
225 } | |
226 | |
227 views::Widget* BrowserActionView::GetParentForContextMenu() { | |
228 // RunMenuAt expects a nested menu to be parented by the same widget as the | |
229 // already visible menu, in this case the Chrome menu. | |
230 return delegate_->ShownInsideMenu() ? | |
231 BrowserView::GetBrowserViewForBrowser(browser_) | |
232 ->toolbar()->app_menu()->GetWidget() : | |
233 GetWidget(); | |
234 } | |
235 | |
236 ToolbarActionViewController* | |
237 BrowserActionView::GetPreferredPopupViewController() { | |
238 return delegate_->ShownInsideMenu() ? | |
239 delegate_->GetMainViewForAction(this)->view_controller() : | |
240 view_controller(); | |
241 } | |
242 | |
243 views::View* BrowserActionView::GetReferenceViewForPopup() { | |
244 // Browser actions in the overflow menu can still show popups, so we may need | |
245 // a reference view other than this button's parent. If so, use the overflow | |
246 // view. | |
247 return visible() ? this : delegate_->GetOverflowReferenceView(); | |
248 } | |
249 | |
250 views::MenuButton* BrowserActionView::GetContextMenuButton() { | |
251 DCHECK(visible()); // We should never show a context menu for a hidden item. | |
252 return this; | |
253 } | |
254 | |
255 content::WebContents* BrowserActionView::GetCurrentWebContents() const { | |
256 return delegate_->GetCurrentWebContents(); | |
257 } | |
258 | |
259 void BrowserActionView::HideActivePopup() { | |
260 delegate_->HideActivePopup(); | |
261 } | |
262 | |
263 void BrowserActionView::OnPopupShown(bool grant_tab_permissions) { | |
264 delegate_->SetPopupOwner(this); | |
265 // If this was through direct user action, we press the menu button. | |
266 if (grant_tab_permissions) { | |
267 // We set the state of the menu button we're using as a reference view, | |
268 // which is either this or the overflow reference view. | |
269 // This cast is safe because GetReferenceViewForPopup returns either |this| | |
270 // or delegate_->GetOverflowReferenceView(), which returns a MenuButton. | |
271 views::MenuButton* reference_view = | |
272 static_cast<views::MenuButton*>(GetReferenceViewForPopup()); | |
273 pressed_lock_.reset(new views::MenuButton::PressedLock(reference_view)); | |
274 } | |
275 } | |
276 | |
277 void BrowserActionView::CleanupPopup() { | |
278 // We need to do these actions synchronously (instead of closing and then | |
279 // performing the rest of the cleanup in OnWidgetDestroyed()) because | |
280 // OnWidgetDestroyed() can be called asynchronously from Close(), and we need | |
281 // to keep the delegate's popup owner up-to-date. | |
282 delegate_->SetPopupOwner(NULL); | |
283 pressed_lock_.reset(); // Unpress the menu button if it was pressed. | |
284 } | |
OLD | NEW |