yuweih | e1bd6ca | 2017-05-19 01:22:45 | [diff] [blame] | 1 | // Copyright 2017 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 | |
nicholss | 7666a28 | 2017-05-24 15:48:02 | [diff] [blame] | 5 | #include "remoting/client/input/trackpad_input_strategy.h" |
yuweih | e1bd6ca | 2017-05-19 01:22:45 | [diff] [blame] | 6 | |
| 7 | #include "remoting/client/ui/desktop_viewport.h" |
| 8 | |
| 9 | namespace remoting { |
| 10 | |
| 11 | namespace { |
| 12 | |
| 13 | // Trackpad mode does not need tap feedback. |
| 14 | const float kTapFeedbackRadius = 0.f; |
| 15 | |
| 16 | const float kDragFeedbackRadius = 8.f; |
| 17 | |
| 18 | } // namespace |
| 19 | |
| 20 | TrackpadInputStrategy::TrackpadInputStrategy(const DesktopViewport& viewport) |
| 21 | : cursor_position_(viewport.GetViewportCenter()) {} |
| 22 | |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame^] | 23 | TrackpadInputStrategy::~TrackpadInputStrategy() = default; |
yuweih | e1bd6ca | 2017-05-19 01:22:45 | [diff] [blame] | 24 | |
| 25 | void TrackpadInputStrategy::HandleZoom(const ViewMatrix::Point& pivot, |
| 26 | float scale, |
| 27 | DesktopViewport* viewport) { |
| 28 | // The cursor position is the pivot point. |
| 29 | ViewMatrix::Point cursor_pivot = |
| 30 | viewport->GetTransformation().MapPoint(cursor_position_); |
| 31 | viewport->ScaleDesktop(cursor_pivot.x, cursor_pivot.y, scale); |
| 32 | |
| 33 | // Keep the cursor on focus. |
| 34 | viewport->SetViewportCenter(cursor_position_.x, cursor_position_.y); |
| 35 | } |
| 36 | |
| 37 | bool TrackpadInputStrategy::HandlePan(const ViewMatrix::Vector2D& translation, |
| 38 | Gesture simultaneous_gesture, |
| 39 | DesktopViewport* viewport) { |
| 40 | if (simultaneous_gesture == ZOOM) { |
| 41 | // Don't move the cursor if the user is scaling the viewport. |
| 42 | return false; |
| 43 | } |
| 44 | ViewMatrix::Vector2D translation_on_desktop = |
| 45 | viewport->GetTransformation().Invert().MapVector(translation); |
| 46 | |
| 47 | cursor_position_.x += translation_on_desktop.x; |
| 48 | cursor_position_.y += translation_on_desktop.y; |
| 49 | |
| 50 | cursor_position_ = viewport->ConstrainPointToDesktop(cursor_position_); |
| 51 | |
| 52 | // Keep the cursor on focus. |
| 53 | viewport->SetViewportCenter(cursor_position_.x, cursor_position_.y); |
| 54 | return true; |
| 55 | } |
| 56 | |
yuweih | b932f1d | 2017-05-23 21:18:52 | [diff] [blame] | 57 | bool TrackpadInputStrategy::TrackTouchInput( |
yuweih | e1bd6ca | 2017-05-19 01:22:45 | [diff] [blame] | 58 | const ViewMatrix::Point& touch_point, |
| 59 | const DesktopViewport& viewport) { |
| 60 | // Do nothing. The cursor position is independent of the touch position. |
yuweih | b932f1d | 2017-05-23 21:18:52 | [diff] [blame] | 61 | // |touch_point| is always valid. |
| 62 | return true; |
yuweih | e1bd6ca | 2017-05-19 01:22:45 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | ViewMatrix::Point TrackpadInputStrategy::GetCursorPosition() const { |
| 66 | return cursor_position_; |
| 67 | } |
| 68 | |
Yuwei Huang | dd311f7 | 2017-08-21 18:55:16 | [diff] [blame] | 69 | void TrackpadInputStrategy::FocusViewportOnCursor( |
| 70 | DesktopViewport* viewport) const { |
| 71 | viewport->SetViewportCenter(cursor_position_.x, cursor_position_.y); |
| 72 | } |
| 73 | |
yuweih | e1bd6ca | 2017-05-19 01:22:45 | [diff] [blame] | 74 | ViewMatrix::Vector2D TrackpadInputStrategy::MapScreenVectorToDesktop( |
| 75 | const ViewMatrix::Vector2D& delta, |
| 76 | const DesktopViewport& viewport) const { |
| 77 | // No conversion is needed for trackpad mode. |
| 78 | return delta; |
| 79 | } |
| 80 | |
nicholss | df09b28 | 2017-05-26 20:08:14 | [diff] [blame] | 81 | float TrackpadInputStrategy::GetFeedbackRadius(TouchFeedbackType type) const { |
yuweih | e1bd6ca | 2017-05-19 01:22:45 | [diff] [blame] | 82 | switch (type) { |
nicholss | df09b28 | 2017-05-26 20:08:14 | [diff] [blame] | 83 | case TouchFeedbackType::TAP_FEEDBACK: |
yuweih | e1bd6ca | 2017-05-19 01:22:45 | [diff] [blame] | 84 | return kTapFeedbackRadius; |
nicholss | df09b28 | 2017-05-26 20:08:14 | [diff] [blame] | 85 | case TouchFeedbackType::DRAG_FEEDBACK: |
yuweih | e1bd6ca | 2017-05-19 01:22:45 | [diff] [blame] | 86 | return kDragFeedbackRadius; |
| 87 | } |
| 88 | NOTREACHED(); |
| 89 | return 0.f; |
| 90 | } |
| 91 | |
| 92 | bool TrackpadInputStrategy::IsCursorVisible() const { |
| 93 | return true; |
| 94 | } |
nicholss | 7666a28 | 2017-05-24 15:48:02 | [diff] [blame] | 95 | |
yuweih | e1bd6ca | 2017-05-19 01:22:45 | [diff] [blame] | 96 | } // namespace remoting |