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/common/accelerators/exit_warning_handler.h" | |
6 | |
7 #include "ash/common/shell_delegate.h" | |
8 #include "ash/common/wm_shell.h" | |
9 #include "ash/common/wm_window.h" | |
10 #include "ash/public/cpp/shell_window_ids.h" | |
11 #include "ash/root_window_controller.h" | |
12 #include "ash/strings/grit/ash_strings.h" | |
13 #include "base/strings/utf_string_conversions.h" | |
14 #include "base/time/time.h" | |
15 #include "base/timer/timer.h" | |
16 #include "ui/accessibility/ax_node_data.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() | |
44 : text_(l10n_util::GetStringUTF16(IDS_ASH_SIGN_OUT_WARNING_POPUP_TEXT)), | |
45 accessible_name_(l10n_util::GetStringUTF16( | |
46 IDS_ASH_SIGN_OUT_WARNING_POPUP_TEXT_ACCESSIBLE)), | |
47 text_width_(0), | |
48 width_(0), | |
49 height_(0) { | |
50 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
51 const gfx::FontList& font_list = | |
52 rb.GetFontList(ui::ResourceBundle::LargeFont); | |
53 text_width_ = gfx::GetStringWidth(text_, font_list); | |
54 width_ = text_width_ + kHorizontalMarginAroundText; | |
55 height_ = font_list.GetHeight() + kVerticalMarginAroundText; | |
56 views::Label* label = new views::Label(); | |
57 label->SetText(text_); | |
58 label->SetHorizontalAlignment(gfx::ALIGN_CENTER); | |
59 label->SetFontList(font_list); | |
60 label->SetEnabledColor(kTextColor); | |
61 label->SetDisabledColor(kTextColor); | |
62 label->SetAutoColorReadabilityEnabled(false); | |
63 label->SetSubpixelRenderingEnabled(false); | |
64 AddChildView(label); | |
65 SetLayoutManager(new views::FillLayout); | |
66 } | |
67 | |
68 gfx::Size GetPreferredSize() const override { | |
69 return gfx::Size(width_, height_); | |
70 } | |
71 | |
72 void OnPaint(gfx::Canvas* canvas) override { | |
73 cc::PaintFlags flags; | |
74 flags.setStyle(cc::PaintFlags::kFill_Style); | |
75 flags.setColor(kWindowBackgroundColor); | |
76 canvas->DrawRoundRect(GetLocalBounds(), kWindowCornerRadius, flags); | |
77 views::WidgetDelegateView::OnPaint(canvas); | |
78 } | |
79 | |
80 void GetAccessibleNodeData(ui::AXNodeData* node_data) override { | |
81 node_data->SetName(accessible_name_); | |
82 node_data->role = ui::AX_ROLE_ALERT; | |
83 } | |
84 | |
85 private: | |
86 base::string16 text_; | |
87 base::string16 accessible_name_; | |
88 int text_width_; | |
89 int width_; | |
90 int height_; | |
91 | |
92 DISALLOW_COPY_AND_ASSIGN(ExitWarningWidgetDelegateView); | |
93 }; | |
94 | |
95 } // namespace | |
96 | |
97 ExitWarningHandler::ExitWarningHandler() | |
98 : state_(IDLE), stub_timer_for_test_(false) {} | |
99 | |
100 ExitWarningHandler::~ExitWarningHandler() { | |
101 // Note: If a timer is outstanding, it is stopped in its destructor. | |
102 Hide(); | |
103 } | |
104 | |
105 void ExitWarningHandler::HandleAccelerator() { | |
106 switch (state_) { | |
107 case IDLE: | |
108 state_ = WAIT_FOR_DOUBLE_PRESS; | |
109 Show(); | |
110 StartTimer(); | |
111 WmShell::Get()->RecordUserMetricsAction(UMA_ACCEL_EXIT_FIRST_Q); | |
112 break; | |
113 case WAIT_FOR_DOUBLE_PRESS: | |
114 state_ = EXITING; | |
115 CancelTimer(); | |
116 Hide(); | |
117 WmShell::Get()->RecordUserMetricsAction(UMA_ACCEL_EXIT_SECOND_Q); | |
118 WmShell::Get()->delegate()->Exit(); | |
119 break; | |
120 case EXITING: | |
121 break; | |
122 } | |
123 } | |
124 | |
125 void ExitWarningHandler::TimerAction() { | |
126 Hide(); | |
127 if (state_ == WAIT_FOR_DOUBLE_PRESS) | |
128 state_ = IDLE; | |
129 } | |
130 | |
131 void ExitWarningHandler::StartTimer() { | |
132 if (stub_timer_for_test_) | |
133 return; | |
134 timer_.Start(FROM_HERE, | |
135 base::TimeDelta::FromMilliseconds(kTimeOutMilliseconds), this, | |
136 &ExitWarningHandler::TimerAction); | |
137 } | |
138 | |
139 void ExitWarningHandler::CancelTimer() { | |
140 timer_.Stop(); | |
141 } | |
142 | |
143 void ExitWarningHandler::Show() { | |
144 if (widget_) | |
145 return; | |
146 WmWindow* root_window = WmShell::Get()->GetRootWindowForNewWindows(); | |
147 ExitWarningWidgetDelegateView* delegate = new ExitWarningWidgetDelegateView; | |
148 gfx::Size rs = root_window->GetBounds().size(); | |
149 gfx::Size ps = delegate->GetPreferredSize(); | |
150 gfx::Rect bounds((rs.width() - ps.width()) / 2, | |
151 (rs.height() - ps.height()) / 3, ps.width(), ps.height()); | |
152 views::Widget::InitParams params; | |
153 params.type = views::Widget::InitParams::TYPE_POPUP; | |
154 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | |
155 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
156 params.accept_events = false; | |
157 params.keep_on_top = true; | |
158 params.remove_standard_frame = true; | |
159 params.delegate = delegate; | |
160 params.bounds = bounds; | |
161 params.name = "ExitWarningWindow"; | |
162 widget_.reset(new views::Widget); | |
163 root_window->GetRootWindowController()->ConfigureWidgetInitParamsForContainer( | |
164 widget_.get(), kShellWindowId_SettingBubbleContainer, ¶ms); | |
165 widget_->Init(params); | |
166 widget_->Show(); | |
167 | |
168 delegate->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true); | |
169 } | |
170 | |
171 void ExitWarningHandler::Hide() { | |
172 widget_.reset(); | |
173 } | |
174 | |
175 } // namespace ash | |
OLD | NEW |