1// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_UI_TABS_TAB_AUDIO_INDICATOR_H_
6#define CHROME_BROWSER_UI_TABS_TAB_AUDIO_INDICATOR_H_
7
8#include <vector>
9
10#include "base/compiler_specific.h"
11#include "base/gtest_prod_util.h"
12#include "base/memory/ref_counted.h"
13#include "base/memory/scoped_ptr.h"
14#include "ui/base/animation/animation_delegate.h"
15#include "ui/gfx/image/image_skia.h"
16
17namespace gfx {
18class Canvas;
19class Rect;
20}
21namespace ui {
22class Animation;
23class AnimationContainer;
24class LinearAnimation;
25}
26
27// This class is used to draw an animating tab audio indicator.
28class TabAudioIndicator : public ui::AnimationDelegate {
29 public:
30  class Delegate {
31   public:
32    virtual void ScheduleAudioIndicatorPaint() = 0;
33
34   protected:
35    virtual ~Delegate() {}
36  };
37
38  explicit TabAudioIndicator(Delegate* delegate);
39  virtual ~TabAudioIndicator();
40
41  void set_favicon(const gfx::ImageSkia& favicon) { favicon_ = favicon; }
42
43  void SetAnimationContainer(ui::AnimationContainer* animation_container);
44  void SetIsPlayingAudio(bool is_playing_audio);
45  bool IsAnimating();
46
47  void Paint(gfx::Canvas* canvas, const gfx::Rect& rect);
48
49 private:
50  enum State {
51    STATE_NOT_ANIMATING,
52    STATE_ANIMATING,
53    STATE_ANIMATION_ENDING,
54  };
55
56  FRIEND_TEST_ALL_PREFIXES(TabAudioIndicatorTest, AnimationState);
57  FRIEND_TEST_ALL_PREFIXES(TabAudioIndicatorTest, SchedulePaint);
58
59  // AnimationDelegate:
60  virtual void AnimationProgressed(const ui::Animation* animation) OVERRIDE;
61  virtual void AnimationEnded(const ui::Animation* animation) OVERRIDE;
62
63  // Gets the equalizer levels for all 3 columns. The values are tweened between
64  // the current and target frame.
65  std::vector<int> GetCurrentEqualizerLevels() const;
66
67  Delegate* delegate_;
68  scoped_ptr<ui::LinearAnimation> animation_;
69  scoped_refptr<ui::AnimationContainer> animation_container_;
70  gfx::ImageSkia favicon_;
71
72  // The equalizer frame that's currently being displayed.
73  size_t frame_index_;
74
75  // The equalizer levels that were last displayed. This is used to prevent
76  // unnecessary drawing when animation progress doesn't result in equalizer
77  // levels changing.
78  std::vector<int> last_displayed_equalizer_levels_;
79
80  State state_;
81
82  DISALLOW_COPY_AND_ASSIGN(TabAudioIndicator);
83};
84
85#endif  // CHROME_BROWSER_UI_TABS_TAB_AUDIO_INDICATOR_H_
86