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/system/shutdown_dialog.h" | |
6 | |
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 | |
19 namespace athena { | |
20 namespace { | |
21 | |
22 // The amount of time that the power button must be held to shut down the | |
23 // device after shutdown dialog is shown. | |
24 const int kShutdownTimeoutMs = 4000; | |
25 | |
26 } // namespace | |
27 | |
28 ShutdownDialog::ShutdownDialog(aura::Window* dialog_container) | |
29 : warning_message_container_(dialog_container), state_(STATE_OTHER) { | |
30 InputManager::Get()->AddPowerButtonObserver(this); | |
31 } | |
32 | |
33 ShutdownDialog::~ShutdownDialog() { | |
34 InputManager::Get()->RemovePowerButtonObserver(this); | |
35 } | |
36 | |
37 void ShutdownDialog::ShowShutdownWarningDialog() { | |
38 state_ = STATE_SHUTDOWN_WARNING_VISIBLE; | |
39 | |
40 shutdown_warning_message_.reset(new views::Widget); | |
41 | |
42 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP); | |
43 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
44 params.parent = warning_message_container_; | |
45 shutdown_warning_message_->Init(params); | |
46 | |
47 views::Label* label = | |
48 new views::Label(l10n_util::GetStringUTF16(IDS_ATHENA_SHUTDOWN_WARNING)); | |
49 label->SetBackgroundColor(SK_ColorWHITE); | |
50 label->SetFontList(gfx::FontList().DeriveWithStyle(gfx::Font::BOLD)); | |
51 | |
52 views::View* container = new views::View; | |
53 container->AddChildView(label); | |
54 | |
55 const int kBorderSpacing = 50; | |
56 container->SetLayoutManager(new views::BoxLayout( | |
57 views::BoxLayout::kHorizontal, kBorderSpacing, kBorderSpacing, 0)); | |
58 container->set_background( | |
59 views::Background::CreateSolidBackground(SK_ColorWHITE)); | |
60 container->SetBorder(views::Border::CreateSolidBorder(1, SK_ColorBLACK)); | |
61 | |
62 shutdown_warning_message_->SetContentsView(container); | |
63 shutdown_warning_message_->CenterWindow(container->GetPreferredSize()); | |
64 shutdown_warning_message_->Show(); | |
65 timer_.Start(FROM_HERE, | |
66 base::TimeDelta::FromMilliseconds(kShutdownTimeoutMs), | |
67 this, | |
68 &ShutdownDialog::Shutdown); | |
69 } | |
70 | |
71 void ShutdownDialog::Shutdown() { | |
72 state_ = STATE_SHUTDOWN_REQUESTED; | |
73 chromeos::DBusThreadManager::Get() | |
74 ->GetPowerManagerClient() | |
75 ->RequestShutdown(); | |
76 } | |
77 | |
78 void ShutdownDialog::OnPowerButtonStateChanged( | |
79 PowerButtonObserver::State state) { | |
80 if (state_ == STATE_SHUTDOWN_REQUESTED) | |
81 return; | |
82 | |
83 switch (state) { | |
84 case PRESSED: | |
85 state_ = STATE_SUSPEND_ON_RELEASE; | |
86 break; | |
87 case LONG_PRESSED: | |
88 ShowShutdownWarningDialog(); | |
89 break; | |
90 case RELEASED: | |
91 if (state_ == STATE_SUSPEND_ON_RELEASE) { | |
92 chromeos::DBusThreadManager::Get() | |
93 ->GetPowerManagerClient() | |
94 ->RequestSuspend(); | |
95 } | |
96 state_ = STATE_OTHER; | |
97 timer_.Stop(); | |
98 shutdown_warning_message_.reset(); | |
99 break; | |
100 } | |
101 } | |
102 | |
103 } // namespace athena | |
OLD | NEW |