Skip to content

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Oct 4, 2025

Overview

This PR implements a headless mode for SWT (Standard Widget Toolkit), similar to Java AWT's java.awt.headless system property. This allows SWT applications to run in environments without a native windowing system, which is particularly useful for server-side applications, testing, CI/CD environments, and command-line tools that reference UI code.

Changes

1. Display.isHeadless() API

Added a new public static method Display.isHeadless() to detect headless mode:

if (Display.isHeadless()) {
    // Running in headless mode - no native widgets will be created
}
  • Returns false for all native platforms (gtk, cocoa, win32)
  • Returns true for the new headless implementation
  • Added to Display.java in gtk, cocoa, and win32 implementations

2. Headless Implementation

Created a new headless source folder in bundles/org.eclipse.swt/Eclipse SWT/ with a complete headless implementation of core SWT widgets:

Core Widgets:

  • Display, Widget, Control, Scrollable, Composite, Canvas, Decorations, Shell
  • Button, Label, Menu, MenuItem, Item, Caret, ScrollBar
  • Device, DeviceData

Key Features:

  • Widget state management: Properties set via setters are stored and returned by getters
  • Parent-child relationships: Composites maintain child lists that can be queried via getChildren()
  • Layout support: Layout managers work normally (though no actual rendering occurs)
  • Event listeners: Can be registered and will be called when events are posted
  • No-op rendering: Methods like redraw() and update() are harmless no-ops

3. Fragment Configuration

Created org.eclipse.swt.headless fragment in binaries/ with:

  • Complete OSGi fragment configuration (MANIFEST.MF, build.properties)
  • Integration with Maven build system (pom.xml)
  • Comprehensive README.md documentation

Usage Example

// Create a headless display
Display display = new Display();
System.out.println("Headless: " + Display.isHeadless()); // true

// Create widgets normally
Shell shell = new Shell(display);
shell.setText("Test Shell");
shell.setSize(400, 300);

Button button = new Button(shell, SWT.PUSH);
button.setText("Click Me");

// Properties work as expected
System.out.println("Button text: " + button.getText()); // "Click Me"
System.out.println("Shell size: " + shell.getSize()); // Point {400, 300}

// Cleanup
shell.dispose();
display.dispose();

Benefits

  1. Server-side applications can reference UI code without requiring a display
  2. Testing UI applications in CI/CD environments without X11/Wayland/display server
  3. Headless rendering of UI layouts for documentation or size calculations
  4. Command-line tools can use SWT libraries without UI dependencies

Implementation Details

  • Minimal changes: Only added new files, didn't modify existing platform code (except for adding isHeadless() method)
  • No native resources: Completely implemented in Java, no platform dependencies
  • Complete public API: All public methods available with working implementations or sensible defaults
  • Consistent patterns: All widgets follow the same implementation patterns for reliability

Limitations

The headless implementation is intentionally minimal and focused on state management:

  • No actual rendering to screen or images
  • No keyboard/mouse event generation
  • No system integration (clipboard, drag-and-drop, dialogs)
  • Advanced widgets (Table, Tree, Text, StyledText, Browser) not yet implemented
  • Graphics operations (GC drawing) are no-ops

These limitations are documented in the included README.md.

Related Issues

Fixes #577498

Testing

The implementation can be tested by setting the org.eclipse.swt.headless fragment as the only platform fragment available, which will cause the OSGi framework to select it automatically. Basic widget creation, property management, and parent-child relationships have been verified to work correctly.

Original prompt

This section details on the original issue you should resolve

<issue_title>Provide an swt-headless fragment</issue_title>
<issue_description>There are several cases where one want to run some code in an headless way that might has references to UI code.

AWT offers for this purpose the java.awt.headless system property.

Something similar would be good for SWT:

  1. Have a static method Display.isHeadless() returning false for all current implementations
  2. Have a swt-headless fragment, where all public API is simply a no-op as far as possible or throwing an SWT exception and isHeadless return true.</issue_description>

<agent_instructions>- Create a new org.eclipse.swt.headless fragment in the binaries

  • Instead of gtk/cococa/win32 as we have use a new source folder "headless" in the host that can be used for the actual implementation classes
  • Create all the required classes like Button, Display, Label in the headless implementation with only the public API shared accross all variants
  • Instead of using calls into native code as with the gtk/cococa/win32 implementations values set throug constructors or setters should just be stored in private fields and returned when a getter is called
  • Creating a Widget (e.g. a Label) with a Composite as a parent should store the widget in a list inside the composite so it can be returned from getChildren(), disposing the child should automatically remove it from the child list
  • In cases that can not be implemented we might simply return a default value or do it as a noop
  • Create a readme.md in the org.eclipse.swt.headless describing current limitation and scope of this feature</agent_instructions>

Comments on the Issue (you are @copilot in this section)

Fixes #1750

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 3 commits October 4, 2025 06:22
…tions

Co-authored-by: laeubi <1331477+laeubi@users.noreply.github.com>
Co-authored-by: laeubi <1331477+laeubi@users.noreply.github.com>
Co-authored-by: laeubi <1331477+laeubi@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Provide an swt-headless fragment Add SWT headless fragment for running SWT without a display Oct 4, 2025
@Copilot Copilot AI requested a review from laeubi October 4, 2025 06:33
Copilot finished work on behalf of laeubi October 4, 2025 06:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide an swt-headless fragment
2 participants