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

Side by Side Diff: chrome/browser/cocoa/infobar_controller.mm

Issue 155788: Infobar UI cleanup on Mac. Adds the yellow background gradient and... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 11 years, 5 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
« no previous file with comments | « chrome/browser/cocoa/infobar_controller.h ('k') | chrome/browser/cocoa/infobar_gradient_view.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 <Cocoa/Cocoa.h> 5 #import <Cocoa/Cocoa.h>
6 6
7 #include "base/logging.h" // for NOTREACHED()
7 #include "base/mac_util.h" 8 #include "base/mac_util.h"
8 #include "base/sys_string_conversions.h" 9 #include "base/sys_string_conversions.h"
9 #include "chrome/browser/cocoa/infobar.h" 10 #include "chrome/browser/cocoa/infobar.h"
10 #import "chrome/browser/cocoa/infobar_container_controller.h" 11 #import "chrome/browser/cocoa/infobar_container_controller.h"
11 #import "chrome/browser/cocoa/infobar_controller.h" 12 #import "chrome/browser/cocoa/infobar_controller.h"
12 #include "chrome/browser/tab_contents/tab_contents.h" 13 #include "chrome/browser/tab_contents/tab_contents.h"
13 #include "skia/ext/skia_utils_mac.h" 14 #include "skia/ext/skia_utils_mac.h"
14 #include "webkit/glue/window_open_disposition.h" 15 #include "webkit/glue/window_open_disposition.h"
15 16
16 17
(...skipping 23 matching lines...) Expand all
40 // All infobars have an icon, so we set up the icon in the base class 41 // All infobars have an icon, so we set up the icon in the base class
41 // awakeFromNib. 42 // awakeFromNib.
42 - (void)awakeFromNib { 43 - (void)awakeFromNib {
43 if (delegate_->GetIcon()) { 44 if (delegate_->GetIcon()) {
44 [image_ setImage:gfx::SkBitmapToNSImage(*(delegate_->GetIcon()))]; 45 [image_ setImage:gfx::SkBitmapToNSImage(*(delegate_->GetIcon()))];
45 } 46 }
46 47
47 [self addAdditionalControls]; 48 [self addAdditionalControls];
48 } 49 }
49 50
51 // Called when someone clicks on the ok button.
52 - (void)ok:(id)sender {
53 // Subclasses must override this method if they do not hide the ok button.
54 NOTREACHED();
55 }
56
57 // Called when someone clicks on the cancel button.
58 - (void)cancel:(id)sender {
59 // Subclasses must override this method if they do not hide the cancel button.
60 NOTREACHED();
61 }
62
50 // Called when someone clicks on the close button. 63 // Called when someone clicks on the close button.
51 - (void)dismiss:(id)sender { 64 - (void)dismiss:(id)sender {
52 [self closeInfoBar]; 65 [self closeInfoBar];
53 } 66 }
54 67
55 - (void)addAdditionalControls { 68 - (void)addAdditionalControls {
56 // Default implementation does nothing. 69 // Default implementation does nothing.
57 } 70 }
58 71
59 @end 72 @end
(...skipping 14 matching lines...) Expand all
74 ///////////////////////////////////////////////////////////////////////// 87 /////////////////////////////////////////////////////////////////////////
75 // AlertInfoBarController implementation 88 // AlertInfoBarController implementation
76 89
77 @implementation AlertInfoBarController 90 @implementation AlertInfoBarController
78 91
79 // Alert infobars have a text message. 92 // Alert infobars have a text message.
80 - (void)addAdditionalControls { 93 - (void)addAdditionalControls {
81 AlertInfoBarDelegate* delegate = delegate_->AsAlertInfoBarDelegate(); 94 AlertInfoBarDelegate* delegate = delegate_->AsAlertInfoBarDelegate();
82 [label_ setStringValue:base::SysWideToNSString( 95 [label_ setStringValue:base::SysWideToNSString(
83 delegate->GetMessageText())]; 96 delegate->GetMessageText())];
97
98 // Remove the ok and cancel buttons, since they are not needed.
99 [okButton_ removeFromSuperview];
100 [cancelButton_ removeFromSuperview];
84 } 101 }
85 102
86 @end 103 @end
87 104
88 105
89 ///////////////////////////////////////////////////////////////////////// 106 /////////////////////////////////////////////////////////////////////////
90 // LinkInfoBarController implementation 107 // LinkInfoBarController implementation
91 108
92 @implementation LinkInfoBarController 109 @implementation LinkInfoBarController
93 110
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 initWithString:base::SysWideToNSString(delegate->GetLinkText()) 159 initWithString:base::SysWideToNSString(delegate->GetLinkText())
143 attributes:linkAttributes] autorelease] 160 attributes:linkAttributes] autorelease]
144 atIndex:offset]; 161 atIndex:offset];
145 162
146 // Update the label view with the new text. The view must be 163 // Update the label view with the new text. The view must be
147 // selectable and allow editing text attributes for the 164 // selectable and allow editing text attributes for the
148 // linkification to work correctly. 165 // linkification to work correctly.
149 [label_ setAllowsEditingTextAttributes: YES]; 166 [label_ setAllowsEditingTextAttributes: YES];
150 [label_ setSelectable: YES]; 167 [label_ setSelectable: YES];
151 [label_ setAttributedStringValue:infoText]; 168 [label_ setAttributedStringValue:infoText];
169
170 // Remove the ok and cancel buttons, since they are not needed.
171 [okButton_ removeFromSuperview];
172 [cancelButton_ removeFromSuperview];
152 } 173 }
153 174
154 // Called when someone clicks on the link in the infobar. This method 175 // Called when someone clicks on the link in the infobar. This method
155 // is called by the InfobarTextField on its delegate (the 176 // is called by the InfobarTextField on its delegate (the
156 // LinkInfoBarController). 177 // LinkInfoBarController).
157 - (void)linkClicked { 178 - (void)linkClicked {
158 // TODO(rohitrao): Set the disposition correctly based on modifier keys. 179 // TODO(rohitrao): Set the disposition correctly based on modifier keys.
159 WindowOpenDisposition disposition = CURRENT_TAB; 180 WindowOpenDisposition disposition = CURRENT_TAB;
160 if (delegate_->AsLinkInfoBarDelegate()->LinkClicked(disposition)) 181 if (delegate_->AsLinkInfoBarDelegate()->LinkClicked(disposition))
161 [self closeInfoBar]; 182 [self closeInfoBar];
(...skipping 17 matching lines...) Expand all
179 - (IBAction)cancel:(id)sender { 200 - (IBAction)cancel:(id)sender {
180 if (delegate_->AsConfirmInfoBarDelegate()->Cancel()) 201 if (delegate_->AsConfirmInfoBarDelegate()->Cancel())
181 [self closeInfoBar]; 202 [self closeInfoBar];
182 } 203 }
183 204
184 // Confirm infobars can have OK and/or cancel buttons, depending on 205 // Confirm infobars can have OK and/or cancel buttons, depending on
185 // the return value of GetButtons(). We create each button if 206 // the return value of GetButtons(). We create each button if
186 // required and position them to the left of the close button. 207 // required and position them to the left of the close button.
187 - (void)addAdditionalControls { 208 - (void)addAdditionalControls {
188 ConfirmInfoBarDelegate* delegate = delegate_->AsConfirmInfoBarDelegate(); 209 ConfirmInfoBarDelegate* delegate = delegate_->AsConfirmInfoBarDelegate();
210 int visibleButtons = delegate->GetButtons();
189 [label_ setStringValue:base::SysWideToNSString(delegate->GetMessageText())]; 211 [label_ setStringValue:base::SysWideToNSString(delegate->GetMessageText())];
190 212
191 int visibleButtons = delegate->GetButtons(); 213 // Save the margins between the buttons, so we can keep them constant.
192 NSButton *okButton = nil; 214 float cancelMargin =
193 NSButton *cancelButton = nil; 215 NSMinX([closeButton_ frame]) - NSMaxX([cancelButton_ frame]);
216 float okMargin = NSMinX([cancelButton_ frame]) - NSMaxX([okButton_ frame]);
217 float labelMargin = NSMinX([okButton_ frame]) - NSMaxX([label_ frame]);
194 218
195 // Create the OK button if needed. 219 // Create and position the cancel button if needed. Otherwise, hide it.
196 if (visibleButtons & ConfirmInfoBarDelegate::BUTTON_OK) { 220 if (visibleButtons & ConfirmInfoBarDelegate::BUTTON_CANCEL) {
197 okButton = [[[NSButton alloc] initWithFrame:NSZeroRect] autorelease]; 221 [cancelButton_ setTitle:base::SysWideToNSString(
198 [okButton setBezelStyle:NSRoundedBezelStyle]; 222 delegate->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_CANCEL))];
199 [okButton setTitle:base::SysWideToNSString( 223 [cancelButton_ sizeToFit];
200 delegate->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_OK))]; 224
201 [okButton sizeToFit]; 225 NSRect cancelFrame = [cancelButton_ frame];
202 [okButton setAutoresizingMask:NSViewMinXMargin]; 226 float cancelWidth = cancelFrame.size.width + cancelMargin;
203 [okButton setTarget:self]; 227
204 [okButton setAction:@selector(ok:)]; 228 // Position the cancel button to the left of the close button.
229 // The appropriate margin is already built into cancelWidth.
230 cancelFrame.origin.x = NSMinX([closeButton_ frame]) - cancelWidth;
231 [cancelButton_ setFrame:cancelFrame];
232
233 // Resize the label box to extend all the way to the cancel button,
234 // minus the saved margin, but only if we're not also adding an OK button.
235 if (!(visibleButtons & ConfirmInfoBarDelegate::BUTTON_OK)) {
236 NSRect labelFrame = [label_ frame];
237 labelFrame.size.width =
238 NSMinX(cancelFrame) - NSMinX(labelFrame) - labelMargin;
239 [label_ setFrame:labelFrame];
240 }
241 } else {
242 [cancelButton_ removeFromSuperview];
205 } 243 }
206 244
207 // Create the cancel button if needed. 245 // Create and position the OK button if needed. Otherwise, hide it.
208 if (visibleButtons & ConfirmInfoBarDelegate::BUTTON_CANCEL) { 246 if (visibleButtons & ConfirmInfoBarDelegate::BUTTON_OK) {
209 cancelButton = [[[NSButton alloc] initWithFrame:NSZeroRect] autorelease]; 247 [okButton_ setTitle:base::SysWideToNSString(
210 [cancelButton setBezelStyle:NSRoundedBezelStyle]; 248 delegate->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_OK))];
211 [cancelButton setTitle:base::SysWideToNSString( 249 [okButton_ sizeToFit];
212 delegate->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_CANCEL))];
213 [cancelButton sizeToFit];
214 [cancelButton setAutoresizingMask:NSViewMinXMargin];
215 [cancelButton setTarget:self];
216 [cancelButton setAction:@selector(cancel:)];
217 }
218 250
219 // Position the cancel button, if it exists. 251 NSRect okFrame = [okButton_ frame];
220 int cancelWidth = 0; 252 int okWidth = okFrame.size.width + okMargin;
221 if (cancelButton) {
222 NSRect cancelFrame = [cancelButton frame];
223 cancelWidth = cancelFrame.size.width + 10;
224 253
225 // Position the cancel button to the left of the close button. A 10px 254 // Position the OK button to the left of the cancel button, if
226 // margin is already built into cancelWidth. 255 // present. Otherwise, position it relative to the close button.
227 cancelFrame.origin.x = NSMinX([closeButton_ frame]) - cancelWidth; 256 float relativeX = (visibleButtons & ConfirmInfoBarDelegate::BUTTON_CANCEL) ?
228 cancelFrame.origin.y = 0; 257 NSMinX([cancelButton_ frame]) :
229 [cancelButton setFrame:cancelFrame]; 258 NSMinX([closeButton_ frame]);
230 [[self view] addSubview:cancelButton];
231 259
232 // Resize the label box to extend all the way to the cancel button, 260 // The appropriate margin is already built into okWidth.
233 // minus a 10px argin. 261 okFrame.origin.x = relativeX - okWidth;
234 NSRect labelFrame = [label_ frame]; 262 [okButton_ setFrame:okFrame];
235 labelFrame.size.width = NSMinX(cancelFrame) - 10 - NSMinX(labelFrame);
236 [label_ setFrame:labelFrame];
237 }
238
239 // Position the OK button, if it exists.
240 if (okButton) {
241 NSRect okFrame = [okButton frame];
242 int okWidth = okFrame.size.width + 10;
243
244 // Position the OK button to the left of the close button as
245 // well. If a cancel button is present, |cancelWidth| will be positive.
246 // In either case, a 10px margin is built into okWidth.
247 okFrame.origin.x =
248 NSMinX([closeButton_ frame]) - cancelWidth - okWidth;
249 okFrame.origin.y = 0;
250 [okButton setFrame:okFrame];
251 [[self view] addSubview:okButton];
252 263
253 // Resize the label box to extend all the way to the OK button, 264 // Resize the label box to extend all the way to the OK button,
254 // minus a 10px argin. 265 // minus the saved margin.
255 NSRect labelFrame = [label_ frame]; 266 NSRect labelFrame = [label_ frame];
256 labelFrame.size.width = NSMinX(okFrame) - 10 - NSMinX(labelFrame); 267 labelFrame.size.width = NSMinX(okFrame) - NSMinX(labelFrame) - labelMargin;
257 [label_ setFrame:labelFrame]; 268 [label_ setFrame:labelFrame];
269 } else {
270 [okButton_ removeFromSuperview];
258 } 271 }
259 } 272 }
260 273
261 @end 274 @end
262 275
263 276
264 ////////////////////////////////////////////////////////////////////////// 277 //////////////////////////////////////////////////////////////////////////
265 // CreateInfoBar() implementations 278 // CreateInfoBar() implementations
266 279
267 InfoBar* AlertInfoBarDelegate::CreateInfoBar() { 280 InfoBar* AlertInfoBarDelegate::CreateInfoBar() {
268 AlertInfoBarController* controller = 281 AlertInfoBarController* controller =
269 [[AlertInfoBarController alloc] initWithDelegate:this]; 282 [[AlertInfoBarController alloc] initWithDelegate:this];
270 return new InfoBar(controller); 283 return new InfoBar(controller);
271 } 284 }
272 285
273 InfoBar* LinkInfoBarDelegate::CreateInfoBar() { 286 InfoBar* LinkInfoBarDelegate::CreateInfoBar() {
274 LinkInfoBarController* controller = 287 LinkInfoBarController* controller =
275 [[LinkInfoBarController alloc] initWithDelegate:this]; 288 [[LinkInfoBarController alloc] initWithDelegate:this];
276 return new InfoBar(controller); 289 return new InfoBar(controller);
277 } 290 }
278 291
279 InfoBar* ConfirmInfoBarDelegate::CreateInfoBar() { 292 InfoBar* ConfirmInfoBarDelegate::CreateInfoBar() {
280 ConfirmInfoBarController* controller = 293 ConfirmInfoBarController* controller =
281 [[ConfirmInfoBarController alloc] initWithDelegate:this]; 294 [[ConfirmInfoBarController alloc] initWithDelegate:this];
282 return new InfoBar(controller); 295 return new InfoBar(controller);
283 } 296 }
OLDNEW
« no previous file with comments | « chrome/browser/cocoa/infobar_controller.h ('k') | chrome/browser/cocoa/infobar_gradient_view.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698