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 "chrome/grit/generated_resources.h" | 7 #include "chrome/grit/generated_resources.h" |
8 #include "grit/ui_resources.h" | |
8 #include "ui/base/l10n/l10n_util.h" | 9 #include "ui/base/l10n/l10n_util.h" |
10 #include "ui/base/resource/resource_bundle.h" | |
11 #include "ui/gfx/image/image_skia.h" | |
12 #include "ui/gfx/image/image_skia_operations.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 // Images for strength bars. | |
28 const int kNumStrengthBarImages = 5; | |
29 | |
23 } // namespace | 30 } // namespace |
24 | 31 |
25 ChooserContentView::ChooserContentView( | 32 ChooserContentView::ChooserContentView( |
26 views::TableViewObserver* table_view_observer, | 33 views::TableViewObserver* table_view_observer, |
27 std::unique_ptr<ChooserController> chooser_controller) | 34 std::unique_ptr<ChooserController> chooser_controller) |
28 : chooser_controller_(std::move(chooser_controller)) { | 35 : chooser_controller_(std::move(chooser_controller)) { |
29 chooser_controller_->set_view(this); | 36 chooser_controller_->set_view(this); |
30 std::vector<ui::TableColumn> table_columns; | 37 std::vector<ui::TableColumn> table_columns; |
31 table_columns.push_back(ui::TableColumn()); | 38 table_columns.push_back(ui::TableColumn()); |
32 table_view_ = | 39 table_view_ = new views::TableView(this, table_columns, |
33 new views::TableView(this, table_columns, views::TEXT_ONLY, true); | 40 chooser_controller_->HasIconBeforeText() |
41 ? views::ICON_AND_TEXT | |
42 : views::TEXT_ONLY, | |
43 true /* single_selection */); | |
34 table_view_->set_select_on_remove(false); | 44 table_view_->set_select_on_remove(false); |
35 table_view_->SetObserver(table_view_observer); | 45 table_view_->SetObserver(table_view_observer); |
36 table_view_->SetEnabled(chooser_controller_->NumOptions() > 0); | 46 table_view_->SetEnabled(chooser_controller_->NumOptions() > 0); |
37 | 47 |
38 SetLayoutManager(new views::FillLayout()); | 48 SetLayoutManager(new views::FillLayout()); |
39 views::View* table_parent = table_view_->CreateParentIfNecessary(); | 49 views::View* table_parent = table_view_->CreateParentIfNecessary(); |
40 AddChildView(table_parent); | 50 AddChildView(table_parent); |
41 | 51 |
42 throbber_ = new views::Throbber(); | 52 throbber_ = new views::Throbber(); |
43 // Set the throbber in the center of the chooser. | 53 // Set the throbber in the center of the chooser. |
(...skipping 29 matching lines...) Expand all Loading... | |
73 return chooser_controller_->GetNoOptionsText(); | 83 return chooser_controller_->GetNoOptionsText(); |
74 } | 84 } |
75 | 85 |
76 DCHECK_GE(row, 0); | 86 DCHECK_GE(row, 0); |
77 DCHECK_LT(row, num_options); | 87 DCHECK_LT(row, num_options); |
78 return chooser_controller_->GetOption(static_cast<size_t>(row)); | 88 return chooser_controller_->GetOption(static_cast<size_t>(row)); |
79 } | 89 } |
80 | 90 |
81 void ChooserContentView::SetObserver(ui::TableModelObserver* observer) {} | 91 void ChooserContentView::SetObserver(ui::TableModelObserver* observer) {} |
82 | 92 |
93 gfx::ImageSkia ChooserContentView::GetIcon(int row) { | |
94 if (!chooser_controller_->HasIconBeforeText()) | |
msw
2016/08/12 23:08:49
nit: can we DCHECK this instead? TableView probabl
juncai
2016/08/15 21:53:19
Done.
| |
95 return gfx::ImageSkia(); | |
96 | |
97 int num_options = static_cast<int>(chooser_controller_->NumOptions()); | |
msw
2016/08/12 23:08:49
nit: avoid this static cast.
juncai
2016/08/15 21:53:19
Use base::checked_cast instead of static_cast.
Do
| |
98 if (num_options == 0) { | |
99 DCHECK_EQ(0, row); | |
100 return gfx::ImageSkia(); | |
101 } | |
102 | |
103 DCHECK_GE(row, 0); | |
104 DCHECK_LT(row, num_options); | |
105 | |
106 int level = | |
107 chooser_controller_->GetSignalStrengthLevel(static_cast<size_t>(row)); | |
108 | |
109 if (level == -1) | |
110 return gfx::ImageSkia(); | |
111 | |
112 gfx::ImageSkia* images = | |
113 ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | |
114 IDR_NETWORK_BARS_DARK); | |
115 int width = images->width(); | |
116 int height = images->height() / kNumStrengthBarImages; | |
117 return gfx::ImageSkiaOperations::ExtractSubset( | |
msw
2016/08/12 23:08:49
The icons look a bit squished in the screenshot, c
juncai
2016/08/15 21:53:19
I update and use icons from:
https://icons.googlep
| |
118 *images, gfx::Rect(0, level * height, width, height)); | |
msw
2016/08/12 23:08:49
interesting! perhaps add a comment?
juncai
2016/08/15 21:53:19
I borrowed the idea from:
https://cs.chromium.org/
| |
119 } | |
120 | |
83 void ChooserContentView::OnOptionsInitialized() { | 121 void ChooserContentView::OnOptionsInitialized() { |
84 table_view_->OnModelChanged(); | 122 table_view_->OnModelChanged(); |
85 UpdateTableView(); | 123 UpdateTableView(); |
86 } | 124 } |
87 | 125 |
88 void ChooserContentView::OnOptionAdded(size_t index) { | 126 void ChooserContentView::OnOptionAdded(size_t index) { |
89 table_view_->OnItemsAdded(static_cast<int>(index), 1); | 127 table_view_->OnItemsAdded(static_cast<int>(index), 1); |
90 UpdateTableView(); | 128 UpdateTableView(); |
91 table_view_->SetVisible(true); | 129 table_view_->SetVisible(true); |
92 throbber_->SetVisible(false); | 130 throbber_->SetVisible(false); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
202 } | 240 } |
203 | 241 |
204 void ChooserContentView::UpdateTableView() { | 242 void ChooserContentView::UpdateTableView() { |
205 if (chooser_controller_->NumOptions() == 0) { | 243 if (chooser_controller_->NumOptions() == 0) { |
206 table_view_->OnModelChanged(); | 244 table_view_->OnModelChanged(); |
207 table_view_->SetEnabled(false); | 245 table_view_->SetEnabled(false); |
208 } else { | 246 } else { |
209 table_view_->SetEnabled(true); | 247 table_view_->SetEnabled(true); |
210 } | 248 } |
211 } | 249 } |
OLD | NEW |