Application.cs 6.5 KB

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