summaryrefslogtreecommitdiff
path: root/chromium/third_party/catapult/tracing/tracing/ui/base/timing_tool.html
blob: 85ca3e440abe02f258d9f6b8043c7bb02cd94707 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<!DOCTYPE html>
<!--
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.
-->

<link rel="import" href="/tracing/base/range.html">
<link rel="import" href="/tracing/model/event_set.html">
<link rel="import" href="/tracing/model/slice.html">
<link rel="import" href="/tracing/ui/base/ui.html">

<script>
'use strict';

/**
 * @fileoverview Provides the TimingTool class.
 */
tr.exportTo('tr.ui.b', function() {

  /**
   * Tool for taking time measurements in the TimelineTrackView using
   * Viewportmarkers.
   * @constructor
   */
  function TimingTool(viewport, targetElement) {
    this.viewport_ = viewport;

    // Prepare the event handlers to be added and removed repeatedly.
    this.onMouseMove_ = this.onMouseMove_.bind(this);
    this.onDblClick_ = this.onDblClick_.bind(this);
    this.targetElement_ = targetElement;

    // Valid only during mousedown.
    this.isMovingLeftEdge_ = false;
  }

  TimingTool.prototype = {

    onEnterTiming: function(e) {
      this.targetElement_.addEventListener('mousemove', this.onMouseMove_);
      this.targetElement_.addEventListener('dblclick', this.onDblClick_);
    },

    onBeginTiming: function(e) {
      if (!this.isTouchPointInsideTrackBounds_(e.clientX, e.clientY))
        return;

      var pt = this.getSnappedToEventPosition_(e);
      this.mouseDownAt_(pt.x, pt.y);

      this.updateSnapIndicators_(pt);
    },

    updateSnapIndicators_: function(pt) {
      if (!pt.snapped)
        return;
      var ir = this.viewport_.interestRange;
      if (ir.min === pt.x)
        ir.leftSnapIndicator = new tr.ui.SnapIndicator(pt.y, pt.height);
      if (ir.max === pt.x)
        ir.rightSnapIndicator = new tr.ui.SnapIndicator(pt.y, pt.height);
    },

    onUpdateTiming: function(e) {
      var pt = this.getSnappedToEventPosition_(e);
      this.mouseMoveAt_(pt.x, pt.y, true);
      this.updateSnapIndicators_(pt);
    },

    onEndTiming: function(e) {
      this.mouseUp_();
    },

    onExitTiming: function(e) {
      this.targetElement_.removeEventListener('mousemove', this.onMouseMove_);
      this.targetElement_.removeEventListener('dblclick', this.onDblClick_);
    },

    onMouseMove_: function(e) {
      if (e.button)
        return;
      var worldX = this.getWorldXFromEvent_(e);
      this.mouseMoveAt_(worldX, e.clientY, false);
    },

    onDblClick_: function(e) {
      // TODO(nduca): Implement dobuleclicking.
      console.error('not implemented');
    },

    ////////////////////////////////////////////////////////////////////////////

    isTouchPointInsideTrackBounds_: function(clientX, clientY) {
      if (!this.viewport_ ||
          !this.viewport_.modelTrackContainer ||
          !this.viewport_.modelTrackContainer.canvas)
        return false;

      var canvas = this.viewport_.modelTrackContainer.canvas;
      var canvasRect = canvas.getBoundingClientRect();
      if (clientX >= canvasRect.left && clientX <= canvasRect.right &&
          clientY >= canvasRect.top && clientY <= canvasRect.bottom)
        return true;

      return false;
    },

    mouseDownAt_: function(worldX, y) {
      var ir = this.viewport_.interestRange;
      var dt = this.viewport_.currentDisplayTransform;

      var pixelRatio = window.devicePixelRatio || 1;
      var nearnessThresholdWorld = dt.xViewVectorToWorld(6 * pixelRatio);

      if (ir.isEmpty) {
        ir.setMinAndMax(worldX, worldX);
        ir.rightSelected = true;
        this.isMovingLeftEdge_ = false;
        return;
      }


      // Left edge test.
      if (Math.abs(worldX - ir.min) < nearnessThresholdWorld) {
        ir.leftSelected = true;
        ir.min = worldX;
        this.isMovingLeftEdge_ = true;
        return;
      }

      // Right edge test.
      if (Math.abs(worldX - ir.max) < nearnessThresholdWorld) {
        ir.rightSelected = true;
        ir.max = worldX;
        this.isMovingLeftEdge_ = false;
        return;
      }

      ir.setMinAndMax(worldX, worldX);
      ir.rightSelected = true;
      this.isMovingLeftEdge_ = false;
    },

    mouseMoveAt_: function(worldX, y, mouseDown) {
      var ir = this.viewport_.interestRange;

      if (mouseDown) {
        this.updateMovingEdge_(worldX);
        return;
      }

      var ir = this.viewport_.interestRange;
      var dt = this.viewport_.currentDisplayTransform;

      var pixelRatio = window.devicePixelRatio || 1;
      var nearnessThresholdWorld = dt.xViewVectorToWorld(6 * pixelRatio);

      // Left edge test.
      if (Math.abs(worldX - ir.min) < nearnessThresholdWorld) {
        ir.leftSelected = true;
        ir.rightSelected = false;
        return;
      }

      // Right edge test.
      if (Math.abs(worldX - ir.max) < nearnessThresholdWorld) {
        ir.leftSelected = false;
        ir.rightSelected = true;
        return;
      }

      ir.leftSelected = false;
      ir.rightSelected = false;
      return;
    },

    updateMovingEdge_: function(newWorldX) {
      var ir = this.viewport_.interestRange;
      var a = ir.min;
      var b = ir.max;
      if (this.isMovingLeftEdge_)
        a = newWorldX;
      else
        b = newWorldX;

      if (a <= b)
        ir.setMinAndMax(a, b);
      else
        ir.setMinAndMax(b, a);

      if (ir.min === newWorldX) {
        this.isMovingLeftEdge_ = true;
        ir.leftSelected = true;
        ir.rightSelected = false;
      } else {
        this.isMovingLeftEdge_ = false;
        ir.leftSelected = false;
        ir.rightSelected = true;
      }
    },

    mouseUp_: function() {
      var dt = this.viewport_.currentDisplayTransform;
      var ir = this.viewport_.interestRange;

      ir.leftSelected = false;
      ir.rightSelected = false;

      var pixelRatio = window.devicePixelRatio || 1;
      var minWidthValue = dt.xViewVectorToWorld(2 * pixelRatio);
      if (ir.range < minWidthValue)
        ir.reset();
    },

    getWorldXFromEvent_: function(e) {
      var pixelRatio = window.devicePixelRatio || 1;
      var canvas = this.viewport_.modelTrackContainer.canvas;
      var worldOffset = canvas.getBoundingClientRect().left;
      var viewX = (e.clientX - worldOffset) * pixelRatio;
      return this.viewport_.currentDisplayTransform.xViewToWorld(viewX);
    },


    /**
     * Get the closest position of an event within a vertical range of the mouse
     * position if possible, otherwise use the position of the mouse pointer.
     * @param {MouseEvent} e Mouse event with the current mouse coordinates.
     * @return {
     *   {Number} x, The x coordinate in world space.
     *   {Number} y, The y coordinate in world space.
     *   {Number} height, The height of the event.
     *   {boolean} snapped Whether the coordinates are from a snapped event or
     *     the mouse position.
     * }
     */
    getSnappedToEventPosition_: function(e) {
      var pixelRatio = window.devicePixelRatio || 1;
      var EVENT_SNAP_RANGE = 16 * pixelRatio;

      var modelTrackContainer = this.viewport_.modelTrackContainer;
      var modelTrackContainerRect = modelTrackContainer.getBoundingClientRect();

      var viewport = this.viewport_;
      var dt = viewport.currentDisplayTransform;
      var worldMaxDist = dt.xViewVectorToWorld(EVENT_SNAP_RANGE);

      var worldX = this.getWorldXFromEvent_(e);
      var mouseY = e.clientY;

      var selection = new tr.model.EventSet();

      // Look at the track under mouse position first for better performance.
      modelTrackContainer.addClosestEventToSelection(
          worldX, worldMaxDist, mouseY, mouseY, selection);

      // Look at all tracks visible on screen.
      if (!selection.length) {
        modelTrackContainer.addClosestEventToSelection(
            worldX, worldMaxDist,
            modelTrackContainerRect.top, modelTrackContainerRect.bottom,
            selection);
      }

      var minDistX = worldMaxDist;
      var minDistY = Infinity;
      var pixWidth = dt.xViewVectorToWorld(1);

      // Create result object with the mouse coordinates.
      var result = {
        x: worldX,
        y: mouseY - modelTrackContainerRect.top,
        height: 0,
        snapped: false
      };

      var eventBounds = new tr.b.Range();
      for (var event of selection) {
        var track = viewport.trackForEvent(event);
        var trackRect = track.getBoundingClientRect();

        eventBounds.reset();
        event.addBoundsToRange(eventBounds);
        var eventX;
        if (Math.abs(eventBounds.min - worldX) <
            Math.abs(eventBounds.max - worldX)) {
          eventX = eventBounds.min;
        } else {
          eventX = eventBounds.max;
        }

        var distX = eventX - worldX;

        var eventY = trackRect.top;
        var eventHeight = trackRect.height;
        var distY = Math.abs(eventY + eventHeight / 2 - mouseY);

        // Prefer events with a closer y position if their x difference is below
        // the width of a pixel.
        if ((distX <= minDistX || Math.abs(distX - minDistX) < pixWidth) &&
            distY < minDistY) {
          minDistX = distX;
          minDistY = distY;

          // Retrieve the event position from the hit.
          result.x = eventX;
          result.y = eventY +
              modelTrackContainer.scrollTop - modelTrackContainerRect.top;
          result.height = eventHeight;
          result.snapped = true;
        }
      }

      return result;
    }
  };

  return {
    TimingTool: TimingTool
  };
});
</script>