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 "chrome/grit/generated_resources.h" | 7 #include "chrome/grit/generated_resources.h" |
8 #include "components/constrained_window/constrained_window_views.h" | 8 #include "components/constrained_window/constrained_window_views.h" |
9 #include "ui/base/l10n/l10n_util.h" | 9 #include "ui/base/l10n/l10n_util.h" |
10 #include "ui/gfx/native_widget_types.h" | 10 #include "ui/gfx/native_widget_types.h" |
11 #include "ui/views/controls/label.h" | 11 #include "ui/views/controls/label.h" |
12 #include "ui/views/controls/table/table_view.h" | |
13 #include "ui/views/layout/box_layout.h" | 12 #include "ui/views/layout/box_layout.h" |
14 #include "ui/views/layout/layout_constants.h" | 13 #include "ui/views/layout/layout_constants.h" |
15 | 14 |
16 namespace { | 15 namespace { |
17 | 16 |
18 int kDialogWidth = 500; | 17 int kDialogWidth = 500; |
19 int kDialogHeight = 400; | 18 int kDialogHeight = 400; |
20 | 19 |
21 } // namespace | 20 } // namespace |
22 | 21 |
23 namespace chrome { | 22 namespace chrome { |
24 | 23 |
25 void ShowWebShareTargetPickerDialog( | 24 void ShowWebShareTargetPickerDialog( |
26 gfx::NativeWindow parent_window, | 25 gfx::NativeWindow parent_window, |
27 const std::vector<base::string16>& targets, | 26 const std::vector<base::string16>& targets, |
28 const base::Callback<void(SharePickerResult)>& callback) { | 27 const base::Callback<void(base::Optional<base::string16>)>& callback) { |
29 constrained_window::CreateBrowserModalDialogViews( | 28 constrained_window::CreateBrowserModalDialogViews( |
30 new WebShareTargetPickerView(targets, callback), parent_window) | 29 new WebShareTargetPickerView(targets, callback), parent_window) |
31 ->Show(); | 30 ->Show(); |
32 } | 31 } |
33 | 32 |
34 } // namespace chrome | 33 } // namespace chrome |
35 | 34 |
36 WebShareTargetPickerView::WebShareTargetPickerView( | 35 WebShareTargetPickerView::WebShareTargetPickerView( |
37 const std::vector<base::string16>& targets, | 36 const std::vector<base::string16>& targets, |
38 const base::Callback<void(SharePickerResult)>& close_callback) | 37 const base::Callback<void(base::Optional<base::string16>)>& close_callback) |
39 : targets_(targets), close_callback_(close_callback) { | 38 : targets_(targets), close_callback_(close_callback) { |
40 views::BoxLayout* layout = new views::BoxLayout( | 39 views::BoxLayout* layout = new views::BoxLayout( |
41 views::BoxLayout::kVertical, views::kPanelHorizMargin, | 40 views::BoxLayout::kVertical, views::kPanelHorizMargin, |
42 views::kPanelVertMargin, views::kRelatedControlVerticalSpacing); | 41 views::kPanelVertMargin, views::kRelatedControlVerticalSpacing); |
43 SetLayoutManager(layout); | 42 SetLayoutManager(layout); |
44 | 43 |
45 views::Label* overview_label = new views::Label( | 44 views::Label* overview_label = new views::Label( |
46 l10n_util::GetStringUTF16(IDS_WEBSHARE_TARGET_PICKER_LABEL)); | 45 l10n_util::GetStringUTF16(IDS_WEBSHARE_TARGET_PICKER_LABEL)); |
47 AddChildView(overview_label); | 46 AddChildView(overview_label); |
48 | 47 |
49 std::vector<ui::TableColumn> table_columns{ui::TableColumn()}; | 48 std::vector<ui::TableColumn> table_columns{ui::TableColumn()}; |
50 views::TableView* table = | 49 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. | 50 // Select the first row. |
53 if (RowCount() > 0) | 51 if (RowCount() > 0) |
54 table->Select(0); | 52 table_->Select(0); |
55 | 53 |
56 // Create the table parent (a ScrollView which includes the scroll bars and | 54 // 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. | 55 // border). We add this parent (not the table itself) to the dialog. |
58 views::View* table_parent = table->CreateParentIfNecessary(); | 56 views::View* table_parent = table_->CreateParentIfNecessary(); |
59 AddChildView(table_parent); | 57 AddChildView(table_parent); |
60 // Make the table expand to fill the space. | 58 // Make the table expand to fill the space. |
61 layout->SetFlexForView(table_parent, 1); | 59 layout->SetFlexForView(table_parent, 1); |
62 } | 60 } |
63 | 61 |
64 WebShareTargetPickerView::~WebShareTargetPickerView() {} | 62 WebShareTargetPickerView::~WebShareTargetPickerView() {} |
65 | 63 |
66 gfx::Size WebShareTargetPickerView::GetPreferredSize() const { | 64 gfx::Size WebShareTargetPickerView::GetPreferredSize() const { |
67 return gfx::Size(kDialogWidth, kDialogHeight); | 65 return gfx::Size(kDialogWidth, kDialogHeight); |
68 } | 66 } |
69 | 67 |
70 ui::ModalType WebShareTargetPickerView::GetModalType() const { | 68 ui::ModalType WebShareTargetPickerView::GetModalType() const { |
71 return ui::MODAL_TYPE_WINDOW; | 69 return ui::MODAL_TYPE_WINDOW; |
72 } | 70 } |
73 | 71 |
74 base::string16 WebShareTargetPickerView::GetWindowTitle() const { | 72 base::string16 WebShareTargetPickerView::GetWindowTitle() const { |
75 return l10n_util::GetStringUTF16(IDS_WEBSHARE_TARGET_PICKER_TITLE); | 73 return l10n_util::GetStringUTF16(IDS_WEBSHARE_TARGET_PICKER_TITLE); |
76 } | 74 } |
77 | 75 |
78 bool WebShareTargetPickerView::Cancel() { | 76 bool WebShareTargetPickerView::Cancel() { |
79 if (!close_callback_.is_null()) | 77 if (!close_callback_.is_null()) |
80 close_callback_.Run(SharePickerResult::CANCEL); | 78 close_callback_.Run(base::nullopt); |
81 | 79 |
82 return true; | 80 return true; |
83 } | 81 } |
84 | 82 |
85 bool WebShareTargetPickerView::Accept() { | 83 bool WebShareTargetPickerView::Accept() { |
86 if (!close_callback_.is_null()) | 84 if (!close_callback_.is_null()) |
87 close_callback_.Run(SharePickerResult::SHARE); | 85 close_callback_.Run(GetText(table_->FirstSelectedRow(), 0)); |
sky
2017/01/31 05:22:32
What if there is no selected row?
constantina
2017/01/31 23:29:42
If there are no targets in the list, there will be
Matt Giuca
2017/01/31 23:48:27
This is true now, but won't be true very soon (and
sky
2017/02/01 00:51:33
Not just that, but there is nothing stopping the u
constantina
2017/02/01 01:06:02
Done. If nothing is selected and user accepts, UI
| |
88 | 86 |
89 return true; | 87 return true; |
90 } | 88 } |
91 | 89 |
92 base::string16 WebShareTargetPickerView::GetDialogButtonLabel( | 90 base::string16 WebShareTargetPickerView::GetDialogButtonLabel( |
93 ui::DialogButton button) const { | 91 ui::DialogButton button) const { |
94 if (button == ui::DIALOG_BUTTON_OK) | 92 if (button == ui::DIALOG_BUTTON_OK) |
95 return l10n_util::GetStringUTF16(IDS_WEBSHARE_TARGET_PICKER_COMMIT); | 93 return l10n_util::GetStringUTF16(IDS_WEBSHARE_TARGET_PICKER_COMMIT); |
96 | 94 |
97 return views::DialogDelegateView::GetDialogButtonLabel(button); | 95 return views::DialogDelegateView::GetDialogButtonLabel(button); |
98 } | 96 } |
99 | 97 |
100 int WebShareTargetPickerView::RowCount() { | 98 int WebShareTargetPickerView::RowCount() { |
101 return targets_.size(); | 99 return targets_.size(); |
102 } | 100 } |
103 | 101 |
104 base::string16 WebShareTargetPickerView::GetText(int row, int /*column_id*/) { | 102 base::string16 WebShareTargetPickerView::GetText(int row, int /*column_id*/) { |
105 return targets_[row]; | 103 return targets_[row]; |
106 } | 104 } |
107 | 105 |
108 void WebShareTargetPickerView::SetObserver(ui::TableModelObserver* observer) {} | 106 void WebShareTargetPickerView::SetObserver(ui::TableModelObserver* observer) {} |
OLD | NEW |