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/main/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 { |
| 27 |
| 28 views::Label* CreateDebugLabel(const std::string& text) { |
| 29 views::Label* label = new views::Label(base::UTF8ToUTF16(text)); |
| 30 label->SetEnabledColor(SK_ColorWHITE); |
| 31 label->SetBackgroundColor(SK_ColorTRANSPARENT); |
| 32 label->SetFontList(gfx::FontList().Derive(-2, gfx::Font::BOLD)); |
| 33 return label; |
| 34 } |
| 35 |
| 36 class PowerStatus : public chromeos::PowerManagerClient::Observer { |
| 37 public: |
| 38 PowerStatus(views::Label* label, const base::Closure& closure) |
| 39 : label_(label), closure_(closure) { |
| 40 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( |
| 41 this); |
| 42 chromeos::DBusThreadManager::Get() |
| 43 ->GetPowerManagerClient() |
| 44 ->RequestStatusUpdate(); |
| 45 } |
| 46 |
| 47 virtual ~PowerStatus() { |
| 48 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( |
| 49 this); |
| 50 } |
| 51 |
| 52 private: |
| 53 // chromeos::PowerManagerClient::Observer: |
| 54 virtual void PowerChanged( |
| 55 const power_manager::PowerSupplyProperties& proto) OVERRIDE { |
| 56 std::string output = |
| 57 proto.is_calculating_battery_time() |
| 58 ? "Calculating..." |
| 59 : base::StringPrintf("%.1lf%%", proto.battery_percent()); |
| 60 label_->SetText(base::UTF8ToUTF16(output)); |
| 61 if (!closure_.is_null()) |
| 62 closure_.Run(); |
| 63 } |
| 64 |
| 65 views::Label* label_; |
| 66 base::Closure closure_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(PowerStatus); |
| 69 }; |
| 70 |
| 71 class NetworkStatus : public chromeos::NetworkStateHandlerObserver { |
| 72 public: |
| 73 NetworkStatus(views::Label* label, const base::Closure& closure) |
| 74 : label_(label), closure_(closure) { |
| 75 chromeos::NetworkStateHandler* handler = |
| 76 chromeos::NetworkHandler::Get()->network_state_handler(); |
| 77 handler->AddObserver(this, FROM_HERE); |
| 78 } |
| 79 |
| 80 virtual ~NetworkStatus() { |
| 81 chromeos::NetworkStateHandler* handler = |
| 82 chromeos::NetworkHandler::Get()->network_state_handler(); |
| 83 handler->RemoveObserver(this, FROM_HERE); |
| 84 } |
| 85 |
| 86 private: |
| 87 void Update() { |
| 88 std::string status = "<unknown>"; |
| 89 chromeos::NetworkStateHandler* handler = |
| 90 chromeos::NetworkHandler::Get()->network_state_handler(); |
| 91 const chromeos::NetworkState* network = handler->DefaultNetwork(); |
| 92 if (network) { |
| 93 status = base::StringPrintf( |
| 94 "%s (%s)", network->ip_address().c_str(), network->name().c_str()); |
| 95 } |
| 96 label_->SetText(base::UTF8ToUTF16(status)); |
| 97 if (!closure_.is_null()) |
| 98 closure_.Run(); |
| 99 } |
| 100 |
| 101 // chromeos::NetworkStateHandlerObserver: |
| 102 virtual void DefaultNetworkChanged( |
| 103 const chromeos::NetworkState* network) OVERRIDE { |
| 104 Update(); |
| 105 } |
| 106 |
| 107 virtual void NetworkConnectionStateChanged( |
| 108 const chromeos::NetworkState* network) OVERRIDE { |
| 109 Update(); |
| 110 } |
| 111 |
| 112 virtual void NetworkPropertiesUpdated( |
| 113 const chromeos::NetworkState* network) OVERRIDE { |
| 114 Update(); |
| 115 } |
| 116 |
| 117 views::Label* label_; |
| 118 base::Closure closure_; |
| 119 }; |
| 120 |
| 121 class DebugWidget { |
| 122 public: |
| 123 DebugWidget() : container_(NULL), widget_(NULL) { |
| 124 CreateContainer(); |
| 125 CreateWidget(); |
| 126 |
| 127 CreateBatteryView(); |
| 128 CreateNetworkView(); |
| 129 |
| 130 UpdateSize(); |
| 131 } |
| 132 |
| 133 virtual ~DebugWidget() {} |
| 134 |
| 135 private: |
| 136 void CreateContainer() { |
| 137 container_ = |
| 138 athena::ScreenManager::Get()->CreateContainer("DebugContainer"); |
| 139 } |
| 140 |
| 141 void CreateWidget() { |
| 142 views::Widget::InitParams params; |
| 143 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS; |
| 144 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; |
| 145 params.activatable = views::Widget::InitParams::ACTIVATABLE_NO; |
| 146 params.accept_events = false; |
| 147 params.bounds = gfx::Rect(200, 0, 100, 105); |
| 148 params.parent = container_; |
| 149 widget_ = new views::Widget(); |
| 150 widget_->Init(params); |
| 151 |
| 152 const int kHorizontalSpacing = 10; |
| 153 const int kBorderVerticalSpacing = 3; |
| 154 const int kBetweenChildSpacing = 10; |
| 155 const int kBackgroundColor = SkColorSetARGB(0x7f, 0, 0, 0); |
| 156 views::View* container = new views::View; |
| 157 container->SetLayoutManager( |
| 158 new views::BoxLayout(views::BoxLayout::kHorizontal, |
| 159 kHorizontalSpacing, |
| 160 kBorderVerticalSpacing, |
| 161 kBetweenChildSpacing)); |
| 162 container->set_background( |
| 163 views::Background::CreateSolidBackground(kBackgroundColor)); |
| 164 container->SetBorder(views::Border::CreateSolidBorder(1, kBackgroundColor)); |
| 165 widget_->SetContentsView(container); |
| 166 widget_->StackAtTop(); |
| 167 widget_->Show(); |
| 168 |
| 169 widget_->SetBounds(gfx::Rect(600, 0, 300, 25)); |
| 170 } |
| 171 |
| 172 void CreateBatteryView() { |
| 173 views::View* container = widget_->GetContentsView(); |
| 174 container->AddChildView(CreateDebugLabel("Battery:")); |
| 175 |
| 176 views::Label* label = CreateDebugLabel(std::string()); |
| 177 container->AddChildView(label); |
| 178 container->Layout(); |
| 179 |
| 180 power_status_.reset(new PowerStatus( |
| 181 label, base::Bind(&DebugWidget::UpdateSize, base::Unretained(this)))); |
| 182 } |
| 183 |
| 184 void CreateNetworkView() { |
| 185 views::View* container = widget_->GetContentsView(); |
| 186 container->AddChildView(CreateDebugLabel("Network:")); |
| 187 |
| 188 views::Label* label = CreateDebugLabel(std::string()); |
| 189 container->AddChildView(label); |
| 190 container->Layout(); |
| 191 |
| 192 network_status_.reset(new NetworkStatus( |
| 193 label, base::Bind(&DebugWidget::UpdateSize, base::Unretained(this)))); |
| 194 } |
| 195 |
| 196 const gfx::Rect GetPositionForSize(const gfx::Size& size) { |
| 197 int right = container_->bounds().right(); |
| 198 int x = right - size.width(); |
| 199 return gfx::Rect(x, 0, size.width(), size.height()); |
| 200 } |
| 201 |
| 202 void UpdateSize() { |
| 203 views::View* container = widget_->GetContentsView(); |
| 204 container->Layout(); |
| 205 gfx::Size size = container->GetPreferredSize(); |
| 206 widget_->SetBounds(GetPositionForSize(size)); |
| 207 } |
| 208 |
| 209 aura::Window* container_; |
| 210 views::Widget* widget_; |
| 211 scoped_ptr<PowerStatus> power_status_; |
| 212 scoped_ptr<NetworkStatus> network_status_; |
| 213 |
| 214 DISALLOW_COPY_AND_ASSIGN(DebugWidget); |
| 215 }; |
| 216 |
| 217 } // namespace |
| 218 |
| 219 void CreateDebugWindow() { |
| 220 new DebugWidget(); |
| 221 } |
OLD | NEW |