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