Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(448)

Side by Side Diff: chrome/browser/ui/tabs/tab_audio_indicator.cc

Issue 12744003: Audio indicator: cross platform drawing and animation (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 #include "chrome/browser/ui/tabs/tab_audio_indicator.h"
6
7 #include "grit/theme_resources.h"
8 #include "ui/base/animation/animation_container.h"
9 #include "ui/base/animation/linear_animation.h"
10 #include "ui/base/resource/resource_bundle.h"
11 #include "ui/gfx/canvas.h"
12 #include "ui/gfx/rect.h"
13
14 namespace {
15
16 // The number of columns to draw for the equalizer graphic.
17 const size_t kEqualizerColumnCount = 3;
18
19 // The maximum level for the equalizer.
20 const size_t kEqualizerMaxLevel = 8;
21
22 // The equalizer cycles between these frames. An equalizer frame is 3 columns
23 // where each column ranges from 0 to |kEqualizerMaxLevel|. TODO(sail): Replace
24 // this with levels from the actual audio source.
25 const size_t kEqualizerFrames[][kEqualizerColumnCount] = {
26 { 7, 5, 3 },
27 { 4, 5, 7 },
28 { 4, 2, 5 },
29 { 3, 7, 4 },
30 { 2, 3, 2 },
31 { 3, 4, 3 },
32 };
33
34 // The duration of each equalizer frame.
35 const size_t kAnimationCycleDurationMs = 300;
36
37 // The duration of the "ending" animation once audio stops playing.
38 const size_t kAnimationEndingDurationMs = 1000;
39
40 // Target frames per second. In reality fewer frames are drawn because the
41 // equalizer levels change slowly.
42 const int kFPS = 15;
43
44 } // namespace
45
46 TabAudioIndicator::TabAudioIndicator(Delegate* delegate)
47 : delegate_(delegate),
48 frame_index_(0),
49 state_(STATE_NOT_ANIMATING) {
50 }
51
52 TabAudioIndicator::~TabAudioIndicator() {
53 }
54
55 void TabAudioIndicator::SetAnimationContainer(
56 ui::AnimationContainer* animation_container) {
57 animation_container_ = animation_container;
58 }
59
60 void TabAudioIndicator::SetIsPlayingAudio(bool is_playing_audio) {
61 if (is_playing_audio && state_ != STATE_ANIMATING) {
62 state_ = STATE_ANIMATING;
63 animation_.reset(
64 new ui::LinearAnimation(kAnimationCycleDurationMs, kFPS, this));
65 animation_->SetContainer(animation_container_);
66 animation_->Start();
67 } else if (!is_playing_audio && state_ == STATE_ANIMATING) {
68 state_ = STATE_ANIMATION_ENDING;
69 animation_.reset(
70 new ui::LinearAnimation(kAnimationEndingDurationMs, kFPS, this));
71 animation_->SetContainer(animation_container_);
72 animation_->Start();
73 }
74 }
75
76 bool TabAudioIndicator::IsAnimating() {
77 return state_ != STATE_NOT_ANIMATING;
78 }
79
80 void TabAudioIndicator::Paint(gfx::Canvas* canvas, const gfx::Rect& rect) {
81 if (state_ == STATE_NOT_ANIMATING)
82 return;
83
84 canvas->Save();
85 canvas->ClipRect(rect);
86
87 // Draw 3 equalizer columns. |IDR_AUDIO_EQUALIZER_COLUMN| is a column of the
88 // equalizer with 8 levels. The current level is between 0 and 8 so the
89 // image is shifted down and then drawn.
90 ui::ResourceBundle& rb = ResourceBundle::GetSharedInstance();
91 gfx::ImageSkia* image(rb.GetImageSkiaNamed(IDR_AUDIO_EQUALIZER_COLUMN));
92 int x = rect.x();
93 std::vector<int> levels = GetCurrentEqualizerLevels();
94 for (size_t i = 0; i < levels.size(); ++i) {
95 // Shift the image down by the level. For example, for level 8 draw the
96 // image at rect.y(), For level 7, draw the image at rect.y() - 2, etc...
97 int y = rect.y() + (kEqualizerMaxLevel - levels[i]) * 2;
98 canvas->DrawImageInt(*image, x, y);
99 x += image->width() - 1;
100 }
101
102 canvas->Restore();
103
104 // Cache the levels that were just drawn. This is used to prevent unnecessary
105 // drawing when animation progress doesn't result in equalizer levels
106 // changing.
107 last_displayed_equalizer_levels_ = levels;
108 }
109
110 void TabAudioIndicator::AnimationProgressed(const ui::Animation* animation) {
111 std::vector<int> levels = GetCurrentEqualizerLevels();
112 if (last_displayed_equalizer_levels_ != levels)
113 delegate_->ScheduleAudioIndicatorPaint();
114 }
115
116 void TabAudioIndicator::AnimationEnded(const ui::Animation* animation) {
117 if (state_ == STATE_ANIMATING) {
118 // The current equalizer frame animation has finished. Start animating the
119 // next frame.
120 frame_index_ = (frame_index_ + 1) % arraysize(kEqualizerFrames);
121 animation_->Start();
122 } else if (state_ == STATE_ANIMATION_ENDING) {
123 // The "ending" animation has stopped. Update the tab state so that the UI
124 // can update the tab icon.
125 state_ = STATE_NOT_ANIMATING;
126 delegate_->ScheduleAudioIndicatorPaint();
127 }
128 }
129
130 std::vector<int> TabAudioIndicator::GetCurrentEqualizerLevels() const {
131 int next_frame_index = (frame_index_ + 1) % arraysize(kEqualizerFrames);
132 std::vector<int> levels;
133 // For all 3 columsn of the equalizer, tween between the current equalizer
134 // level and the target equalizer level.
135 for (size_t i = 0; i < kEqualizerColumnCount; ++i) {
136 int start = kEqualizerFrames[frame_index_][i];
137 int end = state_ == STATE_ANIMATION_ENDING
138 ? 0
139 : kEqualizerFrames[next_frame_index][i];
140 levels.push_back(animation_->CurrentValueBetween(start, end));
141 }
142 return levels;
143 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/tabs/tab_audio_indicator.h ('k') | chrome/browser/ui/tabs/tab_audio_indicator_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698