AvaloniaNativePlatform.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.Runtime.InteropServices;
  5. using Avalonia.Controls.Platform;
  6. using Avalonia.Input;
  7. using Avalonia.Input.Platform;
  8. using Avalonia.Native.Interop;
  9. using Avalonia.OpenGL;
  10. using Avalonia.Platform;
  11. using Avalonia.Rendering;
  12. using Avalonia.Platform.Interop;
  13. namespace Avalonia.Native
  14. {
  15. class AvaloniaNativePlatform : IPlatformSettings, IWindowingPlatform
  16. {
  17. private readonly IAvaloniaNativeFactory _factory;
  18. private AvaloniaNativePlatformOptions _options;
  19. [DllImport("libAvaloniaNative")]
  20. static extern IntPtr CreateAvaloniaNative();
  21. internal static readonly KeyboardDevice KeyboardDevice = new KeyboardDevice();
  22. public Size DoubleClickSize => new Size(4, 4);
  23. public TimeSpan DoubleClickTime => TimeSpan.FromMilliseconds(500); //TODO
  24. public static AvaloniaNativePlatform Initialize(IntPtr factory, AvaloniaNativePlatformOptions options)
  25. {
  26. var result = new AvaloniaNativePlatform(new IAvaloniaNativeFactory(factory));
  27. result.DoInitialize(options);
  28. return result;
  29. }
  30. delegate IntPtr CreateAvaloniaNativeDelegate();
  31. public static AvaloniaNativePlatform Initialize(AvaloniaNativePlatformOptions options)
  32. {
  33. if (options.AvaloniaNativeLibraryPath != null)
  34. {
  35. var loader = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
  36. (IDynLoader)new Win32Loader() :
  37. new UnixLoader();
  38. var lib = loader.LoadLibrary(options.AvaloniaNativeLibraryPath);
  39. var proc = loader.GetProcAddress(lib, "CreateAvaloniaNative", false);
  40. var d = Marshal.GetDelegateForFunctionPointer<CreateAvaloniaNativeDelegate>(proc);
  41. return Initialize(d(), options);
  42. }
  43. else
  44. return Initialize(CreateAvaloniaNative(), options);
  45. }
  46. public void SetupApplicationMenuExporter ()
  47. {
  48. var exporter = new AvaloniaNativeMenuExporter(_factory);
  49. }
  50. public void SetupApplicationName ()
  51. {
  52. if(!string.IsNullOrWhiteSpace(Application.Current.Name))
  53. {
  54. using (var buffer = new Utf8Buffer(Application.Current.Name))
  55. {
  56. _factory.MacOptions.SetApplicationTitle(buffer.DangerousGetHandle());
  57. }
  58. }
  59. }
  60. private AvaloniaNativePlatform(IAvaloniaNativeFactory factory)
  61. {
  62. _factory = factory;
  63. }
  64. void DoInitialize(AvaloniaNativePlatformOptions options)
  65. {
  66. _options = options;
  67. _factory.Initialize();
  68. if (_factory.MacOptions != null)
  69. {
  70. var macOpts = AvaloniaLocator.Current.GetService<MacOSPlatformOptions>();
  71. _factory.MacOptions.ShowInDock = macOpts?.ShowInDock != false ? 1 : 0;
  72. }
  73. AvaloniaLocator.CurrentMutable
  74. .Bind<IPlatformThreadingInterface>()
  75. .ToConstant(new PlatformThreadingInterface(_factory.CreatePlatformThreadingInterface()))
  76. .Bind<IStandardCursorFactory>().ToConstant(new CursorFactory(_factory.CreateCursorFactory()))
  77. .Bind<IPlatformIconLoader>().ToSingleton<IconLoader>()
  78. .Bind<IKeyboardDevice>().ToConstant(KeyboardDevice)
  79. .Bind<IPlatformSettings>().ToConstant(this)
  80. .Bind<IWindowingPlatform>().ToConstant(this)
  81. .Bind<IClipboard>().ToConstant(new ClipboardImpl(_factory.CreateClipboard()))
  82. .Bind<IRenderLoop>().ToConstant(new RenderLoop())
  83. .Bind<IRenderTimer>().ToConstant(new DefaultRenderTimer(60))
  84. .Bind<ISystemDialogImpl>().ToConstant(new SystemDialogs(_factory.CreateSystemDialogs()))
  85. .Bind<IWindowingPlatformGlFeature>().ToConstant(new GlPlatformFeature(_factory.ObtainGlFeature()))
  86. .Bind<PlatformHotkeyConfiguration>().ToConstant(new PlatformHotkeyConfiguration(KeyModifiers.Meta))
  87. .Bind<IMountedVolumeInfoProvider>().ToConstant(new MacOSMountedVolumeInfoProvider());
  88. }
  89. public IWindowImpl CreateWindow()
  90. {
  91. return new WindowImpl(_factory, _options);
  92. }
  93. public IEmbeddableWindowImpl CreateEmbeddableWindow()
  94. {
  95. throw new NotImplementedException();
  96. }
  97. }
  98. public class AvaloniaNativeMacOptions
  99. {
  100. private readonly IAvnMacOptions _opts;
  101. private bool _showInDock;
  102. internal AvaloniaNativeMacOptions(IAvnMacOptions opts)
  103. {
  104. _opts = opts;
  105. ShowInDock = true;
  106. }
  107. public bool ShowInDock
  108. {
  109. get => _showInDock;
  110. set
  111. {
  112. _showInDock = value;
  113. _opts.ShowInDock = value ? 1 : 0;
  114. }
  115. }
  116. }
  117. }