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.
5 #include "ui/app_list/views/speech_view.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "grit/ui_resources.h"
9 #include "grit/ui_strings.h"
10 #include "third_party/skia/include/core/SkPath.h"
11 #include "ui/app_list/app_list_model.h"
12 #include "ui/app_list/app_list_view_delegate.h"
13 #include "ui/app_list/speech_ui_model.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/gfx/canvas.h"
17 #include "ui/gfx/path.h"
18 #include "ui/views/animation/bounds_animator.h"
19 #include "ui/views/background.h"
20 #include "ui/views/controls/button/image_button.h"
21 #include "ui/views/controls/image_view.h"
22 #include "ui/views/controls/label.h"
23 #include "ui/views/layout/fill_layout.h"
24 #include "ui/views/shadow_border.h"
30 const int kShadowOffset = 1;
31 const int kShadowBlur = 4;
32 const int kSpeechViewMaxHeight = 300;
33 const int kMicButtonMargin = 12;
34 const int kTextMargin = 32;
35 const int kLogoMarginLeft = 30;
36 const int kLogoMarginTop = 28;
37 const int kLogoWidth = 104;
38 const int kLogoHeight = 36;
39 const int kIndicatorCenterOffsetY = -1;
40 const int kIndicatorRadiusMinOffset = -3;
41 const int kIndicatorRadiusMax = 100;
42 const int kIndicatorAnimationDuration = 100;
43 const SkColor kShadowColor = SkColorSetARGB(0.3 * 255, 0, 0, 0);
44 const SkColor kHintTextColor = SkColorSetRGB(119, 119, 119);
45 const SkColor kResultTextColor = SkColorSetRGB(178, 178, 178);
46 const SkColor kSoundLevelIndicatorColor = SkColorSetRGB(219, 219, 219);
48 class SoundLevelIndicator : public views::View {
50 SoundLevelIndicator();
51 virtual ~SoundLevelIndicator();
54 // Overridden from views::View:
55 virtual void Paint(gfx::Canvas* canvas) OVERRIDE;
57 DISALLOW_COPY_AND_ASSIGN(SoundLevelIndicator);
60 SoundLevelIndicator::SoundLevelIndicator() {}
62 SoundLevelIndicator::~SoundLevelIndicator() {}
64 void SoundLevelIndicator::Paint(gfx::Canvas* canvas) {
66 paint.setStyle(SkPaint::kFill_Style);
67 paint.setColor(kSoundLevelIndicatorColor);
68 paint.setAntiAlias(true);
69 canvas->DrawCircle(bounds().CenterPoint(), width() / 2, paint);
72 // MicButton is an image button with circular hit area.
73 class MicButton : public views::ImageButton {
75 explicit MicButton(views::ButtonListener* listener);
79 // Overridden from views::View:
80 virtual bool HasHitTestMask() const OVERRIDE;
81 virtual void GetHitTestMask(views::View::HitTestSource source,
82 gfx::Path* mask) const OVERRIDE;
84 DISALLOW_COPY_AND_ASSIGN(MicButton);
87 MicButton::MicButton(views::ButtonListener* listener)
88 : views::ImageButton(listener) {}
90 MicButton::~MicButton() {}
92 bool MicButton::HasHitTestMask() const {
96 void MicButton::GetHitTestMask(views::View::HitTestSource source,
97 gfx::Path* mask) const {
98 // The mic button icon is a circle. |source| doesn't matter.
99 gfx::Rect local_bounds = GetLocalBounds();
100 int radius = local_bounds.width() / 2 + kIndicatorRadiusMinOffset;
101 gfx::Point center = local_bounds.CenterPoint();
102 center.set_y(center.y() + kIndicatorCenterOffsetY);
103 mask->addCircle(SkIntToScalar(center.x()),
104 SkIntToScalar(center.y()),
105 SkIntToScalar(radius));
112 SpeechView::SpeechView(AppListViewDelegate* delegate)
113 : delegate_(delegate),
115 SetBorder(scoped_ptr<views::Border>(
116 new views::ShadowBorder(kShadowBlur,
118 kShadowOffset, // Vertical offset.
121 // To keep the painting order of the border and the background, this class
122 // actually has a single child of 'container' which has white background and
123 // contains all components.
124 views::View* container = new views::View();
125 container->set_background(
126 views::Background::CreateSolidBackground(SK_ColorWHITE));
128 const gfx::ImageSkia& logo_image = delegate_->GetSpeechUI()->logo();
129 if (!logo_image.isNull()) {
130 logo_ = new views::ImageView();
131 logo_->SetImage(&logo_image);
132 container->AddChildView(logo_);
135 indicator_ = new SoundLevelIndicator();
136 indicator_->SetVisible(false);
137 container->AddChildView(indicator_);
139 mic_button_ = new MicButton(this);
140 container->AddChildView(mic_button_);
142 // TODO(mukai): use BoundedLabel to cap 2 lines.
143 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
144 speech_result_ = new views::Label(
145 base::string16(), bundle.GetFontList(ui::ResourceBundle::LargeFont));
146 speech_result_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
148 speech_result_->SetMultiLine(true);
149 container->AddChildView(speech_result_);
151 AddChildView(container);
153 delegate_->GetSpeechUI()->AddObserver(this);
154 indicator_animator_.reset(new views::BoundsAnimator(container));
155 indicator_animator_->SetAnimationDuration(kIndicatorAnimationDuration);
156 indicator_animator_->set_tween_type(gfx::Tween::LINEAR);
161 SpeechView::~SpeechView() {
162 delegate_->GetSpeechUI()->RemoveObserver(this);
165 void SpeechView::Reset() {
166 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
167 speech_result_->SetText(l10n_util::GetStringUTF16(
168 IDS_APP_LIST_SPEECH_HINT_TEXT));
169 speech_result_->SetEnabledColor(kHintTextColor);
170 mic_button_->SetImage(views::Button::STATE_NORMAL,
171 bundle.GetImageSkiaNamed(IDR_APP_LIST_SPEECH_MIC_ON));
174 int SpeechView::GetIndicatorRadius(uint8 level) {
175 int radius_min = mic_button_->width() / 2 + kIndicatorRadiusMinOffset;
176 int range = kIndicatorRadiusMax - radius_min;
177 return level * range / kuint8max + radius_min;
180 void SpeechView::Layout() {
181 views::View* container = child_at(0);
182 container->SetBoundsRect(GetContentsBounds());
184 // Because container is a pure View, this class should layout its children.
185 const gfx::Rect contents_bounds = container->GetContentsBounds();
187 logo_->SetBounds(kLogoMarginLeft, kLogoMarginTop, kLogoWidth, kLogoHeight);
188 gfx::Size mic_size = mic_button_->GetPreferredSize();
189 gfx::Point mic_origin(
190 contents_bounds.right() - kMicButtonMargin - mic_size.width(),
191 contents_bounds.y() + kMicButtonMargin);
192 mic_button_->SetBoundsRect(gfx::Rect(mic_origin, mic_size));
194 int speech_width = contents_bounds.width() - kTextMargin * 2;
195 speech_result_->SizeToFit(speech_width);
196 int speech_height = speech_result_->GetHeightForWidth(speech_width);
197 speech_result_->SetBounds(
198 contents_bounds.x() + kTextMargin,
199 contents_bounds.bottom() - kTextMargin - speech_height,
204 gfx::Size SpeechView::GetPreferredSize() {
205 return gfx::Size(0, kSpeechViewMaxHeight);
208 void SpeechView::ButtonPressed(views::Button* sender, const ui::Event& event) {
209 delegate_->ToggleSpeechRecognition();
212 void SpeechView::OnSpeechSoundLevelChanged(uint8 level) {
216 gfx::Point origin = mic_button_->bounds().CenterPoint();
217 int radius = GetIndicatorRadius(level);
218 origin.Offset(-radius, -radius + kIndicatorCenterOffsetY);
219 gfx::Rect indicator_bounds =
220 gfx::Rect(origin, gfx::Size(radius * 2, radius * 2));
221 if (indicator_->visible()) {
222 indicator_animator_->AnimateViewTo(indicator_, indicator_bounds);
224 indicator_->SetVisible(true);
225 indicator_->SetBoundsRect(indicator_bounds);
229 void SpeechView::OnSpeechResult(const base::string16& result,
231 speech_result_->SetText(result);
232 speech_result_->SetEnabledColor(kResultTextColor);
235 void SpeechView::OnSpeechRecognitionStateChanged(
236 SpeechRecognitionState new_state) {
237 int resource_id = IDR_APP_LIST_SPEECH_MIC_OFF;
238 if (new_state == SPEECH_RECOGNITION_RECOGNIZING)
239 resource_id = IDR_APP_LIST_SPEECH_MIC_ON;
240 else if (new_state == SPEECH_RECOGNITION_IN_SPEECH)
241 resource_id = IDR_APP_LIST_SPEECH_MIC_RECORDING;
243 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
244 mic_button_->SetImage(views::Button::STATE_NORMAL,
245 bundle.GetImageSkiaNamed(resource_id));
248 } // namespace app_list