Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / ui / app_list / views / speech_view.cc
index c6bad7c..7fb213f 100644 (file)
@@ -5,19 +5,23 @@
 #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/resources/grit/ui_resources.h"
+#include "ui/strings/grit/ui_strings.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/masked_targeter_delegate.h"
 #include "ui/views/shadow_border.h"
 
 namespace app_list {
@@ -27,9 +31,14 @@ namespace {
 const int kShadowOffset = 1;
 const int kShadowBlur = 4;
 const int kSpeechViewMaxHeight = 300;
-const int kTextSize = 20;
 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);
@@ -44,7 +53,7 @@ class SoundLevelIndicator : public views::View {
 
  private:
   // Overridden from views::View:
-  virtual void Paint(gfx::Canvas* canvas) OVERRIDE;
+  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
 
   DISALLOW_COPY_AND_ASSIGN(SoundLevelIndicator);
 };
@@ -53,22 +62,24 @@ SoundLevelIndicator::SoundLevelIndicator() {}
 
 SoundLevelIndicator::~SoundLevelIndicator() {}
 
-void SoundLevelIndicator::Paint(gfx::Canvas* canvas) {
+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 {
+// MicButton is an image button with a circular hit test mask.
+class MicButton : public views::ImageButton,
+                  public views::MaskedTargeterDelegate {
  public:
   explicit MicButton(views::ButtonListener* listener);
   virtual ~MicButton();
 
  private:
-  // Overridden from views::View:
-  virtual bool HitTestRect(const gfx::Rect& rect) const OVERRIDE;
+  // views::MaskedTargeterDelegate:
+  virtual bool GetHitTestMask(gfx::Path* mask) const OVERRIDE;
 
   DISALLOW_COPY_AND_ASSIGN(MicButton);
 };
@@ -78,14 +89,18 @@ MicButton::MicButton(views::ButtonListener* listener)
 
 MicButton::~MicButton() {}
 
-bool MicButton::HitTestRect(const gfx::Rect& rect) const {
-  if (!views::ImageButton::HitTestRect(rect))
-    return false;
+bool MicButton::GetHitTestMask(gfx::Path* mask) const {
+  DCHECK(mask);
 
+  // The mic button icon is a circle.
   gfx::Rect local_bounds = GetLocalBounds();
-  int radius = local_bounds.width() / 2;
-  return (rect.origin() - local_bounds.CenterPoint()).LengthSquared() <
-      radius * radius;
+  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));
+  return true;
 }
 
 }  // namespace
@@ -93,7 +108,8 @@ bool MicButton::HitTestRect(const gfx::Rect& rect) const {
 // static
 
 SpeechView::SpeechView(AppListViewDelegate* delegate)
-    : delegate_(delegate) {
+    : delegate_(delegate),
+      logo_(NULL) {
   SetBorder(scoped_ptr<views::Border>(
       new views::ShadowBorder(kShadowBlur,
                               kShadowColor,
@@ -107,19 +123,29 @@ SpeechView::SpeechView(AppListViewDelegate* delegate)
   container->set_background(
       views::Background::CreateSolidBackground(SK_ColorWHITE));
 
-  // TODO(mukai): add Google logo.
+  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);
+  MicButton* mic_button = new MicButton(this);
+  mic_button_ = mic_button;
   container->AddChildView(mic_button_);
+  mic_button_->SetEventTargeter(
+      scoped_ptr<views::ViewTargeter>(new views::ViewTargeter(mic_button)));
 
   // TODO(mukai): use BoundedLabel to cap 2 lines.
-  speech_result_ = new views::Label();
+  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
+  speech_result_ = new views::Label(
+      base::string16(), bundle.GetFontList(ui::ResourceBundle::LargeFont));
   speech_result_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
-  const gfx::FontList& font_list = speech_result_->font_list();
-  speech_result_->SetFontList(font_list.DeriveFontListWithSize(kTextSize));
+
   speech_result_->SetMultiLine(true);
   container->AddChildView(speech_result_);
 
@@ -138,16 +164,11 @@ SpeechView::~SpeechView() {
 }
 
 void SpeechView::Reset() {
-  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
-  speech_result_->SetText(l10n_util::GetStringUTF16(
-      IDS_APP_LIST_SPEECH_HINT_TEXT));
-  speech_result_->SetEnabledColor(kHintTextColor);
-  mic_button_->SetImage(views::Button::STATE_NORMAL, bundle.GetImageSkiaNamed(
-      IDR_APP_LIST_SPEECH_MIC_ON));
+  OnSpeechRecognitionStateChanged(delegate_->GetSpeechUI()->state());
 }
 
 int SpeechView::GetIndicatorRadius(uint8 level) {
-  int radius_min = mic_button_->width() / 2;
+  int radius_min = mic_button_->width() / 2 + kIndicatorRadiusMinOffset;
   int range = kIndicatorRadiusMax - radius_min;
   return level * range / kuint8max + radius_min;
 }
@@ -157,8 +178,9 @@ void SpeechView::Layout() {
   container->SetBoundsRect(GetContentsBounds());
 
   // Because container is a pure View, this class should layout its children.
-  // TODO(mukai): arrange Google logo.
   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(),
@@ -175,7 +197,7 @@ void SpeechView::Layout() {
       speech_height);
 }
 
-gfx::Size SpeechView::GetPreferredSize() {
+gfx::Size SpeechView::GetPreferredSize() const {
   return gfx::Size(0, kSpeechViewMaxHeight);
 }
 
@@ -184,12 +206,13 @@ void SpeechView::ButtonPressed(views::Button* sender, const ui::Event& event) {
 }
 
 void SpeechView::OnSpeechSoundLevelChanged(uint8 level) {
-  if (!visible())
+  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);
+  origin.Offset(-radius, -radius + kIndicatorCenterOffsetY);
   gfx::Rect indicator_bounds =
       gfx::Rect(origin, gfx::Size(radius * 2, radius * 2));
   if (indicator_->visible()) {
@@ -209,14 +232,23 @@ void SpeechView::OnSpeechResult(const base::string16& result,
 void SpeechView::OnSpeechRecognitionStateChanged(
     SpeechRecognitionState new_state) {
   int resource_id = IDR_APP_LIST_SPEECH_MIC_OFF;
-  if (new_state == SPEECH_RECOGNITION_ON)
+  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));
+  mic_button_->SetImage(views::Button::STATE_NORMAL,
+                        bundle.GetImageSkiaNamed(resource_id));
 }
 
 }  // namespace app_list