summaryrefslogtreecommitdiffstats
path: root/content/browser/renderer_host/touch_smooth_scroll_gesture_aura.cc
blob: dd5b4013bd1d19a521dc2bd4382f5aa1eabefca3 (plain)
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
// Copyright (c) 2013 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 "content/browser/renderer_host/touch_smooth_scroll_gesture_aura.h"

#include "content/browser/renderer_host/render_widget_host_impl.h"
#include "ui/aura/root_window.h"
#include "ui/base/events/event.h"
#include "ui/base/events/event_utils.h"
#include "ui/gfx/transform.h"

namespace {

void InjectTouchEvent(const gfx::Point& location,
                      ui::EventType type,
                      aura::Window* window) {
  gfx::Point screen_location = location;
  // First convert the location from Window to RootWindow.
  aura::RootWindow* root_window = window->GetRootWindow();
  aura::Window::ConvertPointToTarget(window, root_window, &screen_location);
  // Then convert the location from RootWindow to screen.
  root_window->ConvertPointToHost(&screen_location);
  ui::TouchEvent touch(type, screen_location, 0, 0, ui::EventTimeForNow(),
                       1.0f, 1.0f, 1.0f, 1.0f);
  aura::RootWindowHostDelegate* root_window_host_delegate =
        root_window->AsRootWindowHostDelegate();
  root_window_host_delegate->OnHostTouchEvent(&touch);
}

}  // namespace

namespace content {

TouchSmoothScrollGestureAura::TouchSmoothScrollGestureAura(bool scroll_down,
                                                           int pixels_to_scroll,
                                                           int mouse_event_x,
                                                           int mouse_event_y,
                                                           aura::Window* window)
    : scroll_down_(scroll_down),
      pixels_to_scroll_(pixels_to_scroll),
      pixels_scrolled_(0),
      location_(mouse_event_x, mouse_event_y),
      window_(window) {
}

TouchSmoothScrollGestureAura::~TouchSmoothScrollGestureAura() {}

bool TouchSmoothScrollGestureAura::ForwardInputEvents(
    base::TimeTicks now,
    RenderWidgetHost* host) {
  if (pixels_scrolled_ >= pixels_to_scroll_)
    return false;

  RenderWidgetHostImpl* host_impl = RenderWidgetHostImpl::From(host);
  float position_delta = synthetic_gesture_calculator_.GetDelta(now,
      host_impl->GetSyntheticGestureMessageInterval());

  if (pixels_scrolled_ == 0) {
    InjectTouchEvent(location_, ui::ET_TOUCH_PRESSED, window_);
  }

  location_.Offset(0, scroll_down_ ? -position_delta : position_delta);
    InjectTouchEvent(location_, ui::ET_TOUCH_MOVED, window_);

  pixels_scrolled_ += abs(position_delta);

  if (pixels_scrolled_ >= pixels_to_scroll_) {
    InjectTouchEvent(location_, ui::ET_TOUCH_RELEASED, window_);
  }

  return true;
}

}  // namespace content