Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/webshare/webshare_target_picker_view.h" | 5 #include "chrome/browser/ui/views/webshare/webshare_target_picker_view.h" |
| 6 | 6 |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/grit/generated_resources.h" | 8 #include "chrome/grit/generated_resources.h" |
| 9 #include "components/constrained_window/constrained_window_views.h" | 9 #include "components/constrained_window/constrained_window_views.h" |
| 10 #include "ui/base/l10n/l10n_util.h" | 10 #include "ui/base/l10n/l10n_util.h" |
| 11 #include "ui/base/models/table_model.h" | |
| 11 #include "ui/gfx/native_widget_types.h" | 12 #include "ui/gfx/native_widget_types.h" |
| 12 #include "ui/views/controls/label.h" | 13 #include "ui/views/controls/label.h" |
| 14 #include "ui/views/controls/table/table_view.h" | |
| 13 #include "ui/views/layout/box_layout.h" | 15 #include "ui/views/layout/box_layout.h" |
| 14 #include "ui/views/layout/layout_constants.h" | 16 #include "ui/views/layout/layout_constants.h" |
| 15 #include "ui/views/window/dialog_client_view.h" | 17 #include "ui/views/window/dialog_client_view.h" |
| 18 #include "url/gurl.h" | |
| 16 | 19 |
| 17 namespace { | 20 namespace { |
| 18 | 21 |
| 19 int kDialogWidth = 500; | 22 int kDialogWidth = 500; |
| 20 int kDialogHeight = 400; | 23 int kDialogHeight = 400; |
| 21 | 24 |
| 22 } // namespace | 25 } |
| 26 | |
| 27 // Supplies data to the table view. | |
| 28 class TargetPickerTableModel : public ui::TableModel { | |
| 29 public: | |
| 30 TargetPickerTableModel( | |
|
sky
2017/02/08 16:58:25
explicit
Matt Giuca
2017/02/08 23:15:47
Done.
| |
| 31 const std::vector<std::pair<base::string16, GURL>>* targets); | |
| 32 | |
| 33 private: | |
| 34 // ui::TableModel overrides: | |
| 35 int RowCount() override; | |
| 36 base::string16 GetText(int row, int column_id) override; | |
| 37 void SetObserver(ui::TableModelObserver* observer) override; | |
| 38 | |
| 39 // Owned by WebShareTargetPickerView. | |
| 40 const std::vector<std::pair<base::string16, GURL>>* targets_; | |
| 41 }; | |
|
sky
2017/02/08 16:58:25
DISALLOW...
Matt Giuca
2017/02/08 23:15:48
Sorry I'm so bad at remembering this...
| |
| 42 | |
| 43 TargetPickerTableModel::TargetPickerTableModel( | |
| 44 const std::vector<std::pair<base::string16, GURL>>* targets) | |
| 45 : targets_(targets) {} | |
| 46 | |
| 47 int TargetPickerTableModel::RowCount() { | |
| 48 return targets_->size(); | |
| 49 } | |
| 50 | |
| 51 base::string16 TargetPickerTableModel::GetText(int row, int /*column_id*/) { | |
| 52 // Show "title (origin)", to disambiguate titles that are the same, and as a | |
| 53 // security measure. | |
| 54 return (*targets_)[row].first + | |
| 55 base::UTF8ToUTF16(" (" + (*targets_)[row].second.GetOrigin().spec() + | |
| 56 ")"); | |
| 57 } | |
| 58 | |
| 59 void TargetPickerTableModel::SetObserver(ui::TableModelObserver* observer) {} | |
| 23 | 60 |
| 24 namespace chrome { | 61 namespace chrome { |
| 25 | 62 |
| 26 void ShowWebShareTargetPickerDialog( | 63 void ShowWebShareTargetPickerDialog( |
| 27 gfx::NativeWindow parent_window, | 64 gfx::NativeWindow parent_window, |
| 28 const std::vector<std::pair<base::string16, GURL>>& targets, | 65 const std::vector<std::pair<base::string16, GURL>>& targets, |
| 29 const base::Callback<void(base::Optional<std::string>)>& callback) { | 66 const base::Callback<void(base::Optional<std::string>)>& callback) { |
| 30 constrained_window::CreateBrowserModalDialogViews( | 67 constrained_window::CreateBrowserModalDialogViews( |
| 31 new WebShareTargetPickerView(targets, callback), parent_window) | 68 new WebShareTargetPickerView(targets, callback), parent_window) |
| 32 ->Show(); | 69 ->Show(); |
| 33 } | 70 } |
| 34 | 71 |
| 35 } // namespace chrome | 72 } // namespace chrome |
| 36 | 73 |
| 37 WebShareTargetPickerView::WebShareTargetPickerView( | 74 WebShareTargetPickerView::WebShareTargetPickerView( |
| 38 const std::vector<std::pair<base::string16, GURL>>& targets, | 75 const std::vector<std::pair<base::string16, GURL>>& targets, |
| 39 const base::Callback<void(base::Optional<std::string>)>& close_callback) | 76 const base::Callback<void(base::Optional<std::string>)>& close_callback) |
| 40 : targets_(targets), close_callback_(close_callback) { | 77 : targets_(targets), |
| 78 table_model_(base::MakeUnique<TargetPickerTableModel>(&targets_)), | |
| 79 close_callback_(close_callback) { | |
| 41 views::BoxLayout* layout = new views::BoxLayout( | 80 views::BoxLayout* layout = new views::BoxLayout( |
| 42 views::BoxLayout::kVertical, views::kPanelHorizMargin, | 81 views::BoxLayout::kVertical, views::kPanelHorizMargin, |
| 43 views::kPanelVertMargin, views::kRelatedControlVerticalSpacing); | 82 views::kPanelVertMargin, views::kRelatedControlVerticalSpacing); |
| 44 SetLayoutManager(layout); | 83 SetLayoutManager(layout); |
| 45 | 84 |
| 46 views::Label* overview_label = new views::Label( | 85 views::Label* overview_label = new views::Label( |
| 47 l10n_util::GetStringUTF16(IDS_WEBSHARE_TARGET_PICKER_LABEL)); | 86 l10n_util::GetStringUTF16(IDS_WEBSHARE_TARGET_PICKER_LABEL)); |
| 48 AddChildView(overview_label); | 87 AddChildView(overview_label); |
| 49 | 88 |
| 50 std::vector<ui::TableColumn> table_columns{ui::TableColumn()}; | 89 std::vector<ui::TableColumn> table_columns{ui::TableColumn()}; |
| 51 table_ = new views::TableView(this, table_columns, views::TEXT_ONLY, true); | 90 table_ = new views::TableView(table_model_.get(), table_columns, |
| 91 views::TEXT_ONLY, true); | |
| 52 // Select the first row. | 92 // Select the first row. |
| 53 if (RowCount() > 0) | 93 if (targets_.size() > 0) |
| 54 table_->Select(0); | 94 table_->Select(0); |
| 55 | 95 |
| 56 table_->set_observer(this); | 96 table_->set_observer(this); |
| 57 | 97 |
| 58 // Create the table parent (a ScrollView which includes the scroll bars and | 98 // Create the table parent (a ScrollView which includes the scroll bars and |
| 59 // border). We add this parent (not the table itself) to the dialog. | 99 // border). We add this parent (not the table itself) to the dialog. |
| 60 views::View* table_parent = table_->CreateParentIfNecessary(); | 100 views::View* table_parent = table_->CreateParentIfNecessary(); |
| 61 AddChildView(table_parent); | 101 AddChildView(table_parent); |
| 62 // Make the table expand to fill the space. | 102 // Make the table expand to fill the space. |
| 63 layout->SetFlexForView(table_parent, 1); | 103 layout->SetFlexForView(table_parent, 1); |
| 64 } | 104 } |
| 65 | 105 |
| 66 WebShareTargetPickerView::~WebShareTargetPickerView() {} | 106 WebShareTargetPickerView::~WebShareTargetPickerView() { |
| 107 // Clear the pointer from |table_| which currently points at |table_model_|. | |
| 108 // Otherwise, |table_model_| will be deleted before |table_|, and |table_|'s | |
| 109 // destructor will try to call a method on the model. | |
| 110 table_->SetModel(nullptr); | |
|
sky
2017/02/08 16:58:25
I'm ok with the separate model, but couldn't you h
Matt Giuca
2017/02/08 23:15:47
Yes, it would technically be fixed. But we'd still
| |
| 111 } | |
| 67 | 112 |
| 68 gfx::Size WebShareTargetPickerView::GetPreferredSize() const { | 113 gfx::Size WebShareTargetPickerView::GetPreferredSize() const { |
| 69 return gfx::Size(kDialogWidth, kDialogHeight); | 114 return gfx::Size(kDialogWidth, kDialogHeight); |
| 70 } | 115 } |
| 71 | 116 |
| 72 ui::ModalType WebShareTargetPickerView::GetModalType() const { | 117 ui::ModalType WebShareTargetPickerView::GetModalType() const { |
| 73 return ui::MODAL_TYPE_WINDOW; | 118 return ui::MODAL_TYPE_WINDOW; |
| 74 } | 119 } |
| 75 | 120 |
| 76 base::string16 WebShareTargetPickerView::GetWindowTitle() const { | 121 base::string16 WebShareTargetPickerView::GetWindowTitle() const { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 110 return true; | 155 return true; |
| 111 } | 156 } |
| 112 | 157 |
| 113 void WebShareTargetPickerView::OnSelectionChanged() { | 158 void WebShareTargetPickerView::OnSelectionChanged() { |
| 114 GetDialogClientView()->UpdateDialogButtons(); | 159 GetDialogClientView()->UpdateDialogButtons(); |
| 115 } | 160 } |
| 116 | 161 |
| 117 void WebShareTargetPickerView::OnDoubleClick() { | 162 void WebShareTargetPickerView::OnDoubleClick() { |
| 118 GetDialogClientView()->AcceptWindow(); | 163 GetDialogClientView()->AcceptWindow(); |
| 119 } | 164 } |
| 120 | |
| 121 int WebShareTargetPickerView::RowCount() { | |
| 122 return targets_.size(); | |
| 123 } | |
| 124 | |
| 125 base::string16 WebShareTargetPickerView::GetText(int row, int /*column_id*/) { | |
| 126 // Show "title (origin)", to disambiguate titles that are the same, and as a | |
| 127 // security measure. | |
| 128 return targets_[row].first + | |
| 129 base::UTF8ToUTF16(" (" + targets_[row].second.GetOrigin().spec() + | |
| 130 ")"); | |
| 131 } | |
| 132 | |
| 133 void WebShareTargetPickerView::SetObserver(ui::TableModelObserver* observer) {} | |
| OLD | NEW |