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