Chromium Code Reviews| OLD | NEW |
|---|---|
| (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/linear_animation.h" | |
| 9 #include "ui/base/resource/resource_bundle.h" | |
| 10 #include "ui/gfx/canvas.h" | |
| 11 #include "ui/gfx/rect.h" | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 // The number of columns to draw for the equalizer graphic. | |
| 16 const size_t kEqualizerColumnCount = 3; | |
| 17 | |
| 18 // The maximum level for the equalizer. | |
| 19 const size_t kEqualizerMaxLevel = 8; | |
| 20 | |
| 21 // The equalizer cycles between these frames. An equalizer frame is 3 columns | |
| 22 // where each column ranges from 0 to |kEqualizerMaxLevel|. TODO(sail): Replace | |
| 23 // this with levels from the actual audio source. | |
| 24 const int kEqualizerFrames[][kEqualizerColumnCount] = { | |
|
cpu_(ooo_6.6-7.5)
2013/03/11 02:40:27
size_t
sail
2013/03/11 23:16:13
Done.
| |
| 25 { 7, 5, 3 }, | |
| 26 { 4, 5, 7 }, | |
| 27 { 4, 2, 5 }, | |
| 28 { 3, 7, 4 }, | |
| 29 { 2, 3, 2 }, | |
| 30 { 3, 4, 3 }, | |
| 31 }; | |
| 32 const int kFrameCount = arraysize(kEqualizerFrames); | |
|
cpu_(ooo_6.6-7.5)
2013/03/11 02:40:27
nuke this constant and use arraysize in the two ca
sail
2013/03/11 23:16:13
Done.
| |
| 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 = 25; | |
|
cpu_(ooo_6.6-7.5)
2013/03/11 02:40:27
So I understand, the duration implies 3fps, but we
sail
2013/03/11 23:16:13
Done.
Good point. Changed to 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::SetIsPlayingAudio(bool is_playing_audio) { | |
| 56 if (is_playing_audio && state_ != STATE_ANIMATING) { | |
| 57 state_ = STATE_ANIMATING; | |
| 58 animation_.reset( | |
|
cpu_(ooo_6.6-7.5)
2013/03/11 02:40:27
do you get AnimationEnded? don't you have to call
sail
2013/03/11 23:16:13
To get AnimationEnded we have to call End(). In th
| |
| 59 new ui::LinearAnimation(kAnimationCycleDurationMs, kFPS, this)); | |
| 60 animation_->Start(); | |
| 61 } else if (!is_playing_audio && state_ == STATE_ANIMATING) { | |
| 62 state_ = STATE_ANIMATION_ENDING; | |
| 63 animation_.reset( | |
| 64 new ui::LinearAnimation(kAnimationEndingDurationMs, kFPS, this)); | |
| 65 animation_->Start(); | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 bool TabAudioIndicator::IsAnimating() { | |
| 70 return state_ != STATE_NOT_ANIMATING; | |
| 71 } | |
| 72 | |
| 73 void TabAudioIndicator::Paint(gfx::Canvas* canvas, const gfx::Rect& rect) { | |
| 74 if (state_ == STATE_NOT_ANIMATING) | |
| 75 return; | |
| 76 | |
| 77 canvas->Save(); | |
| 78 canvas->ClipRect(rect); | |
| 79 | |
| 80 // Draw 3 equalizer columns. |IDR_AUDIO_EQUALIZER_COLUMN| is a column of the | |
| 81 // equalizer with 8 levels. The current level is between 0 and 8 so the | |
| 82 // image is shifted down and then drawn. | |
| 83 ui::ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 84 gfx::ImageSkia* image(rb.GetImageSkiaNamed(IDR_AUDIO_EQUALIZER_COLUMN)); | |
| 85 int x = rect.x(); | |
| 86 std::vector<int> levels = GetCurrentEqualizerLevels(); | |
| 87 for (size_t i = 0; i < levels.size(); ++i) { | |
| 88 // Shift the image down by the level. For example, for level 8 draw the | |
| 89 // image at rect.y(), For level 7, draw the image at rect.y() - 2, etc... | |
| 90 int y = rect.y() + (kEqualizerMaxLevel - levels[i]) * 2; | |
| 91 canvas->DrawImageInt(*image, x, y); | |
|
cpu_(ooo_6.6-7.5)
2013/03/11 02:40:27
I thought we had 8 images, one for each level...
sail
2013/03/11 23:16:13
Using just one image was a lot simpler. Because of
| |
| 92 x += image->width() - 1; | |
| 93 } | |
| 94 | |
| 95 canvas->Restore(); | |
| 96 | |
| 97 // Cache the levels that were just drawn. This is used to prevent unnecessary | |
| 98 // drawing when animation progress doesn't result in equalizer levels | |
| 99 // changing. | |
| 100 last_displayed_equalizer_levels_ = levels; | |
| 101 } | |
| 102 | |
| 103 void TabAudioIndicator::AnimationProgressed(const ui::Animation* animation) { | |
| 104 std::vector<int> levels = GetCurrentEqualizerLevels(); | |
| 105 if (last_displayed_equalizer_levels_ != levels) | |
| 106 delegate_->ScheduleAudioIndicatorPaint(); | |
| 107 } | |
| 108 | |
| 109 void TabAudioIndicator::AnimationEnded(const ui::Animation* animation) { | |
| 110 if (state_ == STATE_ANIMATING) { | |
| 111 // The current equalizer frame animation has finished. Start animating the | |
| 112 // next frame. | |
| 113 frame_index_ = (frame_index_ + 1) % kFrameCount; | |
| 114 animation_->Start(); | |
| 115 } else if (state_ == STATE_ANIMATION_ENDING) { | |
| 116 // The "ending" animation has stopped. Update the tab state so that the UI | |
| 117 // can update the tab icon. | |
| 118 state_ = STATE_NOT_ANIMATING; | |
| 119 delegate_->ScheduleAudioIndicatorPaint(); | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 std::vector<int> TabAudioIndicator::GetCurrentEqualizerLevels() const { | |
| 124 int next_frame_index = (frame_index_ + 1) % kFrameCount; | |
| 125 std::vector<int> levels; | |
| 126 // For all 3 columsn of the equalizer, tween between the current equalizer | |
| 127 // level and the target equalizer level. | |
| 128 for (size_t i = 0; i < kEqualizerColumnCount; ++i) { | |
| 129 int start = kEqualizerFrames[frame_index_][i]; | |
| 130 int end = state_ == STATE_ANIMATION_ENDING | |
| 131 ? 0 | |
| 132 : kEqualizerFrames[next_frame_index][i]; | |
| 133 levels.push_back(animation_->CurrentValueBetween(start, end)); | |
| 134 } | |
| 135 return levels; | |
| 136 } | |
| OLD | NEW |