OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "athena/wm/debug/debug_window.h" | |
6 | |
7 #include "athena/screen/public/screen_manager.h" | |
8 #include "base/bind.h" | |
9 #include "base/macros.h" | |
10 #include "base/strings/stringprintf.h" | |
11 #include "base/strings/utf_string_conversions.h" | |
12 #include "chromeos/dbus/dbus_thread_manager.h" | |
13 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h" | |
14 #include "chromeos/dbus/power_manager_client.h" | |
15 #include "chromeos/network/network_state.h" | |
16 #include "chromeos/network/network_state_handler.h" | |
17 #include "chromeos/network/network_state_handler_observer.h" | |
18 #include "ui/aura/window.h" | |
19 #include "ui/views/background.h" | |
20 #include "ui/views/border.h" | |
21 #include "ui/views/controls/label.h" | |
22 #include "ui/views/layout/box_layout.h" | |
23 #include "ui/views/view.h" | |
24 #include "ui/views/widget/widget.h" | |
25 | |
26 namespace athena { | |
27 | |
28 namespace { | |
29 | |
30 views::Label* CreateDebugLabel(const std::string& text) { | |
31 views::Label* label = new views::Label(base::UTF8ToUTF16(text)); | |
32 label->SetEnabledColor(SK_ColorWHITE); | |
33 label->SetBackgroundColor(SK_ColorTRANSPARENT); | |
34 label->SetFontList(gfx::FontList().Derive(-2, gfx::Font::BOLD)); | |
35 return label; | |
36 } | |
37 | |
38 class PowerStatus : public chromeos::PowerManagerClient::Observer { | |
39 public: | |
40 PowerStatus(views::Label* label, const base::Closure& closure) | |
41 : label_(label), closure_(closure) { | |
42 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( | |
43 this); | |
44 chromeos::DBusThreadManager::Get() | |
45 ->GetPowerManagerClient() | |
46 ->RequestStatusUpdate(); | |
47 } | |
48 | |
49 virtual ~PowerStatus() { | |
50 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( | |
51 this); | |
52 } | |
53 | |
54 private: | |
55 // chromeos::PowerManagerClient::Observer: | |
56 virtual void PowerChanged( | |
57 const power_manager::PowerSupplyProperties& proto) OVERRIDE { | |
58 std::string output = | |
59 proto.is_calculating_battery_time() | |
60 ? "Calculating..." | |
61 : base::StringPrintf("%.1lf%%", proto.battery_percent()); | |
62 label_->SetText(base::UTF8ToUTF16(output)); | |
63 if (!closure_.is_null()) | |
64 closure_.Run(); | |
65 } | |
66 | |
67 views::Label* label_; | |
68 base::Closure closure_; | |
69 | |
70 DISALLOW_COPY_AND_ASSIGN(PowerStatus); | |
71 }; | |
72 | |
73 class NetworkStatus : public chromeos::NetworkStateHandlerObserver { | |
74 public: | |
75 NetworkStatus(views::Label* label, const base::Closure& closure) | |
76 : label_(label), closure_(closure) { | |
77 chromeos::NetworkStateHandler* handler = | |
78 chromeos::NetworkHandler::Get()->network_state_handler(); | |
79 handler->AddObserver(this, FROM_HERE); | |
80 } | |
81 | |
82 virtual ~NetworkStatus() { | |
83 chromeos::NetworkStateHandler* handler = | |
84 chromeos::NetworkHandler::Get()->network_state_handler(); | |
85 handler->RemoveObserver(this, FROM_HERE); | |
86 } | |
87 | |
88 private: | |
89 void Update() { | |
90 std::string status = "<unknown>"; | |
91 chromeos::NetworkStateHandler* handler = | |
92 chromeos::NetworkHandler::Get()->network_state_handler(); | |
93 const chromeos::NetworkState* network = handler->DefaultNetwork(); | |
94 if (network) { | |
95 status = base::StringPrintf( | |
96 "%s (%s)", network->ip_address().c_str(), network->name().c_str()); | |
97 } | |
98 label_->SetText(base::UTF8ToUTF16(status)); | |
99 if (!closure_.is_null()) | |
100 closure_.Run(); | |
101 } | |
102 | |
103 // chromeos::NetworkStateHandlerObserver: | |
104 virtual void DefaultNetworkChanged( | |
105 const chromeos::NetworkState* network) OVERRIDE { | |
106 Update(); | |
107 } | |
108 | |
109 virtual void NetworkConnectionStateChanged( | |
110 const chromeos::NetworkState* network) OVERRIDE { | |
111 Update(); | |
112 } | |
113 | |
114 virtual void NetworkPropertiesUpdated( | |
115 const chromeos::NetworkState* network) OVERRIDE { | |
116 Update(); | |
117 } | |
118 | |
119 views::Label* label_; | |
120 base::Closure closure_; | |
121 }; | |
122 | |
123 class DebugWidget { | |
124 public: | |
125 DebugWidget() : container_(NULL), widget_(NULL) { | |
126 CreateContainer(); | |
127 CreateWidget(); | |
128 | |
129 CreateBatteryDebug(); | |
130 CreateNetworkDebug(); | |
131 | |
132 UpdateSize(); | |
133 } | |
134 | |
135 virtual ~DebugWidget() {} | |
136 | |
137 private: | |
138 void CreateContainer() { | |
139 container_ = ScreenManager::Get()->CreateContainer("DebugContainer"); | |
140 } | |
141 | |
142 void CreateWidget() { | |
143 views::Widget::InitParams params; | |
144 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS; | |
145 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | |
146 params.activatable = views::Widget::InitParams::ACTIVATABLE_NO; | |
147 params.accept_events = false; | |
148 params.bounds = gfx::Rect(200, 0, 100, 105); | |
149 params.parent = container_; | |
150 widget_ = new views::Widget(); | |
151 widget_->Init(params); | |
152 | |
153 const int kHorizontalSpacing = 10; | |
154 const int kBorderVerticalSpacing = 3; | |
155 const int kBetweenChildSpacing = 10; | |
156 const int kBackgroundColor = SkColorSetARGB(0x7f, 0, 0, 0); | |
157 views::View* container = new views::View; | |
158 container->SetLayoutManager( | |
159 new views::BoxLayout(views::BoxLayout::kHorizontal, | |
160 kHorizontalSpacing, | |
161 kBorderVerticalSpacing, | |
162 kBetweenChildSpacing)); | |
163 container->set_background( | |
164 views::Background::CreateSolidBackground(kBackgroundColor)); | |
165 container->SetBorder(views::Border::CreateSolidBorder(1, kBackgroundColor)); | |
166 widget_->SetContentsView(container); | |
167 widget_->StackAtTop(); | |
168 widget_->Show(); | |
169 | |
170 widget_->SetBounds(gfx::Rect(600, 0, 300, 25)); | |
171 } | |
172 | |
173 void CreateBatteryDebug() { | |
oshima
2014/06/23 23:26:52
optional: I'd just call them CreateBattery (View)
sadrul
2014/06/24 01:48:51
Done (changed to Create*View()).
| |
174 views::View* container = widget_->GetContentsView(); | |
175 container->AddChildView(CreateDebugLabel("Battery:")); | |
176 | |
177 views::Label* label = CreateDebugLabel(std::string()); | |
178 container->AddChildView(label); | |
179 container->Layout(); | |
180 | |
181 power_status_.reset(new PowerStatus( | |
182 label, base::Bind(&DebugWidget::UpdateSize, base::Unretained(this)))); | |
183 } | |
184 | |
185 void CreateNetworkDebug() { | |
186 views::View* container = widget_->GetContentsView(); | |
187 container->AddChildView(CreateDebugLabel("Network:")); | |
188 | |
189 views::Label* label = CreateDebugLabel(std::string()); | |
190 container->AddChildView(label); | |
191 container->Layout(); | |
192 | |
193 network_status_.reset(new NetworkStatus( | |
194 label, base::Bind(&DebugWidget::UpdateSize, base::Unretained(this)))); | |
195 } | |
196 | |
197 gfx::Rect GetPositionForSize(const gfx::Size& size) { | |
oshima
2014/06/23 23:26:52
const
sadrul
2014/06/24 01:48:51
Done.
| |
198 int right = container_->bounds().right(); | |
199 int x = right - size.width(); | |
200 return gfx::Rect(x, 0, size.width(), size.height()); | |
201 } | |
202 | |
203 void UpdateSize() { | |
204 views::View* container = widget_->GetContentsView(); | |
205 container->Layout(); | |
206 gfx::Size size = container->GetPreferredSize(); | |
207 widget_->SetBounds(GetPositionForSize(size)); | |
208 } | |
209 | |
210 aura::Window* container_; | |
211 views::Widget* widget_; | |
212 scoped_ptr<PowerStatus> power_status_; | |
213 scoped_ptr<NetworkStatus> network_status_; | |
214 | |
215 DISALLOW_COPY_AND_ASSIGN(DebugWidget); | |
216 }; | |
217 | |
218 } // namespace | |
219 | |
220 void CreateDebugWindow() { | |
221 new DebugWidget(); | |
222 } | |
223 | |
224 } // namespace athena | |
OLD | NEW |