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( | |
| 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 life_time_observer_ = life_time_observer; | |
|
tommi (sloooow) - chröme
2014/03/27 14:44:03
nit: First DCHECK(!life_time_observer_)?
or if lif
Tommy Widenflycht
2014/03/31 10:45:54
Done.
| |
| 135 } | |
| 136 | |
| 137 void SpeechRecognitionBubbleView::OnWidgetDestroyed(views::Widget* widget) { | |
| 138 BubbleDelegateView::OnWidgetDestroyed(widget); | |
| 139 if (life_time_observer_) { | |
|
tommi (sloooow) - chröme
2014/03/27 14:44:03
nit: no {}
Tommy Widenflycht
2014/03/31 10:45:54
Done.
| |
| 140 life_time_observer_->BubbleViewGoingAway(); | |
| 141 } | |
| 142 } | |
| 143 | |
| 122 void SpeechRecognitionBubbleView::OnWidgetActivationChanged( | 144 void SpeechRecognitionBubbleView::OnWidgetActivationChanged( |
| 123 views::Widget* widget, bool active) { | 145 views::Widget* widget, bool active) { |
| 124 if (widget == GetWidget() && !active && notify_delegate_on_activation_change_) | 146 if (widget == GetWidget() && !active && notify_delegate_on_activation_change_) |
| 125 delegate_->InfoBubbleFocusChanged(); | 147 delegate_->InfoBubbleFocusChanged(); |
| 126 BubbleDelegateView::OnWidgetActivationChanged(widget, active); | 148 BubbleDelegateView::OnWidgetActivationChanged(widget, active); |
| 127 } | 149 } |
| 128 | 150 |
| 129 gfx::Rect SpeechRecognitionBubbleView::GetAnchorRect() { | 151 gfx::Rect SpeechRecognitionBubbleView::GetAnchorRect() { |
| 130 gfx::Rect container_rect; | 152 gfx::Rect container_rect; |
| 131 web_contents_->GetView()->GetContainerBounds(&container_rect); | 153 web_contents_->GetView()->GetContainerBounds(&container_rect); |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 320 if (cancel_->visible()) { | 342 if (cancel_->visible()) { |
| 321 control_height = cancel_->GetPreferredSize().height(); | 343 control_height = cancel_->GetPreferredSize().height(); |
| 322 int width = cancel_->GetPreferredSize().width(); | 344 int width = cancel_->GetPreferredSize().width(); |
| 323 cancel_->SetBounds(x + (available_width - width) / 2, y, width, | 345 cancel_->SetBounds(x + (available_width - width) / 2, y, width, |
| 324 control_height); | 346 control_height); |
| 325 } | 347 } |
| 326 } | 348 } |
| 327 } | 349 } |
| 328 | 350 |
| 329 // Implementation of SpeechRecognitionBubble. | 351 // Implementation of SpeechRecognitionBubble. |
| 330 class SpeechRecognitionBubbleImpl : public SpeechRecognitionBubbleBase { | 352 class SpeechRecognitionBubbleImpl |
| 353 : public SpeechRecognitionBubbleBase, | |
| 354 public SpeechRecognitionBubbleViewLifeTimeObserver { | |
| 331 public: | 355 public: |
| 332 SpeechRecognitionBubbleImpl(int render_process_id, int render_view_id, | 356 SpeechRecognitionBubbleImpl(int render_process_id, int render_view_id, |
| 333 Delegate* delegate, | 357 Delegate* delegate, |
| 334 const gfx::Rect& element_rect); | 358 const gfx::Rect& element_rect); |
| 335 virtual ~SpeechRecognitionBubbleImpl(); | 359 virtual ~SpeechRecognitionBubbleImpl(); |
| 336 | 360 |
| 337 // SpeechRecognitionBubble methods. | 361 // SpeechRecognitionBubble methods. |
| 338 virtual void Show() OVERRIDE; | 362 virtual void Show() OVERRIDE; |
| 339 virtual void Hide() OVERRIDE; | 363 virtual void Hide() OVERRIDE; |
| 340 | 364 |
| 341 // SpeechRecognitionBubbleBase methods. | 365 // SpeechRecognitionBubbleBase methods. |
| 342 virtual void UpdateLayout() OVERRIDE; | 366 virtual void UpdateLayout() OVERRIDE; |
| 343 virtual void UpdateImage() OVERRIDE; | 367 virtual void UpdateImage() OVERRIDE; |
| 344 | 368 |
| 369 // SpeechRecognitionBubbleViewLifeTimeObserver methods. | |
| 370 virtual void BubbleViewGoingAway() OVERRIDE; | |
| 371 | |
| 345 private: | 372 private: |
| 346 Delegate* delegate_; | 373 Delegate* delegate_; |
| 347 SpeechRecognitionBubbleView* bubble_; | 374 SpeechRecognitionBubbleView* bubble_; |
| 348 gfx::Rect element_rect_; | 375 gfx::Rect element_rect_; |
| 349 | 376 |
| 350 DISALLOW_COPY_AND_ASSIGN(SpeechRecognitionBubbleImpl); | 377 DISALLOW_COPY_AND_ASSIGN(SpeechRecognitionBubbleImpl); |
| 351 }; | 378 }; |
| 352 | 379 |
| 353 SpeechRecognitionBubbleImpl::SpeechRecognitionBubbleImpl( | 380 SpeechRecognitionBubbleImpl::SpeechRecognitionBubbleImpl( |
| 354 int render_process_id, int render_view_id, Delegate* delegate, | 381 int render_process_id, int render_view_id, Delegate* delegate, |
| 355 const gfx::Rect& element_rect) | 382 const gfx::Rect& element_rect) |
| 356 : SpeechRecognitionBubbleBase(render_process_id, render_view_id), | 383 : SpeechRecognitionBubbleBase(render_process_id, render_view_id), |
| 357 delegate_(delegate), | 384 delegate_(delegate), |
| 358 bubble_(NULL), | 385 bubble_(NULL), |
| 359 element_rect_(element_rect) { | 386 element_rect_(element_rect) { |
| 360 } | 387 } |
| 361 | 388 |
| 362 SpeechRecognitionBubbleImpl::~SpeechRecognitionBubbleImpl() { | 389 SpeechRecognitionBubbleImpl::~SpeechRecognitionBubbleImpl() { |
| 363 if (bubble_) { | 390 if (bubble_) { |
| 391 bubble_->SetLifeTimeObserver(NULL); | |
| 364 bubble_->set_notify_delegate_on_activation_change(false); | 392 bubble_->set_notify_delegate_on_activation_change(false); |
| 365 bubble_->GetWidget()->Close(); | 393 bubble_->GetWidget()->Close(); |
| 366 } | 394 } |
| 367 } | 395 } |
| 368 | 396 |
| 397 void SpeechRecognitionBubbleImpl::BubbleViewGoingAway() { | |
| 398 bubble_ = NULL; | |
| 399 } | |
| 400 | |
| 369 void SpeechRecognitionBubbleImpl::Show() { | 401 void SpeechRecognitionBubbleImpl::Show() { |
| 370 WebContents* web_contents = GetWebContents(); | 402 WebContents* web_contents = GetWebContents(); |
| 371 if (!web_contents) | 403 if (!web_contents) |
| 372 return; | 404 return; |
| 373 | 405 |
| 374 if (!bubble_) { | 406 if (!bubble_) { |
| 375 views::View* icon = NULL; | 407 views::View* icon = NULL; |
| 376 | 408 |
| 377 // Anchor to the location bar, in case |element_rect| is offscreen. | 409 // Anchor to the location bar, in case |element_rect| is offscreen. |
| 378 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); | 410 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); |
| 379 if (browser) { | 411 if (browser) { |
| 380 BrowserView* browser_view = | 412 BrowserView* browser_view = |
| 381 BrowserView::GetBrowserViewForBrowser(browser); | 413 BrowserView::GetBrowserViewForBrowser(browser); |
| 382 icon = browser_view->GetLocationBarView() ? | 414 icon = browser_view->GetLocationBarView() ? |
| 383 browser_view->GetLocationBarView()->GetLocationBarAnchor() : NULL; | 415 browser_view->GetLocationBarView()->GetLocationBarAnchor() : NULL; |
| 384 } | 416 } |
| 385 | 417 |
| 386 bubble_ = new SpeechRecognitionBubbleView(delegate_, icon, element_rect_, | 418 bubble_ = new SpeechRecognitionBubbleView(delegate_, icon, element_rect_, |
| 387 web_contents); | 419 web_contents); |
| 420 bubble_->SetLifeTimeObserver(this); | |
| 388 | 421 |
| 389 if (!icon) { | 422 if (!icon) { |
| 390 // We dont't have an icon to attach to. Manually specify the web contents | 423 // We dont't have an icon to attach to. Manually specify the web contents |
| 391 // window as the parent. | 424 // window as the parent. |
| 392 bubble_->set_parent_window( | 425 bubble_->set_parent_window( |
| 393 web_contents->GetView()->GetTopLevelNativeWindow()); | 426 web_contents->GetView()->GetTopLevelNativeWindow()); |
| 394 } | 427 } |
| 395 | 428 |
| 396 views::BubbleDelegateView::CreateBubble(bubble_); | 429 views::BubbleDelegateView::CreateBubble(bubble_); |
| 397 UpdateLayout(); | 430 UpdateLayout(); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 417 } // namespace | 450 } // namespace |
| 418 | 451 |
| 419 SpeechRecognitionBubble* SpeechRecognitionBubble::CreateNativeBubble( | 452 SpeechRecognitionBubble* SpeechRecognitionBubble::CreateNativeBubble( |
| 420 int render_process_id, | 453 int render_process_id, |
| 421 int render_view_id, | 454 int render_view_id, |
| 422 SpeechRecognitionBubble::Delegate* delegate, | 455 SpeechRecognitionBubble::Delegate* delegate, |
| 423 const gfx::Rect& element_rect) { | 456 const gfx::Rect& element_rect) { |
| 424 return new SpeechRecognitionBubbleImpl(render_process_id, render_view_id, | 457 return new SpeechRecognitionBubbleImpl(render_process_id, render_view_id, |
| 425 delegate, element_rect); | 458 delegate, element_rect); |
| 426 } | 459 } |
| OLD | NEW |