Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(169)

Side by Side Diff: ui/file_manager/gallery/js/image_editor/image_transform.js

Issue 2727353004: Compile Image Editor in gyp v2 (Closed)
Patch Set: . Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 4
5 /** 5 /**
6 * Crop mode. 6 * Crop mode.
7 * 7 *
8 * @extends {ImageEditor.Mode} 8 * @extends {ImageEditorMode}
9 * @constructor 9 * @constructor
10 * @struct 10 * @struct
11 */ 11 */
12 ImageEditor.Mode.Crop = function() { 12 ImageEditorMode.Crop = function() {
13 ImageEditor.Mode.call(this, 'crop', 'GALLERY_CROP'); 13 ImageEditorMode.call(this, 'crop', 'GALLERY_CROP');
14 14
15 this.paddingTop = ImageEditor.Mode.Crop.MOUSE_GRAB_RADIUS; 15 this.paddingTop = ImageEditorMode.Crop.MOUSE_GRAB_RADIUS;
16 this.paddingBottom = ImageEditor.Mode.Crop.MOUSE_GRAB_RADIUS; 16 this.paddingBottom = ImageEditorMode.Crop.MOUSE_GRAB_RADIUS;
17 17
18 /** 18 /**
19 * @type {HTMLElement} 19 * @type {HTMLElement}
20 * @private 20 * @private
21 */ 21 */
22 this.domOverlay_ = null; 22 this.domOverlay_ = null;
23 23
24 /** 24 /**
25 * @type {HTMLElement} 25 * @type {HTMLElement}
26 * @private 26 * @private
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 */ 63 */
64 this.onViewportResizedBound_ = null; 64 this.onViewportResizedBound_ = null;
65 65
66 /** 66 /**
67 * @type {DraggableRect} 67 * @type {DraggableRect}
68 * @private 68 * @private
69 */ 69 */
70 this.cropRect_ = null; 70 this.cropRect_ = null;
71 }; 71 };
72 72
73 ImageEditor.Mode.Crop.prototype = {__proto__: ImageEditor.Mode.prototype}; 73 ImageEditorMode.Crop.prototype = {
74 __proto__: ImageEditorMode.prototype
75 };
74 76
75 /** 77 /**
76 * Sets the mode up. 78 * Sets the mode up.
77 * @override 79 * @override
78 */ 80 */
79 ImageEditor.Mode.Crop.prototype.setUp = function() { 81 ImageEditorMode.Crop.prototype.setUp = function() {
80 ImageEditor.Mode.prototype.setUp.apply(this, arguments); 82 ImageEditorMode.prototype.setUp.apply(this, arguments);
81 83
82 var container = this.getImageView().container_; 84 var container = this.getImageView().container_;
83 var doc = container.ownerDocument; 85 var doc = container.ownerDocument;
84 86
85 this.domOverlay_ = /** @type {!HTMLElement} */ (doc.createElement('div')); 87 this.domOverlay_ = /** @type {!HTMLElement} */ (doc.createElement('div'));
86 this.domOverlay_.className = 'crop-overlay'; 88 this.domOverlay_.className = 'crop-overlay';
87 container.appendChild(this.domOverlay_); 89 container.appendChild(this.domOverlay_);
88 90
89 this.shadowTop_ = /** @type {!HTMLElement} */ (doc.createElement('div')); 91 this.shadowTop_ = /** @type {!HTMLElement} */ (doc.createElement('div'));
90 this.shadowTop_.className = 'shadow'; 92 this.shadowTop_.className = 'shadow';
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 130
129 this.onViewportResizedBound_ = this.onViewportResized_.bind(this); 131 this.onViewportResizedBound_ = this.onViewportResized_.bind(this);
130 this.getViewport().addEventListener('resize', this.onViewportResizedBound_); 132 this.getViewport().addEventListener('resize', this.onViewportResizedBound_);
131 133
132 this.createDefaultCrop(); 134 this.createDefaultCrop();
133 }; 135 };
134 136
135 /** 137 /**
136 * @override 138 * @override
137 */ 139 */
138 ImageEditor.Mode.Crop.prototype.createTools = function(toolbar) { 140 ImageEditorMode.Crop.prototype.createTools = function(toolbar) {
139 var aspects = { 141 var aspects = {
140 GALLERY_ASPECT_RATIO_1_1: 1 / 1, 142 GALLERY_ASPECT_RATIO_1_1: 1 / 1,
141 GALLERY_ASPECT_RATIO_6_4: 6 / 4, 143 GALLERY_ASPECT_RATIO_6_4: 6 / 4,
142 GALLERY_ASPECT_RATIO_7_5: 7 / 5, 144 GALLERY_ASPECT_RATIO_7_5: 7 / 5,
143 GALLERY_ASPECT_RATIO_16_9: 16 / 9 145 GALLERY_ASPECT_RATIO_16_9: 16 / 9
144 }; 146 };
145 147
146 // TODO(fukino): The loop order is not guaranteed. Fix it! 148 // TODO(fukino): The loop order is not guaranteed. Fix it!
147 for (var name in aspects) { 149 for (var name in aspects) {
148 var button = toolbar.addButton( 150 var button = toolbar.addButton(
149 name, 151 name, ImageEditorToolbar.ButtonType.LABEL,
150 ImageEditor.Toolbar.ButtonType.LABEL,
151 this.onCropAspectRatioClicked_.bind(this, toolbar, aspects[name]), 152 this.onCropAspectRatioClicked_.bind(this, toolbar, aspects[name]),
152 'crop-aspect-ratio'); 153 'crop-aspect-ratio');
153 154
154 // Prevent from cropping by Enter key if the button is focused. 155 // Prevent from cropping by Enter key if the button is focused.
155 button.addEventListener('keydown', function(event) { 156 button.addEventListener('keydown', function(event) {
156 var key = util.getKeyModifiers(event) + event.key; 157 var key = util.getKeyModifiers(event) + event.key;
157 if (key === 'Enter') 158 if (key === 'Enter')
158 event.stopPropagation(); 159 event.stopPropagation();
159 }); 160 });
160 } 161 }
161 }; 162 };
162 163
163 /** 164 /**
164 * Handles click events of crop aspect ratio buttons. 165 * Handles click events of crop aspect ratio buttons.
165 * @param {!ImageEditor.Toolbar} toolbar Toolbar. 166 * @param {!ImageEditorToolbar} toolbar Toolbar.
166 * @param {number} aspect Aspect ratio. 167 * @param {number} aspect Aspect ratio.
167 * @param {Event} event An event. 168 * @param {Event} event An event.
168 * @private 169 * @private
169 */ 170 */
170 ImageEditor.Mode.Crop.prototype.onCropAspectRatioClicked_ = function( 171 ImageEditorMode.Crop.prototype.onCropAspectRatioClicked_ = function(
171 toolbar, aspect, event) { 172 toolbar, aspect, event) {
172 var button = event.target; 173 var button = event.target;
173 174
174 if (button.classList.contains('selected')) { 175 if (button.classList.contains('selected')) {
175 button.classList.remove('selected'); 176 button.classList.remove('selected');
176 this.cropRect_.fixedAspectRatio = null; 177 this.cropRect_.fixedAspectRatio = null;
177 } else { 178 } else {
178 var selectedButtons = 179 var selectedButtons =
179 toolbar.getElement().querySelectorAll('button.selected'); 180 toolbar.getElement().querySelectorAll('button.selected');
180 for (var i = 0; i < selectedButtons.length; i++) { 181 for (var i = 0; i < selectedButtons.length; i++) {
181 selectedButtons[i].classList.remove('selected'); 182 selectedButtons[i].classList.remove('selected');
182 } 183 }
183 button.classList.add('selected'); 184 button.classList.add('selected');
184 var clipRect = this.viewport_.screenToImageRect( 185 var clipRect = this.viewport_.screenToImageRect(
185 this.viewport_.getImageBoundsOnScreenClipped()); 186 this.viewport_.getImageBoundsOnScreenClipped());
186 this.cropRect_.fixedAspectRatio = aspect; 187 this.cropRect_.fixedAspectRatio = aspect;
187 this.cropRect_.forceAspectRatio(aspect, clipRect); 188 this.cropRect_.forceAspectRatio(aspect, clipRect);
188 this.markUpdated(); 189 this.markUpdated();
189 this.positionDOM(); 190 this.positionDOM();
190 } 191 }
191 }; 192 };
192 193
193 /** 194 /**
194 * Handles resizing of the viewport and updates the crop rectangle. 195 * Handles resizing of the viewport and updates the crop rectangle.
195 * @private 196 * @private
196 */ 197 */
197 ImageEditor.Mode.Crop.prototype.onViewportResized_ = function() { 198 ImageEditorMode.Crop.prototype.onViewportResized_ = function() {
198 this.positionDOM(); 199 this.positionDOM();
199 }; 200 };
200 201
201 /** 202 /**
202 * Resets the mode. 203 * Resets the mode.
203 */ 204 */
204 ImageEditor.Mode.Crop.prototype.reset = function() { 205 ImageEditorMode.Crop.prototype.reset = function() {
205 ImageEditor.Mode.prototype.reset.call(this); 206 ImageEditorMode.prototype.reset.call(this);
206 this.createDefaultCrop(); 207 this.createDefaultCrop();
207 }; 208 };
208 209
209 /** 210 /**
210 * Updates the position of DOM elements. 211 * Updates the position of DOM elements.
211 */ 212 */
212 ImageEditor.Mode.Crop.prototype.positionDOM = function() { 213 ImageEditorMode.Crop.prototype.positionDOM = function() {
213 var screenCrop = this.viewport_.imageToScreenRect(this.cropRect_.getRect()); 214 var screenCrop = this.viewport_.imageToScreenRect(this.cropRect_.getRect());
214 215
215 this.shadowLeft_.style.width = screenCrop.left + 'px'; 216 this.shadowLeft_.style.width = screenCrop.left + 'px';
216 this.shadowTop_.style.height = screenCrop.top + 'px'; 217 this.shadowTop_.style.height = screenCrop.top + 'px';
217 this.shadowRight_.style.width = window.innerWidth - screenCrop.right + 'px'; 218 this.shadowRight_.style.width = window.innerWidth - screenCrop.right + 'px';
218 this.shadowBottom_.style.height = 219 this.shadowBottom_.style.height =
219 window.innerHeight - screenCrop.bottom + 'px'; 220 window.innerHeight - screenCrop.bottom + 'px';
220 }; 221 };
221 222
222 /** 223 /**
223 * Removes the overlay elements from the document. 224 * Removes the overlay elements from the document.
224 */ 225 */
225 ImageEditor.Mode.Crop.prototype.cleanUpUI = function() { 226 ImageEditorMode.Crop.prototype.cleanUpUI = function() {
226 ImageEditor.Mode.prototype.cleanUpUI.apply(this, arguments); 227 ImageEditorMode.prototype.cleanUpUI.apply(this, arguments);
227 this.domOverlay_.parentNode.removeChild(this.domOverlay_); 228 this.domOverlay_.parentNode.removeChild(this.domOverlay_);
228 this.domOverlay_ = null; 229 this.domOverlay_ = null;
229 this.getViewport().removeEventListener( 230 this.getViewport().removeEventListener(
230 'resize', this.onViewportResizedBound_); 231 'resize', this.onViewportResizedBound_);
231 this.onViewportResizedBound_ = null; 232 this.onViewportResizedBound_ = null;
232 }; 233 };
233 234
234 /** 235 /**
235 * @const 236 * @const
236 * @type {number} 237 * @type {number}
237 */ 238 */
238 ImageEditor.Mode.Crop.MOUSE_GRAB_RADIUS = 6; 239 ImageEditorMode.Crop.MOUSE_GRAB_RADIUS = 6;
239 240
240 /** 241 /**
241 * @const 242 * @const
242 * @type {number} 243 * @type {number}
243 */ 244 */
244 ImageEditor.Mode.Crop.TOUCH_GRAB_RADIUS = 20; 245 ImageEditorMode.Crop.TOUCH_GRAB_RADIUS = 20;
245 246
246 /** 247 /**
247 * Gets command to do the crop depending on the current state. 248 * Gets command to do the crop depending on the current state.
248 * 249 *
249 * @return {!Command.Crop} Crop command. 250 * @return {!Command.Crop} Crop command.
250 */ 251 */
251 ImageEditor.Mode.Crop.prototype.getCommand = function() { 252 ImageEditorMode.Crop.prototype.getCommand = function() {
252 var cropImageRect = this.cropRect_.getRect(); 253 var cropImageRect = this.cropRect_.getRect();
253 return new Command.Crop(cropImageRect); 254 return new Command.Crop(cropImageRect);
254 }; 255 };
255 256
256 /** 257 /**
257 * Creates default (initial) crop. 258 * Creates default (initial) crop.
258 */ 259 */
259 ImageEditor.Mode.Crop.prototype.createDefaultCrop = function() { 260 ImageEditorMode.Crop.prototype.createDefaultCrop = function() {
260 var viewport = this.getViewport(); 261 var viewport = this.getViewport();
261 assert(viewport); 262 assert(viewport);
262 263
263 var rect = viewport.screenToImageRect( 264 var rect = viewport.screenToImageRect(
264 viewport.getImageBoundsOnScreenClipped()); 265 viewport.getImageBoundsOnScreenClipped());
265 rect = rect.inflate( 266 rect = rect.inflate(
266 -Math.round(rect.width / 6), -Math.round(rect.height / 6)); 267 -Math.round(rect.width / 6), -Math.round(rect.height / 6));
267 268
268 this.cropRect_ = new DraggableRect(rect, viewport); 269 this.cropRect_ = new DraggableRect(rect, viewport);
269 270
270 this.positionDOM(); 271 this.positionDOM();
271 }; 272 };
272 273
273 /** 274 /**
274 * Obtains the cursor style depending on the mouse state. 275 * Obtains the cursor style depending on the mouse state.
275 * 276 *
276 * @param {number} x X coordinate for cursor. 277 * @param {number} x X coordinate for cursor.
277 * @param {number} y Y coordinate for cursor. 278 * @param {number} y Y coordinate for cursor.
278 * @param {boolean} mouseDown If mouse button is down. 279 * @param {boolean} mouseDown If mouse button is down.
279 * @return {string} A value for style.cursor CSS property. 280 * @return {string} A value for style.cursor CSS property.
280 */ 281 */
281 ImageEditor.Mode.Crop.prototype.getCursorStyle = function(x, y, mouseDown) { 282 ImageEditorMode.Crop.prototype.getCursorStyle = function(x, y, mouseDown) {
282 return this.cropRect_.getCursorStyle(x, y, mouseDown); 283 return this.cropRect_.getCursorStyle(x, y, mouseDown);
283 }; 284 };
284 285
285 /** 286 /**
286 * Obtains handler function depending on the mouse state. 287 * Obtains handler function depending on the mouse state.
287 * 288 *
288 * @param {number} x Event X coordinate. 289 * @param {number} x Event X coordinate.
289 * @param {number} y Event Y coordinate. 290 * @param {number} y Event Y coordinate.
290 * @param {boolean} touch True if it's a touch event, false if mouse. 291 * @param {boolean} touch True if it's a touch event, false if mouse.
291 * @return {?function(number,number,boolean)} A function to be called on mouse 292 * @return {?function(number,number,boolean)} A function to be called on mouse
292 * drag. It takes x coordinate value, y coordinate value, and shift key 293 * drag. It takes x coordinate value, y coordinate value, and shift key
293 * flag. 294 * flag.
294 */ 295 */
295 ImageEditor.Mode.Crop.prototype.getDragHandler = function(x, y, touch) { 296 ImageEditorMode.Crop.prototype.getDragHandler = function(x, y, touch) {
296 var cropDragHandler = this.cropRect_.getDragHandler(x, y, touch); 297 var cropDragHandler = this.cropRect_.getDragHandler(x, y, touch);
297 if (!cropDragHandler) 298 if (!cropDragHandler)
298 return null; 299 return null;
299 300
300 return function(x, y, shiftKey) { 301 return function(x, y, shiftKey) {
301 cropDragHandler(x, y, shiftKey); 302 cropDragHandler(x, y, shiftKey);
302 this.markUpdated(); 303 this.markUpdated();
303 this.positionDOM(); 304 this.positionDOM();
304 }.bind(this); 305 }.bind(this);
305 }; 306 };
306 307
307 /** 308 /**
308 * Obtains the double tap action depending on the coordinate. 309 * Obtains the double tap action depending on the coordinate.
309 * 310 *
310 * @param {number} x X coordinate of the event. 311 * @param {number} x X coordinate of the event.
311 * @param {number} y Y coordinate of the event. 312 * @param {number} y Y coordinate of the event.
312 * @return {!ImageBuffer.DoubleTapAction} Action to perform as result. 313 * @return {!ImageBuffer.DoubleTapAction} Action to perform as result.
313 */ 314 */
314 ImageEditor.Mode.Crop.prototype.getDoubleTapAction = function(x, y) { 315 ImageEditorMode.Crop.prototype.getDoubleTapAction = function(x, y) {
315 return this.cropRect_.getDoubleTapAction(x, y); 316 return this.cropRect_.getDoubleTapAction(x, y);
316 }; 317 };
317 318
318 /** 319 /**
319 * A draggable rectangle over the image. 320 * A draggable rectangle over the image.
320 * 321 *
321 * @param {!ImageRect} rect Initial size of the image. 322 * @param {!ImageRect} rect Initial size of the image.
322 * @param {!Viewport} viewport Viewport. 323 * @param {!Viewport} viewport Viewport.
323 * @constructor 324 * @constructor
324 * @struct 325 * @struct
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 449
449 var result = { 450 var result = {
450 xSide: DraggableRect.NONE, 451 xSide: DraggableRect.NONE,
451 ySide: DraggableRect.NONE, 452 ySide: DraggableRect.NONE,
452 whole: false, 453 whole: false,
453 newCrop: false 454 newCrop: false
454 }; 455 };
455 456
456 var bounds = this.bounds_; 457 var bounds = this.bounds_;
457 var R = this.viewport_.screenToImageSize( 458 var R = this.viewport_.screenToImageSize(
458 touch ? ImageEditor.Mode.Crop.TOUCH_GRAB_RADIUS : 459 touch ? ImageEditorMode.Crop.TOUCH_GRAB_RADIUS :
459 ImageEditor.Mode.Crop.MOUSE_GRAB_RADIUS); 460 ImageEditorMode.Crop.MOUSE_GRAB_RADIUS);
460 461
461 var circle = new Circle(x, y, R); 462 var circle = new Circle(x, y, R);
462 463
463 var xBetween = ImageUtil.between(bounds.left, x, bounds.right); 464 var xBetween = ImageUtil.between(bounds.left, x, bounds.right);
464 var yBetween = ImageUtil.between(bounds.top, y, bounds.bottom); 465 var yBetween = ImageUtil.between(bounds.top, y, bounds.bottom);
465 466
466 if (circle.inside(bounds.left, bounds.top)) { 467 if (circle.inside(bounds.left, bounds.top)) {
467 result.xSide = DraggableRect.LEFT; 468 result.xSide = DraggableRect.LEFT;
468 result.ySide = DraggableRect.TOP; 469 result.ySide = DraggableRect.TOP;
469 } else if (circle.inside(bounds.left, bounds.bottom)) { 470 } else if (circle.inside(bounds.left, bounds.bottom)) {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
534 * @param {number} initialScreenX X coordinate for cursor in the screen. 535 * @param {number} initialScreenX X coordinate for cursor in the screen.
535 * @param {number} initialScreenY Y coordinate for cursor in the screen. 536 * @param {number} initialScreenY Y coordinate for cursor in the screen.
536 * @param {boolean} touch Whether the operation is done by touch or not. 537 * @param {boolean} touch Whether the operation is done by touch or not.
537 * @return {?function(number,number,boolean)} Drag handler that takes x 538 * @return {?function(number,number,boolean)} Drag handler that takes x
538 * coordinate value, y coordinate value, and shift key flag. 539 * coordinate value, y coordinate value, and shift key flag.
539 */ 540 */
540 DraggableRect.prototype.getDragHandler = function( 541 DraggableRect.prototype.getDragHandler = function(
541 initialScreenX, initialScreenY, touch) { 542 initialScreenX, initialScreenY, touch) {
542 // Check if the initial coordinate is in the image rect. 543 // Check if the initial coordinate is in the image rect.
543 var boundsOnScreen = this.viewport_.getImageBoundsOnScreenClipped(); 544 var boundsOnScreen = this.viewport_.getImageBoundsOnScreenClipped();
544 var handlerRadius = touch ? ImageEditor.Mode.Crop.TOUCH_GRAB_RADIUS : 545 var handlerRadius = touch ? ImageEditorMode.Crop.TOUCH_GRAB_RADIUS :
545 ImageEditor.Mode.Crop.MOUSE_GRAB_RADIUS; 546 ImageEditorMode.Crop.MOUSE_GRAB_RADIUS;
546 var draggableAreas = [ 547 var draggableAreas = [
547 boundsOnScreen, 548 boundsOnScreen,
548 new Circle(boundsOnScreen.left, boundsOnScreen.top, handlerRadius), 549 new Circle(boundsOnScreen.left, boundsOnScreen.top, handlerRadius),
549 new Circle(boundsOnScreen.right, boundsOnScreen.top, handlerRadius), 550 new Circle(boundsOnScreen.right, boundsOnScreen.top, handlerRadius),
550 new Circle(boundsOnScreen.left, boundsOnScreen.bottom, handlerRadius), 551 new Circle(boundsOnScreen.left, boundsOnScreen.bottom, handlerRadius),
551 new Circle(boundsOnScreen.right, boundsOnScreen.bottom, handlerRadius) 552 new Circle(boundsOnScreen.right, boundsOnScreen.bottom, handlerRadius)
552 ]; 553 ];
553 554
554 if (!draggableAreas.some( 555 if (!draggableAreas.some(
555 (area) => area.inside(initialScreenX, initialScreenY))) { 556 (area) => area.inside(initialScreenX, initialScreenY))) {
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
738 break; 739 break;
739 case 'bottom': 740 case 'bottom':
740 this.bounds_.bottom = this.bounds_.top + newHeight; 741 this.bounds_.bottom = this.bounds_.top + newHeight;
741 break; 742 break;
742 case 'none': 743 case 'none':
743 this.bounds_.top = middle - newHeight / 2; 744 this.bounds_.top = middle - newHeight / 2;
744 this.bounds_.bottom = middle + newHeight / 2; 745 this.bounds_.bottom = middle + newHeight / 2;
745 break; 746 break;
746 } 747 }
747 }; 748 };
OLDNEW
« no previous file with comments | « ui/file_manager/gallery/js/image_editor/image_resize.js ('k') | ui/file_manager/gallery/js/slide_mode.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698