HeadlessPlatformStubs.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using Avalonia.Controls;
  8. using Avalonia.Controls.Platform;
  9. using Avalonia.Input;
  10. using Avalonia.Input.Platform;
  11. using Avalonia.Media;
  12. using Avalonia.Media.Fonts;
  13. using Avalonia.Media.TextFormatting;
  14. using Avalonia.Platform;
  15. using Avalonia.Utilities;
  16. namespace Avalonia.Headless
  17. {
  18. class HeadlessClipboardStub : IClipboard
  19. {
  20. private string _text;
  21. private IDataObject _data;
  22. public Task<string> GetTextAsync()
  23. {
  24. return Task.Run(() => _text);
  25. }
  26. public Task SetTextAsync(string text)
  27. {
  28. return Task.Run(() => _text = text);
  29. }
  30. public Task ClearAsync()
  31. {
  32. return Task.Run(() => _text = null);
  33. }
  34. public Task SetDataObjectAsync(IDataObject data)
  35. {
  36. return Task.Run(() => _data = data);
  37. }
  38. public Task<string[]> GetFormatsAsync()
  39. {
  40. throw new NotImplementedException();
  41. }
  42. public async Task<object> GetDataAsync(string format)
  43. {
  44. return await Task.Run(() => _data);
  45. }
  46. }
  47. class HeadlessCursorFactoryStub : IStandardCursorFactory
  48. {
  49. public IPlatformHandle GetCursor(StandardCursorType cursorType)
  50. {
  51. return new PlatformHandle(new IntPtr((int)cursorType), "STUB");
  52. }
  53. }
  54. class HeadlessPlatformSettingsStub : IPlatformSettings
  55. {
  56. public Size DoubleClickSize { get; } = new Size(2, 2);
  57. public TimeSpan DoubleClickTime { get; } = TimeSpan.FromMilliseconds(500);
  58. }
  59. class HeadlessSystemDialogsStub : ISystemDialogImpl
  60. {
  61. public Task<string[]> ShowFileDialogAsync(FileDialog dialog, Window parent)
  62. {
  63. return Task.Run(() => (string[])null);
  64. }
  65. public Task<string> ShowFolderDialogAsync(OpenFolderDialog dialog, Window parent)
  66. {
  67. return Task.Run(() => (string)null);
  68. }
  69. }
  70. class HeadlessGlyphTypefaceImpl : IGlyphTypefaceImpl
  71. {
  72. public short DesignEmHeight => 10;
  73. public int Ascent => 5;
  74. public int Descent => 5;
  75. public int LineGap => 2;
  76. public int UnderlinePosition => 5;
  77. public int UnderlineThickness => 5;
  78. public int StrikethroughPosition => 5;
  79. public int StrikethroughThickness => 2;
  80. public bool IsFixedPitch => true;
  81. public void Dispose()
  82. {
  83. }
  84. public ushort GetGlyph(uint codepoint)
  85. {
  86. return 1;
  87. }
  88. public int GetGlyphAdvance(ushort glyph)
  89. {
  90. return 1;
  91. }
  92. public int[] GetGlyphAdvances(ReadOnlySpan<ushort> glyphs)
  93. {
  94. return glyphs.ToArray().Select(x => (int)x).ToArray();
  95. }
  96. public ushort[] GetGlyphs(ReadOnlySpan<uint> codepoints)
  97. {
  98. return codepoints.ToArray().Select(x => (ushort)x).ToArray();
  99. }
  100. }
  101. class HeadlessTextShaperStub : ITextShaperImpl
  102. {
  103. public GlyphRun ShapeText(ReadOnlySlice<char> text, Typeface typeface, double fontRenderingEmSize, CultureInfo culture)
  104. {
  105. return new GlyphRun(new GlyphTypeface(typeface), 10,
  106. new ReadOnlySlice<ushort>(new ushort[] { 1, 2, 3 }),
  107. new ReadOnlySlice<double>(new double[] { 1, 2, 3 }),
  108. new ReadOnlySlice<Vector>(new Vector[] { new Vector(1, 1), new Vector(2, 2), new Vector(3, 3) }),
  109. text,
  110. new ReadOnlySlice<ushort>(new ushort[] { 1, 2, 3 }));
  111. }
  112. }
  113. class HeadlessFontManagerStub : IFontManagerImpl
  114. {
  115. public IGlyphTypefaceImpl CreateGlyphTypeface(Typeface typeface)
  116. {
  117. return new HeadlessGlyphTypefaceImpl();
  118. }
  119. public string GetDefaultFontFamilyName()
  120. {
  121. return "Arial";
  122. }
  123. public IEnumerable<string> GetInstalledFontFamilyNames(bool checkForUpdates = false)
  124. {
  125. return new List<string> { "Arial" };
  126. }
  127. public bool TryMatchCharacter(int codepoint, FontStyle fontStyle, FontWeight fontWeight, FontFamily fontFamily, CultureInfo culture, out Typeface typeface)
  128. {
  129. typeface = new Typeface("Arial", fontStyle, fontWeight);
  130. return true;
  131. }
  132. }
  133. class HeadlessIconLoaderStub : IPlatformIconLoader
  134. {
  135. class IconStub : IWindowIconImpl
  136. {
  137. public void Save(Stream outputStream)
  138. {
  139. }
  140. }
  141. public IWindowIconImpl LoadIcon(string fileName)
  142. {
  143. return new IconStub();
  144. }
  145. public IWindowIconImpl LoadIcon(Stream stream)
  146. {
  147. return new IconStub();
  148. }
  149. public IWindowIconImpl LoadIcon(IBitmapImpl bitmap)
  150. {
  151. return new IconStub();
  152. }
  153. }
  154. class HeadlessScreensStub : IScreenImpl
  155. {
  156. public int ScreenCount { get; } = 1;
  157. public IReadOnlyList<Screen> AllScreens { get; } = new[]
  158. {
  159. new Screen(1, new PixelRect(0, 0, 1920, 1280),
  160. new PixelRect(0, 0, 1920, 1280), true),
  161. };
  162. }
  163. }