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 { | |
|
msw
2014/03/31 16:53:30
Use the pre-existing WidgetObserver, which already
Tommy Widenflycht
2014/04/01 11:29:03
Done.
| |
| 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( | |
| 54 SpeechRecognitionBubbleViewLifeTimeObserver* life_time_observer); | |
| 55 | |
| 48 void UpdateLayout(SpeechRecognitionBubbleBase::DisplayMode mode, | 56 void UpdateLayout(SpeechRecognitionBubbleBase::DisplayMode mode, |
| 49 const base::string16& message_text, | 57 const base::string16& message_text, |
| 50 const gfx::ImageSkia& image); | 58 const gfx::ImageSkia& image); |
| 51 void SetImage(const gfx::ImageSkia& image); | 59 void SetImage(const gfx::ImageSkia& image); |
| 52 | 60 |
| 53 // views::BubbleDelegateView methods. | 61 // views::BubbleDelegateView methods. |
| 54 virtual void OnWidgetActivationChanged(views::Widget* widget, | 62 virtual void OnWidgetActivationChanged(views::Widget* widget, |
| 55 bool active) OVERRIDE; | 63 bool active) OVERRIDE; |
| 64 virtual void OnWidgetDestroyed(views::Widget* widget) OVERRIDE; | |
| 56 virtual gfx::Rect GetAnchorRect() OVERRIDE; | 65 virtual gfx::Rect GetAnchorRect() OVERRIDE; |
| 57 virtual void Init() OVERRIDE; | 66 virtual void Init() OVERRIDE; |
| 58 | 67 |
| 59 // views::ButtonListener methods. | 68 // views::ButtonListener methods. |
| 60 virtual void ButtonPressed(views::Button* source, | 69 virtual void ButtonPressed(views::Button* source, |
| 61 const ui::Event& event) OVERRIDE; | 70 const ui::Event& event) OVERRIDE; |
| 62 | 71 |
| 63 // views::LinkListener methods. | 72 // views::LinkListener methods. |
| 64 virtual void LinkClicked(views::Link* source, int event_flags) OVERRIDE; | 73 virtual void LinkClicked(views::Link* source, int event_flags) OVERRIDE; |
| 65 | 74 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 78 WebContents* web_contents_; | 87 WebContents* web_contents_; |
| 79 bool notify_delegate_on_activation_change_; | 88 bool notify_delegate_on_activation_change_; |
| 80 views::ImageView* icon_; | 89 views::ImageView* icon_; |
| 81 views::Label* heading_; | 90 views::Label* heading_; |
| 82 views::Label* message_; | 91 views::Label* message_; |
| 83 views::LabelButton* try_again_; | 92 views::LabelButton* try_again_; |
| 84 views::LabelButton* cancel_; | 93 views::LabelButton* cancel_; |
| 85 views::Link* mic_settings_; | 94 views::Link* mic_settings_; |
| 86 SpeechRecognitionBubbleBase::DisplayMode display_mode_; | 95 SpeechRecognitionBubbleBase::DisplayMode display_mode_; |
| 87 const int kIconLayoutMinWidth; | 96 const int kIconLayoutMinWidth; |
| 97 SpeechRecognitionBubbleViewLifeTimeObserver* life_time_observer_; | |
| 88 | 98 |
| 89 DISALLOW_COPY_AND_ASSIGN(SpeechRecognitionBubbleView); | 99 DISALLOW_COPY_AND_ASSIGN(SpeechRecognitionBubbleView); |
| 90 }; | 100 }; |
| 91 | 101 |
| 92 SpeechRecognitionBubbleView::SpeechRecognitionBubbleView( | 102 SpeechRecognitionBubbleView::SpeechRecognitionBubbleView( |
| 93 SpeechRecognitionBubbleDelegate* delegate, | 103 SpeechRecognitionBubbleDelegate* delegate, |
| 94 views::View* anchor_view, | 104 views::View* anchor_view, |
| 95 const gfx::Rect& element_rect, | 105 const gfx::Rect& element_rect, |
| 96 WebContents* web_contents) | 106 WebContents* web_contents) |
| 97 : BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_LEFT), | 107 : BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_LEFT), |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 112 // on deactivation will cause unexpected behavior. | 122 // on deactivation will cause unexpected behavior. |
| 113 set_close_on_deactivate(false); | 123 set_close_on_deactivate(false); |
| 114 // Prevent default behavior of bubble closure on escape key and handle | 124 // Prevent default behavior of bubble closure on escape key and handle |
| 115 // it in the AcceleratorPressed() to avoid an unexpected behavior. | 125 // it in the AcceleratorPressed() to avoid an unexpected behavior. |
| 116 set_close_on_esc(false); | 126 set_close_on_esc(false); |
| 117 | 127 |
| 118 // Update the bubble's bounds when the window's bounds changes. | 128 // Update the bubble's bounds when the window's bounds changes. |
| 119 set_move_with_anchor(true); | 129 set_move_with_anchor(true); |
| 120 } | 130 } |
| 121 | 131 |
| 132 void SpeechRecognitionBubbleView::SetLifeTimeObserver( | |
| 133 SpeechRecognitionBubbleViewLifeTimeObserver* life_time_observer) { | |
| 134 DCHECK((!life_time_observer_ && life_time_observer) || | |
| 135 (life_time_observer_ && !life_time_observer)); | |
| 136 life_time_observer_ = life_time_observer; | |
| 137 } | |
| 138 | |
| 139 void SpeechRecognitionBubbleView::OnWidgetDestroyed(views::Widget* widget) { | |
| 140 BubbleDelegateView::OnWidgetDestroyed(widget); | |
| 141 if (life_time_observer_) | |
| 142 life_time_observer_->BubbleViewGoingAway(); | |
| 143 } | |
| 144 | |
| 122 void SpeechRecognitionBubbleView::OnWidgetActivationChanged( | 145 void SpeechRecognitionBubbleView::OnWidgetActivationChanged( |
| 123 views::Widget* widget, bool active) { | 146 views::Widget* widget, bool active) { |
| 124 if (widget == GetWidget() && !active && notify_delegate_on_activation_change_) | 147 if (widget == GetWidget() && !active && notify_delegate_on_activation_change_) |
| 125 delegate_->InfoBubbleFocusChanged(); | 148 delegate_->InfoBubbleFocusChanged(); |
| 126 BubbleDelegateView::OnWidgetActivationChanged(widget, active); | 149 BubbleDelegateView::OnWidgetActivationChanged(widget, active); |
| 127 } | 150 } |
| 128 | 151 |
| 129 gfx::Rect SpeechRecognitionBubbleView::GetAnchorRect() { | 152 gfx::Rect SpeechRecognitionBubbleView::GetAnchorRect() { |
| 130 gfx::Rect container_rect; | 153 gfx::Rect container_rect; |
| 131 web_contents_->GetView()->GetContainerBounds(&container_rect); | 154 web_contents_->GetView()->GetContainerBounds(&container_rect); |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 320 if (cancel_->visible()) { | 343 if (cancel_->visible()) { |
| 321 control_height = cancel_->GetPreferredSize().height(); | 344 control_height = cancel_->GetPreferredSize().height(); |
| 322 int width = cancel_->GetPreferredSize().width(); | 345 int width = cancel_->GetPreferredSize().width(); |
| 323 cancel_->SetBounds(x + (available_width - width) / 2, y, width, | 346 cancel_->SetBounds(x + (available_width - width) / 2, y, width, |
| 324 control_height); | 347 control_height); |
| 325 } | 348 } |
| 326 } | 349 } |
| 327 } | 350 } |
| 328 | 351 |
| 329 // Implementation of SpeechRecognitionBubble. | 352 // Implementation of SpeechRecognitionBubble. |
| 330 class SpeechRecognitionBubbleImpl : public SpeechRecognitionBubbleBase { | 353 class SpeechRecognitionBubbleImpl |
| 354 : public SpeechRecognitionBubbleBase, | |
| 355 public SpeechRecognitionBubbleViewLifeTimeObserver { | |
| 331 public: | 356 public: |
| 332 SpeechRecognitionBubbleImpl(int render_process_id, int render_view_id, | 357 SpeechRecognitionBubbleImpl(int render_process_id, int render_view_id, |
| 333 Delegate* delegate, | 358 Delegate* delegate, |
| 334 const gfx::Rect& element_rect); | 359 const gfx::Rect& element_rect); |
| 335 virtual ~SpeechRecognitionBubbleImpl(); | 360 virtual ~SpeechRecognitionBubbleImpl(); |
| 336 | 361 |
| 337 // SpeechRecognitionBubble methods. | 362 // SpeechRecognitionBubble methods. |
| 338 virtual void Show() OVERRIDE; | 363 virtual void Show() OVERRIDE; |
| 339 virtual void Hide() OVERRIDE; | 364 virtual void Hide() OVERRIDE; |
| 340 | 365 |
| 341 // SpeechRecognitionBubbleBase methods. | 366 // SpeechRecognitionBubbleBase methods. |
| 342 virtual void UpdateLayout() OVERRIDE; | 367 virtual void UpdateLayout() OVERRIDE; |
| 343 virtual void UpdateImage() OVERRIDE; | 368 virtual void UpdateImage() OVERRIDE; |
| 344 | 369 |
| 370 // SpeechRecognitionBubbleViewLifeTimeObserver methods. | |
| 371 virtual void BubbleViewGoingAway() OVERRIDE; | |
| 372 | |
| 345 private: | 373 private: |
| 346 Delegate* delegate_; | 374 Delegate* delegate_; |
| 347 SpeechRecognitionBubbleView* bubble_; | 375 SpeechRecognitionBubbleView* bubble_; |
| 348 gfx::Rect element_rect_; | 376 gfx::Rect element_rect_; |
| 349 | 377 |
| 350 DISALLOW_COPY_AND_ASSIGN(SpeechRecognitionBubbleImpl); | 378 DISALLOW_COPY_AND_ASSIGN(SpeechRecognitionBubbleImpl); |
| 351 }; | 379 }; |
| 352 | 380 |
| 353 SpeechRecognitionBubbleImpl::SpeechRecognitionBubbleImpl( | 381 SpeechRecognitionBubbleImpl::SpeechRecognitionBubbleImpl( |
| 354 int render_process_id, int render_view_id, Delegate* delegate, | 382 int render_process_id, int render_view_id, Delegate* delegate, |
| 355 const gfx::Rect& element_rect) | 383 const gfx::Rect& element_rect) |
| 356 : SpeechRecognitionBubbleBase(render_process_id, render_view_id), | 384 : SpeechRecognitionBubbleBase(render_process_id, render_view_id), |
| 357 delegate_(delegate), | 385 delegate_(delegate), |
| 358 bubble_(NULL), | 386 bubble_(NULL), |
| 359 element_rect_(element_rect) { | 387 element_rect_(element_rect) { |
| 360 } | 388 } |
| 361 | 389 |
| 362 SpeechRecognitionBubbleImpl::~SpeechRecognitionBubbleImpl() { | 390 SpeechRecognitionBubbleImpl::~SpeechRecognitionBubbleImpl() { |
| 363 if (bubble_) { | 391 if (bubble_) { |
| 392 bubble_->SetLifeTimeObserver(NULL); | |
| 364 bubble_->set_notify_delegate_on_activation_change(false); | 393 bubble_->set_notify_delegate_on_activation_change(false); |
| 365 bubble_->GetWidget()->Close(); | 394 bubble_->GetWidget()->Close(); |
| 366 } | 395 } |
| 367 } | 396 } |
| 368 | 397 |
| 398 void SpeechRecognitionBubbleImpl::BubbleViewGoingAway() { | |
| 399 bubble_ = NULL; | |
| 400 } | |
| 401 | |
| 369 void SpeechRecognitionBubbleImpl::Show() { | 402 void SpeechRecognitionBubbleImpl::Show() { |
| 370 WebContents* web_contents = GetWebContents(); | 403 WebContents* web_contents = GetWebContents(); |
| 371 if (!web_contents) | 404 if (!web_contents) |
| 372 return; | 405 return; |
| 373 | 406 |
| 374 if (!bubble_) { | 407 if (!bubble_) { |
| 375 views::View* icon = NULL; | 408 views::View* icon = NULL; |
| 376 | 409 |
| 377 // Anchor to the location bar, in case |element_rect| is offscreen. | 410 // Anchor to the location bar, in case |element_rect| is offscreen. |
| 378 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); | 411 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); |
| 379 if (browser) { | 412 if (browser) { |
| 380 BrowserView* browser_view = | 413 BrowserView* browser_view = |
| 381 BrowserView::GetBrowserViewForBrowser(browser); | 414 BrowserView::GetBrowserViewForBrowser(browser); |
| 382 icon = browser_view->GetLocationBarView() ? | 415 icon = browser_view->GetLocationBarView() ? |
| 383 browser_view->GetLocationBarView()->GetLocationBarAnchor() : NULL; | 416 browser_view->GetLocationBarView()->GetLocationBarAnchor() : NULL; |
| 384 } | 417 } |
| 385 | 418 |
| 386 bubble_ = new SpeechRecognitionBubbleView(delegate_, icon, element_rect_, | 419 bubble_ = new SpeechRecognitionBubbleView(delegate_, icon, element_rect_, |
| 387 web_contents); | 420 web_contents); |
| 421 bubble_->SetLifeTimeObserver(this); | |
| 388 | 422 |
| 389 if (!icon) { | 423 if (!icon) { |
| 390 // We dont't have an icon to attach to. Manually specify the web contents | 424 // We dont't have an icon to attach to. Manually specify the web contents |
| 391 // window as the parent. | 425 // window as the parent. |
| 392 bubble_->set_parent_window( | 426 bubble_->set_parent_window( |
| 393 web_contents->GetView()->GetTopLevelNativeWindow()); | 427 web_contents->GetView()->GetTopLevelNativeWindow()); |
| 394 } | 428 } |
| 395 | 429 |
| 396 views::BubbleDelegateView::CreateBubble(bubble_); | 430 views::BubbleDelegateView::CreateBubble(bubble_); |
| 397 UpdateLayout(); | 431 UpdateLayout(); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 417 } // namespace | 451 } // namespace |
| 418 | 452 |
| 419 SpeechRecognitionBubble* SpeechRecognitionBubble::CreateNativeBubble( | 453 SpeechRecognitionBubble* SpeechRecognitionBubble::CreateNativeBubble( |
| 420 int render_process_id, | 454 int render_process_id, |
| 421 int render_view_id, | 455 int render_view_id, |
| 422 SpeechRecognitionBubble::Delegate* delegate, | 456 SpeechRecognitionBubble::Delegate* delegate, |
| 423 const gfx::Rect& element_rect) { | 457 const gfx::Rect& element_rect) { |
| 424 return new SpeechRecognitionBubbleImpl(render_process_id, render_view_id, | 458 return new SpeechRecognitionBubbleImpl(render_process_id, render_view_id, |
| 425 delegate, element_rect); | 459 delegate, element_rect); |
| 426 } | 460 } |
| OLD | NEW |