OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/chooser_content_view.h" | 5 #include "chrome/browser/ui/views/chooser_content_view.h" |
6 | 6 |
7 #include "base/numerics/safe_conversions.h" | |
7 #include "chrome/grit/generated_resources.h" | 8 #include "chrome/grit/generated_resources.h" |
9 #include "grit/ui_resources.h" | |
8 #include "ui/base/l10n/l10n_util.h" | 10 #include "ui/base/l10n/l10n_util.h" |
11 #include "ui/base/resource/resource_bundle.h" | |
12 #include "ui/gfx/image/image_skia.h" | |
9 #include "ui/views/controls/link.h" | 13 #include "ui/views/controls/link.h" |
10 #include "ui/views/controls/styled_label.h" | 14 #include "ui/views/controls/styled_label.h" |
11 #include "ui/views/controls/table/table_view.h" | 15 #include "ui/views/controls/table/table_view.h" |
12 #include "ui/views/controls/throbber.h" | 16 #include "ui/views/controls/throbber.h" |
13 #include "ui/views/layout/fill_layout.h" | 17 #include "ui/views/layout/fill_layout.h" |
14 | 18 |
15 namespace { | 19 namespace { |
16 | 20 |
17 const int kChooserWidth = 330; | 21 const int kChooserWidth = 330; |
18 | 22 |
19 const int kChooserHeight = 220; | 23 const int kChooserHeight = 220; |
20 | 24 |
21 const int kThrobberDiameter = 24; | 25 const int kThrobberDiameter = 24; |
22 | 26 |
27 // The lookup table for strength bar image. | |
28 const int strength_bar_ids[5] = {IDR_SIGNAL_0_BAR, IDR_SIGNAL_1_BAR, | |
29 IDR_SIGNAL_2_BAR, IDR_SIGNAL_3_BAR, | |
30 IDR_SIGNAL_4_BAR}; | |
31 | |
23 } // namespace | 32 } // namespace |
24 | 33 |
25 ChooserContentView::ChooserContentView( | 34 ChooserContentView::ChooserContentView( |
26 views::TableViewObserver* table_view_observer, | 35 views::TableViewObserver* table_view_observer, |
27 std::unique_ptr<ChooserController> chooser_controller) | 36 std::unique_ptr<ChooserController> chooser_controller) |
28 : chooser_controller_(std::move(chooser_controller)) { | 37 : chooser_controller_(std::move(chooser_controller)) { |
29 chooser_controller_->set_view(this); | 38 chooser_controller_->set_view(this); |
30 std::vector<ui::TableColumn> table_columns; | 39 std::vector<ui::TableColumn> table_columns; |
31 table_columns.push_back(ui::TableColumn()); | 40 table_columns.push_back(ui::TableColumn()); |
32 table_view_ = | 41 table_view_ = new views::TableView( |
33 new views::TableView(this, table_columns, views::TEXT_ONLY, true); | 42 this, table_columns, |
43 chooser_controller_->ShouldShowIconBeforeText() ? views::ICON_AND_TEXT | |
44 : views::TEXT_ONLY, | |
45 true /* single_selection */); | |
34 table_view_->set_select_on_remove(false); | 46 table_view_->set_select_on_remove(false); |
35 table_view_->SetObserver(table_view_observer); | 47 table_view_->SetObserver(table_view_observer); |
36 table_view_->SetEnabled(chooser_controller_->NumOptions() > 0); | 48 table_view_->SetEnabled(chooser_controller_->NumOptions() > 0); |
37 | 49 |
38 SetLayoutManager(new views::FillLayout()); | 50 SetLayoutManager(new views::FillLayout()); |
39 views::View* table_parent = table_view_->CreateParentIfNecessary(); | 51 views::View* table_parent = table_view_->CreateParentIfNecessary(); |
40 AddChildView(table_parent); | 52 AddChildView(table_parent); |
41 | 53 |
42 throbber_ = new views::Throbber(); | 54 throbber_ = new views::Throbber(); |
43 // Set the throbber in the center of the chooser. | 55 // Set the throbber in the center of the chooser. |
(...skipping 12 matching lines...) Expand all Loading... | |
56 discovery_state_->set_listener(nullptr); | 68 discovery_state_->set_listener(nullptr); |
57 } | 69 } |
58 | 70 |
59 gfx::Size ChooserContentView::GetPreferredSize() const { | 71 gfx::Size ChooserContentView::GetPreferredSize() const { |
60 return gfx::Size(kChooserWidth, kChooserHeight); | 72 return gfx::Size(kChooserWidth, kChooserHeight); |
61 } | 73 } |
62 | 74 |
63 int ChooserContentView::RowCount() { | 75 int ChooserContentView::RowCount() { |
64 // When there are no devices, the table contains a message saying there | 76 // When there are no devices, the table contains a message saying there |
65 // are no devices, so the number of rows is always at least 1. | 77 // are no devices, so the number of rows is always at least 1. |
66 return std::max(static_cast<int>(chooser_controller_->NumOptions()), 1); | 78 return std::max(base::checked_cast<int>(chooser_controller_->NumOptions()), |
79 1); | |
67 } | 80 } |
68 | 81 |
69 base::string16 ChooserContentView::GetText(int row, int column_id) { | 82 base::string16 ChooserContentView::GetText(int row, int column_id) { |
70 int num_options = static_cast<int>(chooser_controller_->NumOptions()); | 83 int num_options = base::checked_cast<int>(chooser_controller_->NumOptions()); |
71 if (num_options == 0) { | 84 if (num_options == 0) { |
72 DCHECK_EQ(0, row); | 85 DCHECK_EQ(0, row); |
73 return chooser_controller_->GetNoOptionsText(); | 86 return chooser_controller_->GetNoOptionsText(); |
74 } | 87 } |
75 | 88 |
76 DCHECK_GE(row, 0); | 89 DCHECK_GE(row, 0); |
77 DCHECK_LT(row, num_options); | 90 DCHECK_LT(row, num_options); |
78 return chooser_controller_->GetOption(static_cast<size_t>(row)); | 91 return chooser_controller_->GetOption(static_cast<size_t>(row)); |
79 } | 92 } |
80 | 93 |
81 void ChooserContentView::SetObserver(ui::TableModelObserver* observer) {} | 94 void ChooserContentView::SetObserver(ui::TableModelObserver* observer) {} |
82 | 95 |
96 gfx::ImageSkia ChooserContentView::GetIcon(int row) { | |
97 DCHECK(chooser_controller_->ShouldShowIconBeforeText()); | |
98 | |
99 int num_options = base::checked_cast<int>(chooser_controller_->NumOptions()); | |
msw
2016/08/16 16:51:18
nit: Avoid casting to compare with 0. Instead, you
juncai
2016/08/16 17:49:52
Done.
| |
100 if (num_options == 0) { | |
101 DCHECK_EQ(0, row); | |
102 return gfx::ImageSkia(); | |
103 } | |
104 | |
105 DCHECK_GE(row, 0); | |
106 DCHECK_LT(row, num_options); | |
107 | |
108 int level = | |
109 chooser_controller_->GetSignalStrengthLevel(static_cast<size_t>(row)); | |
110 | |
111 if (level == -1) | |
112 return gfx::ImageSkia(); | |
113 | |
114 return *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | |
115 strength_bar_ids[level]); | |
msw
2016/08/16 16:51:18
nit: CHECK that the level is in the range of the a
juncai
2016/08/16 17:49:52
Done.
| |
116 } | |
117 | |
83 void ChooserContentView::OnOptionsInitialized() { | 118 void ChooserContentView::OnOptionsInitialized() { |
84 table_view_->OnModelChanged(); | 119 table_view_->OnModelChanged(); |
85 UpdateTableView(); | 120 UpdateTableView(); |
86 } | 121 } |
87 | 122 |
88 void ChooserContentView::OnOptionAdded(size_t index) { | 123 void ChooserContentView::OnOptionAdded(size_t index) { |
89 table_view_->OnItemsAdded(static_cast<int>(index), 1); | 124 table_view_->OnItemsAdded(base::checked_cast<int>(index), 1); |
90 UpdateTableView(); | 125 UpdateTableView(); |
91 table_view_->SetVisible(true); | 126 table_view_->SetVisible(true); |
92 throbber_->SetVisible(false); | 127 throbber_->SetVisible(false); |
93 throbber_->Stop(); | 128 throbber_->Stop(); |
94 } | 129 } |
95 | 130 |
96 void ChooserContentView::OnOptionRemoved(size_t index) { | 131 void ChooserContentView::OnOptionRemoved(size_t index) { |
97 table_view_->OnItemsRemoved(static_cast<int>(index), 1); | 132 table_view_->OnItemsRemoved(base::checked_cast<int>(index), 1); |
98 UpdateTableView(); | 133 UpdateTableView(); |
99 } | 134 } |
100 | 135 |
101 void ChooserContentView::OnAdapterEnabledChanged(bool enabled) { | 136 void ChooserContentView::OnAdapterEnabledChanged(bool enabled) { |
102 // No row is selected since the adapter status has changed. | 137 // No row is selected since the adapter status has changed. |
103 // This will also disable the OK button if it was enabled because | 138 // This will also disable the OK button if it was enabled because |
104 // of a previously selected row. | 139 // of a previously selected row. |
105 table_view_->Select(-1); | 140 table_view_->Select(-1); |
106 UpdateTableView(); | 141 UpdateTableView(); |
107 table_view_->SetVisible(true); | 142 table_view_->SetVisible(true); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
202 } | 237 } |
203 | 238 |
204 void ChooserContentView::UpdateTableView() { | 239 void ChooserContentView::UpdateTableView() { |
205 if (chooser_controller_->NumOptions() == 0) { | 240 if (chooser_controller_->NumOptions() == 0) { |
206 table_view_->OnModelChanged(); | 241 table_view_->OnModelChanged(); |
207 table_view_->SetEnabled(false); | 242 table_view_->SetEnabled(false); |
208 } else { | 243 } else { |
209 table_view_->SetEnabled(true); | 244 table_view_->SetEnabled(true); |
210 } | 245 } |
211 } | 246 } |
OLD | NEW |