diff options
author | satish@chromium.org <satish@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-14 20:14:56 +0000 |
---|---|---|
committer | satish@chromium.org <satish@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-14 20:14:56 +0000 |
commit | 670c0ff0d8bb0e89cd14100c344ecfed0a0772d6 (patch) | |
tree | 8b516538be307e6a02f229d5b834819c98484490 /chrome/browser/ui/cocoa/speech_input_window_controller.mm | |
parent | 4db0d163d209312a7e1200bffa1a984ccfcaa6ad (diff) | |
download | chromium_src-670c0ff0d8bb0e89cd14100c344ecfed0a0772d6.zip chromium_src-670c0ff0d8bb0e89cd14100c344ecfed0a0772d6.tar.gz chromium_src-670c0ff0d8bb0e89cd14100c344ecfed0a0772d6.tar.bz2 |
Get the mac bubble to resize propery and fit content in all states.
This CL cleans up the Mac speech input bubble UI code to do proper calculations
on each state change and resize the window and content view to snugly fit the
controls shown inside. Earlier the window was kept almost the same size while
the content inside changed with varying spacing around.
As part of this we also wait until updateLayout() has been called before
displaying the bubble window on screen.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/6682031
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@78087 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/ui/cocoa/speech_input_window_controller.mm')
-rw-r--r-- | chrome/browser/ui/cocoa/speech_input_window_controller.mm | 77 |
1 files changed, 39 insertions, 38 deletions
diff --git a/chrome/browser/ui/cocoa/speech_input_window_controller.mm b/chrome/browser/ui/cocoa/speech_input_window_controller.mm index 5ab5db0..105e84c 100644 --- a/chrome/browser/ui/cocoa/speech_input_window_controller.mm +++ b/chrome/browser/ui/cocoa/speech_input_window_controller.mm @@ -18,6 +18,7 @@ const int kBubbleControlVerticalSpacing = 10; // Space between controls. const int kBubbleHorizontalMargin = 5; // Space on either sides of controls. +const int kInstructionLabelMaxWidth = 150; @interface SpeechInputWindowController (Private) - (NSSize)calculateContentSize; @@ -35,33 +36,13 @@ const int kBubbleHorizontalMargin = 5; // Space on either sides of controls. anchoredAt:anchoredAt])) { DCHECK(delegate); delegate_ = delegate; - - [self showWindow:nil]; } return self; } - (void)awakeFromNib { [super awakeFromNib]; - - NSWindow* window = [self window]; [[self bubble] setArrowLocation:info_bubble::kTopLeft]; - NSImage* icon = ResourceBundle::GetSharedInstance().GetNativeImageNamed( - IDR_SPEECH_INPUT_MIC_EMPTY); - [iconImage_ setImage:icon]; - - NSSize newSize = [self calculateContentSize]; - [[self bubble] setFrameSize:newSize]; - NSSize windowDelta = NSMakeSize( - newSize.width - NSWidth([[window contentView] bounds]), - newSize.height - NSHeight([[window contentView] bounds])); - windowDelta = [[window contentView] convertSize:windowDelta toView:nil]; - NSRect newFrame = [window frame]; - newFrame.size.width += windowDelta.width; - newFrame.size.height += windowDelta.height; - [window setFrame:newFrame display:NO]; - - [self layout:newSize]; // Layout all the child controls. } - (IBAction)cancel:(id)sender { @@ -87,25 +68,32 @@ const int kBubbleHorizontalMargin = 5; // Space on either sides of controls. NSSize cancelSize = [cancelButton_ bounds].size; NSSize tryAgainSize = [tryAgainButton_ bounds].size; CGFloat newHeight = cancelSize.height + kBubbleControlVerticalSpacing; - CGFloat newWidth = cancelSize.width + tryAgainSize.width; + CGFloat newWidth = cancelSize.width; + if (![tryAgainButton_ isHidden]) + newWidth += tryAgainSize.width; if (![iconImage_ isHidden]) { NSSize size = [[iconImage_ image] size]; newHeight += size.height; - newWidth = std::max(newWidth, size.width); + newWidth = std::max(newWidth, size.width + 2 * kBubbleHorizontalMargin); } if (![instructionLabel_ isHidden]) { - [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField: - instructionLabel_]; - NSSize size = [instructionLabel_ bounds].size; - newHeight += size.height + kBubbleControlVerticalSpacing; - newWidth = std::max(newWidth, size.width); + [instructionLabel_ sizeToFit]; + NSSize textSize = [[instructionLabel_ cell] cellSize]; + NSRect boundsRect = NSMakeRect(0, 0, kInstructionLabelMaxWidth, + CGFLOAT_MAX); + NSSize multiLineSize = + [[instructionLabel_ cell] cellSizeForBounds:boundsRect]; + if (textSize.width > multiLineSize.width) + textSize = multiLineSize; + newHeight += textSize.height + kBubbleControlVerticalSpacing; + newWidth = std::max(newWidth, textSize.width); } if (![micSettingsButton_ isHidden]) { NSSize size = [micSettingsButton_ bounds].size; - newHeight += size.height + kBubbleControlVerticalSpacing; + newHeight += size.height; newWidth = std::max(newWidth, size.width); } @@ -144,11 +132,16 @@ const int kBubbleHorizontalMargin = 5; // Space on either sides of controls. } if (![instructionLabel_ isHidden]) { - rect = [instructionLabel_ bounds]; - rect.origin.x = (size.width - NSWidth(rect)) / 2; - rect.origin.y = y; + int spaceForIcon = 0; + if (![iconImage_ isHidden]) { + spaceForIcon = [[iconImage_ image] size].height + + kBubbleControlVerticalSpacing; + } + + rect = NSMakeRect(0, y, size.width, size.height - y - spaceForIcon - + kBubbleControlVerticalSpacing * 2); [instructionLabel_ setFrame:rect]; - y += rect.size.height + kBubbleControlVerticalSpacing; + y = size.height - spaceForIcon - kBubbleControlVerticalSpacing; } if (![iconImage_ isHidden]) { @@ -157,11 +150,15 @@ const int kBubbleHorizontalMargin = 5; // Space on either sides of controls. rect.origin.y = y; [iconImage_ setFrame:rect]; } - } - (void)updateLayout:(SpeechInputBubbleBase::DisplayMode)mode messageText:(const string16&)messageText { + // The very first time this method is called, the child views would still be + // uninitialized and null. So we invoke [self window] first and that sets up + // the child views properly so we can do the layout calculations below. + NSWindow* window = [self window]; + // Get the right set of controls to be visible. if (mode == SpeechInputBubbleBase::DISPLAY_MODE_MESSAGE) { [instructionLabel_ setStringValue:base::SysUTF16ToNSString(messageText)]; @@ -187,11 +184,15 @@ const int kBubbleHorizontalMargin = 5; // Space on either sides of controls. } NSSize newSize = [self calculateContentSize]; - NSRect rect = [[self bubble] frame]; - rect.origin.y -= newSize.height - rect.size.height; - rect.size = newSize; - [[self bubble] setFrame:rect]; - [self layout:newSize]; + [[self bubble] setFrameSize:newSize]; + + NSSize windowDelta = [[window contentView] convertSize:newSize toView:nil]; + NSRect newFrame = [window frame]; + newFrame.origin.y -= windowDelta.height - newFrame.size.height; + newFrame.size = windowDelta; + [window setFrame:newFrame display:YES]; + + [self layout:newSize]; // Layout all the child controls. } - (void)windowWillClose:(NSNotification*)notification { |