OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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/tabs/tab_audio_indicator.h" | 5 #include "chrome/browser/ui/tabs/tab_audio_indicator.h" |
6 | 6 |
7 #include "grit/theme_resources.h" | 7 #include "grit/theme_resources.h" |
8 #include "ui/base/animation/animation_container.h" | 8 #include "ui/base/animation/animation_container.h" |
9 #include "ui/base/animation/linear_animation.h" | 9 #include "ui/base/animation/linear_animation.h" |
10 #include "ui/base/resource/resource_bundle.h" | 10 #include "ui/base/resource/resource_bundle.h" |
11 #include "ui/gfx/canvas.h" | 11 #include "ui/gfx/canvas.h" |
12 #include "ui/gfx/rect.h" | 12 #include "ui/gfx/rect.h" |
| 13 #include "ui/gfx/skia_util.h" |
13 | 14 |
14 namespace { | 15 namespace { |
15 | 16 |
16 // The number of columns to draw for the equalizer graphic. | 17 // The number of columns to draw for the equalizer graphic. |
17 const size_t kEqualizerColumnCount = 3; | 18 const size_t kEqualizerColumnCount = 2; |
18 | 19 |
19 // The maximum level for the equalizer. | 20 // The maximum level for the equalizer. |
20 const size_t kEqualizerMaxLevel = 8; | 21 const size_t kEqualizerMaxLevel = 4; |
21 | 22 |
22 // The equalizer cycles between these frames. An equalizer frame is 3 columns | 23 // The equalizer cycles between these frames. An equalizer frame is 2 columns |
23 // where each column ranges from 0 to |kEqualizerMaxLevel|. TODO(sail): Replace | 24 // where each column ranges from 0 to |kEqualizerMaxLevel|. TODO(sail): Replace |
24 // this with levels from the actual audio source. | 25 // this with levels from the actual audio source. |
25 const size_t kEqualizerFrames[][kEqualizerColumnCount] = { | 26 const size_t kEqualizerFrames[][kEqualizerColumnCount] = { |
26 { 7, 5, 3 }, | 27 { 2, 1 }, |
27 { 4, 5, 7 }, | 28 { 3, 2 }, |
28 { 4, 2, 5 }, | 29 { 2, 3 }, |
29 { 3, 7, 4 }, | 30 { 2, 2 }, |
30 { 2, 3, 2 }, | 31 { 2, 3 }, |
31 { 3, 4, 3 }, | 32 { 3, 4 }, |
| 33 { 2, 3 }, |
| 34 { 3, 2 }, |
| 35 { 2, 3 }, |
| 36 { 3, 2 }, |
32 }; | 37 }; |
33 | 38 |
| 39 // The space between equalizer levels. |
| 40 const int kEqualizerColumnPadding = 1; |
| 41 |
34 // The duration of each equalizer frame. | 42 // The duration of each equalizer frame. |
35 const size_t kAnimationCycleDurationMs = 300; | 43 const size_t kAnimationCycleDurationMs = 300; |
36 | 44 |
37 // The duration of the "ending" animation once audio stops playing. | 45 // The duration of the "ending" animation once audio stops playing. |
38 const size_t kAnimationEndingDurationMs = 1000; | 46 const size_t kAnimationEndingDurationMs = 1000; |
39 | 47 |
40 // Target frames per second. In reality fewer frames are drawn because the | 48 // Target frames per second. In reality fewer frames are drawn because the |
41 // equalizer levels change slowly. | 49 // equalizer levels change slowly. |
42 const int kFPS = 15; | 50 const int kFPS = 15; |
43 | 51 |
(...skipping 27 matching lines...) Expand all Loading... |
71 animation_->SetContainer(animation_container_); | 79 animation_->SetContainer(animation_container_); |
72 animation_->Start(); | 80 animation_->Start(); |
73 } | 81 } |
74 } | 82 } |
75 | 83 |
76 bool TabAudioIndicator::IsAnimating() { | 84 bool TabAudioIndicator::IsAnimating() { |
77 return state_ != STATE_NOT_ANIMATING; | 85 return state_ != STATE_NOT_ANIMATING; |
78 } | 86 } |
79 | 87 |
80 void TabAudioIndicator::Paint(gfx::Canvas* canvas, const gfx::Rect& rect) { | 88 void TabAudioIndicator::Paint(gfx::Canvas* canvas, const gfx::Rect& rect) { |
81 if (state_ == STATE_NOT_ANIMATING) | |
82 return; | |
83 | |
84 canvas->Save(); | 89 canvas->Save(); |
85 canvas->ClipRect(rect); | 90 canvas->ClipRect(rect); |
86 | 91 |
87 // Draw 3 equalizer columns. |IDR_AUDIO_EQUALIZER_COLUMN| is a column of the | 92 // Draw 2 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 | 93 // equalizer with 4 levels. The current level is between 0 and 4 so the |
89 // image is shifted down and then drawn. | 94 // image is shifted down and then drawn. |
90 ui::ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 95 if (state_ != STATE_NOT_ANIMATING) { |
91 gfx::ImageSkia* image(rb.GetImageSkiaNamed(IDR_AUDIO_EQUALIZER_COLUMN)); | 96 ui::ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
92 int x = rect.x(); | 97 gfx::ImageSkia* image(rb.GetImageSkiaNamed(IDR_AUDIO_EQUALIZER_COLUMN)); |
93 std::vector<int> levels = GetCurrentEqualizerLevels(); | 98 int x = rect.right(); |
94 for (size_t i = 0; i < levels.size(); ++i) { | 99 std::vector<int> levels = GetCurrentEqualizerLevels(); |
95 // Shift the image down by the level. For example, for level 8 draw the | 100 for (int i = levels.size() - 1; i >= 0; --i) { |
96 // image at rect.y(), For level 7, draw the image at rect.y() - 2, etc... | 101 // Shift the image down by the level. |
97 int y = rect.y() + (kEqualizerMaxLevel - levels[i]) * 2; | 102 int y = rect.bottom() - levels[i] * 2; |
98 canvas->DrawImageInt(*image, x, y); | 103 x -= image->width(); |
99 x += image->width() - 1; | 104 canvas->DrawImageInt(*image, x, y); |
| 105 |
| 106 // Clip the equalizer column so the favicon doesn't obscure it. |
| 107 gfx::Rect equalizer_rect(x, y, image->width(), image->height()); |
| 108 equalizer_rect.Inset(-kEqualizerColumnPadding, -kEqualizerColumnPadding); |
| 109 canvas->sk_canvas()->clipRect( |
| 110 gfx::RectToSkRect(equalizer_rect), SkRegion::kDifference_Op); |
| 111 |
| 112 x -= kEqualizerColumnPadding; |
| 113 } |
| 114 |
| 115 // Cache the levels that were just drawn. This is used to prevent |
| 116 // unnecessary drawing when animation progress doesn't result in equalizer |
| 117 // levels changing. |
| 118 last_displayed_equalizer_levels_ = levels; |
| 119 } |
| 120 |
| 121 if (!favicon_.isNull()) { |
| 122 int dst_x = rect.x() - (favicon_.width() - rect.width()) / 2; |
| 123 int dst_y = rect.y() - (favicon_.height()- rect.height()) / 2; |
| 124 canvas->DrawImageInt(favicon_, dst_x, dst_y); |
100 } | 125 } |
101 | 126 |
102 canvas->Restore(); | 127 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 } | 128 } |
109 | 129 |
110 void TabAudioIndicator::AnimationProgressed(const ui::Animation* animation) { | 130 void TabAudioIndicator::AnimationProgressed(const ui::Animation* animation) { |
111 std::vector<int> levels = GetCurrentEqualizerLevels(); | 131 std::vector<int> levels = GetCurrentEqualizerLevels(); |
112 if (last_displayed_equalizer_levels_ != levels) | 132 if (last_displayed_equalizer_levels_ != levels) |
113 delegate_->ScheduleAudioIndicatorPaint(); | 133 delegate_->ScheduleAudioIndicatorPaint(); |
114 } | 134 } |
115 | 135 |
116 void TabAudioIndicator::AnimationEnded(const ui::Animation* animation) { | 136 void TabAudioIndicator::AnimationEnded(const ui::Animation* animation) { |
117 if (state_ == STATE_ANIMATING) { | 137 if (state_ == STATE_ANIMATING) { |
118 // The current equalizer frame animation has finished. Start animating the | 138 // The current equalizer frame animation has finished. Start animating the |
119 // next frame. | 139 // next frame. |
120 frame_index_ = (frame_index_ + 1) % arraysize(kEqualizerFrames); | 140 frame_index_ = (frame_index_ + 1) % arraysize(kEqualizerFrames); |
121 animation_->Start(); | 141 animation_->Start(); |
122 } else if (state_ == STATE_ANIMATION_ENDING) { | 142 } else if (state_ == STATE_ANIMATION_ENDING) { |
123 // The "ending" animation has stopped. Update the tab state so that the UI | 143 // The "ending" animation has stopped. Update the tab state so that the UI |
124 // can update the tab icon. | 144 // can update the tab icon. |
125 state_ = STATE_NOT_ANIMATING; | 145 state_ = STATE_NOT_ANIMATING; |
126 delegate_->ScheduleAudioIndicatorPaint(); | 146 delegate_->ScheduleAudioIndicatorPaint(); |
127 } | 147 } |
128 } | 148 } |
129 | 149 |
130 std::vector<int> TabAudioIndicator::GetCurrentEqualizerLevels() const { | 150 std::vector<int> TabAudioIndicator::GetCurrentEqualizerLevels() const { |
131 int next_frame_index = (frame_index_ + 1) % arraysize(kEqualizerFrames); | 151 int next_frame_index = (frame_index_ + 1) % arraysize(kEqualizerFrames); |
132 std::vector<int> levels; | 152 std::vector<int> levels; |
133 // For all 3 columsn of the equalizer, tween between the current equalizer | 153 // For all 2 columsn of the equalizer, tween between the current equalizer |
134 // level and the target equalizer level. | 154 // level and the target equalizer level. |
135 for (size_t i = 0; i < kEqualizerColumnCount; ++i) { | 155 for (size_t i = 0; i < kEqualizerColumnCount; ++i) { |
136 int start = kEqualizerFrames[frame_index_][i]; | 156 int start = kEqualizerFrames[frame_index_][i]; |
137 int end = state_ == STATE_ANIMATION_ENDING | 157 int end = state_ == STATE_ANIMATION_ENDING |
138 ? 0 | 158 ? 0 |
139 : kEqualizerFrames[next_frame_index][i]; | 159 : kEqualizerFrames[next_frame_index][i]; |
140 levels.push_back(animation_->CurrentValueBetween(start, end)); | 160 levels.push_back(animation_->CurrentValueBetween(start, end)); |
141 } | 161 } |
142 return levels; | 162 return levels; |
143 } | 163 } |
OLD | NEW |