| 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/toolbar_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 // ToolbarActionView | |
| 41 | |
| 42 ToolbarActionView::ToolbarActionView( | |
| 43 scoped_ptr<ToolbarActionViewController> view_controller, | |
| 44 Browser* browser, | |
| 45 ToolbarActionView::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 ToolbarActionView::~ToolbarActionView() { | |
| 69 } | |
| 70 | |
| 71 void ToolbarActionView::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 ToolbarActionView::OnDragDone() { | |
| 82 delegate_->OnToolbarActionViewDragDone(); | |
| 83 } | |
| 84 | |
| 85 gfx::Size ToolbarActionView::GetPreferredSize() const { | |
| 86 return gfx::Size(BrowserActionsContainer::IconWidth(false), | |
| 87 BrowserActionsContainer::IconHeight()); | |
| 88 } | |
| 89 | |
| 90 void ToolbarActionView::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 ToolbarActionView::GetAccessibleState(ui::AXViewState* state) { | |
| 98 views::MenuButton::GetAccessibleState(state); | |
| 99 state->role = ui::AX_ROLE_BUTTON; | |
| 100 } | |
| 101 | |
| 102 void ToolbarActionView::ButtonPressed(views::Button* sender, | |
| 103 const ui::Event& event) { | |
| 104 view_controller_->ExecuteAction(true); | |
| 105 } | |
| 106 | |
| 107 void ToolbarActionView::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 ToolbarActionView::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 ToolbarActionView::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 ToolbarActionView::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 ToolbarActionView::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 ToolbarActionView::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 ToolbarActionView::OnKeyReleased(const ui::KeyEvent& event) { | |
| 188 return view_controller_->HasPopup(GetCurrentWebContents()) ? | |
| 189 MenuButton::OnKeyReleased(event) : LabelButton::OnKeyReleased(event); | |
| 190 } | |
| 191 | |
| 192 void ToolbarActionView::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> ToolbarActionView::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 ToolbarActionView::GetIconForTest() { | |
| 207 return GetImage(views::Button::STATE_NORMAL); | |
| 208 } | |
| 209 | |
| 210 void ToolbarActionView::OnIconUpdated() { | |
| 211 UpdateState(); | |
| 212 } | |
| 213 | |
| 214 views::View* ToolbarActionView::GetAsView() { | |
| 215 return this; | |
| 216 } | |
| 217 | |
| 218 bool ToolbarActionView::IsShownInMenu() { | |
| 219 return delegate_->ShownInsideMenu(); | |
| 220 } | |
| 221 | |
| 222 views::FocusManager* ToolbarActionView::GetFocusManagerForAccelerator() { | |
| 223 return GetFocusManager(); | |
| 224 } | |
| 225 | |
| 226 views::Widget* ToolbarActionView::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 ToolbarActionView::GetPreferredPopupViewController() { | |
| 237 return delegate_->ShownInsideMenu() ? | |
| 238 delegate_->GetMainViewForAction(this)->view_controller() : | |
| 239 view_controller(); | |
| 240 } | |
| 241 | |
| 242 views::View* ToolbarActionView::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* ToolbarActionView::GetContextMenuButton() { | |
| 250 DCHECK(visible()); // We should never show a context menu for a hidden item. | |
| 251 return this; | |
| 252 } | |
| 253 | |
| 254 content::WebContents* ToolbarActionView::GetCurrentWebContents() const { | |
| 255 return delegate_->GetCurrentWebContents(); | |
| 256 } | |
| 257 | |
| 258 void ToolbarActionView::HideActivePopup() { | |
| 259 delegate_->HideActivePopup(); | |
| 260 } | |
| 261 | |
| 262 void ToolbarActionView::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 ToolbarActionView::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 } | |
| OLD | NEW |