Chromium Code Reviews| 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 size_t num_options = chooser_controller_->NumOptions(); | |
| 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, base::checked_cast<int>(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 DCHECK_GE(level, 0); | |
| 115 DCHECK_LT(level, | |
| 116 base::checked_cast<int>(sizeof(strength_bar_ids) / sizeof(int))); | |
|
msw
2016/08/16 18:55:13
nit: use base::checked_cast<int>(arraysize(strengt
juncai
2016/08/16 19:06:26
Done.
| |
| 117 | |
| 118 return *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | |
| 119 strength_bar_ids[level]); | |
| 120 } | |
| 121 | |
| 83 void ChooserContentView::OnOptionsInitialized() { | 122 void ChooserContentView::OnOptionsInitialized() { |
| 84 table_view_->OnModelChanged(); | 123 table_view_->OnModelChanged(); |
| 85 UpdateTableView(); | 124 UpdateTableView(); |
| 86 } | 125 } |
| 87 | 126 |
| 88 void ChooserContentView::OnOptionAdded(size_t index) { | 127 void ChooserContentView::OnOptionAdded(size_t index) { |
| 89 table_view_->OnItemsAdded(static_cast<int>(index), 1); | 128 table_view_->OnItemsAdded(base::checked_cast<int>(index), 1); |
| 90 UpdateTableView(); | 129 UpdateTableView(); |
| 91 table_view_->SetVisible(true); | 130 table_view_->SetVisible(true); |
| 92 throbber_->SetVisible(false); | 131 throbber_->SetVisible(false); |
| 93 throbber_->Stop(); | 132 throbber_->Stop(); |
| 94 } | 133 } |
| 95 | 134 |
| 96 void ChooserContentView::OnOptionRemoved(size_t index) { | 135 void ChooserContentView::OnOptionRemoved(size_t index) { |
| 97 table_view_->OnItemsRemoved(static_cast<int>(index), 1); | 136 table_view_->OnItemsRemoved(base::checked_cast<int>(index), 1); |
| 98 UpdateTableView(); | 137 UpdateTableView(); |
| 99 } | 138 } |
| 100 | 139 |
| 101 void ChooserContentView::OnAdapterEnabledChanged(bool enabled) { | 140 void ChooserContentView::OnAdapterEnabledChanged(bool enabled) { |
| 102 // No row is selected since the adapter status has changed. | 141 // No row is selected since the adapter status has changed. |
| 103 // This will also disable the OK button if it was enabled because | 142 // This will also disable the OK button if it was enabled because |
| 104 // of a previously selected row. | 143 // of a previously selected row. |
| 105 table_view_->Select(-1); | 144 table_view_->Select(-1); |
| 106 UpdateTableView(); | 145 UpdateTableView(); |
| 107 table_view_->SetVisible(true); | 146 table_view_->SetVisible(true); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 202 } | 241 } |
| 203 | 242 |
| 204 void ChooserContentView::UpdateTableView() { | 243 void ChooserContentView::UpdateTableView() { |
| 205 if (chooser_controller_->NumOptions() == 0) { | 244 if (chooser_controller_->NumOptions() == 0) { |
| 206 table_view_->OnModelChanged(); | 245 table_view_->OnModelChanged(); |
| 207 table_view_->SetEnabled(false); | 246 table_view_->SetEnabled(false); |
| 208 } else { | 247 } else { |
| 209 table_view_->SetEnabled(true); | 248 table_view_->SetEnabled(true); |
| 210 } | 249 } |
| 211 } | 250 } |
| OLD | NEW |