| 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 #include "ui/gfx/skia_util.h" |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // The number of columns to draw for the equalizer graphic. | 17 // The number of columns to draw for the equalizer graphic. |
| 18 const size_t kEqualizerColumnCount = 2; | 18 const size_t kEqualizerColumnCount = 3; |
| 19 | |
| 20 // The maximum level for the equalizer. | |
| 21 const size_t kEqualizerMaxLevel = 4; | |
| 22 | 19 |
| 23 // The equalizer cycles between these frames. An equalizer frame is 2 columns | 20 // The equalizer cycles between these frames. An equalizer frame is 2 columns |
| 24 // where each column ranges from 0 to |kEqualizerMaxLevel|. TODO(sail): Replace | 21 // where each column ranges from 0 to 4. |
| 25 // this with levels from the actual audio source. | |
| 26 const size_t kEqualizerFrames[][kEqualizerColumnCount] = { | 22 const size_t kEqualizerFrames[][kEqualizerColumnCount] = { |
| 27 { 2, 1 }, | 23 { 1, 2, 3 }, |
| 28 { 3, 2 }, | 24 { 2, 3, 4 }, |
| 29 { 2, 3 }, | 25 { 3, 4, 3 }, |
| 30 { 2, 2 }, | 26 { 4, 3, 2 }, |
| 31 { 2, 3 }, | 27 { 3, 2, 1 }, |
| 32 { 3, 4 }, | 28 { 2, 1, 2 }, |
| 33 { 2, 3 }, | 29 { 1, 2, 3 }, |
| 34 { 3, 2 }, | 30 { 0, 1, 2 }, |
| 35 { 2, 3 }, | 31 { 1, 0, 1 }, |
| 36 { 3, 2 }, | 32 { 2, 1, 0 }, |
| 33 { 3, 2, 1 }, |
| 34 { 2, 1, 2 }, |
| 37 }; | 35 }; |
| 38 | 36 |
| 39 // The space between equalizer levels. | 37 // The space between equalizer levels. |
| 40 const int kEqualizerColumnPadding = 1; | 38 const int kEqualizerColumnPadding = 1; |
| 41 | 39 |
| 42 // The duration of each equalizer frame. | 40 // The duration of each equalizer frame. |
| 43 const size_t kAnimationCycleDurationMs = 300; | 41 const size_t kAnimationCycleDurationMs = 300; |
| 44 | 42 |
| 45 // The duration of the "ending" animation once audio stops playing. | 43 // The duration of the "ending" animation once audio stops playing. |
| 46 const size_t kAnimationEndingDurationMs = 1000; | 44 const size_t kAnimationEndingDurationMs = 1000; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 } | 80 } |
| 83 | 81 |
| 84 bool TabAudioIndicator::IsAnimating() { | 82 bool TabAudioIndicator::IsAnimating() { |
| 85 return state_ != STATE_NOT_ANIMATING; | 83 return state_ != STATE_NOT_ANIMATING; |
| 86 } | 84 } |
| 87 | 85 |
| 88 void TabAudioIndicator::Paint(gfx::Canvas* canvas, const gfx::Rect& rect) { | 86 void TabAudioIndicator::Paint(gfx::Canvas* canvas, const gfx::Rect& rect) { |
| 89 canvas->Save(); | 87 canvas->Save(); |
| 90 canvas->ClipRect(rect); | 88 canvas->ClipRect(rect); |
| 91 | 89 |
| 92 // Draw 2 equalizer columns. |IDR_AUDIO_EQUALIZER_COLUMN| is a column of the | 90 // Draw 3 equalizer columns. |IDR_AUDIO_EQUALIZER_COLUMN| is a column of the |
| 93 // equalizer with 4 levels. The current level is between 0 and 4 so the | 91 // equalizer with 4 levels. The current level is between 0 and 4 so the |
| 94 // image is shifted down and then drawn. | 92 // image is shifted down and then drawn. |
| 95 if (state_ != STATE_NOT_ANIMATING) { | 93 if (state_ != STATE_NOT_ANIMATING) { |
| 96 ui::ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 94 ui::ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 97 gfx::ImageSkia* image(rb.GetImageSkiaNamed(IDR_AUDIO_EQUALIZER_COLUMN)); | 95 gfx::ImageSkia* image(rb.GetImageSkiaNamed(IDR_AUDIO_EQUALIZER_COLUMN)); |
| 98 int x = rect.right(); | 96 int x = rect.right(); |
| 99 std::vector<int> levels = GetCurrentEqualizerLevels(); | 97 std::vector<int> levels = GetCurrentEqualizerLevels(); |
| 100 for (int i = levels.size() - 1; i >= 0; --i) { | 98 for (int i = levels.size() - 1; i >= 0; --i) { |
| 99 if (levels[i] == 0) |
| 100 continue; |
| 101 |
| 101 // Shift the image down by the level. | 102 // Shift the image down by the level. |
| 102 int y = rect.bottom() - levels[i] * 2; | 103 int y = rect.bottom() - levels[i] * 2; |
| 103 x -= image->width(); | 104 x -= image->width(); |
| 104 canvas->DrawImageInt(*image, x, y); | 105 canvas->DrawImageInt(*image, x, y); |
| 105 | 106 |
| 106 // Clip the equalizer column so the favicon doesn't obscure it. | 107 // Clip the equalizer column so the favicon doesn't obscure it. |
| 107 gfx::Rect equalizer_rect(x, y, image->width(), image->height()); | 108 gfx::Rect equalizer_rect(x, y, image->width(), image->height()); |
| 108 equalizer_rect.Inset(-kEqualizerColumnPadding, -kEqualizerColumnPadding); | |
| 109 canvas->sk_canvas()->clipRect( | 109 canvas->sk_canvas()->clipRect( |
| 110 gfx::RectToSkRect(equalizer_rect), SkRegion::kDifference_Op); | 110 gfx::RectToSkRect(equalizer_rect), SkRegion::kDifference_Op); |
| 111 | 111 |
| 112 x -= kEqualizerColumnPadding; | 112 // Padding is baked into both sides of the icons so overlap the images. |
| 113 x += kEqualizerColumnPadding; |
| 113 } | 114 } |
| 114 | 115 |
| 115 // Cache the levels that were just drawn. This is used to prevent | 116 // Cache the levels that were just drawn. This is used to prevent |
| 116 // unnecessary drawing when animation progress doesn't result in equalizer | 117 // unnecessary drawing when animation progress doesn't result in equalizer |
| 117 // levels changing. | 118 // levels changing. |
| 118 last_displayed_equalizer_levels_ = levels; | 119 last_displayed_equalizer_levels_ = levels; |
| 119 } | 120 } |
| 120 | 121 |
| 121 if (!favicon_.isNull()) { | 122 if (!favicon_.isNull()) { |
| 122 int dst_x = rect.x() - (favicon_.width() - rect.width()) / 2; | 123 int dst_x = rect.x() - (favicon_.width() - rect.width()) / 2; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 // level and the target equalizer level. | 155 // level and the target equalizer level. |
| 155 for (size_t i = 0; i < kEqualizerColumnCount; ++i) { | 156 for (size_t i = 0; i < kEqualizerColumnCount; ++i) { |
| 156 int start = kEqualizerFrames[frame_index_][i]; | 157 int start = kEqualizerFrames[frame_index_][i]; |
| 157 int end = state_ == STATE_ANIMATION_ENDING | 158 int end = state_ == STATE_ANIMATION_ENDING |
| 158 ? 0 | 159 ? 0 |
| 159 : kEqualizerFrames[next_frame_index][i]; | 160 : kEqualizerFrames[next_frame_index][i]; |
| 160 levels.push_back(animation_->CurrentValueBetween(start, end)); | 161 levels.push_back(animation_->CurrentValueBetween(start, end)); |
| 161 } | 162 } |
| 162 return levels; | 163 return levels; |
| 163 } | 164 } |
| OLD | NEW |