Skip to content

Commit 2cc00b7

Browse files
ptzieglerakurtakov
authored andcommitted
[GTK4] Cleanup execution of non-blocking dialog calls
This adds a new AsyncReadyCallback class which is used to handle the asynchronous execution of dialogs. The goal is provide a cleaner and more readable interface than what is currently available by SyncDialogUtil. Note that this class currently simply wraps the call to SyncDialogUtil. But once all of the remaining dialogs (Color/Font/MessageDialog) have been migrated, it might make sense to remove this class entirely to avoid this additional indirection. Follow-up to 2e61b4b
1 parent 45405e5 commit 2cc00b7

File tree

4 files changed

+85
-15
lines changed

4 files changed

+85
-15
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Patrick Ziegler and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Patrick Ziegler - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.swt.internal;
15+
16+
/**
17+
* This class implements the GIO AsyncReadyCallback type and is used to
18+
* transform an asynchronous {@code async} and synchronous {@code await}
19+
* operation into a single synchronous {@code run} operation.
20+
*/
21+
public interface AsyncReadyCallback {
22+
/**
23+
* This method is responsible for initializes the asynchronous operation
24+
*
25+
* @param callback The callback address to execute when the operation is
26+
* complete.
27+
*/
28+
void async(long callback);
29+
30+
/**
31+
* This method is called from within the callback function in order to
32+
* finish the executed operation and to return the result.
33+
*
34+
* @param result The generic, asynchronous function result.
35+
* @return The specific result of the operation.
36+
*/
37+
long await(long result);
38+
}

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/internal/SyncDialogUtil.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ public class SyncDialogUtil {
4848
* therefore essential that callers use the address of the {@link Callback}
4949
* as address for the {@code AsyncReadyCallback} object.
5050
*/
51-
static public long run(Display display, Consumer<Long> asyncOpen, Function<Long, Long> asyncFinish) {
51+
static public long run(Display display, AsyncReadyCallback callback) {
5252
initializeResponseCallback();
5353

54-
dialogAsyncFinish = asyncFinish;
55-
asyncOpen.accept(dialogResponseCallback.getAddress());
54+
dialogAsyncFinish = callback::await;
55+
callback.async(dialogResponseCallback.getAddress());
5656

5757
while (!display.isDisposed()) {
5858
if (dialogAsyncValue != null) {

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/DirectoryDialog.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,17 @@ Optional<String> openNativeChooserDialog () {
214214
int response;
215215
long file = 0;
216216
if (GTK.GTK4) {
217-
file = SyncDialogUtil.run(display,
218-
asyncCallback -> GTK4.gtk_file_dialog_select_folder(handle, shellHandle, 0, asyncCallback, 0),
219-
asyncResult -> GTK4.gtk_file_dialog_select_folder_finish(handle, asyncResult, null));
217+
file = SyncDialogUtil.run(display, new AsyncReadyCallback() {
218+
@Override
219+
public void async(long result) {
220+
GTK4.gtk_file_dialog_select_folder(handle, shellHandle, 0, result, 0);
221+
}
222+
223+
@Override
224+
public long await(long result) {
225+
return GTK4.gtk_file_dialog_select_folder_finish(handle, result, null);
226+
}
227+
});
220228
response = file != 0 ? GTK.GTK_RESPONSE_ACCEPT : GTK.GTK_RESPONSE_CANCEL;
221229
} else {
222230
display.externalEventLoop = true;

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/FileDialog.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -404,18 +404,42 @@ Optional<String> openNativeChooserDialog () {
404404
long file = 0;
405405
if (GTK.GTK4) {
406406
if ((style & SWT.MULTI) != 0) {
407-
file = SyncDialogUtil.run(display,
408-
asyncCallback -> GTK4.gtk_file_dialog_open_multiple(handle, shellHandle, 0, asyncCallback, 0),
409-
asyncResult -> GTK4.gtk_file_dialog_open_multiple_finish(handle, asyncResult, null));
407+
file = SyncDialogUtil.run(display, new AsyncReadyCallback() {
408+
@Override
409+
public void async(long callback) {
410+
GTK4.gtk_file_dialog_open_multiple(handle, shellHandle, 0, callback, 0);
411+
}
412+
413+
@Override
414+
public long await(long result) {
415+
return GTK4.gtk_file_dialog_open_multiple_finish(handle, result, null);
416+
}
417+
});
410418
} else {
411419
if ((style & SWT.SAVE) != 0) {
412-
file = SyncDialogUtil.run(display,
413-
asyncCallback -> GTK4.gtk_file_dialog_save(handle, shellHandle, 0, asyncCallback, 0),
414-
asyncResult -> GTK4.gtk_file_dialog_save_finish(handle, asyncResult, null));
420+
file = SyncDialogUtil.run(display, new AsyncReadyCallback() {
421+
@Override
422+
public void async(long callback) {
423+
GTK4.gtk_file_dialog_save(handle, shellHandle, 0, callback, 0);
424+
}
425+
426+
@Override
427+
public long await(long result) {
428+
return GTK4.gtk_file_dialog_save_finish(handle, result, null);
429+
}
430+
});
415431
} else {
416-
file = SyncDialogUtil.run(display,
417-
asyncCallback -> GTK4.gtk_file_dialog_open(handle, shellHandle, 0, asyncCallback, 0),
418-
asyncResult -> GTK4.gtk_file_dialog_open_finish(handle, asyncResult, null));
432+
file = SyncDialogUtil.run(display, new AsyncReadyCallback() {
433+
@Override
434+
public void async(long callback) {
435+
GTK4.gtk_file_dialog_open(handle, shellHandle, 0, callback, 0);
436+
}
437+
438+
@Override
439+
public long await(long result) {
440+
return GTK4.gtk_file_dialog_open_finish(handle, result, null);
441+
}
442+
});
419443
}
420444
}
421445
response = file != 0 ? GTK.GTK_RESPONSE_ACCEPT : GTK.GTK_RESPONSE_CANCEL;

0 commit comments

Comments
 (0)