25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

398 satır
17 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using System.Windows.Controls;
  9. using System.Windows;
  10. using System.Windows.Input;
  11. using BPA.UIControl.Commons;
  12. namespace BPA.UIControl
  13. {
  14. /// <summary>
  15. /// 通知操作类
  16. /// </summary>
  17. public class Notification
  18. {
  19. /// <summary>
  20. /// Transition 动画时长
  21. /// </summary>
  22. public const int TransitionTime = 300;
  23. /// <summary>
  24. /// 所有容器集合
  25. /// </summary>
  26. internal static Dictionary<string, NotificationContainer> Containers = new Dictionary<string, NotificationContainer>();
  27. /// <summary>
  28. /// 更新容器
  29. /// </summary>
  30. /// <param name="container">容器</param>
  31. /// <param name="identify">标识</param>
  32. internal static void UpdateContainer(NotificationContainer container, string identify)
  33. {
  34. if (Containers.ContainsKey(identify))
  35. {
  36. _ = Containers.Remove(identify);
  37. }
  38. Containers.Add(identify, container);
  39. }
  40. private static NotificationCard GetNotificationCard(NotificationType type, object content, string title, int millisecondTimeOut, bool isClearable)
  41. {
  42. var notificationCard = new NotificationCard
  43. {
  44. Type = type,
  45. Content = content,
  46. IsClearable = isClearable,
  47. };
  48. if (!string.IsNullOrEmpty(title))
  49. {
  50. notificationCard.Title = title;
  51. }
  52. return notificationCard;
  53. }
  54. /// <summary>
  55. /// 延期关闭通知卡片
  56. /// </summary>
  57. /// <param name="millisecondTimeOut">显示时间,为 0 时不自动关闭</param>
  58. /// <param name="notificationCard">通知卡片</param>
  59. /// <param name="token">取消令牌</param>
  60. private static void DelayCloseNotificationCard(int millisecondTimeOut, NotificationCard notificationCard, CancellationToken token)
  61. {
  62. if (millisecondTimeOut > 0)
  63. {
  64. _ = Task.Run(async () =>
  65. {
  66. await Task.Delay(millisecondTimeOut + TransitionTime, token);
  67. Application.Current.Dispatcher.Invoke(() =>
  68. {
  69. notificationCard.IsShow = false;
  70. });
  71. }, token);
  72. }
  73. }
  74. #region 全局
  75. /// <summary>
  76. /// 全局显示
  77. /// </summary>
  78. /// <param name="type">类型</param>
  79. /// <param name="content">内容</param>
  80. /// <param name="title">标题</param>
  81. /// <param name="millisecondTimeOut">关闭延时,为 0 时不自动关闭</param>
  82. /// <param name="isClearable">是否显示关闭按钮</param>
  83. public static void ShowGlobal(NotificationType type, object content, string title = "", int millisecondTimeOut = 3000, bool isClearable = true)
  84. {
  85. NotificationWindow notificationWindow = NotificationWindow.GetInstance();
  86. notificationWindow.Dispatcher.VerifyAccess();
  87. NotificationCard notificationCard = GetNotificationCard(type, content, title, millisecondTimeOut, isClearable);
  88. CancellationTokenSource cts = new CancellationTokenSource();
  89. notificationCard.Close += (sender, e) => notificationWindow.RemoveMessageCard(notificationCard);
  90. notificationCard.MouseEnter += (sender, e) => cts.Cancel();
  91. notificationCard.MouseLeave += (sender, e) =>
  92. {
  93. cts = new CancellationTokenSource();
  94. DelayCloseNotificationCard(millisecondTimeOut, notificationCard, cts.Token);
  95. };
  96. notificationWindow.Show();
  97. notificationWindow.AddMessageCard(notificationCard);
  98. DelayCloseNotificationCard(millisecondTimeOut, notificationCard, cts.Token);
  99. }
  100. /// <summary>
  101. /// 全局显示
  102. /// </summary>
  103. /// <param name="content">内容</param>
  104. /// <param name="title">标题</param>
  105. /// <param name="millisecondTimeOut">关闭延时,为 0 时不自动关闭</param>
  106. /// <param name="isClearable">是否显示关闭按钮</param>
  107. public static void ShowGlobal(object content, string title = "", int millisecondTimeOut = 3000, bool isClearable = true)
  108. {
  109. ShowGlobal(NotificationType.None, content, title, millisecondTimeOut, isClearable);
  110. }
  111. /// <summary>
  112. /// 全局显示信息
  113. /// </summary>
  114. /// <param name="content">内容</param>
  115. /// <param name="title">标题</param>
  116. /// <param name="millisecondTimeOut">关闭延时,为 0 时不自动关闭</param>
  117. /// <param name="isClearable">是否显示关闭按钮</param>
  118. public static void InfoGlobal(object content, string title = "", int millisecondTimeOut = 3000, bool isClearable = true)
  119. {
  120. ShowGlobal(NotificationType.Info, content, title, millisecondTimeOut, isClearable);
  121. }
  122. /// <summary>
  123. /// 全局显示成功
  124. /// </summary>
  125. /// <param name="content">内容</param>
  126. /// <param name="title">标题</param>
  127. /// <param name="millisecondTimeOut">关闭延时,为 0 时不自动关闭</param>
  128. /// <param name="isClearable">是否显示关闭按钮</param>
  129. public static void SuccessGlobal(object content, string title = "", int millisecondTimeOut = 3000, bool isClearable = true)
  130. {
  131. ShowGlobal(NotificationType.Success, content, title, millisecondTimeOut, isClearable);
  132. }
  133. /// <summary>
  134. /// 全局显示警告
  135. /// </summary>
  136. /// <param name="content">内容</param>
  137. /// <param name="title">标题</param>
  138. /// <param name="millisecondTimeOut">关闭延时,为 0 时不自动关闭</param>
  139. /// <param name="isClearable">是否显示关闭按钮</param>
  140. public static void WarningGlobal(object content, string title = "", int millisecondTimeOut = 3000, bool isClearable = true)
  141. {
  142. ShowGlobal(NotificationType.Warning, content, title, millisecondTimeOut, isClearable);
  143. }
  144. /// <summary>
  145. /// 全局显示错误
  146. /// </summary>
  147. /// <param name="content">内容</param>
  148. /// <param name="title">标题</param>
  149. /// <param name="millisecondTimeOut">关闭延时,为 0 时不自动关闭</param>
  150. /// <param name="isClearable">是否显示关闭按钮</param>
  151. public static void ErrorGlobal(object content, string title = "", int millisecondTimeOut = 3000, bool isClearable = true)
  152. {
  153. ShowGlobal(NotificationType.Error, content, title, millisecondTimeOut, isClearable);
  154. }
  155. #endregion 全局
  156. #region 指定容器
  157. private static void ShowInternal(NotificationContainer container, NotificationType type, object content, string title, int millisecondTimeOut, bool isClearable)
  158. {
  159. container.Dispatcher.VerifyAccess();
  160. NotificationCard notificationCard = GetNotificationCard(type, content, title, millisecondTimeOut, isClearable);
  161. CancellationTokenSource cts = new CancellationTokenSource();
  162. notificationCard.Close += (sender, e) => container.RemoveCard(notificationCard);
  163. notificationCard.MouseEnter += (sender, e) => cts.Cancel();
  164. notificationCard.MouseLeave += (sender, e) =>
  165. {
  166. cts = new CancellationTokenSource();
  167. DelayCloseNotificationCard(millisecondTimeOut, notificationCard, cts.Token);
  168. };
  169. container.AddCard(notificationCard);
  170. DelayCloseNotificationCard(millisecondTimeOut, notificationCard, cts.Token);
  171. }
  172. /// <summary>
  173. /// 容器内显示
  174. /// </summary>
  175. /// <param name="containerIdentifier">容器 ID</param>
  176. /// <param name="type">类型</param>
  177. /// <param name="title">标题</param>
  178. /// <param name="content">内容</param>
  179. /// <param name="millisecondTimeOut">关闭延时,为 0 时不自动关闭</param>
  180. /// <param name="isClearable">是否显示关闭按钮</param>
  181. public static void Show(string containerIdentifier, NotificationType type, object content, string title = "", int millisecondTimeOut = 5000, bool isClearable = true)
  182. {
  183. if (!Containers.ContainsKey(containerIdentifier))
  184. {
  185. throw new NullReferenceException($"The notification container Identifier '{containerIdentifier}' could not be found");
  186. }
  187. NotificationContainer container = Containers[containerIdentifier];
  188. ShowInternal(container, type, content, title, millisecondTimeOut, isClearable);
  189. }
  190. /// <summary>
  191. /// 容器内显示
  192. /// (默认 Actived Window 下顶层 MessageContainer 容器)
  193. /// </summary>
  194. /// <param name="type">类型</param>
  195. /// <param name="content">内容</param>
  196. /// <param name="title">标题</param>
  197. /// <param name="millisecondTimeOut">关闭延时,为 0 时不自动关闭</param>
  198. /// <param name="isClearable">是否显示关闭按钮</param>
  199. public static void Show(NotificationType type, object content, string title = "", int millisecondTimeOut = 3000, bool isClearable = true)
  200. {
  201. var activedWindow = WindowHelper.GetCurrentWindow() ?? throw new NullReferenceException("Can't find the actived window");
  202. NotificationContainer container = activedWindow.TryGetChildFromVisualTree<NotificationContainer>(null) ?? throw new NullReferenceException("Can't Find the NotificationContainer");
  203. ShowInternal(container, type, content, title, millisecondTimeOut, isClearable);
  204. }
  205. /// <summary>
  206. /// 容器内显示
  207. /// </summary>
  208. /// <param name="containerIdentifier">容器 ID</param>
  209. /// <param name="content">内容</param>
  210. /// <param name="title">标题</param>
  211. /// <param name="millisecondTimeOut">关闭延时,为 0 时不自动关闭</param>
  212. /// <param name="isClearable">是否显示关闭按钮</param>
  213. public static void Show(string containerIdentifier, object content, string title = "", int millisecondTimeOut = 3000, bool isClearable = true)
  214. {
  215. Show(containerIdentifier, NotificationType.None, content, title, millisecondTimeOut, isClearable);
  216. }
  217. /// <summary>
  218. /// 容器内显示
  219. /// </summary>
  220. /// <param name="content">内容</param>
  221. /// <param name="title">标题</param>
  222. /// <param name="millisecondTimeOut">关闭延时,为 0 时不自动关闭</param>
  223. /// <param name="isClearable">是否显示关闭按钮</param>
  224. public static void Show(object content, string title = "", int millisecondTimeOut = 3000, bool isClearable = true)
  225. {
  226. Show(NotificationType.None, content, title, millisecondTimeOut, isClearable);
  227. }
  228. /// <summary>
  229. /// 容器内显示信息
  230. /// </summary>
  231. /// <param name="containerIdentifier">容器 ID</param>
  232. /// <param name="content">内容</param>
  233. /// <param name="title">标题</param>
  234. /// <param name="millisecondTimeOut">关闭延时,为 0 时不自动关闭</param>
  235. /// <param name="isClearable">是否显示关闭按钮</param>
  236. public static void Info(string containerIdentifier, object content, string title = "", int millisecondTimeOut = 3000, bool isClearable = true)
  237. {
  238. Show(containerIdentifier, NotificationType.Info, content, title, millisecondTimeOut, isClearable);
  239. }
  240. /// <summary>
  241. /// 容器内显示信息
  242. /// (默认 Actived Window 下顶层 MessageContainer 容器)
  243. /// </summary>
  244. /// <param name="content">内容</param>
  245. /// <param name="title">标题</param>
  246. /// <param name="millisecondTimeOut">关闭延时,为 0 时不自动关闭</param>
  247. /// <param name="isClearable">是否显示关闭按钮</param>
  248. public static void Info(object content, string title = "", int millisecondTimeOut = 3000, bool isClearable = true)
  249. {
  250. Show(NotificationType.Info, content, title, millisecondTimeOut, isClearable);
  251. }
  252. /// <summary>
  253. /// 容器内显示成功
  254. /// </summary>
  255. /// <param name="containerIdentifier">容器 ID</param>
  256. /// <param name="content">内容</param>
  257. /// <param name="title">标题</param>
  258. /// <param name="millisecondTimeOut">关闭延时,为 0 时不自动关闭</param>
  259. /// <param name="isClearable">是否显示关闭按钮</param>
  260. public static void Success(string containerIdentifier, object content, string title = "", int millisecondTimeOut = 3000, bool isClearable = true)
  261. {
  262. Show(containerIdentifier, NotificationType.Success, content, title, millisecondTimeOut, isClearable);
  263. }
  264. /// <summary>
  265. /// 容器内显示成功
  266. /// (默认 Actived Window 下顶层 MessageContainer 容器)
  267. /// </summary>
  268. /// <param name="content">内容</param>
  269. /// <param name="title">标题</param>
  270. /// <param name="millisecondTimeOut">关闭延时,为 0 时不自动关闭</param>
  271. /// <param name="isClearable">是否显示关闭按钮</param>
  272. public static void Success(object content, string title = "", int millisecondTimeOut = 3000, bool isClearable = true)
  273. {
  274. Show(NotificationType.Success, content, title, millisecondTimeOut, isClearable);
  275. }
  276. /// <summary>
  277. /// 容器内显示警告
  278. /// </summary>
  279. /// <param name="containerIdentifier">容器 ID</param>
  280. /// <param name="content">内容</param>
  281. /// <param name="title">标题</param>
  282. /// <param name="millisecondTimeOut">关闭延时,为 0 时不自动关闭</param>
  283. /// <param name="isClearable">是否显示关闭按钮</param>
  284. public static void Warning(string containerIdentifier, object content, string title = "", int millisecondTimeOut = 3000, bool isClearable = true)
  285. {
  286. Show(containerIdentifier, NotificationType.Warning, content, title, millisecondTimeOut, isClearable);
  287. }
  288. /// <summary>
  289. /// 容器内显示警告
  290. /// (默认 Actived Window 下顶层 MessageContainer 容器)
  291. /// </summary>
  292. /// <param name="content">内容</param>
  293. /// <param name="title">标题</param>
  294. /// <param name="millisecondTimeOut">关闭延时,为 0 时不自动关闭</param>
  295. /// <param name="isClearable">是否显示关闭按钮</param>
  296. public static void Warning(object content, string title = "", int millisecondTimeOut = 3000, bool isClearable = true)
  297. {
  298. Show(NotificationType.Warning, content, title, millisecondTimeOut, isClearable);
  299. }
  300. /// <summary>
  301. /// 容器内显示错误
  302. /// </summary>
  303. /// <param name="containerIdentifier">容器 ID</param>
  304. /// <param name="content">内容</param>
  305. /// <param name="title">标题</param>
  306. /// <param name="millisecondTimeOut">关闭延时,为 0 时不自动关闭</param>
  307. /// <param name="isClearable">是否显示关闭按钮</param>
  308. public static void Error(string containerIdentifier, object content, string title = "", int millisecondTimeOut = 3000, bool isClearable = true)
  309. {
  310. Show(containerIdentifier, NotificationType.Error, content, title, millisecondTimeOut, isClearable);
  311. }
  312. /// <summary>
  313. /// 容器内显示错误
  314. /// (默认 Actived Window 下顶层 MessageContainer 容器)
  315. /// </summary>
  316. /// <param name="content">内容</param>
  317. /// <param name="title">标题</param>
  318. /// <param name="millisecondTimeOut">关闭延时,为 0 时不自动关闭</param>
  319. /// <param name="isClearable">是否显示关闭按钮</param>
  320. public static void Error(object content, string title = "", int millisecondTimeOut = 3000, bool isClearable = true)
  321. {
  322. Show(NotificationType.Error, content, title, millisecondTimeOut, isClearable);
  323. }
  324. #endregion 指定容器
  325. }
  326. /// <summary>
  327. /// 通知类型
  328. /// </summary>
  329. public enum NotificationType
  330. {
  331. /// <summary>
  332. /// 无
  333. /// </summary>
  334. [Description("无")]
  335. None = 0,
  336. /// <summary>
  337. /// 信息
  338. /// </summary>
  339. [Description("信息")]
  340. Info,
  341. /// <summary>
  342. /// 成功
  343. /// </summary>
  344. [Description("成功")]
  345. Success,
  346. /// <summary>
  347. /// 警告
  348. /// </summary>
  349. [Description("警告")]
  350. Warning,
  351. /// <summary>
  352. /// 错误
  353. /// </summary>
  354. [Description("错误")]
  355. Error
  356. }
  357. }