1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/accelerators/exit_warning_handler.h"
#include <memory>
#include "ash/accelerators/accelerator_lookup.h"
#include "ash/public/cpp/accelerator_actions.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "base/metrics/user_metrics.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/text_utils.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"
namespace ash {
namespace {
using AcceleratorDetails = AcceleratorLookup::AcceleratorDetails;
const int64_t kTimeOutMilliseconds = 2000;
// Color of the text of the warning message.
const SkColor kTextColor = SK_ColorWHITE;
// Color of the window background.
const SkColor kWindowBackgroundColor = SkColorSetARGB(0xC0, 0x0, 0x0, 0x0);
// Radius of the rounded corners of the window.
const int kWindowCornerRadius = 2;
const int kHorizontalMarginAroundText = 100;
const int kVerticalMarginAroundText = 100;
} // namespace
class ExitWarningWidgetDelegateView : public views::WidgetDelegateView {
public:
ExitWarningWidgetDelegateView() : text_width_(0) {
std::vector<AcceleratorDetails> accelerators =
Shell::Get()->accelerator_lookup()->GetAvailableAcceleratorsForAction(
AcceleratorAction::kExit);
CHECK(!accelerators.empty());
// TODO(jimmyxgong): For now fetch the first accelerator of the list. But
// maybe there's a possibility to check which accelerator was most recently
// pressed.
text_ = l10n_util::GetStringFUTF16(
IDS_ASH_SIGN_OUT_WARNING_POPUP_TEXT_DYNAMIC,
AcceleratorLookup::GetAcceleratorDetailsText(accelerators[0]));
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
const gfx::FontList& font_list =
rb.GetFontList(ui::ResourceBundle::LargeFont);
text_width_ = gfx::GetStringWidth(text_, font_list);
SetPreferredSize(
gfx::Size(text_width_ + kHorizontalMarginAroundText,
font_list.GetHeight() + kVerticalMarginAroundText));
auto label = std::make_unique<views::Label>();
label->SetText(text_);
label->SetHorizontalAlignment(gfx::ALIGN_CENTER);
label->SetFontList(font_list);
label->SetEnabledColor(kTextColor);
label->SetAutoColorReadabilityEnabled(false);
label->SetSubpixelRenderingEnabled(false);
AddChildView(std::move(label));
SetLayoutManager(std::make_unique<views::FillLayout>());
GetViewAccessibility().SetRole(ax::mojom::Role::kAlert);
GetViewAccessibility().SetName(l10n_util::GetStringUTF16(
IDS_ASH_SIGN_OUT_WARNING_POPUP_TEXT_ACCESSIBLE));
}
ExitWarningWidgetDelegateView(const ExitWarningWidgetDelegateView&) = delete;
ExitWarningWidgetDelegateView& operator=(
const ExitWarningWidgetDelegateView&) = delete;
void OnPaint(gfx::Canvas* canvas) override {
cc::PaintFlags flags;
flags.setStyle(cc::PaintFlags::kFill_Style);
flags.setColor(kWindowBackgroundColor);
canvas->DrawRoundRect(GetLocalBounds(), kWindowCornerRadius, flags);
views::WidgetDelegateView::OnPaint(canvas);
}
private:
std::u16string text_;
int text_width_;
};
ExitWarningHandler::ExitWarningHandler()
: state_(IDLE), stub_timer_for_test_(false) {}
ExitWarningHandler::~ExitWarningHandler() {
// Note: If a timer is outstanding, it is stopped in its destructor.
Hide();
}
void ExitWarningHandler::HandleAccelerator() {
switch (state_) {
case IDLE:
state_ = WAIT_FOR_DOUBLE_PRESS;
Show();
StartTimer();
base::RecordAction(base::UserMetricsAction("Accel_Exit_First_Q"));
break;
case WAIT_FOR_DOUBLE_PRESS:
state_ = EXITING;
CancelTimer();
Hide();
base::RecordAction(base::UserMetricsAction("Accel_Exit_Second_Q"));
Shell::Get()->session_controller()->RequestSignOut();
break;
case EXITING:
break;
}
}
void ExitWarningHandler::TimerAction() {
Hide();
if (state_ == WAIT_FOR_DOUBLE_PRESS)
state_ = IDLE;
}
void ExitWarningHandler::StartTimer() {
if (stub_timer_for_test_)
return;
timer_.Start(FROM_HERE, base::Milliseconds(kTimeOutMilliseconds), this,
&ExitWarningHandler::TimerAction);
}
void ExitWarningHandler::CancelTimer() {
timer_.Stop();
}
void ExitWarningHandler::Show() {
if (widget_)
return;
aura::Window* root_window = Shell::GetRootWindowForNewWindows();
ExitWarningWidgetDelegateView* delegate = new ExitWarningWidgetDelegateView;
gfx::Size rs = root_window->bounds().size();
gfx::Size ps = delegate->GetPreferredSize();
gfx::Rect bounds((rs.width() - ps.width()) / 2,
(rs.height() - ps.height()) / 3, ps.width(), ps.height());
views::Widget::InitParams params(
views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET,
views::Widget::InitParams::TYPE_POPUP);
params.opacity = views::Widget::InitParams::WindowOpacity::kTranslucent;
params.accept_events = false;
params.z_order = ui::ZOrderLevel::kFloatingUIElement;
params.delegate = delegate;
params.bounds = bounds;
params.name = "ExitWarningWindow";
params.parent =
root_window->GetChildById(kShellWindowId_SettingBubbleContainer);
widget_ = std::make_unique<views::Widget>();
widget_->Init(std::move(params));
widget_->Show();
delegate->NotifyAccessibilityEventDeprecated(ax::mojom::Event::kAlert, true);
}
void ExitWarningHandler::Hide() {
widget_.reset();
}
} // namespace ash
|