Skip to content

[GTK4] Layout not propagated correctly #3330

@tmssngr

Description

@tmssngr

Describe the bug
For previously invisible Composites, setting the size does not trigger a layout for children like on other platforms or with GTK3.

To Reproduce

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Gtk4CardLayoutTest {

	public static void main(String[] args) {
		final Display display = new Display();

		final Shell shell = new Shell(display);
		shell.setLayout(new GridLayout(1, false));

		final Composite buttonBar = new Composite(shell, SWT.NONE);
		buttonBar.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		buttonBar.setLayout(new RowLayout());

		final Composite cards = new Composite(shell, SWT.NONE);
		cards.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		final CardLayout cardLayout = new CardLayout();
		cards.setLayout(cardLayout);

		createCard("Card 1", buttonBar, cards, cardLayout);
		createCard("Card 2", buttonBar, cards, cardLayout);

		shell.setSize(400, 300);
		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}

		display.dispose();
	}

	private static void createCard(String text, Composite buttonBar, Composite cards, CardLayout cardLayout) {
		final boolean isFirst = cards.getChildren().length == 0;

		final Composite card = new Composite(cards, SWT.NONE);
		card.setLayout(new GridLayout());

		final Text textControl = new Text(card, SWT.BORDER);
		textControl.setMessage(text);
		textControl.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
		textControl.addListener(SWT.Resize, event -> System.out.println("Resize " + text));

		final Button button = new Button(buttonBar, SWT.RADIO);
		button.setText(text);
		if (isFirst) {
			button.setSelection(true);
			cardLayout.setCurrent(card);
		}
		else {
			card.setVisible(false);
		}
		button.addListener(SWT.Selection, event -> cardLayout.setCurrent(card));
	}

	private static class CardLayout extends Layout {
		private Control current;

		@Override
		protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
			final Point size = new Point(0, 0);
			for (Control child : composite.getChildren()) {
				final Point childSize = child.computeSize(SWT.DEFAULT, SWT.DEFAULT);
				size.x = Math.max(size.x, childSize.x);
				size.y = Math.max(size.y, childSize.y);
			}
			return size;
		}

		@Override
		protected void layout(Composite composite, boolean flushCache) {
			System.out.println("current = " + current);
			if (current != null) {
				final Point size = composite.getSize();
				System.out.println("size = " + size);
				current.setSize(size);
			}
		}

		public void setCurrent(Control control) {
			if (current != null) {
				current.setVisible(false);
			}
			current = control;
			if (current != null) {
				current.setVisible(true);
				current.getParent().requestLayout();
			}
		}
	}
}
  • launch this snippet with SWT_GTK4=0
  • click the "Card 2" radio button -> the "Card 2" text field becomes visible properly at the center
  • launch this snippet with SWT_GTK4=1
  • click the "Card 2" radio button -> the "Card 2" text field is not resized (though, as the output indicates, setSize() was invoked correctly, but no Resize-event occurs for the second card unless the shell is manually resized)

Expected behavior
It should work like with SWT_GTK4=0 or on other platforms - setSize should trigger a layout. Or it should not trigger a layout on all platforms.

Screenshots
With SWT_GTK4=0:
Image

With SWT_GTK4=1:
Image

Environment:

  1. Select the platform(s) on which the behavior is seen:
    • All OS
    • Windows
    • Linux
    • macOS
  1. Additional OS info (e.g. OS version, Linux Desktop, etc)
    Ubuntu 26.04

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions