OLD | NEW |
(Empty) | |
| 1 // Copyright 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 "ui/app_list/views/speech_view.h" |
| 6 |
| 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "grit/ui_resources.h" |
| 9 #include "grit/ui_strings.h" |
| 10 #include "ui/app_list/app_list_model.h" |
| 11 #include "ui/app_list/app_list_view_delegate.h" |
| 12 #include "ui/base/l10n/l10n_util.h" |
| 13 #include "ui/base/resource/resource_bundle.h" |
| 14 #include "ui/gfx/canvas.h" |
| 15 #include "ui/views/animation/bounds_animator.h" |
| 16 #include "ui/views/background.h" |
| 17 #include "ui/views/controls/button/image_button.h" |
| 18 #include "ui/views/controls/label.h" |
| 19 #include "ui/views/layout/fill_layout.h" |
| 20 #include "ui/views/shadow_border.h" |
| 21 |
| 22 namespace app_list { |
| 23 |
| 24 namespace { |
| 25 |
| 26 const int kShadowOffset = 1; |
| 27 const int kShadowBlur = 4; |
| 28 const int kTextSize = 20; |
| 29 const int kMicButtonMargin = 12; |
| 30 const int kTextMargin = 32; |
| 31 const int kIndicatorRadiusMax = 100; |
| 32 const int kIndicatorAnimationDuration = 100; |
| 33 const SkColor kShadowColor = SkColorSetARGB(0.3 * 255, 0, 0, 0); |
| 34 const SkColor kHintTextColor = SkColorSetRGB(119, 119, 119); |
| 35 const SkColor kResultTextColor = SkColorSetRGB(178, 178, 178); |
| 36 const SkColor kSoundLevelIndicatorColor = SkColorSetRGB(219, 219, 219); |
| 37 |
| 38 // TODO(mukai): check with multiple devices to make sure these limits. |
| 39 const int16 kSoundLevelMin = 50; |
| 40 const int16 kSoundLevelMax = 210; |
| 41 |
| 42 class SoundLevelIndicator : public views::View { |
| 43 public: |
| 44 SoundLevelIndicator(); |
| 45 virtual ~SoundLevelIndicator(); |
| 46 |
| 47 private: |
| 48 // Overridden from views::View: |
| 49 virtual void Paint(gfx::Canvas* canvas) OVERRIDE; |
| 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(SoundLevelIndicator); |
| 52 }; |
| 53 |
| 54 SoundLevelIndicator::SoundLevelIndicator() {} |
| 55 |
| 56 SoundLevelIndicator::~SoundLevelIndicator() {} |
| 57 |
| 58 void SoundLevelIndicator::Paint(gfx::Canvas* canvas) { |
| 59 SkPaint paint; |
| 60 paint.setStyle(SkPaint::kFill_Style); |
| 61 paint.setColor(kSoundLevelIndicatorColor); |
| 62 canvas->DrawCircle(bounds().CenterPoint(), width() / 2, paint); |
| 63 } |
| 64 |
| 65 // MicButton is an image button with circular hit area. |
| 66 class MicButton : public views::ImageButton { |
| 67 public: |
| 68 MicButton(views::ButtonListener* listener); |
| 69 virtual ~MicButton(); |
| 70 |
| 71 private: |
| 72 // Overridden from views::View: |
| 73 virtual bool HitTestRect(const gfx::Rect& rect) const OVERRIDE; |
| 74 |
| 75 DISALLOW_COPY_AND_ASSIGN(MicButton); |
| 76 }; |
| 77 |
| 78 MicButton::MicButton(views::ButtonListener* listener) |
| 79 : views::ImageButton(listener) {} |
| 80 |
| 81 MicButton::~MicButton() {} |
| 82 |
| 83 bool MicButton::HitTestRect(const gfx::Rect& rect) const { |
| 84 if (!views::ImageButton::HitTestRect(rect)) |
| 85 return false; |
| 86 |
| 87 gfx::Rect local_bounds = GetLocalBounds(); |
| 88 int radius = local_bounds.width() / 2; |
| 89 return (rect.origin() - local_bounds.CenterPoint()).LengthSquared() < |
| 90 radius * radius; |
| 91 } |
| 92 |
| 93 } // namespace |
| 94 |
| 95 // static |
| 96 const int SpeechView::kMaxHeight = 300; |
| 97 |
| 98 SpeechView::SpeechView(AppListViewDelegate* delegate) |
| 99 : delegate_(delegate) { |
| 100 set_border(new views::ShadowBorder( |
| 101 kShadowBlur, |
| 102 kShadowColor, |
| 103 kShadowOffset, // Vertical offset. |
| 104 0)); |
| 105 |
| 106 // To keep the painting order of the border and the background, this class |
| 107 // actually has a single child of 'container' which has white background and |
| 108 // contains all components. |
| 109 views::View* container = new views::View(); |
| 110 container->set_background( |
| 111 views::Background::CreateSolidBackground(SK_ColorWHITE)); |
| 112 |
| 113 // TODO: add Google logo. |
| 114 indicator_ = new SoundLevelIndicator(); |
| 115 indicator_->SetVisible(false); |
| 116 container->AddChildView(indicator_); |
| 117 |
| 118 mic_button_ = new MicButton(this); |
| 119 container->AddChildView(mic_button_); |
| 120 |
| 121 // TODO: use BoundedLabel to cap 2 lines. |
| 122 speech_result_ = new views::Label(); |
| 123 speech_result_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 124 const gfx::FontList& font_list = speech_result_->font_list(); |
| 125 speech_result_->SetFontList(font_list.DeriveFontListWithSize(kTextSize)); |
| 126 speech_result_->SetMultiLine(true); |
| 127 container->AddChildView(speech_result_); |
| 128 |
| 129 AddChildView(container); |
| 130 |
| 131 delegate_->GetModel()->speech_ui()->AddObserver(this); |
| 132 indicator_animator_.reset(new views::BoundsAnimator(container)); |
| 133 indicator_animator_->SetAnimationDuration(kIndicatorAnimationDuration); |
| 134 indicator_animator_->set_tween_type(gfx::Tween::LINEAR); |
| 135 |
| 136 Reset(); |
| 137 } |
| 138 |
| 139 SpeechView::~SpeechView() { |
| 140 delegate_->GetModel()->speech_ui()->RemoveObserver(this); |
| 141 } |
| 142 |
| 143 void SpeechView::Reset() { |
| 144 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); |
| 145 speech_result_->SetText(l10n_util::GetStringUTF16( |
| 146 IDS_APP_LIST_SPEECH_HINT_TEXT)); |
| 147 speech_result_->SetEnabledColor(kHintTextColor); |
| 148 mic_button_->SetImage(views::Button::STATE_NORMAL, bundle.GetImageSkiaNamed( |
| 149 IDR_APP_LIST_SPEECH_MIC_ON)); |
| 150 } |
| 151 |
| 152 int SpeechView::GetIndicatorRadius(int16 level) { |
| 153 level = std::min(std::max(level, kSoundLevelMin), kSoundLevelMax); |
| 154 int radius_min = mic_button_->width() / 2; |
| 155 return (level - kSoundLevelMin) * (kIndicatorRadiusMax - radius_min) / |
| 156 (kSoundLevelMax - kSoundLevelMin) + radius_min; |
| 157 } |
| 158 |
| 159 void SpeechView::Layout() { |
| 160 views::View* container = child_at(0); |
| 161 container->SetBoundsRect(GetContentsBounds()); |
| 162 |
| 163 // Because container is a pure View, this class should layout its children. |
| 164 // TODO: arrange Google logo. |
| 165 const gfx::Rect contents_bounds = container->GetContentsBounds(); |
| 166 gfx::Size mic_size = mic_button_->GetPreferredSize(); |
| 167 gfx::Point mic_origin( |
| 168 contents_bounds.right() - kMicButtonMargin - mic_size.width(), |
| 169 contents_bounds.y() + kMicButtonMargin); |
| 170 mic_button_->SetBoundsRect(gfx::Rect(mic_origin, mic_size)); |
| 171 |
| 172 int speech_width = contents_bounds.width() - kTextMargin * 2; |
| 173 speech_result_->SizeToFit(speech_width); |
| 174 int speech_height = speech_result_->GetHeightForWidth(speech_width); |
| 175 speech_result_->SetBounds( |
| 176 contents_bounds.x() + kTextMargin, |
| 177 contents_bounds.bottom() - kTextMargin - speech_height, |
| 178 speech_width, |
| 179 speech_height); |
| 180 } |
| 181 |
| 182 void SpeechView::ButtonPressed(views::Button* sender, const ui::Event& event) { |
| 183 delegate_->ToggleSpeechRecognition(); |
| 184 } |
| 185 |
| 186 void SpeechView::OnSpeechSoundLevelChanged(int16 level) { |
| 187 if (!visible()) |
| 188 return; |
| 189 |
| 190 gfx::Point origin = mic_button_->bounds().CenterPoint(); |
| 191 int radius = GetIndicatorRadius(level); |
| 192 origin.Offset(-radius, -radius); |
| 193 gfx::Rect indicator_bounds = |
| 194 gfx::Rect(origin, gfx::Size(radius * 2, radius * 2)); |
| 195 if (indicator_->visible()) { |
| 196 indicator_animator_->AnimateViewTo(indicator_, indicator_bounds); |
| 197 } else { |
| 198 indicator_->SetVisible(true); |
| 199 indicator_->SetBoundsRect(indicator_bounds); |
| 200 } |
| 201 } |
| 202 |
| 203 void SpeechView::OnSpeechResult(const base::string16& result, |
| 204 bool is_final) { |
| 205 speech_result_->SetText(result); |
| 206 speech_result_->SetEnabledColor(kResultTextColor); |
| 207 } |
| 208 |
| 209 void SpeechView::OnSpeechRecognitionStateChanged( |
| 210 SpeechRecognitionState new_state) { |
| 211 int resource_id = IDR_APP_LIST_SPEECH_MIC_OFF; |
| 212 if (new_state == SPEECH_RECOGNITION_ON) |
| 213 resource_id = IDR_APP_LIST_SPEECH_MIC_ON; |
| 214 else if (new_state == SPEECH_RECOGNITION_IN_SPEECH) |
| 215 resource_id = IDR_APP_LIST_SPEECH_MIC_RECORDING; |
| 216 |
| 217 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); |
| 218 mic_button_->SetImage(views::Button::STATE_NORMAL, bundle.GetImageSkiaNamed( |
| 219 resource_id)); |
| 220 } |
| 221 |
| 222 } // namespace app_list |
OLD | NEW |