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.
5 #include "athena/system/shutdown_dialog.h"
7 #include "athena/screen/public/screen_manager.h"
8 #include "athena/strings/grit/athena_strings.h"
9 #include "chromeos/dbus/dbus_thread_manager.h"
10 #include "chromeos/dbus/power_manager_client.h"
11 #include "ui/base/l10n/l10n_util.h"
12 #include "ui/gfx/font_list.h"
13 #include "ui/views/background.h"
14 #include "ui/views/border.h"
15 #include "ui/views/controls/label.h"
16 #include "ui/views/layout/box_layout.h"
17 #include "ui/views/widget/widget.h"
18 #include "ui/views/window/dialog_delegate.h"
23 // The amount of time that the power button must be held to shut down the
24 // device after shutdown dialog is shown.
25 const int kShutdownTimeoutMs = 4000;
27 class ModalWidgetDelegate : public views::WidgetDelegate {
29 explicit ModalWidgetDelegate(views::View* contents_view)
30 : contents_view_(contents_view) {}
31 ~ModalWidgetDelegate() override {}
33 // Overridden from WidgetDelegate:
34 virtual views::Widget* GetWidget() override {
35 return contents_view_->GetWidget();
37 virtual const views::Widget* GetWidget() const override {
38 return contents_view_->GetWidget();
40 virtual views::View* GetContentsView() override { return contents_view_; }
41 virtual ui::ModalType GetModalType() const override {
42 return ui::MODAL_TYPE_SYSTEM;
46 views::View* contents_view_;
48 DISALLOW_COPY_AND_ASSIGN(ModalWidgetDelegate);
53 ShutdownDialog::ShutdownDialog() : state_(STATE_OTHER) {
54 InputManager::Get()->AddPowerButtonObserver(this);
57 ShutdownDialog::~ShutdownDialog() {
58 InputManager::Get()->RemovePowerButtonObserver(this);
61 void ShutdownDialog::ShowShutdownWarningDialog() {
62 state_ = STATE_SHUTDOWN_WARNING_VISIBLE;
65 new views::Label(l10n_util::GetStringUTF16(IDS_ATHENA_SHUTDOWN_WARNING));
66 label->SetBackgroundColor(SK_ColorWHITE);
67 label->SetFontList(gfx::FontList().DeriveWithStyle(gfx::Font::BOLD));
69 views::View* container = new views::View;
70 container->AddChildView(label);
72 const int kBorderSpacing = 50;
73 container->SetLayoutManager(new views::BoxLayout(
74 views::BoxLayout::kHorizontal, kBorderSpacing, kBorderSpacing, 0));
75 container->set_background(
76 views::Background::CreateSolidBackground(SK_ColorWHITE));
77 container->SetBorder(views::Border::CreateSolidBorder(1, SK_ColorBLACK));
79 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
80 params.delegate = new ModalWidgetDelegate(container);
81 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
82 params.context = ScreenManager::Get()->GetContext();
83 // Use top most modal container.
84 params.keep_on_top = true;
86 shutdown_warning_message_.reset(new views::Widget);
87 shutdown_warning_message_->Init(params);
88 shutdown_warning_message_->Show();
89 shutdown_warning_message_->CenterWindow(container->GetPreferredSize());
91 timer_.Start(FROM_HERE,
92 base::TimeDelta::FromMilliseconds(kShutdownTimeoutMs),
94 &ShutdownDialog::Shutdown);
97 void ShutdownDialog::Shutdown() {
98 state_ = STATE_SHUTDOWN_REQUESTED;
99 chromeos::DBusThreadManager::Get()
100 ->GetPowerManagerClient()
104 void ShutdownDialog::OnPowerButtonStateChanged(
105 PowerButtonObserver::State state) {
106 if (state_ == STATE_SHUTDOWN_REQUESTED)
111 state_ = STATE_SUSPEND_ON_RELEASE;
114 ShowShutdownWarningDialog();
117 if (state_ == STATE_SUSPEND_ON_RELEASE) {
118 chromeos::DBusThreadManager::Get()
119 ->GetPowerManagerClient()
122 state_ = STATE_OTHER;
124 shutdown_warning_message_.reset();
129 } // namespace athena