WindowBase.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reactive.Disposables;
  5. using System.Reactive.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Avalonia.Controls.Primitives;
  9. using Avalonia.Input;
  10. using Avalonia.Layout;
  11. using Avalonia.Platform;
  12. using JetBrains.Annotations;
  13. namespace Avalonia.Controls
  14. {
  15. /// <summary>
  16. /// Base class for top-level windows.
  17. /// </summary>
  18. /// <remarks>
  19. /// This class acts as a base for top level windows such as <see cref="Window"/> and
  20. /// <see cref="PopupRoot"/>. It handles scheduling layout, styling and rendering as well as
  21. /// tracking the window <see cref="TopLevel.ClientSize"/> and <see cref="IsActive"/> state.
  22. /// </remarks>
  23. public class WindowBase : TopLevel
  24. {
  25. /// <summary>
  26. /// Defines the <see cref="IsActive"/> property.
  27. /// </summary>
  28. public static readonly DirectProperty<WindowBase, bool> IsActiveProperty =
  29. AvaloniaProperty.RegisterDirect<WindowBase, bool>(nameof(IsActive), o => o.IsActive);
  30. private bool _isActive;
  31. private bool _ignoreVisibilityChange;
  32. static WindowBase()
  33. {
  34. IsVisibleProperty.OverrideDefaultValue<WindowBase>(false);
  35. IsVisibleProperty.Changed.AddClassHandler<WindowBase>(x => x.IsVisibleChanged);
  36. }
  37. public WindowBase(IWindowBaseImpl impl) : this(impl, AvaloniaLocator.Current)
  38. {
  39. }
  40. public WindowBase(IWindowBaseImpl impl, IAvaloniaDependencyResolver dependencyResolver) : base(impl, dependencyResolver)
  41. {
  42. impl.Activated = HandleActivated;
  43. impl.Deactivated = HandleDeactivated;
  44. impl.PositionChanged = HandlePositionChanged;
  45. this.GetObservable(ClientSizeProperty).Skip(1).Subscribe(x => PlatformImpl?.Resize(x));
  46. }
  47. /// <summary>
  48. /// Fired when the window is activated.
  49. /// </summary>
  50. public event EventHandler Activated;
  51. /// <summary>
  52. /// Fired when the window is deactivated.
  53. /// </summary>
  54. public event EventHandler Deactivated;
  55. /// <summary>
  56. /// Fired when the window position is changed.
  57. /// </summary>
  58. public event EventHandler<PointEventArgs> PositionChanged;
  59. [CanBeNull]
  60. public new IWindowBaseImpl PlatformImpl => (IWindowBaseImpl) base.PlatformImpl;
  61. /// <summary>
  62. /// Gets a value that indicates whether the window is active.
  63. /// </summary>
  64. public bool IsActive
  65. {
  66. get { return _isActive; }
  67. private set { SetAndRaise(IsActiveProperty, ref _isActive, value); }
  68. }
  69. /// <summary>
  70. /// Gets or sets the window position in screen coordinates.
  71. /// </summary>
  72. public Point Position
  73. {
  74. get { return PlatformImpl?.Position ?? default(Point); }
  75. set
  76. {
  77. if (PlatformImpl is IWindowBaseImpl impl)
  78. impl.Position = value;
  79. }
  80. }
  81. /// <summary>
  82. /// Whether an auto-size operation is in progress.
  83. /// </summary>
  84. protected bool AutoSizing
  85. {
  86. get;
  87. private set;
  88. }
  89. /// <summary>
  90. /// Activates the window.
  91. /// </summary>
  92. public void Activate()
  93. {
  94. PlatformImpl?.Activate();
  95. }
  96. /// <summary>
  97. /// Hides the popup.
  98. /// </summary>
  99. public virtual void Hide()
  100. {
  101. _ignoreVisibilityChange = true;
  102. try
  103. {
  104. PlatformImpl?.Hide();
  105. IsVisible = false;
  106. }
  107. finally
  108. {
  109. _ignoreVisibilityChange = false;
  110. }
  111. }
  112. /// <summary>
  113. /// Shows the popup.
  114. /// </summary>
  115. public virtual void Show()
  116. {
  117. _ignoreVisibilityChange = true;
  118. try
  119. {
  120. EnsureInitialized();
  121. IsVisible = true;
  122. LayoutManager.ExecuteInitialLayoutPass(this);
  123. PlatformImpl?.Show();
  124. }
  125. finally
  126. {
  127. _ignoreVisibilityChange = false;
  128. }
  129. }
  130. /// <summary>
  131. /// Begins an auto-resize operation.
  132. /// </summary>
  133. /// <returns>A disposable used to finish the operation.</returns>
  134. /// <remarks>
  135. /// When an auto-resize operation is in progress any resize events received will not be
  136. /// cause the new size to be written to the <see cref="Layoutable.Width"/> and
  137. /// <see cref="Layoutable.Height"/> properties.
  138. /// </remarks>
  139. protected IDisposable BeginAutoSizing()
  140. {
  141. AutoSizing = true;
  142. return Disposable.Create(() => AutoSizing = false);
  143. }
  144. /// <summary>
  145. /// Carries out the arrange pass of the window.
  146. /// </summary>
  147. /// <param name="finalSize">The final window size.</param>
  148. /// <returns>The <paramref name="finalSize"/> parameter unchanged.</returns>
  149. protected override Size ArrangeOverride(Size finalSize)
  150. {
  151. using (BeginAutoSizing())
  152. {
  153. PlatformImpl?.Resize(finalSize);
  154. }
  155. return base.ArrangeOverride(PlatformImpl?.ClientSize ?? default(Size));
  156. }
  157. /// <summary>
  158. /// Ensures that the window is initialized.
  159. /// </summary>
  160. protected void EnsureInitialized()
  161. {
  162. if (!IsInitialized)
  163. {
  164. var init = (ISupportInitialize)this;
  165. init.BeginInit();
  166. init.EndInit();
  167. }
  168. }
  169. protected override void HandleClosed()
  170. {
  171. _ignoreVisibilityChange = true;
  172. try
  173. {
  174. IsVisible = false;
  175. base.HandleClosed();
  176. }
  177. finally
  178. {
  179. _ignoreVisibilityChange = false;
  180. }
  181. }
  182. /// <summary>
  183. /// Handles a resize notification from <see cref="ITopLevelImpl.Resized"/>.
  184. /// </summary>
  185. /// <param name="clientSize">The new client size.</param>
  186. protected override void HandleResized(Size clientSize)
  187. {
  188. if (!AutoSizing)
  189. {
  190. Width = clientSize.Width;
  191. Height = clientSize.Height;
  192. }
  193. ClientSize = clientSize;
  194. LayoutManager.ExecuteLayoutPass();
  195. Renderer?.Resized(clientSize);
  196. }
  197. /// <summary>
  198. /// Handles a window position change notification from
  199. /// <see cref="IWindowBaseImpl.PositionChanged"/>.
  200. /// </summary>
  201. /// <param name="pos">The window position.</param>
  202. private void HandlePositionChanged(Point pos)
  203. {
  204. PositionChanged?.Invoke(this, new PointEventArgs(pos));
  205. }
  206. /// <summary>
  207. /// Handles an activated notification from <see cref="IWindowBaseImpl.Activated"/>.
  208. /// </summary>
  209. private void HandleActivated()
  210. {
  211. Activated?.Invoke(this, EventArgs.Empty);
  212. var scope = this as IFocusScope;
  213. if (scope != null)
  214. {
  215. FocusManager.Instance.SetFocusScope(scope);
  216. }
  217. IsActive = true;
  218. }
  219. /// <summary>
  220. /// Handles a deactivated notification from <see cref="IWindowBaseImpl.Deactivated"/>.
  221. /// </summary>
  222. private void HandleDeactivated()
  223. {
  224. IsActive = false;
  225. Deactivated?.Invoke(this, EventArgs.Empty);
  226. }
  227. private void IsVisibleChanged(AvaloniaPropertyChangedEventArgs e)
  228. {
  229. if (!_ignoreVisibilityChange)
  230. {
  231. if ((bool)e.NewValue)
  232. {
  233. Show();
  234. }
  235. else
  236. {
  237. Hide();
  238. }
  239. }
  240. }
  241. /// <summary>
  242. /// Starts moving a window with left button being held. Should be called from left mouse button press event handler
  243. /// </summary>
  244. public void BeginMoveDrag() => PlatformImpl?.BeginMoveDrag();
  245. /// <summary>
  246. /// Starts resizing a window. This function is used if an application has window resizing controls.
  247. /// Should be called from left mouse button press event handler
  248. /// </summary>
  249. public void BeginResizeDrag(WindowEdge edge) => PlatformImpl?.BeginResizeDrag(edge);
  250. }
  251. }