Chromium Code Reviews| Index: chrome/browser/ui/toolbar/media_router_action.cc |
| diff --git a/chrome/browser/ui/toolbar/media_router_action.cc b/chrome/browser/ui/toolbar/media_router_action.cc |
| index 97c60b2a4d4204916f94fb4734162b036ff85d49..319d9653f0ed80808e9cd16e406f7edc7dab467d 100644 |
| --- a/chrome/browser/ui/toolbar/media_router_action.cc |
| +++ b/chrome/browser/ui/toolbar/media_router_action.cc |
| @@ -17,8 +17,13 @@ |
| #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h" |
| #include "chrome/browser/ui/toolbar/media_router_action_platform_delegate.h" |
| #include "chrome/browser/ui/toolbar/toolbar_action_view_delegate.h" |
| +#include "chrome/browser/ui/toolbar/toolbar_actions_model.h" |
| #include "chrome/browser/ui/webui/media_router/media_router_dialog_controller_impl.h" |
| +#include "chrome/common/pref_names.h" |
| #include "chrome/grit/generated_resources.h" |
| +#include "components/prefs/pref_service.h" |
| +#include "components/prefs/pref_service_factory.h" |
| +#include "grit/theme_resources.h" |
| #include "ui/base/l10n/l10n_util.h" |
| #include "ui/gfx/color_palette.h" |
| #include "ui/gfx/image/image_skia.h" |
| @@ -47,11 +52,13 @@ MediaRouterAction::MediaRouterAction(Browser* browser, |
| platform_delegate_(MediaRouterActionPlatformDelegate::Create(browser)), |
| contextual_menu_(browser), |
| tab_strip_model_observer_(this), |
| + toolbar_actions_bar_observer_(this), |
| weak_ptr_factory_(this) { |
| DCHECK(browser_); |
| DCHECK(toolbar_actions_bar_); |
| tab_strip_model_observer_.Add(browser_->tab_strip_model()); |
| - |
| + toolbar_actions_bar_observer_.Add(toolbar_actions_bar_); |
| + contextual_menu_.SetMediaRouterAction(this); |
| RegisterObserver(); |
| } |
| @@ -113,7 +120,6 @@ bool MediaRouterAction::HasPopup( |
| void MediaRouterAction::HidePopup() { |
| GetMediaRouterDialogController()->HideMediaRouterDialog(); |
| - OnPopupHidden(); |
| } |
| gfx::NativeView MediaRouterAction::GetPopupNativeView() { |
| @@ -166,6 +172,7 @@ void MediaRouterAction::OnRoutesUpdated( |
| return route.is_local() && route.for_display(); |
| }) != routes.end(); |
| MaybeUpdateIcon(); |
| + MaybeRemoveAction(); |
| } |
| void MediaRouterAction::ActiveTabChanged(content::WebContents* old_contents, |
| @@ -173,11 +180,23 @@ void MediaRouterAction::ActiveTabChanged(content::WebContents* old_contents, |
| int index, |
| int reason) { |
| UpdatePopupState(); |
| + MaybeRemoveAction(); |
| +} |
| + |
| +void MediaRouterAction::OnToolbarActionsBarAnimationEnded() { |
| + // Depress the action if the dialog is shown, release it otherwise. |
|
Devlin
2016/08/02 20:18:51
What if the animation was unrelated? Does it matt
takumif
2016/08/03 22:03:51
In that case this does nothing, as the icon would
|
| + if (delegate_) { |
| + if (GetMediaRouterDialogController()->IsShowingMediaRouterDialog()) |
| + delegate_->OnPopupShown(true); |
| + else |
| + delegate_->OnPopupClosed(); |
| + } |
| } |
| void MediaRouterAction::OnPopupHidden() { |
| if (delegate_) |
| delegate_->OnPopupClosed(); |
| + MaybeRemoveAction(); |
| } |
| void MediaRouterAction::OnPopupShown() { |
| @@ -187,6 +206,12 @@ void MediaRouterAction::OnPopupShown() { |
| delegate_->OnPopupShown(true); |
| } |
| +void MediaRouterAction::ToggleVisibilityPreference() { |
| + browser_->profile()->GetPrefs()->SetBoolean( |
| + prefs::kMediaRouterAlwaysShowActionIcon, !ShouldAlwaysShowIcon()); |
| + MaybeRemoveAction(); |
| +} |
| + |
| void MediaRouterAction::UpdatePopupState() { |
| MediaRouterDialogControllerImpl* controller = |
| GetMediaRouterDialogController(); |
| @@ -203,11 +228,14 @@ void MediaRouterAction::UpdatePopupState() { |
| controller->SetMediaRouterAction(weak_ptr_factory_.GetWeakPtr()); |
| // Update the button in case the pressed state is out of sync with dialog |
| - // visibility. |
| - if (controller->IsShowingMediaRouterDialog()) |
| - OnPopupShown(); |
| - else |
| - OnPopupHidden(); |
| + // visibility. If we just added the ephemeral icon to the toolbar and need to |
| + // depress it, we do that in |OnToolbarActionsBarAnimationEnded|. |
| + if (delegate_) { |
| + if (ShouldAlwaysShowIcon() && controller->IsShowingMediaRouterDialog()) |
| + delegate_->OnPopupShown(true); |
| + else if (!controller->IsShowingMediaRouterDialog()) |
| + delegate_->OnPopupClosed(); |
| + } |
| } |
| MediaRouterDialogControllerImpl* |
| @@ -237,6 +265,24 @@ void MediaRouterAction::MaybeUpdateIcon() { |
| } |
| } |
| +void MediaRouterAction::MaybeRemoveAction() { |
| + if (!delegate_) |
| + return; |
| + |
| + ToolbarActionsModel* model = ToolbarActionsModel::Get(browser_->profile()); |
| + if (!ShouldAlwaysShowIcon() && |
| + !GetMediaRouterDialogController()->IsShowingMediaRouterDialog() && |
| + !has_local_display_route_ && |
| + model->HasComponentAction(GetId())) { |
|
Devlin
2016/08/02 20:18:51
Here still, when would this object exist without a
takumif
2016/08/03 22:03:50
This was also only for tests. Changed the tests an
|
| + model->RemoveComponentAction(GetId()); |
| + } |
| +} |
| + |
| +bool MediaRouterAction::ShouldAlwaysShowIcon() { |
| + PrefService* pref_service = browser_->profile()->GetPrefs(); |
| + return pref_service->GetBoolean(prefs::kMediaRouterAlwaysShowActionIcon); |
| +} |
| + |
| gfx::VectorIconId MediaRouterAction::GetCurrentIcon() const { |
| // Highest priority is to indicate whether there's an issue. |
| if (issue_) { |
| @@ -249,3 +295,7 @@ gfx::VectorIconId MediaRouterAction::GetCurrentIcon() const { |
| return has_local_display_route_ ? gfx::VectorIconId::MEDIA_ROUTER_ACTIVE |
| : gfx::VectorIconId::MEDIA_ROUTER_IDLE; |
| } |
| + |
| +base::WeakPtr<MediaRouterAction> MediaRouterAction::GetWeakPtr() { |
| + return weak_ptr_factory_.GetWeakPtr(); |
| +} |