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
3 // found in the LICENSE file.
5 #import <Cocoa/Cocoa.h>
7 #include "chrome/browser/speech/speech_recognition_bubble.h"
9 #import "base/mac/scoped_nsobject.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/cocoa/browser_window_cocoa.h"
12 #include "chrome/browser/ui/cocoa/browser_window_controller.h"
13 #include "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
14 #import "chrome/browser/ui/cocoa/speech_recognition_window_controller.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/browser/web_contents_view.h"
17 #include "ui/gfx/image/image_skia_util_mac.h"
19 using content::WebContents;
23 // A class to bridge between the speech recognition C++ code and the Objective-C
24 // bubble implementation. See chrome/browser/speech/speech_recognition_bubble.h
25 // for more information on how this gets used.
26 class SpeechRecognitionBubbleImpl : public SpeechRecognitionBubbleBase {
28 SpeechRecognitionBubbleImpl(int render_process_id,
31 const gfx::Rect& element_rect);
32 virtual ~SpeechRecognitionBubbleImpl();
33 virtual void Show() OVERRIDE;
34 virtual void Hide() OVERRIDE;
35 virtual void UpdateLayout() OVERRIDE;
36 virtual void UpdateImage() OVERRIDE;
39 base::scoped_nsobject<SpeechRecognitionWindowController> window_;
41 gfx::Rect element_rect_;
44 SpeechRecognitionBubbleImpl::SpeechRecognitionBubbleImpl(
45 int render_process_id,
48 const gfx::Rect& element_rect)
49 : SpeechRecognitionBubbleBase(render_process_id, render_view_id),
51 element_rect_(element_rect) {
54 SpeechRecognitionBubbleImpl::~SpeechRecognitionBubbleImpl() {
56 [window_.get() close];
59 void SpeechRecognitionBubbleImpl::UpdateImage() {
60 if (window_.get() && GetWebContents())
61 [window_.get() setImage:gfx::NSImageFromImageSkia(icon_image())];
64 void SpeechRecognitionBubbleImpl::Show() {
65 if (!GetWebContents())
73 // Find the screen coordinates for the given tab and position the bubble's
74 // arrow anchor point inside that to point at the bottom-left of the html
75 // input element rect if the position is valid, otherwise point it towards
76 // the page icon in the omnibox.
77 gfx::NativeView view = GetWebContents()->GetView()->GetNativeView();
78 NSWindow* parent_window = [view window];
79 NSRect tab_bounds = [view bounds];
80 int anchor_x = tab_bounds.origin.x + element_rect_.x() +
81 element_rect_.width() - kBubbleTargetOffsetX;
82 int anchor_y = tab_bounds.origin.y + tab_bounds.size.height -
83 element_rect_.y() - element_rect_.height();
85 NSPoint anchor = NSMakePoint(anchor_x, anchor_y);
86 if (NSPointInRect(anchor, tab_bounds)) {
87 // Good, convert to window coordinates.
88 anchor = [view convertPoint:anchor toView:nil];
90 LocationBarViewMac* locationBar =
91 [[parent_window windowController] locationBarBridge];
94 anchor = locationBar->GetPageInfoBubblePoint();
96 // This is very rare, but possible. Just use the top-left corner.
97 // See http://crbug.com/119237
98 anchor = NSMakePoint(NSMinX(tab_bounds), NSMaxY(tab_bounds));
99 anchor = [view convertPoint:anchor toView:nil];
103 anchor = [parent_window convertBaseToScreen:anchor];
105 window_.reset([[SpeechRecognitionWindowController alloc]
106 initWithParentWindow:parent_window
111 [window_.get() show];
114 void SpeechRecognitionBubbleImpl::Hide() {
118 [window_.get() close];
122 void SpeechRecognitionBubbleImpl::UpdateLayout() {
123 if (!window_.get() || !GetWebContents())
126 [window_.get() updateLayout:display_mode()
127 messageText:message_text()
128 iconImage:gfx::NSImageFromImageSkia(icon_image())];
133 SpeechRecognitionBubble* SpeechRecognitionBubble::CreateNativeBubble(
134 int render_process_id,
137 const gfx::Rect& element_rect) {
138 return new SpeechRecognitionBubbleImpl(render_process_id,