OSDN Git Service

Make layout editor margins compress when necessary
authorTor Norbye <tnorbye@google.com>
Tue, 8 Feb 2011 21:47:58 +0000 (13:47 -0800)
committerTor Norbye <tnorbye@google.com>
Tue, 8 Feb 2011 22:28:49 +0000 (14:28 -0800)
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
eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/CanvasTransform.java
eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/GCWrapper.java
eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/ICanvasTransform.java [deleted file]
eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/LayoutCanvas.java
eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/ViewHierarchy.java

index d86a78e..235e9f2 100644 (file)
@@ -217,6 +217,7 @@ urls
 validator
 varargs
 verbosity
+viewport
 vs
 webtools
 whilst
index 831367a..0d7fc28 100644 (file)
@@ -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);
     }
 }
index fd41a0f..a117ea9 100755 (executable)
@@ -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 (executable)
index 03d86fb..0000000
+++ /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.
-     * <p/>
-     * 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.
-     * <p/>
-     * This takes into account the {@link #IMAGE_MARGIN},
-     * the current scaling and the current translation.
-     * <p/>
-     * 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);
-}
index 6c1f7ac..289831f 100755 (executable)
@@ -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);
index 01487b9..eac2228 100644 (file)
@@ -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<UiElementNode> children = model.getUiChildren();