Commit 823c3c0e authored by hirono@chromium.org's avatar hirono@chromium.org

Gallery: Random fixes for the Viewport class.

The CL do:

* Remove the device rect argument from paintDeviceRect because the rectangle can
be calculated from other arguments.

* Remove setByFrame(AndFit) helper functions that are used only once each to simplify
the Viewport class.

* Rename getScreenClipped with getImageBoundsOnScreenClipped. The old name
  sounds it retuns the bounds of screen.

* Add getImageElementBounds to obtain the bounds before applying zoom and
  offset.

BUG=245926
TEST=manually

Review URL: https://codereview.chromium.org/398263002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@283719 0039d316-1c4b-4281-b951-d872f2087c98
parent 33371b3f
...@@ -415,9 +415,7 @@ Command.Filter.prototype = { __proto__: Command.prototype }; ...@@ -415,9 +415,7 @@ Command.Filter.prototype = { __proto__: Command.prototype };
Command.Filter.prototype.execute = function( Command.Filter.prototype.execute = function(
document, srcCanvas, callback, uiContext) { document, srcCanvas, callback, uiContext) {
var result = this.createCanvas_(document, srcCanvas); var result = this.createCanvas_(document, srcCanvas);
var self = this; var self = this;
var previousRow = 0; var previousRow = 0;
function onProgressVisible(updatedRow, rowCount) { function onProgressVisible(updatedRow, rowCount) {
...@@ -438,8 +436,7 @@ Command.Filter.prototype.execute = function( ...@@ -438,8 +436,7 @@ Command.Filter.prototype.execute = function(
screenStrip.height = screenStrip.height =
Math.round(viewport.imageToScreenY(updatedRow)) - screenStrip.top; Math.round(viewport.imageToScreenY(updatedRow)) - screenStrip.top;
uiContext.imageView.paintDeviceRect( uiContext.imageView.paintDeviceRect(result, imageStrip);
viewport.screenToDeviceRect(screenStrip), result, imageStrip);
previousRow = updatedRow; previousRow = updatedRow;
} }
} }
......
...@@ -30,7 +30,8 @@ function ImageEditor( ...@@ -30,7 +30,8 @@ function ImageEditor(
ImageUtil.removeChildren(this.container_); ImageUtil.removeChildren(this.container_);
this.viewport_ = viewport; this.viewport_ = viewport;
this.viewport_.sizeByFrame(this.container_); this.viewport_.setScreenSize(
this.container_.clientWidth, this.container_.clientHeight);
this.imageView_ = imageView; this.imageView_ = imageView;
this.imageView_.addContentCallback(this.onContentUpdate_.bind(this)); this.imageView_.addContentCallback(this.onContentUpdate_.bind(this));
......
...@@ -128,7 +128,7 @@ ImageEditor.Mode.Crop.prototype.reset = function() { ...@@ -128,7 +128,7 @@ ImageEditor.Mode.Crop.prototype.reset = function() {
* Updates the position of DOM elements. * Updates the position of DOM elements.
*/ */
ImageEditor.Mode.Crop.prototype.positionDOM = function() { ImageEditor.Mode.Crop.prototype.positionDOM = function() {
var screenClipped = this.viewport_.getScreenClipped(); var screenClipped = this.viewport_.getImageBoundsOnScreenClipped();
var screenCrop = this.viewport_.imageToScreenRect(this.cropRect_.getRect()); var screenCrop = this.viewport_.imageToScreenRect(this.cropRect_.getRect());
var delta = ImageEditor.Mode.Crop.MOUSE_GRAB_RADIUS; var delta = ImageEditor.Mode.Crop.MOUSE_GRAB_RADIUS;
...@@ -190,7 +190,8 @@ ImageEditor.Mode.Crop.prototype.getCommand = function() { ...@@ -190,7 +190,8 @@ ImageEditor.Mode.Crop.prototype.getCommand = function() {
* Creates default (initial) crop. * Creates default (initial) crop.
*/ */
ImageEditor.Mode.Crop.prototype.createDefaultCrop = function() { ImageEditor.Mode.Crop.prototype.createDefaultCrop = function() {
var rect = new Rect(this.getViewport().getImageClipped()); var rect = this.getViewport().screenToImageRect(
new Rect(this.getViewport().getImageBoundsOnScreenClipped()));
rect = rect.inflate( rect = rect.inflate(
-Math.round(rect.width / 6), -Math.round(rect.height / 6)); -Math.round(rect.width / 6), -Math.round(rect.height / 6));
this.cropRect_ = new DraggableRect(rect, this.getViewport()); this.cropRect_ = new DraggableRect(rect, this.getViewport());
...@@ -465,7 +466,8 @@ DraggableRect.prototype.getDragHandler = function( ...@@ -465,7 +466,8 @@ DraggableRect.prototype.getDragHandler = function(
var initialY = this.viewport_.screenToImageY(initialScreenY); var initialY = this.viewport_.screenToImageY(initialScreenY);
var initialWidth = this.bounds_.right - this.bounds_.left; var initialWidth = this.bounds_.right - this.bounds_.left;
var initialHeight = this.bounds_.bottom - this.bounds_.top; var initialHeight = this.bounds_.bottom - this.bounds_.top;
var clipRect = this.viewport_.getImageClipped(); var clipRect = this.viewport_.screenToImageRect(
this.viewport_.getImageBoundsOnScreenClipped());
if (!clipRect.inside(initialX, initialY)) if (!clipRect.inside(initialX, initialY))
return null; return null;
...@@ -557,10 +559,7 @@ DraggableRect.prototype.getDragHandler = function( ...@@ -557,10 +559,7 @@ DraggableRect.prototype.getDragHandler = function(
* @return {ImageBuffer.DoubleTapAction} Double tap action. * @return {ImageBuffer.DoubleTapAction} Double tap action.
*/ */
DraggableRect.prototype.getDoubleTapAction = function(x, y, touch) { DraggableRect.prototype.getDoubleTapAction = function(x, y, touch) {
x = this.viewport_.screenToImageX(x); var clipRect = this.viewport_.getImageBoundsOnScreenClipped();
y = this.viewport_.screenToImageY(y);
var clipRect = this.viewport_.getImageClipped();
if (clipRect.inside(x, y)) if (clipRect.inside(x, y))
return ImageBuffer.DoubleTapAction.COMMIT; return ImageBuffer.DoubleTapAction.COMMIT;
else else
......
...@@ -139,6 +139,28 @@ function Rect() { ...@@ -139,6 +139,28 @@ function Rect() {
Array.apply(null, arguments)); Array.apply(null, arguments));
} }
Rect.prototype = {
/**
* Obtains the x coordinate of right edge. The most right pixels in the
* rectangle are (x = right - 1) and the pixels (x = right) are not included
* in the rectangle.
* @return {number}
*/
get right() {
return this.left + this.width;
},
/**
* Obtains the y coordinate of bottom edge. The most bottom pixels in the
* rectanlge are (y = buttom - 1) and the pixels (y = bottom) are not included
* in the rectangle.
* @return {number}
*/
get bottom() {
return this.top + this.height;
}
};
/** /**
* @param {number} factor Factor to scale. * @param {number} factor Factor to scale.
* @return {Rect} A rectangle with every dimension scaled. * @return {Rect} A rectangle with every dimension scaled.
......
...@@ -119,8 +119,7 @@ ImageView.prototype.draw = function() { ...@@ -119,8 +119,7 @@ ImageView.prototype.draw = function() {
this.displayedContentGeneration_ = this.contentGeneration_; this.displayedContentGeneration_ = this.contentGeneration_;
ImageUtil.trace.resetTimer('paint'); ImageUtil.trace.resetTimer('paint');
this.paintDeviceRect(this.viewport_.getDeviceClipped(), this.paintDeviceRect(this.contentCanvas_, new Rect(this.contentCanvas_));
this.contentCanvas_, this.viewport_.getImageClipped());
ImageUtil.trace.reportTimer('paint'); ImageUtil.trace.reportTimer('paint');
} }
}; };
...@@ -178,22 +177,29 @@ ImageView.prototype.getContentRevision = function() { ...@@ -178,22 +177,29 @@ ImageView.prototype.getContentRevision = function() {
* Copies an image fragment from a full resolution canvas to a device resolution * Copies an image fragment from a full resolution canvas to a device resolution
* canvas. * canvas.
* *
* @param {Rect} deviceRect Rectangle in the device coordinates. * @param {HTMLCanvasElement} canvas Canvas containing whole image. The canvas
* @param {HTMLCanvasElement} canvas Full resolution canvas. * may not be full resolution (scaled).
* @param {Rect} imageRect Rectangle in the full resolution canvas. * @param {Rect} imageRect Rectangle region of the canvas to be rendered.
*/ */
ImageView.prototype.paintDeviceRect = function(deviceRect, canvas, imageRect) { ImageView.prototype.paintDeviceRect = function(canvas, imageRect) {
// Map screen canvas (0,0) to (deviceBounds.left, deviceBounds.top) // Check canvas size.
var deviceBounds = this.viewport_.getDeviceClipped(); var deviceBounds = this.viewport_.getDeviceBounds();
deviceRect = deviceRect.shift(-deviceBounds.left, -deviceBounds.top); if (this.screenImage_.width != deviceBounds.width ||
this.screenImage_.height != deviceBounds.height) {
// The source canvas may have different physical size than the image size console.error('The size of canvas is invalid.', (new Error).stack);
// set at the viewport. Adjust imageRect accordingly. return;
var bounds = this.viewport_.getImageBounds(); }
var scaleX = canvas.width / bounds.width;
var scaleY = canvas.height / bounds.height; // Map the rectangle in full resolution image to the rectangle in the device
imageRect = new Rect(imageRect.left * scaleX, imageRect.top * scaleY, // canvas.
imageRect.width * scaleX, imageRect.height * scaleY); var scaleX = deviceBounds.width / canvas.width;
var scaleY = deviceBounds.height / canvas.height;
var deviceRect = new Rect(
imageRect.left * scaleX,
imageRect.top * scaleY,
imageRect.width * scaleX,
imageRect.height * scaleY);
Rect.drawImage( Rect.drawImage(
this.screenImage_.getContext('2d'), canvas, deviceRect, imageRect); this.screenImage_.getContext('2d'), canvas, deviceRect, imageRect);
}; };
...@@ -217,17 +223,17 @@ ImageView.prototype.createOverlayCanvas = function() { ...@@ -217,17 +223,17 @@ ImageView.prototype.createOverlayCanvas = function() {
* @param {HTMLCanvasElement} canvas The buffer canvas. * @param {HTMLCanvasElement} canvas The buffer canvas.
*/ */
ImageView.prototype.setupDeviceBuffer = function(canvas) { ImageView.prototype.setupDeviceBuffer = function(canvas) {
var deviceRect = this.viewport_.getDeviceClipped();
// Set the canvas position and size in device pixels. // Set the canvas position and size in device pixels.
var deviceRect = this.viewport_.getDeviceBounds();
if (canvas.width !== deviceRect.width) if (canvas.width !== deviceRect.width)
canvas.width = deviceRect.width; canvas.width = deviceRect.width;
if (canvas.height !== deviceRect.height) if (canvas.height !== deviceRect.height)
canvas.height = deviceRect.height; canvas.height = deviceRect.height;
canvas.style.left = deviceRect.left + 'px'; // Center the image.
canvas.style.top = deviceRect.top + 'px'; var imageBoudns = this.viewport_.getImageElementBoundsOnScreen();
canvas.style.left = imageBoudns.left + 'px';
canvas.style.top = imageBoudns.top + 'px';
// Scale the canvas down to screen pixels. // Scale the canvas down to screen pixels.
this.setTransform(canvas); this.setTransform(canvas);
......
...@@ -394,7 +394,7 @@ SlideMode.prototype.getSelectedImageRect = function() { ...@@ -394,7 +394,7 @@ SlideMode.prototype.getSelectedImageRect = function() {
if (this.getSelectedIndex() < 0) if (this.getSelectedIndex() < 0)
return null; return null;
else else
return this.viewport_.getScreenClipped(); return this.viewport_.getImageBoundsOnScreen();
}; };
/** /**
...@@ -889,8 +889,8 @@ SlideMode.prototype.onKeyDown = function(event) { ...@@ -889,8 +889,8 @@ SlideMode.prototype.onKeyDown = function(event) {
* @private * @private
*/ */
SlideMode.prototype.onResize_ = function() { SlideMode.prototype.onResize_ = function() {
this.viewport_.sizeByFrameAndFit(this.container_); this.viewport_.setScreenSize(
this.viewport_.update(); this.container_.clientWidth, this.container_.clientHeight);
this.editor_.getBuffer().draw(); this.editor_.getBuffer().draw();
}; };
...@@ -1047,7 +1047,7 @@ SlideMode.prototype.isSlideshowOn_ = function() { ...@@ -1047,7 +1047,7 @@ SlideMode.prototype.isSlideshowOn_ = function() {
SlideMode.prototype.startSlideshow = function(opt_interval, opt_event) { SlideMode.prototype.startSlideshow = function(opt_interval, opt_event) {
// Reset zoom. // Reset zoom.
this.viewport_.setZoomIndex(0); this.viewport_.setZoomIndex(0);
this.imageView_.draw(); this.imageView_.applyViewportChange();
// Set the attribute early to prevent the toolbar from flashing when // Set the attribute early to prevent the toolbar from flashing when
// the slideshow is being started from the mosaic view. // the slideshow is being started from the mosaic view.
...@@ -1202,7 +1202,7 @@ SlideMode.prototype.toggleEditor = function(opt_event) { ...@@ -1202,7 +1202,7 @@ SlideMode.prototype.toggleEditor = function(opt_event) {
if (this.isEditing()) { // isEditing has just been flipped to a new value. if (this.isEditing()) { // isEditing has just been flipped to a new value.
// Reset zoom. // Reset zoom.
this.viewport_.setZoomIndex(0); this.viewport_.setZoomIndex(0);
this.imageView_.draw(); this.imageView_.applyViewportChange();
if (this.context_.readonlyDirName) { if (this.context_.readonlyDirName) {
this.editor_.getPrompt().showAt( this.editor_.getPrompt().showAt(
'top', 'GALLERY_READONLY_WARNING', 0, this.context_.readonlyDirName); 'top', 'GALLERY_READONLY_WARNING', 0, this.context_.readonlyDirName);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment