| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | |
| 5 /** | 4 /** |
| 6 * @constructor | 5 * @unrestricted |
| 7 * @extends {WebInspector.VBox} | |
| 8 */ | 6 */ |
| 9 WebInspector.ChartViewport = function() | 7 WebInspector.ChartViewport = class extends WebInspector.VBox { |
| 10 { | 8 constructor() { |
| 11 WebInspector.VBox.call(this, true); | 9 super(true); |
| 12 | 10 |
| 13 this.contentElement.addEventListener("mousewheel", this._onMouseWheel.bind(t
his), false); | 11 this.contentElement.addEventListener('mousewheel', this._onMouseWheel.bind(t
his), false); |
| 14 this.contentElement.addEventListener("keydown", this._handleZoomPanKeys.bind
(this), false); | 12 this.contentElement.addEventListener('keydown', this._handleZoomPanKeys.bind
(this), false); |
| 15 | 13 |
| 16 this.viewportElement = this.contentElement.createChild("div", "fill"); | 14 this.viewportElement = this.contentElement.createChild('div', 'fill'); |
| 17 WebInspector.installInertialDragHandle(this.viewportElement, this._startDrag
ging.bind(this), this._dragging.bind(this), this._endDragging.bind(this), "-webk
it-grabbing", null); | 15 WebInspector.installInertialDragHandle( |
| 18 WebInspector.installDragHandle(this.viewportElement, this._startRangeSelecti
on.bind(this), this._rangeSelectionDragging.bind(this), this._endRangeSelection.
bind(this), "text", null); | 16 this.viewportElement, this._startDragging.bind(this), this._dragging.bin
d(this), this._endDragging.bind(this), |
| 19 | 17 '-webkit-grabbing', null); |
| 20 this._vScrollElement = this.contentElement.createChild("div", "flame-chart-v
-scroll"); | 18 WebInspector.installDragHandle( |
| 21 this._vScrollContent = this._vScrollElement.createChild("div"); | 19 this.viewportElement, this._startRangeSelection.bind(this), this._rangeS
electionDragging.bind(this), |
| 22 this._vScrollElement.addEventListener("scroll", this._onScroll.bind(this), f
alse); | 20 this._endRangeSelection.bind(this), 'text', null); |
| 23 | 21 |
| 24 this._selectionOverlay = this.contentElement.createChild("div", "flame-chart
-selection-overlay hidden"); | 22 this._vScrollElement = this.contentElement.createChild('div', 'flame-chart-v
-scroll'); |
| 25 this._selectedTimeSpanLabel = this._selectionOverlay.createChild("div", "tim
e-span"); | 23 this._vScrollContent = this._vScrollElement.createChild('div'); |
| 24 this._vScrollElement.addEventListener('scroll', this._onScroll.bind(this), f
alse); |
| 25 |
| 26 this._selectionOverlay = this.contentElement.createChild('div', 'flame-chart
-selection-overlay hidden'); |
| 27 this._selectedTimeSpanLabel = this._selectionOverlay.createChild('div', 'tim
e-span'); |
| 26 | 28 |
| 27 this.reset(); | 29 this.reset(); |
| 30 } |
| 31 |
| 32 /** |
| 33 * @return {boolean} |
| 34 */ |
| 35 isDragging() { |
| 36 return this._isDragging; |
| 37 } |
| 38 |
| 39 /** |
| 40 * @override |
| 41 * @return {!Array<!Element>} |
| 42 */ |
| 43 elementsToRestoreScrollPositionsFor() { |
| 44 return [this._vScrollElement]; |
| 45 } |
| 46 |
| 47 /** |
| 48 * @private |
| 49 */ |
| 50 _updateScrollBar() { |
| 51 var showScroll = this._totalHeight > this._offsetHeight; |
| 52 if (this._vScrollElement.classList.contains('hidden') !== showScroll) |
| 53 return; |
| 54 this._vScrollElement.classList.toggle('hidden', !showScroll); |
| 55 this._updateContentElementSize(); |
| 56 } |
| 57 |
| 58 /** |
| 59 * @override |
| 60 */ |
| 61 onResize() { |
| 62 this._updateScrollBar(); |
| 63 this._updateContentElementSize(); |
| 64 this.scheduleUpdate(); |
| 65 } |
| 66 |
| 67 reset() { |
| 68 this._vScrollElement.scrollTop = 0; |
| 69 this._scrollTop = 0; |
| 70 this._rangeSelectionStart = 0; |
| 71 this._rangeSelectionEnd = 0; |
| 72 this._isDragging = false; |
| 73 this._dragStartPointX = 0; |
| 74 this._dragStartPointY = 0; |
| 75 this._dragStartScrollTop = 0; |
| 76 this._timeWindowLeft = 0; |
| 77 this._timeWindowRight = 0; |
| 78 this._offsetWidth = 0; |
| 79 this._offsetHeight = 0; |
| 80 this._totalHeight = 0; |
| 81 this._pendingAnimationTimeLeft = 0; |
| 82 this._pendingAnimationTimeRight = 0; |
| 83 } |
| 84 |
| 85 /** |
| 86 * @private |
| 87 */ |
| 88 _updateContentElementSize() { |
| 89 var offsetWidth = this._vScrollElement.offsetLeft; |
| 90 if (!offsetWidth) |
| 91 offsetWidth = this.contentElement.offsetWidth; |
| 92 this._offsetWidth = offsetWidth; |
| 93 this._offsetHeight = this.contentElement.offsetHeight; |
| 94 } |
| 95 |
| 96 setContentHeight(totalHeight) { |
| 97 this._totalHeight = totalHeight; |
| 98 this._vScrollContent.style.height = totalHeight + 'px'; |
| 99 this._updateScrollBar(); |
| 100 if (this._scrollTop + this._offsetHeight <= totalHeight) |
| 101 return; |
| 102 this._scrollTop = Math.max(0, totalHeight - this._offsetHeight); |
| 103 this._vScrollElement.scrollTop = this._scrollTop; |
| 104 } |
| 105 |
| 106 /** |
| 107 * @param {number} offset |
| 108 * @param {number=} height |
| 109 */ |
| 110 setScrollOffset(offset, height) { |
| 111 height = height || 0; |
| 112 if (this._vScrollElement.scrollTop > offset) |
| 113 this._vScrollElement.scrollTop = offset; |
| 114 else if (this._vScrollElement.scrollTop < offset - this._offsetHeight + heig
ht) |
| 115 this._vScrollElement.scrollTop = offset - this._offsetHeight + height; |
| 116 } |
| 117 |
| 118 /** |
| 119 * @return {number} |
| 120 */ |
| 121 scrollOffset() { |
| 122 return this._vScrollElement.scrollTop; |
| 123 } |
| 124 |
| 125 /** |
| 126 * @param {!Event} e |
| 127 * @private |
| 128 */ |
| 129 _onMouseWheel(e) { |
| 130 if (!this._enabled()) |
| 131 return; |
| 132 // Pan vertically when shift down only. |
| 133 var panVertically = e.shiftKey && (e.wheelDeltaY || Math.abs(e.wheelDeltaX)
=== 120); |
| 134 var panHorizontally = Math.abs(e.wheelDeltaX) > Math.abs(e.wheelDeltaY) && !
e.shiftKey; |
| 135 if (panVertically) { |
| 136 this._vScrollElement.scrollTop -= (e.wheelDeltaY || e.wheelDeltaX) / 120 *
this._offsetHeight / 8; |
| 137 } else if (panHorizontally) { |
| 138 var shift = -e.wheelDeltaX * this._pixelToTime; |
| 139 this._muteAnimation = true; |
| 140 this._handlePanGesture(shift); |
| 141 this._muteAnimation = false; |
| 142 } else { // Zoom. |
| 143 const mouseWheelZoomSpeed = 1 / 120; |
| 144 this._handleZoomGesture(Math.pow(1.2, -(e.wheelDeltaY || e.wheelDeltaX) *
mouseWheelZoomSpeed) - 1); |
| 145 } |
| 146 |
| 147 // Block swipe gesture. |
| 148 e.consume(true); |
| 149 } |
| 150 |
| 151 /** |
| 152 * @param {number} x |
| 153 * @param {number} y |
| 154 * @param {!MouseEvent} event |
| 155 * @private |
| 156 * @return {boolean} |
| 157 */ |
| 158 _startDragging(x, y, event) { |
| 159 if (event.shiftKey) |
| 160 return false; |
| 161 if (this._windowRight === Infinity) |
| 162 return false; |
| 163 this._isDragging = true; |
| 164 this._initMaxDragOffset(event); |
| 165 this._dragStartPointX = x; |
| 166 this._dragStartPointY = y; |
| 167 this._dragStartScrollTop = this._vScrollElement.scrollTop; |
| 168 this.viewportElement.style.cursor = ''; |
| 169 this.hideHighlight(); |
| 170 return true; |
| 171 } |
| 172 |
| 173 /** |
| 174 * @param {number} x |
| 175 * @param {number} y |
| 176 * @private |
| 177 */ |
| 178 _dragging(x, y) { |
| 179 var pixelShift = this._dragStartPointX - x; |
| 180 this._dragStartPointX = x; |
| 181 this._muteAnimation = true; |
| 182 this._handlePanGesture(pixelShift * this._pixelToTime); |
| 183 this._muteAnimation = false; |
| 184 |
| 185 var pixelScroll = this._dragStartPointY - y; |
| 186 this._vScrollElement.scrollTop = this._dragStartScrollTop + pixelScroll; |
| 187 this._updateMaxDragOffset(x, y); |
| 188 } |
| 189 |
| 190 /** |
| 191 * @private |
| 192 */ |
| 193 _endDragging() { |
| 194 this._isDragging = false; |
| 195 this._updateHighlight(); |
| 196 } |
| 197 |
| 198 /** |
| 199 * @param {!MouseEvent} event |
| 200 * @private |
| 201 */ |
| 202 _initMaxDragOffset(event) { |
| 203 this._maxDragOffsetSquared = 0; |
| 204 this._dragStartX = event.pageX; |
| 205 this._dragStartY = event.pageY; |
| 206 } |
| 207 |
| 208 /** |
| 209 * @param {number} x |
| 210 * @param {number} y |
| 211 * @private |
| 212 */ |
| 213 _updateMaxDragOffset(x, y) { |
| 214 var dx = x - this._dragStartX; |
| 215 var dy = y - this._dragStartY; |
| 216 var dragOffsetSquared = dx * dx + dy * dy; |
| 217 this._maxDragOffsetSquared = Math.max(this._maxDragOffsetSquared, dragOffset
Squared); |
| 218 } |
| 219 |
| 220 /** |
| 221 * @return {number} |
| 222 */ |
| 223 maxDragOffset() { |
| 224 return Math.sqrt(this._maxDragOffsetSquared); |
| 225 } |
| 226 |
| 227 /** |
| 228 * @param {!MouseEvent} event |
| 229 * @private |
| 230 * @return {boolean} |
| 231 */ |
| 232 _startRangeSelection(event) { |
| 233 if (!event.shiftKey) |
| 234 return false; |
| 235 this._isDragging = true; |
| 236 this._initMaxDragOffset(event); |
| 237 this._selectionOffsetShiftX = event.offsetX - event.pageX; |
| 238 this._selectionOffsetShiftY = event.offsetY - event.pageY; |
| 239 this._selectionStartX = event.offsetX; |
| 240 var style = this._selectionOverlay.style; |
| 241 style.left = this._selectionStartX + 'px'; |
| 242 style.width = '1px'; |
| 243 this._selectedTimeSpanLabel.textContent = ''; |
| 244 this._selectionOverlay.classList.remove('hidden'); |
| 245 this.hideHighlight(); |
| 246 return true; |
| 247 } |
| 248 |
| 249 /** |
| 250 * @private |
| 251 */ |
| 252 _endRangeSelection() { |
| 253 this._isDragging = false; |
| 254 this._updateHighlight(); |
| 255 } |
| 256 |
| 257 hideRangeSelection() { |
| 258 this._selectionOverlay.classList.add('hidden'); |
| 259 } |
| 260 |
| 261 /** |
| 262 * @param {!MouseEvent} event |
| 263 * @private |
| 264 */ |
| 265 _rangeSelectionDragging(event) { |
| 266 this._updateMaxDragOffset(event.pageX, event.pageY); |
| 267 var x = Number.constrain(event.pageX + this._selectionOffsetShiftX, 0, this.
_offsetWidth); |
| 268 var start = this._cursorTime(this._selectionStartX); |
| 269 var end = this._cursorTime(x); |
| 270 this._rangeSelectionStart = Math.min(start, end); |
| 271 this._rangeSelectionEnd = Math.max(start, end); |
| 272 this._updateRangeSelectionOverlay(); |
| 273 this._flameChartDelegate.updateRangeSelection(this._rangeSelectionStart, thi
s._rangeSelectionEnd); |
| 274 } |
| 275 |
| 276 /** |
| 277 * @private |
| 278 */ |
| 279 _updateRangeSelectionOverlay() { |
| 280 var /** @const */ margin = 100; |
| 281 var left = Number.constrain(this._timeToPosition(this._rangeSelectionStart),
-margin, this._offsetWidth + margin); |
| 282 var right = Number.constrain(this._timeToPosition(this._rangeSelectionEnd),
-margin, this._offsetWidth + margin); |
| 283 var style = this._selectionOverlay.style; |
| 284 style.left = left + 'px'; |
| 285 style.width = (right - left) + 'px'; |
| 286 var timeSpan = this._rangeSelectionEnd - this._rangeSelectionStart; |
| 287 this._selectedTimeSpanLabel.textContent = Number.preciseMillisToString(timeS
pan, 2); |
| 288 } |
| 289 |
| 290 /** |
| 291 * @private |
| 292 */ |
| 293 _onScroll() { |
| 294 this._scrollTop = this._vScrollElement.scrollTop; |
| 295 this.scheduleUpdate(); |
| 296 } |
| 297 |
| 298 /** |
| 299 * @param {!Event} e |
| 300 * @private |
| 301 */ |
| 302 _handleZoomPanKeys(e) { |
| 303 if (!WebInspector.KeyboardShortcut.hasNoModifiers(e)) |
| 304 return; |
| 305 var zoomMultiplier = e.shiftKey ? 0.8 : 0.3; |
| 306 var panMultiplier = e.shiftKey ? 320 : 80; |
| 307 if (e.code === 'KeyA') { |
| 308 this._handlePanGesture(-panMultiplier * this._pixelToTime); |
| 309 e.consume(true); |
| 310 } else if (e.code === 'KeyD') { |
| 311 this._handlePanGesture(panMultiplier * this._pixelToTime); |
| 312 e.consume(true); |
| 313 } else if (e.code === 'KeyW') { |
| 314 this._handleZoomGesture(-zoomMultiplier); |
| 315 e.consume(true); |
| 316 } else if (e.code === 'KeyS') { |
| 317 this._handleZoomGesture(zoomMultiplier); |
| 318 e.consume(true); |
| 319 } |
| 320 } |
| 321 |
| 322 /** |
| 323 * @param {number} zoom |
| 324 * @private |
| 325 */ |
| 326 _handleZoomGesture(zoom) { |
| 327 this._cancelAnimation(); |
| 328 var bounds = this._windowForGesture(); |
| 329 var cursorTime = this._cursorTime(this._lastMouseOffsetX); |
| 330 bounds.left += (bounds.left - cursorTime) * zoom; |
| 331 bounds.right += (bounds.right - cursorTime) * zoom; |
| 332 this._requestWindowTimes(bounds); |
| 333 } |
| 334 |
| 335 /** |
| 336 * @param {number} shift |
| 337 * @private |
| 338 */ |
| 339 _handlePanGesture(shift) { |
| 340 this._cancelAnimation(); |
| 341 var bounds = this._windowForGesture(); |
| 342 shift = Number.constrain( |
| 343 shift, this._minimumBoundary - bounds.left, this._totalTime + this._mini
mumBoundary - bounds.right); |
| 344 bounds.left += shift; |
| 345 bounds.right += shift; |
| 346 this._requestWindowTimes(bounds); |
| 347 } |
| 348 |
| 349 /** |
| 350 * @private |
| 351 * @return {{left: number, right: number}} |
| 352 */ |
| 353 _windowForGesture() { |
| 354 var windowLeft = this._timeWindowLeft ? this._timeWindowLeft : this._dataPro
vider.minimumBoundary(); |
| 355 var windowRight = this._timeWindowRight !== Infinity ? |
| 356 this._timeWindowRight : |
| 357 this._dataProvider.minimumBoundary() + this._dataProvider.totalTime(); |
| 358 return {left: windowLeft, right: windowRight}; |
| 359 } |
| 360 |
| 361 /** |
| 362 * @param {{left: number, right: number}} bounds |
| 363 * @private |
| 364 */ |
| 365 _requestWindowTimes(bounds) { |
| 366 bounds.left = Number.constrain(bounds.left, this._minimumBoundary, this._tot
alTime + this._minimumBoundary); |
| 367 bounds.right = Number.constrain(bounds.right, this._minimumBoundary, this._t
otalTime + this._minimumBoundary); |
| 368 if (bounds.right - bounds.left < WebInspector.FlameChart.MinimalTimeWindowMs
) |
| 369 return; |
| 370 this._flameChartDelegate.requestWindowTimes(bounds.left, bounds.right); |
| 371 } |
| 372 |
| 373 /** |
| 374 * @param {number} startTime |
| 375 * @param {number} endTime |
| 376 * @private |
| 377 */ |
| 378 _animateWindowTimes(startTime, endTime) { |
| 379 this._timeWindowLeft = startTime; |
| 380 this._timeWindowRight = endTime; |
| 381 this._updateHighlight(); |
| 382 this.update(); |
| 383 } |
| 384 |
| 385 /** |
| 386 * @private |
| 387 */ |
| 388 _animationCompleted() { |
| 389 delete this._cancelWindowTimesAnimation; |
| 390 this._updateHighlight(); |
| 391 } |
| 392 |
| 393 /** |
| 394 * @private |
| 395 */ |
| 396 _cancelAnimation() { |
| 397 if (!this._cancelWindowTimesAnimation) |
| 398 return; |
| 399 this._timeWindowLeft = this._pendingAnimationTimeLeft; |
| 400 this._timeWindowRight = this._pendingAnimationTimeRight; |
| 401 this._cancelWindowTimesAnimation(); |
| 402 delete this._cancelWindowTimesAnimation; |
| 403 } |
| 404 |
| 405 scheduleUpdate() { |
| 406 if (this._updateTimerId || this._cancelWindowTimesAnimation) |
| 407 return; |
| 408 this._updateTimerId = this.element.window().requestAnimationFrame(() => { |
| 409 this._updateTimerId = 0; |
| 410 this.update(); |
| 411 }); |
| 412 } |
| 413 |
| 414 update() { |
| 415 } |
| 416 |
| 417 /** |
| 418 * @param {number} startTime |
| 419 * @param {number} endTime |
| 420 */ |
| 421 setWindowTimes(startTime, endTime) { |
| 422 this.hideRangeSelection(); |
| 423 if (this._muteAnimation || this._timeWindowLeft === 0 || this._timeWindowRig
ht === Infinity || |
| 424 (startTime === 0 && endTime === Infinity) || (startTime === Infinity &&
endTime === Infinity)) { |
| 425 // Initial setup. |
| 426 this._timeWindowLeft = startTime; |
| 427 this._timeWindowRight = endTime; |
| 428 this.scheduleUpdate(); |
| 429 return; |
| 430 } |
| 431 this._cancelAnimation(); |
| 432 this._cancelWindowTimesAnimation = WebInspector.animateFunction( |
| 433 this.element.window(), this._animateWindowTimes.bind(this), |
| 434 [{from: this._timeWindowLeft, to: startTime}, {from: this._timeWindowRig
ht, to: endTime}], 5, |
| 435 this._animationCompleted.bind(this)); |
| 436 this._pendingAnimationTimeLeft = startTime; |
| 437 this._pendingAnimationTimeRight = endTime; |
| 438 } |
| 28 }; | 439 }; |
| 29 | |
| 30 WebInspector.ChartViewport.prototype = { | |
| 31 /** | |
| 32 * @return {boolean} | |
| 33 */ | |
| 34 isDragging: function() | |
| 35 { | |
| 36 return this._isDragging; | |
| 37 }, | |
| 38 | |
| 39 /** | |
| 40 * @override | |
| 41 * @return {!Array<!Element>} | |
| 42 */ | |
| 43 elementsToRestoreScrollPositionsFor: function() | |
| 44 { | |
| 45 return [this._vScrollElement]; | |
| 46 }, | |
| 47 | |
| 48 /** | |
| 49 * @private | |
| 50 */ | |
| 51 _updateScrollBar: function() | |
| 52 { | |
| 53 var showScroll = this._totalHeight > this._offsetHeight; | |
| 54 if (this._vScrollElement.classList.contains("hidden") !== showScroll) | |
| 55 return; | |
| 56 this._vScrollElement.classList.toggle("hidden", !showScroll); | |
| 57 this._updateContentElementSize(); | |
| 58 }, | |
| 59 | |
| 60 /** | |
| 61 * @override | |
| 62 */ | |
| 63 onResize: function() | |
| 64 { | |
| 65 this._updateScrollBar(); | |
| 66 this._updateContentElementSize(); | |
| 67 this.scheduleUpdate(); | |
| 68 }, | |
| 69 | |
| 70 reset: function() | |
| 71 { | |
| 72 this._vScrollElement.scrollTop = 0; | |
| 73 this._scrollTop = 0; | |
| 74 this._rangeSelectionStart = 0; | |
| 75 this._rangeSelectionEnd = 0; | |
| 76 this._isDragging = false; | |
| 77 this._dragStartPointX = 0; | |
| 78 this._dragStartPointY = 0; | |
| 79 this._dragStartScrollTop = 0; | |
| 80 this._timeWindowLeft = 0; | |
| 81 this._timeWindowRight = 0; | |
| 82 this._offsetWidth = 0; | |
| 83 this._offsetHeight = 0; | |
| 84 this._totalHeight = 0; | |
| 85 this._pendingAnimationTimeLeft = 0; | |
| 86 this._pendingAnimationTimeRight = 0; | |
| 87 }, | |
| 88 | |
| 89 /** | |
| 90 * @private | |
| 91 */ | |
| 92 _updateContentElementSize: function() | |
| 93 { | |
| 94 var offsetWidth = this._vScrollElement.offsetLeft; | |
| 95 if (!offsetWidth) | |
| 96 offsetWidth = this.contentElement.offsetWidth; | |
| 97 this._offsetWidth = offsetWidth; | |
| 98 this._offsetHeight = this.contentElement.offsetHeight; | |
| 99 }, | |
| 100 | |
| 101 setContentHeight: function(totalHeight) | |
| 102 { | |
| 103 this._totalHeight = totalHeight; | |
| 104 this._vScrollContent.style.height = totalHeight + "px"; | |
| 105 this._updateScrollBar(); | |
| 106 if (this._scrollTop + this._offsetHeight <= totalHeight) | |
| 107 return; | |
| 108 this._scrollTop = Math.max(0, totalHeight - this._offsetHeight); | |
| 109 this._vScrollElement.scrollTop = this._scrollTop; | |
| 110 }, | |
| 111 | |
| 112 /** | |
| 113 * @param {number} offset | |
| 114 * @param {number=} height | |
| 115 */ | |
| 116 setScrollOffset: function(offset, height) | |
| 117 { | |
| 118 height = height || 0; | |
| 119 if (this._vScrollElement.scrollTop > offset) | |
| 120 this._vScrollElement.scrollTop = offset; | |
| 121 else if (this._vScrollElement.scrollTop < offset - this._offsetHeight +
height) | |
| 122 this._vScrollElement.scrollTop = offset - this._offsetHeight + heigh
t; | |
| 123 }, | |
| 124 | |
| 125 /** | |
| 126 * @return {number} | |
| 127 */ | |
| 128 scrollOffset: function() | |
| 129 { | |
| 130 return this._vScrollElement.scrollTop; | |
| 131 }, | |
| 132 | |
| 133 /** | |
| 134 * @param {!Event} e | |
| 135 * @private | |
| 136 */ | |
| 137 _onMouseWheel: function(e) | |
| 138 { | |
| 139 if (!this._enabled()) | |
| 140 return; | |
| 141 // Pan vertically when shift down only. | |
| 142 var panVertically = e.shiftKey && (e.wheelDeltaY || Math.abs(e.wheelDelt
aX) === 120); | |
| 143 var panHorizontally = Math.abs(e.wheelDeltaX) > Math.abs(e.wheelDeltaY)
&& !e.shiftKey; | |
| 144 if (panVertically) { | |
| 145 this._vScrollElement.scrollTop -= (e.wheelDeltaY || e.wheelDeltaX) /
120 * this._offsetHeight / 8; | |
| 146 } else if (panHorizontally) { | |
| 147 var shift = -e.wheelDeltaX * this._pixelToTime; | |
| 148 this._muteAnimation = true; | |
| 149 this._handlePanGesture(shift); | |
| 150 this._muteAnimation = false; | |
| 151 } else { // Zoom. | |
| 152 const mouseWheelZoomSpeed = 1 / 120; | |
| 153 this._handleZoomGesture(Math.pow(1.2, -(e.wheelDeltaY || e.wheelDelt
aX) * mouseWheelZoomSpeed) - 1); | |
| 154 } | |
| 155 | |
| 156 // Block swipe gesture. | |
| 157 e.consume(true); | |
| 158 }, | |
| 159 | |
| 160 /** | |
| 161 * @param {number} x | |
| 162 * @param {number} y | |
| 163 * @param {!MouseEvent} event | |
| 164 * @private | |
| 165 * @return {boolean} | |
| 166 */ | |
| 167 _startDragging: function(x, y, event) | |
| 168 { | |
| 169 if (event.shiftKey) | |
| 170 return false; | |
| 171 if (this._windowRight === Infinity) | |
| 172 return false; | |
| 173 this._isDragging = true; | |
| 174 this._initMaxDragOffset(event); | |
| 175 this._dragStartPointX = x; | |
| 176 this._dragStartPointY = y; | |
| 177 this._dragStartScrollTop = this._vScrollElement.scrollTop; | |
| 178 this.viewportElement.style.cursor = ""; | |
| 179 this.hideHighlight(); | |
| 180 return true; | |
| 181 }, | |
| 182 | |
| 183 /** | |
| 184 * @param {number} x | |
| 185 * @param {number} y | |
| 186 * @private | |
| 187 */ | |
| 188 _dragging: function(x, y) | |
| 189 { | |
| 190 var pixelShift = this._dragStartPointX - x; | |
| 191 this._dragStartPointX = x; | |
| 192 this._muteAnimation = true; | |
| 193 this._handlePanGesture(pixelShift * this._pixelToTime); | |
| 194 this._muteAnimation = false; | |
| 195 | |
| 196 var pixelScroll = this._dragStartPointY - y; | |
| 197 this._vScrollElement.scrollTop = this._dragStartScrollTop + pixelScroll; | |
| 198 this._updateMaxDragOffset(x, y); | |
| 199 }, | |
| 200 | |
| 201 /** | |
| 202 * @private | |
| 203 */ | |
| 204 _endDragging: function() | |
| 205 { | |
| 206 this._isDragging = false; | |
| 207 this._updateHighlight(); | |
| 208 }, | |
| 209 | |
| 210 /** | |
| 211 * @param {!MouseEvent} event | |
| 212 * @private | |
| 213 */ | |
| 214 _initMaxDragOffset: function(event) | |
| 215 { | |
| 216 this._maxDragOffsetSquared = 0; | |
| 217 this._dragStartX = event.pageX; | |
| 218 this._dragStartY = event.pageY; | |
| 219 }, | |
| 220 | |
| 221 /** | |
| 222 * @param {number} x | |
| 223 * @param {number} y | |
| 224 * @private | |
| 225 */ | |
| 226 _updateMaxDragOffset: function(x, y) | |
| 227 { | |
| 228 var dx = x - this._dragStartX; | |
| 229 var dy = y - this._dragStartY; | |
| 230 var dragOffsetSquared = dx * dx + dy * dy; | |
| 231 this._maxDragOffsetSquared = Math.max(this._maxDragOffsetSquared, dragOf
fsetSquared); | |
| 232 }, | |
| 233 | |
| 234 /** | |
| 235 * @return {number} | |
| 236 */ | |
| 237 maxDragOffset: function() | |
| 238 { | |
| 239 return Math.sqrt(this._maxDragOffsetSquared); | |
| 240 }, | |
| 241 | |
| 242 /** | |
| 243 * @param {!MouseEvent} event | |
| 244 * @private | |
| 245 * @return {boolean} | |
| 246 */ | |
| 247 _startRangeSelection: function(event) | |
| 248 { | |
| 249 if (!event.shiftKey) | |
| 250 return false; | |
| 251 this._isDragging = true; | |
| 252 this._initMaxDragOffset(event); | |
| 253 this._selectionOffsetShiftX = event.offsetX - event.pageX; | |
| 254 this._selectionOffsetShiftY = event.offsetY - event.pageY; | |
| 255 this._selectionStartX = event.offsetX; | |
| 256 var style = this._selectionOverlay.style; | |
| 257 style.left = this._selectionStartX + "px"; | |
| 258 style.width = "1px"; | |
| 259 this._selectedTimeSpanLabel.textContent = ""; | |
| 260 this._selectionOverlay.classList.remove("hidden"); | |
| 261 this.hideHighlight(); | |
| 262 return true; | |
| 263 }, | |
| 264 | |
| 265 /** | |
| 266 * @private | |
| 267 */ | |
| 268 _endRangeSelection: function() | |
| 269 { | |
| 270 this._isDragging = false; | |
| 271 this._updateHighlight(); | |
| 272 }, | |
| 273 | |
| 274 hideRangeSelection: function() | |
| 275 { | |
| 276 this._selectionOverlay.classList.add("hidden"); | |
| 277 }, | |
| 278 | |
| 279 /** | |
| 280 * @param {!MouseEvent} event | |
| 281 * @private | |
| 282 */ | |
| 283 _rangeSelectionDragging: function(event) | |
| 284 { | |
| 285 this._updateMaxDragOffset(event.pageX, event.pageY); | |
| 286 var x = Number.constrain(event.pageX + this._selectionOffsetShiftX, 0, t
his._offsetWidth); | |
| 287 var start = this._cursorTime(this._selectionStartX); | |
| 288 var end = this._cursorTime(x); | |
| 289 this._rangeSelectionStart = Math.min(start, end); | |
| 290 this._rangeSelectionEnd = Math.max(start, end); | |
| 291 this._updateRangeSelectionOverlay(); | |
| 292 this._flameChartDelegate.updateRangeSelection(this._rangeSelectionStart,
this._rangeSelectionEnd); | |
| 293 }, | |
| 294 | |
| 295 /** | |
| 296 * @private | |
| 297 */ | |
| 298 _updateRangeSelectionOverlay: function() | |
| 299 { | |
| 300 var /** @const */ margin = 100; | |
| 301 var left = Number.constrain(this._timeToPosition(this._rangeSelectionSta
rt), -margin, this._offsetWidth + margin); | |
| 302 var right = Number.constrain(this._timeToPosition(this._rangeSelectionEn
d), -margin, this._offsetWidth + margin); | |
| 303 var style = this._selectionOverlay.style; | |
| 304 style.left = left + "px"; | |
| 305 style.width = (right - left) + "px"; | |
| 306 var timeSpan = this._rangeSelectionEnd - this._rangeSelectionStart; | |
| 307 this._selectedTimeSpanLabel.textContent = Number.preciseMillisToString(t
imeSpan, 2); | |
| 308 }, | |
| 309 | |
| 310 /** | |
| 311 * @private | |
| 312 */ | |
| 313 _onScroll: function() | |
| 314 { | |
| 315 this._scrollTop = this._vScrollElement.scrollTop; | |
| 316 this.scheduleUpdate(); | |
| 317 }, | |
| 318 | |
| 319 /** | |
| 320 * @param {!Event} e | |
| 321 * @private | |
| 322 */ | |
| 323 _handleZoomPanKeys: function(e) | |
| 324 { | |
| 325 if (!WebInspector.KeyboardShortcut.hasNoModifiers(e)) | |
| 326 return; | |
| 327 var zoomMultiplier = e.shiftKey ? 0.8 : 0.3; | |
| 328 var panMultiplier = e.shiftKey ? 320 : 80; | |
| 329 if (e.code === "KeyA") { | |
| 330 this._handlePanGesture(-panMultiplier * this._pixelToTime); | |
| 331 e.consume(true); | |
| 332 } else if (e.code === "KeyD") { | |
| 333 this._handlePanGesture(panMultiplier * this._pixelToTime); | |
| 334 e.consume(true); | |
| 335 } else if (e.code === "KeyW") { | |
| 336 this._handleZoomGesture(-zoomMultiplier); | |
| 337 e.consume(true); | |
| 338 } else if (e.code === "KeyS") { | |
| 339 this._handleZoomGesture(zoomMultiplier); | |
| 340 e.consume(true); | |
| 341 } | |
| 342 }, | |
| 343 | |
| 344 /** | |
| 345 * @param {number} zoom | |
| 346 * @private | |
| 347 */ | |
| 348 _handleZoomGesture: function(zoom) | |
| 349 { | |
| 350 this._cancelAnimation(); | |
| 351 var bounds = this._windowForGesture(); | |
| 352 var cursorTime = this._cursorTime(this._lastMouseOffsetX); | |
| 353 bounds.left += (bounds.left - cursorTime) * zoom; | |
| 354 bounds.right += (bounds.right - cursorTime) * zoom; | |
| 355 this._requestWindowTimes(bounds); | |
| 356 }, | |
| 357 | |
| 358 /** | |
| 359 * @param {number} shift | |
| 360 * @private | |
| 361 */ | |
| 362 _handlePanGesture: function(shift) | |
| 363 { | |
| 364 this._cancelAnimation(); | |
| 365 var bounds = this._windowForGesture(); | |
| 366 shift = Number.constrain(shift, this._minimumBoundary - bounds.left, thi
s._totalTime + this._minimumBoundary - bounds.right); | |
| 367 bounds.left += shift; | |
| 368 bounds.right += shift; | |
| 369 this._requestWindowTimes(bounds); | |
| 370 }, | |
| 371 | |
| 372 /** | |
| 373 * @private | |
| 374 * @return {{left: number, right: number}} | |
| 375 */ | |
| 376 _windowForGesture: function() | |
| 377 { | |
| 378 var windowLeft = this._timeWindowLeft ? this._timeWindowLeft : this._dat
aProvider.minimumBoundary(); | |
| 379 var windowRight = this._timeWindowRight !== Infinity ? this._timeWindowR
ight : this._dataProvider.minimumBoundary() + this._dataProvider.totalTime(); | |
| 380 return {left: windowLeft, right: windowRight}; | |
| 381 }, | |
| 382 | |
| 383 /** | |
| 384 * @param {{left: number, right: number}} bounds | |
| 385 * @private | |
| 386 */ | |
| 387 _requestWindowTimes: function(bounds) | |
| 388 { | |
| 389 bounds.left = Number.constrain(bounds.left, this._minimumBoundary, this.
_totalTime + this._minimumBoundary); | |
| 390 bounds.right = Number.constrain(bounds.right, this._minimumBoundary, thi
s._totalTime + this._minimumBoundary); | |
| 391 if (bounds.right - bounds.left < WebInspector.FlameChart.MinimalTimeWind
owMs) | |
| 392 return; | |
| 393 this._flameChartDelegate.requestWindowTimes(bounds.left, bounds.right); | |
| 394 }, | |
| 395 | |
| 396 /** | |
| 397 * @param {number} startTime | |
| 398 * @param {number} endTime | |
| 399 * @private | |
| 400 */ | |
| 401 _animateWindowTimes: function(startTime, endTime) | |
| 402 { | |
| 403 this._timeWindowLeft = startTime; | |
| 404 this._timeWindowRight = endTime; | |
| 405 this._updateHighlight(); | |
| 406 this.update(); | |
| 407 }, | |
| 408 | |
| 409 /** | |
| 410 * @private | |
| 411 */ | |
| 412 _animationCompleted: function() | |
| 413 { | |
| 414 delete this._cancelWindowTimesAnimation; | |
| 415 this._updateHighlight(); | |
| 416 }, | |
| 417 | |
| 418 /** | |
| 419 * @private | |
| 420 */ | |
| 421 _cancelAnimation: function() | |
| 422 { | |
| 423 if (!this._cancelWindowTimesAnimation) | |
| 424 return; | |
| 425 this._timeWindowLeft = this._pendingAnimationTimeLeft; | |
| 426 this._timeWindowRight = this._pendingAnimationTimeRight; | |
| 427 this._cancelWindowTimesAnimation(); | |
| 428 delete this._cancelWindowTimesAnimation; | |
| 429 }, | |
| 430 | |
| 431 scheduleUpdate: function() | |
| 432 { | |
| 433 if (this._updateTimerId || this._cancelWindowTimesAnimation) | |
| 434 return; | |
| 435 this._updateTimerId = this.element.window().requestAnimationFrame(() =>
{ | |
| 436 this._updateTimerId = 0; | |
| 437 this.update(); | |
| 438 }); | |
| 439 }, | |
| 440 | |
| 441 update: function() {}, | |
| 442 | |
| 443 /** | |
| 444 * @param {number} startTime | |
| 445 * @param {number} endTime | |
| 446 */ | |
| 447 setWindowTimes: function(startTime, endTime) | |
| 448 { | |
| 449 this.hideRangeSelection(); | |
| 450 if (this._muteAnimation || this._timeWindowLeft === 0 || this._timeWindo
wRight === Infinity || (startTime === 0 && endTime === Infinity) || (startTime =
== Infinity && endTime === Infinity)) { | |
| 451 // Initial setup. | |
| 452 this._timeWindowLeft = startTime; | |
| 453 this._timeWindowRight = endTime; | |
| 454 this.scheduleUpdate(); | |
| 455 return; | |
| 456 } | |
| 457 this._cancelAnimation(); | |
| 458 this._cancelWindowTimesAnimation = WebInspector.animateFunction(this.ele
ment.window(), this._animateWindowTimes.bind(this), | |
| 459 [{from: this._timeWindowLeft, to: startTime}, {from: this._timeWindo
wRight, to: endTime}], 5, | |
| 460 this._animationCompleted.bind(this)); | |
| 461 this._pendingAnimationTimeLeft = startTime; | |
| 462 this._pendingAnimationTimeRight = endTime; | |
| 463 }, | |
| 464 | |
| 465 __proto__: WebInspector.VBox.prototype | |
| 466 }; | |
| OLD | NEW |