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 explicit TargetPickerTableModel( |
| 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 |
| 42 DISALLOW_COPY_AND_ASSIGN(TargetPickerTableModel); |
| 43 }; |
| 44 |
| 45 TargetPickerTableModel::TargetPickerTableModel( |
| 46 const std::vector<std::pair<base::string16, GURL>>* targets) |
| 47 : targets_(targets) {} |
| 48 |
| 49 int TargetPickerTableModel::RowCount() { |
| 50 return targets_->size(); |
| 51 } |
| 52 |
| 53 base::string16 TargetPickerTableModel::GetText(int row, int /*column_id*/) { |
| 54 // Show "title (origin)", to disambiguate titles that are the same, and as a |
| 55 // security measure. |
| 56 return (*targets_)[row].first + |
| 57 base::UTF8ToUTF16(" (" + (*targets_)[row].second.GetOrigin().spec() + |
| 58 ")"); |
| 59 } |
| 60 |
| 61 void TargetPickerTableModel::SetObserver(ui::TableModelObserver* observer) {} |
23 | 62 |
24 namespace chrome { | 63 namespace chrome { |
25 | 64 |
26 void ShowWebShareTargetPickerDialog( | 65 void ShowWebShareTargetPickerDialog( |
27 gfx::NativeWindow parent_window, | 66 gfx::NativeWindow parent_window, |
28 const std::vector<std::pair<base::string16, GURL>>& targets, | 67 const std::vector<std::pair<base::string16, GURL>>& targets, |
29 const base::Callback<void(base::Optional<std::string>)>& callback) { | 68 const base::Callback<void(base::Optional<std::string>)>& callback) { |
30 constrained_window::CreateBrowserModalDialogViews( | 69 constrained_window::CreateBrowserModalDialogViews( |
31 new WebShareTargetPickerView(targets, callback), parent_window) | 70 new WebShareTargetPickerView(targets, callback), parent_window) |
32 ->Show(); | 71 ->Show(); |
33 } | 72 } |
34 | 73 |
35 } // namespace chrome | 74 } // namespace chrome |
36 | 75 |
37 WebShareTargetPickerView::WebShareTargetPickerView( | 76 WebShareTargetPickerView::WebShareTargetPickerView( |
38 const std::vector<std::pair<base::string16, GURL>>& targets, | 77 const std::vector<std::pair<base::string16, GURL>>& targets, |
39 const base::Callback<void(base::Optional<std::string>)>& close_callback) | 78 const base::Callback<void(base::Optional<std::string>)>& close_callback) |
40 : targets_(targets), close_callback_(close_callback) { | 79 : targets_(targets), |
| 80 table_model_(base::MakeUnique<TargetPickerTableModel>(&targets_)), |
| 81 close_callback_(close_callback) { |
41 views::BoxLayout* layout = new views::BoxLayout( | 82 views::BoxLayout* layout = new views::BoxLayout( |
42 views::BoxLayout::kVertical, views::kPanelHorizMargin, | 83 views::BoxLayout::kVertical, views::kPanelHorizMargin, |
43 views::kPanelVertMargin, views::kRelatedControlVerticalSpacing); | 84 views::kPanelVertMargin, views::kRelatedControlVerticalSpacing); |
44 SetLayoutManager(layout); | 85 SetLayoutManager(layout); |
45 | 86 |
46 views::Label* overview_label = new views::Label( | 87 views::Label* overview_label = new views::Label( |
47 l10n_util::GetStringUTF16(IDS_WEBSHARE_TARGET_PICKER_LABEL)); | 88 l10n_util::GetStringUTF16(IDS_WEBSHARE_TARGET_PICKER_LABEL)); |
48 AddChildView(overview_label); | 89 AddChildView(overview_label); |
49 | 90 |
50 std::vector<ui::TableColumn> table_columns{ui::TableColumn()}; | 91 std::vector<ui::TableColumn> table_columns{ui::TableColumn()}; |
51 table_ = new views::TableView(this, table_columns, views::TEXT_ONLY, true); | 92 table_ = new views::TableView(table_model_.get(), table_columns, |
| 93 views::TEXT_ONLY, true); |
52 // Select the first row. | 94 // Select the first row. |
53 if (RowCount() > 0) | 95 if (targets_.size() > 0) |
54 table_->Select(0); | 96 table_->Select(0); |
55 | 97 |
56 table_->set_observer(this); | 98 table_->set_observer(this); |
57 | 99 |
58 // Create the table parent (a ScrollView which includes the scroll bars and | 100 // 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. | 101 // border). We add this parent (not the table itself) to the dialog. |
60 views::View* table_parent = table_->CreateParentIfNecessary(); | 102 views::View* table_parent = table_->CreateParentIfNecessary(); |
61 AddChildView(table_parent); | 103 AddChildView(table_parent); |
62 // Make the table expand to fill the space. | 104 // Make the table expand to fill the space. |
63 layout->SetFlexForView(table_parent, 1); | 105 layout->SetFlexForView(table_parent, 1); |
64 } | 106 } |
65 | 107 |
66 WebShareTargetPickerView::~WebShareTargetPickerView() {} | 108 WebShareTargetPickerView::~WebShareTargetPickerView() { |
| 109 // Clear the pointer from |table_| which currently points at |table_model_|. |
| 110 // Otherwise, |table_model_| will be deleted before |table_|, and |table_|'s |
| 111 // destructor will try to call a method on the model. |
| 112 table_->SetModel(nullptr); |
| 113 } |
67 | 114 |
68 gfx::Size WebShareTargetPickerView::GetPreferredSize() const { | 115 gfx::Size WebShareTargetPickerView::GetPreferredSize() const { |
69 return gfx::Size(kDialogWidth, kDialogHeight); | 116 return gfx::Size(kDialogWidth, kDialogHeight); |
70 } | 117 } |
71 | 118 |
72 ui::ModalType WebShareTargetPickerView::GetModalType() const { | 119 ui::ModalType WebShareTargetPickerView::GetModalType() const { |
73 return ui::MODAL_TYPE_WINDOW; | 120 return ui::MODAL_TYPE_WINDOW; |
74 } | 121 } |
75 | 122 |
76 base::string16 WebShareTargetPickerView::GetWindowTitle() const { | 123 base::string16 WebShareTargetPickerView::GetWindowTitle() const { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 return true; | 157 return true; |
111 } | 158 } |
112 | 159 |
113 void WebShareTargetPickerView::OnSelectionChanged() { | 160 void WebShareTargetPickerView::OnSelectionChanged() { |
114 GetDialogClientView()->UpdateDialogButtons(); | 161 GetDialogClientView()->UpdateDialogButtons(); |
115 } | 162 } |
116 | 163 |
117 void WebShareTargetPickerView::OnDoubleClick() { | 164 void WebShareTargetPickerView::OnDoubleClick() { |
118 GetDialogClientView()->AcceptWindow(); | 165 GetDialogClientView()->AcceptWindow(); |
119 } | 166 } |
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 |