Application.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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.Animation;
  7. using Avalonia.Controls;
  8. using Avalonia.Controls.ApplicationLifetimes;
  9. using Avalonia.Controls.Templates;
  10. using Avalonia.Input;
  11. using Avalonia.Input.Platform;
  12. using Avalonia.Input.Raw;
  13. using Avalonia.Platform;
  14. using Avalonia.Rendering;
  15. using Avalonia.Styling;
  16. using Avalonia.Threading;
  17. namespace Avalonia
  18. {
  19. /// <summary>
  20. /// Encapsulates a Avalonia application.
  21. /// </summary>
  22. /// <remarks>
  23. /// The <see cref="Application"/> class encapsulates Avalonia application-specific
  24. /// functionality, including:
  25. /// - A global set of <see cref="DataTemplates"/>.
  26. /// - A global set of <see cref="Styles"/>.
  27. /// - A <see cref="FocusManager"/>.
  28. /// - An <see cref="InputManager"/>.
  29. /// - Registers services needed by the rest of Avalonia in the <see cref="RegisterServices"/>
  30. /// method.
  31. /// - Tracks the lifetime of the application.
  32. /// </remarks>
  33. public class Application : AvaloniaObject, IDataContextProvider, IGlobalDataTemplates, IGlobalStyles, IResourceNode
  34. {
  35. /// <summary>
  36. /// The application-global data templates.
  37. /// </summary>
  38. private DataTemplates _dataTemplates;
  39. private readonly Lazy<IClipboard> _clipboard =
  40. new Lazy<IClipboard>(() => (IClipboard)AvaloniaLocator.Current.GetService(typeof(IClipboard)));
  41. private readonly Styler _styler = new Styler();
  42. private Styles _styles;
  43. private IResourceDictionary _resources;
  44. private bool _notifyingResourcesChanged;
  45. /// <summary>
  46. /// Defines the <see cref="DataContext"/> property.
  47. /// </summary>
  48. public static readonly StyledProperty<object> DataContextProperty =
  49. StyledElement.DataContextProperty.AddOwner<Application>();
  50. /// <inheritdoc/>
  51. public event EventHandler<ResourcesChangedEventArgs> ResourcesChanged;
  52. /// <summary>
  53. /// Creates an instance of the <see cref="Application"/> class.
  54. /// </summary>
  55. public Application()
  56. {
  57. Name = "Avalonia Application";
  58. }
  59. /// <summary>
  60. /// Gets or sets the Applications's data context.
  61. /// </summary>
  62. /// <remarks>
  63. /// The data context property specifies the default object that will
  64. /// be used for data binding.
  65. /// </remarks>
  66. public object DataContext
  67. {
  68. get { return GetValue(DataContextProperty); }
  69. set { SetValue(DataContextProperty, value); }
  70. }
  71. /// <summary>
  72. /// Gets the current instance of the <see cref="Application"/> class.
  73. /// </summary>
  74. /// <value>
  75. /// The current instance of the <see cref="Application"/> class.
  76. /// </value>
  77. public static Application Current
  78. {
  79. get { return AvaloniaLocator.Current.GetService<Application>(); }
  80. }
  81. /// <summary>
  82. /// Gets or sets the application's global data templates.
  83. /// </summary>
  84. /// <value>
  85. /// The application's global data templates.
  86. /// </value>
  87. public DataTemplates DataTemplates => _dataTemplates ?? (_dataTemplates = new DataTemplates());
  88. /// <summary>
  89. /// Gets the application's focus manager.
  90. /// </summary>
  91. /// <value>
  92. /// The application's focus manager.
  93. /// </value>
  94. public IFocusManager FocusManager
  95. {
  96. get;
  97. private set;
  98. }
  99. /// <summary>
  100. /// Gets the application's input manager.
  101. /// </summary>
  102. /// <value>
  103. /// The application's input manager.
  104. /// </value>
  105. public InputManager InputManager
  106. {
  107. get;
  108. private set;
  109. }
  110. /// <summary>
  111. /// Gets the application clipboard.
  112. /// </summary>
  113. public IClipboard Clipboard => _clipboard.Value;
  114. /// <summary>
  115. /// Gets the application's global resource dictionary.
  116. /// </summary>
  117. public IResourceDictionary Resources
  118. {
  119. get => _resources ?? (Resources = new ResourceDictionary());
  120. set
  121. {
  122. Contract.Requires<ArgumentNullException>(value != null);
  123. var hadResources = false;
  124. if (_resources != null)
  125. {
  126. hadResources = _resources.Count > 0;
  127. _resources.ResourcesChanged -= ThisResourcesChanged;
  128. }
  129. _resources = value;
  130. _resources.ResourcesChanged += ThisResourcesChanged;
  131. if (hadResources || _resources.Count > 0)
  132. {
  133. ResourcesChanged?.Invoke(this, new ResourcesChangedEventArgs());
  134. }
  135. }
  136. }
  137. /// <summary>
  138. /// Gets the application's global styles.
  139. /// </summary>
  140. /// <value>
  141. /// The application's global styles.
  142. /// </value>
  143. /// <remarks>
  144. /// Global styles apply to all windows in the application.
  145. /// </remarks>
  146. public Styles Styles
  147. {
  148. get
  149. {
  150. if (_styles == null)
  151. {
  152. _styles = new Styles(this);
  153. _styles.ResourcesChanged += ThisResourcesChanged;
  154. }
  155. return _styles;
  156. }
  157. }
  158. /// <inheritdoc/>
  159. bool IDataTemplateHost.IsDataTemplatesInitialized => _dataTemplates != null;
  160. /// <summary>
  161. /// Gets the styling parent of the application, which is null.
  162. /// </summary>
  163. IStyleHost IStyleHost.StylingParent => null;
  164. /// <inheritdoc/>
  165. bool IStyleHost.IsStylesInitialized => _styles != null;
  166. /// <inheritdoc/>
  167. bool IResourceProvider.HasResources => _resources?.Count > 0;
  168. /// <inheritdoc/>
  169. IResourceNode IResourceNode.ResourceParent => null;
  170. /// <summary>
  171. /// Application lifetime, use it for things like setting the main window and exiting the app from code
  172. /// Currently supported lifetimes are:
  173. /// - <see cref="IClassicDesktopStyleApplicationLifetime"/>
  174. /// - <see cref="ISingleViewApplicationLifetime"/>
  175. /// - <see cref="IControlledApplicationLifetime"/>
  176. /// </summary>
  177. public IApplicationLifetime ApplicationLifetime { get; set; }
  178. /// <summary>
  179. /// Initializes the application by loading XAML etc.
  180. /// </summary>
  181. public virtual void Initialize() { }
  182. /// <inheritdoc/>
  183. bool IResourceProvider.TryGetResource(object key, out object value)
  184. {
  185. value = null;
  186. return (_resources?.TryGetResource(key, out value) ?? false) ||
  187. Styles.TryGetResource(key, out value);
  188. }
  189. /// <summary>
  190. /// Register's the services needed by Avalonia.
  191. /// </summary>
  192. public virtual void RegisterServices()
  193. {
  194. AvaloniaSynchronizationContext.InstallIfNeeded();
  195. FocusManager = new FocusManager();
  196. InputManager = new InputManager();
  197. AvaloniaLocator.CurrentMutable
  198. .Bind<IAccessKeyHandler>().ToTransient<AccessKeyHandler>()
  199. .Bind<IGlobalDataTemplates>().ToConstant(this)
  200. .Bind<IGlobalStyles>().ToConstant(this)
  201. .Bind<IFocusManager>().ToConstant(FocusManager)
  202. .Bind<IInputManager>().ToConstant(InputManager)
  203. .Bind<IKeyboardNavigationHandler>().ToTransient<KeyboardNavigationHandler>()
  204. .Bind<IStyler>().ToConstant(_styler)
  205. .Bind<IScheduler>().ToConstant(AvaloniaScheduler.Instance)
  206. .Bind<IDragDropDevice>().ToConstant(DragDropDevice.Instance)
  207. .Bind<IPlatformDragSource>().ToTransient<InProcessDragSource>();
  208. var clock = new RenderLoopClock();
  209. AvaloniaLocator.CurrentMutable
  210. .Bind<IGlobalClock>().ToConstant(clock)
  211. .GetService<IRenderLoop>()?.Add(clock);
  212. }
  213. public virtual void OnFrameworkInitializationCompleted()
  214. {
  215. }
  216. private void NotifyResourcesChanged(ResourcesChangedEventArgs e)
  217. {
  218. if (_notifyingResourcesChanged)
  219. {
  220. return;
  221. }
  222. try
  223. {
  224. _notifyingResourcesChanged = true;
  225. (_resources as ISetResourceParent)?.ParentResourcesChanged(e);
  226. (_styles as ISetResourceParent)?.ParentResourcesChanged(e);
  227. ResourcesChanged?.Invoke(this, new ResourcesChangedEventArgs());
  228. }
  229. finally
  230. {
  231. _notifyingResourcesChanged = false;
  232. }
  233. }
  234. private void ThisResourcesChanged(object sender, ResourcesChangedEventArgs e)
  235. {
  236. NotifyResourcesChanged(e);
  237. }
  238. private string _name;
  239. /// <summary>
  240. /// Defines Name property
  241. /// </summary>
  242. public static readonly DirectProperty<Application, string> NameProperty =
  243. AvaloniaProperty.RegisterDirect<Application, string>("Name", o => o.Name, (o, v) => o.Name = v);
  244. /// <summary>
  245. /// Application name to be used for various platform-specific purposes
  246. /// </summary>
  247. public string Name
  248. {
  249. get => _name;
  250. set => SetAndRaise(NameProperty, ref _name, value);
  251. }
  252. }
  253. }