Application.cs 8.0 KB

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