GtkPlatform.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Reactive.Disposables;
  5. using System.Threading;
  6. using Avalonia.Controls.Platform;
  7. using Avalonia.Input.Platform;
  8. using Avalonia.Input;
  9. using Avalonia.Platform;
  10. using Avalonia.Controls;
  11. namespace Avalonia
  12. {
  13. public static class GtkApplicationExtensions
  14. {
  15. public static T UseGtk<T>(this T builder) where T : AppBuilderBase<T>, new()
  16. {
  17. builder.UseWindowingSubsystem(Gtk.GtkPlatform.Initialize, "Gtk");
  18. return builder;
  19. }
  20. }
  21. }
  22. namespace Avalonia.Gtk
  23. {
  24. using System.IO;
  25. using Rendering;
  26. using Gtk = global::Gtk;
  27. public class GtkPlatform : IPlatformThreadingInterface, IPlatformSettings, IWindowingPlatform, IPlatformIconLoader, IRendererFactory
  28. {
  29. private static readonly GtkPlatform s_instance = new GtkPlatform();
  30. private static Thread _uiThread;
  31. public GtkPlatform()
  32. {
  33. Gtk.Application.Init();
  34. }
  35. public Size DoubleClickSize => new Size(4, 4);
  36. public TimeSpan DoubleClickTime => TimeSpan.FromMilliseconds(Gtk.Settings.Default.DoubleClickTime);
  37. public double RenderScalingFactor { get; } = 1;
  38. public double LayoutScalingFactor { get; } = 1;
  39. public static void Initialize()
  40. {
  41. AvaloniaLocator.CurrentMutable
  42. .Bind<IWindowingPlatform>().ToConstant(s_instance)
  43. .Bind<IClipboard>().ToSingleton<ClipboardImpl>()
  44. .Bind<IStandardCursorFactory>().ToConstant(CursorFactory.Instance)
  45. .Bind<IKeyboardDevice>().ToConstant(GtkKeyboardDevice.Instance)
  46. .Bind<IPlatformSettings>().ToConstant(s_instance)
  47. .Bind<IPlatformThreadingInterface>().ToConstant(s_instance)
  48. .Bind<IRendererFactory>().ToConstant(s_instance)
  49. .Bind<IRenderLoop>().ToConstant(new DefaultRenderLoop(60))
  50. .Bind<ISystemDialogImpl>().ToSingleton<SystemDialogImpl>()
  51. .Bind<IPlatformIconLoader>().ToConstant(s_instance);
  52. _uiThread = Thread.CurrentThread;
  53. }
  54. public bool HasMessages()
  55. {
  56. return Gtk.Application.EventsPending();
  57. }
  58. public void ProcessMessage()
  59. {
  60. Gtk.Application.RunIteration();
  61. }
  62. public void RunLoop(CancellationToken cancellationToken)
  63. {
  64. while (!cancellationToken.IsCancellationRequested)
  65. Gtk.Application.RunIteration();
  66. }
  67. public IDisposable StartTimer(TimeSpan interval, Action tick)
  68. {
  69. var result = true;
  70. var handle = GLib.Timeout.Add(
  71. (uint)interval.TotalMilliseconds,
  72. () =>
  73. {
  74. tick();
  75. return result;
  76. });
  77. return Disposable.Create(() => result = false);
  78. }
  79. public void Signal()
  80. {
  81. Gtk.Application.Invoke(delegate { Signaled?.Invoke(); });
  82. }
  83. public bool CurrentThreadIsLoopThread => Thread.CurrentThread == _uiThread;
  84. public event Action Signaled;
  85. public IWindowImpl CreateWindow()
  86. {
  87. return new WindowImpl();
  88. }
  89. public IEmbeddableWindowImpl CreateEmbeddableWindow() => new EmbeddableImpl();
  90. public IPopupImpl CreatePopup()
  91. {
  92. return new PopupImpl();
  93. }
  94. public IRenderer CreateRenderer(IRenderRoot root, IRenderLoop renderLoop)
  95. {
  96. return new ImmediateRenderer(root);
  97. }
  98. public IWindowIconImpl LoadIcon(string fileName)
  99. {
  100. return new IconImpl(new Gdk.Pixbuf(fileName));
  101. }
  102. public IWindowIconImpl LoadIcon(Stream stream)
  103. {
  104. return new IconImpl(new Gdk.Pixbuf(stream));
  105. }
  106. public IWindowIconImpl LoadIcon(IBitmapImpl bitmap)
  107. {
  108. if (bitmap is Gdk.Pixbuf)
  109. {
  110. return new IconImpl((Gdk.Pixbuf)bitmap);
  111. }
  112. else
  113. {
  114. using (var memoryStream = new MemoryStream())
  115. {
  116. bitmap.Save(memoryStream);
  117. return new IconImpl(new Gdk.Pixbuf(memoryStream));
  118. }
  119. }
  120. }
  121. }
  122. }