Skip to content

Commit

Permalink
cros: Normal pin keys repeat on lock screen.
Browse files Browse the repository at this point in the history
ie, long pressing 1 will start repeating.

Bug: 800090
Change-Id: Ie395a8a8aece845ec6e24b296ec20f2dd291f7c1
Reviewed-on: https://chromium-review.googlesource.com/855038
Commit-Queue: Jacob Dufault <jdufault@chromium.org>
Reviewed-by: Xiyuan Xia <xiyuan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#527841}
  • Loading branch information
jacobdufault-google authored and Commit Bot committed Jan 9, 2018
1 parent 8298a84 commit 129433c
Showing 1 changed file with 60 additions and 62 deletions.
122 changes: 60 additions & 62 deletions ash/login/ui/login_pin_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ constexpr const char* kPinLabels[] = {

constexpr const char kLoginPinViewClassName[] = "LoginPinView";

// How long does the user have to long-press the backspace button before it
// auto-submits?
const int kInitialBackspaceDelayMs = 500;
// After the first auto-submit, how long until the next backspace event fires?
const int kRepeatingBackspaceDelayMs = 150;
// How long does the user have to long-press a button before it auto-submits?
const int kInitialLongPressDelayMs = 500;
// After the first auto-submit, how long until the next auto-submit event?
const int kRepeatingLongPressDelayMs = 150;

base::string16 GetButtonLabelForNumber(int value) {
DCHECK(value >= 0 && value < int{arraysize(kPinLabels)});
Expand All @@ -68,8 +67,11 @@ int GetViewIdForPinNumber(int number) {
// A base class for pin button in the pin keyboard.
class BasePinButton : public LoginButton, public views::ButtonListener {
public:
explicit BasePinButton(const base::Closure& on_press)
: LoginButton(this), on_press_(on_press) {
explicit BasePinButton(const base::RepeatingClosure& on_press)
: LoginButton(this),
on_press_(on_press),
delay_timer_(std::make_unique<base::OneShotTimer>()),
repeat_timer_(std::make_unique<base::RepeatingTimer>()) {
SetFocusBehavior(FocusBehavior::ALWAYS);
SetPreferredSize(
gfx::Size(LoginPinView::kButtonSizeDp, LoginPinView::kButtonSizeDp));
Expand All @@ -87,9 +89,38 @@ class BasePinButton : public LoginButton, public views::ButtonListener {

~BasePinButton() override = default;

void SetTimersForTesting(std::unique_ptr<base::Timer> delay_timer,
std::unique_ptr<base::Timer> repeat_timer) {
delay_timer_ = std::move(delay_timer);
repeat_timer_ = std::move(repeat_timer);
}

// LoginButton:
bool OnMousePressed(const ui::MouseEvent& event) override {
did_autosubmit_ = false;

if (IsTriggerableEvent(event) && enabled() &&
HitTestPoint(event.location())) {
delay_timer_->Start(
FROM_HERE,
base::TimeDelta::FromMilliseconds(kInitialLongPressDelayMs),
base::BindRepeating(&BasePinButton::DispatchRepeatablePressEvent,
base::Unretained(this)));
}

return LoginButton::OnMousePressed(event);
}
void OnMouseReleased(const ui::MouseEvent& event) override {
delay_timer_->Stop();
repeat_timer_->Stop();
LoginButton::OnMouseReleased(event);
}

// views::ButtonListener:
void ButtonPressed(Button* sender, const ui::Event& event) override {
DCHECK(sender == this);
if (did_autosubmit_)
return;

if (on_press_)
on_press_.Run();
Expand All @@ -99,14 +130,33 @@ class BasePinButton : public LoginButton, public views::ButtonListener {
base::Closure on_press_;

private:
void DispatchRepeatablePressEvent() {
// Start repeat timer if this was fired by the initial delay timer.
if (!repeat_timer_->IsRunning()) {
repeat_timer_->Start(
FROM_HERE,
base::TimeDelta::FromMilliseconds(kRepeatingLongPressDelayMs),
base::BindRepeating(&BasePinButton::DispatchRepeatablePressEvent,
base::Unretained(this)));
}

did_autosubmit_ = true;
on_press_.Run();
}

// If true, then the button submitted from a long-press repeat.
bool did_autosubmit_ = false;
std::unique_ptr<base::Timer> delay_timer_;
std::unique_ptr<base::Timer> repeat_timer_;

DISALLOW_COPY_AND_ASSIGN(BasePinButton);
};

// A PIN button that displays a digit number and corresponding letter mapping.
class DigitPinButton : public BasePinButton {
public:
DigitPinButton(int value, const LoginPinView::OnPinKey& on_key)
: BasePinButton(base::Bind(on_key, value)) {
: BasePinButton(base::BindRepeating(on_key, value)) {
set_id(GetViewIdForPinNumber(value));
const gfx::FontList& base_font_list = views::Label::GetDefaultFontList();
views::Label* label = new views::Label(GetButtonLabelForNumber(value),
Expand Down Expand Up @@ -149,10 +199,8 @@ const int LoginPinView::kButtonSizeDp = 48;
// A PIN button that displays backspace icon.
class LoginPinView::BackspacePinButton : public BasePinButton {
public:
BackspacePinButton(const base::Closure& on_press)
: BasePinButton(on_press),
delay_timer_(std::make_unique<base::OneShotTimer>()),
repeat_timer_(std::make_unique<base::RepeatingTimer>()) {
BackspacePinButton(const base::RepeatingClosure& on_press)
: BasePinButton(on_press) {
SetImage(views::Button::STATE_NORMAL,
gfx::CreateVectorIcon(kLockScreenBackspaceIcon,
login_constants::kButtonEnabledColor));
Expand All @@ -168,57 +216,7 @@ class LoginPinView::BackspacePinButton : public BasePinButton {

~BackspacePinButton() override = default;

void SetTimersForTesting(std::unique_ptr<base::Timer> delay_timer,
std::unique_ptr<base::Timer> repeat_timer) {
delay_timer_ = std::move(delay_timer);
repeat_timer_ = std::move(repeat_timer);
}

// BasePinButton:
bool OnMousePressed(const ui::MouseEvent& event) override {
did_autosubmit_ = false;

if (IsTriggerableEvent(event) && enabled() &&
HitTestPoint(event.location())) {
delay_timer_->Start(
FROM_HERE,
base::TimeDelta::FromMilliseconds(kInitialBackspaceDelayMs),
base::Bind(&BackspacePinButton::DispatchRepeatablePressEvent,
base::Unretained(this)));
}

return BasePinButton::OnMousePressed(event);
}
void OnMouseReleased(const ui::MouseEvent& event) override {
delay_timer_->Stop();
repeat_timer_->Stop();
BasePinButton::OnMouseReleased(event);
}
void ButtonPressed(Button* sender, const ui::Event& event) override {
if (did_autosubmit_)
return;
BasePinButton::ButtonPressed(sender, event);
}

private:
void DispatchRepeatablePressEvent() {
// Start repeat timer if this was fired by the initial delay timer.
if (!repeat_timer_->IsRunning()) {
repeat_timer_->Start(
FROM_HERE,
base::TimeDelta::FromMilliseconds(kRepeatingBackspaceDelayMs),
base::Bind(&BackspacePinButton::DispatchRepeatablePressEvent,
base::Unretained(this)));
}

did_autosubmit_ = true;
on_press_.Run();
}

bool did_autosubmit_ = false;
std::unique_ptr<base::Timer> delay_timer_;
std::unique_ptr<base::Timer> repeat_timer_;

DISALLOW_COPY_AND_ASSIGN(BackspacePinButton);
};

Expand Down

0 comments on commit 129433c

Please sign in to comment.