Application.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.Threading;
  5. using Avalonia.Controls;
  6. using Avalonia.Controls.Templates;
  7. using Avalonia.Input;
  8. using Avalonia.Input.Platform;
  9. using Avalonia.Layout;
  10. using Avalonia.Rendering;
  11. using Avalonia.Styling;
  12. using Avalonia.Threading;
  13. namespace Avalonia
  14. {
  15. /// <summary>
  16. /// Encapsulates a Avalonia application.
  17. /// </summary>
  18. /// <remarks>
  19. /// The <see cref="Application"/> class encapsulates Avalonia application-specific
  20. /// functionality, including:
  21. /// - A global set of <see cref="DataTemplates"/>.
  22. /// - A global set of <see cref="Styles"/>.
  23. /// - A <see cref="FocusManager"/>.
  24. /// - An <see cref="InputManager"/>.
  25. /// - Registers services needed by the rest of Avalonia in the <see cref="RegisterServices"/>
  26. /// method.
  27. /// - Tracks the lifetime of the application.
  28. /// </remarks>
  29. public class Application : IGlobalDataTemplates, IGlobalStyles, IStyleRoot, IApplicationLifecycle
  30. {
  31. /// <summary>
  32. /// The application-global data templates.
  33. /// </summary>
  34. private DataTemplates _dataTemplates;
  35. private readonly Lazy<IClipboard> _clipboard =
  36. new Lazy<IClipboard>(() => (IClipboard)AvaloniaLocator.Current.GetService(typeof(IClipboard)));
  37. private readonly Styler _styler = new Styler();
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="Application"/> class.
  40. /// </summary>
  41. public Application()
  42. {
  43. OnExit += OnExiting;
  44. }
  45. /// <summary>
  46. /// Gets the current instance of the <see cref="Application"/> class.
  47. /// </summary>
  48. /// <value>
  49. /// The current instance of the <see cref="Application"/> class.
  50. /// </value>
  51. public static Application Current
  52. {
  53. get { return AvaloniaLocator.Current.GetService<Application>(); }
  54. }
  55. /// <summary>
  56. /// Gets or sets the application's global data templates.
  57. /// </summary>
  58. /// <value>
  59. /// The application's global data templates.
  60. /// </value>
  61. public DataTemplates DataTemplates
  62. {
  63. get { return _dataTemplates ?? (_dataTemplates = new DataTemplates()); }
  64. set { _dataTemplates = value; }
  65. }
  66. /// <summary>
  67. /// Gets the application's focus manager.
  68. /// </summary>
  69. /// <value>
  70. /// The application's focus manager.
  71. /// </value>
  72. public IFocusManager FocusManager
  73. {
  74. get;
  75. private set;
  76. }
  77. /// <summary>
  78. /// Gets the application's input manager.
  79. /// </summary>
  80. /// <value>
  81. /// The application's input manager.
  82. /// </value>
  83. public InputManager InputManager
  84. {
  85. get;
  86. private set;
  87. }
  88. /// <summary>
  89. /// Gets the application clipboard.
  90. /// </summary>
  91. public IClipboard Clipboard => _clipboard.Value;
  92. /// <summary>
  93. /// Gets the application's global styles.
  94. /// </summary>
  95. /// <value>
  96. /// The application's global styles.
  97. /// </value>
  98. /// <remarks>
  99. /// Global styles apply to all windows in the application.
  100. /// </remarks>
  101. public Styles Styles { get; } = new Styles();
  102. /// <summary>
  103. /// Gets the styling parent of the application, which is null.
  104. /// </summary>
  105. IStyleHost IStyleHost.StylingParent => null;
  106. /// <summary>
  107. /// Initializes the application by loading XAML etc.
  108. /// </summary>
  109. public virtual void Initialize()
  110. {
  111. }
  112. /// <summary>
  113. /// Runs the application's main loop until the <see cref="ICloseable"/> is closed.
  114. /// </summary>
  115. /// <param name="closable">The closable to track</param>
  116. public void Run(ICloseable closable)
  117. {
  118. var source = new CancellationTokenSource();
  119. closable.Closed += OnExiting;
  120. closable.Closed += (s, e) => source.Cancel();
  121. Dispatcher.UIThread.MainLoop(source.Token);
  122. }
  123. /// <summary>
  124. /// Exits the application
  125. /// </summary>
  126. public void Exit()
  127. {
  128. OnExit?.Invoke(this, EventArgs.Empty);
  129. }
  130. /// <summary>
  131. /// Sent when the application is exiting.
  132. /// </summary>
  133. public event EventHandler OnExit;
  134. /// <summary>
  135. /// Called when the application is exiting.
  136. /// </summary>
  137. /// <param name="sender"></param>
  138. /// <param name="e"></param>
  139. protected virtual void OnExiting(object sender, EventArgs e)
  140. {
  141. }
  142. /// <summary>
  143. /// Register's the services needed by Avalonia.
  144. /// </summary>
  145. public virtual void RegisterServices()
  146. {
  147. AvaloniaSynchronizationContext.InstallIfNeeded();
  148. FocusManager = new FocusManager();
  149. InputManager = new InputManager();
  150. AvaloniaLocator.CurrentMutable
  151. .Bind<IAccessKeyHandler>().ToTransient<AccessKeyHandler>()
  152. .Bind<IGlobalDataTemplates>().ToConstant(this)
  153. .Bind<IGlobalStyles>().ToConstant(this)
  154. .Bind<IFocusManager>().ToConstant(FocusManager)
  155. .Bind<IInputManager>().ToConstant(InputManager)
  156. .Bind<IKeyboardNavigationHandler>().ToTransient<KeyboardNavigationHandler>()
  157. .Bind<IStyler>().ToConstant(_styler)
  158. .Bind<ILayoutManager>().ToSingleton<LayoutManager>()
  159. .Bind<IRenderer>().ToTransient<Renderer>()
  160. .Bind<IApplicationLifecycle>().ToConstant(this);
  161. }
  162. }
  163. }