1 // Copyright 2013 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 "ash/accelerators/exit_warning_handler.h"
8 #include "ash/shell_delegate.h"
9 #include "ash/shell_window_ids.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/time/time.h"
12 #include "base/timer/timer.h"
13 #include "grit/ash_strings.h"
14 #include "ui/aura/root_window.h"
15 #include "ui/base/accessibility/accessible_view_state.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/base/resource/resource_bundle.h"
18 #include "ui/gfx/canvas.h"
19 #include "ui/gfx/font.h"
20 #include "ui/views/controls/label.h"
21 #include "ui/views/layout/fill_layout.h"
22 #include "ui/views/view.h"
23 #include "ui/views/widget/widget.h"
24 #include "ui/views/widget/widget_delegate.h"
29 const int64 kTimeOutMilliseconds
= 2000;
30 // Color of the text of the warning message.
31 const SkColor kTextColor
= SK_ColorWHITE
;
32 // Color of the window background.
33 const SkColor kWindowBackgroundColor
= SkColorSetARGB(0xC0, 0x0, 0x0, 0x0);
34 // Radius of the rounded corners of the window.
35 const int kWindowCornerRadius
= 2;
36 const int kHorizontalMarginAroundText
= 100;
37 const int kVerticalMarginAroundText
= 100;
39 class ExitWarningLabel
: public views::Label
{
43 virtual ~ExitWarningLabel() {}
46 virtual void PaintText(gfx::Canvas
* canvas
,
48 const gfx::Rect
& text_bounds
,
50 // Turn off subpixel rendering.
51 views::Label::PaintText(canvas
,
54 flags
| gfx::Canvas::NO_SUBPIXEL_RENDERING
);
57 DISALLOW_COPY_AND_ASSIGN(ExitWarningLabel
);
60 class ExitWarningWidgetDelegateView
: public views::WidgetDelegateView
{
62 ExitWarningWidgetDelegateView() : text_width_(0), width_(0), height_(0) {
63 text_
= l10n_util::GetStringUTF16(IDS_ASH_EXIT_WARNING_POPUP_TEXT
);
65 l10n_util::GetStringUTF16(IDS_ASH_EXIT_WARNING_POPUP_TEXT_ACCESSIBLE
);
66 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
67 font_
= rb
.GetFont(ui::ResourceBundle::LargeFont
);
68 text_width_
= font_
.GetStringWidth(text_
);
69 width_
= text_width_
+ kHorizontalMarginAroundText
;
70 height_
= font_
.GetHeight() + kVerticalMarginAroundText
;
71 views::Label
* label
= new ExitWarningLabel
;
72 label
->SetText(text_
);
73 label
->SetHorizontalAlignment(gfx::ALIGN_CENTER
);
74 label
->SetFont(font_
);
75 label
->SetEnabledColor(kTextColor
);
76 label
->SetDisabledColor(kTextColor
);
77 label
->SetAutoColorReadabilityEnabled(false);
79 SetLayoutManager(new views::FillLayout
);
82 virtual gfx::Size
GetPreferredSize() OVERRIDE
{
83 return gfx::Size(width_
, height_
);
86 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
{
88 paint
.setStyle(SkPaint::kFill_Style
);
89 paint
.setColor(kWindowBackgroundColor
);
90 canvas
->DrawRoundRect(GetLocalBounds(), kWindowCornerRadius
, paint
);
91 views::WidgetDelegateView::OnPaint(canvas
);
94 virtual void GetAccessibleState(ui::AccessibleViewState
* state
) OVERRIDE
{
95 state
->name
= accessible_name_
;
96 state
->role
= ui::AccessibilityTypes::ROLE_ALERT
;
100 base::string16 text_
;
101 base::string16 accessible_name_
;
107 DISALLOW_COPY_AND_ASSIGN(ExitWarningWidgetDelegateView
);
112 ExitWarningHandler::ExitWarningHandler()
114 stub_timer_for_test_(false) {
117 ExitWarningHandler::~ExitWarningHandler() {
118 // Note: If a timer is outstanding, it is stopped in its destructor.
122 void ExitWarningHandler::HandleAccelerator() {
123 ShellDelegate
* shell_delegate
= Shell::GetInstance()->delegate();
126 state_
= WAIT_FOR_DOUBLE_PRESS
;
129 shell_delegate
->RecordUserMetricsAction(UMA_ACCEL_EXIT_FIRST_Q
);
131 case WAIT_FOR_DOUBLE_PRESS
:
135 shell_delegate
->RecordUserMetricsAction(UMA_ACCEL_EXIT_SECOND_Q
);
136 shell_delegate
->Exit();
146 void ExitWarningHandler::TimerAction() {
148 if (state_
== WAIT_FOR_DOUBLE_PRESS
)
152 void ExitWarningHandler::StartTimer() {
153 if (stub_timer_for_test_
)
155 timer_
.Start(FROM_HERE
,
156 base::TimeDelta::FromMilliseconds(kTimeOutMilliseconds
),
158 &ExitWarningHandler::TimerAction
);
161 void ExitWarningHandler::CancelTimer() {
165 void ExitWarningHandler::Show() {
168 aura::RootWindow
* root_window
= Shell::GetTargetRootWindow();
169 ExitWarningWidgetDelegateView
* delegate
= new ExitWarningWidgetDelegateView
;
170 gfx::Size rs
= root_window
->bounds().size();
171 gfx::Size ps
= delegate
->GetPreferredSize();
172 gfx::Rect
bounds((rs
.width() - ps
.width()) / 2,
173 (rs
.height() - ps
.height()) / 3,
174 ps
.width(), ps
.height());
175 views::Widget::InitParams params
;
176 params
.type
= views::Widget::InitParams::TYPE_POPUP
;
177 params
.opacity
= views::Widget::InitParams::TRANSLUCENT_WINDOW
;
178 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
179 params
.accept_events
= false;
180 params
.can_activate
= false;
181 params
.keep_on_top
= true;
182 params
.remove_standard_frame
= true;
183 params
.delegate
= delegate
;
184 params
.bounds
= bounds
;
185 params
.parent
= Shell::GetContainer(
187 internal::kShellWindowId_SettingBubbleContainer
);
188 widget_
.reset(new views::Widget
);
189 widget_
->Init(params
);
190 widget_
->SetContentsView(delegate
);
191 widget_
->GetNativeView()->SetName("ExitWarningWindow");
194 delegate
->NotifyAccessibilityEvent(ui::AccessibilityTypes::EVENT_ALERT
, true);
197 void ExitWarningHandler::Hide() {