summaryrefslogtreecommitdiffstats
path: root/ui/app_list/views/speech_view.cc
blob: 9b92e7e158a621f6fa6d358f34f14a35cddeae52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/app_list/views/speech_view.h"

#include "base/strings/utf_string_conversions.h"
#include "grit/ui_resources.h"
#include "grit/ui_strings.h"
#include "third_party/skia/include/core/SkPath.h"
#include "ui/app_list/app_list_model.h"
#include "ui/app_list/app_list_view_delegate.h"
#include "ui/app_list/speech_ui_model.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/path.h"
#include "ui/views/animation/bounds_animator.h"
#include "ui/views/background.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/shadow_border.h"

namespace app_list {

namespace {

const int kShadowOffset = 1;
const int kShadowBlur = 4;
const int kSpeechViewMaxHeight = 300;
const int kMicButtonMargin = 12;
const int kTextMargin = 32;
const int kLogoMarginLeft = 30;
const int kLogoMarginTop = 28;
const int kLogoWidth = 104;
const int kLogoHeight = 36;
const int kIndicatorCenterOffsetY = -1;
const int kIndicatorRadiusMinOffset = -3;
const int kIndicatorRadiusMax = 100;
const int kIndicatorAnimationDuration = 100;
const SkColor kShadowColor = SkColorSetARGB(0.3 * 255, 0, 0, 0);
const SkColor kHintTextColor = SkColorSetRGB(119, 119, 119);
const SkColor kResultTextColor = SkColorSetRGB(178, 178, 178);
const SkColor kSoundLevelIndicatorColor = SkColorSetRGB(219, 219, 219);

class SoundLevelIndicator : public views::View {
 public:
  SoundLevelIndicator();
  virtual ~SoundLevelIndicator();

 private:
  // Overridden from views::View:
  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;

  DISALLOW_COPY_AND_ASSIGN(SoundLevelIndicator);
};

SoundLevelIndicator::SoundLevelIndicator() {}

SoundLevelIndicator::~SoundLevelIndicator() {}

void SoundLevelIndicator::OnPaint(gfx::Canvas* canvas) {
  SkPaint paint;
  paint.setStyle(SkPaint::kFill_Style);
  paint.setColor(kSoundLevelIndicatorColor);
  paint.setAntiAlias(true);
  canvas->DrawCircle(bounds().CenterPoint(), width() / 2, paint);
}

// MicButton is an image button with circular hit area.
class MicButton : public views::ImageButton {
 public:
  explicit MicButton(views::ButtonListener* listener);
  virtual ~MicButton();

 private:
  // Overridden from views::View:
  virtual bool HasHitTestMask() const OVERRIDE;
  virtual void GetHitTestMaskDeprecated(views::View::HitTestSource source,
                                        gfx::Path* mask) const OVERRIDE;

  DISALLOW_COPY_AND_ASSIGN(MicButton);
};

MicButton::MicButton(views::ButtonListener* listener)
    : views::ImageButton(listener) {}

MicButton::~MicButton() {}

bool MicButton::HasHitTestMask() const {
  return true;
}

void MicButton::GetHitTestMaskDeprecated(views::View::HitTestSource source,
                                         gfx::Path* mask) const {
  DCHECK(mask);

  // The mic button icon is a circle. |source| doesn't matter.
  gfx::Rect local_bounds = GetLocalBounds();
  int radius = local_bounds.width() / 2 + kIndicatorRadiusMinOffset;
  gfx::Point center = local_bounds.CenterPoint();
  center.set_y(center.y() + kIndicatorCenterOffsetY);
  mask->addCircle(SkIntToScalar(center.x()),
                  SkIntToScalar(center.y()),
                  SkIntToScalar(radius));
}

}  // namespace

// static

SpeechView::SpeechView(AppListViewDelegate* delegate)
    : delegate_(delegate),
      logo_(NULL) {
  SetBorder(scoped_ptr<views::Border>(
      new views::ShadowBorder(kShadowBlur,
                              kShadowColor,
                              kShadowOffset,  // Vertical offset.
                              0)));

  // To keep the painting order of the border and the background, this class
  // actually has a single child of 'container' which has white background and
  // contains all components.
  views::View* container = new views::View();
  container->set_background(
      views::Background::CreateSolidBackground(SK_ColorWHITE));

  const gfx::ImageSkia& logo_image = delegate_->GetSpeechUI()->logo();
  if (!logo_image.isNull()) {
    logo_ = new views::ImageView();
    logo_->SetImage(&logo_image);
    container->AddChildView(logo_);
  }

  indicator_ = new SoundLevelIndicator();
  indicator_->SetVisible(false);
  container->AddChildView(indicator_);

  mic_button_ = new MicButton(this);
  container->AddChildView(mic_button_);

  // TODO(mukai): use BoundedLabel to cap 2 lines.
  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
  speech_result_ = new views::Label(
      base::string16(), bundle.GetFontList(ui::ResourceBundle::LargeFont));
  speech_result_->SetHorizontalAlignment(gfx::ALIGN_LEFT);

  speech_result_->SetMultiLine(true);
  container->AddChildView(speech_result_);

  AddChildView(container);

  delegate_->GetSpeechUI()->AddObserver(this);
  indicator_animator_.reset(new views::BoundsAnimator(container));
  indicator_animator_->SetAnimationDuration(kIndicatorAnimationDuration);
  indicator_animator_->set_tween_type(gfx::Tween::LINEAR);

  Reset();
}

SpeechView::~SpeechView() {
  delegate_->GetSpeechUI()->RemoveObserver(this);
}

void SpeechView::Reset() {
  OnSpeechRecognitionStateChanged(delegate_->GetSpeechUI()->state());
}

int SpeechView::GetIndicatorRadius(uint8 level) {
  int radius_min = mic_button_->width() / 2 + kIndicatorRadiusMinOffset;
  int range = kIndicatorRadiusMax - radius_min;
  return level * range / kuint8max + radius_min;
}

void SpeechView::Layout() {
  views::View* container = child_at(0);
  container->SetBoundsRect(GetContentsBounds());

  // Because container is a pure View, this class should layout its children.
  const gfx::Rect contents_bounds = container->GetContentsBounds();
  if (logo_)
    logo_->SetBounds(kLogoMarginLeft, kLogoMarginTop, kLogoWidth, kLogoHeight);
  gfx::Size mic_size = mic_button_->GetPreferredSize();
  gfx::Point mic_origin(
      contents_bounds.right() - kMicButtonMargin - mic_size.width(),
      contents_bounds.y() + kMicButtonMargin);
  mic_button_->SetBoundsRect(gfx::Rect(mic_origin, mic_size));

  int speech_width = contents_bounds.width() - kTextMargin * 2;
  speech_result_->SizeToFit(speech_width);
  int speech_height = speech_result_->GetHeightForWidth(speech_width);
  speech_result_->SetBounds(
      contents_bounds.x() + kTextMargin,
      contents_bounds.bottom() - kTextMargin - speech_height,
      speech_width,
      speech_height);
}

gfx::Size SpeechView::GetPreferredSize() const {
  return gfx::Size(0, kSpeechViewMaxHeight);
}

void SpeechView::ButtonPressed(views::Button* sender, const ui::Event& event) {
  delegate_->ToggleSpeechRecognition();
}

void SpeechView::OnSpeechSoundLevelChanged(uint8 level) {
  if (!visible() ||
      delegate_->GetSpeechUI()->state() == SPEECH_RECOGNITION_NETWORK_ERROR)
    return;

  gfx::Point origin = mic_button_->bounds().CenterPoint();
  int radius = GetIndicatorRadius(level);
  origin.Offset(-radius, -radius + kIndicatorCenterOffsetY);
  gfx::Rect indicator_bounds =
      gfx::Rect(origin, gfx::Size(radius * 2, radius * 2));
  if (indicator_->visible()) {
    indicator_animator_->AnimateViewTo(indicator_, indicator_bounds);
  } else {
    indicator_->SetVisible(true);
    indicator_->SetBoundsRect(indicator_bounds);
  }
}

void SpeechView::OnSpeechResult(const base::string16& result,
                                bool is_final) {
  speech_result_->SetText(result);
  speech_result_->SetEnabledColor(kResultTextColor);
}

void SpeechView::OnSpeechRecognitionStateChanged(
    SpeechRecognitionState new_state) {
  int resource_id = IDR_APP_LIST_SPEECH_MIC_OFF;
  if (new_state == SPEECH_RECOGNITION_RECOGNIZING)
    resource_id = IDR_APP_LIST_SPEECH_MIC_ON;
  else if (new_state == SPEECH_RECOGNITION_IN_SPEECH)
    resource_id = IDR_APP_LIST_SPEECH_MIC_RECORDING;

  int text_resource_id = IDS_APP_LIST_SPEECH_HINT_TEXT;

  if (new_state == SPEECH_RECOGNITION_NETWORK_ERROR) {
    text_resource_id = IDS_APP_LIST_SPEECH_NETWORK_ERROR_HINT_TEXT;
    indicator_->SetVisible(false);
  }
  speech_result_->SetText(l10n_util::GetStringUTF16(text_resource_id));
  speech_result_->SetEnabledColor(kHintTextColor);

  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
  mic_button_->SetImage(views::Button::STATE_NORMAL,
                        bundle.GetImageSkiaNamed(resource_id));
}

}  // namespace app_list