From e6d479e7f2809f199bf3c555300ef842f18f76fb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Thu, 9 Feb 2012 15:48:59 +0200 Subject: [PATCH] Update AbsoluteLayout to be a CalculatingLayout (#8313) It is still doing measurements of its own, which causes reflows. --- .../gwt/client/ui/VAbsoluteLayout.java | 107 +----------------- .../client/ui/VAbsoluteLayoutPaintable.java | 72 +++++++++++- 2 files changed, 74 insertions(+), 105 deletions(-) diff --git a/src/com/vaadin/terminal/gwt/client/ui/VAbsoluteLayout.java b/src/com/vaadin/terminal/gwt/client/ui/VAbsoluteLayout.java index 7ac44bc4b7..fb28f452d8 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VAbsoluteLayout.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VAbsoluteLayout.java @@ -4,10 +4,7 @@ package com.vaadin.terminal.gwt.client.ui; import java.util.HashMap; -import java.util.Iterator; import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; import com.google.gwt.dom.client.DivElement; import com.google.gwt.dom.client.Document; @@ -18,15 +15,13 @@ import com.google.gwt.user.client.ui.ComplexPanel; import com.google.gwt.user.client.ui.SimplePanel; import com.google.gwt.user.client.ui.Widget; import com.vaadin.terminal.gwt.client.ApplicationConnection; -import com.vaadin.terminal.gwt.client.Container; -import com.vaadin.terminal.gwt.client.RenderSpace; import com.vaadin.terminal.gwt.client.UIDL; import com.vaadin.terminal.gwt.client.Util; import com.vaadin.terminal.gwt.client.VCaption; import com.vaadin.terminal.gwt.client.VPaintableMap; import com.vaadin.terminal.gwt.client.VPaintableWidget; -public class VAbsoluteLayout extends ComplexPanel implements Container { +public class VAbsoluteLayout extends ComplexPanel { /** Tag name for widget creation */ public static final String TAGNAME = "absolutelayout"; @@ -38,18 +33,12 @@ public class VAbsoluteLayout extends ComplexPanel implements Container { protected final Element canvas = DOM.createDiv(); - // private int excessPixelsHorizontal; - // - // private int excessPixelsVertical; - private Object previousStyleName; Map pidToComponentWrappper = new HashMap(); protected ApplicationConnection client; - boolean rendering; - public VAbsoluteLayout() { setElement(Document.get().createDivElement()); setStyleName(CLASSNAME); @@ -60,63 +49,6 @@ public class VAbsoluteLayout extends ComplexPanel implements Container { getElement().appendChild(marginElement); } - public RenderSpace getAllocatedSpace(Widget child) { - // TODO needs some special handling for components with only on edge - // horizontally or vertically defined - AbsoluteWrapper wrapper = (AbsoluteWrapper) child.getParent(); - int w; - if (wrapper.left != null && wrapper.right != null) { - w = wrapper.getOffsetWidth(); - } else if (wrapper.right != null) { - // left == null - // available width == right edge == offsetleft + width - w = wrapper.getOffsetWidth() + wrapper.getElement().getOffsetLeft(); - } else { - // left != null && right == null || left == null && - // right == null - // available width == canvas width - offset left - w = canvas.getOffsetWidth() - wrapper.getElement().getOffsetLeft(); - } - int h; - if (wrapper.top != null && wrapper.bottom != null) { - h = wrapper.getOffsetHeight(); - } else if (wrapper.bottom != null) { - // top not defined, available space 0... bottom of wrapper - h = wrapper.getElement().getOffsetTop() + wrapper.getOffsetHeight(); - } else { - // top defined or both undefined, available space == canvas - top - h = canvas.getOffsetHeight() - wrapper.getElement().getOffsetTop(); - } - - return new RenderSpace(w, h); - } - - public boolean hasChildComponent(Widget component) { - for (Iterator> iterator = pidToComponentWrappper - .entrySet().iterator(); iterator.hasNext();) { - if (iterator.next().getValue().paintable == component) { - return true; - } - } - return false; - } - - public void replaceChildComponent(Widget oldComponent, Widget newComponent) { - for (Widget wrapper : getChildren()) { - AbsoluteWrapper w = (AbsoluteWrapper) wrapper; - if (w.getWidget() == oldComponent) { - w.setWidget(newComponent); - return; - } - } - } - - public boolean requestLayout(Set children) { - // component inside an absolute panel never affects parent nor the - // layout - return true; - } - AbsoluteWrapper getWrapper(ApplicationConnection client, UIDL componentUIDL) { AbsoluteWrapper wrapper = pidToComponentWrappper.get(componentUIDL .getId()); @@ -134,35 +66,12 @@ public class VAbsoluteLayout extends ComplexPanel implements Container { super.add(child, canvas); } - @Override - public void setStyleName(String style) { - super.setStyleName(style); - if (previousStyleName == null || !previousStyleName.equals(style)) { - // excessPixelsHorizontal = -1; - // excessPixelsVertical = -1; - } - } - @Override public void setWidth(String width) { super.setWidth(width); // TODO do this so that canvas gets the sized properly (the area // inside marginals) canvas.getStyle().setProperty("width", width); - - if (!rendering) { - relayoutRelativeChildren(); - } - } - - private void relayoutRelativeChildren() { - for (Widget widget : getChildren()) { - if (widget instanceof AbsoluteWrapper) { - AbsoluteWrapper w = (AbsoluteWrapper) widget; - client.handleComponentRelativeSize(w.getWidget()); - w.updateCaptionPosition(); - } - } } @Override @@ -171,18 +80,14 @@ public class VAbsoluteLayout extends ComplexPanel implements Container { // TODO do this so that canvas gets the sized properly (the area // inside marginals) canvas.getStyle().setProperty("height", height); - - if (!rendering) { - relayoutRelativeChildren(); - } } public class AbsoluteWrapper extends SimplePanel { private String css; - private String left; - private String top; - private String right; - private String bottom; + String left; + String top; + String right; + String bottom; private String zIndex; private VPaintableWidget paintable; @@ -284,7 +189,7 @@ public class VAbsoluteLayout extends ComplexPanel implements Container { updateCaptionPosition(); } - private void updateCaptionPosition() { + void updateCaptionPosition() { if (caption != null) { Style style = caption.getElement().getStyle(); style.setProperty("position", "absolute"); diff --git a/src/com/vaadin/terminal/gwt/client/ui/VAbsoluteLayoutPaintable.java b/src/com/vaadin/terminal/gwt/client/ui/VAbsoluteLayoutPaintable.java index 75597e27e0..6ae01c5c23 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VAbsoluteLayoutPaintable.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VAbsoluteLayoutPaintable.java @@ -7,18 +7,22 @@ import java.util.HashSet; import java.util.Iterator; import com.google.gwt.core.client.GWT; +import com.google.gwt.dom.client.Style; +import com.google.gwt.dom.client.Style.Unit; import com.google.gwt.event.dom.client.DomEvent.Type; import com.google.gwt.event.shared.EventHandler; import com.google.gwt.event.shared.HandlerRegistration; import com.google.gwt.user.client.Element; import com.google.gwt.user.client.ui.Widget; import com.vaadin.terminal.gwt.client.ApplicationConnection; +import com.vaadin.terminal.gwt.client.CalculatingLayout; import com.vaadin.terminal.gwt.client.EventId; import com.vaadin.terminal.gwt.client.UIDL; import com.vaadin.terminal.gwt.client.VPaintableWidget; import com.vaadin.terminal.gwt.client.ui.VAbsoluteLayout.AbsoluteWrapper; -public class VAbsoluteLayoutPaintable extends VAbstractPaintableWidgetContainer { +public class VAbsoluteLayoutPaintable extends VAbstractPaintableWidgetContainer + implements CalculatingLayout { private LayoutClickEventHandler clickEventHandler = new LayoutClickEventHandler( this, EventId.LAYOUT_CLICK) { @@ -37,12 +41,10 @@ public class VAbsoluteLayoutPaintable extends VAbstractPaintableWidgetContainer @Override public void updateFromUIDL(UIDL uidl, ApplicationConnection client) { - getWidgetForPaintable().rendering = true; getWidgetForPaintable().client = client; // TODO margin handling super.updateFromUIDL(uidl, client); if (!isRealUpdate(uidl)) { - getWidgetForPaintable().rendering = false; return; } @@ -68,7 +70,6 @@ public class VAbsoluteLayoutPaintable extends VAbstractPaintableWidgetContainer getWidgetForPaintable().pidToComponentWrappper.remove(pid); absoluteWrapper.destroy(); } - getWidgetForPaintable().rendering = false; } public void updateCaption(VPaintableWidget component, UIDL uidl) { @@ -86,4 +87,67 @@ public class VAbsoluteLayoutPaintable extends VAbstractPaintableWidgetContainer public VAbsoluteLayout getWidgetForPaintable() { return (VAbsoluteLayout) super.getWidgetForPaintable(); } + + public void updateVerticalSizes() { + VAbsoluteLayout layout = getWidgetForPaintable(); + for (VPaintableWidget paintable : getChildren()) { + Widget widget = paintable.getWidgetForPaintable(); + AbsoluteWrapper wrapper = (AbsoluteWrapper) widget.getParent(); + Style wrapperStyle = wrapper.getElement().getStyle(); + + if (paintable.isRelativeHeight()) { + int h; + if (wrapper.top != null && wrapper.bottom != null) { + h = wrapper.getOffsetHeight(); + } else if (wrapper.bottom != null) { + // top not defined, available space 0... bottom of + // wrapper + h = wrapper.getElement().getOffsetTop() + + wrapper.getOffsetHeight(); + } else { + // top defined or both undefined, available space == + // canvas - top + h = layout.canvas.getOffsetHeight() + - wrapper.getElement().getOffsetTop(); + } + wrapperStyle.setHeight(h, Unit.PX); + } else { + wrapperStyle.clearHeight(); + } + + wrapper.updateCaptionPosition(); + } + } + + public void updateHorizontalSizes() { + VAbsoluteLayout layout = getWidgetForPaintable(); + for (VPaintableWidget paintable : getChildren()) { + Widget widget = paintable.getWidgetForPaintable(); + AbsoluteWrapper wrapper = (AbsoluteWrapper) widget.getParent(); + Style wrapperStyle = wrapper.getElement().getStyle(); + + if (paintable.isRelativeWidth()) { + int w; + if (wrapper.left != null && wrapper.right != null) { + w = wrapper.getOffsetWidth(); + } else if (wrapper.right != null) { + // left == null + // available width == right edge == offsetleft + width + w = wrapper.getOffsetWidth() + + wrapper.getElement().getOffsetLeft(); + } else { + // left != null && right == null || left == null && + // right == null + // available width == canvas width - offset left + w = layout.canvas.getOffsetWidth() + - wrapper.getElement().getOffsetLeft(); + } + wrapperStyle.setWidth(w, Unit.PX); + } else { + wrapperStyle.clearWidth(); + } + + wrapper.updateCaptionPosition(); + } + } } -- 2.39.5