GtkHelper.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Threading.Tasks;
  3. using Avalonia.Controls.Platform;
  4. using Avalonia.Platform;
  5. using Avalonia.Platform.Interop;
  6. using Avalonia.X11.NativeDialogs;
  7. using static Avalonia.X11.NativeDialogs.Gtk;
  8. using static Avalonia.X11.NativeDialogs.Glib;
  9. namespace NativeEmbedSample
  10. {
  11. public 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. }