From e975100c293c075e17473ac72578f80c228bbbce Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Mon, 18 Jul 2022 15:38:27 +0100 Subject: [PATCH] Merge pull request #8538 from AvaloniaUI/feature/expose-gtk-thread-invoke Introduced GtkInteropHelper.RunOnGlibThread, fixed demos # Conflicts: # samples/ControlCatalog.NetCore/NativeControls/Gtk/EmbedSample.Gtk.cs # samples/ControlCatalog.NetCore/NativeControls/Gtk/GtkHelper.cs --- src/Avalonia.X11/Interop/GtkInteropHelper.cs | 15 +++++++++++ src/Avalonia.X11/NativeDialogs/Gtk.cs | 27 +++++++++++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 src/Avalonia.X11/Interop/GtkInteropHelper.cs diff --git a/src/Avalonia.X11/Interop/GtkInteropHelper.cs b/src/Avalonia.X11/Interop/GtkInteropHelper.cs new file mode 100644 index 00000000000..de0b7558327 --- /dev/null +++ b/src/Avalonia.X11/Interop/GtkInteropHelper.cs @@ -0,0 +1,15 @@ +using System; +using System.ComponentModel; +using System.Threading.Tasks; + +namespace Avalonia.X11.Interop; + +public class GtkInteropHelper +{ + public static async Task RunOnGlibThread(Func cb) + { + if (!await NativeDialogs.Gtk.StartGtk().ConfigureAwait(false)) + throw new Win32Exception("Unable to initialize GTK"); + return await NativeDialogs.Glib.RunOnGlibThread(cb).ConfigureAwait(false); + } +} \ No newline at end of file diff --git a/src/Avalonia.X11/NativeDialogs/Gtk.cs b/src/Avalonia.X11/NativeDialogs/Gtk.cs index 872c824f743..feca8c5e59f 100644 --- a/src/Avalonia.X11/NativeDialogs/Gtk.cs +++ b/src/Avalonia.X11/NativeDialogs/Gtk.cs @@ -3,6 +3,8 @@ using System.Threading; using System.Threading.Tasks; using Avalonia.Platform.Interop; +using JetBrains.Annotations; + // ReSharper disable IdentifierTypo namespace Avalonia.X11.NativeDialogs { @@ -247,10 +249,19 @@ public static extern void public static IntPtr GetForeignWindow(IntPtr xid) => gdk_x11_window_foreign_new_for_display(s_display, xid); + static object s_startGtkLock = new(); + static Task s_startGtkTask; + public static Task StartGtk() { - var tcs = new TaskCompletionSource(); - new Thread(() => + return StartGtkCore(); + lock (s_startGtkLock) + return s_startGtkTask ??= StartGtkCore(); + } + + private static void GtkThread(TaskCompletionSource tcs) + { + try { try { @@ -284,7 +295,17 @@ public static Task StartGtk() tcs.SetResult(true); while (true) gtk_main_iteration(); - }) {Name = "GTK3THREAD", IsBackground = true}.Start(); + } + catch + { + tcs.SetResult(false); + } + } + + private static Task StartGtkCore() + { + var tcs = new TaskCompletionSource(); + new Thread(() => GtkThread(tcs)) {Name = "GTK3THREAD", IsBackground = true}.Start(); return tcs.Task; } }