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