Skip to content

Conversation

jonahgraham
Copy link
Contributor

Obtaining data from the clipboard is fundamentally an asynchronous operation, the event loop, at least at the OS level, needs to run for the data to become available.

This PR proposes new API that provides asynchronous equivalents of getContents methods called getContentsAsync that return CompleteableFutures.

This new API will allow SWT API consumers to interact better with the clipboard when the OS (window system) API is actually implemented in an asynchronous manner.

GTK3 and cocoa provides API that spins the event loop as needed so that a synchronous looking API can be provided to the user of the API. For example, in SWT we use gtk_clipboard_wait_for_contents which spins the loop in the GTK library itself
here

GTK4 does not provide such an API and leaves it to the user of the API to spin the event loop.

Win32 is somewhat of a hybrid. The API appears synchronous, but SWT needs to spin in a couple of places, such as
getData

Part of #2598 and #2126

@github-actions
Copy link
Contributor

github-actions bot commented Oct 17, 2025

Test Results

  115 files    115 suites   11m 37s ⏱️
4 562 tests 4 546 ✅ 16 💤 0 ❌
  311 runs    308 ✅  3 💤 0 ❌

Results for commit 3420711.

♻️ This comment has been updated with latest results.

@jonahgraham
Copy link
Contributor Author

One aspect I am unsure of is the naming here.

We can't simply overload getContents because the only difference in the new method is the return type. But in java.util.concurrent it seems generally the Async suffixed methods imply running in a separate (asynchronous) thread.

Am I overthinking this, is getContentsAsync a good name? @laeubi any thoughts?

@HeikoKlare
Copy link
Contributor

Am I overthinking this, is getContentsAsync a good name?

In my opinion, thinking about that name is definitely valuable. It might be mistaken as "I just have to wait long enough for the contents to be available" (as it would be the case with the referenced concurrent Java API using an Async suffix). E.g., you could use getContentsAsync().join(), but that will never return if no one else spins the event loop. There is a slight analogy to Display#asyncExec() as that will also only be executed if you spin the event loop (though there is no CompletableFuture but you could, e.g., put a barrier in the passed runnable), just that Display#asyncExec() is more common and users usually know how it works.
On the other hand, I have no better name proposal and a sophisticated documentation of what "async" means in this case is given by the Javadoc, so unless someone has a better proposal I would be fine the currently proposed name.

@jonahgraham
Copy link
Contributor Author

I would be fine the currently proposed name.

Great. Thanks @HeikoKlare for having a look and sharing your thoughts here. I'll leave this open a little bit longer in case anyone else has thoughts or concerns.

@laeubi
Copy link
Contributor

laeubi commented Oct 20, 2025

I would be fine the currently proposed name.

Great. Thanks @HeikoKlare for having a look and sharing your thoughts here. I'll leave this open a little bit longer in case anyone else has thoughts or concerns.

I'm also fine with that name, an alternative I often use in this case is simply omit the "get" and use the "shortform" what is the CompletableFuture<?> contents(); but not everyone is happy with that and its more a personal preference.

In any case every java developer should be aware that joining a feature is always a bad thing and a source of code-smell.

@jonahgraham
Copy link
Contributor Author

alternative I often use in this case is simply omit the "get" and use the "shortform"

I am pretty fond of this too, especially when making new API these days. I like "ProcessBuilder" in Java in this way.

@akurtakov
Copy link
Member

I believe this is ready to go . Only request from my side is to add a reference in getContents javadoc refering to the new method and guiding users to choose it.

@jonahgraham jonahgraham marked this pull request as draft October 22, 2025 13:40
Obtaining data from the clipboard is fundamentally an asynchronous
operation, the event loop, at least at the OS level, needs to run
for the data to become available.

This PR proposes new API that provides asynchronous equivalents of
getContents methods called getContentsAsync that return
CompleteableFutures.

This new API will allow SWT API consumers to interact better with the
clipboard when the OS (window system) API is actually implemented in an
asynchronous manner.

GTK3 and cocoa provides API that spins the event loop as needed so
that a synchronous looking API can be provided to the user of the API.
For example, in SWT we use gtk_clipboard_wait_for_contents which
spins the loop in the GTK library itself
[here](https://gitlab.gnome.org/GNOME/gtk/-/blob/716458e86a222f43e64f7a4feda37749f3469ee4/gtk/gtkclipboard.c#L1436)

GTK4 does not provide such an API and leaves it to the user
of the API to spin the event loop.

Win32 is somewhat of a hybrid. The API appears synchronous, but SWT
needs to spin in a couple of places, such as
[getData](https://github.com/eclipse-platform/eclipse.platform.swt/blob/63e8925dc77db3b93ef521dc6cf2bd6ded7bab64/bundles/org.eclipse.swt/Eclipse%20SWT%20Drag%20and%20Drop/win32/org/eclipse/swt/dnd/Transfer.java#L46)

Part of eclipse-platform#2598 and eclipse-platform#2126
@jonahgraham
Copy link
Contributor Author

I believe this is ready to go . Only request from my side is to add a reference in getContents javadoc refering to the new method and guiding users to choose it.

Done: See diffs here

@jonahgraham jonahgraham marked this pull request as ready for review October 22, 2025 15:26
@jonahgraham jonahgraham merged commit 9e3ae28 into eclipse-platform:master Oct 22, 2025
17 checks passed
@jonahgraham jonahgraham deleted the clipboard-async branch October 22, 2025 15:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants