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 = *button_->icon_factory().GetIcon(tab_id).ToImageSkia(); |
66 if (!button_->IsEnabled(tab_id)) | 66 if (!button_->IsEnabled(tab_id)) |
67 icon = gfx::ImageSkiaOperations::CreateTransparentImage(icon, .25); | 67 icon = gfx::ImageSkiaOperations::CreateTransparentImage(icon, .25); |
68 return action->GetIconWithBadge(icon, tab_id, spacing); | 68 return action->GetIconWithBadge(icon, tab_id, spacing); |
69 } | 69 } |
70 | 70 |
71 void BrowserActionView::Layout() { | 71 void BrowserActionView::Layout() { |
72 // We can't rely on button_->GetPreferredSize() here because that's not set | 72 // We can't rely on button_->GetPreferredSize() here because that's not set |
73 // correctly until the first call to | 73 // correctly until the first call to |
74 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be | 74 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be |
75 // called before that when the initial bounds are set (and then not after, | 75 // called before that when the initial bounds are set (and then not after, |
(...skipping 28 matching lines...) Expand all Loading... |
104 // BrowserActionButton | 104 // BrowserActionButton |
105 | 105 |
106 BrowserActionButton::BrowserActionButton(const Extension* extension, | 106 BrowserActionButton::BrowserActionButton(const Extension* extension, |
107 Browser* browser, | 107 Browser* browser, |
108 BrowserActionView::Delegate* delegate) | 108 BrowserActionView::Delegate* delegate) |
109 : ALLOW_THIS_IN_INITIALIZER_LIST( | 109 : ALLOW_THIS_IN_INITIALIZER_LIST( |
110 MenuButton(this, string16(), NULL, false)), | 110 MenuButton(this, string16(), NULL, false)), |
111 browser_(browser), | 111 browser_(browser), |
112 browser_action_(extension->browser_action()), | 112 browser_action_(extension->browser_action()), |
113 extension_(extension), | 113 extension_(extension), |
114 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), | 114 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 115 icon_factory_(extension, extension->browser_action(), 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 views::MenuRunner::CONTEXT_MENU) == | 193 views::MenuRunner::CONTEXT_MENU) == |
208 views::MenuRunner::MENU_DELETED) { | 194 views::MenuRunner::MENU_DELETED) { |
209 return; | 195 return; |
210 } | 196 } |
211 | 197 |
212 menu_runner_.reset(); | 198 menu_runner_.reset(); |
213 SetButtonNotPushed(); | 199 SetButtonNotPushed(); |
214 context_menu_ = NULL; | 200 context_menu_ = NULL; |
215 } | 201 } |
216 | 202 |
217 void BrowserActionButton::OnImageLoaded(const gfx::Image& image, | |
218 const std::string& extension_id, | |
219 int index) { | |
220 browser_action_->CacheIcon(image); | |
221 | |
222 // Call back to UpdateState() because a more specific icon might have been set | |
223 // while the load was outstanding. | |
224 UpdateState(); | |
225 } | |
226 | |
227 void BrowserActionButton::UpdateState() { | 203 void BrowserActionButton::UpdateState() { |
228 int tab_id = delegate_->GetCurrentTabId(); | 204 int tab_id = delegate_->GetCurrentTabId(); |
229 if (tab_id < 0) | 205 if (tab_id < 0) |
230 return; | 206 return; |
231 | 207 |
232 SetShowMultipleIconStates(delegate_->NeedToShowMultipleIconStates()); | 208 SetShowMultipleIconStates(delegate_->NeedToShowMultipleIconStates()); |
233 | 209 |
234 if (!IsEnabled(tab_id)) { | 210 if (!IsEnabled(tab_id)) { |
235 SetState(views::CustomButton::BS_DISABLED); | 211 SetState(views::CustomButton::BS_DISABLED); |
236 } else { | 212 } else { |
237 SetState(menu_visible_ ? | 213 SetState(menu_visible_ ? |
238 views::CustomButton::BS_PUSHED : | 214 views::CustomButton::BS_PUSHED : |
239 views::CustomButton::BS_NORMAL); | 215 views::CustomButton::BS_NORMAL); |
240 } | 216 } |
241 | 217 |
242 gfx::ImageSkia icon = *browser_action()->GetIcon(tab_id).ToImageSkia(); | 218 gfx::ImageSkia icon = *icon_factory_.GetIcon(tab_id).ToImageSkia(); |
243 | 219 |
244 if (!icon.isNull()) { | 220 if (!icon.isNull()) { |
245 if (!browser_action()->GetIsVisible(tab_id)) | 221 if (!browser_action()->GetIsVisible(tab_id)) |
246 icon = gfx::ImageSkiaOperations::CreateTransparentImage(icon, .25); | 222 icon = gfx::ImageSkiaOperations::CreateTransparentImage(icon, .25); |
247 | 223 |
248 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 224 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
249 | 225 |
250 gfx::ImageSkia bg = *rb.GetImageSkiaNamed(IDR_BROWSER_ACTION); | 226 gfx::ImageSkia bg = *rb.GetImageSkiaNamed(IDR_BROWSER_ACTION); |
251 SetIcon(gfx::ImageSkiaOperations::CreateSuperimposedImage(bg, icon)); | 227 SetIcon(gfx::ImageSkiaOperations::CreateSuperimposedImage(bg, icon)); |
252 | 228 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 MaybeUnregisterExtensionCommand(true); | 277 MaybeUnregisterExtensionCommand(true); |
302 } | 278 } |
303 break; | 279 break; |
304 } | 280 } |
305 default: | 281 default: |
306 NOTREACHED(); | 282 NOTREACHED(); |
307 break; | 283 break; |
308 } | 284 } |
309 } | 285 } |
310 | 286 |
| 287 void BrowserActionButton::OnIconUpdated() { |
| 288 UpdateState(); |
| 289 } |
| 290 |
311 bool BrowserActionButton::Activate() { | 291 bool BrowserActionButton::Activate() { |
312 if (!IsPopup()) | 292 if (!IsPopup()) |
313 return true; | 293 return true; |
314 | 294 |
315 delegate_->OnBrowserActionExecuted(this); | 295 delegate_->OnBrowserActionExecuted(this); |
316 | 296 |
317 // 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 |
318 // enable menu-like drag-select behavior. | 298 // enable menu-like drag-select behavior. |
319 | 299 |
320 // The return value of this method is returned via OnMousePressed. | 300 // The return value of this method is returned via OnMousePressed. |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 | 391 |
412 extensions::Command browser_action_command; | 392 extensions::Command browser_action_command; |
413 if (!only_if_active || !command_service->GetBrowserActionCommand( | 393 if (!only_if_active || !command_service->GetBrowserActionCommand( |
414 extension_->id(), | 394 extension_->id(), |
415 extensions::CommandService::ACTIVE_ONLY, | 395 extensions::CommandService::ACTIVE_ONLY, |
416 &browser_action_command, | 396 &browser_action_command, |
417 NULL)) { | 397 NULL)) { |
418 GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); | 398 GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); |
419 } | 399 } |
420 } | 400 } |
OLD | NEW |