Update path of checkdeps to buildtools checkout
[chromium-blink-merge.git] / ui / app_list / views / speech_view.cc
blobba2e690e2f06d816e31429393ddd9a56787a83fa
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"
26 namespace app_list {
28 namespace {
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 {
49 public:
50 SoundLevelIndicator();
51 virtual ~SoundLevelIndicator();
53 private:
54 // Overridden from views::View:
55 virtual void Paint(gfx::Canvas* canvas,
56 const views::CullSet& cull_set) OVERRIDE;
58 DISALLOW_COPY_AND_ASSIGN(SoundLevelIndicator);
61 SoundLevelIndicator::SoundLevelIndicator() {}
63 SoundLevelIndicator::~SoundLevelIndicator() {}
65 void SoundLevelIndicator::Paint(gfx::Canvas* canvas,
66 const views::CullSet& cull_set) {
67 SkPaint paint;
68 paint.setStyle(SkPaint::kFill_Style);
69 paint.setColor(kSoundLevelIndicatorColor);
70 paint.setAntiAlias(true);
71 canvas->DrawCircle(bounds().CenterPoint(), width() / 2, paint);
74 // MicButton is an image button with circular hit area.
75 class MicButton : public views::ImageButton {
76 public:
77 explicit MicButton(views::ButtonListener* listener);
78 virtual ~MicButton();
80 private:
81 // Overridden from views::View:
82 virtual bool HasHitTestMask() const OVERRIDE;
83 virtual void GetHitTestMask(views::View::HitTestSource source,
84 gfx::Path* mask) const OVERRIDE;
86 DISALLOW_COPY_AND_ASSIGN(MicButton);
89 MicButton::MicButton(views::ButtonListener* listener)
90 : views::ImageButton(listener) {}
92 MicButton::~MicButton() {}
94 bool MicButton::HasHitTestMask() const {
95 return true;
98 void MicButton::GetHitTestMask(views::View::HitTestSource source,
99 gfx::Path* mask) const {
100 // The mic button icon is a circle. |source| doesn't matter.
101 gfx::Rect local_bounds = GetLocalBounds();
102 int radius = local_bounds.width() / 2 + kIndicatorRadiusMinOffset;
103 gfx::Point center = local_bounds.CenterPoint();
104 center.set_y(center.y() + kIndicatorCenterOffsetY);
105 mask->addCircle(SkIntToScalar(center.x()),
106 SkIntToScalar(center.y()),
107 SkIntToScalar(radius));
110 } // namespace
112 // static
114 SpeechView::SpeechView(AppListViewDelegate* delegate)
115 : delegate_(delegate),
116 logo_(NULL) {
117 SetBorder(scoped_ptr<views::Border>(
118 new views::ShadowBorder(kShadowBlur,
119 kShadowColor,
120 kShadowOffset, // Vertical offset.
121 0)));
123 // To keep the painting order of the border and the background, this class
124 // actually has a single child of 'container' which has white background and
125 // contains all components.
126 views::View* container = new views::View();
127 container->set_background(
128 views::Background::CreateSolidBackground(SK_ColorWHITE));
130 const gfx::ImageSkia& logo_image = delegate_->GetSpeechUI()->logo();
131 if (!logo_image.isNull()) {
132 logo_ = new views::ImageView();
133 logo_->SetImage(&logo_image);
134 container->AddChildView(logo_);
137 indicator_ = new SoundLevelIndicator();
138 indicator_->SetVisible(false);
139 container->AddChildView(indicator_);
141 mic_button_ = new MicButton(this);
142 container->AddChildView(mic_button_);
144 // TODO(mukai): use BoundedLabel to cap 2 lines.
145 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
146 speech_result_ = new views::Label(
147 base::string16(), bundle.GetFontList(ui::ResourceBundle::LargeFont));
148 speech_result_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
150 speech_result_->SetMultiLine(true);
151 container->AddChildView(speech_result_);
153 AddChildView(container);
155 delegate_->GetSpeechUI()->AddObserver(this);
156 indicator_animator_.reset(new views::BoundsAnimator(container));
157 indicator_animator_->SetAnimationDuration(kIndicatorAnimationDuration);
158 indicator_animator_->set_tween_type(gfx::Tween::LINEAR);
160 Reset();
163 SpeechView::~SpeechView() {
164 delegate_->GetSpeechUI()->RemoveObserver(this);
167 void SpeechView::Reset() {
168 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
169 speech_result_->SetText(l10n_util::GetStringUTF16(
170 IDS_APP_LIST_SPEECH_HINT_TEXT));
171 speech_result_->SetEnabledColor(kHintTextColor);
172 mic_button_->SetImage(views::Button::STATE_NORMAL,
173 bundle.GetImageSkiaNamed(IDR_APP_LIST_SPEECH_MIC_ON));
176 int SpeechView::GetIndicatorRadius(uint8 level) {
177 int radius_min = mic_button_->width() / 2 + kIndicatorRadiusMinOffset;
178 int range = kIndicatorRadiusMax - radius_min;
179 return level * range / kuint8max + radius_min;
182 void SpeechView::Layout() {
183 views::View* container = child_at(0);
184 container->SetBoundsRect(GetContentsBounds());
186 // Because container is a pure View, this class should layout its children.
187 const gfx::Rect contents_bounds = container->GetContentsBounds();
188 if (logo_)
189 logo_->SetBounds(kLogoMarginLeft, kLogoMarginTop, kLogoWidth, kLogoHeight);
190 gfx::Size mic_size = mic_button_->GetPreferredSize();
191 gfx::Point mic_origin(
192 contents_bounds.right() - kMicButtonMargin - mic_size.width(),
193 contents_bounds.y() + kMicButtonMargin);
194 mic_button_->SetBoundsRect(gfx::Rect(mic_origin, mic_size));
196 int speech_width = contents_bounds.width() - kTextMargin * 2;
197 speech_result_->SizeToFit(speech_width);
198 int speech_height = speech_result_->GetHeightForWidth(speech_width);
199 speech_result_->SetBounds(
200 contents_bounds.x() + kTextMargin,
201 contents_bounds.bottom() - kTextMargin - speech_height,
202 speech_width,
203 speech_height);
206 gfx::Size SpeechView::GetPreferredSize() const {
207 return gfx::Size(0, kSpeechViewMaxHeight);
210 void SpeechView::ButtonPressed(views::Button* sender, const ui::Event& event) {
211 delegate_->ToggleSpeechRecognition();
214 void SpeechView::OnSpeechSoundLevelChanged(uint8 level) {
215 if (!visible())
216 return;
218 gfx::Point origin = mic_button_->bounds().CenterPoint();
219 int radius = GetIndicatorRadius(level);
220 origin.Offset(-radius, -radius + kIndicatorCenterOffsetY);
221 gfx::Rect indicator_bounds =
222 gfx::Rect(origin, gfx::Size(radius * 2, radius * 2));
223 if (indicator_->visible()) {
224 indicator_animator_->AnimateViewTo(indicator_, indicator_bounds);
225 } else {
226 indicator_->SetVisible(true);
227 indicator_->SetBoundsRect(indicator_bounds);
231 void SpeechView::OnSpeechResult(const base::string16& result,
232 bool is_final) {
233 speech_result_->SetText(result);
234 speech_result_->SetEnabledColor(kResultTextColor);
237 void SpeechView::OnSpeechRecognitionStateChanged(
238 SpeechRecognitionState new_state) {
239 int resource_id = IDR_APP_LIST_SPEECH_MIC_OFF;
240 if (new_state == SPEECH_RECOGNITION_RECOGNIZING)
241 resource_id = IDR_APP_LIST_SPEECH_MIC_ON;
242 else if (new_state == SPEECH_RECOGNITION_IN_SPEECH)
243 resource_id = IDR_APP_LIST_SPEECH_MIC_RECORDING;
245 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
246 mic_button_->SetImage(views::Button::STATE_NORMAL,
247 bundle.GetImageSkiaNamed(resource_id));
250 } // namespace app_list