WindowBase.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. /// <summary>
  31. /// Defines the <see cref="Owner"/> property.
  32. /// </summary>
  33. public static readonly DirectProperty<WindowBase, WindowBase> OwnerProperty =
  34. AvaloniaProperty.RegisterDirect<WindowBase, WindowBase>(
  35. nameof(Owner),
  36. o => o.Owner,
  37. (o, v) => o.Owner = v);
  38. private bool _hasExecutedInitialLayoutPass;
  39. private bool _isActive;
  40. private bool _ignoreVisibilityChange;
  41. private WindowBase _owner;
  42. static WindowBase()
  43. {
  44. IsVisibleProperty.OverrideDefaultValue<WindowBase>(false);
  45. IsVisibleProperty.Changed.AddClassHandler<WindowBase>(x => x.IsVisibleChanged);
  46. MinWidthProperty.Changed.AddClassHandler<WindowBase>((w, e) => w.PlatformImpl?.SetMinMaxSize(new Size((double)e.NewValue, w.MinHeight), new Size(w.MaxWidth, w.MaxHeight)));
  47. MinHeightProperty.Changed.AddClassHandler<WindowBase>((w, e) => w.PlatformImpl?.SetMinMaxSize(new Size(w.MinWidth, (double)e.NewValue), new Size(w.MaxWidth, w.MaxHeight)));
  48. MaxWidthProperty.Changed.AddClassHandler<WindowBase>((w, e) => w.PlatformImpl?.SetMinMaxSize(new Size(w.MinWidth, w.MinHeight), new Size((double)e.NewValue, w.MaxHeight)));
  49. MaxHeightProperty.Changed.AddClassHandler<WindowBase>((w, e) => w.PlatformImpl?.SetMinMaxSize(new Size(w.MinWidth, w.MinHeight), new Size(w.MaxWidth, (double)e.NewValue)));
  50. }
  51. public WindowBase(IWindowBaseImpl impl) : this(impl, AvaloniaLocator.Current)
  52. {
  53. }
  54. public WindowBase(IWindowBaseImpl impl, IAvaloniaDependencyResolver dependencyResolver) : base(impl, dependencyResolver)
  55. {
  56. impl.Activated = HandleActivated;
  57. impl.Deactivated = HandleDeactivated;
  58. impl.PositionChanged = HandlePositionChanged;
  59. this.GetObservable(ClientSizeProperty).Skip(1).Subscribe(x => PlatformImpl?.Resize(x));
  60. }
  61. /// <summary>
  62. /// Fired when the window is activated.
  63. /// </summary>
  64. public event EventHandler Activated;
  65. /// <summary>
  66. /// Fired when the window is deactivated.
  67. /// </summary>
  68. public event EventHandler Deactivated;
  69. /// <summary>
  70. /// Fired when the window position is changed.
  71. /// </summary>
  72. public event EventHandler<PointEventArgs> PositionChanged;
  73. [CanBeNull]
  74. public new IWindowBaseImpl PlatformImpl => (IWindowBaseImpl) base.PlatformImpl;
  75. /// <summary>
  76. /// Gets a value that indicates whether the window is active.
  77. /// </summary>
  78. public bool IsActive
  79. {
  80. get { return _isActive; }
  81. private set { SetAndRaise(IsActiveProperty, ref _isActive, value); }
  82. }
  83. /// <summary>
  84. /// Gets or sets the window position in screen coordinates.
  85. /// </summary>
  86. public Point Position
  87. {
  88. get { return PlatformImpl?.Position ?? default(Point); }
  89. set
  90. {
  91. if (PlatformImpl is IWindowBaseImpl impl)
  92. impl.Position = value;
  93. }
  94. }
  95. /// <summary>
  96. /// Whether an auto-size operation is in progress.
  97. /// </summary>
  98. protected bool AutoSizing
  99. {
  100. get;
  101. private set;
  102. }
  103. /// <summary>
  104. /// Gets or sets the owner of the window.
  105. /// </summary>
  106. public WindowBase Owner
  107. {
  108. get { return _owner; }
  109. set { SetAndRaise(OwnerProperty, ref _owner, value); }
  110. }
  111. /// <summary>
  112. /// Activates the window.
  113. /// </summary>
  114. public void Activate()
  115. {
  116. PlatformImpl?.Activate();
  117. }
  118. /// <summary>
  119. /// Hides the popup.
  120. /// </summary>
  121. public virtual void Hide()
  122. {
  123. _ignoreVisibilityChange = true;
  124. try
  125. {
  126. Renderer?.Stop();
  127. PlatformImpl?.Hide();
  128. IsVisible = false;
  129. }
  130. finally
  131. {
  132. _ignoreVisibilityChange = false;
  133. }
  134. }
  135. /// <summary>
  136. /// Shows the popup.
  137. /// </summary>
  138. public virtual void Show()
  139. {
  140. _ignoreVisibilityChange = true;
  141. try
  142. {
  143. EnsureInitialized();
  144. IsVisible = true;
  145. if (!_hasExecutedInitialLayoutPass)
  146. {
  147. LayoutManager.Instance.ExecuteInitialLayoutPass(this);
  148. _hasExecutedInitialLayoutPass = true;
  149. }
  150. PlatformImpl?.Show();
  151. Renderer?.Start();
  152. }
  153. finally
  154. {
  155. _ignoreVisibilityChange = false;
  156. }
  157. }
  158. /// <summary>
  159. /// Begins an auto-resize operation.
  160. /// </summary>
  161. /// <returns>A disposable used to finish the operation.</returns>
  162. /// <remarks>
  163. /// When an auto-resize operation is in progress any resize events received will not be
  164. /// cause the new size to be written to the <see cref="Layoutable.Width"/> and
  165. /// <see cref="Layoutable.Height"/> properties.
  166. /// </remarks>
  167. protected IDisposable BeginAutoSizing()
  168. {
  169. AutoSizing = true;
  170. return Disposable.Create(() => AutoSizing = false);
  171. }
  172. /// <summary>
  173. /// Carries out the arrange pass of the window.
  174. /// </summary>
  175. /// <param name="finalSize">The final window size.</param>
  176. /// <returns>The <paramref name="finalSize"/> parameter unchanged.</returns>
  177. protected override Size ArrangeOverride(Size finalSize)
  178. {
  179. using (BeginAutoSizing())
  180. {
  181. PlatformImpl?.Resize(finalSize);
  182. }
  183. return base.ArrangeOverride(PlatformImpl?.ClientSize ?? default(Size));
  184. }
  185. /// <summary>
  186. /// Ensures that the window is initialized.
  187. /// </summary>
  188. protected void EnsureInitialized()
  189. {
  190. if (!IsInitialized)
  191. {
  192. var init = (ISupportInitialize)this;
  193. init.BeginInit();
  194. init.EndInit();
  195. }
  196. }
  197. protected override void HandleClosed()
  198. {
  199. _ignoreVisibilityChange = true;
  200. try
  201. {
  202. IsVisible = false;
  203. base.HandleClosed();
  204. }
  205. finally
  206. {
  207. _ignoreVisibilityChange = false;
  208. }
  209. }
  210. /// <summary>
  211. /// Handles a resize notification from <see cref="ITopLevelImpl.Resized"/>.
  212. /// </summary>
  213. /// <param name="clientSize">The new client size.</param>
  214. protected override void HandleResized(Size clientSize)
  215. {
  216. if (!AutoSizing)
  217. {
  218. Width = clientSize.Width;
  219. Height = clientSize.Height;
  220. }
  221. ClientSize = clientSize;
  222. LayoutManager.Instance.ExecuteLayoutPass();
  223. Renderer?.Resized(clientSize);
  224. }
  225. /// <summary>
  226. /// Handles a window position change notification from
  227. /// <see cref="IWindowBaseImpl.PositionChanged"/>.
  228. /// </summary>
  229. /// <param name="pos">The window position.</param>
  230. private void HandlePositionChanged(Point pos)
  231. {
  232. PositionChanged?.Invoke(this, new PointEventArgs(pos));
  233. }
  234. /// <summary>
  235. /// Handles an activated notification from <see cref="IWindowBaseImpl.Activated"/>.
  236. /// </summary>
  237. private void HandleActivated()
  238. {
  239. Activated?.Invoke(this, EventArgs.Empty);
  240. var scope = this as IFocusScope;
  241. if (scope != null)
  242. {
  243. FocusManager.Instance.SetFocusScope(scope);
  244. }
  245. IsActive = true;
  246. }
  247. /// <summary>
  248. /// Handles a deactivated notification from <see cref="IWindowBaseImpl.Deactivated"/>.
  249. /// </summary>
  250. private void HandleDeactivated()
  251. {
  252. IsActive = false;
  253. Deactivated?.Invoke(this, EventArgs.Empty);
  254. }
  255. private void IsVisibleChanged(AvaloniaPropertyChangedEventArgs e)
  256. {
  257. if (!_ignoreVisibilityChange)
  258. {
  259. if ((bool)e.NewValue)
  260. {
  261. Show();
  262. }
  263. else
  264. {
  265. Hide();
  266. }
  267. }
  268. }
  269. /// <summary>
  270. /// Starts moving a window with left button being held. Should be called from left mouse button press event handler
  271. /// </summary>
  272. public void BeginMoveDrag() => PlatformImpl?.BeginMoveDrag();
  273. /// <summary>
  274. /// Starts resizing a window. This function is used if an application has window resizing controls.
  275. /// Should be called from left mouse button press event handler
  276. /// </summary>
  277. public void BeginResizeDrag(WindowEdge edge) => PlatformImpl?.BeginResizeDrag(edge);
  278. }
  279. }