OLD | NEW |
| (Empty) |
1 // Copyright 2014 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/views/extensions/media_galleries_scan_result_dialog_
views.h" | |
6 | |
7 #include "chrome/browser/ui/views/constrained_window_views.h" | |
8 #include "chrome/browser/ui/views/extensions/media_gallery_checkbox_view.h" | |
9 #include "components/web_modal/web_contents_modal_dialog_manager.h" | |
10 #include "content/public/browser/web_contents.h" | |
11 #include "grit/generated_resources.h" | |
12 #include "grit/locale_settings.h" | |
13 #include "ui/base/l10n/l10n_util.h" | |
14 #include "ui/views/border.h" | |
15 #include "ui/views/controls/button/checkbox.h" | |
16 #include "ui/views/controls/button/image_button.h" | |
17 #include "ui/views/controls/label.h" | |
18 #include "ui/views/controls/menu/menu_runner.h" | |
19 #include "ui/views/controls/scroll_view.h" | |
20 #include "ui/views/layout/box_layout.h" | |
21 #include "ui/views/layout/grid_layout.h" | |
22 #include "ui/views/layout/layout_constants.h" | |
23 #include "ui/views/view.h" | |
24 #include "ui/views/widget/widget.h" | |
25 #include "ui/views/window/dialog_client_view.h" | |
26 | |
27 typedef MediaGalleriesScanResultDialogController::OrderedScanResults | |
28 OrderedScanResults; | |
29 | |
30 namespace { | |
31 | |
32 const int kScrollAreaHeight = 192; | |
33 | |
34 // This container has the right Layout() impl to use within a ScrollView. | |
35 class ScrollableView : public views::View { | |
36 public: | |
37 ScrollableView() {} | |
38 virtual ~ScrollableView() {} | |
39 | |
40 virtual void Layout() OVERRIDE; | |
41 | |
42 private: | |
43 DISALLOW_COPY_AND_ASSIGN(ScrollableView); | |
44 }; | |
45 | |
46 void ScrollableView::Layout() { | |
47 gfx::Size pref = GetPreferredSize(); | |
48 int width = pref.width(); | |
49 int height = pref.height(); | |
50 if (parent()) { | |
51 width = parent()->width(); | |
52 height = std::max(parent()->height(), height); | |
53 } | |
54 SetBounds(x(), y(), width, height); | |
55 | |
56 views::View::Layout(); | |
57 } | |
58 | |
59 } // namespace | |
60 | |
61 MediaGalleriesScanResultDialogViews::MediaGalleriesScanResultDialogViews( | |
62 MediaGalleriesScanResultDialogController* controller) | |
63 : controller_(controller), | |
64 contents_(new views::View()), | |
65 accepted_(false) { | |
66 InitChildViews(); | |
67 ShowWebModalDialogViews(this, controller->web_contents()); | |
68 } | |
69 | |
70 MediaGalleriesScanResultDialogViews::~MediaGalleriesScanResultDialogViews() {} | |
71 | |
72 void MediaGalleriesScanResultDialogViews::InitChildViews() { | |
73 // Outer dialog layout. | |
74 contents_->RemoveAllChildViews(true); | |
75 int dialog_content_width = views::Widget::GetLocalizedContentsWidth( | |
76 IDS_MEDIA_GALLERIES_DIALOG_CONTENT_WIDTH_CHARS); | |
77 views::GridLayout* layout = views::GridLayout::CreatePanel(contents_); | |
78 contents_->SetLayoutManager(layout); | |
79 | |
80 int column_set_id = 0; | |
81 views::ColumnSet* columns = layout->AddColumnSet(column_set_id); | |
82 columns->AddColumn(views::GridLayout::LEADING, | |
83 views::GridLayout::LEADING, | |
84 1, | |
85 views::GridLayout::FIXED, | |
86 dialog_content_width, | |
87 0); | |
88 | |
89 // Message text. | |
90 views::Label* subtext = new views::Label(controller_->GetSubtext()); | |
91 subtext->SetMultiLine(true); | |
92 subtext->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
93 layout->StartRow(0, column_set_id); | |
94 layout->AddView( | |
95 subtext, 1, 1, | |
96 views::GridLayout::FILL, views::GridLayout::LEADING, | |
97 dialog_content_width, subtext->GetHeightForWidth(dialog_content_width)); | |
98 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | |
99 | |
100 // Scrollable area for checkboxes. | |
101 ScrollableView* scroll_container = new ScrollableView(); | |
102 scroll_container->SetLayoutManager(new views::BoxLayout( | |
103 views::BoxLayout::kVertical, 0, 0, | |
104 views::kRelatedControlSmallVerticalSpacing)); | |
105 scroll_container->SetBorder(views::Border::CreateEmptyBorder( | |
106 views::kRelatedControlVerticalSpacing, | |
107 0, | |
108 views::kRelatedControlVerticalSpacing, | |
109 0)); | |
110 | |
111 // Add galleries checkboxes. | |
112 gallery_view_map_.clear(); | |
113 OrderedScanResults scan_results = controller_->GetGalleryList(); | |
114 for (OrderedScanResults::const_iterator it = scan_results.begin(); | |
115 it != scan_results.end(); | |
116 ++it) { | |
117 int spacing = 0; | |
118 if (it + 1 == scan_results.end()) | |
119 spacing = views::kRelatedControlSmallVerticalSpacing; | |
120 AddOrUpdateScanResult(it->pref_info, it->selected, scroll_container, | |
121 spacing); | |
122 } | |
123 | |
124 // Add the scrollable area to the outer dialog view. It will squeeze against | |
125 // the title/subtitle and buttons to occupy all available space in the dialog. | |
126 views::ScrollView* scroll_view = | |
127 views::ScrollView::CreateScrollViewWithBorder(); | |
128 scroll_view->SetContents(scroll_container); | |
129 layout->StartRowWithPadding(1, column_set_id, | |
130 0, views::kRelatedControlVerticalSpacing); | |
131 layout->AddView(scroll_view, 1, 1, | |
132 views::GridLayout::FILL, views::GridLayout::FILL, | |
133 dialog_content_width, kScrollAreaHeight); | |
134 } | |
135 | |
136 void MediaGalleriesScanResultDialogViews::UpdateResults() { | |
137 InitChildViews(); | |
138 contents_->Layout(); | |
139 } | |
140 | |
141 bool MediaGalleriesScanResultDialogViews::AddOrUpdateScanResult( | |
142 const MediaGalleryPrefInfo& gallery, | |
143 bool selected, | |
144 views::View* container, | |
145 int trailing_vertical_space) { | |
146 base::string16 label = gallery.GetGalleryDisplayName(); | |
147 base::string16 tooltip_text = gallery.GetGalleryTooltip(); | |
148 base::string16 details = gallery.GetGalleryAdditionalDetails(); | |
149 bool is_attached = gallery.IsGalleryAvailable(); | |
150 | |
151 GalleryViewMap::iterator it = gallery_view_map_.find(gallery.pref_id); | |
152 if (it != gallery_view_map_.end()) { | |
153 views::Checkbox* checkbox = it->second->checkbox(); | |
154 checkbox->SetChecked(selected); | |
155 checkbox->SetText(label); | |
156 checkbox->SetTooltipText(tooltip_text); | |
157 it->second->folder_viewer_button()->SetVisible(is_attached); | |
158 it->second->secondary_text()->SetText(details); | |
159 it->second->secondary_text()->SetVisible(details.length() > 0); | |
160 return false; | |
161 } | |
162 | |
163 MediaGalleryCheckboxView* gallery_view = | |
164 new MediaGalleryCheckboxView(label, tooltip_text, details, is_attached, | |
165 trailing_vertical_space, this, this); | |
166 gallery_view->checkbox()->SetChecked(selected); | |
167 container->AddChildView(gallery_view); | |
168 | |
169 gallery_view_map_[gallery.pref_id] = gallery_view; | |
170 | |
171 return true; | |
172 } | |
173 | |
174 base::string16 MediaGalleriesScanResultDialogViews::GetWindowTitle() const { | |
175 return controller_->GetHeader(); | |
176 } | |
177 | |
178 void MediaGalleriesScanResultDialogViews::DeleteDelegate() { | |
179 controller_->DialogFinished(accepted_); | |
180 } | |
181 | |
182 views::Widget* MediaGalleriesScanResultDialogViews::GetWidget() { | |
183 return contents_->GetWidget(); | |
184 } | |
185 | |
186 const views::Widget* MediaGalleriesScanResultDialogViews::GetWidget() const { | |
187 return contents_->GetWidget(); | |
188 } | |
189 | |
190 views::View* MediaGalleriesScanResultDialogViews::GetContentsView() { | |
191 return contents_; | |
192 } | |
193 | |
194 base::string16 MediaGalleriesScanResultDialogViews::GetDialogButtonLabel( | |
195 ui::DialogButton button) const { | |
196 return l10n_util::GetStringUTF16(button == ui::DIALOG_BUTTON_OK ? | |
197 IDS_MEDIA_GALLERIES_SCAN_RESULT_DIALOG_CONFIRM : | |
198 IDS_MEDIA_GALLERIES_DIALOG_CANCEL); | |
199 } | |
200 | |
201 ui::ModalType MediaGalleriesScanResultDialogViews::GetModalType() const { | |
202 return ui::MODAL_TYPE_CHILD; | |
203 } | |
204 | |
205 bool MediaGalleriesScanResultDialogViews::Cancel() { | |
206 return true; | |
207 } | |
208 | |
209 bool MediaGalleriesScanResultDialogViews::Accept() { | |
210 accepted_ = true; | |
211 return true; | |
212 } | |
213 | |
214 void MediaGalleriesScanResultDialogViews::ButtonPressed( | |
215 views::Button* sender, | |
216 const ui::Event& event) { | |
217 GetWidget()->client_view()->AsDialogClientView()->UpdateDialogButtons(); | |
218 | |
219 for (GalleryViewMap::const_iterator it = gallery_view_map_.begin(); | |
220 it != gallery_view_map_.end(); ++it) { | |
221 if (sender == it->second->checkbox()) { | |
222 controller_->DidToggleGalleryId(it->first, | |
223 it->second->checkbox()->checked()); | |
224 return; | |
225 } | |
226 if (sender == it->second->folder_viewer_button()) { | |
227 controller_->DidClickOpenFolderViewer(it->first); | |
228 return; | |
229 } | |
230 } | |
231 } | |
232 | |
233 void MediaGalleriesScanResultDialogViews::ShowContextMenuForView( | |
234 views::View* source, | |
235 const gfx::Point& point, | |
236 ui::MenuSourceType source_type) { | |
237 for (GalleryViewMap::const_iterator it = gallery_view_map_.begin(); | |
238 it != gallery_view_map_.end(); ++it) { | |
239 if (it->second->Contains(source)) { | |
240 ShowContextMenu(point, source_type, it->first); | |
241 return; | |
242 } | |
243 } | |
244 NOTREACHED(); | |
245 } | |
246 | |
247 void MediaGalleriesScanResultDialogViews::ShowContextMenu( | |
248 const gfx::Point& point, | |
249 ui::MenuSourceType source_type, | |
250 MediaGalleryPrefId id) { | |
251 context_menu_runner_.reset(new views::MenuRunner( | |
252 controller_->GetContextMenu(id))); | |
253 | |
254 if (context_menu_runner_->RunMenuAt( | |
255 GetWidget(), | |
256 NULL, | |
257 gfx::Rect(point.x(), point.y(), 0, 0), | |
258 views::MENU_ANCHOR_TOPLEFT, | |
259 source_type, | |
260 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU) == | |
261 views::MenuRunner::MENU_DELETED) { | |
262 return; | |
263 } | |
264 } | |
265 | |
266 void MediaGalleriesScanResultDialogViews::AcceptDialogForTesting() { | |
267 accepted_ = true; | |
268 | |
269 web_modal::WebContentsModalDialogManager* web_contents_modal_dialog_manager = | |
270 web_modal::WebContentsModalDialogManager::FromWebContents( | |
271 controller_->web_contents()); | |
272 DCHECK(web_contents_modal_dialog_manager); | |
273 web_modal::WebContentsModalDialogManager::TestApi( | |
274 web_contents_modal_dialog_manager).CloseAllDialogs(); | |
275 } | |
276 | |
277 // MediaGalleriesScanResultDialogViewsController ------------------------------- | |
278 | |
279 // static | |
280 MediaGalleriesScanResultDialog* MediaGalleriesScanResultDialog::Create( | |
281 MediaGalleriesScanResultDialogController* controller) { | |
282 return new MediaGalleriesScanResultDialogViews(controller); | |
283 } | |
OLD | NEW |