summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/resources/pdf/viewport_scroller.js
blob: ca16c9cf7d2604261d823f7f91ae6ca71e82894b (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// 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.

'use strict';

/**
 * Creates a new ViewportScroller.
 * A ViewportScroller scrolls the page in response to drag selection with the
 * mouse.
 *
 */
class ViewportScroller {
  /**
   * @param {Object} viewport The viewport info of the page.
   * @param {Object} plugin The PDF plugin element.
   * @param {Object} window The window containing the viewer.
   */
  constructor(viewport, plugin, window) {
    this.viewport_ = viewport;
    this.plugin_ = plugin;
    this.window_ = window;
    this.mousemoveCallback_ = null;
    this.timerId_ = null;
    this.scrollVelocity_ = null;
    this.lastFrameTime_ = 0;
  }

  /**
   * Start scrolling the page by |scrollVelocity_| every
   * |DRAG_TIMER_INTERVAL_MS_|.
   *
   * @private
   */
  startDragScrollTimer_() {
    if (this.timerId_ === null) {
      this.timerId_ = this.window_.setInterval(
          this.dragScrollPage_.bind(this),
          ViewportScroller.DRAG_TIMER_INTERVAL_MS_);
      this.lastFrameTime_ = Date.now();
    }
  }

  /**
   * Stops the drag scroll timer if it is active.
   *
   * @private
   */
  stopDragScrollTimer_() {
    if (this.timerId_ !== null) {
      this.window_.clearInterval(this.timerId_);
      this.timerId_ = null;
      this.lastFrameTime_ = 0;
    }
  }

  /**
   * Scrolls the viewport by the current scroll velocity.
   *
   * @private
   */
  dragScrollPage_() {
    const position = this.viewport_.position;
    const currentFrameTime = Date.now();
    const timeAdjustment = (currentFrameTime - this.lastFrameTime_) /
        ViewportScroller.DRAG_TIMER_INTERVAL_MS_;
    position.y += (this.scrollVelocity_.y * timeAdjustment);
    position.x += (this.scrollVelocity_.x * timeAdjustment);
    this.viewport_.position = position;
    this.lastFrameTime_ = currentFrameTime;
  }

  /**
   * Calculate the velocity to scroll while dragging using the distance of the
   * cursor outside the viewport.
   *
   * @param {Object} event The mousemove event.
   * @return {Object} Object with x and y direction scroll velocity.
   * @private
   */
  calculateVelocity_(event) {
    const x =
        Math.min(
            Math.max(
                -event.offsetX, event.offsetX - this.plugin_.offsetWidth, 0),
            ViewportScroller.MAX_DRAG_SCROLL_DISTANCE_) *
        Math.sign(event.offsetX);
    const y =
        Math.min(
            Math.max(
                -event.offsetY, event.offsetY - this.plugin_.offsetHeight, 0),
            ViewportScroller.MAX_DRAG_SCROLL_DISTANCE_) *
        Math.sign(event.offsetY);
    return {x: x, y: y};
  }

  /**
   * Handles mousemove events. It updates the scroll velocity and starts and
   * stops timer based on scroll velocity.
   *
   * @param {Object} event The mousemove event.
   * @private
   */
  onMousemove_(event) {
    this.scrollVelocity_ = this.calculateVelocity_(event);
    if (!this.scrollVelocity_.x && !this.scrollVelocity_.y) {
      this.stopDragScrollTimer_();
    } else if (!this.timerId_) {
      this.startDragScrollTimer_();
    }
  }

  /**
   * Sets whether to scroll the viewport when the mouse is outside the
   * viewport.
   *
   * @param {boolean} isSelecting Represents selection status.
   */
  setEnableScrolling(isSelecting) {
    if (isSelecting) {
      if (!this.mousemoveCallback_) {
        this.mousemoveCallback_ = this.onMousemove_.bind(this);
      }
      this.plugin_.addEventListener(
          'mousemove', this.mousemoveCallback_, false);
    } else {
      this.stopDragScrollTimer_();
      if (this.mousemoveCallback_) {
        this.plugin_.removeEventListener(
            'mousemove', this.mousemoveCallback_, false);
      }
    }
  }
}

/**
 * The period of time in milliseconds to wait between updating the viewport
 * position by the scroll velocity.
 *
 * @private
 */
ViewportScroller.DRAG_TIMER_INTERVAL_MS_ = 100;

/**
 * The maximum drag scroll distance per DRAG_TIMER_INTERVAL in pixels.
 *
 * @private
 */
ViewportScroller.MAX_DRAG_SCROLL_DISTANCE_ = 100;