OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/ui/cocoa/extensions/media_galleries_dialog_cocoa.h" | |
6 | |
7 #import <Cocoa/Cocoa.h> | |
Robert Sesek
2012/08/14 21:07:45
Hm. You use NSAlert and NSView in the .h but do no
sail
2012/08/14 21:38:47
Done.
| |
8 | |
9 #include "base/sys_string_conversions.h" | |
10 #include "grit/generated_resources.h" | |
11 #include "ui/base/l10n/l10n_util.h" | |
12 | |
13 const CGFloat kCheckboxMargin = 5; | |
14 const CGFloat kCheckboxMaxWidth = 350; | |
15 | |
16 @interface MediaGalleriesCocoaBridge : NSObject { | |
Robert Sesek
2012/08/14 21:07:45
@private
sail
2012/08/14 21:38:47
Done.
| |
17 chrome::MediaGalleriesDialogCocoa* dialog_; | |
18 } | |
19 | |
20 @property(nonatomic, readwrite) chrome::MediaGalleriesDialogCocoa* dialog; | |
21 | |
22 @end | |
23 | |
24 @implementation MediaGalleriesCocoaBridge | |
Robert Sesek
2012/08/14 21:07:45
naming: bridges are usually thin C++ classes to br
sail
2012/08/14 21:38:47
Done.
| |
25 | |
26 @synthesize dialog = dialog_; | |
27 | |
28 - (void)onAddFolderClicked:(id)sender { | |
29 DCHECK(dialog_); | |
30 dialog_->OnAddFolderClicked(); | |
31 } | |
32 | |
33 - (void)onCheckboxToggled:(id)sender { | |
34 DCHECK(dialog_); | |
35 dialog_->OnCheckboxToggled(sender); | |
36 } | |
37 | |
38 - (void)sheetDidEnd:(NSWindow*)parent | |
39 returnCode:(NSInteger)returnCode | |
40 context:(void*)context { | |
41 DCHECK(dialog_); | |
42 dialog_->SheetDidEnd(returnCode); | |
43 } | |
44 | |
45 @end | |
46 | |
47 namespace chrome { | |
48 | |
49 MediaGalleriesDialogCocoa::MediaGalleriesDialogCocoa( | |
50 MediaGalleriesDialogController* controller, | |
51 MediaGalleriesCocoaBridge* cocoa_bridge) | |
52 : ConstrainedWindowMacDelegateSystemSheet( | |
53 cocoa_bridge, @selector(sheetDidEnd:returnCode:context:)), | |
54 controller_(controller), | |
55 window_(NULL), | |
56 accepted_(false), | |
57 cocoa_bridge_([cocoa_bridge retain]) { | |
58 [cocoa_bridge_ setDialog:this]; | |
59 | |
60 alert_.reset([[NSAlert alloc] init]); | |
61 [alert_ setMessageText:base::SysUTF16ToNSString(controller_->GetHeader())]; | |
62 [alert_ setInformativeText:SysUTF16ToNSString(controller_->GetSubtext())]; | |
63 [alert_ addButtonWithTitle:l10n_util::GetNSString( | |
64 IDS_MEDIA_GALLERIES_DIALOG_CONFIRM)]; | |
65 [alert_ addButtonWithTitle:l10n_util::GetNSString( | |
66 IDS_MEDIA_GALLERIES_DIALOG_CANCEL)]; | |
67 [alert_ addButtonWithTitle:l10n_util::GetNSString( | |
68 IDS_MEDIA_GALLERIES_DIALOG_ADD_GALLERY)]; | |
69 | |
70 // Override the add button click handler to prevent the alert from closing. | |
71 NSButton* add_button = [[alert_ buttons] objectAtIndex:2]; | |
72 [add_button setTarget:cocoa_bridge_]; | |
73 [add_button setAction:@selector(onAddFolderClicked:)]; | |
74 | |
75 // Add gallery permission checkboxes inside an accessory view. | |
76 checkbox_container_.reset([[NSView alloc] initWithFrame:NSZeroRect]); | |
77 checkboxes_.reset([[NSMutableArray alloc] init]); | |
78 const MediaGalleriesDialogController::KnownGalleryPermissions& permissions = | |
79 controller_->permissions(); | |
80 for (MediaGalleriesDialogController::KnownGalleryPermissions:: | |
81 const_reverse_iterator iter = permissions.rbegin(); | |
82 iter != permissions.rend(); iter++) { | |
83 const MediaGalleriesDialogController::GalleryPermission& permission = | |
84 iter->second; | |
85 UpdateGalleryCheckbox(nil, &permission.pref_info, permission.allowed); | |
86 } | |
87 UpdateCheckboxContainerFrame(); | |
88 [alert_ setAccessoryView:checkbox_container_]; | |
89 | |
90 // As a safeguard against the user skipping reading over the dialog and just | |
91 // confirming, the button will be unavailable for dialogs without any checks | |
92 // until the user toggles something. | |
93 [[[alert_ buttons] objectAtIndex:0] setEnabled: | |
94 controller_->HasPermittedGalleries()]; | |
95 | |
96 set_sheet(alert_); | |
97 window_ = new ConstrainedWindowMac(controller->tab_contents(), this); | |
98 } | |
99 | |
100 MediaGalleriesDialogCocoa::~MediaGalleriesDialogCocoa() { | |
101 } | |
102 | |
103 void MediaGalleriesDialogCocoa::OnAddFolderClicked() { | |
104 controller_->OnAddFolderClicked(); | |
105 } | |
106 | |
107 void MediaGalleriesDialogCocoa::OnCheckboxToggled(NSButton* checkbox) { | |
108 const MediaGalleriesDialogController::KnownGalleryPermissions& permissions = | |
109 controller_->permissions(); | |
110 [[[alert_ buttons] objectAtIndex:0] setEnabled:YES]; | |
111 | |
112 for (MediaGalleriesDialogController::KnownGalleryPermissions:: | |
113 const_reverse_iterator iter = permissions.rbegin(); | |
114 iter != permissions.rend(); iter++) { | |
115 const MediaGalleryPrefInfo* gallery = &iter->second.pref_info; | |
116 NSString* device_id = base::SysUTF8ToNSString(gallery->device_id); | |
117 if ([[[checkbox cell] representedObject] isEqual:device_id]) { | |
118 controller_->DidToggleGallery(gallery, [checkbox state] == NSOnState); | |
119 break; | |
120 } | |
121 } | |
122 } | |
123 | |
124 void MediaGalleriesDialogCocoa::SheetDidEnd(NSInteger result) { | |
125 switch (result) { | |
126 case NSAlertFirstButtonReturn: | |
127 accepted_ = true; | |
128 window_->CloseConstrainedWindow(); | |
129 break; | |
130 case NSAlertSecondButtonReturn: | |
131 window_->CloseConstrainedWindow(); | |
132 break; | |
133 default: | |
134 NOTREACHED(); | |
135 break; | |
136 } | |
137 } | |
138 | |
139 void MediaGalleriesDialogCocoa::UpdateGalleryCheckbox( | |
140 NSButton* checkbox, | |
141 const MediaGalleryPrefInfo* gallery, | |
142 bool permitted) { | |
143 CGFloat y_pos = 0; | |
144 if (checkbox) { | |
145 y_pos = NSMaxY([checkbox frame]); | |
146 } else { | |
147 y_pos = NSMaxY([[checkboxes_ lastObject] frame]); | |
148 if ([checkboxes_ count] > 0) | |
149 y_pos += kCheckboxMargin; | |
150 | |
151 scoped_nsobject<NSButton> new_checkbox( | |
152 [[NSButton alloc] initWithFrame:NSZeroRect]); | |
153 NSString* device_id = base::SysUTF8ToNSString(gallery->device_id); | |
154 [[new_checkbox cell] setRepresentedObject:device_id]; | |
155 [[new_checkbox cell] setLineBreakMode:NSLineBreakByTruncatingMiddle]; | |
156 [new_checkbox setButtonType:NSSwitchButton]; | |
157 [new_checkbox setTarget:cocoa_bridge_]; | |
158 [new_checkbox setAction:@selector(onCheckboxToggled:)]; | |
159 | |
160 [checkbox_container_ addSubview:new_checkbox]; | |
161 [checkboxes_ addObject:new_checkbox]; | |
162 checkbox = new_checkbox.get(); | |
163 } | |
164 | |
165 [checkbox setTitle:base::SysUTF16ToNSString(gallery->display_name)]; | |
166 [checkbox setToolTip: | |
167 base::SysUTF16ToNSString(gallery->path.LossyDisplayName())]; | |
168 [checkbox setState:permitted ? NSOnState : NSOffState]; | |
169 | |
170 [checkbox sizeToFit]; | |
171 NSRect rect = [checkbox bounds]; | |
172 rect.origin.y = y_pos; | |
173 rect.size.width = std::min(rect.size.width, kCheckboxMaxWidth); | |
174 [checkbox setFrame:rect]; | |
175 } | |
176 | |
177 void MediaGalleriesDialogCocoa::UpdateCheckboxContainerFrame() { | |
178 NSRect rect = NSMakeRect( | |
179 0, 0, kCheckboxMaxWidth, NSMaxY([[checkboxes_ lastObject] frame])); | |
180 [checkbox_container_ setFrame:rect]; | |
181 } | |
182 | |
183 void MediaGalleriesDialogCocoa::UpdateGallery( | |
184 const MediaGalleryPrefInfo* gallery, | |
185 bool permitted) { | |
186 NSButton* checkbox = nil; | |
187 NSString* device_id = base::SysUTF8ToNSString(gallery->device_id); | |
188 | |
189 for (NSButton* button in checkboxes_.get()) { | |
190 if ([[[button cell] representedObject] isEqual:device_id]) { | |
191 checkbox = button; | |
192 break; | |
193 } | |
194 } | |
195 | |
196 UpdateGalleryCheckbox(checkbox, gallery, permitted); | |
197 UpdateCheckboxContainerFrame(); | |
198 [alert_ layout]; | |
199 } | |
200 | |
201 void MediaGalleriesDialogCocoa::DeleteDelegate() { | |
202 // As required by ConstrainedWindowMacDelegate, close the sheet if | |
203 // it's still open. | |
204 if (is_sheet_open()) | |
205 [NSApp endSheet:sheet()]; | |
206 | |
207 controller_->DialogFinished(accepted_); | |
208 } | |
209 | |
210 // static | |
211 MediaGalleriesDialog* MediaGalleriesDialog::Create( | |
212 MediaGalleriesDialogController* controller) { | |
213 scoped_nsobject<MediaGalleriesCocoaBridge> cocoa_bridge( | |
214 [[MediaGalleriesCocoaBridge alloc] init]); | |
215 return new MediaGalleriesDialogCocoa(controller, cocoa_bridge); | |
216 } | |
217 | |
218 } // namespace chrome | |
OLD | NEW |