summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/views/tabs/media_indicator_button.cc
blob: f1c5267e314dacc644954f96b8428a5f8c23577f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/tabs/media_indicator_button.h"

#include "chrome/browser/ui/views/tabs/tab.h"
#include "chrome/browser/ui/views/tabs/tab_controller.h"
#include "chrome/browser/ui/views/tabs/tab_renderer_data.h"
#include "content/public/browser/user_metrics.h"
#include "ui/gfx/animation/animation_delegate.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/image/image.h"

using base::UserMetricsAction;

const char MediaIndicatorButton::kViewClassName[] = "MediaIndicatorButton";

class MediaIndicatorButton::FadeAnimationDelegate
    : public gfx::AnimationDelegate {
 public:
  explicit FadeAnimationDelegate(MediaIndicatorButton* button)
      : button_(button) {}
  ~FadeAnimationDelegate() override {}

 private:
  // gfx::AnimationDelegate
  void AnimationProgressed(const gfx::Animation* animation) override {
    button_->SchedulePaint();
  }

  void AnimationCanceled(const gfx::Animation* animation) override {
    button_->showing_media_state_ = button_->media_state_;
    button_->SchedulePaint();
  }

  void AnimationEnded(const gfx::Animation* animation) override {
    button_->showing_media_state_ = button_->media_state_;
    button_->SchedulePaint();
  }

  MediaIndicatorButton* const button_;

  DISALLOW_COPY_AND_ASSIGN(FadeAnimationDelegate);
};

MediaIndicatorButton::MediaIndicatorButton()
    : views::ImageButton(NULL),
      media_state_(TAB_MEDIA_STATE_NONE),
      showing_media_state_(TAB_MEDIA_STATE_NONE) {
  SetEventTargeter(
      scoped_ptr<views::ViewTargeter>(new views::ViewTargeter(this)));
}

MediaIndicatorButton::~MediaIndicatorButton() {}

void MediaIndicatorButton::TransitionToMediaState(TabMediaState next_state) {
  if (next_state == media_state_)
    return;

  if (next_state != TAB_MEDIA_STATE_NONE) {
    const gfx::ImageSkia* const indicator_image =
        chrome::GetTabMediaIndicatorImage(next_state).ToImageSkia();
    SetImage(views::CustomButton::STATE_NORMAL, indicator_image);
    SetImage(views::CustomButton::STATE_DISABLED, indicator_image);
    const gfx::ImageSkia* const affordance_image =
        chrome::GetTabMediaIndicatorAffordanceImage(next_state).ToImageSkia();
    SetImage(views::CustomButton::STATE_HOVERED, affordance_image);
    SetImage(views::CustomButton::STATE_PRESSED, affordance_image);
  }

  if ((media_state_ == TAB_MEDIA_STATE_AUDIO_PLAYING &&
       next_state == TAB_MEDIA_STATE_AUDIO_MUTING) ||
      (media_state_ == TAB_MEDIA_STATE_AUDIO_MUTING &&
       next_state == TAB_MEDIA_STATE_AUDIO_PLAYING) ||
      (media_state_ == TAB_MEDIA_STATE_AUDIO_MUTING &&
       next_state == TAB_MEDIA_STATE_NONE)) {
    // Instant user feedback: No fade animation.
    showing_media_state_ = next_state;
    fade_animation_.reset();
  } else {
    if (next_state == TAB_MEDIA_STATE_NONE)
      showing_media_state_ = media_state_;  // Fading-out indicator.
    else
      showing_media_state_ = next_state;  // Fading-in to next indicator.
    fade_animation_ = chrome::CreateTabMediaIndicatorFadeAnimation(next_state);
    if (!fade_animation_delegate_)
      fade_animation_delegate_.reset(new FadeAnimationDelegate(this));
    fade_animation_->set_delegate(fade_animation_delegate_.get());
    fade_animation_->Start();
  }

  SetEnabled(chrome::IsTabAudioMutingFeatureEnabled() &&
             (next_state == TAB_MEDIA_STATE_AUDIO_PLAYING ||
              next_state == TAB_MEDIA_STATE_AUDIO_MUTING));

  // An indicator state change should be made visible immediately, instead of
  // the user being surprised when their mouse leaves the button.
  if (state() == views::CustomButton::STATE_HOVERED) {
    SetState(enabled() ? views::CustomButton::STATE_NORMAL :
             views::CustomButton::STATE_DISABLED);
  }

  media_state_ = next_state;

  // Note: The calls to SetImage(), SetEnabled(), and SetState() above will call
  // SchedulePaint() if necessary.
}

const char* MediaIndicatorButton::GetClassName() const {
  return kViewClassName;
}

views::View* MediaIndicatorButton::GetTooltipHandlerForPoint(
    const gfx::Point& point) {
  return NULL;  // Tab (the parent View) provides the tooltip.
}

bool MediaIndicatorButton::OnMousePressed(const ui::MouseEvent& event) {
  const bool handled = ImageButton::OnMousePressed(event);
  // Explicitly mark midle-mouse clicks as non-handled to ensure the tab sees
  // them.
  return !event.IsMiddleMouseButton() && handled;
}

bool MediaIndicatorButton::OnMouseDragged(const ui::MouseEvent& event) {
  const ButtonState previous_state = state();
  const bool ret = ImageButton::OnMouseDragged(event);
  if (previous_state != views::CustomButton::STATE_NORMAL &&
      state() == views::CustomButton::STATE_NORMAL)
    content::RecordAction(UserMetricsAction("MediaIndicatorButton_Dragged"));
  return ret;
}

void MediaIndicatorButton::OnPaint(gfx::Canvas* canvas) {
  double opaqueness =
      fade_animation_ ? fade_animation_->GetCurrentValue() : 1.0;
  if (media_state_ == TAB_MEDIA_STATE_NONE)
    opaqueness = 1.0 - opaqueness;  // Fading out, not in.
  if (opaqueness < 1.0)
    canvas->SaveLayerAlpha(opaqueness * SK_AlphaOPAQUE);
  ImageButton::OnPaint(canvas);
  if (opaqueness < 1.0)
    canvas->Restore();
}

bool MediaIndicatorButton::DoesIntersectRect(const views::View* target,
                                             const gfx::Rect& rect) const {
  // If this button is not enabled, Tab (the parent View) handles all mouse
  // events.
  return enabled() &&
      views::ViewTargeterDelegate::DoesIntersectRect(target, rect);
}

void MediaIndicatorButton::NotifyClick(const ui::Event& event) {
  if (media_state_ == TAB_MEDIA_STATE_AUDIO_PLAYING)
    content::RecordAction(UserMetricsAction("MediaIndicatorButton_Mute"));
  else if (media_state_ == TAB_MEDIA_STATE_AUDIO_MUTING)
    content::RecordAction(UserMetricsAction("MediaIndicatorButton_Unmute"));
  else
    NOTREACHED();

  DCHECK(parent() && !strcmp(parent()->GetClassName(), Tab::kViewClassName));
  Tab* const tab = static_cast<Tab*>(parent());
  tab->controller()->ToggleTabAudioMute(tab);
}