OLD | NEW |
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 12 matching lines...) Expand all Loading... |
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; | 31 - (void)onAcceptButton:(id)sender; |
32 - (void)onCancelButton:(id)sender; | 32 - (void)onCancelButton:(id)sender; |
33 - (void)onAddFolderClicked:(id)sender; | 33 - (void)onAuxiliaryButton:(id)sender; |
34 | 34 |
35 @end | 35 @end |
36 | 36 |
37 @implementation MediaGalleriesCocoaController | 37 @implementation MediaGalleriesCocoaController |
38 | 38 |
39 @synthesize dialog = dialog_; | 39 @synthesize dialog = dialog_; |
40 | 40 |
41 - (void)onAcceptButton:(id)sender { | 41 - (void)onAcceptButton:(id)sender { |
42 dialog_->OnAcceptClicked(); | 42 dialog_->OnAcceptClicked(); |
43 } | 43 } |
44 | 44 |
45 - (void)onCancelButton:(id)sender { | 45 - (void)onCancelButton:(id)sender { |
46 dialog_->OnCancelClicked(); | 46 dialog_->OnCancelClicked(); |
47 } | 47 } |
48 | 48 |
49 - (void)onAddFolderClicked:(id)sender { | 49 - (void)onAuxiliaryButton:(id)sender { |
50 DCHECK(dialog_); | 50 DCHECK(dialog_); |
51 dialog_->OnAddFolderClicked(); | 51 dialog_->OnAuxiliaryButtonClicked(); |
52 } | 52 } |
53 | 53 |
54 @end | 54 @end |
55 | 55 |
56 namespace { | 56 namespace { |
57 | 57 |
58 const CGFloat kCheckboxMargin = 10; | 58 const CGFloat kCheckboxMargin = 10; |
59 const CGFloat kCheckboxMaxWidth = 440; | 59 const CGFloat kCheckboxMaxWidth = 440; |
60 const CGFloat kScrollAreaHeight = 220; | 60 const CGFloat kScrollAreaHeight = 220; |
61 | 61 |
62 } // namespace | 62 } // namespace |
63 | 63 |
64 MediaGalleriesDialogCocoa::MediaGalleriesDialogCocoa( | 64 MediaGalleriesDialogCocoa::MediaGalleriesDialogCocoa( |
65 MediaGalleriesDialogController* controller, | 65 MediaGalleriesDialogController* controller, |
66 MediaGalleriesCocoaController* cocoa_controller) | 66 MediaGalleriesCocoaController* cocoa_controller) |
67 : controller_(controller), | 67 : controller_(controller), |
68 accepted_(false), | 68 accepted_(false), |
69 cocoa_controller_([cocoa_controller retain]) { | 69 cocoa_controller_([cocoa_controller retain]) { |
70 [cocoa_controller_ setDialog:this]; | 70 [cocoa_controller_ setDialog:this]; |
71 | 71 |
72 alert_.reset([[ConstrainedWindowAlert alloc] init]); | 72 alert_.reset([[ConstrainedWindowAlert alloc] init]); |
73 | 73 |
74 [alert_ setMessageText:base::SysUTF16ToNSString(controller_->GetHeader())]; | 74 [alert_ setMessageText:base::SysUTF16ToNSString(controller_->GetHeader())]; |
75 [alert_ setInformativeText: | 75 [alert_ setInformativeText: |
76 base::SysUTF16ToNSString(controller_->GetSubtext())]; | 76 base::SysUTF16ToNSString(controller_->GetSubtext())]; |
77 [alert_ addButtonWithTitle: | 77 [alert_ addButtonWithTitle: |
78 l10n_util::GetNSString(IDS_MEDIA_GALLERIES_DIALOG_CONFIRM) | 78 base::SysUTF16ToNSString(controller_->GetAcceptButtonText()) |
79 keyEquivalent:kKeyEquivalentReturn | 79 keyEquivalent:kKeyEquivalentReturn |
80 target:cocoa_controller_ | 80 target:cocoa_controller_ |
81 action:@selector(onAcceptButton:)]; | 81 action:@selector(onAcceptButton:)]; |
82 [alert_ addButtonWithTitle: | 82 [alert_ addButtonWithTitle: |
83 l10n_util::GetNSString(IDS_MEDIA_GALLERIES_DIALOG_CANCEL) | 83 l10n_util::GetNSString(IDS_MEDIA_GALLERIES_DIALOG_CANCEL) |
84 keyEquivalent:kKeyEquivalentEscape | 84 keyEquivalent:kKeyEquivalentEscape |
85 target:cocoa_controller_ | 85 target:cocoa_controller_ |
86 action:@selector(onCancelButton:)]; | 86 action:@selector(onCancelButton:)]; |
87 [alert_ addButtonWithTitle: | 87 base::string16 auxiliaryButtonLabel = controller_->GetAuxiliaryButtonText(); |
88 l10n_util::GetNSString(IDS_MEDIA_GALLERIES_DIALOG_ADD_GALLERY) | 88 if (!auxiliaryButtonLabel.empty()) { |
89 keyEquivalent:kKeyEquivalentNone | 89 [alert_ addButtonWithTitle:base::SysUTF16ToNSString(auxiliaryButtonLabel) |
90 target:cocoa_controller_ | 90 keyEquivalent:kKeyEquivalentNone |
91 action:@selector(onAddFolderClicked:)]; | 91 target:cocoa_controller_ |
| 92 action:@selector(onAuxiliaryButton:)]; |
| 93 } |
92 [[alert_ closeButton] setTarget:cocoa_controller_]; | 94 [[alert_ closeButton] setTarget:cocoa_controller_]; |
93 [[alert_ closeButton] setAction:@selector(onCancelButton:)]; | 95 [[alert_ closeButton] setAction:@selector(onCancelButton:)]; |
94 | 96 |
95 InitDialogControls(); | 97 InitDialogControls(); |
96 | 98 |
97 // May be NULL during tests. | 99 // May be NULL during tests. |
98 if (controller->web_contents()) { | 100 if (controller->WebContents()) { |
99 base::scoped_nsobject<CustomConstrainedWindowSheet> sheet( | 101 base::scoped_nsobject<CustomConstrainedWindowSheet> sheet( |
100 [[CustomConstrainedWindowSheet alloc] | 102 [[CustomConstrainedWindowSheet alloc] |
101 initWithCustomWindow:[alert_ window]]); | 103 initWithCustomWindow:[alert_ window]]); |
102 window_.reset(new ConstrainedWindowMac( | 104 window_.reset(new ConstrainedWindowMac( |
103 this, controller->web_contents(), sheet)); | 105 this, controller->WebContents(), sheet)); |
104 } | 106 } |
105 } | 107 } |
106 | 108 |
107 MediaGalleriesDialogCocoa::~MediaGalleriesDialogCocoa() { | 109 MediaGalleriesDialogCocoa::~MediaGalleriesDialogCocoa() { |
108 } | 110 } |
109 | 111 |
110 void MediaGalleriesDialogCocoa::InitDialogControls() { | 112 void MediaGalleriesDialogCocoa::InitDialogControls() { |
111 main_container_.reset([[NSBox alloc] init]); | 113 main_container_.reset([[NSBox alloc] init]); |
112 [main_container_ setBoxType:NSBoxCustom]; | 114 [main_container_ setBoxType:NSBoxCustom]; |
113 [main_container_ setBorderType:NSLineBorder]; | 115 [main_container_ setBorderType:NSLineBorder]; |
114 [main_container_ setBorderWidth:1]; | 116 [main_container_ setBorderWidth:1]; |
115 [main_container_ setCornerRadius:0]; | 117 [main_container_ setCornerRadius:0]; |
116 [main_container_ setContentViewMargins:NSZeroSize]; | 118 [main_container_ setContentViewMargins:NSZeroSize]; |
117 [main_container_ setTitlePosition:NSNoTitle]; | 119 [main_container_ setTitlePosition:NSNoTitle]; |
118 [main_container_ setBorderColor:[NSColor colorWithCalibratedRed:0.625 | 120 [main_container_ setBorderColor:[NSColor disabledControlTextColor]]; |
119 green:0.625 | |
120 blue:0.625 | |
121 alpha:1.0]]; | |
122 | 121 |
123 base::scoped_nsobject<NSScrollView> scroll_view( | 122 base::scoped_nsobject<NSScrollView> scroll_view( |
124 [[NSScrollView alloc] initWithFrame: | 123 [[NSScrollView alloc] initWithFrame: |
125 NSMakeRect(0, 0, kCheckboxMaxWidth, kScrollAreaHeight)]); | 124 NSMakeRect(0, 0, kCheckboxMaxWidth, kScrollAreaHeight)]); |
126 [scroll_view setHasVerticalScroller:YES]; | 125 [scroll_view setHasVerticalScroller:YES]; |
127 [scroll_view setHasHorizontalScroller:NO]; | 126 [scroll_view setHasHorizontalScroller:NO]; |
128 [scroll_view setBorderType:NSNoBorder]; | 127 [scroll_view setBorderType:NSNoBorder]; |
129 [scroll_view setAutohidesScrollers:YES]; | 128 [scroll_view setAutohidesScrollers:YES]; |
130 [[main_container_ contentView] addSubview:scroll_view]; | 129 [[main_container_ contentView] addSubview:scroll_view]; |
131 | 130 |
132 // Add gallery permission checkboxes inside the scrolling view. | 131 // Add gallery checkboxes inside the scrolling view. |
133 checkbox_container_.reset([[FlippedView alloc] initWithFrame:NSZeroRect]); | 132 checkbox_container_.reset([[FlippedView alloc] initWithFrame:NSZeroRect]); |
134 | 133 |
135 CGFloat y_pos = CreateCheckboxes(0, controller_->AttachedPermissions()); | 134 std::vector<base::string16> headers = controller_->GetSectionHeaders(); |
136 | 135 CGFloat y_pos = 0; |
137 if (!controller_->UnattachedPermissions().empty()) { | 136 for (size_t i = 0; i < headers.size(); i++) { |
138 y_pos = CreateCheckboxSeparator(y_pos); | 137 MediaGalleriesDialogController::Entries entries = |
139 y_pos = CreateCheckboxes(y_pos, controller_->UnattachedPermissions()); | 138 controller_->GetSectionEntries(i); |
| 139 if (!entries.empty()) { |
| 140 if (!headers[i].empty()) { |
| 141 y_pos = CreateCheckboxSeparator(y_pos, |
| 142 base::SysUTF16ToNSString(headers[i])); |
| 143 } |
| 144 y_pos = CreateCheckboxes(y_pos, entries); |
| 145 } |
140 } | 146 } |
141 | 147 |
142 // Give the container a reasonable initial size so that the scroll_view can | 148 // Give the container a reasonable initial size so that the scroll_view can |
143 // figure out the content size. | 149 // figure out the content size. |
144 [checkbox_container_ setFrameSize:NSMakeSize(kCheckboxMaxWidth, y_pos)]; | 150 [checkbox_container_ setFrameSize:NSMakeSize(kCheckboxMaxWidth, y_pos)]; |
145 [scroll_view setDocumentView:checkbox_container_]; | 151 [scroll_view setDocumentView:checkbox_container_]; |
146 [checkbox_container_ setFrameSize:NSMakeSize([scroll_view contentSize].width, | 152 [checkbox_container_ setFrameSize:NSMakeSize([scroll_view contentSize].width, |
147 y_pos)]; | 153 y_pos)]; |
148 | 154 |
149 // Resize to pack the scroll view if possible. | 155 // Resize to pack the scroll view if possible. |
(...skipping 11 matching lines...) Expand all Loading... |
161 // confirming, the button will be unavailable for dialogs without any checks | 167 // confirming, the button will be unavailable for dialogs without any checks |
162 // until the user toggles something. | 168 // until the user toggles something. |
163 [[[alert_ buttons] objectAtIndex:0] setEnabled: | 169 [[[alert_ buttons] objectAtIndex:0] setEnabled: |
164 controller_->IsAcceptAllowed()]; | 170 controller_->IsAcceptAllowed()]; |
165 | 171 |
166 [alert_ layout]; | 172 [alert_ layout]; |
167 } | 173 } |
168 | 174 |
169 CGFloat MediaGalleriesDialogCocoa::CreateCheckboxes( | 175 CGFloat MediaGalleriesDialogCocoa::CreateCheckboxes( |
170 CGFloat y_pos, | 176 CGFloat y_pos, |
171 const MediaGalleriesDialogController::GalleryPermissionsVector& | 177 const MediaGalleriesDialogController::Entries& entries) { |
172 permissions) { | 178 for (MediaGalleriesDialogController::Entries::const_iterator iter = |
173 for (MediaGalleriesDialogController::GalleryPermissionsVector:: | 179 entries.begin(); iter != entries.end(); ++iter) { |
174 const_iterator iter = permissions.begin(); | 180 const MediaGalleriesDialogController::Entry& entry = *iter; |
175 iter != permissions.end(); iter++) { | |
176 const MediaGalleriesDialogController::GalleryPermission& permission = *iter; | |
177 base::scoped_nsobject<MediaGalleryListEntry> checkbox_entry( | 181 base::scoped_nsobject<MediaGalleryListEntry> checkbox_entry( |
178 [[MediaGalleryListEntry alloc] | 182 [[MediaGalleryListEntry alloc] |
179 initWithFrame:NSZeroRect | 183 initWithFrame:NSZeroRect |
180 controller:this | 184 controller:this |
181 prefId:permission.gallery_id | 185 prefId:entry.pref_info.pref_id |
182 galleryName:permission.pref_info.GetGalleryDisplayName() | 186 galleryName:entry.pref_info.GetGalleryDisplayName() |
183 subscript:permission.pref_info.GetGalleryAdditionalDetails() | 187 subscript:entry.pref_info.GetGalleryAdditionalDetails() |
184 tooltip:permission.pref_info.GetGalleryTooltip() | 188 tooltip:entry.pref_info.GetGalleryTooltip() |
185 showFolderViewer:false]); | 189 showFolderViewer:controller_->ShouldShowFolderViewer(entry)]); |
186 | 190 |
187 [checkbox_entry setState:permission.allowed]; | 191 [checkbox_entry setState:entry.selected]; |
188 | 192 |
189 [checkbox_entry setFrameOrigin:NSMakePoint(0, y_pos)]; | 193 [checkbox_entry setFrameOrigin:NSMakePoint(0, y_pos)]; |
190 y_pos = NSMaxY([checkbox_entry frame]) + kCheckboxMargin; | 194 y_pos = NSMaxY([checkbox_entry frame]) + kCheckboxMargin; |
191 | 195 |
192 [checkbox_container_ addSubview:checkbox_entry]; | 196 [checkbox_container_ addSubview:checkbox_entry]; |
193 } | 197 } |
194 | 198 |
195 return y_pos; | 199 return y_pos; |
196 } | 200 } |
197 | 201 |
198 CGFloat MediaGalleriesDialogCocoa::CreateCheckboxSeparator(CGFloat y_pos) { | 202 CGFloat MediaGalleriesDialogCocoa::CreateCheckboxSeparator(CGFloat y_pos, |
| 203 NSString* header) { |
199 base::scoped_nsobject<NSBox> separator( | 204 base::scoped_nsobject<NSBox> separator( |
200 [[NSBox alloc] initWithFrame:NSMakeRect( | 205 [[NSBox alloc] initWithFrame:NSMakeRect( |
201 0, y_pos + kCheckboxMargin * 0.5, kCheckboxMaxWidth, 1.0)]); | 206 0, y_pos + kCheckboxMargin * 0.5, kCheckboxMaxWidth, 1.0)]); |
202 [separator setBoxType:NSBoxSeparator]; | 207 [separator setBoxType:NSBoxSeparator]; |
203 [separator setBorderType:NSLineBorder]; | 208 [separator setBorderType:NSLineBorder]; |
204 [separator setAlphaValue:0.2]; | 209 [separator setAlphaValue:0.2]; |
205 [checkbox_container_ addSubview:separator]; | 210 [checkbox_container_ addSubview:separator]; |
206 y_pos += kCheckboxMargin * 0.5 + 4; | 211 y_pos += kCheckboxMargin * 0.5 + 4; |
207 | 212 |
208 base::scoped_nsobject<NSTextField> unattached_label( | 213 base::scoped_nsobject<NSTextField> unattached_label( |
209 [[NSTextField alloc] initWithFrame:NSZeroRect]); | 214 [[NSTextField alloc] initWithFrame:NSZeroRect]); |
210 [unattached_label setEditable:NO]; | 215 [unattached_label setEditable:NO]; |
211 [unattached_label setSelectable:NO]; | 216 [unattached_label setSelectable:NO]; |
212 [unattached_label setBezeled:NO]; | 217 [unattached_label setBezeled:NO]; |
213 [unattached_label setAttributedStringValue: | 218 [unattached_label setAttributedStringValue: |
214 constrained_window::GetAttributedLabelString( | 219 constrained_window::GetAttributedLabelString( |
215 base::SysUTF16ToNSString( | 220 header, |
216 controller_->GetUnattachedLocationsHeader()), | |
217 chrome_style::kTextFontStyle, | 221 chrome_style::kTextFontStyle, |
218 NSNaturalTextAlignment, | 222 NSNaturalTextAlignment, |
219 NSLineBreakByClipping | 223 NSLineBreakByClipping |
220 )]; | 224 )]; |
221 [unattached_label sizeToFit]; | 225 [unattached_label sizeToFit]; |
222 NSSize unattached_label_size = [unattached_label frame].size; | 226 NSSize unattached_label_size = [unattached_label frame].size; |
223 [unattached_label setFrame:NSMakeRect( | 227 [unattached_label setFrame:NSMakeRect( |
224 kCheckboxMargin, y_pos + kCheckboxMargin, | 228 kCheckboxMargin, y_pos + kCheckboxMargin, |
225 kCheckboxMaxWidth, unattached_label_size.height)]; | 229 kCheckboxMaxWidth, unattached_label_size.height)]; |
226 [checkbox_container_ addSubview:unattached_label]; | 230 [checkbox_container_ addSubview:unattached_label]; |
227 y_pos = NSMaxY([unattached_label frame]) + kCheckboxMargin; | 231 y_pos = NSMaxY([unattached_label frame]) + kCheckboxMargin; |
228 | 232 |
229 return y_pos; | 233 return y_pos; |
230 } | 234 } |
231 | 235 |
232 void MediaGalleriesDialogCocoa::OnAcceptClicked() { | 236 void MediaGalleriesDialogCocoa::OnAcceptClicked() { |
233 accepted_ = true; | 237 accepted_ = true; |
234 | 238 |
235 if (window_) | 239 if (window_) |
236 window_->CloseWebContentsModalDialog(); | 240 window_->CloseWebContentsModalDialog(); |
237 } | 241 } |
238 | 242 |
239 void MediaGalleriesDialogCocoa::OnCancelClicked() { | 243 void MediaGalleriesDialogCocoa::OnCancelClicked() { |
240 if (window_) | 244 if (window_) |
241 window_->CloseWebContentsModalDialog(); | 245 window_->CloseWebContentsModalDialog(); |
242 } | 246 } |
243 | 247 |
244 void MediaGalleriesDialogCocoa::OnAddFolderClicked() { | 248 void MediaGalleriesDialogCocoa::OnAuxiliaryButtonClicked() { |
245 controller_->OnAddFolderClicked(); | 249 controller_->DidClickAuxiliaryButton(); |
246 } | 250 } |
247 | 251 |
248 void MediaGalleriesDialogCocoa::UpdateGalleries() { | 252 void MediaGalleriesDialogCocoa::UpdateGalleries() { |
249 InitDialogControls(); | 253 InitDialogControls(); |
250 } | 254 } |
251 | 255 |
252 void MediaGalleriesDialogCocoa::OnConstrainedWindowClosed( | 256 void MediaGalleriesDialogCocoa::OnConstrainedWindowClosed( |
253 ConstrainedWindowMac* window) { | 257 ConstrainedWindowMac* window) { |
254 controller_->DialogFinished(accepted_); | 258 controller_->DialogFinished(accepted_); |
255 } | 259 } |
256 | 260 |
257 void MediaGalleriesDialogCocoa::OnCheckboxToggled(GalleryDialogId gallery_id, | 261 void MediaGalleriesDialogCocoa::OnCheckboxToggled(MediaGalleryPrefId pref_id, |
258 bool checked) { | 262 bool checked) { |
259 controller_->DidToggleGallery(gallery_id, checked); | 263 controller_->DidToggleEntry(pref_id, checked); |
260 | 264 |
261 [[[alert_ buttons] objectAtIndex:0] setEnabled: | 265 [[[alert_ buttons] objectAtIndex:0] setEnabled: |
262 controller_->IsAcceptAllowed()]; | 266 controller_->IsAcceptAllowed()]; |
263 } | 267 } |
264 | 268 |
| 269 void MediaGalleriesDialogCocoa::OnFolderViewerClicked( |
| 270 MediaGalleryPrefId prefId) { |
| 271 controller_->DidClickOpenFolderViewer(prefId); |
| 272 } |
| 273 |
265 ui::MenuModel* MediaGalleriesDialogCocoa::GetContextMenu( | 274 ui::MenuModel* MediaGalleriesDialogCocoa::GetContextMenu( |
266 GalleryDialogId gallery_id) { | 275 MediaGalleryPrefId pref_id) { |
267 return controller_->GetContextMenu(gallery_id); | 276 return controller_->GetContextMenu(pref_id); |
268 } | 277 } |
269 | 278 |
270 // static | 279 // static |
271 MediaGalleriesDialog* MediaGalleriesDialog::Create( | 280 MediaGalleriesDialog* MediaGalleriesDialog::Create( |
272 MediaGalleriesDialogController* controller) { | 281 MediaGalleriesDialogController* controller) { |
273 base::scoped_nsobject<MediaGalleriesCocoaController> cocoa_controller( | 282 base::scoped_nsobject<MediaGalleriesCocoaController> cocoa_controller( |
274 [[MediaGalleriesCocoaController alloc] init]); | 283 [[MediaGalleriesCocoaController alloc] init]); |
275 return new MediaGalleriesDialogCocoa(controller, cocoa_controller); | 284 return new MediaGalleriesDialogCocoa(controller, cocoa_controller); |
276 } | 285 } |
OLD | NEW |