GtkHelper.cs 1.6 KB

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