GtkHelper.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Threading.Tasks;
  3. using Avalonia.Controls.Platform;
  4. using Avalonia.Platform.Interop;
  5. using Avalonia.X11.Interop;
  6. using Avalonia.X11.NativeDialogs;
  7. using static Avalonia.X11.NativeDialogs.Gtk;
  8. using static Avalonia.X11.NativeDialogs.Glib;
  9. namespace ControlCatalog.NetCore;
  10. internal class GtkHelper
  11. {
  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. return GtkInteropHelper.RunOnGlibThread(() =>
  34. {
  35. using (var title = new Utf8Buffer("Embedded"))
  36. {
  37. var widget = gtk_file_chooser_dialog_new(title, IntPtr.Zero, GtkFileChooserAction.SelectFolder,
  38. IntPtr.Zero);
  39. gtk_widget_realize(widget);
  40. var xid = gdk_x11_window_get_xid(gtk_widget_get_window(widget));
  41. gtk_window_present(widget);
  42. return new FileChooser(widget, xid);
  43. }
  44. }).Result;
  45. }
  46. }