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/shell.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.h" | |
12 #include "base/timer.h" | |
13 #include "grit/ash_strings.h" | |
14 #include "ui/aura/root_window.h" | |
15 #include "ui/base/l10n/l10n_util.h" | |
16 #include "ui/base/resource/resource_bundle.h" | |
17 #include "ui/gfx/canvas.h" | |
18 #include "ui/gfx/font.h" | |
19 #include "ui/views/controls/label.h" | |
20 #include "ui/views/view.h" | |
21 #include "ui/views/widget/widget.h" | |
22 #include "ui/views/widget/widget_delegate.h" | |
23 | |
24 namespace ash { | |
25 namespace { | |
26 | |
27 const int64 kDoublePressTimeOutMilliseconds = 300; | |
28 const int64 kHoldTimeOutMilliseconds = 1700; | |
29 const SkColor kForegroundColor = 0xFFFFFFFF; | |
30 const SkColor kBackgroundColor = 0xE0808080; | |
31 const int kHorizontalMarginAroundText = 100; | |
32 const int kVerticalMarginAroundText = 100; | |
33 | |
34 class ExitWarningWidgetDelegateView : public views::WidgetDelegateView { | |
35 public: | |
36 ExitWarningWidgetDelegateView() : text_width_(0), width_(0), height_(0) { | |
37 text_ = l10n_util::GetStringUTF16(IDS_ASH_EXIT_WARNING_POPUP_TEXT); | |
38 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
39 font_ = rb.GetFont(ui::ResourceBundle::LargeFont); | |
40 text_width_ = font_.GetStringWidth(text_); | |
41 width_ = text_width_ + kHorizontalMarginAroundText; | |
42 height_ = font_.GetHeight() + kVerticalMarginAroundText; | |
43 views::Label* label = new views::Label; | |
44 label->SetText(text_); | |
45 label->SetHorizontalAlignment(gfx::ALIGN_CENTER); | |
46 label->SetFont(font_); | |
47 label->SetBounds(0, 0, width_, height_); | |
sky
2013/05/14 17:10:05
Better to remove this and set a filllayoutmanager
sschmitz
2013/05/14 21:36:43
Done.
| |
48 label->SetEnabledColor(kForegroundColor); | |
49 label->SetDisabledColor(kForegroundColor); | |
50 label->SetAutoColorReadabilityEnabled(false); | |
51 AddChildView(label); | |
52 } | |
53 | |
54 virtual gfx::Size GetPreferredSize() OVERRIDE { | |
55 return gfx::Size(width_, height_); | |
56 } | |
57 | |
58 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { | |
59 canvas->FillRect(GetLocalBounds(), kBackgroundColor); | |
60 views::WidgetDelegateView::OnPaint(canvas); | |
61 } | |
62 | |
63 private: | |
64 base::string16 text_; | |
65 gfx::Font font_; | |
66 int text_width_; | |
67 int width_; | |
68 int height_; | |
69 | |
70 DISALLOW_COPY_AND_ASSIGN(ExitWarningWidgetDelegateView); | |
71 }; | |
72 | |
73 } // namespace | |
74 | |
75 ExitWarningHandler::ExitWarningHandler() | |
76 : state_(IDLE), | |
77 widget_(NULL), | |
78 stub_timers_for_test_(false) { | |
79 } | |
80 | |
81 ExitWarningHandler::~ExitWarningHandler() { | |
82 } | |
sky
2013/05/14 17:10:05
What happens if you get here and widget_ is non-nu
sschmitz
2013/05/14 21:36:43
Added Hide(). The timers get canceled automaticall
| |
83 | |
84 void ExitWarningHandler::HandleExitKey(bool press) { | |
85 switch (state_) { | |
86 case IDLE: | |
87 if (press) { | |
88 state_ = WAIT_FOR_QUICK_RELEASE; | |
89 Show(); | |
90 StartTimers(); | |
91 } | |
92 break; | |
93 case WAIT_FOR_QUICK_RELEASE: | |
94 if (!press) | |
95 state_ = WAIT_FOR_DOUBLE_PRESS; | |
96 break; | |
97 case WAIT_FOR_DOUBLE_PRESS: | |
98 if (press) { | |
99 state_ = EXITING; | |
100 CancelTimers(); | |
101 Hide(); | |
102 Shell::GetInstance()->delegate()->Exit(); | |
103 } | |
104 break; | |
105 case WAIT_FOR_LONG_HOLD: | |
106 if (!press) | |
107 state_ = CANCELED; | |
108 break; | |
109 case CANCELED: | |
110 case EXITING: | |
111 break; | |
112 default: | |
113 NOTREACHED(); | |
114 break; | |
115 } | |
116 } | |
117 | |
118 void ExitWarningHandler::Timer1Action() { | |
119 if (state_ == WAIT_FOR_QUICK_RELEASE) | |
120 state_ = WAIT_FOR_LONG_HOLD; | |
121 else if (state_ == WAIT_FOR_DOUBLE_PRESS) | |
122 state_ = CANCELED; | |
123 } | |
124 | |
125 void ExitWarningHandler::Timer2Action() { | |
126 if (state_ == CANCELED) { | |
127 state_ = IDLE; | |
128 Hide(); | |
129 } | |
130 else if (state_ == WAIT_FOR_LONG_HOLD) { | |
131 state_ = EXITING; | |
132 Hide(); | |
133 Shell::GetInstance()->delegate()->Exit(); | |
134 } | |
135 } | |
136 | |
137 void ExitWarningHandler::StartTimers() { | |
138 if (stub_timers_for_test_) | |
139 return; | |
140 timer1_.Start(FROM_HERE, | |
141 base::TimeDelta::FromMilliseconds( | |
142 kDoublePressTimeOutMilliseconds), | |
143 this, | |
144 &ExitWarningHandler::Timer1Action); | |
145 timer2_.Start(FROM_HERE, | |
146 base::TimeDelta::FromMilliseconds(kHoldTimeOutMilliseconds), | |
147 this, | |
148 &ExitWarningHandler::Timer2Action); | |
149 } | |
150 | |
151 void ExitWarningHandler::CancelTimers() { | |
152 timer1_.Stop(); | |
153 timer2_.Stop(); | |
154 } | |
155 | |
156 void ExitWarningHandler::Show() { | |
157 if (widget_) | |
158 return; | |
159 aura::RootWindow* root_window = Shell::GetActiveRootWindow(); | |
160 ExitWarningWidgetDelegateView* delegate = new ExitWarningWidgetDelegateView; | |
161 gfx::Size rs = root_window->bounds().size(); | |
162 gfx::Size ps = delegate->GetPreferredSize(); | |
163 gfx::Rect bounds((rs.width() - ps.width()) / 2, | |
164 (rs.height() - ps.height()) / 3, | |
165 ps.width(), ps.height()); | |
166 views::Widget::InitParams params; | |
167 params.type = views::Widget::InitParams::TYPE_POPUP; | |
168 params.transient = true; | |
169 params.transparent = true; | |
170 params.accept_events = false; | |
171 params.can_activate = false; | |
172 params.keep_on_top = true; | |
173 params.remove_standard_frame = true; | |
174 params.delegate = delegate; | |
175 params.bounds = bounds; | |
176 params.parent = Shell::GetContainer( | |
177 root_window, | |
178 internal::kShellWindowId_SettingBubbleContainer); | |
179 widget_ = new views::Widget; | |
180 widget_->Init(params); | |
181 widget_->SetContentsView(delegate); | |
182 widget_->GetNativeView()->SetName("ExitWarningWindow"); | |
183 widget_->Show(); | |
184 } | |
185 | |
186 void ExitWarningHandler::Hide() { | |
187 if (!widget_) | |
188 return; | |
189 widget_->Close(); | |
190 widget_ = NULL; | |
191 } | |
192 | |
193 } // namespace ash | |
OLD | NEW |