Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(284)

Unified Diff: content/browser/renderer_host/input/synthetic_pinch_gesture.cc

Issue 95153002: Make touch-based synthetic gesture take touch slop into account. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Minor refactoring of pinch gesture. Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/renderer_host/input/synthetic_pinch_gesture.cc
diff --git a/content/browser/renderer_host/input/synthetic_pinch_gesture.cc b/content/browser/renderer_host/input/synthetic_pinch_gesture.cc
index 7700b6d59a587cac17af2b3b6800647be9c979f9..05ae2128eb9ad4a690b9ee2c35041569e6707985 100644
--- a/content/browser/renderer_host/input/synthetic_pinch_gesture.cc
+++ b/content/browser/renderer_host/input/synthetic_pinch_gesture.cc
@@ -10,97 +10,135 @@
#include "ui/events/latency_info.h"
namespace content {
-namespace {
-
-// TODO(dominikg): Use touch slop to compute this value.
-const float kMinPointerDistance = 40.0f;
-
-}
SyntheticPinchGesture::SyntheticPinchGesture(
const SyntheticPinchGestureParams& params)
- : params_(params), started_(false) {
+ : params_(params),
+ current_y_0_(0),
+ current_y_1_(0),
+ target_y_0_(0),
+ target_y_1_(0),
+ gesture_source_type_(SyntheticGestureParams::DEFAULT_INPUT),
+ state_(SETUP) {
DCHECK_GE(params_.total_num_pixels_covered, 0);
-
- float inner_distance_to_anchor = kMinPointerDistance / 2.0f;
- float outer_distance_to_anchor =
- inner_distance_to_anchor + params_.total_num_pixels_covered / 2.0f;
-
- // Move pointers away from each other to zoom in
- // or towards each other to zoom out.
- if (params_.zoom_in) {
- current_y_0_ = params_.anchor.y() - inner_distance_to_anchor;
- current_y_1_ = params_.anchor.y() + inner_distance_to_anchor;
- target_y_0_ = params_.anchor.y() - outer_distance_to_anchor;
- target_y_1_ = params_.anchor.y() + outer_distance_to_anchor;
- } else {
- current_y_0_ = params_.anchor.y() - outer_distance_to_anchor;
- current_y_1_ = params_.anchor.y() + outer_distance_to_anchor;
- target_y_0_ = params_.anchor.y() - inner_distance_to_anchor;
- target_y_1_ = params_.anchor.y() + inner_distance_to_anchor;
- }
}
SyntheticPinchGesture::~SyntheticPinchGesture() {}
SyntheticGesture::Result SyntheticPinchGesture::ForwardInputEvents(
const base::TimeDelta& interval, SyntheticGestureTarget* target) {
+ if (state_ == SETUP) {
+ gesture_source_type_ = params_.gesture_source_type;
+ if (gesture_source_type_ == SyntheticGestureParams::DEFAULT_INPUT)
+ gesture_source_type_ = target->GetDefaultSyntheticGestureSourceType();
- SyntheticGestureParams::GestureSourceType source =
- params_.gesture_source_type;
- if (source == SyntheticGestureParams::DEFAULT_INPUT)
- source = target->GetDefaultSyntheticGestureSourceType();
+ if (!target->SupportsSyntheticGestureSourceType(gesture_source_type_))
+ return SyntheticGesture::GESTURE_SOURCE_TYPE_NOT_SUPPORTED_BY_PLATFORM;
- if (!target->SupportsSyntheticGestureSourceType(source))
- return SyntheticGesture::GESTURE_SOURCE_TYPE_NOT_SUPPORTED_BY_PLATFORM;
+ SetupCoordinates(target);
- if (source == SyntheticGestureParams::TOUCH_INPUT)
- return ForwardTouchInputEvents(interval, target);
+ state_ = STARTED;
+ }
+
+ DCHECK_NE(gesture_source_type_, SyntheticGestureParams::DEFAULT_INPUT);
+ if (gesture_source_type_ == SyntheticGestureParams::TOUCH_INPUT)
+ ForwardTouchInputEvents(interval, target);
else
return SyntheticGesture::GESTURE_SOURCE_TYPE_NOT_IMPLEMENTED;
+
+ return (state_ == DONE) ? SyntheticGesture::GESTURE_FINISHED
+ : SyntheticGesture::GESTURE_RUNNING;
}
-SyntheticGesture::Result SyntheticPinchGesture::ForwardTouchInputEvents(
+void SyntheticPinchGesture::ForwardTouchInputEvents(
const base::TimeDelta& interval, SyntheticGestureTarget* target) {
- if (HasFinished())
- return SyntheticGesture::GESTURE_FINISHED;
-
- if (!started_) {
- touch_event_.PressPoint(params_.anchor.x(), current_y_0_);
- touch_event_.PressPoint(params_.anchor.x(), current_y_1_);
- ForwardTouchEvent(target);
- started_ = true;
+ switch (state_) {
+ case STARTED:
+ // Check for an early finish.
+ if (HasReachedTarget()) {
+ state_ = DONE;
+ break;
+ }
+ PressTouchPoints(target);
+ state_ = MOVING;
+ break;
+ case MOVING:
+ UpdateTouchPoints(interval);
+ MoveTouchPoints(target);
+ if (HasReachedTarget()) {
+ ReleaseTouchPoints(target);
+ state_ = DONE;
+ }
+ break;
+ case SETUP:
+ NOTREACHED()
+ << "State SETUP invalid for synthetic pinch.";
+ case DONE:
+ NOTREACHED()
+ << "State DONE invalid for synthetic pinch.";
}
+}
+void SyntheticPinchGesture::UpdateTouchPoints(base::TimeDelta interval) {
// Compute the delta for the first pointer. The other one moves exactly
// the same but in the opposite direction.
- float delta = GetDeltaForPointer0(interval);
+ const float delta = GetDeltaForPointer0(interval);
current_y_0_ += delta;
current_y_1_ -= delta;
+}
+
+void SyntheticPinchGesture::PressTouchPoints(SyntheticGestureTarget* target) {
+ touch_event_.PressPoint(params_.anchor.x(), current_y_0_);
+ touch_event_.PressPoint(params_.anchor.x(), current_y_1_);
+ ForwardTouchEvent(target);
+}
+
+void SyntheticPinchGesture::MoveTouchPoints(SyntheticGestureTarget* target) {
// The current pointer positions are stored as float but the pointer
- // coordinates of the input event are integers. Floor both positions so that
- // in case of an odd distance one of the pointers (the one whose position goes
- // down) moves one pixel further than the other. The explicit flooring is only
- // needed for negative values.
+ // coordinates of the input event are integers. Floor both positions so
+ // that in case of an odd distance one of the pointers (the one whose
+ // position goes down) moves one pixel further than the other. The
+ // explicit flooring is only needed for negative values.
touch_event_.MovePoint(0, params_.anchor.x(), floor(current_y_0_));
touch_event_.MovePoint(1, params_.anchor.x(), floor(current_y_1_));
ForwardTouchEvent(target);
+}
- if (HasFinished()) {
- touch_event_.ReleasePoint(0);
- touch_event_.ReleasePoint(1);
- ForwardTouchEvent(target);
- return SyntheticGesture::GESTURE_FINISHED;
- }
-
- return SyntheticGesture::GESTURE_RUNNING;
+void SyntheticPinchGesture::ReleaseTouchPoints(SyntheticGestureTarget* target) {
+ touch_event_.ReleasePoint(0);
+ touch_event_.ReleasePoint(1);
+ ForwardTouchEvent(target);
}
-void SyntheticPinchGesture::ForwardTouchEvent(SyntheticGestureTarget* target) {
+void SyntheticPinchGesture::ForwardTouchEvent(SyntheticGestureTarget* target)
+ const {
target->DispatchInputEventToPlatform(
InputEvent(touch_event_, ui::LatencyInfo(), false));
}
+void SyntheticPinchGesture::SetupCoordinates(SyntheticGestureTarget* target) {
+ DCHECK_EQ(state_, SETUP);
+
+ float inner_distance_to_anchor = 2 * target->GetTouchSlopInDips();
+ float outer_distance_to_anchor = inner_distance_to_anchor +
+ params_.total_num_pixels_covered / 2.0f +
+ target->GetTouchSlopInDips();
+
+ // Move pointers away from each other to zoom in
+ // or towards each other to zoom out.
+ if (params_.zoom_in) {
+ current_y_0_ = params_.anchor.y() - inner_distance_to_anchor;
+ current_y_1_ = params_.anchor.y() + inner_distance_to_anchor;
+ target_y_0_ = params_.anchor.y() - outer_distance_to_anchor;
+ target_y_1_ = params_.anchor.y() + outer_distance_to_anchor;
+ } else {
+ current_y_0_ = params_.anchor.y() - outer_distance_to_anchor;
+ current_y_1_ = params_.anchor.y() + outer_distance_to_anchor;
+ target_y_0_ = params_.anchor.y() - inner_distance_to_anchor;
+ target_y_1_ = params_.anchor.y() + inner_distance_to_anchor;
+ }
+}
+
float SyntheticPinchGesture::GetDeltaForPointer0(
const base::TimeDelta& interval) const {
float total_abs_delta =
@@ -123,7 +161,7 @@ float SyntheticPinchGesture::ComputeAbsoluteRemainingDistance() const {
return 2 * distance_0;
}
-bool SyntheticPinchGesture::HasFinished() const {
+bool SyntheticPinchGesture::HasReachedTarget() const {
return ComputeAbsoluteRemainingDistance() == 0;
}

Powered by Google App Engine
This is Rietveld 408576698