Chromium Code Reviews

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

Issue 25305002: Implement initial chrome.browserAction.openPopup API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix obj-c Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
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 234 matching lines...)
245 return NULL; 245 return NULL;
246 246
247 context_menu_model_ = 247 context_menu_model_ =
248 new ExtensionContextMenuModel(extension_, toolbar_->browser(), this); 248 new ExtensionContextMenuModel(extension_, toolbar_->browser(), this);
249 context_menu_.reset( 249 context_menu_.reset(
250 new MenuGtk(this, context_menu_model_.get())); 250 new MenuGtk(this, context_menu_model_.get()));
251 return context_menu_.get(); 251 return context_menu_.get();
252 } 252 }
253 253
254 private: 254 private:
255 // Activate the browser action. 255 // Activate the browser action. Returns true if a popup was shown. Showing the
256 void Activate(GtkWidget* widget) { 256 // popup will grant tab permissions if |should_grant| is true. Popup's shown
257 // via an API should not grant permissions.
258 bool Activate(GtkWidget* widget, bool should_grant) {
257 ExtensionToolbarModel* model = toolbar_->model(); 259 ExtensionToolbarModel* model = toolbar_->model();
258 const Extension* extension = extension_; 260 const Extension* extension = extension_;
259 Browser* browser = toolbar_->browser(); 261 Browser* browser = toolbar_->browser();
260 GURL popup_url; 262 GURL popup_url;
261 263
262 switch (model->ExecuteBrowserAction(extension, browser, &popup_url)) { 264 switch (model->ExecuteBrowserAction(
265 extension, browser, &popup_url, should_grant)) {
263 case ExtensionToolbarModel::ACTION_NONE: 266 case ExtensionToolbarModel::ACTION_NONE:
264 break; 267 break;
265 case ExtensionToolbarModel::ACTION_SHOW_POPUP: 268 case ExtensionToolbarModel::ACTION_SHOW_POPUP:
266 ExtensionPopupGtk::Show(popup_url, browser, widget, 269 ExtensionPopupGtk::Show(popup_url, browser, widget,
267 ExtensionPopupGtk::SHOW); 270 ExtensionPopupGtk::SHOW);
268 break; 271 return true;
269 } 272 }
273 return false;
270 } 274 }
271 275
272 // MenuGtk::Delegate implementation. 276 // MenuGtk::Delegate implementation.
273 virtual void StoppedShowing() OVERRIDE { 277 virtual void StoppedShowing() OVERRIDE {
274 if (enabled_) 278 if (enabled_)
275 button_->UnsetPaintOverride(); 279 button_->UnsetPaintOverride();
276 else 280 else
277 button_->SetPaintOverride(GTK_STATE_INSENSITIVE); 281 button_->SetPaintOverride(GTK_STATE_INSENSITIVE);
278 282
279 // If the context menu was showing for the overflow menu, re-assert the 283 // If the context menu was showing for the overflow menu, re-assert the
(...skipping 36 matching lines...)
316 return FALSE; 320 return FALSE;
317 321
318 button->button_->SetPaintOverride(GTK_STATE_ACTIVE); 322 button->button_->SetPaintOverride(GTK_STATE_ACTIVE);
319 menu->PopupForWidget(widget, event->button, event->time); 323 menu->PopupForWidget(widget, event->button, event->time);
320 324
321 return TRUE; 325 return TRUE;
322 } 326 }
323 327
324 static void OnClicked(GtkWidget* widget, BrowserActionButton* button) { 328 static void OnClicked(GtkWidget* widget, BrowserActionButton* button) {
325 if (button->enabled_) 329 if (button->enabled_)
326 button->Activate(widget); 330 button->Activate(widget, true);
327 } 331 }
328 332
329 static gboolean OnExposeEvent(GtkWidget* widget, 333 static gboolean OnExposeEvent(GtkWidget* widget,
330 GdkEventExpose* event, 334 GdkEventExpose* event,
331 BrowserActionButton* button) { 335 BrowserActionButton* button) {
332 int tab_id = button->toolbar_->GetCurrentTabId(); 336 int tab_id = button->toolbar_->GetCurrentTabId();
333 if (tab_id < 0) 337 if (tab_id < 0)
334 return FALSE; 338 return FALSE;
335 339
336 ExtensionAction* action = button->browser_action(); 340 ExtensionAction* action = button->browser_action();
(...skipping 20 matching lines...)
357 static gboolean OnGtkAccelerator(GtkAccelGroup* accel_group, 361 static gboolean OnGtkAccelerator(GtkAccelGroup* accel_group,
358 GObject* acceleratable, 362 GObject* acceleratable,
359 guint keyval, 363 guint keyval,
360 GdkModifierType modifier, 364 GdkModifierType modifier,
361 BrowserActionButton* button) { 365 BrowserActionButton* button) {
362 // Open the popup for this extension. 366 // Open the popup for this extension.
363 GtkWidget* anchor = button->widget(); 367 GtkWidget* anchor = button->widget();
364 // The anchor might be in the overflow menu. Then we point to the chevron. 368 // The anchor might be in the overflow menu. Then we point to the chevron.
365 if (!gtk_widget_get_visible(anchor)) 369 if (!gtk_widget_get_visible(anchor))
366 anchor = button->toolbar_->chevron(); 370 anchor = button->toolbar_->chevron();
367 button->Activate(anchor); 371 button->Activate(anchor, true);
368 return TRUE; 372 return TRUE;
369 } 373 }
370 374
371 // The handler for when the browser action is realized. |user_data| contains a 375 // The handler for when the browser action is realized. |user_data| contains a
372 // pointer to the BrowserAction shown. 376 // pointer to the BrowserAction shown.
373 static void OnRealize(GtkWidget* widget, void* user_data) { 377 static void OnRealize(GtkWidget* widget, void* user_data) {
374 BrowserActionButton* button = static_cast<BrowserActionButton*>(user_data); 378 BrowserActionButton* button = static_cast<BrowserActionButton*>(user_data);
375 button->ConnectBrowserActionPopupAccelerator(); 379 button->ConnectBrowserActionPopupAccelerator();
376 } 380 }
377 381
(...skipping 284 matching lines...)
662 signals_.Connect(button->widget(), "show", 666 signals_.Connect(button->widget(), "show",
663 G_CALLBACK(&OnButtonShowOrHideThunk), this); 667 G_CALLBACK(&OnButtonShowOrHideThunk), this);
664 signals_.Connect(button->widget(), "hide", 668 signals_.Connect(button->widget(), "hide",
665 G_CALLBACK(&OnButtonShowOrHideThunk), this); 669 G_CALLBACK(&OnButtonShowOrHideThunk), this);
666 670
667 gtk_widget_show(button->widget()); 671 gtk_widget_show(button->widget());
668 672
669 UpdateVisibility(); 673 UpdateVisibility();
670 } 674 }
671 675
676 BrowserActionButton* BrowserActionsToolbarGtk::GetBrowserActionButton(
677 const Extension* extension) {
678 ExtensionButtonMap::iterator it = extension_button_map_.find(
679 extension->id());
680 return it == extension_button_map_.end() ? NULL : it->second.get();
681 }
682
672 GtkWidget* BrowserActionsToolbarGtk::GetBrowserActionWidget( 683 GtkWidget* BrowserActionsToolbarGtk::GetBrowserActionWidget(
673 const Extension* extension) { 684 const Extension* extension) {
674 ExtensionButtonMap::iterator it = extension_button_map_.find( 685 BrowserActionButton* button = GetBrowserActionButton(extension);
675 extension->id()); 686 return button == NULL ? NULL : button->widget();
676 if (it == extension_button_map_.end())
677 return NULL;
678
679 return it->second.get()->widget();
680 } 687 }
681 688
682 void BrowserActionsToolbarGtk::RemoveButtonForExtension( 689 void BrowserActionsToolbarGtk::RemoveButtonForExtension(
683 const Extension* extension) { 690 const Extension* extension) {
684 if (extension_button_map_.erase(extension->id())) 691 if (extension_button_map_.erase(extension->id()))
685 UpdateVisibility(); 692 UpdateVisibility();
686 UpdateChevronVisibility(); 693 UpdateChevronVisibility();
687 } 694 }
688 695
689 void BrowserActionsToolbarGtk::UpdateVisibility() { 696 void BrowserActionsToolbarGtk::UpdateVisibility() {
(...skipping 71 matching lines...)
761 NOTREACHED(); 768 NOTREACHED();
762 return; 769 return;
763 } 770 }
764 771
765 if (profile_->IsOffTheRecord()) 772 if (profile_->IsOffTheRecord())
766 index = model_->OriginalIndexToIncognito(index); 773 index = model_->OriginalIndexToIncognito(index);
767 774
768 gtk_box_reorder_child(GTK_BOX(button_hbox_.get()), button_widget, index); 775 gtk_box_reorder_child(GTK_BOX(button_hbox_.get()), button_widget, index);
769 } 776 }
770 777
778 bool BrowserActionsToolbarGtk::BrowserActionShowPopup(
779 const Extension* extension) {
780 // Do not override other popups and only show in active window.
781 if (ExtensionPopupGtk::get_current_extension_popup() ||
782 !browser_->window()->IsActive()) {
783 return false;
784 }
785
786 BrowserActionButton* button = GetBrowserActionButton(extension);
787 if (button == NULL || button->widget() == NULL)
788 return false;
789
790 GtkWidget* anchor = button->widget();
791 if (!gtk_widget_get_visible(anchor))
792 anchor = button->toolbar_->chevron();
793 return button->Activate(anchor, false);
794 }
795
771 void BrowserActionsToolbarGtk::ModelLoaded() { 796 void BrowserActionsToolbarGtk::ModelLoaded() {
772 SetContainerWidth(); 797 SetContainerWidth();
773 } 798 }
774 799
775 void BrowserActionsToolbarGtk::AnimationProgressed( 800 void BrowserActionsToolbarGtk::AnimationProgressed(
776 const gfx::Animation* animation) { 801 const gfx::Animation* animation) {
777 int width = start_width_ + (desired_width_ - start_width_) * 802 int width = start_width_ + (desired_width_ - start_width_) *
778 animation->GetCurrentValue(); 803 animation->GetCurrentValue();
779 gtk_widget_set_size_request(button_hbox_.get(), width, -1); 804 gtk_widget_set_size_request(button_hbox_.get(), width, -1);
780 805
(...skipping 19 matching lines...)
800 bool BrowserActionsToolbarGtk::GetAcceleratorForCommandId( 825 bool BrowserActionsToolbarGtk::GetAcceleratorForCommandId(
801 int command_id, 826 int command_id,
802 ui::Accelerator* accelerator) { 827 ui::Accelerator* accelerator) {
803 return false; 828 return false;
804 } 829 }
805 830
806 void BrowserActionsToolbarGtk::ExecuteCommand(int command_id, int event_flags) { 831 void BrowserActionsToolbarGtk::ExecuteCommand(int command_id, int event_flags) {
807 const Extension* extension = model_->toolbar_items()[command_id].get(); 832 const Extension* extension = model_->toolbar_items()[command_id].get();
808 GURL popup_url; 833 GURL popup_url;
809 834
810 switch (model_->ExecuteBrowserAction(extension, browser(), &popup_url)) { 835 switch (model_->ExecuteBrowserAction(
836 extension,browser(), &popup_url, true)) {
811 case ExtensionToolbarModel::ACTION_NONE: 837 case ExtensionToolbarModel::ACTION_NONE:
812 break; 838 break;
813 case ExtensionToolbarModel::ACTION_SHOW_POPUP: 839 case ExtensionToolbarModel::ACTION_SHOW_POPUP:
814 ExtensionPopupGtk::Show(popup_url, browser(), chevron(), 840 ExtensionPopupGtk::Show(popup_url, browser(), chevron(),
815 ExtensionPopupGtk::SHOW); 841 ExtensionPopupGtk::SHOW);
816 break; 842 break;
817 } 843 }
818 } 844 }
819 845
820 void BrowserActionsToolbarGtk::StoppedShowing() { 846 void BrowserActionsToolbarGtk::StoppedShowing() {
(...skipping 249 matching lines...)
1070 NOTREACHED(); 1096 NOTREACHED();
1071 return FALSE; 1097 return FALSE;
1072 } 1098 }
1073 1099
1074 item_index += gtk_chrome_shrinkable_hbox_get_visible_child_count( 1100 item_index += gtk_chrome_shrinkable_hbox_get_visible_child_count(
1075 GTK_CHROME_SHRINKABLE_HBOX(button_hbox_.get())); 1101 GTK_CHROME_SHRINKABLE_HBOX(button_hbox_.get()));
1076 if (profile_->IsOffTheRecord()) 1102 if (profile_->IsOffTheRecord())
1077 item_index = model_->IncognitoIndexToOriginal(item_index); 1103 item_index = model_->IncognitoIndexToOriginal(item_index);
1078 1104
1079 const Extension* extension = model_->toolbar_items()[item_index].get(); 1105 const Extension* extension = model_->toolbar_items()[item_index].get();
1080 ExtensionButtonMap::iterator it = extension_button_map_.find(extension->id()); 1106 BrowserActionButton* button = GetBrowserActionButton(extension);
1081 if (it == extension_button_map_.end()) { 1107 if (button == NULL) {
1082 NOTREACHED(); 1108 NOTREACHED();
1083 return FALSE; 1109 return FALSE;
1084 } 1110 }
1085 1111
1086 MenuGtk* menu = it->second.get()->GetContextMenu(); 1112 MenuGtk* menu = button->GetContextMenu();
1087 if (!menu) 1113 if (!menu)
1088 return FALSE; 1114 return FALSE;
1089 1115
1090 menu->PopupAsContext(gfx::Point(event->x_root, event->y_root), 1116 menu->PopupAsContext(gfx::Point(event->x_root, event->y_root),
1091 event->time); 1117 event->time);
1092 return TRUE; 1118 return TRUE;
1093 } 1119 }
1094 1120
1095 void BrowserActionsToolbarGtk::OnButtonShowOrHide(GtkWidget* sender) { 1121 void BrowserActionsToolbarGtk::OnButtonShowOrHide(GtkWidget* sender) {
1096 if (!resize_animation_.is_animating()) 1122 if (!resize_animation_.is_animating())
1097 UpdateChevronVisibility(); 1123 UpdateChevronVisibility();
1098 } 1124 }
OLDNEW

Powered by Google App Engine