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