Browse Source

Fixes the handling of the scroll position of a Window (#12736)

After the first commit the same fix has been applied also for panels, in
addition to other suggested changes.

The scroll position of a Window is now memorized before applying
the fix for bug #11994. The position is restored after the fix.

Because the scrolling issue is known to appear also in other components,
the fix for the scrolling has been moved to the Util class.

Change-Id: I5251011b5bede77a7fb18972e1d90016c0eccc23
tags/7.1.8
Mika Murtojarvi 10 years ago
parent
commit
017bd0684c

+ 55
- 0
client/src/com/vaadin/client/Util.java View File

@@ -32,6 +32,7 @@ import com.google.gwt.dom.client.Node;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.dom.client.Touch;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DOM;
@@ -517,6 +518,60 @@ public class Util {

}

/**
* Prevents some browsers from adding scroll bars to a component (such as a
* Window) whose contents fit in the component.
* <p>
* See: bugs #11994 and #12736.
*
* @param contentNode
* an element that contains a scrollable element as its first
* child
*
* @since 7.1.8
*/
public static void removeUnneededScrollbars(final Element contentNode) {
if (BrowserInfo.get().isWebkit()) {

/*
* Shake up the DOM a bit to make the window shed unnecessary
* scrollbars and resize correctly afterwards. This resulting code
* took over a week to summon forth, and involved some pretty hairy
* black magic. Don't touch it unless you know what you're doing!
* Fixes ticket #11994. Later modified to fix ticket #12736.
*/
Scheduler.get().scheduleFinally(new ScheduledCommand() {

@Override
public void execute() {
final com.google.gwt.dom.client.Element scrollable = contentNode
.getFirstChildElement();

// Adjusting the width or height may change the scroll
// position, so store the current position
int horizontalScrollPosition = scrollable.getScrollLeft();
int verticalScrollPosition = scrollable.getScrollTop();

final String oldWidth = scrollable.getStyle().getWidth();
final String oldHeight = scrollable.getStyle().getHeight();

scrollable.getStyle().setWidth(110, Unit.PCT);
scrollable.getStyle().setHeight(110, Unit.PCT);
scrollable.getOffsetWidth();
scrollable.getOffsetHeight();
scrollable.getStyle().setProperty("width", oldWidth);
scrollable.getStyle().setProperty("height", oldHeight);

// Restore the scroll position
scrollable.setScrollLeft(horizontalScrollPosition);
scrollable.setScrollTop(verticalScrollPosition);

}
});

}
}

/**
* Parses shared state and fetches the relative size of the component. If a
* dimension is not specified as relative it will return -1. If the shared

+ 2
- 42
client/src/com/vaadin/client/ui/VPanel.java View File

@@ -16,18 +16,15 @@

package com.vaadin.client.ui;

import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.SimplePanel;
import com.vaadin.client.ApplicationConnection;
import com.vaadin.client.BrowserInfo;
import com.vaadin.client.Focusable;
import com.vaadin.client.Util;
import com.vaadin.client.ui.ShortcutActionHandler.ShortcutActionHandlerOwner;
import com.vaadin.client.ui.TouchScrollDelegate.TouchScrollHandler;

@@ -210,43 +207,6 @@ public class VPanel extends SimplePanel implements ShortcutActionHandlerOwner,
touchScrollHandler = TouchScrollDelegate.enableTouchScrolling(this);
}
touchScrollHandler.addElement(contentNode);

/*
* Shake up the DOM a bit to make the window shed unnecessary scroll
* bars and resize correctly afterwards. This resulting code took over a
* week to summon forth, and involved some pretty hairy black magic.
* Don't touch it unless you know what you're doing! Fixes ticket
* #12727.
*
* This solution comes from the ticket #11994: Windows get unnecessary
* scroll bars in WebKit when content is 100% wide.
*/
if (BrowserInfo.get().isWebkit()) {
Scheduler.get().scheduleFinally(new ScheduledCommand() {
@Override
public void execute() {
final com.google.gwt.dom.client.Element scrollable = contentNode
.getFirstChildElement();

int contentNodeScrollTop = contentNode.getScrollTop();
int contentNodeScrollLeft = contentNode.getScrollLeft();

final String oldWidth = scrollable.getStyle().getWidth();
final String oldHeight = scrollable.getStyle().getHeight();

scrollable.getStyle().setWidth(110, Unit.PCT);
scrollable.getOffsetWidth();
scrollable.getStyle().setProperty("width", oldWidth);

scrollable.getStyle().setHeight(110, Unit.PCT);
scrollable.getOffsetHeight();
scrollable.getStyle().setProperty("height", oldHeight);

// Recovering scroll position:
contentNode.setScrollTop(contentNodeScrollTop);
contentNode.setScrollLeft(contentNodeScrollLeft);
}
});
}
Util.removeUnneededScrollbars(contentNode);
}
}

+ 3
- 29
client/src/com/vaadin/client/ui/VWindow.java View File

@@ -342,36 +342,10 @@ public class VWindow extends VOverlay implements ShortcutActionHandlerOwner,
if (!visibilityChangesDisabled) {
super.setVisible(visible);
}

if (visible && BrowserInfo.get().isWebkit()) {

/*
* Shake up the DOM a bit to make the window shed unnecessary
* scrollbars and resize correctly afterwards. This resulting code
* took over a week to summon forth, and involved some pretty hairy
* black magic. Don't touch it unless you know what you're doing!
* Fixes ticket #11994
*/
Scheduler.get().scheduleFinally(new ScheduledCommand() {
@Override
public void execute() {
final com.google.gwt.dom.client.Element scrollable = contents
.getFirstChildElement();
final String oldWidth = scrollable.getStyle().getWidth();
final String oldHeight = scrollable.getStyle().getHeight();

scrollable.getStyle().setWidth(110, Unit.PCT);
scrollable.getOffsetWidth();
scrollable.getStyle().setProperty("width", oldWidth);

scrollable.getStyle().setHeight(110, Unit.PCT);
scrollable.getOffsetHeight();
scrollable.getStyle().setProperty("height", oldHeight);

updateContentsSize();
positionOrSizeUpdated();
}
});
Util.removeUnneededScrollbars(contents);
updateContentsSize();
positionOrSizeUpdated();
}
}


+ 66
- 0
uitest/src/com/vaadin/tests/components/window/ComboboxScrollableWindow.java View File

@@ -0,0 +1,66 @@
/*
* Copyright 2000-2013 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.tests.components.window;

import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

/**
*
* @since
* @author Vaadin Ltd
*/
public class ComboboxScrollableWindow extends AbstractTestUI {

static final String WINDOW_ID = "window";
static final String COMBOBOX_ID = "combobox";

@Override
protected void setup(VaadinRequest request) {

Window w = new Window();
w.setId(WINDOW_ID);
w.setWidth("300px");
w.setHeight("300px");
w.center();

VerticalLayout content = new VerticalLayout();
w.setContent(content);
content.setHeight("1000px");
ComboBox cb = new ComboBox();
cb.setId(COMBOBOX_ID);
content.addComponent(cb);
content.setComponentAlignment(cb, Alignment.BOTTOM_CENTER);

addWindow(w);

}

@Override
protected String getTestDescription() {
return "The combo box in the bottom of the scrollable window should remain visible when it is clicked.";
}

@Override
protected Integer getTicketNumber() {
return 12736;
}

}

+ 57
- 0
uitest/src/com/vaadin/tests/components/window/ComboboxScrollableWindowTest.java View File

@@ -0,0 +1,57 @@
/*
* Copyright 2000-2013 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.tests.components.window;

import static com.vaadin.tests.components.window.ComboboxScrollableWindow.COMBOBOX_ID;
import static com.vaadin.tests.components.window.ComboboxScrollableWindow.WINDOW_ID;

import org.junit.Test;
import org.openqa.selenium.WebElement;

import com.vaadin.testbench.By;
import com.vaadin.testbench.commands.TestBenchElementCommands;
import com.vaadin.tests.tb3.MultiBrowserTest;

/**
*
* @since
* @author Vaadin Ltd
*/
public class ComboboxScrollableWindowTest extends MultiBrowserTest {

@Test
public void testWindowScrollbars() throws Exception {
openTestURL();
com.vaadin.testbench.Parameters
.setScreenshotComparisonCursorDetection(true);

WebElement window = driver.findElement(By.id(WINDOW_ID));
WebElement scrollableElement = window.findElement(By
.className("v-scrollable"));
TestBenchElementCommands scrollable = testBenchElement(scrollableElement);
scrollable.scroll(1000);
WebElement comboBox = driver.findElement(By.id(COMBOBOX_ID));
WebElement selectButton = driver.findElement(By
.className("v-filterselect-button"));
selectButton.click();

// Wait for the browser before taking a screenshot
Thread.sleep(1000);
compareScreen(getScreenshotBaseName());

}

}

Loading…
Cancel
Save