| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "chrome/browser/ui/cocoa/web_intent_bubble_controller.h" | 5 #import "chrome/browser/ui/cocoa/web_intent_sheet_controller.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_nsobject.h" | 7 #include "base/memory/scoped_nsobject.h" |
| 8 #include "base/sys_string_conversions.h" | 8 #include "base/sys_string_conversions.h" |
| 9 #include "chrome/browser/ui/browser_list.h" | 9 #include "chrome/browser/ui/browser_list.h" |
| 10 #import "chrome/browser/ui/cocoa/hyperlink_button_cell.h" | 10 #import "chrome/browser/ui/cocoa/hyperlink_button_cell.h" |
| 11 #import "chrome/browser/ui/cocoa/info_bubble_view.h" | 11 #import "chrome/browser/ui/cocoa/info_bubble_view.h" |
| 12 #import "chrome/browser/ui/cocoa/info_bubble_window.h" | 12 #import "chrome/browser/ui/cocoa/info_bubble_window.h" |
| 13 #include "chrome/browser/ui/cocoa/web_intent_picker_cocoa.h" | 13 #include "chrome/browser/ui/cocoa/web_intent_picker_cocoa.h" |
| 14 #include "chrome/browser/ui/intents/web_intent_picker_delegate.h" | 14 #include "chrome/browser/ui/intents/web_intent_picker_delegate.h" |
| 15 #include "chrome/browser/ui/intents/web_intent_picker_model.h" | 15 #include "chrome/browser/ui/intents/web_intent_picker_model.h" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 // sections is easier. Apple recommends flipping the coordinate origin when | 64 // sections is easier. Apple recommends flipping the coordinate origin when |
| 65 // doing a lot of text layout because it's more natural. | 65 // doing a lot of text layout because it's more natural. |
| 66 @interface WebIntentsContentView : NSView | 66 @interface WebIntentsContentView : NSView |
| 67 @end | 67 @end |
| 68 @implementation WebIntentsContentView | 68 @implementation WebIntentsContentView |
| 69 - (BOOL)isFlipped { | 69 - (BOOL)isFlipped { |
| 70 return YES; | 70 return YES; |
| 71 } | 71 } |
| 72 @end | 72 @end |
| 73 | 73 |
| 74 @implementation WebIntentBubbleController; | 74 @implementation WebIntentPickerSheetController; |
| 75 | 75 |
| 76 - (id)initWithPicker:(WebIntentPickerCocoa*)picker | 76 - (id)initWithPicker:(WebIntentPickerCocoa*)picker { |
| 77 parentWindow:(NSWindow*)parent | |
| 78 anchoredAt:(NSPoint)point { | |
| 79 // Use an arbitrary height because it will reflect the size of the content. | 77 // Use an arbitrary height because it will reflect the size of the content. |
| 80 NSRect contentRect = NSMakeRect(0, 0, kWindowWidth, kVerticalSpacing); | 78 NSRect contentRect = NSMakeRect(0, 0, kWindowWidth, kVerticalSpacing); |
| 81 // Create an empty window into which content is placed. | 79 |
| 82 scoped_nsobject<InfoBubbleWindow> window( | 80 // |window| is retained by the ConstrainedWindowMacDelegateCustomSheet when |
| 83 [[InfoBubbleWindow alloc] initWithContentRect:contentRect | 81 // the sheet is initialized. |
| 84 styleMask:NSBorderlessWindowMask | 82 scoped_nsobject<NSWindow> window( |
| 85 backing:NSBackingStoreBuffered | 83 [[NSWindow alloc] initWithContentRect:contentRect |
| 86 defer:NO]); | 84 styleMask:NSTitledWindowMask |
| 87 if ((self = [super initWithWindow:window.get() | 85 backing:NSBackingStoreBuffered |
| 88 parentWindow:parent | 86 defer:YES]); |
| 89 anchoredAt:point])) { | 87 |
| 88 if ((self = [super initWithWindow:window.get()])) { |
| 90 picker_ = picker; | 89 picker_ = picker; |
| 90 [self performLayoutWithModel:NULL]; |
| 91 } |
| 92 return self; |
| 93 } |
| 91 | 94 |
| 92 [[self bubble] setArrowLocation:info_bubble::kTopLeft]; | 95 - (void)sheetDidEnd:(NSWindow*)sheet |
| 93 [self performLayoutWithModel:NULL]; | 96 returnCode:(int)returnCode |
| 94 [self showWindow:nil]; | 97 contextInfo:(void*)contextInfo { |
| 95 picker_->set_controller(self); | 98 // Also called when user navigates to another page while the sheet is open. |
| 96 } | 99 if (picker_) |
| 97 | 100 picker_->OnSheetDidEnd(sheet); |
| 98 return self; | |
| 99 } | 101 } |
| 100 | 102 |
| 101 - (void)setInlineDispositionTabContents:(TabContentsWrapper*)wrapper { | 103 - (void)setInlineDispositionTabContents:(TabContentsWrapper*)wrapper { |
| 102 contents_ = wrapper; | 104 contents_ = wrapper; |
| 103 } | 105 } |
| 104 | 106 |
| 105 // We need to watch for window closing so we can notify up via |picker_|. | |
| 106 - (void)windowWillClose:(NSNotification*)notification { | |
| 107 if (picker_) { | |
| 108 WebIntentPickerCocoa* temp = picker_; | |
| 109 picker_ = NULL; // Abandon picker, we are done with it. | |
| 110 temp->OnCancelled(); | |
| 111 } | |
| 112 [super windowWillClose:notification]; | |
| 113 } | |
| 114 | |
| 115 // Pop up a new tab with the Chrome Web Store. | 107 // Pop up a new tab with the Chrome Web Store. |
| 116 - (IBAction)showChromeWebStore:(id)sender { | 108 - (IBAction)showChromeWebStore:(id)sender { |
| 117 GURL url(l10n_util::GetStringUTF8(IDS_WEBSTORE_URL)); | 109 GURL url(l10n_util::GetStringUTF8(IDS_WEBSTORE_URL)); |
| 118 Browser* browser = BrowserList::GetLastActive(); | 110 Browser* browser = BrowserList::GetLastActive(); |
| 119 OpenURLParams params( | 111 OpenURLParams params( |
| 120 url, Referrer(), NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_LINK, | 112 url, Referrer(), NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_LINK, |
| 121 false); | 113 false); |
| 122 browser->OpenURL(params); | 114 browser->OpenURL(params); |
| 123 } | 115 } |
| 124 | 116 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 contents_->web_contents()); | 204 contents_->web_contents()); |
| 213 NSRect frame = NSMakeRect(kFramePadding, offset, size.width(), size.height()); | 205 NSRect frame = NSMakeRect(kFramePadding, offset, size.width(), size.height()); |
| 214 | 206 |
| 215 [contents_->web_contents()->GetNativeView() setFrame:frame]; | 207 [contents_->web_contents()->GetNativeView() setFrame:frame]; |
| 216 [subviews addObject:contents_->web_contents()->GetNativeView()]; | 208 [subviews addObject:contents_->web_contents()->GetNativeView()]; |
| 217 | 209 |
| 218 return NSHeight(frame); | 210 return NSHeight(frame); |
| 219 } | 211 } |
| 220 | 212 |
| 221 // Add a single button for a specific service | 213 // Add a single button for a specific service |
| 222 -(CGFloat)addServiceButton:(NSString*)title | 214 - (CGFloat)addServiceButton:(NSString*)title |
| 223 withImage:(NSImage*)image | 215 withImage:(NSImage*)image |
| 224 index:(NSUInteger)index | 216 index:(NSUInteger)index |
| 225 toSubviews:(NSMutableArray*)subviews | 217 toSubviews:(NSMutableArray*)subviews |
| 226 atOffset:(CGFloat)offset { | 218 atOffset:(CGFloat)offset { |
| 227 NSRect frame = NSMakeRect(kFramePadding, offset, kServiceButtonWidth, 45); | 219 NSRect frame = NSMakeRect(kFramePadding, offset, kServiceButtonWidth, 45); |
| 228 scoped_nsobject<NSButton> button([[NSButton alloc] initWithFrame:frame]); | 220 scoped_nsobject<NSButton> button([[NSButton alloc] initWithFrame:frame]); |
| 229 | 221 |
| 230 if (image) { | 222 if (image) { |
| 231 [button setImage:image]; | 223 [button setImage:image]; |
| 232 [button setImagePosition:NSImageLeft]; | 224 [button setImagePosition:NSImageLeft]; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 246 frame = [button frame]; | 238 frame = [button frame]; |
| 247 frame.size.width = kServiceButtonWidth; | 239 frame.size.width = kServiceButtonWidth; |
| 248 [button setFrame:frame]; | 240 [button setFrame:frame]; |
| 249 | 241 |
| 250 return NSHeight([button frame]); | 242 return NSHeight([button frame]); |
| 251 } | 243 } |
| 252 | 244 |
| 253 // Layout the contents of the picker bubble. | 245 // Layout the contents of the picker bubble. |
| 254 - (void)performLayoutWithModel:(WebIntentPickerModel*)model { | 246 - (void)performLayoutWithModel:(WebIntentPickerModel*)model { |
| 255 // |offset| is the Y position that should be drawn at next. | 247 // |offset| is the Y position that should be drawn at next. |
| 256 CGFloat offset = kFramePadding + info_bubble::kBubbleArrowHeight; | 248 CGFloat offset = kFramePadding; |
| 257 | 249 |
| 258 // Keep the new subviews in an array that gets replaced at the end. | 250 // Keep the new subviews in an array that gets replaced at the end. |
| 259 NSMutableArray* subviews = [NSMutableArray array]; | 251 NSMutableArray* subviews = [NSMutableArray array]; |
| 260 | 252 |
| 261 if (contents_) { | 253 if (contents_) { |
| 262 offset += [self addInlineHtmlToSubviews:subviews atOffset:offset]; | 254 offset += [self addInlineHtmlToSubviews:subviews atOffset:offset]; |
| 263 } else { | 255 } else { |
| 264 offset += [self addHeaderToSubviews:subviews atOffset:offset]; | 256 offset += [self addHeaderToSubviews:subviews atOffset:offset]; |
| 265 if (model) { | 257 if (model) { |
| 266 for (NSUInteger i = 0; i < model->GetInstalledServiceCount(); ++i) { | 258 for (NSUInteger i = 0; i < model->GetInstalledServiceCount(); ++i) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 283 NSRect contentFrame = NSMakeRect(0, 0, kWindowWidth, offset); | 275 NSRect contentFrame = NSMakeRect(0, 0, kWindowWidth, offset); |
| 284 scoped_nsobject<WebIntentsContentView> contentView( | 276 scoped_nsobject<WebIntentsContentView> contentView( |
| 285 [[WebIntentsContentView alloc] initWithFrame:contentFrame]); | 277 [[WebIntentsContentView alloc] initWithFrame:contentFrame]); |
| 286 [contentView setSubviews:subviews]; | 278 [contentView setSubviews:subviews]; |
| 287 [contentView setAutoresizingMask:NSViewMinYMargin]; | 279 [contentView setAutoresizingMask:NSViewMinYMargin]; |
| 288 | 280 |
| 289 // Adjust frame to fit all elements. | 281 // Adjust frame to fit all elements. |
| 290 NSRect windowFrame = NSMakeRect(0, 0, kWindowWidth, offset); | 282 NSRect windowFrame = NSMakeRect(0, 0, kWindowWidth, offset); |
| 291 windowFrame.size = [[[self window] contentView] convertSize:windowFrame.size | 283 windowFrame.size = [[[self window] contentView] convertSize:windowFrame.size |
| 292 toView:nil]; | 284 toView:nil]; |
| 293 // Adjust the origin by the difference in height. | |
| 294 windowFrame.origin = [[self window] frame].origin; | |
| 295 windowFrame.origin.y -= NSHeight(windowFrame) - | |
| 296 NSHeight([[self window] frame]); | |
| 297 | 285 |
| 286 // Adjust the window frame to accomodate the content. |
| 287 windowFrame=[[self window] frameRectForContentRect:windowFrame]; |
| 298 [[self window] setFrame:windowFrame display:YES animate:YES]; | 288 [[self window] setFrame:windowFrame display:YES animate:YES]; |
| 299 | 289 |
| 300 // Replace the window's content. | 290 // Replace the window's content. |
| 301 [[[self window] contentView] setSubviews: | 291 [[[self window] contentView] setSubviews: |
| 302 [NSArray arrayWithObject:contentView]]; | 292 [NSArray arrayWithObject:contentView]]; |
| 303 } | 293 } |
| 304 | 294 |
| 305 @end // WebIntentBubbleController | 295 - (void)closeSheet { |
| 296 [NSApp endSheet:[self window]]; |
| 297 } |
| 298 @end // WebIntentPickerSheetController |
| OLD | NEW |