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 "chrome/grit/generated_resources.h" | 8 #include "chrome/grit/generated_resources.h" |
8 #include "components/constrained_window/constrained_window_views.h" | 9 #include "components/constrained_window/constrained_window_views.h" |
9 #include "ui/base/l10n/l10n_util.h" | 10 #include "ui/base/l10n/l10n_util.h" |
10 #include "ui/gfx/native_widget_types.h" | 11 #include "ui/gfx/native_widget_types.h" |
11 #include "ui/views/controls/label.h" | 12 #include "ui/views/controls/label.h" |
12 #include "ui/views/controls/table/table_view.h" | |
13 #include "ui/views/layout/box_layout.h" | 13 #include "ui/views/layout/box_layout.h" |
14 #include "ui/views/layout/layout_constants.h" | 14 #include "ui/views/layout/layout_constants.h" |
| 15 #include "ui/views/window/dialog_client_view.h" |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
18 int kDialogWidth = 500; | 19 int kDialogWidth = 500; |
19 int kDialogHeight = 400; | 20 int kDialogHeight = 400; |
20 | 21 |
21 } // namespace | 22 } // namespace |
22 | 23 |
23 namespace chrome { | 24 namespace chrome { |
24 | 25 |
25 void ShowWebShareTargetPickerDialog( | 26 void ShowWebShareTargetPickerDialog( |
26 gfx::NativeWindow parent_window, | 27 gfx::NativeWindow parent_window, |
27 const std::vector<base::string16>& targets, | 28 const std::vector<std::pair<base::string16, GURL>>& targets, |
28 const base::Callback<void(SharePickerResult)>& callback) { | 29 const base::Callback<void(base::Optional<std::string>)>& callback) { |
29 constrained_window::CreateBrowserModalDialogViews( | 30 constrained_window::CreateBrowserModalDialogViews( |
30 new WebShareTargetPickerView(targets, callback), parent_window) | 31 new WebShareTargetPickerView(targets, callback), parent_window) |
31 ->Show(); | 32 ->Show(); |
32 } | 33 } |
33 | 34 |
34 } // namespace chrome | 35 } // namespace chrome |
35 | 36 |
36 WebShareTargetPickerView::WebShareTargetPickerView( | 37 WebShareTargetPickerView::WebShareTargetPickerView( |
37 const std::vector<base::string16>& targets, | 38 const std::vector<std::pair<base::string16, GURL>>& targets, |
38 const base::Callback<void(SharePickerResult)>& close_callback) | 39 const base::Callback<void(base::Optional<std::string>)>& close_callback) |
39 : targets_(targets), close_callback_(close_callback) { | 40 : targets_(targets), close_callback_(close_callback) { |
40 views::BoxLayout* layout = new views::BoxLayout( | 41 views::BoxLayout* layout = new views::BoxLayout( |
41 views::BoxLayout::kVertical, views::kPanelHorizMargin, | 42 views::BoxLayout::kVertical, views::kPanelHorizMargin, |
42 views::kPanelVertMargin, views::kRelatedControlVerticalSpacing); | 43 views::kPanelVertMargin, views::kRelatedControlVerticalSpacing); |
43 SetLayoutManager(layout); | 44 SetLayoutManager(layout); |
44 | 45 |
45 views::Label* overview_label = new views::Label( | 46 views::Label* overview_label = new views::Label( |
46 l10n_util::GetStringUTF16(IDS_WEBSHARE_TARGET_PICKER_LABEL)); | 47 l10n_util::GetStringUTF16(IDS_WEBSHARE_TARGET_PICKER_LABEL)); |
47 AddChildView(overview_label); | 48 AddChildView(overview_label); |
48 | 49 |
49 std::vector<ui::TableColumn> table_columns{ui::TableColumn()}; | 50 std::vector<ui::TableColumn> table_columns{ui::TableColumn()}; |
50 views::TableView* table = | 51 table_ = new views::TableView(this, table_columns, views::TEXT_ONLY, true); |
51 new views::TableView(this, table_columns, views::TEXT_ONLY, true); | |
52 // Select the first row. | 52 // Select the first row. |
53 if (RowCount() > 0) | 53 if (RowCount() > 0) |
54 table->Select(0); | 54 table_->Select(0); |
| 55 |
| 56 table_->set_observer(this); |
55 | 57 |
56 // Create the table parent (a ScrollView which includes the scroll bars and | 58 // Create the table parent (a ScrollView which includes the scroll bars and |
57 // border). We add this parent (not the table itself) to the dialog. | 59 // border). We add this parent (not the table itself) to the dialog. |
58 views::View* table_parent = table->CreateParentIfNecessary(); | 60 views::View* table_parent = table_->CreateParentIfNecessary(); |
59 AddChildView(table_parent); | 61 AddChildView(table_parent); |
60 // Make the table expand to fill the space. | 62 // Make the table expand to fill the space. |
61 layout->SetFlexForView(table_parent, 1); | 63 layout->SetFlexForView(table_parent, 1); |
62 } | 64 } |
63 | 65 |
64 WebShareTargetPickerView::~WebShareTargetPickerView() {} | 66 WebShareTargetPickerView::~WebShareTargetPickerView() {} |
65 | 67 |
66 gfx::Size WebShareTargetPickerView::GetPreferredSize() const { | 68 gfx::Size WebShareTargetPickerView::GetPreferredSize() const { |
67 return gfx::Size(kDialogWidth, kDialogHeight); | 69 return gfx::Size(kDialogWidth, kDialogHeight); |
68 } | 70 } |
69 | 71 |
70 ui::ModalType WebShareTargetPickerView::GetModalType() const { | 72 ui::ModalType WebShareTargetPickerView::GetModalType() const { |
71 return ui::MODAL_TYPE_WINDOW; | 73 return ui::MODAL_TYPE_WINDOW; |
72 } | 74 } |
73 | 75 |
74 base::string16 WebShareTargetPickerView::GetWindowTitle() const { | 76 base::string16 WebShareTargetPickerView::GetWindowTitle() const { |
75 return l10n_util::GetStringUTF16(IDS_WEBSHARE_TARGET_PICKER_TITLE); | 77 return l10n_util::GetStringUTF16(IDS_WEBSHARE_TARGET_PICKER_TITLE); |
76 } | 78 } |
77 | 79 |
78 bool WebShareTargetPickerView::Cancel() { | 80 bool WebShareTargetPickerView::Cancel() { |
79 if (!close_callback_.is_null()) | 81 if (!close_callback_.is_null()) |
80 close_callback_.Run(SharePickerResult::CANCEL); | 82 close_callback_.Run(base::nullopt); |
81 | 83 |
82 return true; | 84 return true; |
83 } | 85 } |
84 | 86 |
85 bool WebShareTargetPickerView::Accept() { | 87 bool WebShareTargetPickerView::Accept() { |
86 if (!close_callback_.is_null()) | 88 if (!close_callback_.is_null()) { |
87 close_callback_.Run(SharePickerResult::SHARE); | 89 DCHECK(!table_->selection_model().empty()); |
| 90 close_callback_.Run(targets_[table_->FirstSelectedRow()].second.spec()); |
| 91 } |
88 | 92 |
89 return true; | 93 return true; |
90 } | 94 } |
91 | 95 |
92 base::string16 WebShareTargetPickerView::GetDialogButtonLabel( | 96 base::string16 WebShareTargetPickerView::GetDialogButtonLabel( |
93 ui::DialogButton button) const { | 97 ui::DialogButton button) const { |
94 if (button == ui::DIALOG_BUTTON_OK) | 98 if (button == ui::DIALOG_BUTTON_OK) |
95 return l10n_util::GetStringUTF16(IDS_WEBSHARE_TARGET_PICKER_COMMIT); | 99 return l10n_util::GetStringUTF16(IDS_WEBSHARE_TARGET_PICKER_COMMIT); |
96 | 100 |
97 return views::DialogDelegateView::GetDialogButtonLabel(button); | 101 return views::DialogDelegateView::GetDialogButtonLabel(button); |
98 } | 102 } |
99 | 103 |
| 104 bool WebShareTargetPickerView::IsDialogButtonEnabled( |
| 105 ui::DialogButton button) const { |
| 106 // User shouldn't select OK button if they haven't selected a target. |
| 107 if (button == ui::DIALOG_BUTTON_OK) |
| 108 return !table_->selection_model().empty(); |
| 109 |
| 110 return true; |
| 111 } |
| 112 |
| 113 void WebShareTargetPickerView::OnSelectionChanged() { |
| 114 GetDialogClientView()->UpdateDialogButtons(); |
| 115 } |
| 116 |
| 117 void WebShareTargetPickerView::OnDoubleClick() { |
| 118 GetDialogClientView()->AcceptWindow(); |
| 119 } |
| 120 |
100 int WebShareTargetPickerView::RowCount() { | 121 int WebShareTargetPickerView::RowCount() { |
101 return targets_.size(); | 122 return targets_.size(); |
102 } | 123 } |
103 | 124 |
104 base::string16 WebShareTargetPickerView::GetText(int row, int /*column_id*/) { | 125 base::string16 WebShareTargetPickerView::GetText(int row, int /*column_id*/) { |
105 return targets_[row]; | 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 ")"); |
106 } | 131 } |
107 | 132 |
108 void WebShareTargetPickerView::SetObserver(ui::TableModelObserver* observer) {} | 133 void WebShareTargetPickerView::SetObserver(ui::TableModelObserver* observer) {} |
OLD | NEW |