IPanel.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using InABox.Configuration;
  8. using InABox.Core;
  9. namespace InABox.Wpf;
  10. public interface IPanelActionItem
  11. {
  12. }
  13. public interface IPanelActionEntry
  14. {
  15. object? Data { get; set; }
  16. string? Caption { get; set; }
  17. Bitmap? Image { get; set; }
  18. void Execute();
  19. }
  20. public class PanelActionEntry<T> : DependencyObject, IPanelActionEntry
  21. {
  22. public static readonly DependencyProperty DataProperty = DependencyProperty.Register(
  23. nameof(Data),
  24. typeof(T),
  25. typeof(PanelActionEntry<T>)
  26. );
  27. public T? Data { get; set; }
  28. object? IPanelActionEntry.Data
  29. {
  30. get => this.Data;
  31. set => this.Data = value == null ? default(T) : (T)value;
  32. }
  33. public static readonly DependencyProperty CaptionProperty = DependencyProperty.Register(
  34. nameof(Caption),
  35. typeof(string),
  36. typeof(PanelActionEntry<T>),
  37. new PropertyMetadata("")
  38. );
  39. public string? Caption { get; set; }
  40. public static readonly DependencyProperty ImageProperty = DependencyProperty.Register(
  41. nameof(Image),
  42. typeof(Bitmap),
  43. typeof(PanelActionEntry<T>)
  44. );
  45. public Bitmap? Image
  46. {
  47. get => GetValue(ImageProperty) as Bitmap;
  48. set => SetValue(ImageProperty, value);
  49. }
  50. public Action<PanelActionEntry<T>>? OnExecute { get; set; }
  51. public PanelActionEntry()
  52. {
  53. }
  54. public PanelActionEntry(string? caption, Bitmap? image, T? data, Action<PanelActionEntry<T>>? onExecute)
  55. {
  56. Data = data;
  57. Caption = caption;
  58. Image = image;
  59. OnExecute = onExecute;
  60. }
  61. public void Execute()
  62. {
  63. OnExecute?.Invoke(this);
  64. }
  65. }
  66. public class PanelAction : DependencyObject, IPanelActionItem
  67. {
  68. public Func<PanelAction,IPanelActionEntry[]>? OnPopulate { get; set; }
  69. public Action<PanelAction>? OnExecute { get; set; }
  70. public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.Register(
  71. nameof(IsEnabled),
  72. typeof(bool),
  73. typeof(PanelAction),
  74. new PropertyMetadata(true)
  75. );
  76. public bool IsEnabled
  77. {
  78. get => (bool)GetValue(IsEnabledProperty);
  79. set => SetValue(IsEnabledProperty, value);
  80. }
  81. public static readonly DependencyProperty VisibilityProperty = DependencyProperty.Register(
  82. nameof(Visibility),
  83. typeof(Visibility),
  84. typeof(PanelAction),
  85. new PropertyMetadata(Visibility.Visible)
  86. );
  87. public Visibility Visibility
  88. {
  89. get => (Visibility)GetValue(VisibilityProperty);
  90. set => SetValue(VisibilityProperty, value);
  91. }
  92. public static readonly DependencyProperty CaptionProperty = DependencyProperty.Register(
  93. nameof(Caption),
  94. typeof(string),
  95. typeof(PanelAction),
  96. new PropertyMetadata("")
  97. );
  98. public string Caption
  99. {
  100. get => (string)GetValue(CaptionProperty);
  101. set => SetValue(CaptionProperty, value);
  102. }
  103. public static readonly DependencyProperty ImageProperty = DependencyProperty.Register(
  104. nameof(Image),
  105. typeof(Bitmap),
  106. typeof(PanelAction)
  107. );
  108. public Bitmap? Image
  109. {
  110. get => GetValue(ImageProperty) as Bitmap;
  111. set => SetValue(ImageProperty, value);
  112. }
  113. public static readonly DependencyProperty MenuProperty = DependencyProperty.Register(
  114. nameof(Menu),
  115. typeof(ContextMenu),
  116. typeof(PanelAction)
  117. );
  118. public ContextMenu? Menu
  119. {
  120. get => GetValue(MenuProperty) as ContextMenu;
  121. set => SetValue(MenuProperty, value);
  122. }
  123. public PanelAction()
  124. {
  125. }
  126. public PanelAction(string caption, Bitmap? image, Action<PanelAction>? onExecute,
  127. Func<PanelAction, IPanelActionEntry[]>? onPopulate = null)
  128. {
  129. Caption = caption;
  130. Image = image;
  131. OnPopulate = onPopulate;
  132. OnExecute = onExecute;
  133. }
  134. public void Execute()
  135. {
  136. OnExecute?.Invoke(this);
  137. }
  138. public IPanelActionEntry[]? Populate()
  139. {
  140. return OnPopulate?.Invoke(this);
  141. }
  142. }
  143. public class PanelActionSeparator : IPanelActionItem
  144. {
  145. }
  146. public interface ICorePanel
  147. {
  148. void Setup();
  149. /// <summary>
  150. /// Shutdown the panel.
  151. /// </summary>
  152. /// <param name="cancel">If the operation can be cancelled, this is not <see langword="null"/></param>
  153. void Shutdown(CancelEventArgs? cancel);
  154. void Refresh();
  155. }
  156. public interface IBasePanel : ICorePanel, IDataModelSource
  157. {
  158. bool IsReady { get; set; }
  159. void CreateToolbarButtons(IPanelHost host);
  160. Dictionary<string, object[]> Selected();
  161. void Heartbeat(TimeSpan time);
  162. }
  163. public interface IPanel<T> : IBasePanel
  164. {
  165. }
  166. public interface IPropertiesPanel<TProperties>
  167. where TProperties : BaseObject, IGlobalConfigurationSettings, new()
  168. {
  169. public TProperties Properties { get; set; }
  170. }
  171. public interface IPropertiesPanel<TProperties, TSecurity> : IPropertiesPanel<TProperties>
  172. where TProperties : BaseObject, IGlobalConfigurationSettings, new()
  173. where TSecurity : ISecurityDescriptor, new()
  174. {
  175. }
  176. public interface IPanelHost
  177. {
  178. void CreatePanelAction(PanelAction action);
  179. void CreateReport(PanelAction action);
  180. void CreateSetupAction(PanelAction action);
  181. void CreateSetupSeparator();
  182. }
  183. public static class IPanelHostExtensions
  184. {
  185. public static void CreateSetupAction(this IPanelHost host, string caption, Bitmap image, Action<PanelAction> onExecute, ContextMenu? menu = null)
  186. {
  187. host.CreateSetupAction(new PanelAction(caption, image, onExecute) { Menu = menu });
  188. }
  189. public static void CreateSetupActionIf(this IPanelHost host, string caption, Bitmap image, Action<PanelAction> onExecute, bool canView, ContextMenu? menu = null)
  190. {
  191. if (canView)
  192. {
  193. host.CreateSetupAction(new PanelAction(caption, image, onExecute) { Menu = menu });
  194. }
  195. }
  196. public static void CreateSetupActionIf<TSecurity>(this IPanelHost host, string caption, Bitmap image, Action<PanelAction> onExecute, ContextMenu? menu = null)
  197. where TSecurity : ISecurityDescriptor, new()
  198. {
  199. host.CreateSetupActionIf(caption, image, onExecute, Security.IsAllowed<TSecurity>(), menu);
  200. }
  201. public static void CreateSetupActionIfCanView<T>(this IPanelHost host, string caption, Bitmap image, Action<PanelAction> onExecute, ContextMenu? menu = null)
  202. where T : Entity, new()
  203. {
  204. host.CreateSetupActionIf(caption, image, onExecute, Security.CanView<T>(), menu);
  205. }
  206. }
  207. /// <summary>
  208. /// Implement this interface to cause a class to act as a host of <see cref="ISubPanel"/>. Then, you can use the extension functions
  209. /// <see cref="ISubPanelHostExtensions.ShutdownSubPanels(ISubPanelHost, CancelEventArgs?)"/> and
  210. /// <see cref="ISubPanelHostExtensions.AddSubPanel(ISubPanelHost, ISubPanel)"/> to interact with it.
  211. /// </summary>
  212. /// <remarks>
  213. /// If you mark an <see cref="IPanel{T}"/> as an <see cref="ISubPanelHost"/>, then the shutdown method is called automatically by <see cref="IPanelHost"/>.
  214. /// </remarks>
  215. public interface ISubPanelHost
  216. {
  217. List<ISubPanel> SubPanels { get; }
  218. static ISubPanelHost Global = new GlobalSubPanelHost();
  219. private class GlobalSubPanelHost : ISubPanelHost
  220. {
  221. public List<ISubPanel> SubPanels { get; set; } = new();
  222. }
  223. }
  224. public static class ISubPanelHostExtensions
  225. {
  226. public static void ShutdownSubPanels(this ISubPanelHost host, CancelEventArgs? cancel)
  227. {
  228. ISubPanel[] panels;
  229. lock (host.SubPanels)
  230. {
  231. panels = host.SubPanels.ToArray();
  232. host.SubPanels.Clear();
  233. var isCancelled = false;
  234. foreach(var panel in panels)
  235. {
  236. if (isCancelled)
  237. {
  238. host.SubPanels.Add(panel);
  239. }
  240. else
  241. {
  242. panel.Shutdown(cancel);
  243. if (cancel?.Cancel == true)
  244. {
  245. isCancelled = true;
  246. host.SubPanels.Add(panel);
  247. }
  248. }
  249. }
  250. }
  251. }
  252. public static void AddSubPanel(this ISubPanelHost host, ISubPanel panel)
  253. {
  254. host.SubPanels.Add(panel);
  255. panel.SubPanelClosed += p =>
  256. {
  257. lock (host.SubPanels)
  258. {
  259. host.SubPanels.Remove(p);
  260. }
  261. };
  262. }
  263. }
  264. /// <summary>
  265. /// An <see cref="ISubPanel"/> is a non-modal window, which is tied to a parent
  266. /// </summary>
  267. public interface ISubPanel
  268. {
  269. public delegate void ClosedEvent(ISubPanel panel);
  270. /// <summary>
  271. /// Event to be called when a sub-panel closes itself; in this case, <see cref="Shutdown(CancelEventArgs?)"/> will not be called. This allows
  272. /// the host to get rid of the sub-panel, instead of keeping it forever.
  273. /// </summary>
  274. /// <remarks>
  275. /// You may call this after <see cref="Shutdown(CancelEventArgs?)"/> has been called.
  276. /// </remarks>
  277. public event ClosedEvent? SubPanelClosed;
  278. /// <summary>
  279. /// Shutdown the panel.
  280. /// </summary>
  281. /// <param name="cancel">If the operation can be cancelled, this is not <see langword="null"/></param>
  282. void Shutdown(CancelEventArgs? cancel);
  283. }