From eb526121bdb1bdda56100a27c05af1033cf4e378 Mon Sep 17 00:00:00 2001 From: Tor Norbye Date: Tue, 8 Feb 2011 13:47:58 -0800 Subject: [PATCH] Make layout editor margins compress when necessary If there isn't enough room to show the margins, make the margins smaller. This gives more useful layout room when you are dealing with large screens. Change-Id: I6f967dbe6b18ca5ee85cc04db88d903cb976e851 --- eclipse/dictionary.txt | 1 + .../editors/layout/gle2/CanvasTransform.java | 33 +++++++++-- .../internal/editors/layout/gle2/GCWrapper.java | 6 +- .../editors/layout/gle2/ICanvasTransform.java | 65 ---------------------- .../internal/editors/layout/gle2/LayoutCanvas.java | 36 +++++++++--- .../editors/layout/gle2/ViewHierarchy.java | 4 +- 6 files changed, 64 insertions(+), 81 deletions(-) delete mode 100755 eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/ICanvasTransform.java diff --git a/eclipse/dictionary.txt b/eclipse/dictionary.txt index d86a78edf..235e9f254 100644 --- a/eclipse/dictionary.txt +++ b/eclipse/dictionary.txt @@ -217,6 +217,7 @@ urls validator varargs verbosity +viewport vs webtools whilst diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/CanvasTransform.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/CanvasTransform.java index 831367a2e..0d7fc2873 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/CanvasTransform.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/CanvasTransform.java @@ -23,7 +23,13 @@ import org.eclipse.swt.widgets.ScrollBar; * Helper class to convert between control pixel coordinates and canvas coordinates. * Takes care of the zooming and offset of the canvas. */ -public class CanvasTransform implements ICanvasTransform { +public class CanvasTransform { + /** + * Default margin around the rendered image, reduced + * when the contents do not fit. + */ + public static final int DEFAULT_MARGIN = 25; + /** * The canvas which controls the zooming. */ @@ -38,6 +44,9 @@ public class CanvasTransform implements ICanvasTransform { /** Left-top offset in client pixel coordinates. */ private int mTranslate; + /** Current margin */ + private int mMargin = DEFAULT_MARGIN; + /** Scaling factor, > 0. */ private double mScale; @@ -116,8 +125,20 @@ public class CanvasTransform implements ICanvasTransform { // scaled image size int sx = (int) (mImgSize * mScale); + // Adjust margin such that for zoomed out views + // we don't waste space (unless the viewport is + // large enough to accommodate it) + int delta = mClientSize - sx; + if (delta < 0) { + mMargin = 0; + } else if (delta < 2 * DEFAULT_MARGIN) { + mMargin = delta / 2; + } else { + mMargin = DEFAULT_MARGIN; + } + // actual client area is always reduced by the margins - int cx = mClientSize - 2 * IMAGE_MARGIN; + int cx = mClientSize - 2 * mMargin; if (sx < cx) { mTranslate = 0; @@ -143,8 +164,12 @@ public class CanvasTransform implements ICanvasTransform { } } + public int getMargin() { + return mMargin; + } + public int translate(int canvasX) { - return IMAGE_MARGIN - mTranslate + (int) (mScale * canvasX); + return mMargin - mTranslate + (int) (mScale * canvasX); } public int scale(int canwasW) { @@ -152,6 +177,6 @@ public class CanvasTransform implements ICanvasTransform { } public int inverseTranslate(int screenX) { - return (int) ((screenX - IMAGE_MARGIN + mTranslate) / mScale); + return (int) ((screenX - mMargin + mTranslate) / mScale); } } diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/GCWrapper.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/GCWrapper.java index fd41a0f0d..a117ea9a5 100755 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/GCWrapper.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/GCWrapper.java @@ -91,11 +91,11 @@ public class GCWrapper implements IGraphics { private int mFontHeight = 0; /** The scaling of the canvas in X. */ - private final ICanvasTransform mHScale; + private final CanvasTransform mHScale; /** The scaling of the canvas in Y. */ - private final ICanvasTransform mVScale; + private final CanvasTransform mVScale; - public GCWrapper(ICanvasTransform hScale, ICanvasTransform vScale) { + public GCWrapper(CanvasTransform hScale, CanvasTransform vScale) { mHScale = hScale; mVScale = vScale; mGc = null; diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/ICanvasTransform.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/ICanvasTransform.java deleted file mode 100755 index 03d86fb0e..000000000 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/ICanvasTransform.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Eclipse Public License, Version 1.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.eclipse.org/org/documents/epl-v10.php - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.ide.eclipse.adt.internal.editors.layout.gle2; - -/** - * Interface for a class that can convert between client pixel's coordinates - * and canvas coordinates. Each instance of such a transform deals with only - * one axis, so clients need to use 2 instances for X and Y. - */ -/* package */ interface ICanvasTransform { - /** - * Margin around the rendered image. - * Should be enough space to display the layout width and height pseudo widgets. - */ - public static final int IMAGE_MARGIN = 25; - - /** - * Computes the transformation from a X/Y canvas image coordinate - * to client pixel coordinate. - *

- * This takes into account the {@link #IMAGE_MARGIN}, - * the current scaling and the current translation. - * - * @param canvasX A canvas image coordinate (X or Y). - * @return The transformed coordinate in client pixel space. - */ - public int translate(int canvasX); - - /** - * Computes the transformation from a canvas image size (width or height) to - * client pixel coordinates. - * - * @param canwasW A canvas image size (W or H). - * @return The transformed coordinate in client pixel space. - */ - public int scale(int canwasW); - - /** - * Computes the transformation from a X/Y client pixel coordinate - * to canvas image coordinate. - *

- * This takes into account the {@link #IMAGE_MARGIN}, - * the current scaling and the current translation. - *

- * This is the inverse of {@link #translate(int)}. - * - * @param screenX A client pixel space coordinate (X or Y). - * @return The transformed coordinate in canvas image space. - */ - public int inverseTranslate(int screenX); -} diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/LayoutCanvas.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/LayoutCanvas.java index 6c1f7acfc..289831f36 100755 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/LayoutCanvas.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/LayoutCanvas.java @@ -228,7 +228,8 @@ public class LayoutCanvas extends Canvas { mHScale = new CanvasTransform(this, getHorizontalBar()); mVScale = new CanvasTransform(this, getVerticalBar()); - IFile file = layoutEditor.getInputFile(); + // Unit test suite passes a null here; TODO: Replace with mocking + IFile file = layoutEditor != null ? layoutEditor.getInputFile() : null; if (file != null) { String zoom = AdtPlugin.getFileProperty(file, NAME_ZOOM); if (zoom != null) { @@ -579,17 +580,38 @@ public class LayoutCanvas extends Canvas { /** Scales the canvas to best fit */ void setFitScale() { - Rectangle canvasSize = getClientArea(); - int canvasWidth = canvasSize.width - 2 * ICanvasTransform.IMAGE_MARGIN; - int canvasHeight = canvasSize.height - 2 * ICanvasTransform.IMAGE_MARGIN; - Image image = getImageOverlay().getImage(); if (image != null) { + Rectangle canvasSize = getClientArea(); + int canvasWidth = canvasSize.width; + int canvasHeight = canvasSize.height; + ImageData imageData = image.getImageData(); int sceneWidth = imageData.width; int sceneHeight = imageData.height; - double hScale = canvasWidth / (double) sceneWidth; - double vScale = canvasHeight / (double) sceneHeight; + if (sceneWidth == 0.0 || sceneHeight == 0.0) { + return; + } + + // Reduce the margins if necessary + int hDelta = canvasWidth - sceneWidth; + int hMargin = 0; + if (hDelta > 2 * CanvasTransform.DEFAULT_MARGIN) { + hMargin = CanvasTransform.DEFAULT_MARGIN; + } else if (hDelta > 0) { + hMargin = hDelta / 2; + } + + int vDelta = canvasHeight - sceneHeight; + int vMargin = 0; + if (vDelta > 2 * CanvasTransform.DEFAULT_MARGIN) { + vMargin = CanvasTransform.DEFAULT_MARGIN; + } else if (vDelta > 0) { + vMargin = vDelta / 2; + } + + double hScale = canvasWidth / (double) (sceneWidth - hMargin); + double vScale = canvasHeight / (double) (sceneHeight - vMargin); double scale = Math.min(hScale, vScale); setScale(scale, true); diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/ViewHierarchy.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/ViewHierarchy.java index 01487b953..eac222822 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/ViewHierarchy.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/ViewHierarchy.java @@ -202,8 +202,8 @@ public class ViewHierarchy { private ViewInfo createMergeInfo(RenderSession session) { BufferedImage image = session.getImage(); ControlPoint imageSize = ControlPoint.create(mCanvas, - ICanvasTransform.IMAGE_MARGIN + image.getWidth(), - ICanvasTransform.IMAGE_MARGIN + image.getHeight()); + mCanvas.getHorizontalTransform().getMargin() + image.getWidth(), + mCanvas.getVerticalTransform().getMargin() + image.getHeight()); LayoutPoint layoutSize = imageSize.toLayout(); UiDocumentNode model = mCanvas.getLayoutEditor().getUiRootNode(); List children = model.getUiChildren(); -- 2.11.0