diff options
author | groby@chromium.org <groby@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-16 04:24:47 +0000 |
---|---|---|
committer | groby@chromium.org <groby@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-16 04:24:47 +0000 |
commit | 85059c375bfd167ba24b51f59fe0b5ed70a53d9b (patch) | |
tree | 276e9f0eca519be64a6b821bc45997a2619a7902 | |
parent | 22b5d905166de8d6c704f3a11cd82bc2df5423af (diff) | |
download | chromium_src-85059c375bfd167ba24b51f59fe0b5ed70a53d9b.zip chromium_src-85059c375bfd167ba24b51f59fe0b5ed70a53d9b.tar.gz chromium_src-85059c375bfd167ba24b51f59fe0b5ed70a53d9b.tar.bz2 |
[WebIntents] Add close button (and hotkeys) to OSX Intents Picker
BUG=118068
TEST=none
Review URL: http://codereview.chromium.org/9703010
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@127103 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/ui/cocoa/web_intent_sheet_controller.mm | 43 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/web_intent_sheet_controller_unittest.mm | 21 |
2 files changed, 54 insertions, 10 deletions
diff --git a/chrome/browser/ui/cocoa/web_intent_sheet_controller.mm b/chrome/browser/ui/cocoa/web_intent_sheet_controller.mm index ef30039..29547b4 100644 --- a/chrome/browser/ui/cocoa/web_intent_sheet_controller.mm +++ b/chrome/browser/ui/cocoa/web_intent_sheet_controller.mm @@ -7,6 +7,7 @@ #include "base/memory/scoped_nsobject.h" #include "base/sys_string_conversions.h" #include "chrome/browser/ui/browser_list.h" +#import "chrome/browser/ui/cocoa/hover_close_button.h" #import "chrome/browser/ui/cocoa/hyperlink_button_cell.h" #import "chrome/browser/ui/cocoa/info_bubble_view.h" #import "chrome/browser/ui/cocoa/info_bubble_window.h" @@ -47,6 +48,9 @@ const CGFloat kVerticalSpacing = 10; // Square size of the image. const CGFloat kImageSize = 32; +// Square size of the close button. +const CGFloat kCloseButtonSize = 16; + // Spacing between the image and the text. const CGFloat kImageSpacing = 10; @@ -88,10 +92,25 @@ const CGFloat kTextWidth = kWindowWidth - (kImageSize + kImageSpacing + if ((self = [super initWithWindow:window.get()])) { picker_ = picker; [self performLayoutWithModel:NULL]; + [[self window] makeFirstResponder:self]; } return self; } +// Handle default OSX dialog cancel mechanisms. (Cmd-.) +- (void)cancelOperation:(id)sender { + [self closeSheet]; +} + +// Handle keyDown events, specifically ESC. +- (void)keyDown:(NSEvent*)event { + // Check for escape key. + if ([[event charactersIgnoringModifiers] isEqualToString:@"\e"]) + [self cancelOperation:self]; + else + [super keyDown:event]; +} + - (void)sheetDidEnd:(NSWindow*)sheet returnCode:(int)returnCode contextInfo:(void*)contextInfo { @@ -114,6 +133,11 @@ const CGFloat kTextWidth = kWindowWidth - (kImageSize + kImageSpacing + browser->OpenURL(params); } +// Cancels the current selection - no intent is selected. +- (IBAction)cancelSelection:(id)sender { + [self closeSheet]; +} + // A picker button has been pressed - invoke corresponding service. - (IBAction)invokeService:(id)sender { if (picker_) @@ -180,17 +204,26 @@ const CGFloat kTextWidth = kWindowWidth - (kImageSize + kImageSpacing + [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField: textField]; + NSRect buttonFrame = NSMakeRect(kFramePadding+kImageSize+kTextWidth, + offset, kCloseButtonSize, kCloseButtonSize); + scoped_nsobject<HoverCloseButton> closeButton( + [[HoverCloseButton alloc] initWithFrame:buttonFrame]); + [closeButton setTarget:self]; + [closeButton setAction:@selector(cancelSelection:)]; + // Adjust view height to fit elements, center-align elements. - CGFloat maxHeight = std::max(imageFrame.size.height,textFrame.size.height); - if (maxHeight > textFrame.size.height) - textFrame.origin.y += (maxHeight - textFrame.size.height) / 2; - else - imageFrame.origin.y += maxHeight / 2; + CGFloat maxHeight = std::max(buttonFrame.size.height, + std::max(imageFrame.size.height,textFrame.size.height)); + textFrame.origin.y += (maxHeight - textFrame.size.height) / 2; + imageFrame.origin.y += (maxHeight - imageFrame.size.height) / 2; + [textField setFrame:textFrame]; [imageView setFrame:imageFrame]; [subviews addObject:textField.get()]; [subviews addObject:imageView.get()]; + [subviews addObject:closeButton.get()]; + return NSHeight([imageView frame]); } diff --git a/chrome/browser/ui/cocoa/web_intent_sheet_controller_unittest.mm b/chrome/browser/ui/cocoa/web_intent_sheet_controller_unittest.mm index e047829..691ac37 100644 --- a/chrome/browser/ui/cocoa/web_intent_sheet_controller_unittest.mm +++ b/chrome/browser/ui/cocoa/web_intent_sheet_controller_unittest.mm @@ -5,6 +5,7 @@ #include "base/basictypes.h" #include "base/message_loop.h" #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" +#import "chrome/browser/ui/cocoa/hover_close_button.h" #import "chrome/browser/ui/cocoa/hyperlink_button_cell.h" #import "chrome/browser/ui/cocoa/info_bubble_window.h" #import "chrome/browser/ui/cocoa/web_intent_sheet_controller.h" @@ -74,22 +75,31 @@ class WebIntentPickerSheetControllerTest void CheckWindow(size_t icon_count) { NSArray* flip_views = [[window_ contentView] subviews]; + // Check for proper firstResponder. + ASSERT_EQ(controller_, [window_ firstResponder]); + // Expect 1 subview - the flip view. ASSERT_EQ(1U, [flip_views count]); NSArray* views = [[flip_views objectAtIndex:0] subviews]; - // 3 + |icon_count| subviews - Icon, Header text, |icon_count| buttons, - // and a CWS link. - ASSERT_EQ(3U + icon_count, [views count]); + // 4 + |icon_count| subviews - icon, header text, close button, + // |icon_count| buttons, and a CWS link. + ASSERT_EQ(4U + icon_count, [views count]); ASSERT_TRUE([[views objectAtIndex:0] isKindOfClass:[NSTextField class]]); ASSERT_TRUE([[views objectAtIndex:1] isKindOfClass:[NSImageView class]]); + ASSERT_TRUE([[views objectAtIndex:2] isKindOfClass: + [HoverCloseButton class]]); for(NSUInteger i = 0; i < icon_count; ++i) { - ASSERT_TRUE([[views objectAtIndex:2 + i] isKindOfClass: + ASSERT_TRUE([[views objectAtIndex:3 + i] isKindOfClass: [NSButton class]]); } + // Verify the close button + NSButton* close_button = static_cast<NSButton*>([views objectAtIndex:2]); + CheckButton(close_button, @selector(cancelSelection:)); + // Verify the Chrome Web Store button. NSButton* button = static_cast<NSButton*>([views lastObject]); ASSERT_TRUE([button isKindOfClass:[NSButton class]]); @@ -98,7 +108,7 @@ class WebIntentPickerSheetControllerTest // Verify buttons pointing to services. for(NSUInteger i = 0; i < icon_count; ++i) { - NSButton* button = [views objectAtIndex:2 + i]; + NSButton* button = [views objectAtIndex:3 + i]; CheckServiceButton(button, i); } } @@ -108,6 +118,7 @@ class WebIntentPickerSheetControllerTest CheckButton(button, @selector(invokeService:)); EXPECT_EQ(NSInteger(service_index), [button tag]); } + // Checks that a button is hooked up correctly. void CheckButton(id button, SEL action) { EXPECT_TRUE([button isKindOfClass:[NSButton class]] || |