GtkHelper.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #if DESKTOP
  2. using System;
  3. using System.Threading.Tasks;
  4. using Avalonia.Controls.Platform;
  5. using Avalonia.Platform;
  6. using Avalonia.Platform.Interop;
  7. using Avalonia.X11.NativeDialogs;
  8. using static Avalonia.X11.NativeDialogs.Gtk;
  9. using static Avalonia.X11.NativeDialogs.Glib;
  10. namespace NativeEmbedSample;
  11. internal class GtkHelper
  12. {
  13. private static Task<bool> s_gtkTask;
  14. class FileChooser : INativeControlHostDestroyableControlHandle
  15. {
  16. private readonly IntPtr _widget;
  17. public FileChooser(IntPtr widget, IntPtr xid)
  18. {
  19. _widget = widget;
  20. Handle = xid;
  21. }
  22. public IntPtr Handle { get; }
  23. public string HandleDescriptor => "XID";
  24. public void Destroy()
  25. {
  26. RunOnGlibThread(() =>
  27. {
  28. gtk_widget_destroy(_widget);
  29. return 0;
  30. }).Wait();
  31. }
  32. }
  33. public static IPlatformHandle CreateGtkFileChooser(IntPtr parentXid)
  34. {
  35. if (s_gtkTask == null)
  36. s_gtkTask = StartGtk();
  37. if (!s_gtkTask.Result)
  38. return null;
  39. return RunOnGlibThread(() =>
  40. {
  41. using (var title = new Utf8Buffer("Embedded"))
  42. {
  43. var widget = gtk_file_chooser_dialog_new(title, IntPtr.Zero, GtkFileChooserAction.SelectFolder,
  44. IntPtr.Zero);
  45. gtk_widget_realize(widget);
  46. var xid = gdk_x11_window_get_xid(gtk_widget_get_window(widget));
  47. gtk_window_present(widget);
  48. return new FileChooser(widget, xid);
  49. }
  50. }).Result;
  51. }
  52. }
  53. #endif