WindowImpl.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.Platform;
  4. using Avalonia.Input;
  5. using Avalonia.Input.Raw;
  6. using Avalonia.Native.Interop;
  7. using Avalonia.OpenGL;
  8. using Avalonia.Platform;
  9. using Avalonia.Platform.Interop;
  10. namespace Avalonia.Native
  11. {
  12. public class WindowImpl : WindowBaseImpl, IWindowImpl, ITopLevelImplWithNativeMenuExporter
  13. {
  14. private readonly IAvaloniaNativeFactory _factory;
  15. private readonly AvaloniaNativePlatformOptions _opts;
  16. private readonly GlPlatformFeature _glFeature;
  17. IAvnWindow _native;
  18. internal WindowImpl(IAvaloniaNativeFactory factory, AvaloniaNativePlatformOptions opts,
  19. GlPlatformFeature glFeature) : base(opts, glFeature)
  20. {
  21. _factory = factory;
  22. _opts = opts;
  23. _glFeature = glFeature;
  24. using (var e = new WindowEvents(this))
  25. {
  26. var context = _opts.UseGpu ? glFeature?.DeferredContext : null;
  27. Init(_native = factory.CreateWindow(e, context?.Context), factory.CreateScreens(), context);
  28. }
  29. NativeMenuExporter = new AvaloniaNativeMenuExporter(_native, factory);
  30. }
  31. class WindowEvents : WindowBaseEvents, IAvnWindowEvents
  32. {
  33. readonly WindowImpl _parent;
  34. public WindowEvents(WindowImpl parent) : base(parent)
  35. {
  36. _parent = parent;
  37. }
  38. bool IAvnWindowEvents.Closing()
  39. {
  40. if (_parent.Closing != null)
  41. {
  42. return _parent.Closing();
  43. }
  44. return true;
  45. }
  46. void IAvnWindowEvents.WindowStateChanged(AvnWindowState state)
  47. {
  48. _parent.WindowStateChanged?.Invoke((WindowState)state);
  49. }
  50. void IAvnWindowEvents.GotInputWhenDisabled()
  51. {
  52. _parent.GotInputWhenDisabled?.Invoke();
  53. }
  54. }
  55. public IAvnWindow Native => _native;
  56. public void CanResize(bool value)
  57. {
  58. _native.CanResize = value;
  59. }
  60. public void SetSystemDecorations(Controls.SystemDecorations enabled)
  61. {
  62. _native.Decorations = (Interop.SystemDecorations)enabled;
  63. }
  64. public void SetTitleBarColor(Avalonia.Media.Color color)
  65. {
  66. _native.SetTitleBarColor(new AvnColor { Alpha = color.A, Red = color.R, Green = color.G, Blue = color.B });
  67. }
  68. public void SetTitle(string title)
  69. {
  70. using (var buffer = new Utf8Buffer(title))
  71. {
  72. _native.SetTitle(buffer.DangerousGetHandle());
  73. }
  74. }
  75. public WindowState WindowState
  76. {
  77. get
  78. {
  79. return (WindowState)_native.GetWindowState();
  80. }
  81. set
  82. {
  83. _native.SetWindowState((AvnWindowState)value);
  84. }
  85. }
  86. public Action<WindowState> WindowStateChanged { get; set; }
  87. public Action<bool> ExtendClientAreaToDecorationsChanged { get; set; }
  88. public Thickness ExtendedMargins { get; } = new Thickness(0, 20, 0, 0);
  89. public Thickness OffScreenMargin { get; } = new Thickness();
  90. private bool _isExtended;
  91. public bool IsClientAreaExtendedToDecorations => _isExtended;
  92. protected override bool ChromeHitTest (RawPointerEventArgs e)
  93. {
  94. if(_isExtended)
  95. {
  96. if(e.Type == RawPointerEventType.LeftButtonDown)
  97. {
  98. var visual = (_inputRoot as Window).Renderer.HitTestFirst(e.Position, _inputRoot as Window, x =>
  99. {
  100. if (x is IInputElement ie && !ie.IsHitTestVisible)
  101. {
  102. return false;
  103. }
  104. return true;
  105. });
  106. if(visual == null)
  107. {
  108. _native.BeginMoveDrag();
  109. }
  110. }
  111. }
  112. return false;
  113. }
  114. public void SetExtendClientAreaToDecorationsHint(bool extendIntoClientAreaHint)
  115. {
  116. _isExtended = extendIntoClientAreaHint;
  117. _native.SetExtendClientArea(extendIntoClientAreaHint);
  118. ExtendClientAreaToDecorationsChanged?.Invoke(true);
  119. }
  120. public void SetExtendClientAreaChromeHints(ExtendClientAreaChromeHints hints)
  121. {
  122. }
  123. public void SetExtendClientAreaTitleBarHeightHint(double titleBarHeight)
  124. {
  125. }
  126. public void ShowTaskbarIcon(bool value)
  127. {
  128. // NO OP On OSX
  129. }
  130. public void SetIcon(IWindowIconImpl icon)
  131. {
  132. // NO OP on OSX
  133. }
  134. public Func<bool> Closing { get; set; }
  135. public ITopLevelNativeMenuExporter NativeMenuExporter { get; }
  136. public void Move(PixelPoint point) => Position = point;
  137. public override IPopupImpl CreatePopup() =>
  138. _opts.OverlayPopups ? null : new PopupImpl(_factory, _opts, _glFeature, this);
  139. public Action GotInputWhenDisabled { get; set; }
  140. public void SetParent(IWindowImpl parent)
  141. {
  142. _native.SetParent(((WindowImpl)parent).Native);
  143. }
  144. public void SetEnabled(bool enable)
  145. {
  146. _native.SetEnabled(enable);
  147. }
  148. }
  149. }