Chromium Code Reviews| Index: athena/wm/debug/debug_window.cc |
| diff --git a/athena/wm/debug/debug_window.cc b/athena/wm/debug/debug_window.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ac24d86041f3daaa7647758513ce1ee36789a7c4 |
| --- /dev/null |
| +++ b/athena/wm/debug/debug_window.cc |
| @@ -0,0 +1,224 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "athena/wm/debug/debug_window.h" |
| + |
| +#include "athena/screen/public/screen_manager.h" |
| +#include "base/bind.h" |
| +#include "base/macros.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "chromeos/dbus/dbus_thread_manager.h" |
| +#include "chromeos/dbus/power_manager/power_supply_properties.pb.h" |
| +#include "chromeos/dbus/power_manager_client.h" |
| +#include "chromeos/network/network_state.h" |
| +#include "chromeos/network/network_state_handler.h" |
| +#include "chromeos/network/network_state_handler_observer.h" |
| +#include "ui/aura/window.h" |
| +#include "ui/views/background.h" |
| +#include "ui/views/border.h" |
| +#include "ui/views/controls/label.h" |
| +#include "ui/views/layout/box_layout.h" |
| +#include "ui/views/view.h" |
| +#include "ui/views/widget/widget.h" |
| + |
| +namespace athena { |
| + |
| +namespace { |
| + |
| +views::Label* CreateDebugLabel(const std::string& text) { |
| + views::Label* label = new views::Label(base::UTF8ToUTF16(text)); |
| + label->SetEnabledColor(SK_ColorWHITE); |
| + label->SetBackgroundColor(SK_ColorTRANSPARENT); |
| + label->SetFontList(gfx::FontList().Derive(-2, gfx::Font::BOLD)); |
| + return label; |
| +} |
| + |
| +class PowerStatus : public chromeos::PowerManagerClient::Observer { |
| + public: |
| + PowerStatus(views::Label* label, const base::Closure& closure) |
| + : label_(label), closure_(closure) { |
| + chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( |
| + this); |
| + chromeos::DBusThreadManager::Get() |
| + ->GetPowerManagerClient() |
| + ->RequestStatusUpdate(); |
| + } |
| + |
| + virtual ~PowerStatus() { |
| + chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( |
| + this); |
| + } |
| + |
| + private: |
| + // chromeos::PowerManagerClient::Observer: |
| + virtual void PowerChanged( |
| + const power_manager::PowerSupplyProperties& proto) OVERRIDE { |
| + std::string output = |
| + proto.is_calculating_battery_time() |
| + ? "Calculating..." |
| + : base::StringPrintf("%.1lf%%", proto.battery_percent()); |
| + label_->SetText(base::UTF8ToUTF16(output)); |
| + if (!closure_.is_null()) |
| + closure_.Run(); |
| + } |
| + |
| + views::Label* label_; |
| + base::Closure closure_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(PowerStatus); |
| +}; |
| + |
| +class NetworkStatus : public chromeos::NetworkStateHandlerObserver { |
| + public: |
| + NetworkStatus(views::Label* label, const base::Closure& closure) |
| + : label_(label), closure_(closure) { |
| + chromeos::NetworkStateHandler* handler = |
| + chromeos::NetworkHandler::Get()->network_state_handler(); |
| + handler->AddObserver(this, FROM_HERE); |
| + } |
| + |
| + virtual ~NetworkStatus() { |
| + chromeos::NetworkStateHandler* handler = |
| + chromeos::NetworkHandler::Get()->network_state_handler(); |
| + handler->RemoveObserver(this, FROM_HERE); |
| + } |
| + |
| + private: |
| + void Update() { |
| + std::string status = "<unknown>"; |
| + chromeos::NetworkStateHandler* handler = |
| + chromeos::NetworkHandler::Get()->network_state_handler(); |
| + const chromeos::NetworkState* network = handler->DefaultNetwork(); |
| + if (network) { |
| + status = base::StringPrintf( |
| + "%s (%s)", network->ip_address().c_str(), network->name().c_str()); |
| + } |
| + label_->SetText(base::UTF8ToUTF16(status)); |
| + if (!closure_.is_null()) |
| + closure_.Run(); |
| + } |
| + |
| + // chromeos::NetworkStateHandlerObserver: |
| + virtual void DefaultNetworkChanged( |
| + const chromeos::NetworkState* network) OVERRIDE { |
| + Update(); |
| + } |
| + |
| + virtual void NetworkConnectionStateChanged( |
| + const chromeos::NetworkState* network) OVERRIDE { |
| + Update(); |
| + } |
| + |
| + virtual void NetworkPropertiesUpdated( |
| + const chromeos::NetworkState* network) OVERRIDE { |
| + Update(); |
| + } |
| + |
| + views::Label* label_; |
| + base::Closure closure_; |
| +}; |
| + |
| +class DebugWidget { |
| + public: |
| + DebugWidget() : container_(NULL), widget_(NULL) { |
| + CreateContainer(); |
| + CreateWidget(); |
| + |
| + CreateBatteryDebug(); |
| + CreateNetworkDebug(); |
| + |
| + UpdateSize(); |
| + } |
| + |
| + virtual ~DebugWidget() {} |
| + |
| + private: |
| + void CreateContainer() { |
| + container_ = ScreenManager::Get()->CreateContainer("DebugContainer"); |
| + } |
| + |
| + void CreateWidget() { |
| + views::Widget::InitParams params; |
| + params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS; |
| + params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; |
| + params.activatable = views::Widget::InitParams::ACTIVATABLE_NO; |
| + params.accept_events = false; |
| + params.bounds = gfx::Rect(200, 0, 100, 105); |
| + params.parent = container_; |
| + widget_ = new views::Widget(); |
| + widget_->Init(params); |
| + |
| + const int kHorizontalSpacing = 10; |
| + const int kBorderVerticalSpacing = 3; |
| + const int kBetweenChildSpacing = 10; |
| + const int kBackgroundColor = SkColorSetARGB(0x7f, 0, 0, 0); |
| + views::View* container = new views::View; |
| + container->SetLayoutManager( |
| + new views::BoxLayout(views::BoxLayout::kHorizontal, |
| + kHorizontalSpacing, |
| + kBorderVerticalSpacing, |
| + kBetweenChildSpacing)); |
| + container->set_background( |
| + views::Background::CreateSolidBackground(kBackgroundColor)); |
| + container->SetBorder(views::Border::CreateSolidBorder(1, kBackgroundColor)); |
| + widget_->SetContentsView(container); |
| + widget_->StackAtTop(); |
| + widget_->Show(); |
| + |
| + widget_->SetBounds(gfx::Rect(600, 0, 300, 25)); |
| + } |
| + |
| + 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()).
|
| + views::View* container = widget_->GetContentsView(); |
| + container->AddChildView(CreateDebugLabel("Battery:")); |
| + |
| + views::Label* label = CreateDebugLabel(std::string()); |
| + container->AddChildView(label); |
| + container->Layout(); |
| + |
| + power_status_.reset(new PowerStatus( |
| + label, base::Bind(&DebugWidget::UpdateSize, base::Unretained(this)))); |
| + } |
| + |
| + void CreateNetworkDebug() { |
| + views::View* container = widget_->GetContentsView(); |
| + container->AddChildView(CreateDebugLabel("Network:")); |
| + |
| + views::Label* label = CreateDebugLabel(std::string()); |
| + container->AddChildView(label); |
| + container->Layout(); |
| + |
| + network_status_.reset(new NetworkStatus( |
| + label, base::Bind(&DebugWidget::UpdateSize, base::Unretained(this)))); |
| + } |
| + |
| + gfx::Rect GetPositionForSize(const gfx::Size& size) { |
|
oshima
2014/06/23 23:26:52
const
sadrul
2014/06/24 01:48:51
Done.
|
| + int right = container_->bounds().right(); |
| + int x = right - size.width(); |
| + return gfx::Rect(x, 0, size.width(), size.height()); |
| + } |
| + |
| + void UpdateSize() { |
| + views::View* container = widget_->GetContentsView(); |
| + container->Layout(); |
| + gfx::Size size = container->GetPreferredSize(); |
| + widget_->SetBounds(GetPositionForSize(size)); |
| + } |
| + |
| + aura::Window* container_; |
| + views::Widget* widget_; |
| + scoped_ptr<PowerStatus> power_status_; |
| + scoped_ptr<NetworkStatus> network_status_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(DebugWidget); |
| +}; |
| + |
| +} // namespace |
| + |
| +void CreateDebugWindow() { |
| + new DebugWidget(); |
| +} |
| + |
| +} // namespace athena |