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/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> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/i18n/rtl.h" | 13 #include "base/i18n/rtl.h" |
14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
16 #include "chrome/browser/extensions/api/commands/command_service.h" | 16 #include "chrome/browser/extensions/api/commands/command_service.h" |
17 #include "chrome/browser/extensions/api/commands/command_service_factory.h" | 17 #include "chrome/browser/extensions/api/commands/command_service_factory.h" |
18 #include "chrome/browser/extensions/extension_action_icon_factory.h" | |
18 #include "chrome/browser/extensions/extension_context_menu_model.h" | 19 #include "chrome/browser/extensions/extension_context_menu_model.h" |
19 #include "chrome/browser/extensions/extension_service.h" | 20 #include "chrome/browser/extensions/extension_service.h" |
20 #include "chrome/browser/extensions/image_loading_tracker.h" | |
21 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
22 #include "chrome/browser/sessions/session_tab_helper.h" | 22 #include "chrome/browser/sessions/session_tab_helper.h" |
23 #include "chrome/browser/ui/browser.h" | 23 #include "chrome/browser/ui/browser.h" |
24 #include "chrome/browser/ui/browser_tabstrip.h" | 24 #include "chrome/browser/ui/browser_tabstrip.h" |
25 #include "chrome/browser/ui/gtk/browser_window_gtk.h" | 25 #include "chrome/browser/ui/gtk/browser_window_gtk.h" |
26 #include "chrome/browser/ui/gtk/extensions/extension_popup_gtk.h" | 26 #include "chrome/browser/ui/gtk/extensions/extension_popup_gtk.h" |
27 #include "chrome/browser/ui/gtk/gtk_chrome_button.h" | 27 #include "chrome/browser/ui/gtk/gtk_chrome_button.h" |
28 #include "chrome/browser/ui/gtk/gtk_chrome_shrinkable_hbox.h" | 28 #include "chrome/browser/ui/gtk/gtk_chrome_shrinkable_hbox.h" |
29 #include "chrome/browser/ui/gtk/gtk_theme_service.h" | 29 #include "chrome/browser/ui/gtk/gtk_theme_service.h" |
30 #include "chrome/browser/ui/gtk/gtk_util.h" | 30 #include "chrome/browser/ui/gtk/gtk_util.h" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
84 gint WidthForIconCount(gint icon_count) { | 84 gint WidthForIconCount(gint icon_count) { |
85 return std::max((kButtonWidth + kButtonPadding) * icon_count - kButtonPadding, | 85 return std::max((kButtonWidth + kButtonPadding) * icon_count - kButtonPadding, |
86 0); | 86 0); |
87 } | 87 } |
88 | 88 |
89 } // namespace | 89 } // namespace |
90 | 90 |
91 using ui::SimpleMenuModel; | 91 using ui::SimpleMenuModel; |
92 | 92 |
93 class BrowserActionButton : public content::NotificationObserver, | 93 class BrowserActionButton : public content::NotificationObserver, |
94 public ImageLoadingTracker::Observer, | 94 public ExtensionActionIconFactory::Observer, |
95 public ExtensionContextMenuModel::PopupDelegate, | 95 public ExtensionContextMenuModel::PopupDelegate, |
96 public MenuGtk::Delegate { | 96 public MenuGtk::Delegate { |
97 public: | 97 public: |
98 BrowserActionButton(BrowserActionsToolbarGtk* toolbar, | 98 BrowserActionButton(BrowserActionsToolbarGtk* toolbar, |
99 const Extension* extension, | 99 const Extension* extension, |
100 GtkThemeService* theme_provider) | 100 GtkThemeService* theme_provider) |
101 : toolbar_(toolbar), | 101 : toolbar_(toolbar), |
102 extension_(extension), | 102 extension_(extension), |
103 image_(NULL), | 103 image_(NULL), |
104 tracker_(this), | 104 icon_factory_(extension, extension->browser_action(), this), |
105 tab_specific_icon_(NULL), | |
106 default_icon_(NULL), | |
107 accel_group_(NULL) { | 105 accel_group_(NULL) { |
108 button_.reset(new CustomDrawButton( | 106 button_.reset(new CustomDrawButton( |
109 theme_provider, | 107 theme_provider, |
110 IDR_BROWSER_ACTION, | 108 IDR_BROWSER_ACTION, |
111 IDR_BROWSER_ACTION_P, | 109 IDR_BROWSER_ACTION_P, |
112 IDR_BROWSER_ACTION_H, | 110 IDR_BROWSER_ACTION_H, |
113 0, | 111 0, |
114 NULL)); | 112 NULL)); |
115 gtk_widget_set_size_request(button(), kButtonWidth, kButtonWidth); | 113 gtk_widget_set_size_request(button(), kButtonWidth, kButtonWidth); |
116 alignment_.Own(gtk_alignment_new(0, 0, 1, 1)); | 114 alignment_.Own(gtk_alignment_new(0, 0, 1, 1)); |
117 gtk_container_add(GTK_CONTAINER(alignment_.get()), button()); | 115 gtk_container_add(GTK_CONTAINER(alignment_.get()), button()); |
118 gtk_widget_show(button()); | 116 gtk_widget_show(button()); |
119 | 117 |
120 DCHECK(extension_->browser_action()); | 118 DCHECK(extension_->browser_action()); |
121 | 119 |
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. | |
124 std::string path = extension_->browser_action()->default_icon_path(); | |
125 if (!path.empty()) { | |
126 tracker_.LoadImage(extension_, extension_->GetResource(path), | |
127 gfx::Size(Extension::kBrowserActionIconMaxSize, | |
128 Extension::kBrowserActionIconMaxSize), | |
129 ImageLoadingTracker::DONT_CACHE); | |
130 } | |
131 | |
132 UpdateState(); | 120 UpdateState(); |
133 | 121 |
134 signals_.Connect(button(), "button-press-event", | 122 signals_.Connect(button(), "button-press-event", |
135 G_CALLBACK(OnButtonPress), this); | 123 G_CALLBACK(OnButtonPress), this); |
136 signals_.Connect(button(), "clicked", | 124 signals_.Connect(button(), "clicked", |
137 G_CALLBACK(OnClicked), this); | 125 G_CALLBACK(OnClicked), this); |
138 signals_.Connect(button(), "drag-begin", | 126 signals_.Connect(button(), "drag-begin", |
139 G_CALLBACK(&OnDragBegin), this); | 127 G_CALLBACK(&OnDragBegin), this); |
140 signals_.ConnectAfter(widget(), "expose-event", | 128 signals_.ConnectAfter(widget(), "expose-event", |
141 G_CALLBACK(OnExposeEvent), this); | 129 G_CALLBACK(OnExposeEvent), this); |
(...skipping 21 matching lines...) Expand all Loading... | |
163 toolbar->browser()->profile()->GetOriginalProfile())); | 151 toolbar->browser()->profile()->GetOriginalProfile())); |
164 registrar_.Add( | 152 registrar_.Add( |
165 this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED, | 153 this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED, |
166 content::Source<Profile>( | 154 content::Source<Profile>( |
167 toolbar->browser()->profile()->GetOriginalProfile())); | 155 toolbar->browser()->profile()->GetOriginalProfile())); |
168 } | 156 } |
169 | 157 |
170 ~BrowserActionButton() { | 158 ~BrowserActionButton() { |
171 DisconnectBrowserActionPopupAccelerator(); | 159 DisconnectBrowserActionPopupAccelerator(); |
172 | 160 |
173 if (tab_specific_icon_) | |
174 g_object_unref(tab_specific_icon_); | |
175 | |
176 if (default_icon_) | |
177 g_object_unref(default_icon_); | |
178 | |
179 alignment_.Destroy(); | 161 alignment_.Destroy(); |
180 } | 162 } |
181 | 163 |
182 GtkWidget* button() { return button_->widget(); } | 164 GtkWidget* button() { return button_->widget(); } |
183 | 165 |
184 GtkWidget* widget() { return alignment_.get(); } | 166 GtkWidget* widget() { return alignment_.get(); } |
185 | 167 |
186 const Extension* extension() { return extension_; } | 168 const Extension* extension() { return extension_; } |
187 | 169 |
188 // NotificationObserver implementation. | 170 // NotificationObserver implementation. |
(...skipping 22 matching lines...) Expand all Loading... | |
211 DisconnectBrowserActionPopupAccelerator(); | 193 DisconnectBrowserActionPopupAccelerator(); |
212 } | 194 } |
213 break; | 195 break; |
214 } | 196 } |
215 default: | 197 default: |
216 NOTREACHED(); | 198 NOTREACHED(); |
217 break; | 199 break; |
218 } | 200 } |
219 } | 201 } |
220 | 202 |
221 // ImageLoadingTracker::Observer implementation. | 203 // ExtensionActionIconFactory::Observer implementation. |
222 void OnImageLoaded(const gfx::Image& image, | 204 void OnIconUpdated() OVERRIDE { |
223 const std::string& extension_id, | |
224 int index) OVERRIDE { | |
225 extension_->browser_action()->CacheIcon(image); | |
226 UpdateState(); | 205 UpdateState(); |
227 } | 206 } |
228 | 207 |
229 // Updates the button based on the latest state from the associated | 208 // Updates the button based on the latest state from the associated |
230 // browser action. | 209 // browser action. |
231 void UpdateState() { | 210 void UpdateState() { |
232 int tab_id = toolbar_->GetCurrentTabId(); | 211 int tab_id = toolbar_->GetCurrentTabId(); |
233 if (tab_id < 0) | 212 if (tab_id < 0) |
234 return; | 213 return; |
235 | 214 |
236 std::string tooltip = extension_->browser_action()->GetTitle(tab_id); | 215 std::string tooltip = extension_->browser_action()->GetTitle(tab_id); |
237 if (tooltip.empty()) | 216 if (tooltip.empty()) |
238 gtk_widget_set_has_tooltip(button(), FALSE); | 217 gtk_widget_set_has_tooltip(button(), FALSE); |
239 else | 218 else |
240 gtk_widget_set_tooltip_text(button(), tooltip.c_str()); | 219 gtk_widget_set_tooltip_text(button(), tooltip.c_str()); |
241 | 220 |
242 gfx::Image image = extension_->browser_action()->GetIcon(tab_id); | 221 gfx::Image image = icon_factory_.GetIcon(tab_id); |
243 if (!image.IsEmpty()) | 222 if (!image.IsEmpty()) |
244 SetImage(image.ToGdkPixbuf()); | 223 SetImage(image.ToGdkPixbuf()); |
245 bool enabled = extension_->browser_action()->GetIsVisible(tab_id); | 224 bool enabled = extension_->browser_action()->GetIsVisible(tab_id); |
246 gtk_widget_set_sensitive(button(), enabled); | 225 gtk_widget_set_sensitive(button(), enabled); |
247 | 226 |
248 gtk_widget_queue_draw(button()); | 227 gtk_widget_queue_draw(button()); |
249 } | 228 } |
250 | 229 |
251 gfx::Image GetIcon() { | 230 gfx::Image GetIcon() { |
252 return extension_->browser_action()->GetIcon( | 231 return icon_factory_.GetIcon(toolbar_->GetCurrentTabId()); |
253 toolbar_->GetCurrentTabId()); | |
254 } | 232 } |
255 | 233 |
256 MenuGtk* GetContextMenu() { | 234 MenuGtk* GetContextMenu() { |
257 if (!extension_->ShowConfigureContextMenus()) | 235 if (!extension_->ShowConfigureContextMenus()) |
258 return NULL; | 236 return NULL; |
259 | 237 |
260 context_menu_model_ = | 238 context_menu_model_ = |
261 new ExtensionContextMenuModel(extension_, toolbar_->browser(), this); | 239 new ExtensionContextMenuModel(extension_, toolbar_->browser(), this); |
262 context_menu_.reset( | 240 context_menu_.reset( |
263 new MenuGtk(this, context_menu_model_.get())); | 241 new MenuGtk(this, context_menu_model_.get())); |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
454 | 432 |
455 // The top level widget (parent of |button_|). | 433 // The top level widget (parent of |button_|). |
456 ui::OwnedWidgetGtk alignment_; | 434 ui::OwnedWidgetGtk alignment_; |
457 | 435 |
458 // The one image subwidget in |button_|. We keep this out so we don't alter | 436 // The one image subwidget in |button_|. We keep this out so we don't alter |
459 // the widget hierarchy while changing the button image because changing the | 437 // the widget hierarchy while changing the button image because changing the |
460 // GTK widget hierarchy invalidates all tooltips and several popular | 438 // GTK widget hierarchy invalidates all tooltips and several popular |
461 // extensions change browser action icon in a loop. | 439 // extensions change browser action icon in a loop. |
462 GtkWidget* image_; | 440 GtkWidget* image_; |
463 | 441 |
464 // Loads the button's icons for us on the file thread. | 442 // The object that will be used to get the browser action icon for us. |
465 ImageLoadingTracker tracker_; | 443 // It may load the icon asynchronously (in which case the initial icon |
466 | 444 // returned by the factory will be transparent), so we have to observe it for |
467 // If we are displaying a tab-specific icon, it will be here. | 445 // updates to the icon. |
468 GdkPixbuf* tab_specific_icon_; | 446 ExtensionActionIconFactory icon_factory_; |
Jeffrey Yasskin
2012/09/17 20:18:39
Remove the second space between ExtensionActionIco
tbarzic
2012/09/17 22:06:10
Done.
| |
469 | |
470 // If the browser action has a default icon, it will be here. | |
471 GdkPixbuf* default_icon_; | |
472 | 447 |
473 // Same as |default_icon_|, but stored as SkBitmap. | 448 // Same as |default_icon_|, but stored as SkBitmap. |
474 SkBitmap default_skbitmap_; | 449 SkBitmap default_skbitmap_; |
475 | 450 |
476 ui::GtkSignalRegistrar signals_; | 451 ui::GtkSignalRegistrar signals_; |
477 content::NotificationRegistrar registrar_; | 452 content::NotificationRegistrar registrar_; |
478 | 453 |
479 // The accelerator group used to handle accelerators, owned by this object. | 454 // The accelerator group used to handle accelerators, owned by this object. |
480 GtkAccelGroup* accel_group_; | 455 GtkAccelGroup* accel_group_; |
481 | 456 |
(...skipping 611 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1093 | 1068 |
1094 menu->PopupAsContext(gfx::Point(event->x_root, event->y_root), | 1069 menu->PopupAsContext(gfx::Point(event->x_root, event->y_root), |
1095 event->time); | 1070 event->time); |
1096 return TRUE; | 1071 return TRUE; |
1097 } | 1072 } |
1098 | 1073 |
1099 void BrowserActionsToolbarGtk::OnButtonShowOrHide(GtkWidget* sender) { | 1074 void BrowserActionsToolbarGtk::OnButtonShowOrHide(GtkWidget* sender) { |
1100 if (!resize_animation_.is_animating()) | 1075 if (!resize_animation_.is_animating()) |
1101 UpdateChevronVisibility(); | 1076 UpdateChevronVisibility(); |
1102 } | 1077 } |
OLD | NEW |