summaryrefslogtreecommitdiffstats
path: root/chrome/browser/speech/speech_input_bubble_mac.mm
blob: 912370ebad8b92f165f8519076ba60ce161f1c9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import <Cocoa/Cocoa.h>

#include "chrome/browser/speech/speech_input_bubble.h"

#import "base/scoped_nsobject.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/tab_contents/tab_contents_view.h"
#import "chrome/browser/ui/cocoa/speech_input_window_controller.h"
#include "skia/ext/skia_utils_mac.h"

namespace {

// A class to bridge between the speech recognition C++ code and the Objective-C
// bubble implementation. See chrome/browser/speech/speech_input_bubble.h for
// more information on how this gets used.
class SpeechInputBubbleImpl : public SpeechInputBubbleBase {
 public:
  SpeechInputBubbleImpl(TabContents* tab_contents,
                        Delegate* delegate,
                        const gfx::Rect& element_rect);
  virtual ~SpeechInputBubbleImpl();
  virtual void Show();
  virtual void Hide();
  virtual void UpdateLayout();
  virtual void SetImage(const SkBitmap& image);

 private:
  scoped_nsobject<SpeechInputWindowController> window_;
  Delegate* delegate_;
  gfx::Rect element_rect_;
};

SpeechInputBubbleImpl::SpeechInputBubbleImpl(TabContents* tab_contents,
                                             Delegate* delegate,
                                             const gfx::Rect& element_rect)
    : SpeechInputBubbleBase(tab_contents),
      delegate_(delegate),
      element_rect_(element_rect) {
}

SpeechInputBubbleImpl::~SpeechInputBubbleImpl() {
  if (window_.get())
    [window_.get() close];
}

void SpeechInputBubbleImpl::SetImage(const SkBitmap& image) {
  if (window_.get())
    [window_.get() setImage:gfx::SkBitmapToNSImage(image)];
}

void SpeechInputBubbleImpl::Show() {
  if (window_.get()) {
    [window_.get() show];
    return;
  }

  // Find the screen coordinates for the given tab and position the bubble's
  // arrow anchor point inside that to point at the bottom-left of the html
  // input element rect.
  gfx::NativeView view = tab_contents()->view()->GetNativeView();
  NSRect tab_bounds = [view bounds];
  NSPoint anchor = NSMakePoint(
      tab_bounds.origin.x + element_rect_.x() + kBubbleTargetOffsetX,
      tab_bounds.origin.y + tab_bounds.size.height - element_rect_.y() -
      element_rect_.height());
  anchor = [view convertPoint:anchor toView:nil];
  anchor = [[view window] convertBaseToScreen:anchor];

  window_.reset([[SpeechInputWindowController alloc]
      initWithParentWindow:tab_contents()->view()->GetTopLevelNativeWindow()
                  delegate:delegate_
                anchoredAt:anchor]);

  UpdateLayout();
}

void SpeechInputBubbleImpl::Hide() {
  if (!window_.get())
    return;

  [window_.get() close];
  window_.reset();
}

void SpeechInputBubbleImpl::UpdateLayout() {
  if (!window_.get())
    return;

  [window_.get() updateLayout:display_mode()
                  messageText:message_text()];
}

}  // namespace

SpeechInputBubble* SpeechInputBubble::CreateNativeBubble(
    TabContents* tab_contents,
    Delegate* delegate,
    const gfx::Rect& element_rect) {
  return new SpeechInputBubbleImpl(tab_contents, delegate, element_rect);
}