Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/speech/speech_recognition_bubble.h" | 5 #include "chrome/browser/speech/speech_recognition_bubble.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 #include "ui/views/controls/image_view.h" | 24 #include "ui/views/controls/image_view.h" |
| 25 #include "ui/views/controls/label.h" | 25 #include "ui/views/controls/label.h" |
| 26 #include "ui/views/controls/link.h" | 26 #include "ui/views/controls/link.h" |
| 27 #include "ui/views/controls/link_listener.h" | 27 #include "ui/views/controls/link_listener.h" |
| 28 #include "ui/views/layout/layout_constants.h" | 28 #include "ui/views/layout/layout_constants.h" |
| 29 | 29 |
| 30 using content::WebContents; | 30 using content::WebContents; |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 class SpeechRecognitionBubbleViewLifeTimeObserver { | |
| 35 public: | |
| 36 virtual void BubbleViewGoingAway() = 0; | |
| 37 }; | |
| 38 | |
| 34 const int kBubbleHorizMargin = 6; | 39 const int kBubbleHorizMargin = 6; |
| 35 const int kBubbleVertMargin = 4; | 40 const int kBubbleVertMargin = 4; |
| 36 const int kBubbleHeadingVertMargin = 6; | 41 const int kBubbleHeadingVertMargin = 6; |
| 37 | 42 |
| 38 // This is the SpeechRecognitionBubble content and views bubble delegate. | 43 // This is the SpeechRecognitionBubble content and views bubble delegate. |
| 39 class SpeechRecognitionBubbleView : public views::BubbleDelegateView, | 44 class SpeechRecognitionBubbleView : public views::BubbleDelegateView, |
| 40 public views::ButtonListener, | 45 public views::ButtonListener, |
| 41 public views::LinkListener { | 46 public views::LinkListener { |
| 42 public: | 47 public: |
| 43 SpeechRecognitionBubbleView(SpeechRecognitionBubbleDelegate* delegate, | 48 SpeechRecognitionBubbleView(SpeechRecognitionBubbleDelegate* delegate, |
| 44 views::View* anchor_view, | 49 views::View* anchor_view, |
| 45 const gfx::Rect& element_rect, | 50 const gfx::Rect& element_rect, |
| 46 WebContents* web_contents); | 51 WebContents* web_contents); |
| 47 | 52 |
| 53 void SetLifeTImeObserver( | |
|
tommi (sloooow) - chröme
2014/03/26 17:01:43
SetLifeTimeObserver
(lowercase i)
Tommy Widenflycht
2014/03/27 12:50:44
Done.
| |
| 54 SpeechRecognitionBubbleViewLifeTimeObserver* life_time_observer) { | |
| 55 life_time_observer_ = life_time_observer; | |
|
tommi (sloooow) - chröme
2014/03/26 17:01:43
fix indent
Tommy Widenflycht
2014/03/27 12:50:44
Done.
| |
| 56 } | |
| 57 | |
| 48 void UpdateLayout(SpeechRecognitionBubbleBase::DisplayMode mode, | 58 void UpdateLayout(SpeechRecognitionBubbleBase::DisplayMode mode, |
| 49 const base::string16& message_text, | 59 const base::string16& message_text, |
| 50 const gfx::ImageSkia& image); | 60 const gfx::ImageSkia& image); |
| 51 void SetImage(const gfx::ImageSkia& image); | 61 void SetImage(const gfx::ImageSkia& image); |
| 52 | 62 |
| 53 // views::BubbleDelegateView methods. | 63 // views::BubbleDelegateView methods. |
| 54 virtual void OnWidgetActivationChanged(views::Widget* widget, | 64 virtual void OnWidgetActivationChanged(views::Widget* widget, |
| 55 bool active) OVERRIDE; | 65 bool active) OVERRIDE; |
| 66 virtual void OnWidgetDestroyed(views::Widget* widget) OVERRIDE; | |
| 56 virtual gfx::Rect GetAnchorRect() OVERRIDE; | 67 virtual gfx::Rect GetAnchorRect() OVERRIDE; |
| 57 virtual void Init() OVERRIDE; | 68 virtual void Init() OVERRIDE; |
| 58 | 69 |
| 59 // views::ButtonListener methods. | 70 // views::ButtonListener methods. |
| 60 virtual void ButtonPressed(views::Button* source, | 71 virtual void ButtonPressed(views::Button* source, |
| 61 const ui::Event& event) OVERRIDE; | 72 const ui::Event& event) OVERRIDE; |
| 62 | 73 |
| 63 // views::LinkListener methods. | 74 // views::LinkListener methods. |
| 64 virtual void LinkClicked(views::Link* source, int event_flags) OVERRIDE; | 75 virtual void LinkClicked(views::Link* source, int event_flags) OVERRIDE; |
| 65 | 76 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 78 WebContents* web_contents_; | 89 WebContents* web_contents_; |
| 79 bool notify_delegate_on_activation_change_; | 90 bool notify_delegate_on_activation_change_; |
| 80 views::ImageView* icon_; | 91 views::ImageView* icon_; |
| 81 views::Label* heading_; | 92 views::Label* heading_; |
| 82 views::Label* message_; | 93 views::Label* message_; |
| 83 views::LabelButton* try_again_; | 94 views::LabelButton* try_again_; |
| 84 views::LabelButton* cancel_; | 95 views::LabelButton* cancel_; |
| 85 views::Link* mic_settings_; | 96 views::Link* mic_settings_; |
| 86 SpeechRecognitionBubbleBase::DisplayMode display_mode_; | 97 SpeechRecognitionBubbleBase::DisplayMode display_mode_; |
| 87 const int kIconLayoutMinWidth; | 98 const int kIconLayoutMinWidth; |
| 99 SpeechRecognitionBubbleViewLifeTimeObserver* life_time_observer_; | |
| 88 | 100 |
| 89 DISALLOW_COPY_AND_ASSIGN(SpeechRecognitionBubbleView); | 101 DISALLOW_COPY_AND_ASSIGN(SpeechRecognitionBubbleView); |
| 90 }; | 102 }; |
| 91 | 103 |
| 92 SpeechRecognitionBubbleView::SpeechRecognitionBubbleView( | 104 SpeechRecognitionBubbleView::SpeechRecognitionBubbleView( |
| 93 SpeechRecognitionBubbleDelegate* delegate, | 105 SpeechRecognitionBubbleDelegate* delegate, |
| 94 views::View* anchor_view, | 106 views::View* anchor_view, |
| 95 const gfx::Rect& element_rect, | 107 const gfx::Rect& element_rect, |
| 96 WebContents* web_contents) | 108 WebContents* web_contents) |
| 97 : BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_LEFT), | 109 : BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_LEFT), |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 112 // on deactivation will cause unexpected behavior. | 124 // on deactivation will cause unexpected behavior. |
| 113 set_close_on_deactivate(false); | 125 set_close_on_deactivate(false); |
| 114 // Prevent default behavior of bubble closure on escape key and handle | 126 // Prevent default behavior of bubble closure on escape key and handle |
| 115 // it in the AcceleratorPressed() to avoid an unexpected behavior. | 127 // it in the AcceleratorPressed() to avoid an unexpected behavior. |
| 116 set_close_on_esc(false); | 128 set_close_on_esc(false); |
| 117 | 129 |
| 118 // Update the bubble's bounds when the window's bounds changes. | 130 // Update the bubble's bounds when the window's bounds changes. |
| 119 set_move_with_anchor(true); | 131 set_move_with_anchor(true); |
| 120 } | 132 } |
| 121 | 133 |
| 134 void SpeechRecognitionBubbleView::OnWidgetDestroyed(views::Widget* widget) { | |
| 135 BubbleDelegateView::OnWidgetDestroyed(widget); | |
|
tommi (sloooow) - chröme
2014/03/26 17:01:43
fix indent
Tommy Widenflycht
2014/03/27 12:50:44
Done.
| |
| 136 if (life_time_observer_) | |
| 137 life_time_observer_->BubbleViewGoingAway(); | |
| 138 } | |
| 139 | |
| 122 void SpeechRecognitionBubbleView::OnWidgetActivationChanged( | 140 void SpeechRecognitionBubbleView::OnWidgetActivationChanged( |
| 123 views::Widget* widget, bool active) { | 141 views::Widget* widget, bool active) { |
| 124 if (widget == GetWidget() && !active && notify_delegate_on_activation_change_) | 142 if (widget == GetWidget() && !active && notify_delegate_on_activation_change_) |
| 125 delegate_->InfoBubbleFocusChanged(); | 143 delegate_->InfoBubbleFocusChanged(); |
| 126 BubbleDelegateView::OnWidgetActivationChanged(widget, active); | 144 BubbleDelegateView::OnWidgetActivationChanged(widget, active); |
| 127 } | 145 } |
| 128 | 146 |
| 129 gfx::Rect SpeechRecognitionBubbleView::GetAnchorRect() { | 147 gfx::Rect SpeechRecognitionBubbleView::GetAnchorRect() { |
| 130 gfx::Rect container_rect; | 148 gfx::Rect container_rect; |
| 131 web_contents_->GetView()->GetContainerBounds(&container_rect); | 149 web_contents_->GetView()->GetContainerBounds(&container_rect); |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 320 if (cancel_->visible()) { | 338 if (cancel_->visible()) { |
| 321 control_height = cancel_->GetPreferredSize().height(); | 339 control_height = cancel_->GetPreferredSize().height(); |
| 322 int width = cancel_->GetPreferredSize().width(); | 340 int width = cancel_->GetPreferredSize().width(); |
| 323 cancel_->SetBounds(x + (available_width - width) / 2, y, width, | 341 cancel_->SetBounds(x + (available_width - width) / 2, y, width, |
| 324 control_height); | 342 control_height); |
| 325 } | 343 } |
| 326 } | 344 } |
| 327 } | 345 } |
| 328 | 346 |
| 329 // Implementation of SpeechRecognitionBubble. | 347 // Implementation of SpeechRecognitionBubble. |
| 330 class SpeechRecognitionBubbleImpl : public SpeechRecognitionBubbleBase { | 348 class SpeechRecognitionBubbleImpl |
| 349 : public SpeechRecognitionBubbleBase, | |
| 350 public SpeechRecognitionBubbleViewLifeTimeObserver { | |
| 331 public: | 351 public: |
| 332 SpeechRecognitionBubbleImpl(int render_process_id, int render_view_id, | 352 SpeechRecognitionBubbleImpl(int render_process_id, int render_view_id, |
| 333 Delegate* delegate, | 353 Delegate* delegate, |
| 334 const gfx::Rect& element_rect); | 354 const gfx::Rect& element_rect); |
| 335 virtual ~SpeechRecognitionBubbleImpl(); | 355 virtual ~SpeechRecognitionBubbleImpl(); |
| 336 | 356 |
| 337 // SpeechRecognitionBubble methods. | 357 // SpeechRecognitionBubble methods. |
| 338 virtual void Show() OVERRIDE; | 358 virtual void Show() OVERRIDE; |
| 339 virtual void Hide() OVERRIDE; | 359 virtual void Hide() OVERRIDE; |
| 340 | 360 |
| 341 // SpeechRecognitionBubbleBase methods. | 361 // SpeechRecognitionBubbleBase methods. |
| 342 virtual void UpdateLayout() OVERRIDE; | 362 virtual void UpdateLayout() OVERRIDE; |
| 343 virtual void UpdateImage() OVERRIDE; | 363 virtual void UpdateImage() OVERRIDE; |
| 344 | 364 |
| 365 // SpeechRecognitionBubbleViewLifeTimeObserver methods. | |
| 366 virtual void BubbleViewGoingAway() OVERRIDE; | |
| 367 | |
| 345 private: | 368 private: |
| 346 Delegate* delegate_; | 369 Delegate* delegate_; |
| 347 SpeechRecognitionBubbleView* bubble_; | 370 SpeechRecognitionBubbleView* bubble_; |
| 348 gfx::Rect element_rect_; | 371 gfx::Rect element_rect_; |
| 349 | 372 |
| 350 DISALLOW_COPY_AND_ASSIGN(SpeechRecognitionBubbleImpl); | 373 DISALLOW_COPY_AND_ASSIGN(SpeechRecognitionBubbleImpl); |
| 351 }; | 374 }; |
| 352 | 375 |
| 353 SpeechRecognitionBubbleImpl::SpeechRecognitionBubbleImpl( | 376 SpeechRecognitionBubbleImpl::SpeechRecognitionBubbleImpl( |
| 354 int render_process_id, int render_view_id, Delegate* delegate, | 377 int render_process_id, int render_view_id, Delegate* delegate, |
| 355 const gfx::Rect& element_rect) | 378 const gfx::Rect& element_rect) |
| 356 : SpeechRecognitionBubbleBase(render_process_id, render_view_id), | 379 : SpeechRecognitionBubbleBase(render_process_id, render_view_id), |
| 357 delegate_(delegate), | 380 delegate_(delegate), |
| 358 bubble_(NULL), | 381 bubble_(NULL), |
| 359 element_rect_(element_rect) { | 382 element_rect_(element_rect) { |
| 360 } | 383 } |
| 361 | 384 |
| 362 SpeechRecognitionBubbleImpl::~SpeechRecognitionBubbleImpl() { | 385 SpeechRecognitionBubbleImpl::~SpeechRecognitionBubbleImpl() { |
| 363 if (bubble_) { | 386 if (bubble_) { |
| 387 bubble_->SetLifeTImeObserver(NULL); | |
| 364 bubble_->set_notify_delegate_on_activation_change(false); | 388 bubble_->set_notify_delegate_on_activation_change(false); |
| 365 bubble_->GetWidget()->Close(); | 389 bubble_->GetWidget()->Close(); |
| 366 } | 390 } |
| 367 } | 391 } |
| 368 | 392 |
| 393 void SpeechRecognitionBubbleImpl::BubbleViewGoingAway() { | |
| 394 bubble_ = NULL; | |
| 395 } | |
| 396 | |
| 369 void SpeechRecognitionBubbleImpl::Show() { | 397 void SpeechRecognitionBubbleImpl::Show() { |
| 370 WebContents* web_contents = GetWebContents(); | 398 WebContents* web_contents = GetWebContents(); |
| 371 if (!web_contents) | 399 if (!web_contents) |
| 372 return; | 400 return; |
| 373 | 401 |
| 374 if (!bubble_) { | 402 if (!bubble_) { |
| 375 views::View* icon = NULL; | 403 views::View* icon = NULL; |
| 376 | 404 |
| 377 // Anchor to the location bar, in case |element_rect| is offscreen. | 405 // Anchor to the location bar, in case |element_rect| is offscreen. |
| 378 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); | 406 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); |
| 379 if (browser) { | 407 if (browser) { |
| 380 BrowserView* browser_view = | 408 BrowserView* browser_view = |
| 381 BrowserView::GetBrowserViewForBrowser(browser); | 409 BrowserView::GetBrowserViewForBrowser(browser); |
| 382 icon = browser_view->GetLocationBarView() ? | 410 icon = browser_view->GetLocationBarView() ? |
| 383 browser_view->GetLocationBarView()->GetLocationBarAnchor() : NULL; | 411 browser_view->GetLocationBarView()->GetLocationBarAnchor() : NULL; |
| 384 } | 412 } |
| 385 | 413 |
| 386 bubble_ = new SpeechRecognitionBubbleView(delegate_, icon, element_rect_, | 414 bubble_ = new SpeechRecognitionBubbleView(delegate_, icon, element_rect_, |
| 387 web_contents); | 415 web_contents); |
| 416 bubble_->SetLifeTImeObserver(this); | |
| 388 | 417 |
| 389 if (!icon) { | 418 if (!icon) { |
| 390 // We dont't have an icon to attach to. Manually specify the web contents | 419 // We dont't have an icon to attach to. Manually specify the web contents |
| 391 // window as the parent. | 420 // window as the parent. |
| 392 bubble_->set_parent_window( | 421 bubble_->set_parent_window( |
| 393 web_contents->GetView()->GetTopLevelNativeWindow()); | 422 web_contents->GetView()->GetTopLevelNativeWindow()); |
| 394 } | 423 } |
| 395 | 424 |
| 396 views::BubbleDelegateView::CreateBubble(bubble_); | 425 views::BubbleDelegateView::CreateBubble(bubble_); |
| 397 UpdateLayout(); | 426 UpdateLayout(); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 417 } // namespace | 446 } // namespace |
| 418 | 447 |
| 419 SpeechRecognitionBubble* SpeechRecognitionBubble::CreateNativeBubble( | 448 SpeechRecognitionBubble* SpeechRecognitionBubble::CreateNativeBubble( |
| 420 int render_process_id, | 449 int render_process_id, |
| 421 int render_view_id, | 450 int render_view_id, |
| 422 SpeechRecognitionBubble::Delegate* delegate, | 451 SpeechRecognitionBubble::Delegate* delegate, |
| 423 const gfx::Rect& element_rect) { | 452 const gfx::Rect& element_rect) { |
| 424 return new SpeechRecognitionBubbleImpl(render_process_id, render_view_id, | 453 return new SpeechRecognitionBubbleImpl(render_process_id, render_view_id, |
| 425 delegate, element_rect); | 454 delegate, element_rect); |
| 426 } | 455 } |
| OLD | NEW |