Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add column width recalculation when vertical scrollbar hidden/shown. #12058

Merged
merged 1 commit into from Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,94 @@
/*
* Copyright 2000-2018 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
*
* 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.vaadin.client.widget.grid.events;

import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.GwtEvent;

/**
* FOR INTERNAL USE ONLY, MAY GET REMOVED OR MODIFIED AT ANY TIME!
* <p>
* Event handler that gets notified when the visibility of the vertical
* scrollbar of the Escalator changes.
*
* @author Vaadin Ltd
*/
public interface VerticalScrollbarVisibilityChangeHandler
extends EventHandler {

/**
* FOR INTERNAL USE ONLY, MAY GET REMOVED OR MODIFIED AT ANY TIME!
* <p>
* Called when the visibility of the vertical scrollbar of the Escalator
* changes.
*
* @param event
* the row visibility change event describing the change
*/
void onVisibilityChange(
VerticalScrollbarVisibilityChangeEvent event);

/**
* FOR INTERNAL USE ONLY, MAY GET REMOVED OR MODIFIED AT ANY TIME!
* <p>
* Event fired when the visibility of the vertical scrollbar of the
* Escalator changes.
*
* @author Vaadin Ltd
*/
public class VerticalScrollbarVisibilityChangeEvent extends
GwtEvent<VerticalScrollbarVisibilityChangeHandler> {
/**
* FOR INTERNAL USE ONLY, MAY GET REMOVED OR MODIFIED AT ANY TIME!
* <p>
* The type of this event.
*/
public static final Type<VerticalScrollbarVisibilityChangeHandler> TYPE = new Type<>();

/**
* FOR INTERNAL USE ONLY, MAY GET REMOVED OR MODIFIED AT ANY TIME!
* <p>
* Creates a new Escalator vertical scrollbar visibility change event.
*
*/
public VerticalScrollbarVisibilityChangeEvent() {
// NOP
}

/*
* (non-Javadoc)
*
* @see com.google.gwt.event.shared.GwtEvent#getAssociatedType()
*/
@Override
public Type<VerticalScrollbarVisibilityChangeHandler> getAssociatedType() {
return TYPE;
}

/*
* (non-Javadoc)
*
* @see
* com.google.gwt.event.shared.GwtEvent#dispatch(com.google.gwt.event.
* shared .EventHandler)
*/
@Override
protected void dispatch(
VerticalScrollbarVisibilityChangeHandler handler) {
handler.onVisibilityChange(this);
}
}
}
45 changes: 45 additions & 0 deletions client/src/main/java/com/vaadin/client/widgets/Escalator.java
Expand Up @@ -98,6 +98,8 @@
import com.vaadin.client.widget.grid.events.EscalatorSizeChangeHandler.EscalatorSizeChangeEvent;
import com.vaadin.client.widget.grid.events.ScrollEvent;
import com.vaadin.client.widget.grid.events.ScrollHandler;
import com.vaadin.client.widget.grid.events.VerticalScrollbarVisibilityChangeHandler;
import com.vaadin.client.widget.grid.events.VerticalScrollbarVisibilityChangeHandler.VerticalScrollbarVisibilityChangeEvent;
import com.vaadin.client.widgets.Escalator.JsniUtil.TouchHandlerBundle;
import com.vaadin.shared.Range;
import com.vaadin.shared.ui.grid.HeightMode;
Expand Down Expand Up @@ -7291,6 +7293,29 @@ private void setupScrollbars(final Element root) {
root.appendChild(verticalScrollbar.getElement());
verticalScrollbar.addScrollHandler(scrollHandler);
verticalScrollbar.setScrollbarThickness(scrollbarThickness);
verticalScrollbar
.addVisibilityHandler(new ScrollbarBundle.VisibilityHandler() {

private boolean queued = false;

@Override
public void visibilityChanged(
ScrollbarBundle.VisibilityChangeEvent event) {
if (queued) {
return;
}
queued = true;

/*
* We either lost or gained a scrollbar. In either case,
* we may need to update the column widths.
*/
Scheduler.get().scheduleFinally(() -> {
fireVerticalScrollbarVisibilityChangeEvent();
queued = false;
});
}
});

root.appendChild(horizontalScrollbar.getElement());
horizontalScrollbar.addScrollHandler(scrollHandler);
Expand Down Expand Up @@ -7873,6 +7898,26 @@ private static double[] snapDeltas(final double deltaX, final double deltaY,
return array;
}

/**
* FOR INTERNAL USE ONLY, MAY GET REMOVED OR MODIFIED AT ANY TIME!
* <p>
* Adds an event handler that gets notified when the visibility of the
* vertical scrollbar changes.
*
* @param verticalScrollbarVisibilityChangeHandler
* the event handler
* @return a handler registration for the added handler
*/
public HandlerRegistration addVerticalScrollbarVisibilityChangeHandler(
VerticalScrollbarVisibilityChangeHandler verticalScrollbarVisibilityChangeHandler) {
return addHandler(verticalScrollbarVisibilityChangeHandler,
VerticalScrollbarVisibilityChangeEvent.TYPE);
}

private void fireVerticalScrollbarVisibilityChangeEvent() {
fireEvent(new VerticalScrollbarVisibilityChangeEvent());
}

/**
* FOR INTERNAL USE ONLY, MAY GET REMOVED OR MODIFIED AT ANY TIME!
* <p>
Expand Down
14 changes: 11 additions & 3 deletions client/src/main/java/com/vaadin/client/widgets/Grid.java
Expand Up @@ -4294,6 +4294,7 @@ private void removeColumnHidingToggle(Column<?, T> column) {
*/
private DataSource<T> dataSource;
private Registration changeHandler;
private boolean recalculateColumnWidthsNeeded = false;

/**
* Currently available row range in DataSource.
Expand Down Expand Up @@ -5319,8 +5320,7 @@ private void setHidden(boolean hidden, boolean userOriginated) {
} else {
this.hidden = hidden;

int columnIndex = grid.getVisibleColumns()
.indexOf(this);
int columnIndex = grid.getVisibleColumns().indexOf(this);
grid.escalator.getColumnConfiguration()
.insertColumns(columnIndex, 1);

Expand Down Expand Up @@ -6408,6 +6408,15 @@ public Grid() {
}
});

escalator.addVerticalScrollbarVisibilityChangeHandler(event -> {
if (!(currentDataAvailable.isEmpty()
&& escalator.getBody().getRowCount() > 0)) {
recalculateColumnWidths();
} else {
recalculateColumnWidthsNeeded = true;
}
});

// Default action on SelectionEvents. Refresh the body so changed
// become visible.
addSelectionHandler(new SelectionHandler<T>() {
Expand Down Expand Up @@ -7247,7 +7256,6 @@ public void setDataSource(final DataSource<T> dataSource)
this.dataSource = dataSource;
changeHandler = dataSource
.addDataChangeHandler(new DataChangeHandler() {
private boolean recalculateColumnWidthsNeeded = false;

@Override
public void dataUpdated(int firstIndex, int numberOfItems) {
Expand Down
Expand Up @@ -26,7 +26,7 @@ public class GridSizeChange extends AbstractTestUI {
protected void setup(VaadinRequest request) {
grid = new Grid<>();
data = new ArrayList<>();
for (int i = 0; i < 10; ++i) {
for (int i = 0; i < 8; ++i) {
data.add(i);
++counter;
}
Expand All @@ -39,8 +39,8 @@ protected void setup(VaadinRequest request) {

// set height mode and height
grid.setHeightMode(HeightMode.ROW);
grid.setHeightByRows(10);
grid.setWidth(90, Unit.PIXELS);
grid.setHeightByRows(8);
grid.setWidth(100, Unit.PIXELS);

// create a tabsheet with one tab and place grid inside
VerticalLayout tab = new VerticalLayout();
Expand Down
Expand Up @@ -25,7 +25,7 @@ public void verifyUserAgent() {
// Chrome version does not necessarily match the desired version
// because of auto updates...
browserIdentifier = getExpectedUserAgentString(
getDesiredCapabilities()) + "83";
getDesiredCapabilities()) + "84";
} else if (BrowserUtil.isFirefox(getDesiredCapabilities())) {
browserIdentifier = getExpectedUserAgentString(
getDesiredCapabilities()) + "75";
Expand Down
Expand Up @@ -41,36 +41,42 @@ public void scrollbarsTakenIntoAccountInSizeChanges() {

assertGridWithinTabSheet();
ensureVerticalScrollbar(true);
ensureHorizontalScrollbar(false);

$(ButtonElement.class).caption("Remove row").first().click();
// height matches rows -> no scrollbar

assertGridWithinTabSheet();
ensureVerticalScrollbar(false);
ensureHorizontalScrollbar(false);

$(ButtonElement.class).caption("Reduce width").first().click();
// column too wide -> scrollbar

assertGridWithinTabSheet();
ensureVerticalScrollbar(false);
ensureHorizontalScrollbar(true);

$(ButtonElement.class).caption("Increase width").first().click();
// column fits -> no scrollbar

assertGridWithinTabSheet();
ensureVerticalScrollbar(false);
ensureHorizontalScrollbar(false);

$(ButtonElement.class).caption("Add row").first().click();
// more rows than height -> scrollbar

assertGridWithinTabSheet();
ensureVerticalScrollbar(true);
ensureHorizontalScrollbar(false);

$(ButtonElement.class).caption("Increase height").first().click();
// height matches rows -> no scrollbar

assertGridWithinTabSheet();
ensureVerticalScrollbar(false);
ensureHorizontalScrollbar(false);
}

private void ensureVerticalScrollbar(boolean displayed) {
Expand Down