Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.util.Arrays;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.core.runtime.IProgressMonitor;
Expand Down Expand Up @@ -1109,13 +1110,24 @@ private void hookZoomChangeListenerForDefaultSizedDialogs() {
}

AtomicReference<Listener> customResizeListener = new AtomicReference<>();
// setSize() sends the next SWT.Resize, which calls this listener again. Without the
// re-entrancy guard the listener recurses until the stack is exhausted whenever the
// computed size is not a fixed point, e.g. because a scrollbar appears and disappears
// with it.
AtomicBoolean adaptingDefaultSize = new AtomicBoolean();
Listener adaptDefaultSizeListener = event -> {
Shell shell = getShell();
if (shell == null || shell.isDisposed()) {
if (shell == null || shell.isDisposed() || !adaptingDefaultSize.compareAndSet(false, true)) {
return;
}
Point size = shell.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
shell.setSize(size);
try {
Point size = shell.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
if (!size.equals(shell.getSize())) {
shell.setSize(size);
}
} finally {
adaptingDefaultSize.set(false);
}
};
AtomicReference<Listener> zoomChangeListener = new AtomicReference<>();
zoomChangeListener.set(event -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,16 @@ public int getShellStyle() {
return super.getShellStyle();
}

/**
* A wizard dialog is resizable unless a caller has stripped {@link SWT#RESIZE} from its shell
* style, so the answer is read from that style instead of being inherited from
* <code>Dialog</code>, which always answers <code>false</code>.
*/
@Override
protected boolean isResizable() {
return (getShellStyle() & SWT.RESIZE) != 0;
Comment on lines +351 to +352
}

private static int getShellModality(boolean modal) {
return modal ? SWT.PRIMARY_MODAL : SWT.NONE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,30 @@
******************************************************************************/
package org.eclipse.jface.tests.dialogs;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

public class DialogTest {

/** Width the growing test content adds per size computation. */
private static final int GROWTH = 50;

/**
* The dialog being tested.
*/
Expand Down Expand Up @@ -77,6 +87,106 @@ public void testButtonAlignmentBug272583() {
forceLayoutDialog.close();
}

/**
* A default-sized, non-resizable dialog adapts its size after a zoom change.
* The adaptation calls <code>Shell.setSize</code>, which sends the next
* <code>SWT.Resize</code> to the same listener. The listener must not recurse
* when the computed size keeps changing, must still adapt on a later resize,
* and must not resize when the computed size is unchanged.
*/
@Test
public void testZoomChangeAdaptationOfDefaultSizedDialogTerminates() {
GrowingContentDialog growingDialog = new GrowingContentDialog();
dialog = growingDialog;
growingDialog.setBlockOnOpen(false);
growingDialog.create();
Shell shell = growingDialog.getShell();
Control firstChild = shell.getChildren()[0];
Point initialSize = shell.getSize();
// the adaptation is hooked only for a dialog that reports its default size
// after creation; where the platform reports another size for a shell that
// is not open yet, there is nothing to test
assumeFalse(growingDialog.resizeOccurredBeforeInitializeBounds(),
"a resize before initializeBounds marks the dialog as custom-sized");
assumeTrue(initialSize.equals(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT, true)),
"the dialog must report its default size for the adaptation to be hooked");

// every computed size differs from the previous one, so a listener without
// re-entrancy guard recurses until the stack is exhausted
int widthBeforeZoomChange = growingDialog.content.preferredWidth;
growingDialog.content.growOnEveryComputeSize = true;
firstChild.notifyListeners(SWT.ZoomChanged, new Event());
shell.notifyListeners(SWT.Resize, new Event());
assertTrue(growingDialog.content.preferredWidth > widthBeforeZoomChange,
"the adaptation computes the size after the zoom change");
Point adaptedSize = shell.getSize();
assertNotEquals(initialSize, adaptedSize, "the dialog adapts its size once after the zoom change");

// a later independent resize adapts to the new preferred size
growingDialog.content.growOnEveryComputeSize = false;
growingDialog.content.preferredWidth += GROWTH;
shell.notifyListeners(SWT.Resize, new Event());
Point resizedSize = shell.getSize();
assertNotEquals(adaptedSize, resizedSize, "a later resize adapts the dialog to its new preferred size");

// an unchanged computed size causes no further resize
int[] resizeEvents = { 0 };
shell.addListener(SWT.Resize, event -> resizeEvents[0]++);
shell.notifyListeners(SWT.Resize, new Event());
assertEquals(resizedSize, shell.getSize(), "an unchanged computed size keeps the size");
assertEquals(1, resizeEvents[0], "an unchanged computed size sends no further resize");
}

/**
* A dialog whose content reports a preferred width that can grow with every
* computation, so that the computed shell size is never a fixed point while
* growing is on.
*/
private class GrowingContentDialog extends Dialog {

GrowingContent content;

GrowingContentDialog() {
super((Shell) null);
}

boolean resizeOccurredBeforeInitializeBounds() {
return resizeHasOccurred;
}

@Override
protected Control createDialogArea(Composite parent) {
Composite area = (Composite) super.createDialogArea(parent);
content = new GrowingContent(area);
content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
return area;
}
}

/**
* The content has to be wider and higher than the rest of the dialog, so that
* its preferred size determines the computed shell size on every platform. A
* narrow content would leave the button bar as the widest part, and growing it
* would not change the size of the shell.
*/
private static class GrowingContent extends Composite {

int preferredWidth = 600;
boolean growOnEveryComputeSize;

GrowingContent(Composite parent) {
super(parent, SWT.NONE);
}

@Override
public Point computeSize(int wHint, int hHint, boolean changed) {
if (growOnEveryComputeSize) {
preferredWidth += GROWTH;
}
return new Point(preferredWidth, 400);
}
}

/**
* A dialog that explicitly invokes a layout operation prior to the shell being
* opened. Calls to moveBelow(Control) should be followed by a layout operation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public Button getFinishedButton() {
return getButton(IDialogConstants.FINISH_ID);
}

@Override
public boolean isResizable() {
return super.isResizable();
}

@Override
public Button getCancelButton() {
return getButton(IDialogConstants.CANCEL_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@
import org.eclipse.jface.dialogs.IPageChangingListener;
import org.eclipse.jface.util.ILogger;
import org.eclipse.jface.util.Policy;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -253,6 +256,40 @@ public void testWizardPageDispose() {

}

/**
* A wizard dialog is resizable, so a zoom change must not install the
* adaptation to the computed size that non-resizable dialogs get: a size
* requested afterwards has to stay.
*/
@Test
public void testResizableWizardKeepsRequestedSizeAfterZoomChange() {
assertTrue(dialog.isResizable(), "a wizard dialog with the default shell style is resizable");
Shell shell = dialog.getShell();

shell.getChildren()[0].notifyListeners(SWT.ZoomChanged, new Event());
Point requestedSize = new Point(shell.getSize().x + 60, shell.getSize().y + 40);
shell.setSize(requestedSize);
shell.notifyListeners(SWT.Resize, new Event());

assertEquals(requestedSize, shell.getSize(), "the requested size is kept after a zoom change");
}

/**
* Removing SWT.RESIZE from the shell style before the shell is created gives a
* fixed-size wizard dialog.
*/
@Test
public void testWizardWithoutResizeStyleIsNotResizable() {
TheTestWizardDialog fixedSizeDialog = new TheTestWizardDialog(null, new TheTestWizard());
fixedSizeDialog.setShellStyle(fixedSizeDialog.getShellStyle() & ~SWT.RESIZE);
try {
fixedSizeDialog.create();
assertFalse(fixedSizeDialog.isResizable(), "a wizard dialog without SWT.RESIZE is not resizable");
} finally {
fixedSizeDialog.close();
}
}

//----------------------------------------------------

@BeforeEach
Expand Down
Loading