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

Side by Side Diff: chrome/browser/ui/cocoa/speech_recognition_window_controller.mm

Issue 260903010: Start removing support for the experimental x-webkit-speech API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Don't touch histograms.xml Created 6 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
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 #import "speech_recognition_window_controller.h"
6
7 #include "base/logging.h"
8 #include "base/strings/sys_string_conversions.h"
9 #include "chrome/browser/ui/cocoa/info_bubble_view.h"
10 #include "grit/generated_resources.h"
11 #include "grit/theme_resources.h"
12 #import "skia/ext/skia_utils_mac.h"
13 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTw eaker.h"
14 #include "ui/base/l10n/l10n_util_mac.h"
15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/gfx/image/image.h"
17
18 const int kBubbleControlVerticalSpacing = 10; // Space between controls.
19 const int kBubbleHorizontalMargin = 5; // Space on either sides of controls.
20 const int kInstructionLabelMaxWidth = 150;
21
22 @interface SpeechRecognitionWindowController (Private)
23 - (NSSize)calculateContentSize;
24 - (void)layout:(NSSize)size;
25 @end
26
27 @implementation SpeechRecognitionWindowController
28
29 - (id)initWithParentWindow:(NSWindow*)parentWindow
30 delegate:(SpeechRecognitionBubbleDelegate*)delegate
31 anchoredAt:(NSPoint)anchoredAt {
32 if ((self = [super initWithWindowNibPath:@"SpeechRecognitionBubble"
33 parentWindow:parentWindow
34 anchoredAt:anchoredAt])) {
35 DCHECK(delegate);
36 delegate_ = delegate;
37 displayMode_ = SpeechRecognitionBubbleBase::DISPLAY_MODE_WARM_UP;
38 }
39 return self;
40 }
41
42 - (void)awakeFromNib {
43 [super awakeFromNib];
44 InfoBubbleView* bubble = [self bubble];
45 [bubble setArrowLocation:info_bubble::kTopLeft];
46 NSSize arrowSize = NSMakeSize(0, info_bubble::kBubbleArrowHeight);
47 arrowSize = [bubble convertSize:arrowSize toView:nil];
48 NSPoint anchorPoint = self.anchorPoint;
49 anchorPoint.y += arrowSize.height / 2.0;
50 self.anchorPoint = anchorPoint;
51 }
52
53 - (IBAction)cancel:(id)sender {
54 delegate_->InfoBubbleButtonClicked(SpeechRecognitionBubble::BUTTON_CANCEL);
55 }
56
57 - (IBAction)tryAgain:(id)sender {
58 delegate_->InfoBubbleButtonClicked(SpeechRecognitionBubble::BUTTON_TRY_AGAIN);
59 }
60
61 - (IBAction)micSettings:(id)sender {
62 [[NSWorkspace sharedWorkspace] openFile:
63 @"/System/Library/PreferencePanes/Sound.prefPane"];
64 }
65
66 // Calculate the window dimensions to reflect the sum height and max width of
67 // all controls, with appropriate spacing between and around them. The returned
68 // size is in view coordinates.
69 - (NSSize)calculateContentSize {
70 [GTMUILocalizerAndLayoutTweaker sizeToFitView:cancelButton_];
71 [GTMUILocalizerAndLayoutTweaker sizeToFitView:tryAgainButton_];
72 [GTMUILocalizerAndLayoutTweaker sizeToFitView:micSettingsButton_];
73 NSSize cancelSize = [cancelButton_ bounds].size;
74 NSSize tryAgainSize = [tryAgainButton_ bounds].size;
75 CGFloat newHeight = cancelSize.height + kBubbleControlVerticalSpacing;
76 CGFloat newWidth = cancelSize.width;
77 if (![tryAgainButton_ isHidden])
78 newWidth += tryAgainSize.width;
79
80 // The size of the bubble in warm up mode is fixed to be the same as in
81 // recording mode, so from warm up it can transition to recording without any
82 // UI jank.
83 bool isWarmUp = (displayMode_ ==
84 SpeechRecognitionBubbleBase::DISPLAY_MODE_WARM_UP);
85
86 if (![iconImage_ isHidden]) {
87 NSSize size = [[iconImage_ image] size];
88 if (isWarmUp) {
89 NSImage* volumeIcon =
90 ResourceBundle::GetSharedInstance().GetNativeImageNamed(
91 IDR_SPEECH_INPUT_MIC_EMPTY).ToNSImage();
92 size = [volumeIcon size];
93 }
94 newHeight += size.height;
95 newWidth = std::max(newWidth, size.width + 2 * kBubbleHorizontalMargin);
96 }
97
98 if (![instructionLabel_ isHidden] || isWarmUp) {
99 [instructionLabel_ sizeToFit];
100 NSSize textSize = [[instructionLabel_ cell] cellSize];
101 NSRect boundsRect = NSMakeRect(0, 0, kInstructionLabelMaxWidth,
102 CGFLOAT_MAX);
103 NSSize multiLineSize =
104 [[instructionLabel_ cell] cellSizeForBounds:boundsRect];
105 if (textSize.width > multiLineSize.width)
106 textSize = multiLineSize;
107 newHeight += textSize.height + kBubbleControlVerticalSpacing;
108 newWidth = std::max(newWidth, textSize.width);
109 }
110
111 if (![micSettingsButton_ isHidden]) {
112 NSSize size = [micSettingsButton_ bounds].size;
113 newHeight += size.height;
114 newWidth = std::max(newWidth, size.width);
115 }
116
117 return NSMakeSize(newWidth + 2 * kBubbleHorizontalMargin,
118 newHeight + 3 * kBubbleControlVerticalSpacing);
119 }
120
121 // Position the controls within the given content area bounds.
122 - (void)layout:(NSSize)size {
123 int y = kBubbleControlVerticalSpacing;
124
125 NSRect cancelRect = [cancelButton_ bounds];
126
127 if ([tryAgainButton_ isHidden]) {
128 cancelRect.origin.x = (size.width - NSWidth(cancelRect)) / 2;
129 } else {
130 NSRect tryAgainRect = [tryAgainButton_ bounds];
131 cancelRect.origin.x = (size.width - NSWidth(cancelRect) -
132 NSWidth(tryAgainRect)) / 2;
133 tryAgainRect.origin.x = cancelRect.origin.x + NSWidth(cancelRect);
134 tryAgainRect.origin.y = y;
135 [tryAgainButton_ setFrame:tryAgainRect];
136 }
137 cancelRect.origin.y = y;
138
139 if (![cancelButton_ isHidden]) {
140 [cancelButton_ setFrame:cancelRect];
141 y += NSHeight(cancelRect) + kBubbleControlVerticalSpacing;
142 }
143
144 NSRect rect;
145 if (![micSettingsButton_ isHidden]) {
146 rect = [micSettingsButton_ bounds];
147 rect.origin.x = (size.width - NSWidth(rect)) / 2;
148 rect.origin.y = y;
149 [micSettingsButton_ setFrame:rect];
150 y += rect.size.height + kBubbleControlVerticalSpacing;
151 }
152
153 if (![instructionLabel_ isHidden]) {
154 int spaceForIcon = 0;
155 if (![iconImage_ isHidden]) {
156 spaceForIcon = [[iconImage_ image] size].height +
157 kBubbleControlVerticalSpacing;
158 }
159
160 rect = NSMakeRect(0, y, size.width, size.height - y - spaceForIcon -
161 kBubbleControlVerticalSpacing * 2);
162 [instructionLabel_ setFrame:rect];
163 y = size.height - spaceForIcon - kBubbleControlVerticalSpacing;
164 }
165
166 if (![iconImage_ isHidden]) {
167 rect.size = [[iconImage_ image] size];
168 // In warm-up mode only the icon gets displayed so center it vertically.
169 if (displayMode_ == SpeechRecognitionBubbleBase::DISPLAY_MODE_WARM_UP)
170 y = (size.height - rect.size.height) / 2;
171 rect.origin.x = (size.width - NSWidth(rect)) / 2;
172 rect.origin.y = y;
173 [iconImage_ setFrame:rect];
174 }
175 }
176
177 - (void)updateLayout:(SpeechRecognitionBubbleBase::DisplayMode)mode
178 messageText:(const base::string16&)messageText
179 iconImage:(NSImage*)iconImage {
180 // The very first time this method is called, the child views would still be
181 // uninitialized and null. So we invoke [self window] first and that sets up
182 // the child views properly so we can do the layout calculations below.
183 NSWindow* window = [self window];
184 displayMode_ = mode;
185 BOOL is_message = (mode == SpeechRecognitionBubbleBase::DISPLAY_MODE_MESSAGE);
186 BOOL is_recording =
187 (mode == SpeechRecognitionBubbleBase::DISPLAY_MODE_RECORDING);
188 BOOL is_warm_up = (mode == SpeechRecognitionBubbleBase::DISPLAY_MODE_WARM_UP);
189 [iconImage_ setHidden:is_message];
190 [tryAgainButton_ setHidden:!is_message];
191 [micSettingsButton_ setHidden:!is_message];
192 [instructionLabel_ setHidden:!is_message && !is_recording];
193 [cancelButton_ setHidden:is_warm_up];
194
195 // Get the right set of controls to be visible.
196 if (is_message) {
197 [instructionLabel_ setStringValue:base::SysUTF16ToNSString(messageText)];
198 } else {
199 [iconImage_ setImage:iconImage];
200 [instructionLabel_ setStringValue:l10n_util::GetNSString(
201 IDS_SPEECH_INPUT_BUBBLE_HEADING)];
202 }
203
204 NSSize newSize = [self calculateContentSize];
205 [[self bubble] setFrameSize:newSize];
206
207 NSSize windowDelta = [[window contentView] convertSize:newSize toView:nil];
208 NSRect newFrame = [window frame];
209 newFrame.origin.y -= windowDelta.height - newFrame.size.height;
210 newFrame.size = windowDelta;
211 [window setFrame:newFrame display:YES];
212
213 [self layout:newSize]; // Layout all the child controls.
214 }
215
216 - (void)windowWillClose:(NSNotification*)notification {
217 delegate_->InfoBubbleFocusChanged();
218 }
219
220 - (void)show {
221 [self showWindow:nil];
222 }
223
224 - (void)hide {
225 [[self window] orderOut:nil];
226 }
227
228 - (void)setImage:(NSImage*)image {
229 [iconImage_ setImage:image];
230 }
231
232 @end // implementation SpeechRecognitionWindowController
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698