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/views/extensions/media_galleries_dialog_views.h" | 5 #include "chrome/browser/ui/views/extensions/media_galleries_dialog_views.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/browser/ui/views/constrained_window_views.h" | 8 #include "chrome/browser/ui/views/constrained_window_views.h" |
9 #include "chrome/browser/ui/views/extensions/media_gallery_checkbox_view.h" | 9 #include "chrome/browser/ui/views/extensions/media_gallery_checkbox_view.h" |
10 #include "content/public/browser/web_contents.h" | 10 #include "content/public/browser/web_contents.h" |
11 #include "grit/generated_resources.h" | 11 #include "grit/generated_resources.h" |
12 #include "grit/locale_settings.h" | 12 #include "grit/locale_settings.h" |
13 #include "ui/base/l10n/l10n_util.h" | 13 #include "ui/base/l10n/l10n_util.h" |
14 #include "ui/native_theme/native_theme.h" | 14 #include "ui/native_theme/native_theme.h" |
15 #include "ui/views/border.h" | 15 #include "ui/views/border.h" |
16 #include "ui/views/controls/button/checkbox.h" | 16 #include "ui/views/controls/button/checkbox.h" |
| 17 #include "ui/views/controls/button/image_button.h" |
17 #include "ui/views/controls/button/label_button.h" | 18 #include "ui/views/controls/button/label_button.h" |
18 #include "ui/views/controls/label.h" | 19 #include "ui/views/controls/label.h" |
19 #include "ui/views/controls/menu/menu_runner.h" | 20 #include "ui/views/controls/menu/menu_runner.h" |
20 #include "ui/views/controls/scroll_view.h" | 21 #include "ui/views/controls/scroll_view.h" |
21 #include "ui/views/controls/separator.h" | 22 #include "ui/views/controls/separator.h" |
22 #include "ui/views/layout/box_layout.h" | 23 #include "ui/views/layout/box_layout.h" |
23 #include "ui/views/layout/grid_layout.h" | 24 #include "ui/views/layout/grid_layout.h" |
24 #include "ui/views/layout/layout_constants.h" | 25 #include "ui/views/layout/layout_constants.h" |
25 #include "ui/views/view.h" | 26 #include "ui/views/view.h" |
26 #include "ui/views/widget/widget.h" | 27 #include "ui/views/widget/widget.h" |
(...skipping 23 matching lines...) Expand all Loading... |
50 width = parent()->width(); | 51 width = parent()->width(); |
51 height = std::max(parent()->height(), height); | 52 height = std::max(parent()->height(), height); |
52 } | 53 } |
53 SetBounds(x(), y(), width, height); | 54 SetBounds(x(), y(), width, height); |
54 | 55 |
55 views::View::Layout(); | 56 views::View::Layout(); |
56 } | 57 } |
57 | 58 |
58 } // namespace | 59 } // namespace |
59 | 60 |
60 typedef MediaGalleriesDialogController::GalleryPermissionsVector | |
61 GalleryPermissionsVector; | |
62 | |
63 MediaGalleriesDialogViews::MediaGalleriesDialogViews( | 61 MediaGalleriesDialogViews::MediaGalleriesDialogViews( |
64 MediaGalleriesDialogController* controller) | 62 MediaGalleriesDialogController* controller) |
65 : controller_(controller), | 63 : controller_(controller), |
66 contents_(new views::View()), | 64 contents_(new views::View()), |
67 add_gallery_button_(NULL), | 65 auxiliary_button_(NULL), |
68 confirm_available_(false), | 66 confirm_available_(false), |
69 accepted_(false) { | 67 accepted_(false) { |
70 InitChildViews(); | 68 InitChildViews(); |
71 if (ControllerHasWebContents()) | 69 if (ControllerHasWebContents()) |
72 ShowWebModalDialogViews(this, controller->web_contents()); | 70 ShowWebModalDialogViews(this, controller->WebContents()); |
73 } | 71 } |
74 | 72 |
75 MediaGalleriesDialogViews::~MediaGalleriesDialogViews() { | 73 MediaGalleriesDialogViews::~MediaGalleriesDialogViews() { |
76 if (!ControllerHasWebContents()) | 74 if (!ControllerHasWebContents()) |
77 delete contents_; | 75 delete contents_; |
78 } | 76 } |
79 | 77 |
80 void MediaGalleriesDialogViews::InitChildViews() { | 78 void MediaGalleriesDialogViews::InitChildViews() { |
81 // Outer dialog layout. | 79 // Outer dialog layout. |
82 contents_->RemoveAllChildViews(true); | 80 contents_->RemoveAllChildViews(true); |
| 81 checkbox_map_.clear(); |
| 82 |
83 int dialog_content_width = views::Widget::GetLocalizedContentsWidth( | 83 int dialog_content_width = views::Widget::GetLocalizedContentsWidth( |
84 IDS_MEDIA_GALLERIES_DIALOG_CONTENT_WIDTH_CHARS); | 84 IDS_MEDIA_GALLERIES_DIALOG_CONTENT_WIDTH_CHARS); |
85 views::GridLayout* layout = views::GridLayout::CreatePanel(contents_); | 85 views::GridLayout* layout = views::GridLayout::CreatePanel(contents_); |
86 contents_->SetLayoutManager(layout); | 86 contents_->SetLayoutManager(layout); |
87 | 87 |
88 int column_set_id = 0; | 88 int column_set_id = 0; |
89 views::ColumnSet* columns = layout->AddColumnSet(column_set_id); | 89 views::ColumnSet* columns = layout->AddColumnSet(column_set_id); |
90 columns->AddColumn(views::GridLayout::LEADING, | 90 columns->AddColumn(views::GridLayout::LEADING, |
91 views::GridLayout::LEADING, | 91 views::GridLayout::LEADING, |
92 1, | 92 1, |
(...skipping 16 matching lines...) Expand all Loading... |
109 ScrollableView* scroll_container = new ScrollableView(); | 109 ScrollableView* scroll_container = new ScrollableView(); |
110 scroll_container->SetLayoutManager(new views::BoxLayout( | 110 scroll_container->SetLayoutManager(new views::BoxLayout( |
111 views::BoxLayout::kVertical, 0, 0, | 111 views::BoxLayout::kVertical, 0, 0, |
112 views::kRelatedControlSmallVerticalSpacing)); | 112 views::kRelatedControlSmallVerticalSpacing)); |
113 scroll_container->SetBorder( | 113 scroll_container->SetBorder( |
114 views::Border::CreateEmptyBorder(views::kRelatedControlVerticalSpacing, | 114 views::Border::CreateEmptyBorder(views::kRelatedControlVerticalSpacing, |
115 0, | 115 0, |
116 views::kRelatedControlVerticalSpacing, | 116 views::kRelatedControlVerticalSpacing, |
117 0)); | 117 0)); |
118 | 118 |
119 // Add attached galleries checkboxes. | 119 std::vector<base::string16> section_headers = |
120 checkbox_map_.clear(); | 120 controller_->GetSectionHeaders(); |
121 GalleryPermissionsVector permissions = controller_->AttachedPermissions(); | 121 for (size_t i = 0; i < section_headers.size(); i++) { |
122 for (GalleryPermissionsVector::const_iterator iter = permissions.begin(); | 122 MediaGalleriesDialogController::Entries entries = |
123 iter != permissions.end(); ++iter) { | 123 controller_->GetSectionEntries(i); |
124 int spacing = 0; | |
125 if (iter + 1 == permissions.end()) | |
126 spacing = views::kRelatedControlSmallVerticalSpacing; | |
127 AddOrUpdateGallery(*iter, scroll_container, spacing); | |
128 } | |
129 | 124 |
130 GalleryPermissionsVector unattached_permissions = | 125 // Header and separator line. |
131 controller_->UnattachedPermissions(); | 126 if (!section_headers[i].empty() && !entries.empty()) { |
| 127 views::Separator* separator = new views::Separator( |
| 128 views::Separator::HORIZONTAL); |
| 129 scroll_container->AddChildView(separator); |
132 | 130 |
133 if (!unattached_permissions.empty()) { | 131 views::Label* header = new views::Label(section_headers[i]); |
134 // Separator line. | 132 header->SetMultiLine(true); |
135 views::Separator* separator = new views::Separator( | 133 header->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
136 views::Separator::HORIZONTAL); | 134 header->SetBorder(views::Border::CreateEmptyBorder( |
137 scroll_container->AddChildView(separator); | 135 views::kRelatedControlVerticalSpacing, |
| 136 views::kPanelHorizMargin, |
| 137 views::kRelatedControlVerticalSpacing, |
| 138 0)); |
| 139 scroll_container->AddChildView(header); |
| 140 } |
138 | 141 |
139 // Unattached locations section. | 142 // Checkboxes. |
140 views::Label* unattached_text = new views::Label( | 143 MediaGalleriesDialogController::Entries::const_iterator iter; |
141 controller_->GetUnattachedLocationsHeader()); | 144 for (iter = entries.begin(); iter != entries.end(); ++iter) { |
142 unattached_text->SetMultiLine(true); | 145 int spacing = 0; |
143 unattached_text->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 146 if (iter + 1 == entries.end()) |
144 unattached_text->SetBorder( | 147 spacing = views::kRelatedControlSmallVerticalSpacing; |
145 views::Border::CreateEmptyBorder(views::kRelatedControlVerticalSpacing, | 148 AddOrUpdateGallery(*iter, scroll_container, spacing); |
146 views::kPanelHorizMargin, | |
147 views::kRelatedControlVerticalSpacing, | |
148 0)); | |
149 scroll_container->AddChildView(unattached_text); | |
150 | |
151 // Add unattached galleries checkboxes. | |
152 for (GalleryPermissionsVector::const_iterator iter = | |
153 unattached_permissions.begin(); | |
154 iter != unattached_permissions.end(); ++iter) { | |
155 AddOrUpdateGallery(*iter, scroll_container, 0); | |
156 } | 149 } |
157 } | 150 } |
158 | 151 |
159 confirm_available_ = controller_->IsAcceptAllowed(); | 152 confirm_available_ = controller_->IsAcceptAllowed(); |
160 | 153 |
161 // Add the scrollable area to the outer dialog view. It will squeeze against | 154 // Add the scrollable area to the outer dialog view. It will squeeze against |
162 // the title/subtitle and buttons to occupy all available space in the dialog. | 155 // the title/subtitle and buttons to occupy all available space in the dialog. |
163 views::ScrollView* scroll_view = | 156 views::ScrollView* scroll_view = |
164 views::ScrollView::CreateScrollViewWithBorder(); | 157 views::ScrollView::CreateScrollViewWithBorder(); |
165 scroll_view->SetContents(scroll_container); | 158 scroll_view->SetContents(scroll_container); |
166 layout->StartRowWithPadding(1, column_set_id, | 159 layout->StartRowWithPadding(1, column_set_id, |
167 0, views::kRelatedControlVerticalSpacing); | 160 0, views::kRelatedControlVerticalSpacing); |
168 layout->AddView(scroll_view, 1, 1, | 161 layout->AddView(scroll_view, 1, 1, |
169 views::GridLayout::FILL, views::GridLayout::FILL, | 162 views::GridLayout::FILL, views::GridLayout::FILL, |
170 dialog_content_width, kScrollAreaHeight); | 163 dialog_content_width, kScrollAreaHeight); |
171 } | 164 } |
172 | 165 |
173 void MediaGalleriesDialogViews::UpdateGalleries() { | 166 void MediaGalleriesDialogViews::UpdateGalleries() { |
174 InitChildViews(); | 167 InitChildViews(); |
175 contents_->Layout(); | 168 contents_->Layout(); |
176 } | 169 } |
177 | 170 |
178 bool MediaGalleriesDialogViews::AddOrUpdateGallery( | 171 bool MediaGalleriesDialogViews::AddOrUpdateGallery( |
179 const MediaGalleriesDialogController::GalleryPermission& gallery, | 172 const MediaGalleriesDialogController::Entry& gallery, |
180 views::View* container, | 173 views::View* container, |
181 int trailing_vertical_space) { | 174 int trailing_vertical_space) { |
182 base::string16 label = gallery.pref_info.GetGalleryDisplayName(); | 175 base::string16 label = gallery.pref_info.GetGalleryDisplayName(); |
183 base::string16 tooltip_text = gallery.pref_info.GetGalleryTooltip(); | 176 base::string16 tooltip_text = gallery.pref_info.GetGalleryTooltip(); |
184 base::string16 details = gallery.pref_info.GetGalleryAdditionalDetails(); | 177 base::string16 details = gallery.pref_info.GetGalleryAdditionalDetails(); |
| 178 bool show_folder_viewer = controller_->ShouldShowFolderViewer(gallery); |
185 | 179 |
186 CheckboxMap::iterator iter = checkbox_map_.find(gallery.gallery_id); | 180 CheckboxMap::iterator iter = checkbox_map_.find(gallery.pref_info.pref_id); |
187 if (iter != checkbox_map_.end()) { | 181 if (iter != checkbox_map_.end()) { |
188 views::Checkbox* checkbox = iter->second->checkbox(); | 182 views::Checkbox* checkbox = iter->second->checkbox(); |
189 checkbox->SetChecked(gallery.allowed); | 183 checkbox->SetChecked(gallery.selected); |
190 checkbox->SetText(label); | 184 checkbox->SetText(label); |
191 checkbox->SetTooltipText(tooltip_text); | 185 checkbox->SetTooltipText(tooltip_text); |
192 iter->second->secondary_text()->SetText(details); | 186 iter->second->secondary_text()->SetText(details); |
193 iter->second->secondary_text()->SetVisible(details.length() > 0); | 187 iter->second->secondary_text()->SetVisible(details.length() > 0); |
| 188 iter->second->folder_viewer_button()->SetVisible(show_folder_viewer); |
194 return false; | 189 return false; |
195 } | 190 } |
196 | 191 |
197 MediaGalleryCheckboxView* gallery_view = | 192 MediaGalleryCheckboxView* gallery_view = |
198 new MediaGalleryCheckboxView(label, tooltip_text, details, false, | 193 new MediaGalleryCheckboxView(label, tooltip_text, details, |
199 trailing_vertical_space, this, this); | 194 show_folder_viewer, trailing_vertical_space, |
200 gallery_view->checkbox()->SetChecked(gallery.allowed); | 195 this, this); |
| 196 gallery_view->checkbox()->SetChecked(gallery.selected); |
201 container->AddChildView(gallery_view); | 197 container->AddChildView(gallery_view); |
202 checkbox_map_[gallery.gallery_id] = gallery_view; | 198 checkbox_map_[gallery.pref_info.pref_id] = gallery_view; |
203 | 199 |
204 return true; | 200 return true; |
205 } | 201 } |
206 | 202 |
207 base::string16 MediaGalleriesDialogViews::GetWindowTitle() const { | 203 base::string16 MediaGalleriesDialogViews::GetWindowTitle() const { |
208 return controller_->GetHeader(); | 204 return controller_->GetHeader(); |
209 } | 205 } |
210 | 206 |
211 void MediaGalleriesDialogViews::DeleteDelegate() { | 207 void MediaGalleriesDialogViews::DeleteDelegate() { |
212 controller_->DialogFinished(accepted_); | 208 controller_->DialogFinished(accepted_); |
213 } | 209 } |
214 | 210 |
215 views::Widget* MediaGalleriesDialogViews::GetWidget() { | 211 views::Widget* MediaGalleriesDialogViews::GetWidget() { |
216 return contents_->GetWidget(); | 212 return contents_->GetWidget(); |
217 } | 213 } |
218 | 214 |
219 const views::Widget* MediaGalleriesDialogViews::GetWidget() const { | 215 const views::Widget* MediaGalleriesDialogViews::GetWidget() const { |
220 return contents_->GetWidget(); | 216 return contents_->GetWidget(); |
221 } | 217 } |
222 | 218 |
223 views::View* MediaGalleriesDialogViews::GetContentsView() { | 219 views::View* MediaGalleriesDialogViews::GetContentsView() { |
224 return contents_; | 220 return contents_; |
225 } | 221 } |
226 | 222 |
227 base::string16 MediaGalleriesDialogViews::GetDialogButtonLabel( | 223 base::string16 MediaGalleriesDialogViews::GetDialogButtonLabel( |
228 ui::DialogButton button) const { | 224 ui::DialogButton button) const { |
229 return l10n_util::GetStringUTF16(button == ui::DIALOG_BUTTON_OK ? | 225 if (button == ui::DIALOG_BUTTON_OK) |
230 IDS_MEDIA_GALLERIES_DIALOG_CONFIRM : | 226 return controller_->GetAcceptButtonText(); |
231 IDS_MEDIA_GALLERIES_DIALOG_CANCEL); | 227 return l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_DIALOG_CANCEL); |
232 } | 228 } |
233 | 229 |
234 bool MediaGalleriesDialogViews::IsDialogButtonEnabled( | 230 bool MediaGalleriesDialogViews::IsDialogButtonEnabled( |
235 ui::DialogButton button) const { | 231 ui::DialogButton button) const { |
236 return button != ui::DIALOG_BUTTON_OK || confirm_available_; | 232 return button != ui::DIALOG_BUTTON_OK || confirm_available_; |
237 } | 233 } |
238 | 234 |
239 ui::ModalType MediaGalleriesDialogViews::GetModalType() const { | 235 ui::ModalType MediaGalleriesDialogViews::GetModalType() const { |
240 return ui::MODAL_TYPE_CHILD; | 236 return ui::MODAL_TYPE_CHILD; |
241 } | 237 } |
242 | 238 |
243 views::View* MediaGalleriesDialogViews::CreateExtraView() { | 239 views::View* MediaGalleriesDialogViews::CreateExtraView() { |
244 DCHECK(!add_gallery_button_); | 240 DCHECK(!auxiliary_button_); |
245 add_gallery_button_ = new views::LabelButton(this, | 241 base::string16 button_label = controller_->GetAuxiliaryButtonText(); |
246 l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_DIALOG_ADD_GALLERY)); | 242 if (!button_label.empty()) { |
247 add_gallery_button_->SetStyle(views::Button::STYLE_BUTTON); | 243 auxiliary_button_ = new views::LabelButton(this, button_label); |
248 return add_gallery_button_; | 244 auxiliary_button_->SetStyle(views::Button::STYLE_BUTTON); |
| 245 } |
| 246 return auxiliary_button_; |
249 } | 247 } |
250 | 248 |
251 bool MediaGalleriesDialogViews::Cancel() { | 249 bool MediaGalleriesDialogViews::Cancel() { |
252 return true; | 250 return true; |
253 } | 251 } |
254 | 252 |
255 bool MediaGalleriesDialogViews::Accept() { | 253 bool MediaGalleriesDialogViews::Accept() { |
256 accepted_ = true; | 254 accepted_ = true; |
257 return true; | 255 return true; |
258 } | 256 } |
259 | 257 |
260 void MediaGalleriesDialogViews::ButtonPressed(views::Button* sender, | 258 void MediaGalleriesDialogViews::ButtonPressed(views::Button* sender, |
261 const ui::Event& /* event */) { | 259 const ui::Event& /* event */) { |
262 confirm_available_ = true; | 260 confirm_available_ = true; |
263 | 261 |
264 if (ControllerHasWebContents()) | 262 if (ControllerHasWebContents()) |
265 GetWidget()->client_view()->AsDialogClientView()->UpdateDialogButtons(); | 263 GetWidget()->client_view()->AsDialogClientView()->UpdateDialogButtons(); |
266 | 264 |
267 if (sender == add_gallery_button_) { | 265 if (sender == auxiliary_button_) { |
268 controller_->OnAddFolderClicked(); | 266 controller_->DidClickAuxiliaryButton(); |
269 return; | 267 return; |
270 } | 268 } |
271 | 269 |
272 for (CheckboxMap::const_iterator iter = checkbox_map_.begin(); | 270 for (CheckboxMap::const_iterator iter = checkbox_map_.begin(); |
273 iter != checkbox_map_.end(); ++iter) { | 271 iter != checkbox_map_.end(); ++iter) { |
274 if (sender == iter->second->checkbox()) { | 272 if (sender == iter->second->checkbox()) { |
275 controller_->DidToggleGallery(iter->first, | 273 controller_->DidToggleEntry(iter->first, |
276 iter->second->checkbox()->checked()); | 274 iter->second->checkbox()->checked()); |
| 275 return; |
| 276 } |
| 277 if (sender == iter->second->folder_viewer_button()) { |
| 278 controller_->DidClickOpenFolderViewer(iter->first); |
277 return; | 279 return; |
278 } | 280 } |
279 } | 281 } |
280 } | 282 } |
281 | 283 |
282 void MediaGalleriesDialogViews::ShowContextMenuForView( | 284 void MediaGalleriesDialogViews::ShowContextMenuForView( |
283 views::View* source, | 285 views::View* source, |
284 const gfx::Point& point, | 286 const gfx::Point& point, |
285 ui::MenuSourceType source_type) { | 287 ui::MenuSourceType source_type) { |
286 for (CheckboxMap::const_iterator iter = checkbox_map_.begin(); | 288 for (CheckboxMap::const_iterator iter = checkbox_map_.begin(); |
287 iter != checkbox_map_.end(); ++iter) { | 289 iter != checkbox_map_.end(); ++iter) { |
288 if (iter->second->Contains(source)) { | 290 if (iter->second->Contains(source)) { |
289 ShowContextMenu(point, source_type, iter->first); | 291 ShowContextMenu(point, source_type, iter->first); |
290 return; | 292 return; |
291 } | 293 } |
292 } | 294 } |
293 } | 295 } |
294 | 296 |
295 void MediaGalleriesDialogViews::ShowContextMenu(const gfx::Point& point, | 297 void MediaGalleriesDialogViews::ShowContextMenu(const gfx::Point& point, |
296 ui::MenuSourceType source_type, | 298 ui::MenuSourceType source_type, |
297 GalleryDialogId id) { | 299 MediaGalleryPrefId id) { |
298 context_menu_runner_.reset(new views::MenuRunner( | 300 context_menu_runner_.reset(new views::MenuRunner( |
299 controller_->GetContextMenu(id))); | 301 controller_->GetContextMenu(id))); |
300 | 302 |
301 if (context_menu_runner_->RunMenuAt( | 303 if (context_menu_runner_->RunMenuAt( |
302 GetWidget(), | 304 GetWidget(), |
303 NULL, | 305 NULL, |
304 gfx::Rect(point.x(), point.y(), 0, 0), | 306 gfx::Rect(point.x(), point.y(), 0, 0), |
305 views::MENU_ANCHOR_TOPLEFT, | 307 views::MENU_ANCHOR_TOPLEFT, |
306 source_type, | 308 source_type, |
307 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU) == | 309 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU) == |
308 views::MenuRunner::MENU_DELETED) { | 310 views::MenuRunner::MENU_DELETED) { |
309 return; | 311 return; |
310 } | 312 } |
311 } | 313 } |
312 | 314 |
313 bool MediaGalleriesDialogViews::ControllerHasWebContents() const { | 315 bool MediaGalleriesDialogViews::ControllerHasWebContents() const { |
314 return controller_->web_contents() != NULL; | 316 return controller_->WebContents() != NULL; |
315 } | 317 } |
316 | 318 |
317 // MediaGalleriesDialogViewsController ----------------------------------------- | 319 // MediaGalleriesDialogViewsController ----------------------------------------- |
318 | 320 |
319 // static | 321 // static |
320 MediaGalleriesDialog* MediaGalleriesDialog::Create( | 322 MediaGalleriesDialog* MediaGalleriesDialog::Create( |
321 MediaGalleriesDialogController* controller) { | 323 MediaGalleriesDialogController* controller) { |
322 return new MediaGalleriesDialogViews(controller); | 324 return new MediaGalleriesDialogViews(controller); |
323 } | 325 } |
OLD | NEW |