GtkPlatform.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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
  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<IRenderLoop>().ToConstant(new DefaultRenderLoop(60))
  49. .Bind<ISystemDialogImpl>().ToSingleton<SystemDialogImpl>()
  50. .Bind<IPlatformIconLoader>().ToConstant(s_instance);
  51. _uiThread = Thread.CurrentThread;
  52. }
  53. public bool HasMessages()
  54. {
  55. return Gtk.Application.EventsPending();
  56. }
  57. public void ProcessMessage()
  58. {
  59. Gtk.Application.RunIteration();
  60. }
  61. public void RunLoop(CancellationToken cancellationToken)
  62. {
  63. while (!cancellationToken.IsCancellationRequested)
  64. Gtk.Application.RunIteration();
  65. }
  66. public IDisposable StartTimer(TimeSpan interval, Action tick)
  67. {
  68. var result = true;
  69. var handle = GLib.Timeout.Add(
  70. (uint)interval.TotalMilliseconds,
  71. () =>
  72. {
  73. tick();
  74. return result;
  75. });
  76. return Disposable.Create(() => result = false);
  77. }
  78. public void Signal()
  79. {
  80. Gtk.Application.Invoke(delegate { Signaled?.Invoke(); });
  81. }
  82. public bool CurrentThreadIsLoopThread => Thread.CurrentThread == _uiThread;
  83. public event Action Signaled;
  84. public IWindowImpl CreateWindow()
  85. {
  86. return new WindowImpl();
  87. }
  88. public IEmbeddableWindowImpl CreateEmbeddableWindow() => new EmbeddableImpl();
  89. public IPopupImpl CreatePopup()
  90. {
  91. return new PopupImpl();
  92. }
  93. public IWindowIconImpl LoadIcon(string fileName)
  94. {
  95. return new IconImpl(new Gdk.Pixbuf(fileName));
  96. }
  97. public IWindowIconImpl LoadIcon(Stream stream)
  98. {
  99. return new IconImpl(new Gdk.Pixbuf(stream));
  100. }
  101. public IWindowIconImpl LoadIcon(IBitmapImpl bitmap)
  102. {
  103. if (bitmap is Gdk.Pixbuf)
  104. {
  105. return new IconImpl((Gdk.Pixbuf)bitmap);
  106. }
  107. else
  108. {
  109. using (var memoryStream = new MemoryStream())
  110. {
  111. bitmap.Save(memoryStream);
  112. return new IconImpl(new Gdk.Pixbuf(memoryStream));
  113. }
  114. }
  115. }
  116. }
  117. }