Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | // Copyright 2014 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 | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 4 | |
Paul Lewis | 17e384e | 2020-01-08 15:46:51 | [diff] [blame] | 5 | import * as Common from '../common/common.js'; |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 6 | import {elementDragStart} from './UIUtils.js'; |
| 7 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 8 | /** |
| 9 | * @unrestricted |
| 10 | */ |
Paul Lewis | 17e384e | 2020-01-08 15:46:51 | [diff] [blame] | 11 | export class ResizerWidget extends Common.ObjectWrapper.ObjectWrapper { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 12 | constructor() { |
| 13 | super(); |
| 14 | |
| 15 | this._isEnabled = true; |
Simon Zünd | 5bbcd5c | 2020-03-02 09:09:21 | [diff] [blame^] | 16 | /** @type {!Set<!Element>} */ |
| 17 | this._elements = new Set(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 18 | this._installDragOnMouseDownBound = this._installDragOnMouseDown.bind(this); |
| 19 | this._cursor = 'nwse-resize'; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * @return {boolean} |
| 24 | */ |
| 25 | isEnabled() { |
| 26 | return this._isEnabled; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @param {boolean} enabled |
| 31 | */ |
| 32 | setEnabled(enabled) { |
| 33 | this._isEnabled = enabled; |
| 34 | this.updateElementCursors(); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @return {!Array.<!Element>} |
| 39 | */ |
| 40 | elements() { |
Simon Zünd | 5bbcd5c | 2020-03-02 09:09:21 | [diff] [blame^] | 41 | return [...this._elements]; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @param {!Element} element |
| 46 | */ |
| 47 | addElement(element) { |
Simon Zünd | 5bbcd5c | 2020-03-02 09:09:21 | [diff] [blame^] | 48 | if (!this._elements.has(element)) { |
| 49 | this._elements.add(element); |
| 50 | element.addEventListener('mousedown', this._installDragOnMouseDownBound, false); |
| 51 | this._updateElementCursor(element); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 52 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @param {!Element} element |
| 57 | */ |
| 58 | removeElement(element) { |
Simon Zünd | 5bbcd5c | 2020-03-02 09:09:21 | [diff] [blame^] | 59 | if (this._elements.has(element)) { |
| 60 | this._elements.delete(element); |
| 61 | element.removeEventListener('mousedown', this._installDragOnMouseDownBound, false); |
| 62 | element.style.removeProperty('cursor'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 63 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | updateElementCursors() { |
| 67 | this._elements.forEach(this._updateElementCursor.bind(this)); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param {!Element} element |
| 72 | */ |
| 73 | _updateElementCursor(element) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 74 | if (this._isEnabled) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 75 | element.style.setProperty('cursor', this.cursor()); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 76 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 77 | element.style.removeProperty('cursor'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 78 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @return {string} |
| 83 | */ |
| 84 | cursor() { |
| 85 | return this._cursor; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @param {string} cursor |
| 90 | */ |
| 91 | setCursor(cursor) { |
| 92 | this._cursor = cursor; |
| 93 | this.updateElementCursors(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @param {!Event} event |
| 98 | */ |
| 99 | _installDragOnMouseDown(event) { |
Simon Zünd | 5bbcd5c | 2020-03-02 09:09:21 | [diff] [blame^] | 100 | const element = /** @type {!Element} */ (event.target); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 101 | // Only handle drags of the nodes specified. |
Simon Zünd | 5bbcd5c | 2020-03-02 09:09:21 | [diff] [blame^] | 102 | if (!this._elements.has(element)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 103 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 104 | } |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 105 | elementDragStart( |
Simon Zünd | 5bbcd5c | 2020-03-02 09:09:21 | [diff] [blame^] | 106 | element, this._dragStart.bind(this), this._drag.bind(this), this._dragEnd.bind(this), this.cursor(), event); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @param {!MouseEvent} event |
| 111 | * @return {boolean} |
| 112 | */ |
| 113 | _dragStart(event) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 114 | if (!this._isEnabled) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 115 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 116 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 117 | this._startX = event.pageX; |
| 118 | this._startY = event.pageY; |
| 119 | this.sendDragStart(this._startX, this._startY); |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @param {number} x |
| 125 | * @param {number} y |
| 126 | */ |
| 127 | sendDragStart(x, y) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 128 | this.dispatchEventToListeners(Events.ResizeStart, {startX: x, currentX: x, startY: y, currentY: y}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @param {!MouseEvent} event |
| 133 | * @return {boolean} |
| 134 | */ |
| 135 | _drag(event) { |
| 136 | if (!this._isEnabled) { |
| 137 | this._dragEnd(event); |
| 138 | return true; // Cancel drag. |
| 139 | } |
| 140 | |
| 141 | this.sendDragMove(this._startX, event.pageX, this._startY, event.pageY, event.shiftKey); |
| 142 | event.preventDefault(); |
| 143 | return false; // Continue drag. |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * @param {number} startX |
| 148 | * @param {number} currentX |
| 149 | * @param {number} startY |
| 150 | * @param {number} currentY |
| 151 | * @param {boolean} shiftKey |
| 152 | */ |
| 153 | sendDragMove(startX, currentX, startY, currentY, shiftKey) { |
| 154 | this.dispatchEventToListeners( |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 155 | Events.ResizeUpdate, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 156 | {startX: startX, currentX: currentX, startY: startY, currentY: currentY, shiftKey: shiftKey}); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @param {!MouseEvent} event |
| 161 | */ |
| 162 | _dragEnd(event) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 163 | this.dispatchEventToListeners(Events.ResizeEnd); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 164 | delete this._startX; |
| 165 | delete this._startY; |
| 166 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 167 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 168 | |
| 169 | /** @enum {symbol} */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 170 | export const Events = { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 171 | ResizeStart: Symbol('ResizeStart'), |
| 172 | ResizeUpdate: Symbol('ResizeUpdate'), |
| 173 | ResizeEnd: Symbol('ResizeEnd') |
| 174 | }; |
| 175 | |
| 176 | /** |
| 177 | * @unrestricted |
| 178 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 179 | export class SimpleResizerWidget extends ResizerWidget { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 180 | constructor() { |
| 181 | super(); |
| 182 | this._isVertical = true; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * @return {boolean} |
| 187 | */ |
| 188 | isVertical() { |
| 189 | return this._isVertical; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Vertical widget resizes height (along y-axis). |
| 194 | * @param {boolean} vertical |
| 195 | */ |
| 196 | setVertical(vertical) { |
| 197 | this._isVertical = vertical; |
| 198 | this.updateElementCursors(); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * @override |
| 203 | * @return {string} |
| 204 | */ |
| 205 | cursor() { |
| 206 | return this._isVertical ? 'ns-resize' : 'ew-resize'; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * @override |
| 211 | * @param {number} x |
| 212 | * @param {number} y |
| 213 | */ |
| 214 | sendDragStart(x, y) { |
| 215 | const position = this._isVertical ? y : x; |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 216 | this.dispatchEventToListeners(Events.ResizeStart, {startPosition: position, currentPosition: position}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /** |
| 220 | * @override |
| 221 | * @param {number} startX |
| 222 | * @param {number} currentX |
| 223 | * @param {number} startY |
| 224 | * @param {number} currentY |
| 225 | * @param {boolean} shiftKey |
| 226 | */ |
| 227 | sendDragMove(startX, currentX, startY, currentY, shiftKey) { |
| 228 | if (this._isVertical) { |
| 229 | this.dispatchEventToListeners( |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 230 | Events.ResizeUpdate, {startPosition: startY, currentPosition: currentY, shiftKey: shiftKey}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 231 | } else { |
| 232 | this.dispatchEventToListeners( |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 233 | Events.ResizeUpdate, {startPosition: startX, currentPosition: currentX, shiftKey: shiftKey}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 234 | } |
| 235 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 236 | } |