Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | // Copyright (c) 2015 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. |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 4 | |
Tim van der Lippe | 91d3256 | 2020-02-13 15:08:47 | [diff] [blame] | 5 | import * as Common from '../common/common.js'; |
| 6 | import * as Host from '../host/host.js'; |
Simon Zünd | 684ebae | 2020-03-09 11:44:16 | [diff] [blame] | 7 | import * as Platform from '../platform/platform.js'; |
Tim van der Lippe | 91d3256 | 2020-02-13 15:08:47 | [diff] [blame] | 8 | import * as SDK from '../sdk/sdk.js'; |
| 9 | import * as UI from '../ui/ui.js'; |
| 10 | |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 11 | import {AnimationGroupPreviewUI} from './AnimationGroupPreviewUI.js'; |
| 12 | import {AnimationEffect, AnimationGroup, AnimationImpl, AnimationModel, Events} from './AnimationModel.js'; // eslint-disable-line no-unused-vars |
| 13 | import {AnimationScreenshotPopover} from './AnimationScreenshotPopover.js'; |
| 14 | import {AnimationUI} from './AnimationUI.js'; |
| 15 | |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 16 | /** @type {!WeakMap<!SDK.DOMModel.DOMNode, !NodeUI>} */ |
| 17 | const nodeUIsByNode = new WeakMap(); |
| 18 | |
| 19 | /** @type {!WeakMap<!HTMLElement, number>} */ |
| 20 | const playbackRates = new WeakMap(); |
| 21 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 22 | /** |
Tim van der Lippe | 91d3256 | 2020-02-13 15:08:47 | [diff] [blame] | 23 | * @implements {SDK.SDKModel.SDKModelObserver<!AnimationModel>} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 24 | * @unrestricted |
| 25 | */ |
Tim van der Lippe | 91d3256 | 2020-02-13 15:08:47 | [diff] [blame] | 26 | export class AnimationTimeline extends UI.Widget.VBox { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 27 | constructor() { |
| 28 | super(true); |
Jack Franklin | 71519f8 | 2020-11-03 12:08:59 | [diff] [blame^] | 29 | this.registerRequiredCSS('animation/animationTimeline.css', {enableLegacyPatching: true}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 30 | this.element.classList.add('animations-timeline'); |
| 31 | |
Paul Lewis | 44e3757 | 2020-10-28 15:57:26 | [diff] [blame] | 32 | this._grid = UI.UIUtils.createSVGChild(this.contentElement, 'svg', 'animation-timeline-grid'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 33 | |
| 34 | this._playbackRate = 1; |
| 35 | this._allPaused = false; |
| 36 | this._createHeader(); |
| 37 | this._animationsContainer = this.contentElement.createChild('div', 'animation-timeline-rows'); |
| 38 | const timelineHint = this.contentElement.createChild('div', 'animation-timeline-rows-hint'); |
| 39 | timelineHint.textContent = ls`Select an effect above to inspect and modify.`; |
| 40 | |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 41 | /** @type {!Array<!HTMLElement>} */ |
| 42 | this._playbackRateButtons; |
| 43 | /** @type {!HTMLElement} */ |
| 44 | this._previewContainer; |
| 45 | /** @type {!HTMLElement} */ |
| 46 | this._timelineScrubber; |
| 47 | /** @type {!HTMLElement} */ |
| 48 | this._currentTime; |
| 49 | /** @type {!UI.PopoverHelper.PopoverHelper} */ |
| 50 | this._popoverHelper; |
| 51 | /** @type {!UI.Toolbar.ToolbarButton} */ |
| 52 | this._clearButton; |
| 53 | /** @type {?AnimationGroup} */ |
| 54 | this._selectedGroup; |
| 55 | /** @type {!Array<!AnimationUI>} */ |
| 56 | this._renderQueue; |
| 57 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 58 | /** @const */ this._defaultDuration = 100; |
| 59 | this._duration = this._defaultDuration; |
| 60 | /** @const */ this._timelineControlsWidth = 150; |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 61 | /** @type {!Map.<!Protocol.DOM.BackendNodeId, !NodeUI>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 62 | this._nodesMap = new Map(); |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 63 | /** @type {!Array<!AnimationUI>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 64 | this._uiAnimations = []; |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 65 | /** @type {!Array<!AnimationGroup>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 66 | this._groupBuffer = []; |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 67 | /** @type {!Map.<!AnimationGroup, !AnimationGroupPreviewUI>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 68 | this._previewMap = new Map(); |
| 69 | this._symbol = Symbol('animationTimeline'); |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 70 | /** @type {!Map.<string, !AnimationImpl>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 71 | this._animationsMap = new Map(); |
Paul Lewis | daac106 | 2020-03-05 14:37:10 | [diff] [blame] | 72 | SDK.SDKModel.TargetManager.instance().addModelListener( |
Tim van der Lippe | 91d3256 | 2020-02-13 15:08:47 | [diff] [blame] | 73 | SDK.DOMModel.DOMModel, SDK.DOMModel.Events.NodeRemoved, this._nodeRemoved, this); |
Paul Lewis | daac106 | 2020-03-05 14:37:10 | [diff] [blame] | 74 | SDK.SDKModel.TargetManager.instance().observeModels(AnimationModel, this); |
Tim van der Lippe | d1a00aa | 2020-08-19 16:03:56 | [diff] [blame] | 75 | UI.Context.Context.instance().addFlavorChangeListener(SDK.DOMModel.DOMNode, this._nodeChanged, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @override |
| 80 | */ |
| 81 | wasShown() { |
Paul Lewis | daac106 | 2020-03-05 14:37:10 | [diff] [blame] | 82 | for (const animationModel of SDK.SDKModel.TargetManager.instance().models(AnimationModel)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 83 | this._addEventListeners(animationModel); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 84 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @override |
| 89 | */ |
| 90 | willHide() { |
Paul Lewis | daac106 | 2020-03-05 14:37:10 | [diff] [blame] | 91 | for (const animationModel of SDK.SDKModel.TargetManager.instance().models(AnimationModel)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 92 | this._removeEventListeners(animationModel); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 93 | } |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 94 | |
| 95 | if (this._popoverHelper) { |
| 96 | this._popoverHelper.hidePopover(); |
| 97 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @override |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 102 | * @param {!AnimationModel} animationModel |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 103 | */ |
| 104 | modelAdded(animationModel) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 105 | if (this.isShowing()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 106 | this._addEventListeners(animationModel); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 107 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @override |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 112 | * @param {!AnimationModel} animationModel |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 113 | */ |
| 114 | modelRemoved(animationModel) { |
| 115 | this._removeEventListeners(animationModel); |
| 116 | } |
| 117 | |
| 118 | /** |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 119 | * @param {!AnimationModel} animationModel |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 120 | */ |
| 121 | _addEventListeners(animationModel) { |
| 122 | animationModel.ensureEnabled(); |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 123 | animationModel.addEventListener(Events.AnimationGroupStarted, this._animationGroupStarted, this); |
| 124 | animationModel.addEventListener(Events.ModelReset, this._reset, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 128 | * @param {!AnimationModel} animationModel |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 129 | */ |
| 130 | _removeEventListeners(animationModel) { |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 131 | animationModel.removeEventListener(Events.AnimationGroupStarted, this._animationGroupStarted, this); |
| 132 | animationModel.removeEventListener(Events.ModelReset, this._reset, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | _nodeChanged() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 136 | for (const nodeUI of this._nodesMap.values()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 137 | nodeUI._nodeChanged(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 138 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /** |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 142 | * @return {!HTMLElement} element |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 143 | */ |
| 144 | _createScrubber() { |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 145 | this._timelineScrubber = /** @type {!HTMLElement} */ (document.createElement('div')); |
Tim van der Lippe | e7f2705 | 2020-05-01 15:15:28 | [diff] [blame] | 146 | this._timelineScrubber.classList.add('animation-scrubber'); |
| 147 | this._timelineScrubber.classList.add('hidden'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 148 | this._timelineScrubberLine = this._timelineScrubber.createChild('div', 'animation-scrubber-line'); |
| 149 | this._timelineScrubberLine.createChild('div', 'animation-scrubber-head'); |
| 150 | this._timelineScrubber.createChild('div', 'animation-time-overlay'); |
| 151 | return this._timelineScrubber; |
| 152 | } |
| 153 | |
| 154 | _createHeader() { |
| 155 | const toolbarContainer = this.contentElement.createChild('div', 'animation-timeline-toolbar-container'); |
Tim van der Lippe | 91d3256 | 2020-02-13 15:08:47 | [diff] [blame] | 156 | const topToolbar = new UI.Toolbar.Toolbar('animation-timeline-toolbar', toolbarContainer); |
| 157 | this._clearButton = new UI.Toolbar.ToolbarButton(ls`Clear all`, 'largeicon-clear'); |
| 158 | this._clearButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click, this._reset.bind(this)); |
Kalon Hinds | 0fae2e7 | 2020-01-08 20:02:02 | [diff] [blame] | 159 | topToolbar.appendToolbarItem(this._clearButton); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 160 | topToolbar.appendSeparator(); |
| 161 | |
Tim van der Lippe | 91d3256 | 2020-02-13 15:08:47 | [diff] [blame] | 162 | this._pauseButton = new UI.Toolbar.ToolbarToggle(ls`Pause all`, 'largeicon-pause', 'largeicon-resume'); |
| 163 | this._pauseButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click, this._togglePauseAll.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 164 | topToolbar.appendToolbarItem(this._pauseButton); |
| 165 | |
| 166 | const playbackRateControl = toolbarContainer.createChild('div', 'animation-playback-rate-control'); |
Kalon Hinds | 0fae2e7 | 2020-01-08 20:02:02 | [diff] [blame] | 167 | playbackRateControl.addEventListener('keydown', this._handlePlaybackRateControlKeyDown.bind(this)); |
Kalon Hinds | 08dd368 | 2020-01-10 23:51:25 | [diff] [blame] | 168 | UI.ARIAUtils.markAsListBox(playbackRateControl); |
| 169 | UI.ARIAUtils.setAccessibleName(playbackRateControl, ls`Playback rates`); |
| 170 | |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 171 | /** @type {!Array<!HTMLElement>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 172 | this._playbackRateButtons = []; |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 173 | for (const playbackRate of GlobalPlaybackRates) { |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 174 | const button = |
| 175 | /** @type {!HTMLElement} */ (playbackRateControl.createChild('button', 'animation-playback-rate-button')); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 176 | button.textContent = playbackRate ? ls`${playbackRate * 100}%` : ls`Pause`; |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 177 | playbackRates.set(button, playbackRate); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 178 | button.addEventListener('click', this._setPlaybackRate.bind(this, playbackRate)); |
Kalon Hinds | 08dd368 | 2020-01-10 23:51:25 | [diff] [blame] | 179 | UI.ARIAUtils.markAsOption(button); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 180 | button.title = ls`Set speed to ${button.textContent}`; |
Kalon Hinds | 0fae2e7 | 2020-01-08 20:02:02 | [diff] [blame] | 181 | button.tabIndex = -1; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 182 | this._playbackRateButtons.push(button); |
| 183 | } |
| 184 | this._updatePlaybackControls(); |
| 185 | |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 186 | this._previewContainer = |
| 187 | /** @type {!HTMLElement} */ (this.contentElement.createChild('div', 'animation-timeline-buffer')); |
Kalon Hinds | 08dd368 | 2020-01-10 23:51:25 | [diff] [blame] | 188 | UI.ARIAUtils.markAsListBox(this._previewContainer); |
| 189 | UI.ARIAUtils.setAccessibleName(this._previewContainer, ls`Animation previews`); |
Tim van der Lippe | 91d3256 | 2020-02-13 15:08:47 | [diff] [blame] | 190 | this._popoverHelper = |
| 191 | new UI.PopoverHelper.PopoverHelper(this._previewContainer, this._getPopoverRequest.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 192 | this._popoverHelper.setDisableOnClick(true); |
| 193 | this._popoverHelper.setTimeout(0); |
| 194 | const emptyBufferHint = this.contentElement.createChild('div', 'animation-timeline-buffer-hint'); |
| 195 | emptyBufferHint.textContent = ls`Listening for animations...`; |
| 196 | const container = this.contentElement.createChild('div', 'animation-timeline-header'); |
| 197 | const controls = container.createChild('div', 'animation-controls'); |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 198 | this._currentTime = |
| 199 | /** @type {!HTMLElement} */ (controls.createChild('div', 'animation-timeline-current-time monospace')); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 200 | |
Tim van der Lippe | 91d3256 | 2020-02-13 15:08:47 | [diff] [blame] | 201 | const toolbar = new UI.Toolbar.Toolbar('animation-controls-toolbar', controls); |
| 202 | this._controlButton = new UI.Toolbar.ToolbarToggle(ls`Replay timeline`, 'largeicon-replay-animation'); |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 203 | this._controlState = _ControlState.Replay; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 204 | this._controlButton.setToggled(true); |
Tim van der Lippe | 91d3256 | 2020-02-13 15:08:47 | [diff] [blame] | 205 | this._controlButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click, this._controlButtonToggle.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 206 | toolbar.appendToolbarItem(this._controlButton); |
| 207 | |
| 208 | const gridHeader = container.createChild('div', 'animation-grid-header'); |
Tim van der Lippe | 91d3256 | 2020-02-13 15:08:47 | [diff] [blame] | 209 | UI.UIUtils.installDragHandle( |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 210 | gridHeader, this._repositionScrubber.bind(this), this._scrubberDragMove.bind(this), |
| 211 | this._scrubberDragEnd.bind(this), 'text'); |
| 212 | container.appendChild(this._createScrubber()); |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 213 | |
| 214 | if (this._timelineScrubberLine) { |
| 215 | UI.UIUtils.installDragHandle( |
| 216 | this._timelineScrubberLine, this._scrubberDragStart.bind(this), this._scrubberDragMove.bind(this), |
| 217 | this._scrubberDragEnd.bind(this), 'col-resize'); |
| 218 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 219 | this._currentTime.textContent = ''; |
| 220 | |
| 221 | return container; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * @param {!Event} event |
Kalon Hinds | 0fae2e7 | 2020-01-08 20:02:02 | [diff] [blame] | 226 | */ |
| 227 | _handlePlaybackRateControlKeyDown(event) { |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 228 | const keyboardEvent = /** @type {!KeyboardEvent} */ (event); |
| 229 | switch (keyboardEvent.key) { |
Kalon Hinds | 0fae2e7 | 2020-01-08 20:02:02 | [diff] [blame] | 230 | case 'ArrowLeft': |
| 231 | case 'ArrowUp': |
| 232 | this._focusNextPlaybackRateButton(event.target, /* focusPrevious */ true); |
| 233 | break; |
| 234 | case 'ArrowRight': |
| 235 | case 'ArrowDown': |
| 236 | this._focusNextPlaybackRateButton(event.target); |
| 237 | break; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * @param {!EventTarget|null} target |
| 243 | * @param {boolean=} focusPrevious |
| 244 | */ |
| 245 | _focusNextPlaybackRateButton(target, focusPrevious) { |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 246 | const button = /** @type {!HTMLElement} */ (target); |
| 247 | const currentIndex = this._playbackRateButtons.indexOf(button); |
Kalon Hinds | 0fae2e7 | 2020-01-08 20:02:02 | [diff] [blame] | 248 | const nextIndex = focusPrevious ? currentIndex - 1 : currentIndex + 1; |
| 249 | if (nextIndex < 0 || nextIndex >= this._playbackRateButtons.length) { |
| 250 | return; |
| 251 | } |
| 252 | const nextButton = this._playbackRateButtons[nextIndex]; |
| 253 | nextButton.tabIndex = 0; |
| 254 | nextButton.focus(); |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 255 | if (target) { |
| 256 | /** @type {!HTMLElement} */ (target).tabIndex = -1; |
| 257 | } |
Kalon Hinds | 0fae2e7 | 2020-01-08 20:02:02 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /** |
| 261 | * @param {!Event} event |
Tim van der Lippe | 49ff36b | 2020-10-08 12:11:00 | [diff] [blame] | 262 | * @return {?UI.PopoverHelper.PopoverRequest} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 263 | */ |
| 264 | _getPopoverRequest(event) { |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 265 | const element = /** @type {!HTMLElement} */ (event.target); |
| 266 | if (!element || !element.isDescendant(this._previewContainer)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 267 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 268 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 269 | |
| 270 | return { |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 271 | box: element.boxInWindow(), |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 272 | show: popover => { |
| 273 | let animGroup; |
Simon Zünd | f27be3d | 2020-02-11 14:46:27 | [diff] [blame] | 274 | for (const [group, previewUI] of this._previewMap) { |
| 275 | if (previewUI.element === element.parentElement) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 276 | animGroup = group; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 277 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 278 | } |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 279 | console.assert(typeof animGroup !== 'undefined'); |
Michael Liao | 64b9ee3 | 2020-05-01 17:18:29 | [diff] [blame] | 280 | if (!animGroup) { |
| 281 | return Promise.resolve(false); |
| 282 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 283 | const screenshots = animGroup.screenshots(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 284 | if (!screenshots.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 285 | return Promise.resolve(false); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 286 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 287 | |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 288 | /** @type {function(*):void} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 289 | let fulfill; |
Patrick Brosset | e65aaac | 2020-06-22 08:04:40 | [diff] [blame] | 290 | const promise = new Promise(x => { |
| 291 | fulfill = x; |
| 292 | }); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 293 | if (!screenshots[0].complete) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 294 | screenshots[0].onload = onFirstScreenshotLoaded.bind(null, screenshots); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 295 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 296 | onFirstScreenshotLoaded(screenshots); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 297 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 298 | return promise; |
| 299 | |
| 300 | /** |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 301 | * @param {!Array.<!HTMLImageElement>} screenshots |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 302 | */ |
| 303 | function onFirstScreenshotLoaded(screenshots) { |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 304 | new AnimationScreenshotPopover(screenshots).show(popover.contentElement); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 305 | fulfill(true); |
| 306 | } |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 307 | }, |
| 308 | hide: undefined |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 309 | }; |
| 310 | } |
| 311 | |
| 312 | _togglePauseAll() { |
| 313 | this._allPaused = !this._allPaused; |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 314 | if (this._pauseButton) { |
| 315 | this._pauseButton.setToggled(this._allPaused); |
| 316 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 317 | this._setPlaybackRate(this._playbackRate); |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 318 | if (this._pauseButton) { |
| 319 | this._pauseButton.setTitle(this._allPaused ? ls`Resume all` : ls`Pause all`); |
| 320 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /** |
| 324 | * @param {number} playbackRate |
| 325 | */ |
| 326 | _setPlaybackRate(playbackRate) { |
| 327 | this._playbackRate = playbackRate; |
Paul Lewis | daac106 | 2020-03-05 14:37:10 | [diff] [blame] | 328 | for (const animationModel of SDK.SDKModel.TargetManager.instance().models(AnimationModel)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 329 | animationModel.setPlaybackRate(this._allPaused ? 0 : this._playbackRate); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 330 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 331 | Host.userMetrics.actionTaken(Host.UserMetrics.Action.AnimationsPlaybackRateChanged); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 332 | if (this._scrubberPlayer) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 333 | this._scrubberPlayer.playbackRate = this._effectivePlaybackRate(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 334 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 335 | |
| 336 | this._updatePlaybackControls(); |
| 337 | } |
| 338 | |
| 339 | _updatePlaybackControls() { |
| 340 | for (const button of this._playbackRateButtons) { |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 341 | const selected = this._playbackRate === playbackRates.get(button); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 342 | button.classList.toggle('selected', selected); |
Kalon Hinds | 0fae2e7 | 2020-01-08 20:02:02 | [diff] [blame] | 343 | button.tabIndex = selected ? 0 : -1; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
| 347 | _controlButtonToggle() { |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 348 | if (this._controlState === _ControlState.Play) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 349 | this._togglePause(false); |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 350 | } else if (this._controlState === _ControlState.Replay) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 351 | this._replay(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 352 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 353 | this._togglePause(true); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 354 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | _updateControlButton() { |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 358 | if (!this._controlButton) { |
| 359 | return; |
| 360 | } |
| 361 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 362 | this._controlButton.setEnabled(!!this._selectedGroup); |
| 363 | if (this._selectedGroup && this._selectedGroup.paused()) { |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 364 | this._controlState = _ControlState.Play; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 365 | this._controlButton.setToggled(true); |
| 366 | this._controlButton.setTitle(ls`Play timeline`); |
| 367 | this._controlButton.setGlyph('largeicon-play-animation'); |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 368 | } else if ( |
| 369 | !this._scrubberPlayer || !this._scrubberPlayer.currentTime || |
| 370 | this._scrubberPlayer.currentTime >= this.duration()) { |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 371 | this._controlState = _ControlState.Replay; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 372 | this._controlButton.setToggled(true); |
| 373 | this._controlButton.setTitle(ls`Replay timeline`); |
| 374 | this._controlButton.setGlyph('largeicon-replay-animation'); |
| 375 | } else { |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 376 | this._controlState = _ControlState.Pause; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 377 | this._controlButton.setToggled(false); |
| 378 | this._controlButton.setTitle(ls`Pause timeline`); |
| 379 | this._controlButton.setGlyph('largeicon-pause-animation'); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * @return {number} |
| 385 | */ |
| 386 | _effectivePlaybackRate() { |
| 387 | return (this._allPaused || (this._selectedGroup && this._selectedGroup.paused())) ? 0 : this._playbackRate; |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * @param {boolean} pause |
| 392 | */ |
| 393 | _togglePause(pause) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 394 | if (this._scrubberPlayer) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 395 | this._scrubberPlayer.playbackRate = this._effectivePlaybackRate(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 396 | } |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 397 | if (this._selectedGroup) { |
| 398 | this._selectedGroup.togglePause(pause); |
| 399 | const preview = this._previewMap.get(this._selectedGroup); |
| 400 | if (preview) { |
| 401 | preview.element.classList.toggle('paused', pause); |
| 402 | } |
| 403 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 404 | this._updateControlButton(); |
| 405 | } |
| 406 | |
| 407 | _replay() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 408 | if (!this._selectedGroup) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 409 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 410 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 411 | this._selectedGroup.seekTo(0); |
| 412 | this._animateTime(0); |
| 413 | this._updateControlButton(); |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * @return {number} |
| 418 | */ |
| 419 | duration() { |
| 420 | return this._duration; |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * @param {number} duration |
| 425 | */ |
| 426 | setDuration(duration) { |
| 427 | this._duration = duration; |
| 428 | this.scheduleRedraw(); |
| 429 | } |
| 430 | |
| 431 | _clearTimeline() { |
| 432 | this._uiAnimations = []; |
| 433 | this._nodesMap.clear(); |
| 434 | this._animationsMap.clear(); |
| 435 | this._animationsContainer.removeChildren(); |
| 436 | this._duration = this._defaultDuration; |
| 437 | this._timelineScrubber.classList.add('hidden'); |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 438 | this._selectedGroup = null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 439 | if (this._scrubberPlayer) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 440 | this._scrubberPlayer.cancel(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 441 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 442 | delete this._scrubberPlayer; |
| 443 | this._currentTime.textContent = ''; |
| 444 | this._updateControlButton(); |
| 445 | } |
| 446 | |
| 447 | _reset() { |
| 448 | this._clearTimeline(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 449 | if (this._allPaused) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 450 | this._togglePauseAll(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 451 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 452 | this._setPlaybackRate(this._playbackRate); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 453 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 454 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 455 | for (const group of this._groupBuffer) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 456 | group.release(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 457 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 458 | this._groupBuffer = []; |
| 459 | this._previewMap.clear(); |
| 460 | this._previewContainer.removeChildren(); |
| 461 | this._popoverHelper.hidePopover(); |
| 462 | this._renderGrid(); |
| 463 | } |
| 464 | |
| 465 | /** |
Tim van der Lippe | c02a97c | 2020-02-14 14:39:27 | [diff] [blame] | 466 | * @param {!Common.EventTarget.EventTargetEvent} event |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 467 | */ |
| 468 | _animationGroupStarted(event) { |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 469 | this._addAnimationGroup(/** @type {!AnimationGroup} */ (event.data)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /** |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 473 | * @param {!AnimationGroup} group |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 474 | */ |
| 475 | _addAnimationGroup(group) { |
| 476 | /** |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 477 | * @param {!AnimationGroup} left |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 478 | * @param {!AnimationGroup} right |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 479 | */ |
| 480 | function startTimeComparator(left, right) { |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 481 | if (left.startTime() === right.startTime()) { |
| 482 | return 0; |
| 483 | } |
| 484 | return left.startTime() > right.startTime() ? 1 : -1; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 485 | } |
| 486 | |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 487 | const previewGroup = this._previewMap.get(group); |
| 488 | if (previewGroup) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 489 | if (this._selectedGroup === group) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 490 | this._syncScrubber(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 491 | } else { |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 492 | previewGroup.replay(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 493 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 494 | return; |
| 495 | } |
| 496 | this._groupBuffer.sort(startTimeComparator); |
| 497 | // Discard oldest groups from buffer if necessary |
| 498 | const groupsToDiscard = []; |
| 499 | const bufferSize = this.width() / 50; |
| 500 | while (this._groupBuffer.length > bufferSize) { |
| 501 | const toDiscard = this._groupBuffer.splice(this._groupBuffer[0] === this._selectedGroup ? 1 : 0, 1); |
| 502 | groupsToDiscard.push(toDiscard[0]); |
| 503 | } |
| 504 | for (const g of groupsToDiscard) { |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 505 | const discardGroup = this._previewMap.get(g); |
| 506 | if (!discardGroup) { |
| 507 | continue; |
| 508 | } |
| 509 | discardGroup.element.remove(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 510 | this._previewMap.delete(g); |
| 511 | g.release(); |
| 512 | } |
| 513 | // Generate preview |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 514 | const preview = new AnimationGroupPreviewUI(group); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 515 | this._groupBuffer.push(group); |
| 516 | this._previewMap.set(group, preview); |
| 517 | this._previewContainer.appendChild(preview.element); |
| 518 | preview.removeButton().addEventListener('click', this._removeAnimationGroup.bind(this, group)); |
| 519 | preview.element.addEventListener('click', this._selectAnimationGroup.bind(this, group)); |
Kalon Hinds | 0fae2e7 | 2020-01-08 20:02:02 | [diff] [blame] | 520 | preview.element.addEventListener('keydown', this._handleAnimationGroupKeyDown.bind(this, group)); |
Kalon Hinds | 08dd368 | 2020-01-10 23:51:25 | [diff] [blame] | 521 | UI.ARIAUtils.setAccessibleName(preview.element, ls`Animation Preview ${this._groupBuffer.indexOf(group) + 1}`); |
| 522 | UI.ARIAUtils.markAsOption(preview.element); |
| 523 | |
Kalon Hinds | 0fae2e7 | 2020-01-08 20:02:02 | [diff] [blame] | 524 | if (this._previewMap.size === 1) { |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 525 | const preview = this._previewMap.get(this._groupBuffer[0]); |
| 526 | if (preview) { |
| 527 | preview.element.tabIndex = 0; |
| 528 | } |
Kalon Hinds | 0fae2e7 | 2020-01-08 20:02:02 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
| 532 | /** |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 533 | * @param {!AnimationGroup} group |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 534 | * @param {!KeyboardEvent} event |
Kalon Hinds | 0fae2e7 | 2020-01-08 20:02:02 | [diff] [blame] | 535 | */ |
| 536 | _handleAnimationGroupKeyDown(group, event) { |
| 537 | switch (event.key) { |
| 538 | case ' ': |
| 539 | case 'Enter': |
| 540 | this._selectAnimationGroup(group); |
| 541 | break; |
| 542 | case 'Backspace': |
| 543 | case 'Delete': |
| 544 | this._removeAnimationGroup(group, event); |
| 545 | break; |
| 546 | case 'ArrowLeft': |
| 547 | case 'ArrowUp': |
| 548 | this._focusNextGroup(group, /* target */ event.target, /* focusPrevious */ true); |
| 549 | break; |
| 550 | case 'ArrowRight': |
| 551 | case 'ArrowDown': |
| 552 | this._focusNextGroup(group, /* target */ event.target); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | /** |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 557 | * @param {!AnimationGroup} group |
Kalon Hinds | 0fae2e7 | 2020-01-08 20:02:02 | [diff] [blame] | 558 | * @param {!EventTarget|null} target |
| 559 | * @param {boolean=} focusPrevious |
| 560 | */ |
| 561 | _focusNextGroup(group, target, focusPrevious) { |
| 562 | const currentGroupIndex = this._groupBuffer.indexOf(group); |
| 563 | const nextIndex = focusPrevious ? currentGroupIndex - 1 : currentGroupIndex + 1; |
| 564 | if (nextIndex < 0 || nextIndex >= this._groupBuffer.length) { |
| 565 | return; |
| 566 | } |
| 567 | const preview = this._previewMap.get(this._groupBuffer[nextIndex]); |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 568 | if (preview) { |
| 569 | preview.element.tabIndex = 0; |
| 570 | preview.element.focus(); |
| 571 | } |
| 572 | |
| 573 | if (target) { |
| 574 | /** @type {!HTMLElement} */ (target).tabIndex = -1; |
| 575 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /** |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 579 | * @param {!AnimationGroup} group |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 580 | * @param {!Event} event |
| 581 | */ |
| 582 | _removeAnimationGroup(group, event) { |
Kalon Hinds | 0fae2e7 | 2020-01-08 20:02:02 | [diff] [blame] | 583 | const currentGroupIndex = this._groupBuffer.indexOf(group); |
| 584 | |
Simon Zünd | 684ebae | 2020-03-09 11:44:16 | [diff] [blame] | 585 | Platform.ArrayUtilities.removeElement(this._groupBuffer, group); |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 586 | const previewGroup = this._previewMap.get(group); |
| 587 | if (previewGroup) { |
| 588 | previewGroup.element.remove(); |
| 589 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 590 | this._previewMap.delete(group); |
| 591 | group.release(); |
| 592 | event.consume(true); |
| 593 | |
| 594 | if (this._selectedGroup === group) { |
| 595 | this._clearTimeline(); |
| 596 | this._renderGrid(); |
| 597 | } |
Kalon Hinds | 0fae2e7 | 2020-01-08 20:02:02 | [diff] [blame] | 598 | |
| 599 | const groupLength = this._groupBuffer.length; |
| 600 | if (groupLength === 0) { |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 601 | /** @type {!HTMLElement} */ (this._clearButton.element).focus(); |
Kalon Hinds | 0fae2e7 | 2020-01-08 20:02:02 | [diff] [blame] | 602 | return; |
| 603 | } |
| 604 | const nextGroup = currentGroupIndex >= this._groupBuffer.length ? |
| 605 | this._previewMap.get(this._groupBuffer[this._groupBuffer.length - 1]) : |
| 606 | this._previewMap.get(this._groupBuffer[currentGroupIndex]); |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 607 | |
| 608 | if (nextGroup) { |
| 609 | nextGroup.element.tabIndex = 0; |
| 610 | nextGroup.element.focus(); |
| 611 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | /** |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 615 | * @param {!AnimationGroup} group |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 616 | */ |
| 617 | _selectAnimationGroup(group) { |
| 618 | /** |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 619 | * @param {!AnimationGroupPreviewUI} ui |
| 620 | * @param {!AnimationGroup} group |
| 621 | * @this {!AnimationTimeline} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 622 | */ |
| 623 | function applySelectionClass(ui, group) { |
| 624 | ui.element.classList.toggle('selected', this._selectedGroup === group); |
| 625 | } |
| 626 | |
| 627 | if (this._selectedGroup === group) { |
| 628 | this._togglePause(false); |
| 629 | this._replay(); |
| 630 | return; |
| 631 | } |
| 632 | this._clearTimeline(); |
| 633 | this._selectedGroup = group; |
| 634 | this._previewMap.forEach(applySelectionClass, this); |
| 635 | this.setDuration(Math.max(500, group.finiteDuration() + 100)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 636 | for (const anim of group.animations()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 637 | this._addAnimation(anim); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 638 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 639 | this.scheduleRedraw(); |
| 640 | this._timelineScrubber.classList.remove('hidden'); |
| 641 | this._togglePause(false); |
| 642 | this._replay(); |
| 643 | } |
| 644 | |
| 645 | /** |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 646 | * @param {!AnimationImpl} animation |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 647 | */ |
| 648 | _addAnimation(animation) { |
| 649 | /** |
Tim van der Lippe | 91d3256 | 2020-02-13 15:08:47 | [diff] [blame] | 650 | * @param {?SDK.DOMModel.DOMNode} node |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 651 | * @this {AnimationTimeline} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 652 | */ |
| 653 | function nodeResolved(node) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 654 | uiAnimation.setNode(node); |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 655 | if (node && nodeUI) { |
| 656 | nodeUI.nodeResolved(node); |
| 657 | nodeUIsByNode.set(node, nodeUI); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 658 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | let nodeUI = this._nodesMap.get(animation.source().backendNodeId()); |
| 662 | if (!nodeUI) { |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 663 | nodeUI = new NodeUI(animation.source()); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 664 | this._animationsContainer.appendChild(nodeUI.element); |
| 665 | this._nodesMap.set(animation.source().backendNodeId(), nodeUI); |
| 666 | } |
| 667 | const nodeRow = nodeUI.createNewRow(); |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 668 | const uiAnimation = new AnimationUI(animation, this, nodeRow); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 669 | animation.source().deferredNode().resolve(nodeResolved.bind(this)); |
| 670 | this._uiAnimations.push(uiAnimation); |
| 671 | this._animationsMap.set(animation.id(), animation); |
| 672 | } |
| 673 | |
| 674 | /** |
Tim van der Lippe | c02a97c | 2020-02-14 14:39:27 | [diff] [blame] | 675 | * @param {!Common.EventTarget.EventTargetEvent} event |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 676 | */ |
| 677 | _nodeRemoved(event) { |
| 678 | const node = event.data.node; |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 679 | const nodeUI = nodeUIsByNode.get(node); |
| 680 | if (nodeUI) { |
| 681 | nodeUI.nodeRemoved(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 682 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | _renderGrid() { |
| 686 | /** @const */ const gridSize = 250; |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 687 | this._grid.setAttribute('width', (this.width() + 10).toString()); |
| 688 | this._grid.setAttribute('height', ((this._cachedTimelineHeight || 0) + 30).toString()); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 689 | this._grid.setAttribute('shape-rendering', 'crispEdges'); |
| 690 | this._grid.removeChildren(); |
| 691 | let lastDraw = undefined; |
| 692 | for (let time = 0; time < this.duration(); time += gridSize) { |
Paul Lewis | 44e3757 | 2020-10-28 15:57:26 | [diff] [blame] | 693 | const line = UI.UIUtils.createSVGChild(this._grid, 'rect', 'animation-timeline-grid-line'); |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 694 | line.setAttribute('x', (time * this.pixelMsRatio() + 10).toString()); |
| 695 | line.setAttribute('y', '23'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 696 | line.setAttribute('height', '100%'); |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 697 | line.setAttribute('width', '1'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 698 | } |
| 699 | for (let time = 0; time < this.duration(); time += gridSize) { |
| 700 | const gridWidth = time * this.pixelMsRatio(); |
| 701 | if (lastDraw === undefined || gridWidth - lastDraw > 50) { |
| 702 | lastDraw = gridWidth; |
Paul Lewis | 44e3757 | 2020-10-28 15:57:26 | [diff] [blame] | 703 | const label = UI.UIUtils.createSVGChild(this._grid, 'text', 'animation-timeline-grid-label'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 704 | label.textContent = Number.millisToString(time); |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 705 | label.setAttribute('x', (gridWidth + 10).toString()); |
| 706 | label.setAttribute('y', '16'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | scheduleRedraw() { |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 712 | /** @type {!Array<!AnimationUI>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 713 | this._renderQueue = []; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 714 | for (const ui of this._uiAnimations) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 715 | this._renderQueue.push(ui); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 716 | } |
| 717 | if (this._redrawing) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 718 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 719 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 720 | this._redrawing = true; |
| 721 | this._renderGrid(); |
| 722 | this._animationsContainer.window().requestAnimationFrame(this._render.bind(this)); |
| 723 | } |
| 724 | |
| 725 | /** |
| 726 | * @param {number=} timestamp |
| 727 | */ |
| 728 | _render(timestamp) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 729 | while (this._renderQueue.length && (!timestamp || window.performance.now() - timestamp < 50)) { |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 730 | const animationUI = this._renderQueue.shift(); |
| 731 | if (animationUI) { |
| 732 | animationUI.redraw(); |
| 733 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 734 | } |
| 735 | if (this._renderQueue.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 736 | this._animationsContainer.window().requestAnimationFrame(this._render.bind(this)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 737 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 738 | delete this._redrawing; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 739 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | /** |
| 743 | * @override |
| 744 | */ |
| 745 | onResize() { |
| 746 | this._cachedTimelineWidth = Math.max(0, this._animationsContainer.offsetWidth - this._timelineControlsWidth) || 0; |
| 747 | this._cachedTimelineHeight = this._animationsContainer.offsetHeight; |
| 748 | this.scheduleRedraw(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 749 | if (this._scrubberPlayer) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 750 | this._syncScrubber(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 751 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 752 | delete this._gridOffsetLeft; |
| 753 | } |
| 754 | |
| 755 | /** |
| 756 | * @return {number} |
| 757 | */ |
| 758 | width() { |
| 759 | return this._cachedTimelineWidth || 0; |
| 760 | } |
| 761 | |
| 762 | /** |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 763 | * @param {!AnimationImpl} animation |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 764 | * @return {boolean} |
| 765 | */ |
| 766 | _resizeWindow(animation) { |
| 767 | let resized = false; |
| 768 | |
| 769 | // This shows at most 3 iterations |
| 770 | const duration = animation.source().duration() * Math.min(2, animation.source().iterations()); |
| 771 | const requiredDuration = animation.source().delay() + duration + animation.source().endDelay(); |
| 772 | if (requiredDuration > this._duration) { |
| 773 | resized = true; |
| 774 | this._duration = requiredDuration + 200; |
| 775 | } |
| 776 | return resized; |
| 777 | } |
| 778 | |
| 779 | _syncScrubber() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 780 | if (!this._selectedGroup) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 781 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 782 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 783 | this._selectedGroup.currentTimePromise() |
| 784 | .then(this._animateTime.bind(this)) |
| 785 | .then(this._updateControlButton.bind(this)); |
| 786 | } |
| 787 | |
| 788 | /** |
| 789 | * @param {number} currentTime |
| 790 | */ |
| 791 | _animateTime(currentTime) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 792 | if (this._scrubberPlayer) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 793 | this._scrubberPlayer.cancel(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 794 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 795 | |
| 796 | this._scrubberPlayer = this._timelineScrubber.animate( |
| 797 | [{transform: 'translateX(0px)'}, {transform: 'translateX(' + this.width() + 'px)'}], |
| 798 | {duration: this.duration(), fill: 'forwards'}); |
| 799 | this._scrubberPlayer.playbackRate = this._effectivePlaybackRate(); |
| 800 | this._scrubberPlayer.onfinish = this._updateControlButton.bind(this); |
| 801 | this._scrubberPlayer.currentTime = currentTime; |
| 802 | this.element.window().requestAnimationFrame(this._updateScrubber.bind(this)); |
| 803 | } |
| 804 | |
| 805 | /** |
| 806 | * @return {number} |
| 807 | */ |
| 808 | pixelMsRatio() { |
| 809 | return this.width() / this.duration() || 0; |
| 810 | } |
| 811 | |
| 812 | /** |
| 813 | * @param {number} timestamp |
| 814 | */ |
| 815 | _updateScrubber(timestamp) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 816 | if (!this._scrubberPlayer) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 817 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 818 | } |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 819 | this._currentTime.textContent = Number.millisToString(this._scrubberPlayer.currentTime || 0); |
| 820 | if (this._scrubberPlayer.playState.toString() === 'pending' || this._scrubberPlayer.playState === 'running') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 821 | this.element.window().requestAnimationFrame(this._updateScrubber.bind(this)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 822 | } else if (this._scrubberPlayer.playState === 'finished') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 823 | this._currentTime.textContent = ''; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 824 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | /** |
| 828 | * @param {!Event} event |
| 829 | * @return {boolean} |
| 830 | */ |
| 831 | _repositionScrubber(event) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 832 | if (!this._selectedGroup) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 833 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 834 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 835 | |
| 836 | // Seek to current mouse position. |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 837 | if (!this._gridOffsetLeft) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 838 | this._gridOffsetLeft = this._grid.totalOffsetLeft() + 10; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 839 | } |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 840 | const {x} = /** @type {*} */ (event); |
| 841 | const seekTime = Math.max(0, x - this._gridOffsetLeft) / this.pixelMsRatio(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 842 | this._selectedGroup.seekTo(seekTime); |
| 843 | this._togglePause(true); |
| 844 | this._animateTime(seekTime); |
| 845 | |
| 846 | // Interface with scrubber drag. |
| 847 | this._originalScrubberTime = seekTime; |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 848 | this._originalMousePosition = x; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 849 | return true; |
| 850 | } |
| 851 | |
| 852 | /** |
| 853 | * @param {!Event} event |
| 854 | * @return {boolean} |
| 855 | */ |
| 856 | _scrubberDragStart(event) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 857 | if (!this._scrubberPlayer || !this._selectedGroup) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 858 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 859 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 860 | |
| 861 | this._originalScrubberTime = this._scrubberPlayer.currentTime; |
| 862 | this._timelineScrubber.classList.remove('animation-timeline-end'); |
| 863 | this._scrubberPlayer.pause(); |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 864 | const {x} = /** @type {*} */ (event); |
| 865 | this._originalMousePosition = x; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 866 | |
| 867 | this._togglePause(true); |
| 868 | return true; |
| 869 | } |
| 870 | |
| 871 | /** |
| 872 | * @param {!Event} event |
| 873 | */ |
| 874 | _scrubberDragMove(event) { |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 875 | const {x} = /** @type {*} */ (event); |
| 876 | const delta = x - this._originalMousePosition; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 877 | const currentTime = |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 878 | Math.max(0, Math.min((this._originalScrubberTime || 0) + delta / this.pixelMsRatio(), this.duration())); |
| 879 | if (this._scrubberPlayer) { |
| 880 | this._scrubberPlayer.currentTime = currentTime; |
| 881 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 882 | this._currentTime.textContent = Number.millisToString(Math.round(currentTime)); |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 883 | |
| 884 | if (this._selectedGroup) { |
| 885 | this._selectedGroup.seekTo(currentTime); |
| 886 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | /** |
| 890 | * @param {!Event} event |
| 891 | */ |
| 892 | _scrubberDragEnd(event) { |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 893 | if (this._scrubberPlayer) { |
| 894 | const currentTime = Math.max(0, this._scrubberPlayer.currentTime || 0); |
| 895 | this._scrubberPlayer.play(); |
| 896 | this._scrubberPlayer.currentTime = currentTime; |
| 897 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 898 | this._currentTime.window().requestAnimationFrame(this._updateScrubber.bind(this)); |
| 899 | } |
Paul Lewis | 4962e2a | 2019-11-19 14:20:16 | [diff] [blame] | 900 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 901 | |
Paul Lewis | 4962e2a | 2019-11-19 14:20:16 | [diff] [blame] | 902 | export const GlobalPlaybackRates = [1, 0.25, 0.1]; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 903 | |
| 904 | /** @enum {string} */ |
Paul Lewis | 4962e2a | 2019-11-19 14:20:16 | [diff] [blame] | 905 | export const _ControlState = { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 906 | Play: 'play-outline', |
| 907 | Replay: 'replay-outline', |
| 908 | Pause: 'pause-outline' |
| 909 | }; |
| 910 | |
| 911 | /** |
| 912 | * @unrestricted |
| 913 | */ |
Paul Lewis | 4962e2a | 2019-11-19 14:20:16 | [diff] [blame] | 914 | export class NodeUI { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 915 | /** |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 916 | * @param {!AnimationEffect} animationEffect |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 917 | */ |
| 918 | constructor(animationEffect) { |
Tim van der Lippe | f49e232 | 2020-05-01 15:03:09 | [diff] [blame] | 919 | this.element = document.createElement('div'); |
| 920 | this.element.classList.add('animation-node-row'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 921 | this._description = this.element.createChild('div', 'animation-node-description'); |
Brandon Goddard | 99420bb | 2019-12-17 23:30:29 | [diff] [blame] | 922 | this._description.tabIndex = 0; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 923 | this._timelineElement = this.element.createChild('div', 'animation-node-timeline'); |
Kalon Hinds | 08dd368 | 2020-01-10 23:51:25 | [diff] [blame] | 924 | UI.ARIAUtils.markAsApplication(this._timelineElement); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | /** |
Tim van der Lippe | 91d3256 | 2020-02-13 15:08:47 | [diff] [blame] | 928 | * @param {?SDK.DOMModel.DOMNode} node |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 929 | */ |
Jeff Fisher | 3f5f19c | 2019-08-28 19:10:02 | [diff] [blame] | 930 | nodeResolved(node) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 931 | if (!node) { |
Sigurd Schneider | 23c5297 | 2020-10-13 09:31:14 | [diff] [blame] | 932 | UI.UIUtils.createTextChild(this._description, '<node>'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 933 | return; |
| 934 | } |
| 935 | this._node = node; |
| 936 | this._nodeChanged(); |
Brandon Goddard | 74711eb | 2020-10-22 15:17:31 | [diff] [blame] | 937 | Common.Linkifier.Linkifier.linkify(node).then(link => this._description.appendChild(link)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 938 | if (!node.ownerDocument) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 939 | this.nodeRemoved(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 940 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | /** |
| 944 | * @return {!Element} |
| 945 | */ |
| 946 | createNewRow() { |
| 947 | return this._timelineElement.createChild('div', 'animation-timeline-row'); |
| 948 | } |
| 949 | |
| 950 | nodeRemoved() { |
| 951 | this.element.classList.add('animation-node-removed'); |
| 952 | this._node = null; |
| 953 | } |
| 954 | |
| 955 | _nodeChanged() { |
Paul Lewis | eae437e | 2020-10-19 14:09:45 | [diff] [blame] | 956 | let animationNodeSelected = false; |
| 957 | if (this._node) { |
| 958 | animationNodeSelected = (this._node === UI.Context.Context.instance().flavor(SDK.DOMModel.DOMNode)); |
| 959 | } |
| 960 | |
| 961 | this.element.classList.toggle('animation-node-selected', animationNodeSelected); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 962 | } |
Paul Lewis | 4962e2a | 2019-11-19 14:20:16 | [diff] [blame] | 963 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 964 | |
| 965 | /** |
| 966 | * @unrestricted |
| 967 | */ |
Paul Lewis | 4962e2a | 2019-11-19 14:20:16 | [diff] [blame] | 968 | export class StepTimingFunction { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 969 | /** |
| 970 | * @param {number} steps |
| 971 | * @param {string} stepAtPosition |
| 972 | */ |
| 973 | constructor(steps, stepAtPosition) { |
| 974 | this.steps = steps; |
| 975 | this.stepAtPosition = stepAtPosition; |
| 976 | } |
| 977 | |
| 978 | /** |
| 979 | * @param {string} text |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 980 | * @return {?StepTimingFunction} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 981 | */ |
| 982 | static parse(text) { |
| 983 | let match = text.match(/^steps\((\d+), (start|middle)\)$/); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 984 | if (match) { |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 985 | return new StepTimingFunction(parseInt(match[1], 10), match[2]); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 986 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 987 | match = text.match(/^steps\((\d+)\)$/); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 988 | if (match) { |
Paul Lewis | 752031f | 2020-01-09 14:14:23 | [diff] [blame] | 989 | return new StepTimingFunction(parseInt(match[1], 10), 'end'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 990 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 991 | return null; |
| 992 | } |
Paul Lewis | 4962e2a | 2019-11-19 14:20:16 | [diff] [blame] | 993 | } |