forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow users to extend a longpress-triggered selection by dragging their finger continuously after the longpress. Enabled using the "--enable-longpress-drag-selection" flag on Android. BUG=466749 Review URL: https://codereview.chromium.org/1087893003 Cr-Commit-Position: refs/heads/master@{#332439}
- Loading branch information
jdduke
authored and
Commit bot
committed
Jun 2, 2015
1 parent
d6d92d4
commit 3f2a3ab
Showing
17 changed files
with
1,026 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// Copyright 2015 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "ui/touch_selection/longpress_drag_selector.h" | ||
|
||
#include "base/auto_reset.h" | ||
#include "ui/events/gesture_detection/motion_event.h" | ||
|
||
namespace ui { | ||
|
||
LongPressDragSelector::LongPressDragSelector( | ||
LongPressDragSelectorClient* client) | ||
: client_(client), | ||
state_(INACTIVE), | ||
has_longpress_drag_start_anchor_(false) { | ||
} | ||
|
||
LongPressDragSelector::~LongPressDragSelector() { | ||
} | ||
|
||
bool LongPressDragSelector::WillHandleTouchEvent(const MotionEvent& event) { | ||
switch (event.GetAction()) { | ||
case MotionEvent::ACTION_DOWN: | ||
touch_down_position_.SetPoint(event.GetX(), event.GetY()); | ||
touch_down_time_ = event.GetEventTime(); | ||
has_longpress_drag_start_anchor_ = false; | ||
SetState(LONGPRESS_PENDING); | ||
return false; | ||
|
||
case MotionEvent::ACTION_UP: | ||
case MotionEvent::ACTION_CANCEL: | ||
SetState(INACTIVE); | ||
return false; | ||
|
||
case MotionEvent::ACTION_MOVE: | ||
break; | ||
|
||
default: | ||
return false; | ||
} | ||
|
||
if (state_ != DRAG_PENDING && state_ != DRAGGING) | ||
return false; | ||
|
||
gfx::PointF position(event.GetX(), event.GetY()); | ||
if (state_ == DRAGGING) { | ||
gfx::PointF drag_position = position + longpress_drag_selection_offset_; | ||
client_->OnDragUpdate(*this, drag_position); | ||
return true; | ||
} | ||
|
||
// We can't use |touch_down_position_| as the offset anchor, as | ||
// showing the selection UI may have shifted the motion coordinates. | ||
if (!has_longpress_drag_start_anchor_) { | ||
has_longpress_drag_start_anchor_ = true; | ||
longpress_drag_start_anchor_ = position; | ||
return true; | ||
} | ||
|
||
// Allow an additional slop affordance after the longpress occurs. | ||
gfx::Vector2dF delta = position - longpress_drag_start_anchor_; | ||
if (client_->IsWithinTapSlop(delta)) | ||
return true; | ||
|
||
gfx::PointF selection_start = client_->GetSelectionStart(); | ||
gfx::PointF selection_end = client_->GetSelectionEnd(); | ||
bool extend_selection_start = false; | ||
if (std::abs(delta.y()) > std::abs(delta.x())) { | ||
// If initial motion is up/down, extend the start/end selection bound. | ||
extend_selection_start = delta.y() < 0; | ||
} else { | ||
// Otherwise extend the selection bound toward which we're moving. | ||
// Note that, for mixed RTL text, or for multiline selections triggered | ||
// by longpress, this may not pick the most suitable drag target | ||
gfx::Vector2dF start_delta = selection_start - position; | ||
|
||
// The vectors must be normalized to make dot product comparison meaningful. | ||
if (!start_delta.IsZero()) | ||
start_delta.Scale(1.f / start_delta.Length()); | ||
gfx::Vector2dF end_delta = selection_end - position; | ||
if (!end_delta.IsZero()) | ||
end_delta.Scale(1.f / start_delta.Length()); | ||
|
||
// The larger the dot product the more similar the direction. | ||
extend_selection_start = | ||
gfx::DotProduct(start_delta, delta) > gfx::DotProduct(end_delta, delta); | ||
} | ||
|
||
gfx::PointF extent = extend_selection_start ? selection_start : selection_end; | ||
longpress_drag_selection_offset_ = extent - position; | ||
client_->OnDragBegin(*this, extent); | ||
SetState(DRAGGING); | ||
return true; | ||
} | ||
|
||
bool LongPressDragSelector::IsActive() const { | ||
return state_ != INACTIVE && state_ != LONGPRESS_PENDING; | ||
} | ||
|
||
void LongPressDragSelector::OnLongPressEvent(base::TimeTicks event_time, | ||
const gfx::PointF& position) { | ||
// We have no guarantees that the current gesture stream is aligned with the | ||
// observed touch stream. We only know that the gesture sequence is downstream | ||
// from the touch sequence. Using a time/distance heuristic helps ensure that | ||
// the observed longpress corresponds to the active touch sequence. | ||
if (state_ == LONGPRESS_PENDING && | ||
// Ensure the down event occurs *before* the longpress event. Use a | ||
// small time epsilon to account for floating point time conversion. | ||
(touch_down_time_ < event_time + base::TimeDelta::FromMicroseconds(10)) && | ||
client_->IsWithinTapSlop(touch_down_position_ - position)) { | ||
SetState(SELECTION_PENDING); | ||
} | ||
} | ||
|
||
void LongPressDragSelector::OnSelectionActivated() { | ||
if (state_ == SELECTION_PENDING) | ||
SetState(DRAG_PENDING); | ||
} | ||
|
||
void LongPressDragSelector::OnSelectionDeactivated() { | ||
SetState(INACTIVE); | ||
} | ||
|
||
void LongPressDragSelector::SetState(SelectionState state) { | ||
if (state_ == state) | ||
return; | ||
|
||
const bool was_dragging = state_ == DRAGGING; | ||
const bool was_active = IsActive(); | ||
state_ = state; | ||
|
||
// TODO(jdduke): Add UMA for tracking relative longpress drag frequency. | ||
if (was_dragging) | ||
client_->OnDragEnd(*this); | ||
|
||
if (was_active != IsActive()) | ||
client_->OnLongPressDragActiveStateChanged(); | ||
} | ||
|
||
} // namespace ui |
Oops, something went wrong.