Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(56)

Side by Side Diff: chrome/browser/ui/gtk/browser_actions_toolbar_gtk.cc

Issue 10806058: Move icon fallbacks into ExtensionAction. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix kalman's comments. Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/gtk/browser_actions_toolbar_gtk.h" 5 #include "chrome/browser/ui/gtk/browser_actions_toolbar_gtk.h"
6 6
7 #include <gtk/gtk.h> 7 #include <gtk/gtk.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <vector> 10 #include <vector>
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 DCHECK(extension_->browser_action()); 120 DCHECK(extension_->browser_action());
121 121
122 // The Browser Action API does not allow the default icon path to be 122 // The Browser Action API does not allow the default icon path to be
123 // changed at runtime, so we can load this now and cache it. 123 // changed at runtime, so we can load this now and cache it.
124 std::string path = extension_->browser_action()->default_icon_path(); 124 std::string path = extension_->browser_action()->default_icon_path();
125 if (!path.empty()) { 125 if (!path.empty()) {
126 tracker_.LoadImage(extension_, extension_->GetResource(path), 126 tracker_.LoadImage(extension_, extension_->GetResource(path),
127 gfx::Size(Extension::kBrowserActionIconMaxSize, 127 gfx::Size(Extension::kBrowserActionIconMaxSize,
128 Extension::kBrowserActionIconMaxSize), 128 Extension::kBrowserActionIconMaxSize),
129 ImageLoadingTracker::DONT_CACHE); 129 ImageLoadingTracker::DONT_CACHE);
130 } else {
131 const SkBitmap* bm =
132 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
133 IDR_EXTENSIONS_FAVICON).ToSkBitmap();
134 default_skbitmap_ = *bm;
135 default_icon_ = gfx::GdkPixbufFromSkBitmap(*bm);
136 } 130 }
137 131
138 UpdateState(); 132 UpdateState();
139 133
140 signals_.Connect(button(), "button-press-event", 134 signals_.Connect(button(), "button-press-event",
141 G_CALLBACK(OnButtonPress), this); 135 G_CALLBACK(OnButtonPress), this);
142 signals_.Connect(button(), "clicked", 136 signals_.Connect(button(), "clicked",
143 G_CALLBACK(OnClicked), this); 137 G_CALLBACK(OnClicked), this);
144 signals_.Connect(button(), "drag-begin", 138 signals_.Connect(button(), "drag-begin",
145 G_CALLBACK(&OnDragBegin), this); 139 G_CALLBACK(&OnDragBegin), this);
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 default: 192 default:
199 NOTREACHED(); 193 NOTREACHED();
200 break; 194 break;
201 } 195 }
202 } 196 }
203 197
204 // ImageLoadingTracker::Observer implementation. 198 // ImageLoadingTracker::Observer implementation.
205 void OnImageLoaded(const gfx::Image& image, 199 void OnImageLoaded(const gfx::Image& image,
206 const std::string& extension_id, 200 const std::string& extension_id,
207 int index) OVERRIDE { 201 int index) OVERRIDE {
208 if (!image.IsEmpty()) { 202 if (!image.IsEmpty())
209 default_skbitmap_ = *image.ToSkBitmap(); 203 loaded_icons_[extension_->browser_action()->default_icon_path()] = image;
210 default_icon_ =
211 static_cast<GdkPixbuf*>(g_object_ref(image.ToGdkPixbuf()));
212 }
213 UpdateState(); 204 UpdateState();
214 } 205 }
215 206
216 // Updates the button based on the latest state from the associated 207 // Updates the button based on the latest state from the associated
217 // browser action. 208 // browser action.
218 void UpdateState() { 209 void UpdateState() {
219 int tab_id = toolbar_->GetCurrentTabId(); 210 int tab_id = toolbar_->GetCurrentTabId();
220 if (tab_id < 0) 211 if (tab_id < 0)
221 return; 212 return;
222 213
223 std::string tooltip = extension_->browser_action()->GetTitle(tab_id); 214 std::string tooltip = extension_->browser_action()->GetTitle(tab_id);
224 if (tooltip.empty()) 215 if (tooltip.empty())
225 gtk_widget_set_has_tooltip(button(), FALSE); 216 gtk_widget_set_has_tooltip(button(), FALSE);
226 else 217 else
227 gtk_widget_set_tooltip_text(button(), tooltip.c_str()); 218 gtk_widget_set_tooltip_text(button(), tooltip.c_str());
228 219
229 SkBitmap image = extension_->browser_action()->GetIcon(tab_id); 220 gfx::Image image = extension_->browser_action()->GetIcon(
230 if (!image.isNull()) { 221 tab_id, loaded_icons_);
231 GdkPixbuf* previous_gdk_icon = tab_specific_icon_; 222 if (!image.IsEmpty())
232 tab_specific_icon_ = gfx::GdkPixbufFromSkBitmap(image); 223 SetImage(image.ToGdkPixbuf());
233 SetImage(tab_specific_icon_);
234 if (previous_gdk_icon)
235 g_object_unref(previous_gdk_icon);
236 } else if (default_icon_) {
237 SetImage(default_icon_);
238 }
239 bool enabled = extension_->browser_action()->GetIsVisible(tab_id); 224 bool enabled = extension_->browser_action()->GetIsVisible(tab_id);
240 gtk_widget_set_sensitive(button(), enabled); 225 gtk_widget_set_sensitive(button(), enabled);
241 226
242 gtk_widget_queue_draw(button()); 227 gtk_widget_queue_draw(button());
243 } 228 }
244 229
245 SkBitmap GetIcon() { 230 SkBitmap GetIcon() {
246 const SkBitmap& image = extension_->browser_action()->GetIcon( 231 return *extension_->browser_action()->GetIcon(
247 toolbar_->GetCurrentTabId()); 232 toolbar_->GetCurrentTabId(), loaded_icons_).ToSkBitmap();
248 if (!image.isNull()) {
249 return image;
250 } else {
251 return default_skbitmap_;
252 }
253 } 233 }
254 234
255 MenuGtk* GetContextMenu() { 235 MenuGtk* GetContextMenu() {
256 if (!extension_->ShowConfigureContextMenus()) 236 if (!extension_->ShowConfigureContextMenus())
257 return NULL; 237 return NULL;
258 238
259 context_menu_model_ = 239 context_menu_model_ =
260 new ExtensionContextMenuModel(extension_, toolbar_->browser(), this); 240 new ExtensionContextMenuModel(extension_, toolbar_->browser(), this);
261 context_menu_.reset( 241 context_menu_.reset(
262 new MenuGtk(this, context_menu_model_.get())); 242 new MenuGtk(this, context_menu_model_.get()));
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
454 // GTK widget hierarchy invalidates all tooltips and several popular 434 // GTK widget hierarchy invalidates all tooltips and several popular
455 // extensions change browser action icon in a loop. 435 // extensions change browser action icon in a loop.
456 GtkWidget* image_; 436 GtkWidget* image_;
457 437
458 // Loads the button's icons for us on the file thread. 438 // Loads the button's icons for us on the file thread.
459 ImageLoadingTracker tracker_; 439 ImageLoadingTracker tracker_;
460 440
461 // If we are displaying a tab-specific icon, it will be here. 441 // If we are displaying a tab-specific icon, it will be here.
462 GdkPixbuf* tab_specific_icon_; 442 GdkPixbuf* tab_specific_icon_;
463 443
444 // Icons loaded from paths. Currently just the browser action's default icon.
445 ExtensionAction::PathToIconCache loaded_icons_;
446
464 // If the browser action has a default icon, it will be here. 447 // If the browser action has a default icon, it will be here.
465 GdkPixbuf* default_icon_; 448 GdkPixbuf* default_icon_;
466 449
467 // Same as |default_icon_|, but stored as SkBitmap. 450 // Same as |default_icon_|, but stored as SkBitmap.
468 SkBitmap default_skbitmap_; 451 SkBitmap default_skbitmap_;
469 452
470 ui::GtkSignalRegistrar signals_; 453 ui::GtkSignalRegistrar signals_;
471 content::NotificationRegistrar registrar_; 454 content::NotificationRegistrar registrar_;
472 455
473 // The accelerator group used to handle accelerators, owned by this object. 456 // The accelerator group used to handle accelerators, owned by this object.
(...skipping 612 matching lines...) Expand 10 before | Expand all | Expand 10 after
1086 1069
1087 menu->PopupAsContext(gfx::Point(event->x_root, event->y_root), 1070 menu->PopupAsContext(gfx::Point(event->x_root, event->y_root),
1088 event->time); 1071 event->time);
1089 return TRUE; 1072 return TRUE;
1090 } 1073 }
1091 1074
1092 void BrowserActionsToolbarGtk::OnButtonShowOrHide(GtkWidget* sender) { 1075 void BrowserActionsToolbarGtk::OnButtonShowOrHide(GtkWidget* sender) {
1093 if (!resize_animation_.is_animating()) 1076 if (!resize_animation_.is_animating())
1094 UpdateChevronVisibility(); 1077 UpdateChevronVisibility();
1095 } 1078 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698