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 // Height of the list of networks in the popup. | |
| 29 const int kNetworkListHeight = 160; | |
| 30 | |
| 31 class ViewClickListener { | |
| 32 public: | |
| 33 virtual void ClickedOn(views::View* sender) = 0; | |
| 34 }; | |
| 35 | |
| 36 class HoverHighlightView : public views::View { | |
| 37 public: | |
| 38 explicit HoverHighlightView(ViewClickListener* listener) | |
| 39 : listener_(listener) { | |
| 40 set_notify_enter_exit_on_child(true); | |
| 41 } | |
| 42 | |
| 43 virtual ~HoverHighlightView() {} | |
| 44 | |
| 45 // Convenience function for adding an icon and a label. | |
| 46 void AddIconAndLabel(const SkBitmap& image, string16 label) { | |
|
Nikita (slow)
2012/03/14 16:48:45
const string16&
sadrul
2012/03/14 18:37:46
Done.
| |
| 47 SetLayoutManager(new views::BoxLayout( | |
| 48 views::BoxLayout::kHorizontal, 0, 3, 5)); | |
| 49 views::ImageView* image_view = new views::ImageView; | |
| 50 image_view->SetImage(image); | |
| 51 AddChildView(image_view); | |
| 52 AddChildView(new views::Label(label)); | |
| 53 } | |
| 54 | |
| 55 void AddLabel(string16 text) { | |
|
Nikita (slow)
2012/03/14 16:48:45
const string16&
sadrul
2012/03/14 18:37:46
Done.
| |
| 56 SetLayoutManager(new views::FillLayout()); | |
| 57 views::Label* label = new views::Label(text); | |
| 58 label->set_border(views::Border::CreateEmptyBorder(5, kItemPaddingLeft, | |
| 59 5, 0)); | |
| 60 label->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
| 61 AddChildView(label); | |
| 62 } | |
| 63 | |
| 64 private: | |
| 65 // Overridden from views::View. | |
| 66 virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE { | |
| 67 if (!listener_) | |
| 68 return false; | |
| 69 listener_->ClickedOn(this); | |
| 70 return true; | |
| 71 } | |
| 72 | |
| 73 virtual void OnMouseEntered(const views::MouseEvent& event) OVERRIDE { | |
| 74 set_background(views::Background::CreateSolidBackground( | |
| 75 SkColorSetARGB(10, 0, 0, 0))); | |
| 76 SchedulePaint(); | |
| 77 } | |
| 78 | |
| 79 virtual void OnMouseExited(const views::MouseEvent& event) OVERRIDE { | |
| 80 set_background(NULL); | |
| 81 SchedulePaint(); | |
| 82 } | |
| 83 | |
| 84 ViewClickListener* listener_; | |
| 85 | |
| 86 DISALLOW_COPY_AND_ASSIGN(HoverHighlightView); | |
| 87 }; | |
| 88 | |
| 89 // A custom scroll-view that has a specified dimension. | |
| 90 class FixedSizedScrollView : public views::ScrollView { | |
| 91 public: | |
| 92 FixedSizedScrollView() {} | |
| 93 virtual ~FixedSizedScrollView() {} | |
| 94 | |
| 95 void SetContentsView(View* view) { | |
| 96 SetContents(view); | |
| 97 view->SetBoundsRect(gfx::Rect(view->GetPreferredSize())); | |
| 98 } | |
| 99 | |
| 100 void set_fixed_size(gfx::Size size) { fixed_size_ = size; } | |
| 101 | |
| 102 private: | |
| 103 // Overridden from views::View. | |
| 104 virtual gfx::Size GetPreferredSize() OVERRIDE { | |
| 105 return fixed_size_; | |
| 106 } | |
| 107 | |
| 108 gfx::Size fixed_size_; | |
| 109 | |
| 110 DISALLOW_COPY_AND_ASSIGN(FixedSizedScrollView); | |
| 111 }; | |
| 112 | |
| 113 } | |
| 14 | 114 |
| 15 namespace ash { | 115 namespace ash { |
| 16 namespace internal { | 116 namespace internal { |
| 17 | 117 |
| 18 namespace tray { | 118 namespace tray { |
| 19 | 119 |
| 20 enum ResourceSize { | 120 enum ResourceSize { |
| 21 SMALL, | 121 SMALL, |
| 22 LARGE, | 122 LARGE, |
| 23 }; | 123 }; |
| 24 | 124 |
| 25 class NetworkTrayView : public views::View { | 125 class NetworkTrayView : public views::View { |
| 26 public: | 126 public: |
| 27 explicit NetworkTrayView(ResourceSize size) : resource_size_(size) { | 127 explicit NetworkTrayView(ResourceSize size) : resource_size_(size) { |
| 28 SetLayoutManager(new views::FillLayout()); | 128 SetLayoutManager(new views::FillLayout()); |
| 29 | 129 |
| 30 image_view_ = new views::ImageView; | 130 image_view_ = new views::ImageView; |
| 31 AddChildView(image_view_); | 131 AddChildView(image_view_); |
| 32 | 132 |
| 33 Update(Shell::GetInstance()->tray_delegate()->GetMostRelevantNetworkIcon()); | 133 Update(Shell::GetInstance()->tray_delegate()-> |
| 134 GetMostRelevantNetworkIcon(resource_size_ == LARGE)); | |
| 34 } | 135 } |
| 35 | 136 |
| 36 virtual ~NetworkTrayView() {} | 137 virtual ~NetworkTrayView() {} |
| 37 | 138 |
| 38 void Update(const NetworkIconInfo& info) { | 139 void Update(const NetworkIconInfo& info) { |
| 39 image_view_->SetImage(info.image); | 140 image_view_->SetImage(info.image); |
| 40 SchedulePaint(); | 141 SchedulePaint(); |
| 41 } | 142 } |
| 42 | 143 |
| 43 private: | 144 private: |
| 44 views::ImageView* image_view_; | 145 views::ImageView* image_view_; |
| 45 ResourceSize resource_size_; | 146 ResourceSize resource_size_; |
| 46 | 147 |
| 47 DISALLOW_COPY_AND_ASSIGN(NetworkTrayView); | 148 DISALLOW_COPY_AND_ASSIGN(NetworkTrayView); |
| 48 }; | 149 }; |
| 49 | 150 |
| 50 class NetworkDefaultView : public views::View { | 151 class NetworkDefaultView : public views::View { |
| 51 public: | 152 public: |
| 52 NetworkDefaultView() { | 153 explicit NetworkDefaultView(SystemTrayItem* owner) : owner_(owner) { |
| 53 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal, | 154 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal, |
| 54 0, 0, 5)); | 155 0, 0, 5)); |
| 55 | 156 |
| 56 icon_ = new NetworkTrayView(LARGE); | 157 icon_ = new NetworkTrayView(LARGE); |
| 57 AddChildView(icon_); | 158 AddChildView(icon_); |
| 58 | 159 |
| 59 label_ = new views::Label(); | 160 label_ = new views::Label(); |
| 60 AddChildView(label_); | 161 AddChildView(label_); |
| 61 | 162 |
| 62 Update(Shell::GetInstance()->tray_delegate()->GetMostRelevantNetworkIcon()); | 163 views::ImageView* more = new views::ImageView; |
| 164 more->SetImage(ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 165 IDR_AURA_UBER_TRAY_MORE).ToSkBitmap()); | |
| 166 AddChildView(more); | |
| 167 | |
| 168 Update(Shell::GetInstance()->tray_delegate()-> | |
| 169 GetMostRelevantNetworkIcon(true)); | |
| 63 } | 170 } |
| 64 | 171 |
| 65 virtual ~NetworkDefaultView() {} | 172 virtual ~NetworkDefaultView() {} |
| 66 | 173 |
| 67 void Update(const NetworkIconInfo& info) { | 174 void Update(const NetworkIconInfo& info) { |
| 68 icon_->Update(info); | 175 icon_->Update(info); |
| 69 label_->SetText(info.description); | 176 label_->SetText(info.description); |
| 70 } | 177 } |
| 71 | 178 |
| 72 private: | 179 private: |
| 180 // Overridden from views::View. | |
| 181 virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE { | |
| 182 owner_->PopupDetailedView(0, true); | |
| 183 return true; | |
| 184 } | |
| 185 | |
| 186 SystemTrayItem* owner_; | |
| 73 NetworkTrayView* icon_; | 187 NetworkTrayView* icon_; |
| 74 views::Label* label_; | 188 views::Label* label_; |
| 75 | 189 |
| 76 DISALLOW_COPY_AND_ASSIGN(NetworkDefaultView); | 190 DISALLOW_COPY_AND_ASSIGN(NetworkDefaultView); |
| 77 }; | 191 }; |
| 78 | 192 |
| 193 class NetworkDetailedView : public views::View, | |
| 194 public ViewClickListener { | |
| 195 public: | |
| 196 explicit NetworkDetailedView(user::LoginStatus login) | |
| 197 : login_(login), | |
| 198 header_(NULL), | |
| 199 airplane_(NULL), | |
| 200 settings_(NULL), | |
| 201 proxy_settings_(NULL) { | |
| 202 SetLayoutManager(new views::BoxLayout( | |
| 203 views::BoxLayout::kVertical, 1, 1, 1)); | |
| 204 Update(); | |
| 205 } | |
| 206 | |
| 207 virtual ~NetworkDetailedView() {} | |
| 208 | |
| 209 void Update() { | |
| 210 RemoveAllChildViews(true); | |
| 211 | |
| 212 AppendHeaderEntry(); | |
| 213 AppendNetworkEntries(); | |
| 214 AppendAirplaneModeEntry(); | |
| 215 AppendSettingsEntry(); | |
| 216 } | |
| 217 | |
| 218 private: | |
| 219 void AppendHeaderEntry() { | |
| 220 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 221 HoverHighlightView* container = new HoverHighlightView(this); | |
| 222 container->SetLayoutManager(new | |
| 223 views::BoxLayout(views::BoxLayout::kHorizontal, 0, 3, 5)); | |
| 224 views::ImageView* back = new views::ImageView; | |
| 225 back->SetImage(rb.GetImageNamed(IDR_AURA_UBER_TRAY_LESS).ToSkBitmap()); | |
| 226 container->AddChildView(back); | |
| 227 views::Label* header = new views::Label(rb.GetLocalizedString( | |
| 228 IDS_ASH_STATUS_TRAY_NETWORK)); | |
| 229 header->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
| 230 header->SetFont(header->font().DeriveFont(4)); | |
| 231 container->AddChildView(header); | |
| 232 AddChildView(container); | |
| 233 header_ = container; | |
| 234 } | |
| 235 | |
| 236 void AppendNetworkEntries() { | |
| 237 std::vector<NetworkIconInfo> list; | |
| 238 Shell::GetInstance()->tray_delegate()->GetAvailableNetworks(&list); | |
| 239 FixedSizedScrollView* scroller = new FixedSizedScrollView; | |
| 240 views::View* networks = new views::View; | |
| 241 networks->SetLayoutManager(new views::BoxLayout( | |
| 242 views::BoxLayout::kVertical, 0, 0, 1)); | |
| 243 for (size_t i = 0; i < list.size(); i++) { | |
| 244 HoverHighlightView* container = new HoverHighlightView(this); | |
| 245 container->AddIconAndLabel(list[i].image, list[i].name); | |
| 246 networks->AddChildView(container); | |
| 247 network_map_[container] = list[i].unique_id; | |
| 248 } | |
| 249 scroller->set_border(views::Border::CreateSolidSidedBorder(1, 0, 1, 0, | |
| 250 SkColorSetARGB(25, 0, 0, 0))); | |
| 251 scroller->set_fixed_size( | |
| 252 gfx::Size(networks->GetPreferredSize().width() + | |
| 253 scroller->GetScrollBarWidth(), | |
| 254 kNetworkListHeight)); | |
| 255 scroller->SetContentsView(networks); | |
| 256 AddChildView(scroller); | |
| 257 } | |
| 258 | |
| 259 void AppendAirplaneModeEntry() { | |
| 260 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 261 HoverHighlightView* container = new HoverHighlightView(this); | |
| 262 container->AddIconAndLabel( | |
| 263 *rb.GetImageNamed(IDR_AURA_UBER_TRAY_NETWORK_AIRPLANE).ToSkBitmap(), | |
| 264 rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_AIRPLANE_MODE)); | |
| 265 AddChildView(container); | |
| 266 airplane_ = container; | |
| 267 } | |
| 268 | |
| 269 // Adds a settings entry when logged in, and an entry for changing proxy | |
| 270 // settings otherwise. | |
| 271 void AppendSettingsEntry() { | |
| 272 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 273 if (login_ != user::LOGGED_IN_NONE) { | |
| 274 // Settings, only if logged in. | |
| 275 HoverHighlightView* container = new HoverHighlightView(this); | |
| 276 container->AddLabel(rb.GetLocalizedString( | |
| 277 IDS_ASH_STATUS_TRAY_NETWORK_SETTINGS)); | |
| 278 AddChildView(container); | |
| 279 settings_ = container; | |
| 280 } else { | |
| 281 // Allow changing proxy settings in the login screen. | |
| 282 HoverHighlightView* container = new HoverHighlightView(this); | |
| 283 container->AddLabel(rb.GetLocalizedString( | |
| 284 IDS_ASH_STATUS_TRAY_NETWORK_PROXY_SETTINGS)); | |
| 285 AddChildView(container); | |
| 286 proxy_settings_ = container; | |
| 287 } | |
| 288 } | |
| 289 | |
| 290 // Overridden from ViewClickListener. | |
| 291 virtual void ClickedOn(views::View* sender) OVERRIDE { | |
| 292 ash::SystemTrayDelegate* delegate = | |
| 293 ash::Shell::GetInstance()->tray_delegate(); | |
| 294 if (sender == header_) { | |
| 295 Shell::GetInstance()->tray()->ShowDefaultView(); | |
| 296 } else if (sender == settings_) { | |
| 297 delegate->ShowNetworkSettings(); | |
| 298 } else if (sender == proxy_settings_) { | |
| 299 delegate->ChangeProxySettings(); | |
| 300 } else if (sender == airplane_) { | |
| 301 delegate->ToggleAirplaneMode(); | |
| 302 } else { | |
| 303 std::map<views::View*, std::string>::iterator find; | |
| 304 find = network_map_.find(sender); | |
| 305 if (find != network_map_.end()) { | |
| 306 std::string network_id = find->second; | |
| 307 delegate->ConnectToNetwork(network_id); | |
| 308 } | |
| 309 } | |
| 310 } | |
| 311 | |
| 312 user::LoginStatus login_; | |
| 313 std::map<views::View*, std::string> network_map_; | |
| 314 views::View* header_; | |
| 315 views::View* airplane_; | |
| 316 views::View* settings_; | |
| 317 views::View* proxy_settings_; | |
| 318 DISALLOW_COPY_AND_ASSIGN(NetworkDetailedView); | |
| 319 }; | |
| 320 | |
| 79 } // namespace tray | 321 } // namespace tray |
| 80 | 322 |
| 81 TrayNetwork::TrayNetwork() { | 323 TrayNetwork::TrayNetwork() { |
| 82 } | 324 } |
| 83 | 325 |
| 84 TrayNetwork::~TrayNetwork() { | 326 TrayNetwork::~TrayNetwork() { |
| 85 } | 327 } |
| 86 | 328 |
| 87 views::View* TrayNetwork::CreateTrayView(user::LoginStatus status) { | 329 views::View* TrayNetwork::CreateTrayView(user::LoginStatus status) { |
| 88 tray_.reset(new tray::NetworkTrayView(tray::SMALL)); | 330 tray_.reset(new tray::NetworkTrayView(tray::SMALL)); |
| 89 return tray_.get(); | 331 return tray_.get(); |
| 90 } | 332 } |
| 91 | 333 |
| 92 views::View* TrayNetwork::CreateDefaultView(user::LoginStatus status) { | 334 views::View* TrayNetwork::CreateDefaultView(user::LoginStatus status) { |
| 93 default_.reset(new tray::NetworkDefaultView); | 335 default_.reset(new tray::NetworkDefaultView(this)); |
| 94 return default_.get(); | 336 return default_.get(); |
| 95 } | 337 } |
| 96 | 338 |
| 97 views::View* TrayNetwork::CreateDetailedView(user::LoginStatus status) { | 339 views::View* TrayNetwork::CreateDetailedView(user::LoginStatus status) { |
| 98 return NULL; | 340 detailed_.reset(new tray::NetworkDetailedView(status)); |
| 341 return detailed_.get(); | |
| 99 } | 342 } |
| 100 | 343 |
| 101 void TrayNetwork::DestroyTrayView() { | 344 void TrayNetwork::DestroyTrayView() { |
| 102 tray_.reset(); | 345 tray_.reset(); |
| 103 } | 346 } |
| 104 | 347 |
| 105 void TrayNetwork::DestroyDefaultView() { | 348 void TrayNetwork::DestroyDefaultView() { |
| 106 default_.reset(); | 349 default_.reset(); |
| 107 } | 350 } |
| 108 | 351 |
| 109 void TrayNetwork::DestroyDetailedView() { | 352 void TrayNetwork::DestroyDetailedView() { |
| 353 detailed_.reset(); | |
| 110 } | 354 } |
| 111 | 355 |
| 112 void TrayNetwork::OnNetworkRefresh(const NetworkIconInfo& info) { | 356 void TrayNetwork::OnNetworkRefresh(const NetworkIconInfo& info) { |
| 113 if (tray_.get()) | 357 if (tray_.get()) |
| 114 tray_->Update(info); | 358 tray_->Update(info); |
| 115 if (default_.get()) | 359 if (default_.get()) |
| 116 default_->Update(info); | 360 default_->Update(info); |
| 361 if (detailed_.get()) | |
| 362 detailed_->Update(); | |
| 117 } | 363 } |
| 118 | 364 |
| 119 } // namespace internal | 365 } // namespace internal |
| 120 } // namespace ash | 366 } // namespace ash |
| OLD | NEW |