Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ash/system/network/tray_network.h" | 5 #include "ash/system/network/tray_network.h" |
| 6 | 6 |
| 7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
| 8 #include "ash/system/tray/system_tray.h" | |
| 8 #include "ash/system/tray/system_tray_delegate.h" | 9 #include "ash/system/tray/system_tray_delegate.h" |
| 10 #include "base/utf_string_conversions.h" | |
| 11 #include "grit/ash_strings.h" | |
| 12 #include "grit/ui_resources.h" | |
| 13 #include "ui/base/resource/resource_bundle.h" | |
| 14 #include "ui/gfx/font.h" | |
| 15 #include "ui/gfx/image/image.h" | |
| 9 #include "ui/views/controls/image_view.h" | 16 #include "ui/views/controls/image_view.h" |
| 10 #include "ui/views/controls/label.h" | 17 #include "ui/views/controls/label.h" |
| 18 #include "ui/views/controls/scroll_view.h" | |
| 11 #include "ui/views/layout/fill_layout.h" | 19 #include "ui/views/layout/fill_layout.h" |
| 12 #include "ui/views/layout/box_layout.h" | 20 #include "ui/views/layout/box_layout.h" |
| 13 #include "ui/views/view.h" | 21 #include "ui/views/view.h" |
| 22 #include "ui/views/widget/widget.h" | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 // Padding for items that do not have any icons. | |
| 27 const int kItemPaddingLeft = 27; | |
| 28 | |
| 29 class ViewClickListener { | |
| 30 public: | |
| 31 virtual void ClickedOn(views::View* sender) = 0; | |
| 32 }; | |
| 33 | |
| 34 class HoverHighlightView : public views::View { | |
| 35 public: | |
| 36 explicit HoverHighlightView(ViewClickListener* listener) | |
| 37 : listener_(listener) { | |
| 38 set_notify_enter_exit_on_child(true); | |
| 39 } | |
| 40 | |
| 41 virtual ~HoverHighlightView() {} | |
| 42 | |
| 43 // Convenience function for adding an icon and a label. | |
| 44 void AddIconAndLabel(const SkBitmap& image, string16 label) { | |
| 45 SetLayoutManager(new views::BoxLayout( | |
| 46 views::BoxLayout::kHorizontal, 0, 3, 5)); | |
|
Nikita (slow)
2012/03/13 15:36:33
nit: align
| |
| 47 views::ImageView* image_view = new views::ImageView; | |
| 48 image_view->SetImage(image); | |
| 49 AddChildView(image_view); | |
| 50 AddChildView(new views::Label(label)); | |
| 51 } | |
| 52 | |
| 53 void AddLabel(string16 text) { | |
| 54 SetLayoutManager(new views::FillLayout()); | |
| 55 views::Label* label = new views::Label(text); | |
| 56 label->set_border(views::Border::CreateEmptyBorder(5, kItemPaddingLeft, | |
| 57 5, 0)); | |
|
Nikita (slow)
2012/03/13 15:36:33
nit: align
| |
| 58 label->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
| 59 AddChildView(label); | |
| 60 } | |
| 61 | |
| 62 private: | |
| 63 // Overridden from views::View. | |
| 64 virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE { | |
| 65 if (!listener_) | |
| 66 return false; | |
| 67 listener_->ClickedOn(this); | |
| 68 return true; | |
| 69 } | |
| 70 | |
| 71 virtual void OnMouseEntered(const views::MouseEvent& event) OVERRIDE { | |
| 72 set_background(views::Background::CreateSolidBackground( | |
| 73 SkColorSetARGB(10, 0, 0, 0))); | |
| 74 SchedulePaint(); | |
| 75 } | |
| 76 | |
| 77 virtual void OnMouseExited(const views::MouseEvent& event) OVERRIDE { | |
| 78 set_background(NULL); | |
| 79 SchedulePaint(); | |
| 80 } | |
| 81 | |
| 82 ViewClickListener* listener_; | |
| 83 | |
| 84 DISALLOW_COPY_AND_ASSIGN(HoverHighlightView); | |
| 85 }; | |
| 86 | |
| 87 } | |
| 14 | 88 |
| 15 namespace ash { | 89 namespace ash { |
| 16 namespace internal { | 90 namespace internal { |
| 17 | 91 |
| 18 namespace tray { | 92 namespace tray { |
| 19 | 93 |
| 20 enum ResourceSize { | 94 enum ResourceSize { |
| 21 SMALL, | 95 SMALL, |
| 22 LARGE, | 96 LARGE, |
| 23 }; | 97 }; |
| 24 | 98 |
| 25 class NetworkTrayView : public views::View { | 99 class NetworkTrayView : public views::View { |
| 26 public: | 100 public: |
| 27 explicit NetworkTrayView(ResourceSize size) : resource_size_(size) { | 101 explicit NetworkTrayView(ResourceSize size) : resource_size_(size) { |
| 28 SetLayoutManager(new views::FillLayout()); | 102 SetLayoutManager(new views::FillLayout()); |
| 29 | 103 |
| 30 image_view_ = new views::ImageView; | 104 image_view_ = new views::ImageView; |
| 31 AddChildView(image_view_); | 105 AddChildView(image_view_); |
| 32 | 106 |
| 33 Update(Shell::GetInstance()->tray_delegate()->GetMostRelevantNetworkIcon()); | 107 Update(Shell::GetInstance()->tray_delegate()-> |
| 108 GetMostRelevantNetworkIcon(resource_size_ == LARGE)); | |
| 34 } | 109 } |
| 35 | 110 |
| 36 virtual ~NetworkTrayView() {} | 111 virtual ~NetworkTrayView() {} |
| 37 | 112 |
| 38 void Update(const NetworkIconInfo& info) { | 113 void Update(const NetworkIconInfo& info) { |
| 39 image_view_->SetImage(info.image); | 114 image_view_->SetImage(info.image); |
| 40 SchedulePaint(); | 115 SchedulePaint(); |
| 41 } | 116 } |
| 42 | 117 |
| 43 private: | 118 private: |
| 44 views::ImageView* image_view_; | 119 views::ImageView* image_view_; |
| 45 ResourceSize resource_size_; | 120 ResourceSize resource_size_; |
| 46 | 121 |
| 47 DISALLOW_COPY_AND_ASSIGN(NetworkTrayView); | 122 DISALLOW_COPY_AND_ASSIGN(NetworkTrayView); |
| 48 }; | 123 }; |
| 49 | 124 |
| 50 class NetworkDefaultView : public views::View { | 125 class NetworkDefaultView : public views::View { |
| 51 public: | 126 public: |
| 52 NetworkDefaultView() { | 127 explicit NetworkDefaultView(SystemTrayItem* owner) : owner_(owner) { |
| 53 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal, | 128 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal, |
| 54 0, 0, 5)); | 129 0, 0, 5)); |
| 55 | 130 |
| 56 icon_ = new NetworkTrayView(LARGE); | 131 icon_ = new NetworkTrayView(LARGE); |
| 57 AddChildView(icon_); | 132 AddChildView(icon_); |
| 58 | 133 |
| 59 label_ = new views::Label(); | 134 label_ = new views::Label(); |
| 60 AddChildView(label_); | 135 AddChildView(label_); |
| 61 | 136 |
| 62 Update(Shell::GetInstance()->tray_delegate()->GetMostRelevantNetworkIcon()); | 137 views::ImageView* more = new views::ImageView; |
| 138 more->SetImage(ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 139 IDR_AURA_UBER_TRAY_MORE).ToSkBitmap()); | |
| 140 AddChildView(more); | |
| 141 | |
| 142 Update(Shell::GetInstance()->tray_delegate()-> | |
| 143 GetMostRelevantNetworkIcon(true)); | |
| 63 } | 144 } |
| 64 | 145 |
| 65 virtual ~NetworkDefaultView() {} | 146 virtual ~NetworkDefaultView() {} |
| 66 | 147 |
| 67 void Update(const NetworkIconInfo& info) { | 148 void Update(const NetworkIconInfo& info) { |
| 68 icon_->Update(info); | 149 icon_->Update(info); |
| 69 label_->SetText(info.description); | 150 label_->SetText(info.description); |
| 70 } | 151 } |
| 71 | 152 |
| 72 private: | 153 private: |
| 154 // Overridden from views::View. | |
| 155 virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE { | |
| 156 owner_->PopupDetailedView(0, true); | |
| 157 return true; | |
| 158 } | |
| 159 | |
| 160 SystemTrayItem* owner_; | |
| 73 NetworkTrayView* icon_; | 161 NetworkTrayView* icon_; |
| 74 views::Label* label_; | 162 views::Label* label_; |
| 75 | 163 |
| 76 DISALLOW_COPY_AND_ASSIGN(NetworkDefaultView); | 164 DISALLOW_COPY_AND_ASSIGN(NetworkDefaultView); |
| 77 }; | 165 }; |
| 78 | 166 |
| 167 class NetworkDetailedView : public views::View, | |
| 168 public ViewClickListener { | |
| 169 public: | |
| 170 explicit NetworkDetailedView(user::LoginStatus login) | |
| 171 : login_(login), | |
| 172 header_(NULL), | |
| 173 airplane_(NULL), | |
| 174 settings_(NULL), | |
| 175 proxy_settings_(NULL) { | |
| 176 SetLayoutManager(new views::BoxLayout( | |
| 177 views::BoxLayout::kVertical, 1, 1, 1)); | |
| 178 Update(); | |
| 179 } | |
| 180 | |
| 181 virtual ~NetworkDetailedView() {} | |
| 182 | |
| 183 void Update() { | |
| 184 RemoveAllChildViews(true); | |
| 185 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 186 | |
| 187 HoverHighlightView* container = new HoverHighlightView(this); | |
|
Ben Goodger (Google)
2012/03/13 15:31:18
AppendNetworkHeaderEntry();
sadrul
2012/03/13 15:48:39
Done.
| |
| 188 container->SetLayoutManager(new | |
| 189 views::BoxLayout(views::BoxLayout::kHorizontal, 0, 3, 5)); | |
| 190 views::ImageView* back = new views::ImageView; | |
| 191 back->SetImage(rb.GetImageNamed(IDR_AURA_UBER_TRAY_LESS).ToSkBitmap()); | |
| 192 container->AddChildView(back); | |
| 193 views::Label* header = new views::Label(rb.GetLocalizedString( | |
| 194 IDS_ASH_STATUS_TRAY_NETWORK)); | |
| 195 header->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
| 196 header->SetFont(header->font().DeriveFont(4)); | |
| 197 container->AddChildView(header); | |
| 198 AddChildView(container); | |
| 199 header_ = container; | |
| 200 | |
|
Nikita (slow)
2012/03/13 15:36:33
Where will be enable/disable interface (WiFi/Cellu
sadrul
2012/03/13 15:48:39
I am not sure. They are not in the mocks [yet]. I
| |
| 201 // List the networks. | |
| 202 std::vector<NetworkIconInfo> list; | |
|
Ben Goodger (Google)
2012/03/13 15:31:18
can't these lines go into AppendNetworkEntries() t
sadrul
2012/03/13 15:48:39
Done.
| |
| 203 Shell::GetInstance()->tray_delegate()->GetAvailableNetworks(&list); | |
| 204 AppendNetworkEntries(list); | |
| 205 | |
| 206 // Airplane mode. | |
| 207 container = new HoverHighlightView(this); | |
|
Ben Goodger (Google)
2012/03/13 15:31:18
AppendAirplaneModeEntry();
sadrul
2012/03/13 15:48:39
Done.
| |
| 208 container->AddIconAndLabel( | |
| 209 *rb.GetImageNamed(IDR_AURA_UBER_TRAY_NETWORK_AIRPLANE).ToSkBitmap(), | |
| 210 rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_AIRPLANE_MODE)); | |
| 211 AddChildView(container); | |
| 212 airplane_ = container; | |
| 213 | |
| 214 AppendSettingsEntry(); | |
| 215 } | |
| 216 | |
| 217 private: | |
| 218 void AppendNetworkEntries(std::vector<NetworkIconInfo>& list) { | |
| 219 views::View* networks = new views::View; | |
| 220 networks->SetLayoutManager(new views::BoxLayout( | |
| 221 views::BoxLayout::kVertical, 0, 0, 1)); | |
| 222 for (size_t i = 0; i < list.size(); i++) { | |
| 223 HoverHighlightView* container = new HoverHighlightView(this); | |
| 224 container->AddIconAndLabel(list[i].image, list[i].name); | |
| 225 networks->AddChildView(container); | |
| 226 network_map_[container] = list[i].unique_id; | |
| 227 } | |
| 228 networks->set_border(views::Border::CreateSolidSidedBorder(1, 0, 1, 0, | |
| 229 SkColorSetARGB(25, 0, 0, 0))); | |
| 230 AddChildView(networks); | |
| 231 } | |
| 232 | |
| 233 // Adds a settings entry when logged in, and an entry for changing proxy | |
| 234 // settings otherwise. | |
| 235 void AppendSettingsEntry() { | |
| 236 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 237 if (login_ != user::LOGGED_IN_NONE) { | |
| 238 // Settings, only if logged in. | |
| 239 HoverHighlightView* container = new HoverHighlightView(this); | |
| 240 container->AddLabel(rb.GetLocalizedString( | |
| 241 IDS_ASH_STATUS_TRAY_NETWORK_SETTINGS)); | |
| 242 AddChildView(container); | |
| 243 settings_ = container; | |
| 244 } else { | |
| 245 // Allow changing proxy settings in the login screen. | |
| 246 HoverHighlightView* container = new HoverHighlightView(this); | |
| 247 container->AddLabel(rb.GetLocalizedString( | |
| 248 IDS_ASH_STATUS_TRAY_NETWORK_PROXY_SETTINGS)); | |
| 249 AddChildView(container); | |
| 250 proxy_settings_ = container; | |
| 251 } | |
| 252 } | |
| 253 | |
| 254 // Overridden from ViewClickListener. | |
| 255 virtual void ClickedOn(views::View* sender) OVERRIDE { | |
| 256 ash::SystemTrayDelegate* delegate = | |
| 257 ash::Shell::GetInstance()->tray_delegate(); | |
| 258 if (sender == header_) { | |
| 259 Shell::GetInstance()->tray()->ShowDefaultView(); | |
| 260 } else if (sender == settings_) { | |
| 261 delegate->ShowNetworkSettings(); | |
| 262 } else if (sender == proxy_settings_) { | |
| 263 delegate->ChangeProxySettings(); | |
| 264 } else if (sender == airplane_) { | |
| 265 delegate->ToggleAirplaneMode(); | |
| 266 } else { | |
| 267 std::map<views::View*, std::string>::iterator find; | |
| 268 find = network_map_.find(sender); | |
| 269 if (find != network_map_.end()) { | |
| 270 std::string network_id = find->second; | |
| 271 delegate->ConnectToNetwork(network_id); | |
| 272 } | |
| 273 } | |
| 274 } | |
| 275 | |
| 276 user::LoginStatus login_; | |
| 277 std::map<views::View*, std::string> network_map_; | |
| 278 views::View* header_; | |
| 279 views::View* airplane_; | |
| 280 views::View* settings_; | |
| 281 views::View* proxy_settings_; | |
| 282 DISALLOW_COPY_AND_ASSIGN(NetworkDetailedView); | |
| 283 }; | |
| 284 | |
| 79 } // namespace tray | 285 } // namespace tray |
| 80 | 286 |
| 81 TrayNetwork::TrayNetwork() { | 287 TrayNetwork::TrayNetwork() { |
| 82 } | 288 } |
| 83 | 289 |
| 84 TrayNetwork::~TrayNetwork() { | 290 TrayNetwork::~TrayNetwork() { |
| 85 } | 291 } |
| 86 | 292 |
| 87 views::View* TrayNetwork::CreateTrayView(user::LoginStatus status) { | 293 views::View* TrayNetwork::CreateTrayView(user::LoginStatus status) { |
| 88 tray_.reset(new tray::NetworkTrayView(tray::SMALL)); | 294 tray_.reset(new tray::NetworkTrayView(tray::SMALL)); |
| 89 return tray_.get(); | 295 return tray_.get(); |
| 90 } | 296 } |
| 91 | 297 |
| 92 views::View* TrayNetwork::CreateDefaultView(user::LoginStatus status) { | 298 views::View* TrayNetwork::CreateDefaultView(user::LoginStatus status) { |
| 93 default_.reset(new tray::NetworkDefaultView); | 299 default_.reset(new tray::NetworkDefaultView(this)); |
| 94 return default_.get(); | 300 return default_.get(); |
| 95 } | 301 } |
| 96 | 302 |
| 97 views::View* TrayNetwork::CreateDetailedView(user::LoginStatus status) { | 303 views::View* TrayNetwork::CreateDetailedView(user::LoginStatus status) { |
| 98 return NULL; | 304 detailed_.reset(new tray::NetworkDetailedView(status)); |
| 305 return detailed_.get(); | |
| 99 } | 306 } |
| 100 | 307 |
| 101 void TrayNetwork::DestroyTrayView() { | 308 void TrayNetwork::DestroyTrayView() { |
| 102 tray_.reset(); | 309 tray_.reset(); |
| 103 } | 310 } |
| 104 | 311 |
| 105 void TrayNetwork::DestroyDefaultView() { | 312 void TrayNetwork::DestroyDefaultView() { |
| 106 default_.reset(); | 313 default_.reset(); |
| 107 } | 314 } |
| 108 | 315 |
| 109 void TrayNetwork::DestroyDetailedView() { | 316 void TrayNetwork::DestroyDetailedView() { |
| 317 detailed_.reset(); | |
| 110 } | 318 } |
| 111 | 319 |
| 112 void TrayNetwork::OnNetworkRefresh(const NetworkIconInfo& info) { | 320 void TrayNetwork::OnNetworkRefresh(const NetworkIconInfo& info) { |
| 113 if (tray_.get()) | 321 if (tray_.get()) |
| 114 tray_->Update(info); | 322 tray_->Update(info); |
| 115 if (default_.get()) | 323 if (default_.get()) |
| 116 default_->Update(info); | 324 default_->Update(info); |
| 325 if (detailed_.get()) | |
| 326 detailed_->Update(); | |
| 117 } | 327 } |
| 118 | 328 |
| 119 } // namespace internal | 329 } // namespace internal |
| 120 } // namespace ash | 330 } // namespace ash |
| OLD | NEW |