Skip to content

Commit

Permalink
Adjust column reorder drop marker when auto scrolling grid. (#16643)
Browse files Browse the repository at this point in the history
Change-Id: I11377fb8b007c3c50c3e3bd1c29afcf8485431f0
  • Loading branch information
pleku committed Feb 25, 2015
1 parent 6db686c commit c2fdfca
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 29 deletions.
75 changes: 57 additions & 18 deletions client/src/com/vaadin/client/widget/grid/AutoScroller.java
Expand Up @@ -43,28 +43,34 @@ public class AutoScroller {
/** /**
* Callback that notifies when the cursor is on top of a new row or column * Callback that notifies when the cursor is on top of a new row or column
* because of the automatic scrolling. * because of the automatic scrolling.
*
* @since
*/ */
public interface AutoScrollerCallback { public interface AutoScrollerCallback {


/** /**
* Triggered when doing automatic horizontal scrolling. * Triggered when doing automatic scrolling.
* <p>
* Because the auto scroller currently only supports scrolling in one
* axis, this method is used for both vertical and horizontal scrolling.
* *
* @param scrollDiff * @param scrollDiff
* the amount of pixels that have been auto scrolled since * the amount of pixels that have been auto scrolled since
* last call * last call
*/ */
void onHorizontalAutoScroll(int scrollDiff); void onAutoScroll(int scrollDiff);


/** /**
* Triggered when doing automatic vertical scrolling. * Triggered when the grid scroll has reached the minimum scroll
* * position. Depending on the scroll axis, either scrollLeft or
* @param scrollDiff * scrollTop is 0.
* the amount of pixels that have been auto scrolled since
* last call
*/ */
void onVerticalAutoScroll(int scrollDiff); void onAutoScrollReachedMin();

/**
* Triggered when the grid scroll has reached the max scroll position.
* Depending on the scroll axis, either scrollLeft or scrollTop is at
* its maximum value.
*/
void onAutoScrollReachedMax();
} }


public enum ScrollAxis { public enum ScrollAxis {
Expand Down Expand Up @@ -229,12 +235,30 @@ public void execute(final double timestamp) {
pixelsToScroll -= intPixelsToScroll; pixelsToScroll -= intPixelsToScroll;


if (intPixelsToScroll != 0) { if (intPixelsToScroll != 0) {
double scrollPos;
double maxScrollPos;
double newScrollPos;
if (scrollDirection == ScrollAxis.VERTICAL) { if (scrollDirection == ScrollAxis.VERTICAL) {
grid.setScrollTop(grid.getScrollTop() + intPixelsToScroll); scrollPos = grid.getScrollTop();
callback.onVerticalAutoScroll(intPixelsToScroll); maxScrollPos = getMaxScrollTop();
} else { } else {
grid.setScrollLeft(grid.getScrollLeft() + intPixelsToScroll); scrollPos = grid.getScrollLeft();
callback.onHorizontalAutoScroll(intPixelsToScroll); maxScrollPos = getMaxScrollLeft();
}
if (intPixelsToScroll > 0 && scrollPos < maxScrollPos
|| intPixelsToScroll < 0 && scrollPos > 0) {
newScrollPos = scrollPos + intPixelsToScroll;
if (scrollDirection == ScrollAxis.VERTICAL) {
grid.setScrollTop(newScrollPos);
} else {
grid.setScrollLeft(newScrollPos);
}
callback.onAutoScroll(intPixelsToScroll);
if (newScrollPos <= 0) {
callback.onAutoScrollReachedMin();
} else if (newScrollPos >= maxScrollPos) {
callback.onAutoScrollReachedMax();
}
} }
} }


Expand Down Expand Up @@ -521,10 +545,7 @@ private void start(final NativeEvent event) {
private void updateScrollBounds() { private void updateScrollBounds() {
double startBorder = getBodyClientStart(); double startBorder = getBodyClientStart();
final int endBorder = getBodyClientEnd(); final int endBorder = getBodyClientEnd();

startBorder += getFrozenColumnsWidth();
for (int i = 0; i < grid.getFrozenColumnCount(); i++) {
startBorder += grid.getColumn(i).getWidthActual();
}


final int scrollCompensation = getScrollCompensation(); final int scrollCompensation = getScrollCompensation();
startingBound = scrollCompensation + startBorder + scrollAreaPX; startingBound = scrollCompensation + startBorder + scrollAreaPX;
Expand Down Expand Up @@ -646,4 +667,22 @@ private int getBodyClientStart() {
return getClientLeft(getTbodyElement()); return getClientLeft(getTbodyElement());
} }
} }

private double getFrozenColumnsWidth() {
double value = 0;
for (int i = 0; i < grid.getFrozenColumnCount(); i++) {
value += grid.getColumn(i).getWidthActual();
}
return value;
}

private double getMaxScrollLeft() {
return grid.getScrollWidth()
- (getTableElement().getParentElement().getOffsetWidth() - getFrozenColumnsWidth());
}

private double getMaxScrollTop() {
return grid.getScrollHeight() - getTfootElement().getOffsetHeight()
- getTheadElement().getOffsetHeight();
}
} }
22 changes: 22 additions & 0 deletions client/src/com/vaadin/client/widgets/Escalator.java
Expand Up @@ -4713,6 +4713,28 @@ public void setScrollLeft(final double scrollLeft) {
horizontalScrollbar.setScrollPos(scrollLeft); horizontalScrollbar.setScrollPos(scrollLeft);
} }


/**
* Returns the scroll width for the escalator. Note that this is not
* necessary the same as {@code Element.scrollWidth} in the DOM.
*
* @since
* @return the scroll width in pixels
*/
public double getScrollWidth() {
return horizontalScrollbar.getScrollSize();
}

/**
* Returns the scroll height for the escalator. Note that this is not
* necessary the same as {@code Element.scrollHeight} in the DOM.
*
* @since
* @return the scroll height in pixels
*/
public double getScrollHeight() {
return verticalScrollbar.getScrollSize();
}

/** /**
* Scrolls the body horizontally so that the column at the given index is * Scrolls the body horizontally so that the column at the given index is
* visible and there is at least {@code padding} pixels in the direction of * visible and there is at least {@code padding} pixels in the direction of
Expand Down
50 changes: 39 additions & 11 deletions client/src/com/vaadin/client/widgets/Grid.java
Expand Up @@ -2875,13 +2875,23 @@ public boolean isScheduled() {
private final AutoScrollerCallback autoScrollerCallback = new AutoScrollerCallback() { private final AutoScrollerCallback autoScrollerCallback = new AutoScrollerCallback() {


@Override @Override
public void onVerticalAutoScroll(int scrollDiff) { public void onAutoScroll(int scrollDiff) {
// NOP autoScrollX = scrollDiff;
onDragUpdate(null);
} }


@Override @Override
public void onHorizontalAutoScroll(int scrollDiff) { public void onAutoScrollReachedMin() {
onDragUpdate(null); // make sure the drop marker is visible on the left
autoScrollX = 0;
updateDragDropMarker(clientX);
}

@Override
public void onAutoScrollReachedMax() {
// make sure the drop marker is visible on the right
autoScrollX = 0;
updateDragDropMarker(clientX);
} }
}; };
/** /**
Expand All @@ -2903,6 +2913,9 @@ public void onHorizontalAutoScroll(int scrollDiff) {


private int clientX; private int clientX;


/** How much the grid is being auto scrolled while dragging. */
private int autoScrollX;

private void initHeaderDragElementDOM() { private void initHeaderDragElementDOM() {
if (table == null) { if (table == null) {
tableHeader = DOM.createTHead(); tableHeader = DOM.createTHead();
Expand All @@ -2925,6 +2938,7 @@ public void onDragUpdate(NativePreviewEvent event) {
if (event != null) { if (event != null) {
clientX = WidgetUtil.getTouchOrMouseClientX(event clientX = WidgetUtil.getTouchOrMouseClientX(event
.getNativeEvent()); .getNativeEvent());
autoScrollX = 0;
} }
resolveDragElementHorizontalPosition(clientX); resolveDragElementHorizontalPosition(clientX);
updateDragDropMarker(clientX); updateDragDropMarker(clientX);
Expand All @@ -2946,6 +2960,7 @@ private void updateDragDropMarker(final int clientX) {
dropMarkerLeft += cellWidth; dropMarkerLeft += cellWidth;
} }
} }
dropMarkerLeft += autoScrollX;
if (dropMarkerLeft > header.getElement().getOffsetWidth() if (dropMarkerLeft > header.getElement().getOffsetWidth()
|| dropMarkerLeft < 0) { || dropMarkerLeft < 0) {
dropMarkerLeft = -10000000; dropMarkerLeft = -10000000;
Expand Down Expand Up @@ -3014,10 +3029,6 @@ private void transferCellFocusOnDrop() {
final int focusedRowIndex = focusedCell.getRow(); final int focusedRowIndex = focusedCell.getRow();
final int draggedColumnIndex = eventCell.getColumnIndex(); final int draggedColumnIndex = eventCell.getColumnIndex();
// transfer focus if it was effected by the new column order // transfer focus if it was effected by the new column order
// FIXME if the dragged column is partly outside of the view
// port and the focused cell is +-1 of the dragged column, the
// grid scrolls to the right end. maybe fixed when the automatic
// scroll handling is implemented?
final RowContainer rowContainer = escalator final RowContainer rowContainer = escalator
.findRowContainer(focusedCell.getElement()); .findRowContainer(focusedCell.getElement());
if (focusedCellColumnIndex == draggedColumnIndex) { if (focusedCellColumnIndex == draggedColumnIndex) {
Expand All @@ -3032,9 +3043,6 @@ private void transferCellFocusOnDrop() {
&& draggedColumnIndex < focusedCellColumnIndex) { && draggedColumnIndex < focusedCellColumnIndex) {
cellFocusHandler.setCellFocus(focusedRowIndex, cellFocusHandler.setCellFocus(focusedRowIndex,
focusedCellColumnIndex - 1, rowContainer); focusedCellColumnIndex - 1, rowContainer);
} else {
cellFocusHandler.setCellFocus(focusedRowIndex,
focusedCellColumnIndex, rowContainer);
} }
} }
} }
Expand Down Expand Up @@ -5022,6 +5030,26 @@ public double getScrollLeft() {
return escalator.getScrollLeft(); return escalator.getScrollLeft();
} }


/**
* Returns the height of the scrollable area in pixels.
*
* @since
* @return the height of the scrollable area in pixels
*/
public double getScrollHeight() {
return escalator.getScrollHeight();
}

/**
* Returns the width of the scrollable area in pixels.
*
* @since
* @return the width of the scrollable area in pixels.
*/
public double getScrollWidth() {
return escalator.getScrollWidth();
}

private static final Logger getLogger() { private static final Logger getLogger() {
return Logger.getLogger(Grid.class.getName()); return Logger.getLogger(Grid.class.getName());
} }
Expand Down

0 comments on commit c2fdfca

Please sign in to comment.