OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_input_bubble.h" | 5 #include "chrome/browser/speech/speech_input_bubble.h" |
6 | 6 |
7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #include "chrome/browser/browser_window.h" | 11 #include "chrome/browser/browser_window.h" |
12 #include "chrome/browser/tab_contents/tab_contents.h" | 12 #include "chrome/browser/tab_contents/tab_contents.h" |
13 #include "chrome/browser/tab_contents/tab_contents_view.h" | 13 #include "chrome/browser/tab_contents/tab_contents_view.h" |
14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
15 #include "chrome/browser/views/info_bubble.h" | 15 #include "chrome/browser/views/info_bubble.h" |
16 #include "chrome/common/notification_observer.h" | |
17 #include "chrome/common/notification_registrar.h" | |
18 #include "chrome/common/notification_source.h" | |
19 #include "chrome/common/notification_type.h" | |
20 #include "gfx/canvas.h" | 16 #include "gfx/canvas.h" |
21 #include "grit/generated_resources.h" | 17 #include "grit/generated_resources.h" |
22 #include "grit/theme_resources.h" | 18 #include "grit/theme_resources.h" |
23 #include "views/controls/button/native_button.h" | 19 #include "views/controls/button/native_button.h" |
24 #include "views/controls/image_view.h" | 20 #include "views/controls/image_view.h" |
25 #include "views/controls/label.h" | 21 #include "views/controls/label.h" |
26 #include "views/standard_layout.h" | 22 #include "views/standard_layout.h" |
27 #include "views/view.h" | 23 #include "views/view.h" |
28 | 24 |
29 namespace { | 25 namespace { |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 | 189 |
194 height = cancel_->GetPreferredSize().height(); | 190 height = cancel_->GetPreferredSize().height(); |
195 int width = cancel_->GetPreferredSize().width(); | 191 int width = cancel_->GetPreferredSize().width(); |
196 cancel_->SetBounds(x + (available_width - width) / 2, y, width, height); | 192 cancel_->SetBounds(x + (available_width - width) / 2, y, width, height); |
197 } | 193 } |
198 } | 194 } |
199 | 195 |
200 // Implementation of SpeechInputBubble. | 196 // Implementation of SpeechInputBubble. |
201 class SpeechInputBubbleImpl | 197 class SpeechInputBubbleImpl |
202 : public SpeechInputBubbleBase, | 198 : public SpeechInputBubbleBase, |
203 public InfoBubbleDelegate, | 199 public InfoBubbleDelegate { |
204 public NotificationObserver { | |
205 public: | 200 public: |
206 SpeechInputBubbleImpl(TabContents* tab_contents, | 201 SpeechInputBubbleImpl(TabContents* tab_contents, |
207 Delegate* delegate, | 202 Delegate* delegate, |
208 const gfx::Rect& element_rect); | 203 const gfx::Rect& element_rect); |
209 virtual ~SpeechInputBubbleImpl(); | 204 virtual ~SpeechInputBubbleImpl(); |
210 | 205 |
211 // SpeechInputBubble methods. | 206 // SpeechInputBubble methods. |
212 virtual void Show(); | 207 virtual void Show(); |
213 virtual void Hide(); | 208 virtual void Hide(); |
214 | 209 |
215 // SpeechInputBubbleBase methods. | 210 // SpeechInputBubbleBase methods. |
216 virtual void UpdateLayout(); | 211 virtual void UpdateLayout(); |
217 virtual void SetImage(const SkBitmap& image); | 212 virtual void SetImage(const SkBitmap& image); |
218 | 213 |
219 // Returns the screen rectangle to use as the info bubble's target. | 214 // Returns the screen rectangle to use as the info bubble's target. |
220 // |element_rect| is the html element's bounds in page coordinates. | 215 // |element_rect| is the html element's bounds in page coordinates. |
221 gfx::Rect GetInfoBubbleTarget(const gfx::Rect& element_rect); | 216 gfx::Rect GetInfoBubbleTarget(const gfx::Rect& element_rect); |
222 | 217 |
223 // NotificationObserver implementation. | |
224 virtual void Observe(NotificationType type, | |
225 const NotificationSource& source, | |
226 const NotificationDetails& details); | |
227 | |
228 // InfoBubbleDelegate | 218 // InfoBubbleDelegate |
229 virtual void InfoBubbleClosing(InfoBubble* info_bubble, | 219 virtual void InfoBubbleClosing(InfoBubble* info_bubble, |
230 bool closed_by_escape); | 220 bool closed_by_escape); |
231 virtual bool CloseOnEscape(); | 221 virtual bool CloseOnEscape(); |
232 virtual bool FadeInOnShow(); | 222 virtual bool FadeInOnShow(); |
233 | 223 |
234 private: | 224 private: |
235 Delegate* delegate_; | 225 Delegate* delegate_; |
236 InfoBubble* info_bubble_; | 226 InfoBubble* info_bubble_; |
237 TabContents* tab_contents_; | |
238 ContentView* bubble_content_; | 227 ContentView* bubble_content_; |
239 NotificationRegistrar registrar_; | |
240 gfx::Rect element_rect_; | 228 gfx::Rect element_rect_; |
241 | 229 |
242 // Set to true if the object is being destroyed normally instead of the | 230 // Set to true if the object is being destroyed normally instead of the |
243 // user clicking outside the window causing it to close automatically. | 231 // user clicking outside the window causing it to close automatically. |
244 bool did_invoke_close_; | 232 bool did_invoke_close_; |
245 | 233 |
246 DISALLOW_COPY_AND_ASSIGN(SpeechInputBubbleImpl); | 234 DISALLOW_COPY_AND_ASSIGN(SpeechInputBubbleImpl); |
247 }; | 235 }; |
248 | 236 |
249 SpeechInputBubbleImpl::SpeechInputBubbleImpl(TabContents* tab_contents, | 237 SpeechInputBubbleImpl::SpeechInputBubbleImpl(TabContents* tab_contents, |
250 Delegate* delegate, | 238 Delegate* delegate, |
251 const gfx::Rect& element_rect) | 239 const gfx::Rect& element_rect) |
252 : delegate_(delegate), | 240 : SpeechInputBubbleBase(tab_contents), |
| 241 delegate_(delegate), |
253 info_bubble_(NULL), | 242 info_bubble_(NULL), |
254 tab_contents_(tab_contents), | |
255 bubble_content_(NULL), | 243 bubble_content_(NULL), |
256 element_rect_(element_rect), | 244 element_rect_(element_rect), |
257 did_invoke_close_(false) { | 245 did_invoke_close_(false) { |
258 } | 246 } |
259 | 247 |
260 SpeechInputBubbleImpl::~SpeechInputBubbleImpl() { | 248 SpeechInputBubbleImpl::~SpeechInputBubbleImpl() { |
261 did_invoke_close_ = true; | 249 did_invoke_close_ = true; |
262 Hide(); | 250 Hide(); |
263 } | 251 } |
264 | 252 |
265 gfx::Rect SpeechInputBubbleImpl::GetInfoBubbleTarget( | 253 gfx::Rect SpeechInputBubbleImpl::GetInfoBubbleTarget( |
266 const gfx::Rect& element_rect) { | 254 const gfx::Rect& element_rect) { |
267 gfx::Rect container_rect; | 255 gfx::Rect container_rect; |
268 tab_contents_->GetContainerBounds(&container_rect); | 256 tab_contents()->GetContainerBounds(&container_rect); |
269 return gfx::Rect( | 257 return gfx::Rect( |
270 container_rect.x() + element_rect.x() + kBubbleTargetOffsetX, | 258 container_rect.x() + element_rect.x() + kBubbleTargetOffsetX, |
271 container_rect.y() + element_rect.y() + element_rect.height(), 1, 1); | 259 container_rect.y() + element_rect.y() + element_rect.height(), 1, 1); |
272 } | 260 } |
273 | 261 |
274 void SpeechInputBubbleImpl::Observe(NotificationType type, | |
275 const NotificationSource& source, | |
276 const NotificationDetails& details) { | |
277 if (type == NotificationType::TAB_CONTENTS_DESTROYED) { | |
278 delegate_->InfoBubbleButtonClicked(SpeechInputBubble::BUTTON_CANCEL); | |
279 } else { | |
280 NOTREACHED() << "Unknown notification"; | |
281 } | |
282 } | |
283 | |
284 void SpeechInputBubbleImpl::InfoBubbleClosing(InfoBubble* info_bubble, | 262 void SpeechInputBubbleImpl::InfoBubbleClosing(InfoBubble* info_bubble, |
285 bool closed_by_escape) { | 263 bool closed_by_escape) { |
286 registrar_.Remove(this, NotificationType::TAB_CONTENTS_DESTROYED, | |
287 Source<TabContents>(tab_contents_)); | |
288 info_bubble_ = NULL; | 264 info_bubble_ = NULL; |
289 bubble_content_ = NULL; | 265 bubble_content_ = NULL; |
290 if (!did_invoke_close_) | 266 if (!did_invoke_close_) |
291 delegate_->InfoBubbleFocusChanged(); | 267 delegate_->InfoBubbleFocusChanged(); |
292 } | 268 } |
293 | 269 |
294 bool SpeechInputBubbleImpl::CloseOnEscape() { | 270 bool SpeechInputBubbleImpl::CloseOnEscape() { |
295 return false; | 271 return false; |
296 } | 272 } |
297 | 273 |
298 bool SpeechInputBubbleImpl::FadeInOnShow() { | 274 bool SpeechInputBubbleImpl::FadeInOnShow() { |
299 return false; | 275 return false; |
300 } | 276 } |
301 | 277 |
302 void SpeechInputBubbleImpl::Show() { | 278 void SpeechInputBubbleImpl::Show() { |
303 if (info_bubble_) | 279 if (info_bubble_) |
304 return; // nothing to do, already visible. | 280 return; // nothing to do, already visible. |
305 | 281 |
306 bubble_content_ = new ContentView(delegate_); | 282 bubble_content_ = new ContentView(delegate_); |
307 UpdateLayout(); | 283 UpdateLayout(); |
308 | 284 |
309 views::Widget* parent = views::Widget::GetWidgetFromNativeWindow( | 285 views::Widget* parent = views::Widget::GetWidgetFromNativeWindow( |
310 tab_contents_->view()->GetTopLevelNativeWindow()); | 286 tab_contents()->view()->GetTopLevelNativeWindow()); |
311 info_bubble_ = InfoBubble::Show(parent, | 287 info_bubble_ = InfoBubble::Show(parent, |
312 GetInfoBubbleTarget(element_rect_), | 288 GetInfoBubbleTarget(element_rect_), |
313 BubbleBorder::TOP_LEFT, bubble_content_, | 289 BubbleBorder::TOP_LEFT, bubble_content_, |
314 this); | 290 this); |
315 | 291 |
316 // We don't want fade outs when closing because it makes speech recognition | 292 // We don't want fade outs when closing because it makes speech recognition |
317 // appear slower than it is. Also setting it to false allows |Close| to | 293 // appear slower than it is. Also setting it to false allows |Close| to |
318 // destroy the bubble immediately instead of waiting for the fade animation | 294 // destroy the bubble immediately instead of waiting for the fade animation |
319 // to end so the caller can manage this object's life cycle like a normal | 295 // to end so the caller can manage this object's life cycle like a normal |
320 // stack based or member variable object. | 296 // stack based or member variable object. |
321 info_bubble_->set_fade_away_on_close(false); | 297 info_bubble_->set_fade_away_on_close(false); |
322 | |
323 registrar_.Add(this, NotificationType::TAB_CONTENTS_DESTROYED, | |
324 Source<TabContents>(tab_contents_)); | |
325 } | 298 } |
326 | 299 |
327 void SpeechInputBubbleImpl::Hide() { | 300 void SpeechInputBubbleImpl::Hide() { |
328 if (info_bubble_) | 301 if (info_bubble_) |
329 info_bubble_->Close(); | 302 info_bubble_->Close(); |
330 } | 303 } |
331 | 304 |
332 void SpeechInputBubbleImpl::UpdateLayout() { | 305 void SpeechInputBubbleImpl::UpdateLayout() { |
333 if (bubble_content_) | 306 if (bubble_content_) |
334 bubble_content_->UpdateLayout(display_mode(), message_text()); | 307 bubble_content_->UpdateLayout(display_mode(), message_text()); |
335 if (info_bubble_) // Will be null on first call. | 308 if (info_bubble_) // Will be null on first call. |
336 info_bubble_->SizeToContents(); | 309 info_bubble_->SizeToContents(); |
337 } | 310 } |
338 | 311 |
339 void SpeechInputBubbleImpl::SetImage(const SkBitmap& image) { | 312 void SpeechInputBubbleImpl::SetImage(const SkBitmap& image) { |
340 if (bubble_content_) | 313 if (bubble_content_) |
341 bubble_content_->SetImage(image); | 314 bubble_content_->SetImage(image); |
342 } | 315 } |
343 | 316 |
344 } // namespace | 317 } // namespace |
345 | 318 |
346 SpeechInputBubble* SpeechInputBubble::CreateNativeBubble( | 319 SpeechInputBubble* SpeechInputBubble::CreateNativeBubble( |
347 TabContents* tab_contents, | 320 TabContents* tab_contents, |
348 SpeechInputBubble::Delegate* delegate, | 321 SpeechInputBubble::Delegate* delegate, |
349 const gfx::Rect& element_rect) { | 322 const gfx::Rect& element_rect) { |
350 return new SpeechInputBubbleImpl(tab_contents, delegate, element_rect); | 323 return new SpeechInputBubbleImpl(tab_contents, delegate, element_rect); |
351 } | 324 } |
OLD | NEW |