diff options
author | Henrik Paul <henrik@vaadin.com> | 2014-12-08 01:02:29 +0200 |
---|---|---|
committer | Henrik Paul <henrik@vaadin.com> | 2014-12-12 09:44:00 +0200 |
commit | b3e6edc9634f444ccece00e200cb51eee7994d75 (patch) | |
tree | cec9735edc99ec2d0454914ff16fb0f68fa97aa9 /client/src/com/vaadin | |
parent | 9927297a94edcc96ac7a653dcdc1cd4fb91951e2 (diff) | |
download | vaadin-framework-b3e6edc9634f444ccece00e200cb51eee7994d75.tar.gz vaadin-framework-b3e6edc9634f444ccece00e200cb51eee7994d75.zip |
Columns can now have subpixel accuracy widths (#13334)
Change-Id: I1d16260be7b15c9fbdbfdd8f51e50e9f34e96272
Diffstat (limited to 'client/src/com/vaadin')
7 files changed, 94 insertions, 91 deletions
diff --git a/client/src/com/vaadin/client/ui/grid/ColumnConfiguration.java b/client/src/com/vaadin/client/ui/grid/ColumnConfiguration.java index 6c304ddaea..88f07e023f 100644 --- a/client/src/com/vaadin/client/ui/grid/ColumnConfiguration.java +++ b/client/src/com/vaadin/client/ui/grid/ColumnConfiguration.java @@ -123,7 +123,7 @@ public interface ColumnConfiguration { * @throws IllegalArgumentException * if <code>index</code> is not a valid column index */ - public void setColumnWidth(int index, int px) + public void setColumnWidth(int index, double px) throws IllegalArgumentException; /** @@ -136,7 +136,7 @@ public interface ColumnConfiguration { * @throws IllegalArgumentException * if <code>index</code> is not a valid column index */ - public int getColumnWidth(int index) throws IllegalArgumentException; + public double getColumnWidth(int index) throws IllegalArgumentException; /** * Returns the actual width of a column. @@ -147,7 +147,8 @@ public interface ColumnConfiguration { * @throws IllegalArgumentException * if <code>index</code> is not a valid column index */ - public int getColumnWidthActual(int index) throws IllegalArgumentException; + public double getColumnWidthActual(int index) + throws IllegalArgumentException; /** * Refreshes a range of rows in the current row containers in each Escalator diff --git a/client/src/com/vaadin/client/ui/grid/Escalator.java b/client/src/com/vaadin/client/ui/grid/Escalator.java index 1bbcaaf166..3ea7d94282 100644 --- a/client/src/com/vaadin/client/ui/grid/Escalator.java +++ b/client/src/com/vaadin/client/ui/grid/Escalator.java @@ -744,7 +744,7 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker */ public void recalculateScrollbarsForVirtualViewport() { int scrollContentHeight = body.calculateEstimatedTotalRowHeight(); - int scrollContentWidth = columnConfiguration.calculateRowWidth(); + double scrollContentWidth = columnConfiguration.calculateRowWidth(); double tableWrapperHeight = heightOfEscalator; double tableWrapperWidth = widthOfEscalator; @@ -789,11 +789,11 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker */ double prevScrollPos = horizontalScrollbar.getScrollPos(); - int unfrozenPixels = columnConfiguration + double unfrozenPixels = columnConfiguration .getCalculatedColumnsWidth(Range.between( columnConfiguration.getFrozenColumnCount(), columnConfiguration.getColumnCount())); - int frozenPixels = scrollContentWidth - unfrozenPixels; + double frozenPixels = scrollContentWidth - unfrozenPixels; double hScrollOffsetWidth = tableWrapperWidth - frozenPixels; horizontalScrollbar.setOffsetSize(hScrollOffsetWidth); horizontalScrollbar.setScrollSize(unfrozenPixels); @@ -1034,19 +1034,19 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker * structure effectively means that scrollLeft also ignores the * frozen columns. */ - final int frozenPixels = columnConfiguration + final double frozenPixels = columnConfiguration .getCalculatedColumnsWidth(Range.withLength(0, columnConfiguration.frozenColumns)); - final int targetStartPx = columnConfiguration + final double targetStartPx = columnConfiguration .getCalculatedColumnsWidth(Range.withLength(0, columnIndex)) - frozenPixels; - final int targetEndPx = targetStartPx + final double targetEndPx = targetStartPx + columnConfiguration.getColumnWidthActual(columnIndex); final double viewportStartPx = getScrollLeft(); double viewportEndPx = viewportStartPx - + getElement().getOffsetWidth() - frozenPixels; + + getPreciseWidth(getElement()) - frozenPixels; if (verticalScrollbar.showsScrollHandle()) { viewportEndPx -= Util.getNativeScrollbarSize(); } @@ -1399,7 +1399,7 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker tr.addClassName(getStylePrimaryName() + "-row"); for (int col = 0; col < columnConfiguration.getColumnCount(); col++) { - final int colWidth = columnConfiguration + final double colWidth = columnConfiguration .getColumnWidthActual(col); final TableCellElement cellElem = createCellElement( rowHeight, colWidth); @@ -1542,11 +1542,11 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker * @return a set-up empty cell element */ public TableCellElement createCellElement(final int height, - final int width) { + final double colWidth) { final TableCellElement cellElem = TableCellElement.as(DOM .createElement(getCellElementTagName())); cellElem.getStyle().setHeight(height, Unit.PX); - cellElem.getStyle().setWidth(width, Unit.PX); + cellElem.getStyle().setWidth(colWidth, Unit.PX); cellElem.addClassName(getStylePrimaryName() + "-cell"); return cellElem; } @@ -1640,7 +1640,7 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker final int rowHeight = getDefaultRowHeight(); for (FlyweightCell cell : cells) { - final int colWidth = columnConfiguration + final double colWidth = columnConfiguration .getColumnWidthActual(cell.getColumn()); final TableCellElement cellElem = createCellElement(rowHeight, colWidth); @@ -1706,16 +1706,16 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker * the index of the column to inspect * @return the pixel width of the widest element in the indicated column */ - public int calculateMaxColWidth(int index) { + public double calculateMaxColWidth(int index) { TableRowElement row = TableRowElement.as(root .getFirstChildElement()); - int maxWidth = 0; + double maxWidth = 0; while (row != null) { final TableCellElement cell = row.getCells().getItem(index); final boolean isVisible = !cell.getStyle().getDisplay() .equals(Display.NONE.getCssName()); if (isVisible) { - maxWidth = Math.max(maxWidth, cell.getScrollWidth()); + maxWidth = Math.max(maxWidth, getPreciseWidth(cell)); } row = TableRowElement.as(row.getNextSiblingElement()); } @@ -1732,8 +1732,8 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker Element cell = row.getFirstChildElement(); int columnIndex = 0; while (cell != null) { - final int width = getCalculatedColumnWidthWithColspan(cell, - columnIndex); + final double width = getCalculatedColumnWidthWithColspan( + cell, columnIndex); /* * TODO Should Escalator implement ProvidesResize at some @@ -1750,7 +1750,7 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker reapplyRowWidths(); } - private int getCalculatedColumnWidthWithColspan(final Element cell, + private double getCalculatedColumnWidthWithColspan(final Element cell, final int columnIndex) { final int colspan = cell.getPropertyInt(FlyweightCell.COLSPAN_ATTR); Range spannedColumns = Range.withLength(columnIndex, colspan); @@ -1775,7 +1775,7 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker * cells within. */ protected void reapplyRowWidths() { - int rowWidth = columnConfiguration.calculateRowWidth(); + double rowWidth = columnConfiguration.calculateRowWidth(); com.google.gwt.dom.client.Element row = root.getFirstChildElement(); while (row != null) { @@ -1957,8 +1957,8 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker return new Cell(domRowIndex, domColumnIndex, cellElement); } - int getMaxCellWidth(int colIndex) throws IllegalArgumentException { - int maxCellWidth = -1; + double getMaxCellWidth(int colIndex) throws IllegalArgumentException { + double maxCellWidth = -1; assert isAttached() : "Can't measure max width of cell, since Escalator is not attached to the DOM."; @@ -1988,7 +1988,7 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker cellClone.getStyle().clearWidth(); rowElement.insertBefore(cellClone, cellOriginal); - maxCellWidth = Math.max(cellClone.getOffsetWidth(), + maxCellWidth = Math.max(getPreciseWidth(cellClone), maxCellWidth); cellClone.removeFromParent(); } @@ -3684,8 +3684,8 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker public class Column { private static final int DEFAULT_COLUMN_WIDTH_PX = 100; - private int definedWidth = -1; - private int calculatedWidth = DEFAULT_COLUMN_WIDTH_PX; + private double definedWidth = -1; + private double calculatedWidth = DEFAULT_COLUMN_WIDTH_PX; private boolean measuringRequested = false; /** @@ -3695,7 +3695,7 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker */ private boolean widthHasBeenFinalized = false; - public void setWidth(int px) { + public void setWidth(double px) { definedWidth = px; if (px < 0) { @@ -3713,7 +3713,7 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker } } - public int getDefinedWidth() { + public double getDefinedWidth() { return definedWidth; } @@ -3723,7 +3723,7 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker * @return the width in pixels in the DOM. Returns -1 if the column * needs measuring, but has not been yet measured */ - public int getCalculatedWidth() { + public double getCalculatedWidth() { /* * This might return an untrue value (e.g. during init/onload), * since we haven't had a proper chance to actually calculate @@ -3777,7 +3777,7 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker * * @see #getCalculatedColumnWidths() */ - private int[] widthsArray = null; + private double[] widthsArray = null; /** * {@inheritDoc} @@ -3884,7 +3884,7 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker * * @return the width of a row, in pixels */ - public int calculateRowWidth() { + public double calculateRowWidth() { return getCalculatedColumnsWidth(Range.between(0, getColumnCount())); } @@ -3966,12 +3966,12 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker } // Adjust scrollbar - int pixelsToInsertedColumn = columnConfiguration + double pixelsToInsertedColumn = columnConfiguration .getCalculatedColumnsWidth(Range.withLength(0, index)); final boolean columnsWereAddedToTheLeftOfViewport = scroller.lastScrollLeft > pixelsToInsertedColumn; if (columnsWereAddedToTheLeftOfViewport) { - int insertedColumnsWidth = columnConfiguration + double insertedColumnsWidth = columnConfiguration .getCalculatedColumnsWidth(Range.withLength(index, numberOfColumns)); horizontalScrollbar.setScrollPos(scroller.lastScrollLeft @@ -4042,7 +4042,7 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker } @Override - public void setColumnWidth(int index, int px) + public void setColumnWidth(int index, double px) throws IllegalArgumentException { checkValidColumnIndex(index); @@ -4069,25 +4069,25 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker } @Override - public int getColumnWidth(int index) throws IllegalArgumentException { + public double getColumnWidth(int index) throws IllegalArgumentException { checkValidColumnIndex(index); return columns.get(index).getDefinedWidth(); } @Override - public int getColumnWidthActual(int index) { + public double getColumnWidthActual(int index) { return columns.get(index).getCalculatedWidth(); } - private int getMaxCellWidth(int colIndex) + private double getMaxCellWidth(int colIndex) throws IllegalArgumentException { - int headerWidth = header.getMaxCellWidth(colIndex); - int bodyWidth = body.getMaxCellWidth(colIndex); - int footerWidth = footer.getMaxCellWidth(colIndex); + double headerWidth = header.getMaxCellWidth(colIndex); + double bodyWidth = body.getMaxCellWidth(colIndex); + double footerWidth = footer.getMaxCellWidth(colIndex); - int maxWidth = Math.max(headerWidth, + double maxWidth = Math.max(headerWidth, Math.max(bodyWidth, footerWidth)); - assert maxWidth > 0 : "Got a negative max width for a column, which should be impossible."; + assert maxWidth >= 0 : "Got a negative max width for a column, which should be impossible."; return maxWidth; } @@ -4099,7 +4099,7 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker * @return the total width of the columns in the given * <code>columns</code> */ - int getCalculatedColumnsWidth(final Range columns) { + double getCalculatedColumnsWidth(final Range columns) { /* * This is an assert instead of an exception, since this is an * internal method. @@ -4110,17 +4110,17 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker + ", but was given :" + columns; - int sum = 0; + double sum = 0; for (int i = columns.getStart(); i < columns.getEnd(); i++) { - int columnWidthActual = getColumnWidthActual(i); + double columnWidthActual = getColumnWidthActual(i); sum += columnWidthActual; } return sum; } - int[] getCalculatedColumnWidths() { + double[] getCalculatedColumnWidths() { if (widthsArray == null || widthsArray.length != getColumnCount()) { - widthsArray = new int[getColumnCount()]; + widthsArray = new double[getColumnCount()]; for (int i = 0; i < columns.size(); i++) { widthsArray[i] = columns.get(i).getCalculatedWidth(); } diff --git a/client/src/com/vaadin/client/ui/grid/FlyweightCell.java b/client/src/com/vaadin/client/ui/grid/FlyweightCell.java index adcca1b630..fe826b16c3 100644 --- a/client/src/com/vaadin/client/ui/grid/FlyweightCell.java +++ b/client/src/com/vaadin/client/ui/grid/FlyweightCell.java @@ -169,8 +169,8 @@ public class FlyweightCell { final int cellsToTheRight = currentIterator.rawPeekNext( numberOfCells - 1).size(); - final int selfWidth = row.getColumnWidth(column); - int widthsOfColumnsToTheRight = 0; + final double selfWidth = row.getColumnWidth(column); + double widthsOfColumnsToTheRight = 0; for (int i = 0; i < cellsToTheRight; i++) { widthsOfColumnsToTheRight += row.getColumnWidth(column + i + 1); } diff --git a/client/src/com/vaadin/client/ui/grid/FlyweightRow.java b/client/src/com/vaadin/client/ui/grid/FlyweightRow.java index 0e9c6ad955..9f913f5cd1 100644 --- a/client/src/com/vaadin/client/ui/grid/FlyweightRow.java +++ b/client/src/com/vaadin/client/ui/grid/FlyweightRow.java @@ -140,10 +140,10 @@ class FlyweightRow implements Row { private int row; private TableRowElement element; - private int[] columnWidths = null; + private double[] columnWidths = null; private final List<FlyweightCell> cells = new ArrayList<FlyweightCell>(); - void setup(final TableRowElement e, final int row, int[] columnWidths) { + void setup(final TableRowElement e, final int row, double[] columnWidths) { element = e; this.row = row; this.columnWidths = columnWidths; @@ -285,7 +285,7 @@ class FlyweightRow implements Row { + "has been stored and accessed."; } - int getColumnWidth(int column) { + double getColumnWidth(int column) { assertSetup(); return columnWidths[column]; } diff --git a/client/src/com/vaadin/client/ui/grid/Grid.java b/client/src/com/vaadin/client/ui/grid/Grid.java index c6d7e22d3b..80ecc9a9e9 100644 --- a/client/src/com/vaadin/client/ui/grid/Grid.java +++ b/client/src/com/vaadin/client/ui/grid/Grid.java @@ -1875,7 +1875,7 @@ public class Grid<T> extends ResizeComposite implements } @Override - public GridColumn<Boolean, T> setWidth(int pixels) { + public GridColumn<Boolean, T> setWidth(double pixels) { if (pixels != getWidth() && initDone) { throw new UnsupportedOperationException("The selection " + "column cannot be modified after init"); @@ -2208,8 +2208,11 @@ public class Grid<T> extends ResizeComposite implements */ private boolean visible = true; - /** Width of column in pixels as {@link #setWidth(int)} has been called */ - private int widthUser = GridColumnState.DEFAULT_COLUMN_WIDTH_PX; + /** + * Width of column in pixels as {@link #setWidth(double)} has been + * called + */ + private double widthUser = GridColumnState.DEFAULT_COLUMN_WIDTH_PX; /** * Renderer for rendering a value into the cell @@ -2387,7 +2390,7 @@ public class Grid<T> extends ResizeComposite implements * the width in pixels or negative for auto sizing * @return the column itself */ - public GridColumn<C, T> setWidth(int pixels) { + public GridColumn<C, T> setWidth(double pixels) { widthUser = pixels; if (pixels < 0) { setWidthAutodetect(); @@ -2409,14 +2412,14 @@ public class Grid<T> extends ResizeComposite implements */ } - private void setWidthAbsolute(int pixels) { + private void setWidthAbsolute(double pixels) { asyncAutodetectWidth.stop(); if (grid != null) { setWidthForce(pixels); } } - private void setWidthForce(int pixels) { + private void setWidthForce(double pixels) { int index = grid.columns.indexOf(this); ColumnConfiguration conf = grid.escalator.getColumnConfiguration(); conf.setColumnWidth(index, pixels); @@ -2426,14 +2429,14 @@ public class Grid<T> extends ResizeComposite implements * Returns the pixel width of the column as given by the user. * <p> * <em>Note:</em> If a negative value was given to - * {@link #setWidth(int)}, that same negative value is returned here. + * {@link #setWidth(double)}, that same negative value is returned here. * * @return pixel width of the column, or a negative number if the column * width has been automatically calculated. - * @see #setWidth(int) + * @see #setWidth(double) * @see #getWidthActual() */ - public int getWidth() { + public double getWidth() { return widthUser; } @@ -2445,7 +2448,7 @@ public class Grid<T> extends ResizeComposite implements * * @return pixel width of the column. */ - public int getWidthActual() { + public double getWidthActual() { return grid.escalator.getColumnConfiguration() .getColumnWidthActual(grid.columns.indexOf(this)); } diff --git a/client/src/com/vaadin/client/ui/grid/GridUtil.java b/client/src/com/vaadin/client/ui/grid/GridUtil.java index 0eed0e98b5..8dc0822d9d 100644 --- a/client/src/com/vaadin/client/ui/grid/GridUtil.java +++ b/client/src/com/vaadin/client/ui/grid/GridUtil.java @@ -16,7 +16,6 @@ package com.vaadin.client.ui.grid; import com.google.gwt.dom.client.Element; -import com.google.gwt.user.client.ui.Composite; import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.Util; @@ -29,8 +28,17 @@ import com.vaadin.client.Util; public class GridUtil { /** + * The allowed value inaccuracy when comparing two double-typed pixel + * values. + * <p> + * Since we're comparing pixels on a screen, epsilon must be less than 1. + * 0.49 was deemed a perfectly fine and beautifully round number. + */ + public static final double PIXEL_EPSILON = 0.49d; + + /** * Returns the cell the given element belongs to. - * + * * @param grid * the grid instance that is queried * @param e @@ -78,4 +86,17 @@ public class GridUtil { widget.@com.google.gwt.user.client.ui.Widget::setParent(Lcom/google/gwt/user/client/ui/Widget;)(parent); }-*/; + /** + * Compares two double values with the error margin of + * {@link #PIXEL_EPSILON} (i.e. {@value #PIXEL_EPSILON}) + * + * @param num1 + * the first value for which to compare equality + * @param num2 + * the second value for which to compare equality + */ + public static boolean pixelValuesEqual(final double num1, final double num2) { + return Math.abs(num1 - num2) <= PIXEL_EPSILON; + } + } diff --git a/client/src/com/vaadin/client/ui/grid/ScrollbarBundle.java b/client/src/com/vaadin/client/ui/grid/ScrollbarBundle.java index a2df48e6c6..7d6d050e64 100644 --- a/client/src/com/vaadin/client/ui/grid/ScrollbarBundle.java +++ b/client/src/com/vaadin/client/ui/grid/ScrollbarBundle.java @@ -179,15 +179,6 @@ abstract class ScrollbarBundle { private static final int OSX_INVISIBLE_SCROLLBAR_FAKE_SIZE_PX = 13; /** - * The allowed value inaccuracy when comparing two double-typed pixel - * values. - * <p> - * Since we're comparing pixels on a screen, epsilon must be less than 1. - * 0.49 was deemed a perfectly fine and beautifully round number. - */ - private static final double PIXEL_EPSILON = 0.49d; - - /** * A representation of a single vertical scrollbar. * * @see VerticalScrollbarBundle#getElement() @@ -211,7 +202,7 @@ abstract class ScrollbarBundle { } @Override - protected void internalSetScrollSize(int px) { + protected void internalSetScrollSize(double px) { scrollSizeElement.getStyle().setHeight(px, Unit.PX); } @@ -280,7 +271,7 @@ abstract class ScrollbarBundle { } @Override - protected void internalSetScrollSize(int px) { + protected void internalSetScrollSize(double px) { scrollSizeElement.getStyle().setWidth(px, Unit.PX); } @@ -451,7 +442,7 @@ abstract class ScrollbarBundle { double oldScrollPos = scrollPos; scrollPos = Math.max(0, Math.min(maxScrollPos, truncate(px))); - if (!pixelValuesEqual(oldScrollPos, scrollPos)) { + if (!GridUtil.pixelValuesEqual(oldScrollPos, scrollPos)) { if (isInvisibleScrollbar) { invisibleScrollbarTemporaryResizer.show(); } @@ -533,7 +524,7 @@ abstract class ScrollbarBundle { * the new size of {@link #scrollSizeElement} in the dimension * this scrollbar is representing */ - protected abstract void internalSetScrollSize(int px); + protected abstract void internalSetScrollSize(double px); /** * Sets the amount of pixels the scrollbar needs to be able to scroll @@ -549,7 +540,7 @@ abstract class ScrollbarBundle { * through */ public final void setScrollSize(double px) { - internalSetScrollSize(toInt32(Math.max(0, truncate(px)))); + internalSetScrollSize(Math.max(0, px)); forceScrollbar(showsScrollHandle()); recalculateMaxScrollPos(); fireVisibilityChangeIfNeeded(); @@ -719,19 +710,6 @@ abstract class ScrollbarBundle { }-*/; /** - * Compares two double values with the error margin of - * {@link #PIXEL_EPSILON} (i.e. {@value #PIXEL_EPSILON}) - * - * @param num1 - * the first value for which to compare equality - * @param num2 - * the second value for which to compare equality - */ - private static boolean pixelValuesEqual(final double num1, final double num2) { - return Math.abs(num1 - num2) <= PIXEL_EPSILON; - } - - /** * Locks or unlocks the scrollbar bundle. * <p> * A locked scrollbar bundle will refuse to scroll, both programmatically |