OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/chromeos/network_menu_button.h" | 5 #include "chrome/browser/chromeos/network_menu_button.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "app/gfx/canvas.h" | 9 #include "app/gfx/canvas.h" |
10 #include "app/l10n_util.h" | 10 #include "app/l10n_util.h" |
11 #include "app/resource_bundle.h" | 11 #include "app/resource_bundle.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "grit/generated_resources.h" | 13 #include "grit/generated_resources.h" |
14 #include "grit/theme_resources.h" | 14 #include "grit/theme_resources.h" |
15 #include "views/widget/widget.h" | 15 #include "views/widget/widget.h" |
16 #include "views/window/window.h" | 16 #include "views/window/window.h" |
17 | 17 |
| 18 namespace chromeos { |
| 19 |
18 //////////////////////////////////////////////////////////////////////////////// | 20 //////////////////////////////////////////////////////////////////////////////// |
19 // NetworkMenuButton | 21 // NetworkMenuButton |
20 | 22 |
21 // static | 23 // static |
22 const int NetworkMenuButton::kNumWifiImages = 3; | 24 const int NetworkMenuButton::kNumWifiImages = 3; |
23 const int NetworkMenuButton::kMinOpacity = 50; | 25 const int NetworkMenuButton::kMinOpacity = 50; |
24 const int NetworkMenuButton::kMaxOpacity = 256; | 26 const int NetworkMenuButton::kMaxOpacity = 256; |
25 const int NetworkMenuButton::kThrobDuration = 1000; | 27 const int NetworkMenuButton::kThrobDuration = 1000; |
26 | 28 |
27 NetworkMenuButton::NetworkMenuButton(gfx::NativeWindow browser_window) | 29 NetworkMenuButton::NetworkMenuButton(gfx::NativeWindow browser_window) |
28 : StatusAreaButton(this), | 30 : StatusAreaButton(this), |
29 ALLOW_THIS_IN_INITIALIZER_LIST(network_menu_(this)), | 31 ALLOW_THIS_IN_INITIALIZER_LIST(network_menu_(this)), |
30 browser_window_(browser_window), | 32 browser_window_(browser_window), |
31 ALLOW_THIS_IN_INITIALIZER_LIST(animation_connecting_(this)), | 33 ALLOW_THIS_IN_INITIALIZER_LIST(animation_connecting_(this)), |
32 ALLOW_THIS_IN_INITIALIZER_LIST(animation_downloading_(this)), | 34 ALLOW_THIS_IN_INITIALIZER_LIST(animation_downloading_(this)), |
33 ALLOW_THIS_IN_INITIALIZER_LIST(animation_uploading_(this)) { | 35 ALLOW_THIS_IN_INITIALIZER_LIST(animation_uploading_(this)) { |
34 animation_connecting_.SetThrobDuration(kThrobDuration); | 36 animation_connecting_.SetThrobDuration(kThrobDuration); |
35 animation_connecting_.SetTweenType(SlideAnimation::NONE); | 37 animation_connecting_.SetTweenType(SlideAnimation::NONE); |
36 animation_downloading_.SetThrobDuration(kThrobDuration); | 38 animation_downloading_.SetThrobDuration(kThrobDuration); |
37 animation_downloading_.SetTweenType(SlideAnimation::NONE); | 39 animation_downloading_.SetTweenType(SlideAnimation::NONE); |
38 animation_uploading_.SetThrobDuration(kThrobDuration); | 40 animation_uploading_.SetThrobDuration(kThrobDuration); |
39 animation_uploading_.SetTweenType(SlideAnimation::NONE); | 41 animation_uploading_.SetTweenType(SlideAnimation::NONE); |
40 NetworkChanged(CrosNetworkLibrary::Get()); | 42 NetworkChanged(NetworkLibrary::Get()); |
41 CrosNetworkLibrary::Get()->AddObserver(this); | 43 NetworkLibrary::Get()->AddObserver(this); |
42 } | 44 } |
43 | 45 |
44 NetworkMenuButton::~NetworkMenuButton() { | 46 NetworkMenuButton::~NetworkMenuButton() { |
45 CrosNetworkLibrary::Get()->RemoveObserver(this); | 47 NetworkLibrary::Get()->RemoveObserver(this); |
46 } | 48 } |
47 | 49 |
48 //////////////////////////////////////////////////////////////////////////////// | 50 //////////////////////////////////////////////////////////////////////////////// |
49 // NetworkMenuButton, views::Menu2Model implementation: | 51 // NetworkMenuButton, views::Menu2Model implementation: |
50 | 52 |
51 int NetworkMenuButton::GetItemCount() const { | 53 int NetworkMenuButton::GetItemCount() const { |
52 // The menu contains the available wifi networks. If there are none, then it | 54 // The menu contains the available wifi networks. If there are none, then it |
53 // only has one item with a message that no networks are available. | 55 // only has one item with a message that no networks are available. |
54 return wifi_networks_.empty() ? 1 : static_cast<int>(wifi_networks_.size()); | 56 return wifi_networks_.empty() ? 1 : static_cast<int>(wifi_networks_.size()); |
55 } | 57 } |
56 | 58 |
57 views::Menu2Model::ItemType NetworkMenuButton::GetTypeAt(int index) const { | 59 views::Menu2Model::ItemType NetworkMenuButton::GetTypeAt(int index) const { |
58 return wifi_networks_.empty() ? views::Menu2Model::TYPE_COMMAND : | 60 return wifi_networks_.empty() ? views::Menu2Model::TYPE_COMMAND : |
59 views::Menu2Model::TYPE_CHECK; | 61 views::Menu2Model::TYPE_CHECK; |
60 } | 62 } |
61 | 63 |
62 string16 NetworkMenuButton::GetLabelAt(int index) const { | 64 string16 NetworkMenuButton::GetLabelAt(int index) const { |
63 return wifi_networks_.empty() ? | 65 return wifi_networks_.empty() ? |
64 l10n_util::GetStringUTF16(IDS_STATUSBAR_NO_NETWORKS_MESSAGE) : | 66 l10n_util::GetStringUTF16(IDS_STATUSBAR_NO_NETWORKS_MESSAGE) : |
65 ASCIIToUTF16(wifi_networks_[index].ssid); | 67 ASCIIToUTF16(wifi_networks_[index].ssid); |
66 } | 68 } |
67 | 69 |
68 bool NetworkMenuButton::IsItemCheckedAt(int index) const { | 70 bool NetworkMenuButton::IsItemCheckedAt(int index) const { |
69 // WifiNetwork that we are connected to (or connecting to) is checked. | 71 // WifiNetwork that we are connected to (or connecting to) is checked. |
70 return wifi_networks_.empty() ? false : | 72 return wifi_networks_.empty() ? false : |
71 wifi_networks_[index].ssid == CrosNetworkLibrary::Get()->wifi_ssid(); | 73 wifi_networks_[index].ssid == NetworkLibrary::Get()->wifi_ssid(); |
72 } | 74 } |
73 | 75 |
74 bool NetworkMenuButton::IsEnabledAt(int index) const { | 76 bool NetworkMenuButton::IsEnabledAt(int index) const { |
75 return !wifi_networks_.empty(); | 77 return !wifi_networks_.empty(); |
76 } | 78 } |
77 | 79 |
78 void NetworkMenuButton::ActivatedAt(int index) { | 80 void NetworkMenuButton::ActivatedAt(int index) { |
79 // When we are refreshing the menu, ignore menu item activation. | 81 // When we are refreshing the menu, ignore menu item activation. |
80 if (refreshing_menu_) | 82 if (refreshing_menu_) |
81 return; | 83 return; |
82 | 84 |
83 CrosNetworkLibrary* cros = CrosNetworkLibrary::Get(); | 85 NetworkLibrary* cros = NetworkLibrary::Get(); |
84 | 86 |
85 // If clicked on a network that we are already connected to or we are | 87 // If clicked on a network that we are already connected to or we are |
86 // currently trying to connect to, then do nothing. | 88 // currently trying to connect to, then do nothing. |
87 if (wifi_networks_[index].ssid == cros->wifi_ssid()) | 89 if (wifi_networks_[index].ssid == cros->wifi_ssid()) |
88 return; | 90 return; |
89 | 91 |
90 activated_wifi_network_ = wifi_networks_[index]; | 92 activated_wifi_network_ = wifi_networks_[index]; |
91 | 93 |
92 // If wifi network is not encrypted, then directly connect. | 94 // If wifi network is not encrypted, then directly connect. |
93 // Otherwise, we open password dialog window. | 95 // Otherwise, we open password dialog window. |
(...skipping 12 matching lines...) Expand all Loading... |
106 window->SetBounds(gfx::Rect(point, size), browser_window_); | 108 window->SetBounds(gfx::Rect(point, size), browser_window_); |
107 window->Show(); | 109 window->Show(); |
108 } | 110 } |
109 } | 111 } |
110 | 112 |
111 //////////////////////////////////////////////////////////////////////////////// | 113 //////////////////////////////////////////////////////////////////////////////// |
112 // NetworkMenuButton, PasswordDialogDelegate implementation: | 114 // NetworkMenuButton, PasswordDialogDelegate implementation: |
113 | 115 |
114 bool NetworkMenuButton::OnPasswordDialogAccept(const std::string& ssid, | 116 bool NetworkMenuButton::OnPasswordDialogAccept(const std::string& ssid, |
115 const string16& password) { | 117 const string16& password) { |
116 CrosNetworkLibrary::Get()->ConnectToWifiNetwork(activated_wifi_network_, | 118 NetworkLibrary::Get()->ConnectToWifiNetwork(activated_wifi_network_, |
117 password); | 119 password); |
118 return true; | 120 return true; |
119 } | 121 } |
120 | 122 |
121 //////////////////////////////////////////////////////////////////////////////// | 123 //////////////////////////////////////////////////////////////////////////////// |
122 // NetworkMenuButton, AnimationDelegate implementation: | 124 // NetworkMenuButton, AnimationDelegate implementation: |
123 | 125 |
124 void NetworkMenuButton::AnimationProgressed(const Animation* animation) { | 126 void NetworkMenuButton::AnimationProgressed(const Animation* animation) { |
125 if (animation == &animation_connecting_ || | 127 if (animation == &animation_connecting_ || |
126 animation == &animation_downloading_ || | 128 animation == &animation_downloading_ || |
127 animation == &animation_uploading_) | 129 animation == &animation_uploading_) |
128 SchedulePaint(); | 130 SchedulePaint(); |
129 else | 131 else |
130 MenuButton::AnimationProgressed(animation); | 132 MenuButton::AnimationProgressed(animation); |
131 } | 133 } |
132 | 134 |
133 //////////////////////////////////////////////////////////////////////////////// | 135 //////////////////////////////////////////////////////////////////////////////// |
134 // NetworkMenuButton, StatusAreaButton implementation: | 136 // NetworkMenuButton, StatusAreaButton implementation: |
135 | 137 |
136 // Override the DrawIcon method to draw the wifi icon. | 138 // Override the DrawIcon method to draw the wifi icon. |
137 // The wifi icon is composed of 1 or more alpha-blended icons to show the | 139 // The wifi icon is composed of 1 or more alpha-blended icons to show the |
138 // network strength. We also draw an animation for when there's upload/download | 140 // network strength. We also draw an animation for when there's upload/download |
139 // traffic. | 141 // traffic. |
140 void NetworkMenuButton::DrawIcon(gfx::Canvas* canvas) { | 142 void NetworkMenuButton::DrawIcon(gfx::Canvas* canvas) { |
141 // First draw the base icon. | 143 // First draw the base icon. |
142 canvas->DrawBitmapInt(icon(), 0, 0); | 144 canvas->DrawBitmapInt(icon(), 0, 0); |
143 | 145 |
144 // If wifi, we draw the wifi signal bars. | 146 // If wifi, we draw the wifi signal bars. |
145 CrosNetworkLibrary* cros = CrosNetworkLibrary::Get(); | 147 NetworkLibrary* cros = NetworkLibrary::Get(); |
146 if (cros->wifi_connecting() || | 148 if (cros->wifi_connecting() || |
147 (!cros->ethernet_connected() && !cros->wifi_ssid().empty())) { | 149 (!cros->ethernet_connected() && !cros->wifi_ssid().empty())) { |
148 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 150 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
149 // We want a value between 0-1. | 151 // We want a value between 0-1. |
150 // 0 reperesents no signal and 1 represents full signal strength. | 152 // 0 reperesents no signal and 1 represents full signal strength. |
151 double value = cros->wifi_connecting() ? | 153 double value = cros->wifi_connecting() ? |
152 animation_connecting_.GetCurrentValue() : cros->wifi_strength() / 100.0; | 154 animation_connecting_.GetCurrentValue() : cros->wifi_strength() / 100.0; |
153 if (value < 0) | 155 if (value < 0) |
154 value = 0; | 156 value = 0; |
155 else if (value > 1) | 157 else if (value > 1) |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 canvas->DrawBitmapInt(*rb.GetBitmapNamed(IDR_STATUSBAR_WIFI_UP1P + i), | 219 canvas->DrawBitmapInt(*rb.GetBitmapNamed(IDR_STATUSBAR_WIFI_UP1P + i), |
218 0, 0, paint); | 220 0, 0, paint); |
219 } | 221 } |
220 } | 222 } |
221 } | 223 } |
222 | 224 |
223 //////////////////////////////////////////////////////////////////////////////// | 225 //////////////////////////////////////////////////////////////////////////////// |
224 // NetworkMenuButton, views::ViewMenuDelegate implementation: | 226 // NetworkMenuButton, views::ViewMenuDelegate implementation: |
225 | 227 |
226 void NetworkMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { | 228 void NetworkMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { |
227 wifi_networks_ = CrosNetworkLibrary::Get()->wifi_networks(); | 229 wifi_networks_ = NetworkLibrary::Get()->wifi_networks(); |
228 refreshing_menu_ = true; | 230 refreshing_menu_ = true; |
229 network_menu_.Rebuild(); | 231 network_menu_.Rebuild(); |
230 network_menu_.UpdateStates(); | 232 network_menu_.UpdateStates(); |
231 refreshing_menu_ = false; | 233 refreshing_menu_ = false; |
232 network_menu_.RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); | 234 network_menu_.RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); |
233 } | 235 } |
234 | 236 |
235 //////////////////////////////////////////////////////////////////////////////// | 237 //////////////////////////////////////////////////////////////////////////////// |
236 // NetworkMenuButton, CrosNetworkLibrary::Observer implementation: | 238 // NetworkMenuButton, NetworkLibrary::Observer implementation: |
237 | 239 |
238 void NetworkMenuButton::NetworkChanged(CrosNetworkLibrary* cros) { | 240 void NetworkMenuButton::NetworkChanged(NetworkLibrary* cros) { |
239 int id = IDR_STATUSBAR_WARNING; | 241 int id = IDR_STATUSBAR_WARNING; |
240 if (cros->loaded()) { | 242 if (cros->loaded()) { |
241 id = IDR_STATUSBAR_NETWORK_DISCONNECTED; | 243 id = IDR_STATUSBAR_NETWORK_DISCONNECTED; |
242 if (cros->wifi_connecting()) { | 244 if (cros->wifi_connecting()) { |
243 // Start the connecting animation if not running. | 245 // Start the connecting animation if not running. |
244 if (!animation_connecting_.IsAnimating()) { | 246 if (!animation_connecting_.IsAnimating()) { |
245 animation_connecting_.Reset(); | 247 animation_connecting_.Reset(); |
246 animation_connecting_.StartThrobbing(std::numeric_limits<int>::max()); | 248 animation_connecting_.StartThrobbing(std::numeric_limits<int>::max()); |
247 } | 249 } |
248 // Stop network traffic animation when we are connecting. | 250 // Stop network traffic animation when we are connecting. |
(...skipping 11 matching lines...) Expand all Loading... |
260 } else if (!cros->wifi_ssid().empty()) { | 262 } else if (!cros->wifi_ssid().empty()) { |
261 id = IDR_STATUSBAR_WIFI_DOT; | 263 id = IDR_STATUSBAR_WIFI_DOT; |
262 } | 264 } |
263 } | 265 } |
264 } | 266 } |
265 | 267 |
266 SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed(id)); | 268 SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed(id)); |
267 SchedulePaint(); | 269 SchedulePaint(); |
268 } | 270 } |
269 | 271 |
270 void NetworkMenuButton::NetworkTraffic(CrosNetworkLibrary* cros, | 272 void NetworkMenuButton::NetworkTraffic(NetworkLibrary* cros, int traffic_type) { |
271 int traffic_type) { | |
272 if (!cros->ethernet_connected() && !cros->wifi_ssid().empty() && | 273 if (!cros->ethernet_connected() && !cros->wifi_ssid().empty() && |
273 !cros->wifi_connecting()) { | 274 !cros->wifi_connecting()) { |
274 // For downloading/uploading animation, we want to force at least one cycle | 275 // For downloading/uploading animation, we want to force at least one cycle |
275 // so that it looks smooth. And if we keep downloading/uploading, we will | 276 // so that it looks smooth. And if we keep downloading/uploading, we will |
276 // keep calling StartThrobbing which will update the cycle count back to 2. | 277 // keep calling StartThrobbing which will update the cycle count back to 2. |
277 if (traffic_type & TRAFFIC_DOWNLOAD) | 278 if (traffic_type & TRAFFIC_DOWNLOAD) |
278 animation_downloading_.StartThrobbing(2); | 279 animation_downloading_.StartThrobbing(2); |
279 if (traffic_type & TRAFFIC_UPLOAD) | 280 if (traffic_type & TRAFFIC_UPLOAD) |
280 animation_uploading_.StartThrobbing(2); | 281 animation_uploading_.StartThrobbing(2); |
281 } | 282 } |
282 } | 283 } |
| 284 |
| 285 } // namespace chromeos |
OLD | NEW |