| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/browser_action_view.h" | 5 #include "chrome/browser/ui/views/browser_action_view.h" |
| 6 | 6 |
| 7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/extensions/api/commands/command_service.h" | 8 #include "chrome/browser/extensions/api/commands/command_service.h" |
| 9 #include "chrome/browser/extensions/api/commands/command_service_factory.h" | 9 #include "chrome/browser/extensions/api/commands/command_service_factory.h" |
| 10 #include "chrome/browser/extensions/extension_context_menu_model.h" | 10 #include "chrome/browser/extensions/extension_context_menu_model.h" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 | 55 |
| 56 BrowserActionView::~BrowserActionView() { | 56 BrowserActionView::~BrowserActionView() { |
| 57 button_->Destroy(); | 57 button_->Destroy(); |
| 58 } | 58 } |
| 59 | 59 |
| 60 gfx::ImageSkia BrowserActionView::GetIconWithBadge() { | 60 gfx::ImageSkia BrowserActionView::GetIconWithBadge() { |
| 61 int tab_id = delegate_->GetCurrentTabId(); | 61 int tab_id = delegate_->GetCurrentTabId(); |
| 62 | 62 |
| 63 const ExtensionAction* action = button_->extension()->browser_action(); | 63 const ExtensionAction* action = button_->extension()->browser_action(); |
| 64 gfx::Size spacing(0, ToolbarView::kVertSpacing); | 64 gfx::Size spacing(0, ToolbarView::kVertSpacing); |
| 65 gfx::ImageSkia icon = *action->GetIcon(tab_id).ToImageSkia(); | 65 gfx::ImageSkia icon = |
| 66 *action->GetIcon(tab_id, &button_->icon_factory()).ToImageSkia(); |
| 66 if (!button_->IsEnabled(tab_id)) | 67 if (!button_->IsEnabled(tab_id)) |
| 67 icon = gfx::ImageSkiaOperations::CreateTransparentImage(icon, .25); | 68 icon = gfx::ImageSkiaOperations::CreateTransparentImage(icon, .25); |
| 68 return action->GetIconWithBadge(icon, tab_id, spacing); | 69 return action->GetIconWithBadge(icon, tab_id, spacing); |
| 69 } | 70 } |
| 70 | 71 |
| 71 void BrowserActionView::Layout() { | 72 void BrowserActionView::Layout() { |
| 72 // We can't rely on button_->GetPreferredSize() here because that's not set | 73 // We can't rely on button_->GetPreferredSize() here because that's not set |
| 73 // correctly until the first call to | 74 // correctly until the first call to |
| 74 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be | 75 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be |
| 75 // called before that when the initial bounds are set (and then not after, | 76 // called before that when the initial bounds are set (and then not after, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 104 // BrowserActionButton | 105 // BrowserActionButton |
| 105 | 106 |
| 106 BrowserActionButton::BrowserActionButton(const Extension* extension, | 107 BrowserActionButton::BrowserActionButton(const Extension* extension, |
| 107 Browser* browser, | 108 Browser* browser, |
| 108 BrowserActionView::Delegate* delegate) | 109 BrowserActionView::Delegate* delegate) |
| 109 : ALLOW_THIS_IN_INITIALIZER_LIST( | 110 : ALLOW_THIS_IN_INITIALIZER_LIST( |
| 110 MenuButton(this, string16(), NULL, false)), | 111 MenuButton(this, string16(), NULL, false)), |
| 111 browser_(browser), | 112 browser_(browser), |
| 112 browser_action_(extension->browser_action()), | 113 browser_action_(extension->browser_action()), |
| 113 extension_(extension), | 114 extension_(extension), |
| 114 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), | 115 ALLOW_THIS_IN_INITIALIZER_LIST(icon_factory_(extension, this)), |
| 115 delegate_(delegate), | 116 delegate_(delegate), |
| 116 context_menu_(NULL), | 117 context_menu_(NULL), |
| 117 called_registered_extension_command_(false) { | 118 called_registered_extension_command_(false) { |
| 118 set_border(NULL); | 119 set_border(NULL); |
| 119 set_alignment(TextButton::ALIGN_CENTER); | 120 set_alignment(TextButton::ALIGN_CENTER); |
| 120 set_context_menu_controller(this); | 121 set_context_menu_controller(this); |
| 121 | 122 |
| 122 // No UpdateState() here because View hierarchy not setup yet. Our parent | 123 // No UpdateState() here because View hierarchy not setup yet. Our parent |
| 123 // should call UpdateState() after creation. | 124 // should call UpdateState() after creation. |
| 124 | 125 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 138 if (context_menu_) { | 139 if (context_menu_) { |
| 139 context_menu_->Cancel(); | 140 context_menu_->Cancel(); |
| 140 MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 141 MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
| 141 } else { | 142 } else { |
| 142 delete this; | 143 delete this; |
| 143 } | 144 } |
| 144 } | 145 } |
| 145 | 146 |
| 146 void BrowserActionButton::ViewHierarchyChanged( | 147 void BrowserActionButton::ViewHierarchyChanged( |
| 147 bool is_add, View* parent, View* child) { | 148 bool is_add, View* parent, View* child) { |
| 148 if (is_add && child == this) { | |
| 149 // The Browser Action API does not allow the default icon path to be | |
| 150 // changed at runtime, so we can load this now and cache it. | |
| 151 std::string relative_path = browser_action_->default_icon_path(); | |
| 152 if (!relative_path.empty()) { | |
| 153 // LoadImage is not guaranteed to be synchronous, so we might see the | |
| 154 // callback OnImageLoaded execute immediately. It (through UpdateState) | |
| 155 // expects parent() to return the owner for this button, so this | |
| 156 // function is as early as we can start this request. | |
| 157 tracker_.LoadImage(extension_, extension_->GetResource(relative_path), | |
| 158 gfx::Size(Extension::kBrowserActionIconMaxSize, | |
| 159 Extension::kBrowserActionIconMaxSize), | |
| 160 ImageLoadingTracker::DONT_CACHE); | |
| 161 } | |
| 162 } | |
| 163 | 149 |
| 164 if (is_add && !called_registered_extension_command_ && GetFocusManager()) { | 150 if (is_add && !called_registered_extension_command_ && GetFocusManager()) { |
| 165 MaybeRegisterExtensionCommand(); | 151 MaybeRegisterExtensionCommand(); |
| 166 called_registered_extension_command_ = true; | 152 called_registered_extension_command_ = true; |
| 167 } | 153 } |
| 168 | 154 |
| 169 MenuButton::ViewHierarchyChanged(is_add, parent, child); | 155 MenuButton::ViewHierarchyChanged(is_add, parent, child); |
| 170 } | 156 } |
| 171 | 157 |
| 172 bool BrowserActionButton::CanHandleAccelerators() const { | 158 bool BrowserActionButton::CanHandleAccelerators() const { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 views::MenuItemView::TOPLEFT, views::MenuRunner::HAS_MNEMONICS) == | 192 views::MenuItemView::TOPLEFT, views::MenuRunner::HAS_MNEMONICS) == |
| 207 views::MenuRunner::MENU_DELETED) { | 193 views::MenuRunner::MENU_DELETED) { |
| 208 return; | 194 return; |
| 209 } | 195 } |
| 210 | 196 |
| 211 menu_runner_.reset(); | 197 menu_runner_.reset(); |
| 212 SetButtonNotPushed(); | 198 SetButtonNotPushed(); |
| 213 context_menu_ = NULL; | 199 context_menu_ = NULL; |
| 214 } | 200 } |
| 215 | 201 |
| 216 void BrowserActionButton::OnImageLoaded(const gfx::Image& image, | |
| 217 const std::string& extension_id, | |
| 218 int index) { | |
| 219 browser_action_->CacheIcon(browser_action_->default_icon_path(), image); | |
| 220 | |
| 221 // Call back to UpdateState() because a more specific icon might have been set | |
| 222 // while the load was outstanding. | |
| 223 UpdateState(); | |
| 224 } | |
| 225 | |
| 226 void BrowserActionButton::UpdateState() { | 202 void BrowserActionButton::UpdateState() { |
| 227 int tab_id = delegate_->GetCurrentTabId(); | 203 int tab_id = delegate_->GetCurrentTabId(); |
| 228 if (tab_id < 0) | 204 if (tab_id < 0) |
| 229 return; | 205 return; |
| 230 | 206 |
| 231 SetShowMultipleIconStates(delegate_->NeedToShowMultipleIconStates()); | 207 SetShowMultipleIconStates(delegate_->NeedToShowMultipleIconStates()); |
| 232 | 208 |
| 233 if (!IsEnabled(tab_id)) { | 209 if (!IsEnabled(tab_id)) { |
| 234 SetState(views::CustomButton::BS_DISABLED); | 210 SetState(views::CustomButton::BS_DISABLED); |
| 235 } else { | 211 } else { |
| 236 SetState(menu_visible_ ? | 212 SetState(menu_visible_ ? |
| 237 views::CustomButton::BS_PUSHED : | 213 views::CustomButton::BS_PUSHED : |
| 238 views::CustomButton::BS_NORMAL); | 214 views::CustomButton::BS_NORMAL); |
| 239 } | 215 } |
| 240 | 216 |
| 241 gfx::ImageSkia icon = *browser_action()->GetIcon(tab_id).ToImageSkia(); | 217 gfx::ImageSkia icon = |
| 218 *browser_action()->GetIcon(tab_id, &icon_factory_).ToImageSkia(); |
| 242 | 219 |
| 243 if (!icon.isNull()) { | 220 if (!icon.isNull()) { |
| 244 if (!browser_action()->GetIsVisible(tab_id)) | 221 if (!browser_action()->GetIsVisible(tab_id)) |
| 245 icon = gfx::ImageSkiaOperations::CreateTransparentImage(icon, .25); | 222 icon = gfx::ImageSkiaOperations::CreateTransparentImage(icon, .25); |
| 246 | 223 |
| 247 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 224 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 248 | 225 |
| 249 gfx::ImageSkia bg = *rb.GetImageSkiaNamed(IDR_BROWSER_ACTION); | 226 gfx::ImageSkia bg = *rb.GetImageSkiaNamed(IDR_BROWSER_ACTION); |
| 250 SetIcon(gfx::ImageSkiaOperations::CreateSuperimposedImage(bg, icon)); | 227 SetIcon(gfx::ImageSkiaOperations::CreateSuperimposedImage(bg, icon)); |
| 251 | 228 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 MaybeUnregisterExtensionCommand(true); | 277 MaybeUnregisterExtensionCommand(true); |
| 301 } | 278 } |
| 302 break; | 279 break; |
| 303 } | 280 } |
| 304 default: | 281 default: |
| 305 NOTREACHED(); | 282 NOTREACHED(); |
| 306 break; | 283 break; |
| 307 } | 284 } |
| 308 } | 285 } |
| 309 | 286 |
| 287 void BrowserActionButton::OnIconUpdated() { |
| 288 UpdateState(); |
| 289 } |
| 290 |
| 310 bool BrowserActionButton::Activate() { | 291 bool BrowserActionButton::Activate() { |
| 311 if (!IsPopup()) | 292 if (!IsPopup()) |
| 312 return true; | 293 return true; |
| 313 | 294 |
| 314 delegate_->OnBrowserActionExecuted(this); | 295 delegate_->OnBrowserActionExecuted(this); |
| 315 | 296 |
| 316 // TODO(erikkay): Run a nested modal loop while the mouse is down to | 297 // TODO(erikkay): Run a nested modal loop while the mouse is down to |
| 317 // enable menu-like drag-select behavior. | 298 // enable menu-like drag-select behavior. |
| 318 | 299 |
| 319 // The return value of this method is returned via OnMousePressed. | 300 // The return value of this method is returned via OnMousePressed. |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 | 387 |
| 407 extensions::Command browser_action_command; | 388 extensions::Command browser_action_command; |
| 408 if (!only_if_active || !command_service->GetBrowserActionCommand( | 389 if (!only_if_active || !command_service->GetBrowserActionCommand( |
| 409 extension_->id(), | 390 extension_->id(), |
| 410 extensions::CommandService::ACTIVE_ONLY, | 391 extensions::CommandService::ACTIVE_ONLY, |
| 411 &browser_action_command, | 392 &browser_action_command, |
| 412 NULL)) { | 393 NULL)) { |
| 413 GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); | 394 GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); |
| 414 } | 395 } |
| 415 } | 396 } |
| OLD | NEW |