OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "ash/accelerators/exit_warning_handler.h" | |
6 | |
7 #include "ash/common/shell_delegate.h" | |
8 #include "ash/common/shell_window_ids.h" | |
9 #include "ash/common/wm_root_window_controller.h" | |
10 #include "ash/common/wm_shell.h" | |
11 #include "ash/common/wm_window.h" | |
12 #include "base/strings/utf_string_conversions.h" | |
13 #include "base/time/time.h" | |
14 #include "base/timer/timer.h" | |
15 #include "grit/ash_strings.h" | |
16 #include "ui/accessibility/ax_view_state.h" | |
17 #include "ui/base/l10n/l10n_util.h" | |
18 #include "ui/base/resource/resource_bundle.h" | |
19 #include "ui/gfx/canvas.h" | |
20 #include "ui/gfx/font_list.h" | |
21 #include "ui/gfx/text_utils.h" | |
22 #include "ui/views/controls/label.h" | |
23 #include "ui/views/layout/fill_layout.h" | |
24 #include "ui/views/view.h" | |
25 #include "ui/views/widget/widget.h" | |
26 #include "ui/views/widget/widget_delegate.h" | |
27 | |
28 namespace ash { | |
29 namespace { | |
30 | |
31 const int64_t kTimeOutMilliseconds = 2000; | |
32 // Color of the text of the warning message. | |
33 const SkColor kTextColor = SK_ColorWHITE; | |
34 // Color of the window background. | |
35 const SkColor kWindowBackgroundColor = SkColorSetARGB(0xC0, 0x0, 0x0, 0x0); | |
36 // Radius of the rounded corners of the window. | |
37 const int kWindowCornerRadius = 2; | |
38 const int kHorizontalMarginAroundText = 100; | |
39 const int kVerticalMarginAroundText = 100; | |
40 | |
41 class ExitWarningWidgetDelegateView : public views::WidgetDelegateView { | |
42 public: | |
43 ExitWarningWidgetDelegateView() : text_width_(0), width_(0), height_(0) { | |
44 #ifdef OS_CHROMEOS | |
45 text_ = l10n_util::GetStringUTF16(IDS_ASH_SIGN_OUT_WARNING_POPUP_TEXT); | |
46 accessible_name_ = l10n_util::GetStringUTF16( | |
47 IDS_ASH_SIGN_OUT_WARNING_POPUP_TEXT_ACCESSIBLE); | |
48 #else | |
49 text_ = l10n_util::GetStringUTF16(IDS_ASH_EXIT_WARNING_POPUP_TEXT); | |
50 accessible_name_ = | |
51 l10n_util::GetStringUTF16(IDS_ASH_EXIT_WARNING_POPUP_TEXT_ACCESSIBLE); | |
52 #endif | |
53 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
54 const gfx::FontList& font_list = | |
55 rb.GetFontList(ui::ResourceBundle::LargeFont); | |
56 text_width_ = gfx::GetStringWidth(text_, font_list); | |
57 width_ = text_width_ + kHorizontalMarginAroundText; | |
58 height_ = font_list.GetHeight() + kVerticalMarginAroundText; | |
59 views::Label* label = new views::Label(); | |
60 label->SetText(text_); | |
61 label->SetHorizontalAlignment(gfx::ALIGN_CENTER); | |
62 label->SetFontList(font_list); | |
63 label->SetEnabledColor(kTextColor); | |
64 label->SetDisabledColor(kTextColor); | |
65 label->SetAutoColorReadabilityEnabled(false); | |
66 label->SetSubpixelRenderingEnabled(false); | |
67 AddChildView(label); | |
68 SetLayoutManager(new views::FillLayout); | |
69 } | |
70 | |
71 gfx::Size GetPreferredSize() const override { | |
72 return gfx::Size(width_, height_); | |
73 } | |
74 | |
75 void OnPaint(gfx::Canvas* canvas) override { | |
76 SkPaint paint; | |
77 paint.setStyle(SkPaint::kFill_Style); | |
78 paint.setColor(kWindowBackgroundColor); | |
79 canvas->DrawRoundRect(GetLocalBounds(), kWindowCornerRadius, paint); | |
80 views::WidgetDelegateView::OnPaint(canvas); | |
81 } | |
82 | |
83 void GetAccessibleState(ui::AXViewState* state) override { | |
84 state->name = accessible_name_; | |
85 state->role = ui::AX_ROLE_ALERT; | |
86 } | |
87 | |
88 private: | |
89 base::string16 text_; | |
90 base::string16 accessible_name_; | |
91 int text_width_; | |
92 int width_; | |
93 int height_; | |
94 | |
95 DISALLOW_COPY_AND_ASSIGN(ExitWarningWidgetDelegateView); | |
96 }; | |
97 | |
98 } // namespace | |
99 | |
100 ExitWarningHandler::ExitWarningHandler() | |
101 : state_(IDLE), stub_timer_for_test_(false) {} | |
102 | |
103 ExitWarningHandler::~ExitWarningHandler() { | |
104 // Note: If a timer is outstanding, it is stopped in its destructor. | |
105 Hide(); | |
106 } | |
107 | |
108 void ExitWarningHandler::HandleAccelerator() { | |
109 switch (state_) { | |
110 case IDLE: | |
111 state_ = WAIT_FOR_DOUBLE_PRESS; | |
112 Show(); | |
113 StartTimer(); | |
114 WmShell::Get()->RecordUserMetricsAction(UMA_ACCEL_EXIT_FIRST_Q); | |
115 break; | |
116 case WAIT_FOR_DOUBLE_PRESS: | |
117 state_ = EXITING; | |
118 CancelTimer(); | |
119 Hide(); | |
120 WmShell::Get()->RecordUserMetricsAction(UMA_ACCEL_EXIT_SECOND_Q); | |
121 WmShell::Get()->delegate()->Exit(); | |
122 break; | |
123 case EXITING: | |
124 break; | |
125 } | |
126 } | |
127 | |
128 void ExitWarningHandler::TimerAction() { | |
129 Hide(); | |
130 if (state_ == WAIT_FOR_DOUBLE_PRESS) | |
131 state_ = IDLE; | |
132 } | |
133 | |
134 void ExitWarningHandler::StartTimer() { | |
135 if (stub_timer_for_test_) | |
136 return; | |
137 timer_.Start(FROM_HERE, | |
138 base::TimeDelta::FromMilliseconds(kTimeOutMilliseconds), this, | |
139 &ExitWarningHandler::TimerAction); | |
140 } | |
141 | |
142 void ExitWarningHandler::CancelTimer() { | |
143 timer_.Stop(); | |
144 } | |
145 | |
146 void ExitWarningHandler::Show() { | |
147 if (widget_) | |
148 return; | |
149 WmWindow* root_window = WmShell::Get()->GetRootWindowForNewWindows(); | |
150 ExitWarningWidgetDelegateView* delegate = new ExitWarningWidgetDelegateView; | |
151 gfx::Size rs = root_window->GetBounds().size(); | |
152 gfx::Size ps = delegate->GetPreferredSize(); | |
153 gfx::Rect bounds((rs.width() - ps.width()) / 2, | |
154 (rs.height() - ps.height()) / 3, ps.width(), ps.height()); | |
155 views::Widget::InitParams params; | |
156 params.type = views::Widget::InitParams::TYPE_POPUP; | |
157 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | |
158 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
159 params.accept_events = false; | |
160 params.keep_on_top = true; | |
161 params.remove_standard_frame = true; | |
162 params.delegate = delegate; | |
163 params.bounds = bounds; | |
164 params.name = "ExitWarningWindow"; | |
165 widget_.reset(new views::Widget); | |
166 root_window->GetRootWindowController()->ConfigureWidgetInitParamsForContainer( | |
167 widget_.get(), kShellWindowId_SettingBubbleContainer, ¶ms); | |
168 widget_->Init(params); | |
169 widget_->SetContentsView(delegate); | |
170 widget_->Show(); | |
171 | |
172 delegate->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true); | |
173 } | |
174 | |
175 void ExitWarningHandler::Hide() { | |
176 widget_.reset(); | |
177 } | |
178 | |
179 } // namespace ash | |
OLD | NEW |