Skip to content

ExpandBar is not requesting layout after expand/collapse breaking the layout #551

@laeubi

Description

@laeubi

Describe the bug
I have a layout that dynamically grow/shrink according to the content, and an ExpandBar widget to show/hide content.
When the user expand/collapse content, the UI is not updated appropriately:

  • If the item is collapsed and user expands it, the content is cut of
  • If the item is expanded and the user collapse it, space is not reclaimed
  • If the users change the size of the window, the layout is updated (so this is not a bug in the layout itself).

To Reproduce
The following snippet can be used to reproduce the behavior (derived from Snippet223):

package test.me;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.BorderData;
import org.eclipse.swt.layout.BorderLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class Snippet223_Bug {

public static void main (String [] args) {
	Display display = new Display ();
	Shell shell = new Shell (display);
  shell.setLayout(new BorderLayout());
	shell.setText("ExpandBar Example");
  ExpandBar bar = new ExpandBar(shell, SWT.NONE);
  bar.setLayoutData(new BorderData(SWT.TOP));
  Text text = new Text(shell, SWT.V_SCROLL);
  text.setLayoutData(new BorderData(SWT.CENTER));
	Image image = display.getSystemImage(SWT.ICON_QUESTION);
	Composite composite = new Composite (bar, SWT.NONE);
  GridLayout layout = new GridLayout();
  layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 10;
  layout.verticalSpacing = 10;
	composite.setLayout(layout);
	Button button = new Button (composite, SWT.PUSH);
	button.setText("SWT.PUSH");
	button = new Button (composite, SWT.RADIO);
	button.setText("SWT.RADIO");
	button = new Button (composite, SWT.CHECK);
	button.setText("SWT.CHECK");
	button = new Button (composite, SWT.TOGGLE);
	button.setText("SWT.TOGGLE");
	ExpandItem item0 = new ExpandItem (bar, SWT.NONE, 0);
	item0.setText("What is your favorite button");
	item0.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
	item0.setControl(composite);
	item0.setImage(image);
  item0.setExpanded(true);
	bar.setSpacing(8);
  bar.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
	shell.setSize(400, 350);
	shell.open();
	while (!shell.isDisposed ()) {
		if (!display.readAndDispatch ()) {
			display.sleep ();
		}
	}
	image.dispose();
	display.dispose();
}

}

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
After starting the snippet it looks like this:
grafik

The collapsing the item looks like this:
grafik

Now make the window a bit larger:
grafik

Now expand the item and it looks like this:
grafik

Now make the window a bit smaller:
grafik

Environment:

  1. Select the platform(s) on which the behavior is seen:
    • All OS
    • Windows
    • Linux
    • macOS
  1. Debian 4.19.194-3 (2021-07-18) x86_64 GNU/Linux
  2. Java 17

Version since
Eclipse or SWT version since when the behavior is seen [e.g. 4.23]: Current SWT release

Workaround (or) Additional context
If I register a listener and request layout manually everything works as expected without a glitch:

  bar.addExpandListener(new ExpandListener() {

    @Override
    public void itemExpanded(ExpandEvent e) {
      forceLayoutUpdate();
    }

    @Override
    public void itemCollapsed(ExpandEvent e) {
      forceLayoutUpdate();
    }

    private void forceLayoutUpdate() {
      bar.requestLayout();
    }
  });

This is really annoying, as it has two consequences:

  1. Developers must install workarounds and use try-and-error to find out what makes the layout broke, if noticed at all at development time (e.g. problem might only become visible with larger inputs)
  2. Users are getting bad experience with SWT application not reacting to size changes, probably even flicker if they try to resize windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    Linux/GTKHappens on LinuxWindowsHappens on Windows OSmacOShappens on macOS

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions