eclipse.platform.swt icon indicating copy to clipboard operation
eclipse.platform.swt copied to clipboard

StyledText: internal error setting text

Open tmssngr opened this issue 1 year ago • 2 comments

Describe the bug On macOS select the US International keyboard layout, run the below snippet and type a quote. It will remain in some intermediate state. Now click a button that clears the text of the StyledText control. You will get following exception:

Exception in thread "main" java.lang.IllegalArgumentException: Argument not valid
	at org.eclipse.swt.SWT.error(SWT.java:4899)
	at org.eclipse.swt.SWT.error(SWT.java:4833)
	at org.eclipse.swt.SWT.error(SWT.java:4804)
	at org.eclipse.swt.custom.DefaultContent.error(DefaultContent.java:691)
	at org.eclipse.swt.custom.DefaultContent.getLineAtOffset(DefaultContent.java:584)
	at org.eclipse.swt.custom.StyledTextRenderer.getTextLayout(StyledTextRenderer.java:1225)
	at org.eclipse.swt.custom.StyledTextRenderer.getTextLayout(StyledTextRenderer.java:911)
	at org.eclipse.swt.custom.StyledTextRenderer.calculate(StyledTextRenderer.java:301)
	at org.eclipse.swt.custom.StyledTextRenderer.calculateClientArea(StyledTextRenderer.java:331)
	at org.eclipse.swt.custom.StyledText.resetCache(StyledText.java:7831)
	at org.eclipse.swt.custom.StyledText.reset(StyledText.java:7800)
	at org.eclipse.swt.custom.StyledText.handleTextSet(StyledText.java:6153)
	at org.eclipse.swt.custom.StyledText$5.textSet(StyledText.java:5410)
	at org.eclipse.swt.custom.StyledTextListener.handleEvent(StyledTextListener.java:84)
	at org.eclipse.swt.custom.DefaultContent.sendTextEvent(DefaultContent.java:819)
	at org.eclipse.swt.custom.DefaultContent.setText(DefaultContent.java:839)
	at org.eclipse.swt.custom.StyledText.setText(StyledText.java:10477)
	at StyledTextWithQuote.lambda$main$0(StyledTextWithQuote.java:24)
	at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:89)
	at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4646)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1524)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1547)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1532)
	at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1325)
	at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4413)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3989)
	at StyledTextWithQuote.main(StyledTextWithQuote.java:30)

Snippet

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class StyledTextWithQuote {

    public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(1, false));

        final StyledText text = new StyledText(shell, SWT.BORDER | SWT.MULTI);
        text.setText("On MacOS select the US-Internation keyboard layout\n- press the ' key\n- click the \"Clear\" button");
        text.setSelection(text.getCharCount());
        text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        final Button button = new Button(shell, SWT.PUSH);
        button.setText("Clear");
        button.setLayoutData(new GridData(SWT.END, SWT.END, false, false));
        button.addListener(SWT.Selection, event -> text.setText(""));

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

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

        display.dispose();
    }
}

Expected behavior No crash. The StyledText will get out of the intermediate state and clears the text.

Environment:

  1. Select the platform(s) on which the behavior is seen:
    • [ ] All OS
    • [ ] Windows
    • [ ] Linux
    • [x] macOS
  1. Additional OS info (e.g. OS version, Linux Desktop, etc)
  2. Tried on macOS 14.3.1

tmssngr avatar Mar 05 '24 17:03 tmssngr

Screenshot of the intermediate state: Screenshot 2024-03-05 at 18 01 38 before clicking the Clear button.

tmssngr avatar Mar 05 '24 17:03 tmssngr

One user reported this bug for Linux, too.

tmssngr avatar Mar 06 '24 10:03 tmssngr

Seems to have been fixed already with commit f3d2bf8c on April 16th by Jörg Kubitz. Thanks!

tmssngr avatar Aug 26 '24 15:08 tmssngr