From be23b014866160cb4bc3af8ed808c6f749270dce Mon Sep 17 00:00:00 2001 From: Anna Koskinen Date: Fri, 5 Feb 2021 14:55:00 +0200 Subject: Re-use ComputedStyles in Escalator when possible. (#12186) --- .../java/com/vaadin/client/widgets/Escalator.java | 39 ++++++++++++++++++++-- .../main/java/com/vaadin/client/widgets/Grid.java | 8 +---- 2 files changed, 38 insertions(+), 9 deletions(-) (limited to 'client') diff --git a/client/src/main/java/com/vaadin/client/widgets/Escalator.java b/client/src/main/java/com/vaadin/client/widgets/Escalator.java index a5046ce970..a9f8e0862c 100644 --- a/client/src/main/java/com/vaadin/client/widgets/Escalator.java +++ b/client/src/main/java/com/vaadin/client/widgets/Escalator.java @@ -7160,6 +7160,8 @@ public class Escalator extends Widget private final ElementPositionBookkeeper positions = new ElementPositionBookkeeper(); + private Map computedStyleMap = new HashMap<>(); + /** * Creates a new Escalator widget instance. */ @@ -7249,14 +7251,28 @@ public class Escalator extends Widget private double getBoundingWidth(Element element) { // Gets the current width, including border and padding, for the element // while ignoring any transforms applied to the element (e.g. scale) - return new ComputedStyle(element).getWidthIncludingBorderPadding(); + if (!computedStyleMap.containsKey(element)) { + if (computedStyleMap.isEmpty()) { + // ensure the next event loop calculates the sizes anew + Scheduler.get().scheduleDeferred(() -> clearComputedStyles()); + } + computedStyleMap.put(element, new ComputedStyle(element)); + } + return computedStyleMap.get(element).getWidthIncludingBorderPadding(); } private double getBoundingHeight(Element element) { // Gets the current height, including border and padding, for the // element while ignoring any transforms applied to the element (e.g. // scale) - return new ComputedStyle(element).getHeightIncludingBorderPadding(); + if (!computedStyleMap.containsKey(element)) { + if (computedStyleMap.isEmpty()) { + // ensure the next event loop calculates the sizes anew + Scheduler.get().scheduleDeferred(() -> clearComputedStyles()); + } + computedStyleMap.put(element, new ComputedStyle(element)); + } + return computedStyleMap.get(element).getHeightIncludingBorderPadding(); } private int getBodyRowCount() { @@ -8305,6 +8321,25 @@ public class Escalator extends Widget return getBoundingWidth(tableWrapper); } + /** + * Gets the escalator's inner height. This is the entire height in pixels, + * without the horizontal scrollbar. + * + * @return escalator's inner height + */ + public double getInnerHeight() { + return getBoundingHeight(tableWrapper); + } + + /** + * FOR INTERNAL USE ONLY, MAY GET REMOVED OR MODIFIED AT ANY TIME! + *

+ * Clears the computed styles. + */ + void clearComputedStyles() { + computedStyleMap.clear(); + } + /** * Resets all cached pixel sizes and reads new values from the DOM. This * methods should be used e.g. when styles affecting the dimensions of diff --git a/client/src/main/java/com/vaadin/client/widgets/Grid.java b/client/src/main/java/com/vaadin/client/widgets/Grid.java index d4c93751ea..99a4a269bd 100755 --- a/client/src/main/java/com/vaadin/client/widgets/Grid.java +++ b/client/src/main/java/com/vaadin/client/widgets/Grid.java @@ -77,7 +77,6 @@ import com.google.gwt.user.client.ui.PopupPanel; import com.google.gwt.user.client.ui.ResizeComposite; import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.BrowserInfo; -import com.vaadin.client.ComputedStyle; import com.vaadin.client.DeferredWorker; import com.vaadin.client.Focusable; import com.vaadin.client.WidgetUtil; @@ -3456,7 +3455,7 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, // Update latest width to prevent recalculate on height change. lastCalculatedInnerWidth = escalator.getInnerWidth(); - lastCalculatedInnerHeight = getEscalatorInnerHeight(); + lastCalculatedInnerHeight = escalator.getInnerHeight(); } private boolean columnsAreGuaranteedToBeWiderThanGrid() { @@ -9404,11 +9403,6 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, }); } - private double getEscalatorInnerHeight() { - return new ComputedStyle(getEscalator().getTableWrapper()) - .getHeightIncludingBorderPadding(); - } - /** * Grid does not support adding Widgets this way. *

-- cgit v1.2.3