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

Side by Side Diff: chrome/browser/ui/cocoa/extensions/extension_install_dialog_controller.mm

Issue 7835039: Implement the inline extensions/apps install UI for Cocoa. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 3 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
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 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/extensions/extension_install_dialog_controller. h" 5 #import "chrome/browser/ui/cocoa/extensions/extension_install_dialog_controller. h"
6 6
7 #include "base/mac/mac_util.h" 7 #include "base/mac/mac_util.h"
8 #include "base/memory/scoped_nsobject.h"
8 #include "base/string_util.h" 9 #include "base/string_util.h"
9 #include "base/sys_string_conversions.h" 10 #include "base/sys_string_conversions.h"
10 #include "base/utf_string_conversions.h" 11 #include "base/utf_string_conversions.h"
11 #include "chrome/browser/extensions/extension_install_dialog.h" 12 #include "chrome/browser/extensions/extension_install_dialog.h"
12 #include "chrome/browser/ui/browser.h" 13 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_list.h" 14 #include "chrome/browser/ui/browser_list.h"
14 #include "chrome/browser/ui/browser_window.h" 15 #include "chrome/browser/ui/browser_window.h"
15 #include "chrome/common/extensions/extension.h" 16 #include "chrome/common/extensions/extension.h"
16 #include "grit/generated_resources.h" 17 #include "grit/generated_resources.h"
17 #include "skia/ext/skia_utils_mac.h" 18 #include "skia/ext/skia_utils_mac.h"
18 #include "ui/base/l10n/l10n_util.h" 19 #include "ui/base/l10n/l10n_util.h"
19 #include "ui/base/l10n/l10n_util_mac.h" 20 #include "ui/base/l10n/l10n_util_mac.h"
20 21
22 @interface ExtensionInstallDialogController ()
23 - (bool)isInlineInstall;
24 - (NSImageView*)appendRatingStar:(const SkBitmap*)skiaImage;
25 @end
26
21 namespace { 27 namespace {
22 28
29 // Padding above the warnings separator, we must also subtract this when hiding
30 // it.
31 const CGFloat kWarningsSeparatorPadding = 14;
32
23 // Maximum height we will adjust controls to when trying to accomodate their 33 // Maximum height we will adjust controls to when trying to accomodate their
24 // contents. 34 // contents.
25 const CGFloat kMaxControlHeight = 400; 35 const CGFloat kMaxControlHeight = 400;
26 36
27 // Adjust a control's height so that its content its not clipped. Returns the 37 // Adjust a control's height so that its content its not clipped. Returns the
28 // amount the control's height had to be adjusted. 38 // amount the control's height had to be adjusted.
29 CGFloat AdjustControlHeightToFitContent(NSControl* control) { 39 CGFloat AdjustControlHeightToFitContent(NSControl* control) {
30 NSRect currentRect = [control frame]; 40 NSRect currentRect = [control frame];
31 NSRect fitRect = currentRect; 41 NSRect fitRect = currentRect;
32 fitRect.size.height = kMaxControlHeight; 42 fitRect.size.height = kMaxControlHeight;
33 CGFloat desiredHeight = [[control cell] cellSizeForBounds:fitRect].height; 43 CGFloat desiredHeight = [[control cell] cellSizeForBounds:fitRect].height;
34 CGFloat offset = desiredHeight - currentRect.size.height; 44 CGFloat offset = desiredHeight - currentRect.size.height;
35 45
36 [control setFrameSize:NSMakeSize(currentRect.size.width, 46 [control setFrameSize:NSMakeSize(currentRect.size.width,
37 currentRect.size.height + offset)]; 47 currentRect.size.height + offset)];
38 return offset; 48 return offset;
39 } 49 }
40 50
41 // Moves the control vertically by the specified amount. 51 // Moves the control vertically by the specified amount.
42 void OffsetControlVertically(NSControl* control, CGFloat amount) { 52 void OffsetControlVertically(NSControl* control, CGFloat amount) {
43 NSPoint origin = [control frame].origin; 53 NSPoint origin = [control frame].origin;
44 origin.y += amount; 54 origin.y += amount;
45 [control setFrameOrigin:origin]; 55 [control setFrameOrigin:origin];
46 } 56 }
47 57
58 void* AppendRatingStarsShim(const SkBitmap* skiaImage, void* data) {
59 ExtensionInstallDialogController* controller =
60 static_cast<ExtensionInstallDialogController*>(data);
61 return [controller appendRatingStar:skiaImage];
62 }
63
48 } 64 }
49 65
50 @implementation ExtensionInstallDialogController 66 @implementation ExtensionInstallDialogController
51 67
52 @synthesize iconView = iconView_; 68 @synthesize iconView = iconView_;
53 @synthesize titleField = titleField_; 69 @synthesize titleField = titleField_;
54 @synthesize subtitleField = subtitleField_; 70 @synthesize subtitleField = subtitleField_;
55 @synthesize warningsField = warningsField_; 71 @synthesize warningsField = warningsField_;
56 @synthesize warningsBox= warningsBox_;
57 @synthesize cancelButton = cancelButton_; 72 @synthesize cancelButton = cancelButton_;
58 @synthesize okButton = okButton_; 73 @synthesize okButton = okButton_;
74 @synthesize warningsSeparator = warningsSeparator_;
75 @synthesize ratingStars = ratingStars_;
76 @synthesize ratingCountField = ratingCountField_;
77 @synthesize userCountField = userCountField_;
59 78
60 - (id)initWithParentWindow:(NSWindow*)window 79 - (id)initWithParentWindow:(NSWindow*)window
61 profile:(Profile*)profile 80 profile:(Profile*)profile
62 extension:(const Extension*)extension 81 extension:(const Extension*)extension
63 delegate:(ExtensionInstallUI::Delegate*)delegate 82 delegate:(ExtensionInstallUI::Delegate*)delegate
64 icon:(SkBitmap*)icon 83 icon:(SkBitmap*)icon
65 prompt:(const ExtensionInstallUI::Prompt&)prompt { 84 prompt:(const ExtensionInstallUI::Prompt&)prompt {
66 NSString* nibpath = nil; 85 NSString* nibpath = nil;
67 86
68 // We use a different XIB in the case of no permission warnings, that is a 87 // We use a different XIB in the case of inline installs or no permission
69 // little bit more nicely laid out. 88 // warnings, that respectively show webstore ratings data and are a more
70 if (prompt.GetPermissionCount() == 0) { 89 // nicely laid out.
90 if (prompt.type() == ExtensionInstallUI::INLINE_INSTALL_PROMPT) {
91 nibpath = [base::mac::MainAppBundle()
92 pathForResource:@"ExtensionInstallPromptInline"
93 ofType:@"nib"];
94 } else if (prompt.GetPermissionCount() == 0) {
71 nibpath = [base::mac::MainAppBundle() 95 nibpath = [base::mac::MainAppBundle()
72 pathForResource:@"ExtensionInstallPromptNoWarnings" 96 pathForResource:@"ExtensionInstallPromptNoWarnings"
73 ofType:@"nib"]; 97 ofType:@"nib"];
74 } else { 98 } else {
75 nibpath = [base::mac::MainAppBundle() 99 nibpath = [base::mac::MainAppBundle()
76 pathForResource:@"ExtensionInstallPrompt" 100 pathForResource:@"ExtensionInstallPrompt"
77 ofType:@"nib"]; 101 ofType:@"nib"];
78 } 102 }
79 103
80 if ((self = [super initWithWindowNibPath:nibpath owner:self])) { 104 if ((self = [super initWithWindowNibPath:nibpath owner:self])) {
81 parentWindow_ = window; 105 parentWindow_ = window;
82 profile_ = profile; 106 profile_ = profile;
83 icon_ = *icon; 107 icon_ = *icon;
84 delegate_ = delegate; 108 delegate_ = delegate;
85 109 extension_ = extension;
86 title_.reset([base::SysUTF16ToNSString( 110 prompt_.reset(new ExtensionInstallUI::Prompt(prompt));
87 prompt.GetHeading(extension->name())) retain]);
88 subtitle_.reset([base::SysUTF16ToNSString(
89 prompt.GetPermissionsHeader()) retain]);
90 button_.reset([base::SysUTF16ToNSString(
91 prompt.GetAcceptButtonLabel()) retain]);
92 NSString* cancel_button_label = prompt.HasAbortButtonLabel() ?
93 base::SysUTF16ToNSString(prompt.GetAbortButtonLabel()) :
94 l10n_util::GetNSString(IDS_CANCEL);
95 cancel_button_.reset([cancel_button_label retain]);
96
97 // We display the permission warnings as a simple text string, separated by
98 // newlines.
99 if (prompt.GetPermissionCount()) {
100 string16 joined_warnings;
101 for (size_t i = 0; i < prompt.GetPermissionCount(); ++i) {
102 if (i > 0)
103 joined_warnings += UTF8ToUTF16("\n\n");
104
105 joined_warnings += prompt.GetPermission(i);
106 }
107
108 warnings_.reset(
109 [base::SysUTF16ToNSString(joined_warnings) retain]);
110 }
111 } 111 }
112 return self; 112 return self;
113 } 113 }
114 114
115 - (void)runAsModalSheet { 115 - (void)runAsModalSheet {
116 [NSApp beginSheet:[self window] 116 [NSApp beginSheet:[self window]
117 modalForWindow:parentWindow_ 117 modalForWindow:parentWindow_
118 modalDelegate:self 118 modalDelegate:self
119 didEndSelector:@selector(didEndSheet:returnCode:contextInfo:) 119 didEndSelector:@selector(didEndSheet:returnCode:contextInfo:)
120 contextInfo:nil]; 120 contextInfo:nil];
121 } 121 }
122 122
123 - (IBAction)cancel:(id)sender { 123 - (IBAction)storeLinkClicked:(id)sender {
124 delegate_->InstallUIAbort(true); 124 GURL store_url(
125 extension_urls::GetWebstoreItemDetailURLPrefix() + extension_->id());
126 BrowserList::GetLastActiveWithProfile(profile_)->
127 OpenURL(store_url, GURL(), NEW_FOREGROUND_TAB, PageTransition::LINK);
128
129 delegate_->InstallUIAbort(/*user_initiated=*/true);
125 [NSApp endSheet:[self window]]; 130 [NSApp endSheet:[self window]];
126 } 131 }
127 132
133 - (IBAction)cancel:(id)sender {
134 delegate_->InstallUIAbort(/*user_initiated=*/true);
135 [NSApp endSheet:[self window]];
136 }
137
128 - (IBAction)ok:(id)sender { 138 - (IBAction)ok:(id)sender {
129 delegate_->InstallUIProceed(); 139 delegate_->InstallUIProceed();
130 [NSApp endSheet:[self window]]; 140 [NSApp endSheet:[self window]];
131 } 141 }
132 142
133 - (void)awakeFromNib { 143 - (void)awakeFromNib {
134 [titleField_ setStringValue:title_.get()]; 144 // Make sure we're the window's delegate as set in the nib.
135 [subtitleField_ setStringValue:subtitle_.get()]; 145 DCHECK_EQ(self, static_cast<ExtensionInstallDialogController*>(
136 [okButton_ setTitle:button_.get()]; 146 [[self window] delegate]));
137 [cancelButton_ setTitle:cancel_button_.get()]; 147
148 // Set control labels.
149 [titleField_ setStringValue:base::SysUTF16ToNSString(
150 prompt_->GetHeading(extension_->name()))];
151 [okButton_ setTitle:base::SysUTF16ToNSString(
152 prompt_->GetAcceptButtonLabel())];
153 [cancelButton_ setTitle:prompt_->HasAbortButtonLabel() ?
154 base::SysUTF16ToNSString(prompt_->GetAbortButtonLabel()) :
155 l10n_util::GetNSString(IDS_CANCEL)];
156 if ([self isInlineInstall]) {
157 prompt_->AppendRatingStars(AppendRatingStarsShim, self);
Nico 2011/09/06 01:04:00 The AppendRatingStarsShim() return value appears u
Mihai Parparita -not on Chrome 2011/09/06 01:25:59 Done. Also removed the reinterpret_cast for the ot
158 [ratingCountField_ setStringValue:base::SysUTF16ToNSString(
159 prompt_->GetRatingCount())];
160 [userCountField_ setStringValue:base::SysUTF16ToNSString(
161 prompt_->GetUserCount())];
162 }
138 163
139 NSImage* image = gfx::SkBitmapToNSImage(icon_); 164 NSImage* image = gfx::SkBitmapToNSImage(icon_);
140 [iconView_ setImage:image]; 165 [iconView_ setImage:image];
141 166
142 // Reisze |titleField_| to fit title 167 // Resize |titleField_| to fit the title.
143 CGFloat originalTitleWidth = [titleField_ frame].size.width; 168 CGFloat originalTitleWidth = [titleField_ frame].size.width;
144 [titleField_ sizeToFit]; 169 [titleField_ sizeToFit];
145 CGFloat newTitleWidth = [titleField_ frame].size.width; 170 CGFloat newTitleWidth = [titleField_ frame].size.width;
146 if (newTitleWidth > originalTitleWidth) { 171 if (newTitleWidth > originalTitleWidth) {
147 NSRect frame = [[self window] frame]; 172 NSRect frame = [[self window] frame];
148 frame.size.width += newTitleWidth - originalTitleWidth; 173 frame.size.width += newTitleWidth - originalTitleWidth;
149 [[self window] setFrame:frame display:NO]; 174 [[self window] setFrame:frame display:NO];
150 } 175 }
151 176
152 // Make sure we're the window's delegate as set in the nib. 177 // Resize |okButton_| and |cancelButton_| to fit the button labels, but keep
153 DCHECK_EQ(self, static_cast<ExtensionInstallDialogController*>( 178 // them right-aligned.
154 [[self window] delegate])); 179 NSSize buttonDelta = [GTMUILocalizerAndLayoutTweaker sizeToFitView:okButton_];
180 if (buttonDelta.width) {
181 [okButton_ setFrame:NSOffsetRect([okButton_ frame], -buttonDelta.width, 0)];
182 [cancelButton_ setFrame:NSOffsetRect([cancelButton_ frame],
183 -buttonDelta.width, 0)];
184 }
185 buttonDelta = [GTMUILocalizerAndLayoutTweaker sizeToFitView:cancelButton_];
186 if (buttonDelta.width) {
187 [cancelButton_ setFrame:NSOffsetRect([cancelButton_ frame],
188 -buttonDelta.width, 0)];
189 }
155 190
191 CGFloat totalOffset = 0.0;
156 // If there are any warnings, then we have to do some special layout. 192 // If there are any warnings, then we have to do some special layout.
157 if ([warnings_.get() length] > 0) { 193 if (prompt_->GetPermissionCount() > 0) {
158 [warningsField_ setStringValue:warnings_.get()]; 194 [subtitleField_ setStringValue:base::SysUTF16ToNSString(
195 prompt_->GetPermissionsHeader())];
196
197 // We display the permission warnings as a simple text string, separated by
198 // newlines.
199 NSMutableString* joinedWarnings = [NSMutableString string];
200 for (size_t i = 0; i < prompt_->GetPermissionCount(); ++i) {
201 if (i > 0)
202 [joinedWarnings appendString:@"\n"];
203 [joinedWarnings appendString:base::SysUTF16ToNSString(
204 prompt_->GetPermission(i))];
205 }
206 [warningsField_ setStringValue:joinedWarnings];
159 207
160 // The dialog is laid out in the NIB exactly how we want it assuming that 208 // The dialog is laid out in the NIB exactly how we want it assuming that
161 // each label fits on one line. However, for each label, we want to allow 209 // each label fits on one line. However, for each label, we want to allow
162 // wrapping onto multiple lines. So we accumulate an offset by measuring how 210 // wrapping onto multiple lines. So we accumulate an offset by measuring how
163 // big each label wants to be, and comparing it to how bit it actually is. 211 // big each label wants to be, and comparing it to how big it actually is.
164 // Then we shift each label down and resize by the appropriate amount, then 212 // Then we shift each label down and resize by the appropriate amount, then
165 // finally resize the window. 213 // finally resize the window.
166 CGFloat totalOffset = 0.0;
167 214
168 // Text fields. 215 // Additionally, in the store version of the dialog the icon extends past
169 totalOffset += AdjustControlHeightToFitContent(titleField_); 216 // the one-line version of the permission field. Therefore when increasing
170 OffsetControlVertically(titleField_, -totalOffset); 217 // the window size for multi-line permissions we don't have to add the full
218 //
219 CGFloat warningsGrowthSlack = 0;
220 if ([warningsField_ frame].origin.y > [iconView_ frame].origin.y) {
221 warningsGrowthSlack =
222 [warningsField_ frame].origin.y - [iconView_ frame].origin.y;
223 }
171 224
172 totalOffset += AdjustControlHeightToFitContent(subtitleField_); 225 totalOffset += AdjustControlHeightToFitContent(subtitleField_);
173 OffsetControlVertically(subtitleField_, -totalOffset); 226 OffsetControlVertically(subtitleField_, -totalOffset);
174 227
175 CGFloat warningsOffset = AdjustControlHeightToFitContent(warningsField_); 228 totalOffset += AdjustControlHeightToFitContent(warningsField_);
176 OffsetControlVertically(warningsField_, -warningsOffset); 229 OffsetControlVertically(warningsField_, -totalOffset);
177 totalOffset += warningsOffset; 230 totalOffset = MAX(totalOffset - warningsGrowthSlack, 0);
231 } else if ([self isInlineInstall]) {
232 // Inline installs that don't have a permissions section need to hide
233 // controls related to that and shrink the window by the space they take
234 // up.
235 NSRect hiddenRect = NSUnionRect([warningsSeparator_ frame],
236 [subtitleField_ frame]);
237 hiddenRect = NSUnionRect(hiddenRect, [warningsField_ frame]);
238 [warningsSeparator_ setHidden:YES];
239 [subtitleField_ setHidden:YES];
240 [warningsField_ setHidden:YES];
241 totalOffset -= hiddenRect.size.height + kWarningsSeparatorPadding;
242 }
178 243
179 NSRect warningsBoxRect = [warningsBox_ frame]; 244 // If necessary, adjust the window size.
180 warningsBoxRect.origin.y -= totalOffset; 245 if (totalOffset) {
181 warningsBoxRect.size.height += warningsOffset;
182 [warningsBox_ setFrame:warningsBoxRect];
183
184 // buttons are positioned automatically in the XIB.
185
186 // Finally, adjust the window size.
187 NSRect currentRect = [[self window] frame]; 246 NSRect currentRect = [[self window] frame];
188 [[self window] setFrame:NSMakeRect(currentRect.origin.x, 247 [[self window] setFrame:NSMakeRect(currentRect.origin.x,
189 currentRect.origin.y - totalOffset, 248 currentRect.origin.y - totalOffset,
190 currentRect.size.width, 249 currentRect.size.width,
191 currentRect.size.height + totalOffset) 250 currentRect.size.height + totalOffset)
192 display:NO]; 251 display:NO];
193 } 252 }
194 } 253 }
195 254
196 - (void)didEndSheet:(NSWindow*)sheet 255 - (void)didEndSheet:(NSWindow*)sheet
197 returnCode:(int)returnCode 256 returnCode:(int)returnCode
198 contextInfo:(void*)contextInfo { 257 contextInfo:(void*)contextInfo {
199 [sheet close]; 258 [sheet close];
200 } 259 }
201 260
202 - (void)windowWillClose:(NSNotification*)notification { 261 - (void)windowWillClose:(NSNotification*)notification {
203 [self autorelease]; 262 [self autorelease];
204 } 263 }
205 264
265 - (bool)isInlineInstall {
266 return prompt_->type() == ExtensionInstallUI::INLINE_INSTALL_PROMPT;
267 }
268
269 - (NSImageView*)appendRatingStar:(const SkBitmap*)skiaImage {
270 NSImage* image = gfx::SkBitmapToNSImageWithColorSpace(
271 *skiaImage, base::mac::GetSystemColorSpace());
272 NSRect frame = NSMakeRect(0, 0, skiaImage->width(), skiaImage->height());
273 scoped_nsobject<NSImageView> view([[NSImageView alloc] initWithFrame:frame]);
274 [view setImage:image];
275
276 // Add this star after all the other ones
277 CGFloat maxStarRight = 0;
278 if ([[ratingStars_ subviews] count]) {
279 maxStarRight = NSMaxX([[[ratingStars_ subviews] lastObject] frame]);
280 }
281 NSRect starBounds = NSMakeRect(maxStarRight, 0,
282 skiaImage->width(), skiaImage->height());
283 [view setFrame:starBounds];
284 [ratingStars_ addSubview:view];
285
286 return view.get();
287 }
288
206 @end // ExtensionInstallDialogController 289 @end // ExtensionInstallDialogController
207 290
208 void ShowExtensionInstallDialog( 291 void ShowExtensionInstallDialog(
209 Profile* profile, 292 Profile* profile,
210 ExtensionInstallUI::Delegate* delegate, 293 ExtensionInstallUI::Delegate* delegate,
211 const Extension* extension, 294 const Extension* extension,
212 SkBitmap* icon, 295 SkBitmap* icon,
213 const ExtensionInstallUI::Prompt& prompt) { 296 const ExtensionInstallUI::Prompt& prompt) {
214 Browser* browser = BrowserList::GetLastActiveWithProfile(profile); 297 Browser* browser = BrowserList::GetLastActiveWithProfile(profile);
215 if (!browser) { 298 if (!browser) {
(...skipping 11 matching lines...) Expand all
227 310
228 ExtensionInstallDialogController* controller = 311 ExtensionInstallDialogController* controller =
229 [[ExtensionInstallDialogController alloc] 312 [[ExtensionInstallDialogController alloc]
230 initWithParentWindow:native_window 313 initWithParentWindow:native_window
231 profile:profile 314 profile:profile
232 extension:extension 315 extension:extension
233 delegate:delegate 316 delegate:delegate
234 icon:icon 317 icon:icon
235 prompt:prompt]; 318 prompt:prompt];
236 319
320 // TODO(mihaip): Consider switching this to being tab-modal by using
321 // ConstrainedWindow.
237 [controller runAsModalSheet]; 322 [controller runAsModalSheet];
238 } 323 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698