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