Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / app_list / views / speech_view.cc
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 "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"
25
26 namespace app_list {
27
28 namespace {
29
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);
47
48 class SoundLevelIndicator : public views::View {
49  public:
50   SoundLevelIndicator();
51   virtual ~SoundLevelIndicator();
52
53  private:
54   // Overridden from views::View:
55   virtual void Paint(gfx::Canvas* canvas) OVERRIDE;
56
57   DISALLOW_COPY_AND_ASSIGN(SoundLevelIndicator);
58 };
59
60 SoundLevelIndicator::SoundLevelIndicator() {}
61
62 SoundLevelIndicator::~SoundLevelIndicator() {}
63
64 void SoundLevelIndicator::Paint(gfx::Canvas* canvas) {
65   SkPaint paint;
66   paint.setStyle(SkPaint::kFill_Style);
67   paint.setColor(kSoundLevelIndicatorColor);
68   paint.setAntiAlias(true);
69   canvas->DrawCircle(bounds().CenterPoint(), width() / 2, paint);
70 }
71
72 // MicButton is an image button with circular hit area.
73 class MicButton : public views::ImageButton {
74  public:
75   explicit MicButton(views::ButtonListener* listener);
76   virtual ~MicButton();
77
78  private:
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;
83
84   DISALLOW_COPY_AND_ASSIGN(MicButton);
85 };
86
87 MicButton::MicButton(views::ButtonListener* listener)
88     : views::ImageButton(listener) {}
89
90 MicButton::~MicButton() {}
91
92 bool MicButton::HasHitTestMask() const {
93   return true;
94 }
95
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));
106 }
107
108 }  // namespace
109
110 // static
111
112 SpeechView::SpeechView(AppListViewDelegate* delegate)
113     : delegate_(delegate),
114       logo_(NULL) {
115   SetBorder(scoped_ptr<views::Border>(
116       new views::ShadowBorder(kShadowBlur,
117                               kShadowColor,
118                               kShadowOffset,  // Vertical offset.
119                               0)));
120
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));
127
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_);
133   }
134
135   indicator_ = new SoundLevelIndicator();
136   indicator_->SetVisible(false);
137   container->AddChildView(indicator_);
138
139   mic_button_ = new MicButton(this);
140   container->AddChildView(mic_button_);
141
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);
147
148   speech_result_->SetMultiLine(true);
149   container->AddChildView(speech_result_);
150
151   AddChildView(container);
152
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);
157
158   Reset();
159 }
160
161 SpeechView::~SpeechView() {
162   delegate_->GetSpeechUI()->RemoveObserver(this);
163 }
164
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));
172 }
173
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;
178 }
179
180 void SpeechView::Layout() {
181   views::View* container = child_at(0);
182   container->SetBoundsRect(GetContentsBounds());
183
184   // Because container is a pure View, this class should layout its children.
185   const gfx::Rect contents_bounds = container->GetContentsBounds();
186   if (logo_)
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));
193
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,
200       speech_width,
201       speech_height);
202 }
203
204 gfx::Size SpeechView::GetPreferredSize() {
205   return gfx::Size(0, kSpeechViewMaxHeight);
206 }
207
208 void SpeechView::ButtonPressed(views::Button* sender, const ui::Event& event) {
209   delegate_->ToggleSpeechRecognition();
210 }
211
212 void SpeechView::OnSpeechSoundLevelChanged(uint8 level) {
213   if (!visible())
214     return;
215
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);
223   } else {
224     indicator_->SetVisible(true);
225     indicator_->SetBoundsRect(indicator_bounds);
226   }
227 }
228
229 void SpeechView::OnSpeechResult(const base::string16& result,
230                                 bool is_final) {
231   speech_result_->SetText(result);
232   speech_result_->SetEnabledColor(kResultTextColor);
233 }
234
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;
242
243   ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
244   mic_button_->SetImage(views::Button::STATE_NORMAL,
245                         bundle.GetImageSkiaNamed(resource_id));
246 }
247
248 }  // namespace app_list