| 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_actions_container.h" | 5 #include "chrome/browser/ui/views/browser_actions_container.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 size_t visible_actions = 0; | 513 size_t visible_actions = 0; |
| 514 for (size_t i = 0; i < browser_action_views_.size(); ++i) { | 514 for (size_t i = 0; i < browser_action_views_.size(); ++i) { |
| 515 if (browser_action_views_[i]->visible()) | 515 if (browser_action_views_[i]->visible()) |
| 516 ++visible_actions; | 516 ++visible_actions; |
| 517 } | 517 } |
| 518 return visible_actions; | 518 return visible_actions; |
| 519 } | 519 } |
| 520 | 520 |
| 521 void BrowserActionsContainer::OnBrowserActionExecuted( | 521 void BrowserActionsContainer::OnBrowserActionExecuted( |
| 522 BrowserActionButton* button) { | 522 BrowserActionButton* button) { |
| 523 ExtensionAction* browser_action = button->browser_action(); | 523 const Extension* extension = button->extension(); |
| 524 | 524 GURL popup_url; |
| 525 // Popups just display. No notification to the extension. | 525 switch (model_->ExecuteBrowserAction(extension, browser_, &popup_url)) { |
| 526 // TODO(erikkay): should there be? | 526 case ExtensionToolbarModel::ACTION_NONE: |
| 527 if (!button->IsPopup()) { | 527 break; |
| 528 model_->ExecuteBrowserAction(browser_action->extension_id(), browser_); | 528 case ExtensionToolbarModel::ACTION_SHOW_POPUP: |
| 529 return; | 529 ShowPopup(button, popup_url); |
| 530 break; |
| 530 } | 531 } |
| 531 | |
| 532 // If we're showing the same popup, just hide it and return. | |
| 533 bool same_showing = popup_ && button == popup_button_; | |
| 534 | |
| 535 // Always hide the current popup, even if it's not the same. | |
| 536 // Only one popup should be visible at a time. | |
| 537 HidePopup(); | |
| 538 | |
| 539 if (same_showing) | |
| 540 return; | |
| 541 | |
| 542 // We can get the execute event for browser actions that are not visible, | |
| 543 // since buttons can be activated from the overflow menu (chevron). In that | |
| 544 // case we show the popup as originating from the chevron. | |
| 545 View* reference_view = button->parent()->visible() ? button : chevron_; | |
| 546 views::BubbleBorder::ArrowLocation arrow_location = base::i18n::IsRTL() ? | |
| 547 views::BubbleBorder::TOP_LEFT : views::BubbleBorder::TOP_RIGHT; | |
| 548 popup_ = ExtensionPopup::ShowPopup(button->GetPopupUrl(), browser_, | |
| 549 reference_view, arrow_location); | |
| 550 popup_->GetWidget()->AddObserver(this); | |
| 551 popup_button_ = button; | |
| 552 popup_button_->SetButtonPushed(); | |
| 553 } | 532 } |
| 554 | 533 |
| 555 gfx::Size BrowserActionsContainer::GetPreferredSize() { | 534 gfx::Size BrowserActionsContainer::GetPreferredSize() { |
| 556 if (browser_action_views_.empty()) | 535 if (browser_action_views_.empty()) |
| 557 return gfx::Size(ToolbarView::kStandardSpacing, 0); | 536 return gfx::Size(ToolbarView::kStandardSpacing, 0); |
| 558 | 537 |
| 559 // We calculate the size of the view by taking the current width and | 538 // We calculate the size of the view by taking the current width and |
| 560 // subtracting resize_amount_ (the latter represents how far the user is | 539 // subtracting resize_amount_ (the latter represents how far the user is |
| 561 // resizing the view or, if animating the snapping, how far to animate it). | 540 // resizing the view or, if animating the snapping, how far to animate it). |
| 562 // But we also clamp it to a minimum size and the maximum size, so that the | 541 // But we also clamp it to a minimum size and the maximum size, so that the |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 836 void BrowserActionsContainer::MoveBrowserAction(const std::string& extension_id, | 815 void BrowserActionsContainer::MoveBrowserAction(const std::string& extension_id, |
| 837 size_t new_index) { | 816 size_t new_index) { |
| 838 ExtensionService* service = profile_->GetExtensionService(); | 817 ExtensionService* service = profile_->GetExtensionService(); |
| 839 if (service) { | 818 if (service) { |
| 840 const Extension* extension = service->GetExtensionById(extension_id, false); | 819 const Extension* extension = service->GetExtensionById(extension_id, false); |
| 841 model_->MoveBrowserAction(extension, new_index); | 820 model_->MoveBrowserAction(extension, new_index); |
| 842 SchedulePaint(); | 821 SchedulePaint(); |
| 843 } | 822 } |
| 844 } | 823 } |
| 845 | 824 |
| 825 void BrowserActionsContainer::ShowPopup(BrowserActionButton* button, |
| 826 const GURL& popup_url) { |
| 827 // If we're showing the same popup, just hide it and return. |
| 828 bool same_showing = popup_ && button == popup_button_; |
| 829 |
| 830 // Always hide the current popup, even if it's not the same. |
| 831 // Only one popup should be visible at a time. |
| 832 HidePopup(); |
| 833 |
| 834 if (same_showing) |
| 835 return; |
| 836 |
| 837 // We can get the execute event for browser actions that are not visible, |
| 838 // since buttons can be activated from the overflow menu (chevron). In that |
| 839 // case we show the popup as originating from the chevron. |
| 840 View* reference_view = button->parent()->visible() ? button : chevron_; |
| 841 views::BubbleBorder::ArrowLocation arrow_location = base::i18n::IsRTL() ? |
| 842 views::BubbleBorder::TOP_LEFT : views::BubbleBorder::TOP_RIGHT; |
| 843 popup_ = ExtensionPopup::ShowPopup(popup_url, |
| 844 browser_, |
| 845 reference_view, |
| 846 arrow_location); |
| 847 popup_->GetWidget()->AddObserver(this); |
| 848 popup_button_ = button; |
| 849 popup_button_->SetButtonPushed(); |
| 850 } |
| 851 |
| 846 void BrowserActionsContainer::HidePopup() { | 852 void BrowserActionsContainer::HidePopup() { |
| 847 // Remove this as an observer and clear |popup_| and |popup_button_| here, | 853 // Remove this as an observer and clear |popup_| and |popup_button_| here, |
| 848 // since we might change them before OnWidgetClosing() gets called. | 854 // since we might change them before OnWidgetClosing() gets called. |
| 849 if (popup_) { | 855 if (popup_) { |
| 850 popup_->GetWidget()->RemoveObserver(this); | 856 popup_->GetWidget()->RemoveObserver(this); |
| 851 popup_->GetWidget()->Close(); | 857 popup_->GetWidget()->Close(); |
| 852 popup_ = NULL; | 858 popup_ = NULL; |
| 853 } | 859 } |
| 854 if (popup_button_) { | 860 if (popup_button_) { |
| 855 popup_button_->SetButtonNotPushed(); | 861 popup_button_->SetButtonNotPushed(); |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1135 } | 1141 } |
| 1136 } | 1142 } |
| 1137 | 1143 |
| 1138 bool BrowserActionsContainer::ShouldDisplayBrowserAction( | 1144 bool BrowserActionsContainer::ShouldDisplayBrowserAction( |
| 1139 const Extension* extension) { | 1145 const Extension* extension) { |
| 1140 // Only display incognito-enabled extensions while in incognito mode. | 1146 // Only display incognito-enabled extensions while in incognito mode. |
| 1141 return | 1147 return |
| 1142 (!profile_->IsOffTheRecord() || | 1148 (!profile_->IsOffTheRecord() || |
| 1143 profile_->GetExtensionService()->IsIncognitoEnabled(extension->id())); | 1149 profile_->GetExtensionService()->IsIncognitoEnabled(extension->id())); |
| 1144 } | 1150 } |
| OLD | NEW |