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

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

Issue 231873003: Refactor media galleries dialogs on cocoa to extract common code (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Layout in place Created 6 years, 7 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) 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 #include "chrome/browser/ui/cocoa/extensions/media_galleries_dialog_cocoa.h" 5 #include "chrome/browser/ui/cocoa/extensions/media_galleries_dialog_cocoa.h"
6 6
7 #include "base/mac/scoped_nsobject.h" 7 #include "base/mac/scoped_nsobject.h"
8 #include "base/strings/sys_string_conversions.h" 8 #include "base/strings/sys_string_conversions.h"
9 #include "chrome/browser/ui/chrome_style.h" 9 #include "chrome/browser/ui/chrome_style.h"
10 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_alert.h" 10 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_alert.h"
(...skipping 10 matching lines...) Expand all
21 #include "ui/base/resource/resource_bundle.h" 21 #include "ui/base/resource/resource_bundle.h"
22 22
23 // Controller for UI events on items in the media galleries dialog. 23 // Controller for UI events on items in the media galleries dialog.
24 @interface MediaGalleriesCocoaController : NSObject { 24 @interface MediaGalleriesCocoaController : NSObject {
25 @private 25 @private
26 MediaGalleriesDialogCocoa* dialog_; 26 MediaGalleriesDialogCocoa* dialog_;
27 } 27 }
28 28
29 @property(assign, nonatomic) MediaGalleriesDialogCocoa* dialog; 29 @property(assign, nonatomic) MediaGalleriesDialogCocoa* dialog;
30 30
31 - (void)onAcceptButton:(id)sender;
32 - (void)onCancelButton:(id)sender;
33 - (void)onAddFolderClicked:(id)sender;
34
31 @end 35 @end
32 36
33 @implementation MediaGalleriesCocoaController 37 @implementation MediaGalleriesCocoaController
34 38
35 @synthesize dialog = dialog_; 39 @synthesize dialog = dialog_;
36 40
37 - (void)onAcceptButton:(id)sender { 41 - (void)onAcceptButton:(id)sender {
38 dialog_->OnAcceptClicked(); 42 dialog_->OnAcceptClicked();
39 } 43 }
40 44
41 - (void)onCancelButton:(id)sender { 45 - (void)onCancelButton:(id)sender {
42 dialog_->OnCancelClicked(); 46 dialog_->OnCancelClicked();
43 } 47 }
44 48
45 - (void)onAddFolderClicked:(id)sender { 49 - (void)onAddFolderClicked:(id)sender {
46 DCHECK(dialog_); 50 DCHECK(dialog_);
47 dialog_->OnAddFolderClicked(); 51 dialog_->OnAddFolderClicked();
48 } 52 }
49 53
50 - (void)onCheckboxToggled:(id)sender {
51 DCHECK(dialog_);
52 dialog_->OnCheckboxToggled(sender);
53 }
54
55 @end
56
57
58 @interface MediaGalleriesCheckbox : NSButton {
59 @private
60 MediaGalleriesDialogCocoa* dialog_; // |dialog_| owns |this|.
61 GalleryDialogId galleryId_;
62 base::scoped_nsobject<MenuController> menuController_;
63 }
64
65 - (id)initWithFrame:(NSRect)frameRect
66 dialog:(MediaGalleriesDialogCocoa*)dialog
67 galleryId:(GalleryDialogId)galleryId;
68 - (NSMenu*)menuForEvent:(NSEvent*)theEvent;
69
70 @end
71
72 @implementation MediaGalleriesCheckbox
73
74 - (id)initWithFrame:(NSRect)frameRect
75 dialog:(MediaGalleriesDialogCocoa*)dialog
76 galleryId:(GalleryDialogId)galleryId {
77 if ((self = [super initWithFrame:frameRect])) {
78 dialog_ = dialog;
79 galleryId_ = galleryId;
80 }
81 return self;
82 }
83
84 - (NSMenu*)menuForEvent:(NSEvent*)theEvent {
85 menuController_.reset(
86 [[MenuController alloc] initWithModel:dialog_->GetContextMenu(galleryId_)
87 useWithPopUpButtonCell:NO]);
88 return [menuController_ menu];
89 }
90
91 @end 54 @end
92 55
93 namespace { 56 namespace {
94 57
95 const CGFloat kCheckboxMargin = 10; 58 const CGFloat kCheckboxMargin = 10;
96 const CGFloat kCheckboxMaxWidth = 440; 59 const CGFloat kCheckboxMaxWidth = 440;
97 const CGFloat kScrollAreaHeight = 220; 60 const CGFloat kScrollAreaHeight = 220;
98 61
99 } // namespace 62 } // namespace
100 63
101 MediaGalleriesDialogCocoa::MediaGalleriesDialogCocoa( 64 MediaGalleriesDialogCocoa::MediaGalleriesDialogCocoa(
102 MediaGalleriesDialogController* controller, 65 MediaGalleriesDialogController* controller,
103 MediaGalleriesCocoaController* cocoa_controller) 66 MediaGalleriesCocoaController* cocoa_controller)
104 : controller_(controller), 67 : controller_(controller),
105 accepted_(false), 68 accepted_(false),
106 cocoa_controller_([cocoa_controller retain]) { 69 cocoa_controller_([cocoa_controller retain]) {
107 [cocoa_controller_ setDialog:this]; 70 [cocoa_controller_ setDialog:this];
108 71
109 alert_.reset([[ConstrainedWindowAlert alloc] init]); 72 alert_.reset([[ConstrainedWindowAlert alloc] init]);
110 73
111 [alert_ setMessageText:base::SysUTF16ToNSString(controller_->GetHeader())]; 74 [alert_ setMessageText:base::SysUTF16ToNSString(controller_->GetHeader())];
112 [alert_ setInformativeText: 75 [alert_ setInformativeText:
113 base::SysUTF16ToNSString(controller_->GetSubtext())]; 76 base::SysUTF16ToNSString(controller_->GetSubtext())];
114 [alert_ addButtonWithTitle: 77 [alert_ addButtonWithTitle:
115 l10n_util::GetNSString(IDS_MEDIA_GALLERIES_DIALOG_CONFIRM) 78 l10n_util::GetNSString(IDS_MEDIA_GALLERIES_DIALOG_CONFIRM)
116 keyEquivalent:kKeyEquivalentReturn 79 keyEquivalent:kKeyEquivalentReturn
117 target:cocoa_controller_ 80 target:cocoa_controller_
118 action:@selector(onAcceptButton:)]; 81 action:@selector(onAcceptButton:)];
119 [alert_ addButtonWithTitle: 82 [alert_ addButtonWithTitle:
120 l10n_util::GetNSString(IDS_MEDIA_GALLERIES_DIALOG_CANCEL) 83 l10n_util::GetNSString(IDS_MEDIA_GALLERIES_DIALOG_CANCEL)
121 keyEquivalent:kKeyEquivalentEscape 84 keyEquivalent:kKeyEquivalentEscape
122 target:cocoa_controller_ 85 target:cocoa_controller_
123 action:@selector(onCancelButton:)]; 86 action:@selector(onCancelButton:)];
124 [alert_ addButtonWithTitle: 87 [alert_ addButtonWithTitle:
125 l10n_util::GetNSString(IDS_MEDIA_GALLERIES_DIALOG_ADD_GALLERY) 88 l10n_util::GetNSString(IDS_MEDIA_GALLERIES_DIALOG_ADD_GALLERY)
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 [[NSScrollView alloc] initWithFrame: 124 [[NSScrollView alloc] initWithFrame:
162 NSMakeRect(0, 0, kCheckboxMaxWidth, kScrollAreaHeight)]); 125 NSMakeRect(0, 0, kCheckboxMaxWidth, kScrollAreaHeight)]);
163 [scroll_view setHasVerticalScroller:YES]; 126 [scroll_view setHasVerticalScroller:YES];
164 [scroll_view setHasHorizontalScroller:NO]; 127 [scroll_view setHasHorizontalScroller:NO];
165 [scroll_view setBorderType:NSNoBorder]; 128 [scroll_view setBorderType:NSNoBorder];
166 [scroll_view setAutohidesScrollers:YES]; 129 [scroll_view setAutohidesScrollers:YES];
167 [[main_container_ contentView] addSubview:scroll_view]; 130 [[main_container_ contentView] addSubview:scroll_view];
168 131
169 // Add gallery permission checkboxes inside the scrolling view. 132 // Add gallery permission checkboxes inside the scrolling view.
170 checkbox_container_.reset([[FlippedView alloc] initWithFrame:NSZeroRect]); 133 checkbox_container_.reset([[FlippedView alloc] initWithFrame:NSZeroRect]);
171 checkboxes_.reset([[NSMutableArray alloc] init]);
172 [scroll_view setDocumentView:checkbox_container_];
173 134
174 CGFloat y_pos = 0; 135 CGFloat y_pos = CreateCheckboxes(0, controller_->AttachedPermissions());
175
176 y_pos = CreateCheckboxes(y_pos, controller_->AttachedPermissions());
177 136
178 if (!controller_->UnattachedPermissions().empty()) { 137 if (!controller_->UnattachedPermissions().empty()) {
179 y_pos = CreateCheckboxSeparator(y_pos); 138 y_pos = CreateCheckboxSeparator(y_pos);
180 y_pos = CreateCheckboxes(y_pos, controller_->UnattachedPermissions()); 139 y_pos = CreateCheckboxes(y_pos, controller_->UnattachedPermissions());
181 } 140 }
182 141
183 [checkbox_container_ setFrame:NSMakeRect(0, 0, kCheckboxMaxWidth, y_pos + 2)]; 142 // Give the container a reasonable initial size so that the scroll_view can
143 // figure out the content size.
144 [checkbox_container_ setFrameSize:NSMakeSize(kCheckboxMaxWidth, y_pos)];
145 [scroll_view setDocumentView:checkbox_container_];
146 [checkbox_container_ setFrameSize:NSMakeSize([scroll_view contentSize].width,
147 y_pos)];
184 148
185 // Resize to pack the scroll view if possible. 149 // Resize to pack the scroll view if possible.
186 NSRect scroll_frame = [scroll_view frame]; 150 NSRect scroll_frame = [scroll_view frame];
187 if (NSHeight(scroll_frame) > NSHeight([checkbox_container_ frame])) { 151 if (NSHeight(scroll_frame) > NSHeight([checkbox_container_ frame])) {
188 scroll_frame.size.height = NSHeight([checkbox_container_ frame]); 152 scroll_frame.size.height = NSHeight([checkbox_container_ frame]);
189 [scroll_view setFrame:scroll_frame]; 153 [scroll_view setFrameSize:scroll_frame.size];
190 } 154 }
191 155
192 [main_container_ setFrameFromContentFrame:scroll_frame]; 156 [main_container_ setFrameFromContentFrame:scroll_frame];
157 [main_container_ setFrameOrigin:NSZeroPoint];
groby-ooo-7-16 2014/04/28 22:17:12 Not sure you need that - it defaults to NSZeroPoin
vandebo (ex-Chrome) 2014/04/29 18:40:29 I do - setFrameFromContentFrame adjusts origin so
193 [alert_ setAccessoryView:main_container_]; 158 [alert_ setAccessoryView:main_container_];
194 159
195 // As a safeguard against the user skipping reading over the dialog and just 160 // As a safeguard against the user skipping reading over the dialog and just
196 // confirming, the button will be unavailable for dialogs without any checks 161 // confirming, the button will be unavailable for dialogs without any checks
197 // until the user toggles something. 162 // until the user toggles something.
198 [[[alert_ buttons] objectAtIndex:0] setEnabled: 163 [[[alert_ buttons] objectAtIndex:0] setEnabled:
199 controller_->IsAcceptAllowed()]; 164 controller_->IsAcceptAllowed()];
200 165
201 [alert_ layout]; 166 [alert_ layout];
202 } 167 }
203 168
204 CGFloat MediaGalleriesDialogCocoa::CreateCheckboxes( 169 CGFloat MediaGalleriesDialogCocoa::CreateCheckboxes(
205 CGFloat y_pos, 170 CGFloat y_pos,
206 const MediaGalleriesDialogController::GalleryPermissionsVector& 171 const MediaGalleriesDialogController::GalleryPermissionsVector&
207 permissions) { 172 permissions) {
208 y_pos += kCheckboxMargin;
209
210 for (MediaGalleriesDialogController::GalleryPermissionsVector:: 173 for (MediaGalleriesDialogController::GalleryPermissionsVector::
211 const_iterator iter = permissions.begin(); 174 const_iterator iter = permissions.begin();
212 iter != permissions.end(); iter++) { 175 iter != permissions.end(); iter++) {
213 const MediaGalleriesDialogController::GalleryPermission& permission = *iter; 176 const MediaGalleriesDialogController::GalleryPermission& permission = *iter;
214 UpdateGalleryCheckbox(permission, y_pos); 177 base::scoped_nsobject<MediaGalleryListEntry> checkbox_entry(
215 y_pos = NSMaxY([[checkboxes_ lastObject] frame]) + kCheckboxMargin; 178 [[MediaGalleryListEntry alloc]
179 initWithFrame:NSZeroRect
180 controller:this
181 prefId:permission.gallery_id
182 galleryName:permission.pref_info.GetGalleryDisplayName()
183 subscript:permission.pref_info.GetGalleryAdditionalDetails()
184 tooltip:permission.pref_info.GetGalleryTooltip()
185 showFolderViewer:false]);
186
187 [checkbox_entry setState:permission.allowed];
188
189 [checkbox_entry setFrameOrigin:NSMakePoint(0, y_pos)];
190 y_pos = NSMaxY([checkbox_entry frame]) + kCheckboxMargin;
191
192 [checkbox_container_ addSubview:checkbox_entry];
216 } 193 }
217 194
218 return y_pos; 195 return y_pos;
219 } 196 }
220 197
221 CGFloat MediaGalleriesDialogCocoa::CreateCheckboxSeparator(CGFloat y_pos) { 198 CGFloat MediaGalleriesDialogCocoa::CreateCheckboxSeparator(CGFloat y_pos) {
222 base::scoped_nsobject<NSBox> separator( 199 base::scoped_nsobject<NSBox> separator(
223 [[NSBox alloc] initWithFrame:NSMakeRect( 200 [[NSBox alloc] initWithFrame:NSMakeRect(
224 0, y_pos + kCheckboxMargin * 0.5, kCheckboxMaxWidth, 1.0)]); 201 0, y_pos + kCheckboxMargin * 0.5, kCheckboxMaxWidth, 1.0)]);
225 [separator setBoxType:NSBoxSeparator]; 202 [separator setBoxType:NSBoxSeparator];
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 238
262 void MediaGalleriesDialogCocoa::OnCancelClicked() { 239 void MediaGalleriesDialogCocoa::OnCancelClicked() {
263 if (window_) 240 if (window_)
264 window_->CloseWebContentsModalDialog(); 241 window_->CloseWebContentsModalDialog();
265 } 242 }
266 243
267 void MediaGalleriesDialogCocoa::OnAddFolderClicked() { 244 void MediaGalleriesDialogCocoa::OnAddFolderClicked() {
268 controller_->OnAddFolderClicked(); 245 controller_->OnAddFolderClicked();
269 } 246 }
270 247
271 void MediaGalleriesDialogCocoa::OnCheckboxToggled(NSButton* checkbox) {
272 GalleryDialogId gallery_id =
273 [[[checkbox cell] representedObject] longLongValue];
274 controller_->DidToggleGallery(gallery_id, [checkbox state] == NSOnState);
275
276 [[[alert_ buttons] objectAtIndex:0] setEnabled:
277 controller_->IsAcceptAllowed()];
278 }
279
280 void MediaGalleriesDialogCocoa::UpdateGalleryCheckbox(
281 const MediaGalleriesDialogController::GalleryPermission& gallery,
282 CGFloat y_pos) {
283 // Checkbox.
284 base::scoped_nsobject<MediaGalleriesCheckbox> checkbox(
285 [[MediaGalleriesCheckbox alloc] initWithFrame:NSZeroRect
286 dialog:this
287 galleryId:gallery.gallery_id]);
288 NSNumber* gallery_id_object =
289 [NSNumber numberWithLongLong:gallery.gallery_id];
290 [[checkbox cell] setRepresentedObject:gallery_id_object];
291 [[checkbox cell] setLineBreakMode:NSLineBreakByTruncatingMiddle];
292 [checkbox setButtonType:NSSwitchButton];
293 [checkbox setTarget:cocoa_controller_];
294 [checkbox setAction:@selector(onCheckboxToggled:)];
295 [checkboxes_ addObject:checkbox];
296
297 [checkbox setTitle:base::SysUTF16ToNSString(
298 gallery.pref_info.GetGalleryDisplayName())];
299 [checkbox setToolTip:base::SysUTF16ToNSString(
300 gallery.pref_info.GetGalleryTooltip())];
301 [checkbox setState:gallery.allowed ? NSOnState : NSOffState];
302
303 [checkbox sizeToFit];
304 NSRect rect = [checkbox bounds];
305
306 // Detail text.
307 base::scoped_nsobject<NSTextField> details(
308 [[NSTextField alloc] initWithFrame:NSZeroRect]);
309 [details setEditable:NO];
310 [details setSelectable:NO];
311 [details setBezeled:NO];
312 [details setAttributedStringValue:
313 constrained_window::GetAttributedLabelString(
314 base::SysUTF16ToNSString(
315 gallery.pref_info.GetGalleryAdditionalDetails()),
316 chrome_style::kTextFontStyle,
317 NSNaturalTextAlignment,
318 NSLineBreakByClipping
319 )];
320 [details setTextColor:[NSColor colorWithCalibratedRed:0.625
321 green:0.625
322 blue:0.625
323 alpha:1.0]];
324 [details sizeToFit];
325 NSRect details_rect = [details bounds];
326
327 // The checkbox will elide so reduce its size so it will all fit.
328 rect.size.width =
329 std::min(NSWidth(rect),
330 kCheckboxMaxWidth - 3 * kCheckboxMargin - NSWidth(details_rect));
331 rect.origin = NSMakePoint(kCheckboxMargin, y_pos);
332 [checkbox setFrame:rect];
333 [checkbox_container_ addSubview:checkbox];
334
335 details_rect.origin = NSMakePoint(NSMaxX(rect) + kCheckboxMargin, y_pos - 1);
336 [details setFrame:details_rect];
337
338 [checkbox_container_ addSubview:details];
339 }
340
341 void MediaGalleriesDialogCocoa::UpdateGalleries() { 248 void MediaGalleriesDialogCocoa::UpdateGalleries() {
342 InitDialogControls(); 249 InitDialogControls();
343 } 250 }
344 251
345 void MediaGalleriesDialogCocoa::OnConstrainedWindowClosed( 252 void MediaGalleriesDialogCocoa::OnConstrainedWindowClosed(
346 ConstrainedWindowMac* window) { 253 ConstrainedWindowMac* window) {
347 controller_->DialogFinished(accepted_); 254 controller_->DialogFinished(accepted_);
348 } 255 }
349 256
257 void MediaGalleriesDialogCocoa::OnCheckboxToggled(GalleryDialogId gallery_id,
258 bool checked) {
259 controller_->DidToggleGallery(gallery_id, checked);
260
261 [[[alert_ buttons] objectAtIndex:0] setEnabled:
262 controller_->IsAcceptAllowed()];
263 }
264
350 ui::MenuModel* MediaGalleriesDialogCocoa::GetContextMenu( 265 ui::MenuModel* MediaGalleriesDialogCocoa::GetContextMenu(
351 GalleryDialogId gallery_id) { 266 GalleryDialogId gallery_id) {
352 return controller_->GetContextMenu(gallery_id); 267 return controller_->GetContextMenu(gallery_id);
353 } 268 }
354 269
355 // static 270 // static
356 MediaGalleriesDialog* MediaGalleriesDialog::Create( 271 MediaGalleriesDialog* MediaGalleriesDialog::Create(
357 MediaGalleriesDialogController* controller) { 272 MediaGalleriesDialogController* controller) {
358 base::scoped_nsobject<MediaGalleriesCocoaController> cocoa_controller( 273 base::scoped_nsobject<MediaGalleriesCocoaController> cocoa_controller(
359 [[MediaGalleriesCocoaController alloc] init]); 274 [[MediaGalleriesCocoaController alloc] init]);
360 return new MediaGalleriesDialogCocoa(controller, cocoa_controller); 275 return new MediaGalleriesDialogCocoa(controller, cocoa_controller);
361 } 276 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698