Gtk3Platform.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Avalonia.Controls;
  8. using Avalonia.Controls.Platform;
  9. using Avalonia.Gtk3.Interop;
  10. using Avalonia.Input;
  11. using Avalonia.Input.Platform;
  12. using Avalonia.Platform;
  13. using Avalonia.Rendering;
  14. using Avalonia.Gtk3;
  15. namespace Avalonia.Gtk3
  16. {
  17. public class Gtk3Platform : IWindowingPlatform, IPlatformSettings, IPlatformThreadingInterface, IRendererFactory
  18. {
  19. internal static readonly Gtk3Platform Instance = new Gtk3Platform();
  20. internal static readonly MouseDevice Mouse = new MouseDevice();
  21. internal static readonly KeyboardDevice Keyboard = new KeyboardDevice();
  22. internal static IntPtr App { get; set; }
  23. public static void Initialize()
  24. {
  25. Resolver.Resolve();
  26. Native.GtkInit(0, IntPtr.Zero);
  27. using (var utf = new Utf8Buffer("avalonia.app." + Guid.NewGuid()))
  28. App = Native.GtkApplicationNew(utf, 0);
  29. //Mark current thread as UI thread
  30. s_tlsMarker = true;
  31. AvaloniaLocator.CurrentMutable.Bind<IWindowingPlatform>().ToConstant(Instance)
  32. .Bind<IClipboard>().ToSingleton<ClipboardImpl>()
  33. .Bind<IStandardCursorFactory>().ToConstant(new CursorFactory())
  34. .Bind<IKeyboardDevice>().ToConstant(Keyboard)
  35. .Bind<IMouseDevice>().ToConstant(Mouse)
  36. .Bind<IPlatformSettings>().ToConstant(Instance)
  37. .Bind<IPlatformThreadingInterface>().ToConstant(Instance)
  38. .Bind<ISystemDialogImpl>().ToSingleton<SystemDialog>()
  39. .Bind<IRendererFactory>().ToConstant(Instance)
  40. .Bind<IRenderLoop>().ToConstant(new DefaultRenderLoop(60))
  41. .Bind<IPlatformIconLoader>().ToConstant(new PlatformIconLoader());
  42. }
  43. public IWindowImpl CreateWindow() => new WindowImpl();
  44. public IEmbeddableWindowImpl CreateEmbeddableWindow()
  45. {
  46. throw new NotImplementedException();
  47. }
  48. public IPopupImpl CreatePopup() => new PopupImpl();
  49. public IRenderer CreateRenderer(IRenderRoot root, IRenderLoop renderLoop)
  50. {
  51. return new ImmediateRenderer(root);
  52. }
  53. public Size DoubleClickSize => new Size(4, 4);
  54. public TimeSpan DoubleClickTime => TimeSpan.FromMilliseconds(100); //STUB
  55. public double RenderScalingFactor { get; } = 1;
  56. public double LayoutScalingFactor { get; } = 1;
  57. public void RunLoop(CancellationToken cancellationToken)
  58. {
  59. while (!cancellationToken.IsCancellationRequested)
  60. Native.GtkMainIteration();
  61. }
  62. public IDisposable StartTimer(TimeSpan interval, Action tick)
  63. {
  64. return GlibTimeout.StarTimer((uint) interval.TotalMilliseconds, tick);
  65. }
  66. private bool _signaled = false;
  67. object _lock = new object();
  68. public void Signal()
  69. {
  70. lock(_lock)
  71. if (!_signaled)
  72. {
  73. _signaled = true;
  74. GlibTimeout.Add(0, () =>
  75. {
  76. lock (_lock)
  77. {
  78. _signaled = false;
  79. }
  80. Signaled?.Invoke();
  81. return false;
  82. });
  83. }
  84. }
  85. public event Action Signaled;
  86. [ThreadStatic]
  87. private static bool s_tlsMarker;
  88. public bool CurrentThreadIsLoopThread => s_tlsMarker;
  89. }
  90. }
  91. namespace Avalonia
  92. {
  93. public static class Gtk3AppBuilderExtensions
  94. {
  95. public static T UseGtk3<T>(this AppBuilderBase<T> builder, ICustomGtk3NativeLibraryResolver resolver = null)
  96. where T : AppBuilderBase<T>, new()
  97. {
  98. Resolver.Custom = resolver;
  99. return builder.UseWindowingSubsystem(Gtk3Platform.Initialize, "GTK3");
  100. }
  101. }
  102. }