Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(359)

Side by Side Diff: chrome/browser/ui/views/webshare/webshare_target_picker_view.cc

Issue 2667803002: Picker takes a vector of pairs, and passes back the user chosen target. (Closed)
Patch Set: Fixed, as per feedback Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 15
16 namespace { 16 namespace {
17 17
18 int kDialogWidth = 500; 18 int kDialogWidth = 500;
19 int kDialogHeight = 400; 19 int kDialogHeight = 400;
20 20
21 } // namespace 21 } // namespace
22 22
23 namespace chrome { 23 namespace chrome {
24 24
25 void ShowWebShareTargetPickerDialog( 25 void ShowWebShareTargetPickerDialog(
26 gfx::NativeWindow parent_window, 26 gfx::NativeWindow parent_window,
27 const std::vector<base::string16>& targets, 27 const std::vector<std::pair<base::string16, GURL>>& targets,
28 const base::Callback<void(SharePickerResult)>& callback) { 28 const base::Callback<void(base::Optional<std::string>)>& callback) {
29 constrained_window::CreateBrowserModalDialogViews( 29 constrained_window::CreateBrowserModalDialogViews(
30 new WebShareTargetPickerView(targets, callback), parent_window) 30 new WebShareTargetPickerView(targets, callback), parent_window)
31 ->Show(); 31 ->Show();
32 } 32 }
33 33
34 } // namespace chrome 34 } // namespace chrome
35 35
36 WebShareTargetPickerView::WebShareTargetPickerView( 36 WebShareTargetPickerView::WebShareTargetPickerView(
37 const std::vector<base::string16>& targets, 37 const std::vector<std::pair<base::string16, GURL>>& targets,
38 const base::Callback<void(SharePickerResult)>& close_callback) 38 const base::Callback<void(base::Optional<std::string>)>& close_callback)
39 : targets_(targets), close_callback_(close_callback) { 39 : targets_(targets), close_callback_(close_callback) {
40 views::BoxLayout* layout = new views::BoxLayout( 40 views::BoxLayout* layout = new views::BoxLayout(
41 views::BoxLayout::kVertical, views::kPanelHorizMargin, 41 views::BoxLayout::kVertical, views::kPanelHorizMargin,
42 views::kPanelVertMargin, views::kRelatedControlVerticalSpacing); 42 views::kPanelVertMargin, views::kRelatedControlVerticalSpacing);
43 SetLayoutManager(layout); 43 SetLayoutManager(layout);
44 44
45 views::Label* overview_label = new views::Label( 45 views::Label* overview_label = new views::Label(
46 l10n_util::GetStringUTF16(IDS_WEBSHARE_TARGET_PICKER_LABEL)); 46 l10n_util::GetStringUTF16(IDS_WEBSHARE_TARGET_PICKER_LABEL));
47 AddChildView(overview_label); 47 AddChildView(overview_label);
48 48
49 std::vector<ui::TableColumn> table_columns{ui::TableColumn()}; 49 std::vector<ui::TableColumn> table_columns{ui::TableColumn()};
50 views::TableView* table = 50 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. 51 // Select the first row.
53 if (RowCount() > 0) 52 if (RowCount() > 0)
54 table->Select(0); 53 table_->Select(0);
55 54
56 // Create the table parent (a ScrollView which includes the scroll bars and 55 // 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. 56 // border). We add this parent (not the table itself) to the dialog.
58 views::View* table_parent = table->CreateParentIfNecessary(); 57 views::View* table_parent = table_->CreateParentIfNecessary();
59 AddChildView(table_parent); 58 AddChildView(table_parent);
60 // Make the table expand to fill the space. 59 // Make the table expand to fill the space.
61 layout->SetFlexForView(table_parent, 1); 60 layout->SetFlexForView(table_parent, 1);
62 } 61 }
63 62
64 WebShareTargetPickerView::~WebShareTargetPickerView() {} 63 WebShareTargetPickerView::~WebShareTargetPickerView() {}
65 64
66 gfx::Size WebShareTargetPickerView::GetPreferredSize() const { 65 gfx::Size WebShareTargetPickerView::GetPreferredSize() const {
67 return gfx::Size(kDialogWidth, kDialogHeight); 66 return gfx::Size(kDialogWidth, kDialogHeight);
68 } 67 }
69 68
70 ui::ModalType WebShareTargetPickerView::GetModalType() const { 69 ui::ModalType WebShareTargetPickerView::GetModalType() const {
71 return ui::MODAL_TYPE_WINDOW; 70 return ui::MODAL_TYPE_WINDOW;
72 } 71 }
73 72
74 base::string16 WebShareTargetPickerView::GetWindowTitle() const { 73 base::string16 WebShareTargetPickerView::GetWindowTitle() const {
75 return l10n_util::GetStringUTF16(IDS_WEBSHARE_TARGET_PICKER_TITLE); 74 return l10n_util::GetStringUTF16(IDS_WEBSHARE_TARGET_PICKER_TITLE);
76 } 75 }
77 76
78 bool WebShareTargetPickerView::Cancel() { 77 bool WebShareTargetPickerView::Cancel() {
79 if (!close_callback_.is_null()) 78 if (!close_callback_.is_null())
80 close_callback_.Run(SharePickerResult::CANCEL); 79 close_callback_.Run(base::nullopt);
81 80
82 return true; 81 return true;
83 } 82 }
84 83
85 bool WebShareTargetPickerView::Accept() { 84 bool WebShareTargetPickerView::Accept() {
86 if (!close_callback_.is_null()) 85 if (!close_callback_.is_null()) {
87 close_callback_.Run(SharePickerResult::SHARE); 86 DCHECK(!table_->selection_model().empty());
87 close_callback_.Run(targets_[table_->FirstSelectedRow()].second.spec());
88 }
88 89
89 return true; 90 return true;
90 } 91 }
91 92
92 base::string16 WebShareTargetPickerView::GetDialogButtonLabel( 93 base::string16 WebShareTargetPickerView::GetDialogButtonLabel(
93 ui::DialogButton button) const { 94 ui::DialogButton button) const {
94 if (button == ui::DIALOG_BUTTON_OK) 95 if (button == ui::DIALOG_BUTTON_OK)
95 return l10n_util::GetStringUTF16(IDS_WEBSHARE_TARGET_PICKER_COMMIT); 96 return l10n_util::GetStringUTF16(IDS_WEBSHARE_TARGET_PICKER_COMMIT);
96 97
97 return views::DialogDelegateView::GetDialogButtonLabel(button); 98 return views::DialogDelegateView::GetDialogButtonLabel(button);
98 } 99 }
99 100
101 bool WebShareTargetPickerView::IsDialogButtonEnabled(
102 ui::DialogButton button) const {
103 // User shouldn't select OK button if they haven't selected a target.
104 if (button == ui::DIALOG_BUTTON_OK)
105 return !table_->selection_model().empty();
sky 2017/02/03 18:48:42 You need respond to OnSelectionChanged() and trigg
constantina 2017/02/06 03:02:35 Done.
106
107 return true;
108 }
109
100 int WebShareTargetPickerView::RowCount() { 110 int WebShareTargetPickerView::RowCount() {
101 return targets_.size(); 111 return targets_.size();
102 } 112 }
103 113
104 base::string16 WebShareTargetPickerView::GetText(int row, int /*column_id*/) { 114 base::string16 WebShareTargetPickerView::GetText(int row, int /*column_id*/) {
105 return targets_[row]; 115 // Show "title (origin)", to disambiguate titles that are the same, and as a
116 // security measure.
117 return targets_[row].first +
118 base::UTF8ToUTF16(" (" + targets_[row].second.GetOrigin().spec() +
119 ")");
106 } 120 }
107 121
108 void WebShareTargetPickerView::SetObserver(ui::TableModelObserver* observer) {} 122 void WebShareTargetPickerView::SetObserver(ui::TableModelObserver* observer) {}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698