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

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: nit Created 6 years, 8 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_ setFrame:NSMakeRect(0, 0, kCheckboxMaxWidth, y_pos)];
145 [scroll_view setDocumentView:checkbox_container_];
146
147 NSRect checkboxes_frame =
148 NSMakeRect(0, 0, [scroll_view contentSize].width, y_pos);
149 [checkbox_container_ setFrame:checkboxes_frame];
184 150
185 // Resize to pack the scroll view if possible. 151 // Resize to pack the scroll view if possible.
186 NSRect scroll_frame = [scroll_view frame]; 152 NSRect scroll_frame = [scroll_view frame];
187 if (NSHeight(scroll_frame) > NSHeight([checkbox_container_ frame])) { 153 if (NSHeight(scroll_frame) > NSHeight(checkboxes_frame)) {
188 scroll_frame.size.height = NSHeight([checkbox_container_ frame]); 154 scroll_frame.size.height = NSHeight(checkboxes_frame);
189 [scroll_view setFrame:scroll_frame]; 155 [scroll_view setFrame:scroll_frame];
190 } 156 }
191 157
192 [main_container_ setFrameFromContentFrame:scroll_frame]; 158 [main_container_ setFrameFromContentFrame:scroll_frame];
159 NSRect main_frame = [main_container_ frame];
groby-ooo-7-16 2014/04/23 21:57:58 [main_container_ setFrameOrigin:NSZeroPoint];
vandebo (ex-Chrome) 2014/04/25 20:10:56 Done.
160 main_frame.origin = NSZeroPoint;
161 [main_container_ setFrame:main_frame];
193 [alert_ setAccessoryView:main_container_]; 162 [alert_ setAccessoryView:main_container_];
194 163
195 // As a safeguard against the user skipping reading over the dialog and just 164 // 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 165 // confirming, the button will be unavailable for dialogs without any checks
197 // until the user toggles something. 166 // until the user toggles something.
198 [[[alert_ buttons] objectAtIndex:0] setEnabled: 167 [[[alert_ buttons] objectAtIndex:0] setEnabled:
199 controller_->IsAcceptAllowed()]; 168 controller_->IsAcceptAllowed()];
200 169
201 [alert_ layout]; 170 [alert_ layout];
202 } 171 }
203 172
204 CGFloat MediaGalleriesDialogCocoa::CreateCheckboxes( 173 CGFloat MediaGalleriesDialogCocoa::CreateCheckboxes(
205 CGFloat y_pos, 174 CGFloat y_pos,
206 const MediaGalleriesDialogController::GalleryPermissionsVector& 175 const MediaGalleriesDialogController::GalleryPermissionsVector&
207 permissions) { 176 permissions) {
208 y_pos += kCheckboxMargin;
209
210 for (MediaGalleriesDialogController::GalleryPermissionsVector:: 177 for (MediaGalleriesDialogController::GalleryPermissionsVector::
211 const_iterator iter = permissions.begin(); 178 const_iterator iter = permissions.begin();
212 iter != permissions.end(); iter++) { 179 iter != permissions.end(); iter++) {
213 const MediaGalleriesDialogController::GalleryPermission& permission = *iter; 180 const MediaGalleriesDialogController::GalleryPermission& permission = *iter;
214 UpdateGalleryCheckbox(permission, y_pos); 181 base::scoped_nsobject<MediaGalleryListEntry> checkbox_entry(
215 y_pos = NSMaxY([[checkboxes_ lastObject] frame]) + kCheckboxMargin; 182 [[MediaGalleryListEntry alloc]
183 initWithFrame:NSZeroRect
184 controller:this
185 prefId:permission.gallery_id
groby-ooo-7-16 2014/04/23 21:57:58 I'd suggest just initializing it with the permissi
vandebo (ex-Chrome) 2014/04/25 20:10:56 For the two call sites, there are two of these par
186 galleryName:permission.pref_info.GetGalleryDisplayName()
187 subscript:permission.pref_info.GetGalleryAdditionalDetails()
188 tooltip:permission.pref_info.GetGalleryTooltip()
189 showFolderViewer:false]);
190
191 [checkbox_entry setState:permission.allowed];
groby-ooo-7-16 2014/04/23 21:57:58 ... which'd also allow to roll the setState: into
192
193 NSRect entry_rect = [checkbox_entry frame];
194 entry_rect.origin.y = y_pos;
195 [checkbox_entry setFrame:entry_rect];
groby-ooo-7-16 2014/04/23 21:57:58 setFrameOrigin: does just set the origin.
vandebo (ex-Chrome) 2014/04/25 20:10:56 Done.
196 [checkbox_container_ addSubview:checkbox_entry];
197 y_pos = NSMaxY(entry_rect) + kCheckboxMargin;
216 } 198 }
217 199
218 return y_pos; 200 return y_pos;
219 } 201 }
220 202
221 CGFloat MediaGalleriesDialogCocoa::CreateCheckboxSeparator(CGFloat y_pos) { 203 CGFloat MediaGalleriesDialogCocoa::CreateCheckboxSeparator(CGFloat y_pos) {
222 base::scoped_nsobject<NSBox> separator( 204 base::scoped_nsobject<NSBox> separator(
223 [[NSBox alloc] initWithFrame:NSMakeRect( 205 [[NSBox alloc] initWithFrame:NSMakeRect(
224 0, y_pos + kCheckboxMargin * 0.5, kCheckboxMaxWidth, 1.0)]); 206 0, y_pos + kCheckboxMargin * 0.5, kCheckboxMaxWidth, 1.0)]);
225 [separator setBoxType:NSBoxSeparator]; 207 [separator setBoxType:NSBoxSeparator];
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 243
262 void MediaGalleriesDialogCocoa::OnCancelClicked() { 244 void MediaGalleriesDialogCocoa::OnCancelClicked() {
263 if (window_) 245 if (window_)
264 window_->CloseWebContentsModalDialog(); 246 window_->CloseWebContentsModalDialog();
265 } 247 }
266 248
267 void MediaGalleriesDialogCocoa::OnAddFolderClicked() { 249 void MediaGalleriesDialogCocoa::OnAddFolderClicked() {
268 controller_->OnAddFolderClicked(); 250 controller_->OnAddFolderClicked();
269 } 251 }
270 252
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() { 253 void MediaGalleriesDialogCocoa::UpdateGalleries() {
342 InitDialogControls(); 254 InitDialogControls();
343 } 255 }
344 256
345 void MediaGalleriesDialogCocoa::OnConstrainedWindowClosed( 257 void MediaGalleriesDialogCocoa::OnConstrainedWindowClosed(
346 ConstrainedWindowMac* window) { 258 ConstrainedWindowMac* window) {
347 controller_->DialogFinished(accepted_); 259 controller_->DialogFinished(accepted_);
348 } 260 }
349 261
262 void MediaGalleriesDialogCocoa::OnCheckboxToggled(GalleryDialogId gallery_id,
263 bool checked) {
264 controller_->DidToggleGallery(gallery_id, checked);
265
266 [[[alert_ buttons] objectAtIndex:0] setEnabled:
267 controller_->IsAcceptAllowed()];
268 }
269
350 ui::MenuModel* MediaGalleriesDialogCocoa::GetContextMenu( 270 ui::MenuModel* MediaGalleriesDialogCocoa::GetContextMenu(
351 GalleryDialogId gallery_id) { 271 GalleryDialogId gallery_id) {
352 return controller_->GetContextMenu(gallery_id); 272 return controller_->GetContextMenu(gallery_id);
353 } 273 }
354 274
355 // static 275 // static
356 MediaGalleriesDialog* MediaGalleriesDialog::Create( 276 MediaGalleriesDialog* MediaGalleriesDialog::Create(
357 MediaGalleriesDialogController* controller) { 277 MediaGalleriesDialogController* controller) {
358 base::scoped_nsobject<MediaGalleriesCocoaController> cocoa_controller( 278 base::scoped_nsobject<MediaGalleriesCocoaController> cocoa_controller(
359 [[MediaGalleriesCocoaController alloc] init]); 279 [[MediaGalleriesCocoaController alloc] init]);
360 return new MediaGalleriesDialogCocoa(controller, cocoa_controller); 280 return new MediaGalleriesDialogCocoa(controller, cocoa_controller);
361 } 281 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698