Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(185)

Side by Side Diff: chrome/browser/speech/speech_recognition_bubble_views.cc

Issue 10407048: browser: Move speech and login files into views/ directory. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/ui/login/login_prompt_views.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/speech/speech_recognition_bubble.h"
6
7 #include <algorithm>
8
9 #include "base/utf_string_conversions.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/browser_finder.h"
12 #include "chrome/browser/ui/views/frame/browser_view.h"
13 #include "chrome/browser/ui/views/location_bar/location_icon_view.h"
14 #include "chrome/browser/ui/views/toolbar_view.h"
15 #include "content/public/browser/resource_context.h"
16 #include "content/public/browser/speech_recognition_manager.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/browser/web_contents_view.h"
19 #include "grit/generated_resources.h"
20 #include "grit/theme_resources.h"
21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/base/resource/resource_bundle.h"
23 #include "ui/views/bubble/bubble_delegate.h"
24 #include "ui/views/controls/button/text_button.h"
25 #include "ui/views/controls/image_view.h"
26 #include "ui/views/controls/label.h"
27 #include "ui/views/controls/link.h"
28 #include "ui/views/controls/link_listener.h"
29 #include "ui/views/layout/layout_constants.h"
30
31 using content::WebContents;
32
33 namespace {
34
35 const int kBubbleHorizMargin = 6;
36 const int kBubbleVertMargin = 4;
37 const int kBubbleHeadingVertMargin = 6;
38 const int kIconHorizontalOffset = 27;
39 const int kIconVerticalOffset = -7;
40
41 // This is the SpeechRecognitionBubble content and views bubble delegate.
42 class SpeechRecognitionBubbleView
43 : public views::BubbleDelegateView,
44 public views::ButtonListener,
45 public views::LinkListener {
46 public:
47 SpeechRecognitionBubbleView(SpeechRecognitionBubbleDelegate* delegate,
48 views::View* anchor_view,
49 const gfx::Rect& element_rect,
50 WebContents* web_contents);
51
52 void UpdateLayout(SpeechRecognitionBubbleBase::DisplayMode mode,
53 const string16& message_text,
54 const SkBitmap& image);
55 void SetImage(const SkBitmap& image);
56
57 // views::BubbleDelegateView methods.
58 virtual void OnWidgetActivationChanged(views::Widget* widget,
59 bool active) OVERRIDE;
60 virtual gfx::Rect GetAnchorRect() OVERRIDE;
61 virtual void Init() OVERRIDE;
62
63 // views::ButtonListener methods.
64 virtual void ButtonPressed(views::Button* source,
65 const views::Event& event) OVERRIDE;
66
67 // views::LinkListener methods.
68 virtual void LinkClicked(views::Link* source, int event_flags) OVERRIDE;
69
70 // views::View overrides.
71 virtual gfx::Size GetPreferredSize() OVERRIDE;
72 virtual void Layout() OVERRIDE;
73
74 void set_notify_delegate_on_activation_change(bool notify) {
75 notify_delegate_on_activation_change_ = notify;
76 }
77
78 private:
79 SpeechRecognitionBubbleDelegate* delegate_;
80 gfx::Rect element_rect_;
81 WebContents* web_contents_;
82 bool notify_delegate_on_activation_change_;
83 views::ImageView* icon_;
84 views::Label* heading_;
85 views::Label* message_;
86 views::TextButton* try_again_;
87 views::TextButton* cancel_;
88 views::Link* mic_settings_;
89 SpeechRecognitionBubbleBase::DisplayMode display_mode_;
90 const int kIconLayoutMinWidth;
91
92 DISALLOW_COPY_AND_ASSIGN(SpeechRecognitionBubbleView);
93 };
94
95 SpeechRecognitionBubbleView::SpeechRecognitionBubbleView(
96 SpeechRecognitionBubbleDelegate* delegate,
97 views::View* anchor_view,
98 const gfx::Rect& element_rect,
99 WebContents* web_contents)
100 : BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_LEFT),
101 delegate_(delegate),
102 element_rect_(element_rect),
103 web_contents_(web_contents),
104 notify_delegate_on_activation_change_(true),
105 icon_(NULL),
106 heading_(NULL),
107 message_(NULL),
108 try_again_(NULL),
109 cancel_(NULL),
110 mic_settings_(NULL),
111 display_mode_(SpeechRecognitionBubbleBase::DISPLAY_MODE_WARM_UP),
112 kIconLayoutMinWidth(ResourceBundle::GetSharedInstance().GetBitmapNamed(
113 IDR_SPEECH_INPUT_MIC_EMPTY)->width()) {
114 // The bubble lifetime is managed by its controller; closing on escape or
115 // explicitly closing on deactivation will cause unexpected behavior.
116 set_close_on_esc(false);
117 set_close_on_deactivate(false);
118 }
119
120 void SpeechRecognitionBubbleView::OnWidgetActivationChanged(
121 views::Widget* widget, bool active) {
122 if (widget == GetWidget() && !active && notify_delegate_on_activation_change_)
123 delegate_->InfoBubbleFocusChanged();
124 BubbleDelegateView::OnWidgetActivationChanged(widget, active);
125 }
126
127 gfx::Rect SpeechRecognitionBubbleView::GetAnchorRect() {
128 gfx::Rect container_rect;
129 web_contents_->GetContainerBounds(&container_rect);
130 gfx::Rect anchor(element_rect_);
131 anchor.Offset(container_rect.origin());
132 if (!container_rect.Intersects(anchor))
133 return BubbleDelegateView::GetAnchorRect();
134 return anchor;
135 }
136
137 void SpeechRecognitionBubbleView::Init() {
138 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
139 const gfx::Font& font = rb.GetFont(ResourceBundle::MediumFont);
140
141 heading_ = new views::Label(
142 l10n_util::GetStringUTF16(IDS_SPEECH_INPUT_BUBBLE_HEADING));
143 heading_->set_border(views::Border::CreateEmptyBorder(
144 kBubbleHeadingVertMargin, 0, kBubbleHeadingVertMargin, 0));
145 heading_->SetFont(font);
146 heading_->SetHorizontalAlignment(views::Label::ALIGN_CENTER);
147 heading_->SetText(
148 l10n_util::GetStringUTF16(IDS_SPEECH_INPUT_BUBBLE_HEADING));
149 AddChildView(heading_);
150
151 message_ = new views::Label();
152 message_->SetFont(font);
153 message_->SetHorizontalAlignment(views::Label::ALIGN_CENTER);
154 message_->SetMultiLine(true);
155 AddChildView(message_);
156
157 icon_ = new views::ImageView();
158 icon_->SetHorizontalAlignment(views::ImageView::CENTER);
159 AddChildView(icon_);
160
161 cancel_ = new views::NativeTextButton(
162 this, l10n_util::GetStringUTF16(IDS_CANCEL));
163 AddChildView(cancel_);
164
165 try_again_ = new views::NativeTextButton(
166 this, l10n_util::GetStringUTF16(IDS_SPEECH_INPUT_TRY_AGAIN));
167 AddChildView(try_again_);
168
169 mic_settings_ = new views::Link(
170 l10n_util::GetStringUTF16(IDS_SPEECH_INPUT_MIC_SETTINGS));
171 mic_settings_->set_listener(this);
172 AddChildView(mic_settings_);
173 }
174
175 void SpeechRecognitionBubbleView::UpdateLayout(
176 SpeechRecognitionBubbleBase::DisplayMode mode,
177 const string16& message_text,
178 const SkBitmap& image) {
179 display_mode_ = mode;
180 bool is_message = (mode == SpeechRecognitionBubbleBase::DISPLAY_MODE_MESSAGE);
181 icon_->SetVisible(!is_message);
182 message_->SetVisible(is_message);
183 mic_settings_->SetVisible(is_message);
184 try_again_->SetVisible(is_message);
185 cancel_->SetVisible(
186 mode != SpeechRecognitionBubbleBase::DISPLAY_MODE_WARM_UP);
187 heading_->SetVisible(
188 mode == SpeechRecognitionBubbleBase::DISPLAY_MODE_RECORDING);
189
190 // Clickable elements should be enabled if and only if they are visible.
191 mic_settings_->SetEnabled(mic_settings_->visible());
192 try_again_->SetEnabled(try_again_->visible());
193 cancel_->SetEnabled(cancel_->visible());
194
195 if (is_message) {
196 message_->SetText(message_text);
197 } else {
198 SetImage(image);
199 }
200
201 if (icon_->visible())
202 icon_->ResetImageSize();
203
204 // When moving from warming up to recording state, the size of the content
205 // stays the same. So we wouldn't get a resize/layout call from the view
206 // system and we do it ourselves.
207 if (GetPreferredSize() == size()) // |size()| here is the current size.
208 Layout();
209
210 SizeToContents();
211 }
212
213 void SpeechRecognitionBubbleView::SetImage(const SkBitmap& image) {
214 icon_->SetImage(image);
215 }
216
217 void SpeechRecognitionBubbleView::ButtonPressed(views::Button* source,
218 const views::Event& event) {
219 if (source == cancel_) {
220 delegate_->InfoBubbleButtonClicked(SpeechRecognitionBubble::BUTTON_CANCEL);
221 } else if (source == try_again_) {
222 delegate_->InfoBubbleButtonClicked(
223 SpeechRecognitionBubble::BUTTON_TRY_AGAIN);
224 } else {
225 NOTREACHED() << "Unknown button";
226 }
227 }
228
229 void SpeechRecognitionBubbleView::LinkClicked(views::Link* source,
230 int event_flags) {
231 DCHECK_EQ(source, mic_settings_);
232 content::SpeechRecognitionManager::GetInstance()->ShowAudioInputSettings();
233 }
234
235 gfx::Size SpeechRecognitionBubbleView::GetPreferredSize() {
236 int width = heading_->GetPreferredSize().width();
237 int control_width = cancel_->GetPreferredSize().width();
238 if (try_again_->visible()) {
239 control_width += try_again_->GetPreferredSize().width() +
240 views::kRelatedButtonHSpacing;
241 }
242 width = std::max(width, control_width);
243 control_width = std::max(icon_->GetPreferredSize().width(),
244 kIconLayoutMinWidth);
245 width = std::max(width, control_width);
246 if (mic_settings_->visible()) {
247 control_width = mic_settings_->GetPreferredSize().width();
248 width = std::max(width, control_width);
249 }
250
251 int height = cancel_->GetPreferredSize().height();
252 if (message_->visible()) {
253 height += message_->GetHeightForWidth(width) +
254 views::kLabelToControlVerticalSpacing;
255 }
256 if (heading_->visible())
257 height += heading_->GetPreferredSize().height();
258 if (icon_->visible())
259 height += icon_->GetImage().height();
260 if (mic_settings_->visible())
261 height += mic_settings_->GetPreferredSize().height();
262 width += kBubbleHorizMargin * 2;
263 height += kBubbleVertMargin * 2;
264
265 return gfx::Size(width, height);
266 }
267
268 void SpeechRecognitionBubbleView::Layout() {
269 int x = kBubbleHorizMargin;
270 int y = kBubbleVertMargin;
271 int available_width = width() - kBubbleHorizMargin * 2;
272 int available_height = height() - kBubbleVertMargin * 2;
273
274 if (message_->visible()) {
275 DCHECK(try_again_->visible());
276
277 int control_height = try_again_->GetPreferredSize().height();
278 int try_again_width = try_again_->GetPreferredSize().width();
279 int cancel_width = cancel_->GetPreferredSize().width();
280 y += available_height - control_height;
281 x += (available_width - cancel_width - try_again_width -
282 views::kRelatedButtonHSpacing) / 2;
283 try_again_->SetBounds(x, y, try_again_width, control_height);
284 cancel_->SetBounds(x + try_again_width + views::kRelatedButtonHSpacing, y,
285 cancel_width, control_height);
286
287 control_height = message_->GetHeightForWidth(available_width);
288 message_->SetBounds(kBubbleHorizMargin, kBubbleVertMargin,
289 available_width, control_height);
290 y = kBubbleVertMargin + control_height;
291
292 control_height = mic_settings_->GetPreferredSize().height();
293 mic_settings_->SetBounds(kBubbleHorizMargin, y, available_width,
294 control_height);
295 } else {
296 DCHECK(icon_->visible());
297
298 int control_height = icon_->GetImage().height();
299 if (display_mode_ == SpeechRecognitionBubbleBase::DISPLAY_MODE_WARM_UP)
300 y = (available_height - control_height) / 2;
301 icon_->SetBounds(x, y, available_width, control_height);
302 y += control_height;
303
304 if (heading_->visible()) {
305 control_height = heading_->GetPreferredSize().height();
306 heading_->SetBounds(x, y, available_width, control_height);
307 y += control_height;
308 }
309
310 if (cancel_->visible()) {
311 control_height = cancel_->GetPreferredSize().height();
312 int width = cancel_->GetPreferredSize().width();
313 cancel_->SetBounds(x + (available_width - width) / 2, y, width,
314 control_height);
315 }
316 }
317 }
318
319 // Implementation of SpeechRecognitionBubble.
320 class SpeechRecognitionBubbleImpl : public SpeechRecognitionBubbleBase {
321 public:
322 SpeechRecognitionBubbleImpl(WebContents* web_contents,
323 Delegate* delegate,
324 const gfx::Rect& element_rect);
325 virtual ~SpeechRecognitionBubbleImpl();
326
327 // SpeechRecognitionBubble methods.
328 virtual void Show() OVERRIDE;
329 virtual void Hide() OVERRIDE;
330
331 // SpeechRecognitionBubbleBase methods.
332 virtual void UpdateLayout() OVERRIDE;
333 virtual void UpdateImage() OVERRIDE;
334
335 private:
336 Delegate* delegate_;
337 SpeechRecognitionBubbleView* bubble_;
338 gfx::Rect element_rect_;
339
340 DISALLOW_COPY_AND_ASSIGN(SpeechRecognitionBubbleImpl);
341 };
342
343 SpeechRecognitionBubbleImpl::SpeechRecognitionBubbleImpl(
344 WebContents* web_contents, Delegate* delegate,
345 const gfx::Rect& element_rect)
346 : SpeechRecognitionBubbleBase(web_contents),
347 delegate_(delegate),
348 bubble_(NULL),
349 element_rect_(element_rect) {
350 }
351
352 SpeechRecognitionBubbleImpl::~SpeechRecognitionBubbleImpl() {
353 if (bubble_) {
354 bubble_->set_notify_delegate_on_activation_change(false);
355 bubble_->GetWidget()->Close();
356 }
357 }
358
359 void SpeechRecognitionBubbleImpl::Show() {
360 if (!bubble_) {
361 // Anchor to the location icon view, in case |element_rect| is offscreen.
362 Browser* browser = browser::FindOrCreateTabbedBrowser(
363 Profile::FromBrowserContext(GetWebContents()->GetBrowserContext()));
364 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser);
365 views::View* icon = browser_view->GetLocationBarView() ?
366 browser_view->GetLocationBarView()->location_icon_view() : NULL;
367 bubble_ = new SpeechRecognitionBubbleView(delegate_, icon, element_rect_,
368 GetWebContents());
369 views::BubbleDelegateView::CreateBubble(bubble_);
370 UpdateLayout();
371 }
372 bubble_->Show();
373 }
374
375 void SpeechRecognitionBubbleImpl::Hide() {
376 if (bubble_)
377 bubble_->GetWidget()->Hide();
378 }
379
380 void SpeechRecognitionBubbleImpl::UpdateLayout() {
381 if (bubble_)
382 bubble_->UpdateLayout(display_mode(), message_text(), icon_image());
383 }
384
385 void SpeechRecognitionBubbleImpl::UpdateImage() {
386 if (bubble_)
387 bubble_->SetImage(icon_image());
388 }
389
390 } // namespace
391
392 SpeechRecognitionBubble* SpeechRecognitionBubble::CreateNativeBubble(
393 WebContents* web_contents,
394 SpeechRecognitionBubble::Delegate* delegate,
395 const gfx::Rect& element_rect) {
396 return new SpeechRecognitionBubbleImpl(web_contents, delegate, element_rect);
397 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/ui/login/login_prompt_views.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698