DialogHelper.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using System;
  2. using System.Collections;
  3. using System.Windows;
  4. using HandyControl.Controls;
  5. using HandyControl.Data;
  6. using TeamAAS.Dialogs.Dialogs;
  7. namespace TeamAAS
  8. {
  9. /// <summary>
  10. /// Unified notification helper for toasts, dialogs, and small input flows.
  11. /// </summary>
  12. public static class DialogHelper
  13. {
  14. public static void Success(string message)
  15. {
  16. AppLogger.Info(message, nameof(DialogHelper));
  17. ShowGrowl(() => Growl.Success(message));
  18. }
  19. public static void Success(string message, int waitTime)
  20. {
  21. AppLogger.Info(message, nameof(DialogHelper));
  22. ShowGrowl(() => Growl.Success(new GrowlInfo { Message = message, WaitTime = waitTime }));
  23. }
  24. public static void Info(string message)
  25. {
  26. AppLogger.Info(message, nameof(DialogHelper));
  27. ShowGrowl(() => Growl.Info(message));
  28. }
  29. public static void Info(string message, int waitTime)
  30. {
  31. AppLogger.Info(message, nameof(DialogHelper));
  32. ShowGrowl(() => Growl.Info(new GrowlInfo { Message = message, WaitTime = waitTime }));
  33. }
  34. public static void Warning(string message)
  35. {
  36. AppLogger.Warning(message, nameof(DialogHelper));
  37. ShowGrowl(() => Growl.Warning(message));
  38. }
  39. public static void Warning(string message, int waitTime)
  40. {
  41. AppLogger.Warning(message, nameof(DialogHelper));
  42. ShowGrowl(() => Growl.Warning(new GrowlInfo { Message = message, WaitTime = waitTime }));
  43. }
  44. public static void Error(string message)
  45. {
  46. AppLogger.Error(message, nameof(DialogHelper));
  47. ShowGrowl(() => Growl.Error(message));
  48. }
  49. public static void Error(string message, int waitTime)
  50. {
  51. AppLogger.Error(message, nameof(DialogHelper));
  52. ShowGrowl(() => Growl.Error(new GrowlInfo { Message = message, WaitTime = waitTime }));
  53. }
  54. public static void Error(string prefix, Exception ex)
  55. {
  56. var msg = ex != null ? $"{prefix}: {ex.Message}" : prefix;
  57. AppLogger.Error(prefix, ex, nameof(DialogHelper));
  58. ShowGrowl(() => Growl.Error(msg));
  59. }
  60. public static void Fatal(string message)
  61. {
  62. AppLogger.Error(message, nameof(DialogHelper));
  63. ShowGrowl(() => Growl.Fatal(message));
  64. }
  65. public static void Show(string message, string title = "提示")
  66. {
  67. AppLogger.Info($"{title}: {message}", nameof(DialogHelper));
  68. ShowDialog(dialog => dialog.SetupInfo(message, title));
  69. }
  70. public static void ShowWarning(string message, string title = "警告")
  71. {
  72. AppLogger.Warning($"{title}: {message}", nameof(DialogHelper));
  73. ShowDialog(dialog => dialog.SetupWarning(message, title));
  74. }
  75. public static void ShowError(string message, string title = "错误")
  76. {
  77. AppLogger.Error($"{title}: {message}", nameof(DialogHelper));
  78. ShowDialog(dialog => dialog.SetupError(message, title));
  79. }
  80. public static void ShowError(string prefix, Exception ex, string title = "错误")
  81. {
  82. var msg = ex != null ? $"{prefix}: {ex.Message}" : prefix;
  83. AppLogger.Error(prefix, ex, nameof(DialogHelper));
  84. ShowDialog(dialog => dialog.SetupError(msg, title, ex?.ToString()));
  85. }
  86. public static bool Confirm(string message, string title = "确认")
  87. {
  88. AppLogger.Info($"{title}: {message}", nameof(DialogHelper));
  89. return ShowDialog(dialog => dialog.SetupConfirm(message, title));
  90. }
  91. public static bool ConfirmYesNo(string message, string title = "确认")
  92. {
  93. AppLogger.Info($"{title}: {message}", nameof(DialogHelper));
  94. return ShowDialog(dialog => dialog.SetupAskYesNo(message, title));
  95. }
  96. /// <summary>是/否/取消三键确认。返回 true=是, false=否, null=取消。</summary>
  97. public static bool? ConfirmYesNoCancel(string message, string title = "确认")
  98. {
  99. AppLogger.Info($"{title}: {message}", nameof(DialogHelper));
  100. bool? result = null;
  101. void ShowCore()
  102. {
  103. var dialog = new MessageDialog();
  104. SetOwner(dialog);
  105. dialog.SetupAskYesNoCancel(message, title);
  106. dialog.ShowDialog();
  107. result = dialog.YesNoCancelResult;
  108. }
  109. InvokeOnUi(ShowCore);
  110. return result;
  111. }
  112. public static string Input(string label, string title = "输入", string defaultText = "", string placeholder = "请输入...")
  113. {
  114. string result = null;
  115. void ShowCore()
  116. {
  117. var dialog = new InputDialog();
  118. SetOwner(dialog);
  119. dialog.Setup(label, title, defaultText, placeholder);
  120. dialog.ShowDialog();
  121. if (dialog.Result)
  122. result = dialog.InputText;
  123. }
  124. InvokeOnUi(ShowCore);
  125. return result;
  126. }
  127. public static object Select(string label, IEnumerable items, string title = "选择", object defaultItem = null)
  128. {
  129. object result = null;
  130. void ShowCore()
  131. {
  132. var dialog = new SelectDialog();
  133. SetOwner(dialog);
  134. dialog.Setup(label, items, title, defaultItem);
  135. dialog.ShowDialog();
  136. if (dialog.Result)
  137. result = dialog.SelectedItem;
  138. }
  139. InvokeOnUi(ShowCore);
  140. return result;
  141. }
  142. public static bool EditProperties(object obj, string title = null)
  143. {
  144. bool result = false;
  145. void ShowCore()
  146. {
  147. var dialog = new PropertyGridDialog();
  148. SetOwner(dialog);
  149. dialog.Setup(obj, title);
  150. dialog.ShowDialog();
  151. result = dialog.Result;
  152. }
  153. InvokeOnUi(ShowCore);
  154. return result;
  155. }
  156. public static bool EditProperties(object obj, string title, Action<object, System.Threading.CancellationToken> executeAction)
  157. {
  158. bool result = false;
  159. void ShowCore()
  160. {
  161. var dialog = new PropertyGridDialog();
  162. SetOwner(dialog);
  163. dialog.Setup(obj, title, executeAction);
  164. dialog.ShowDialog();
  165. result = dialog.Result;
  166. }
  167. InvokeOnUi(ShowCore);
  168. return result;
  169. }
  170. public static bool ShowPluginView(FrameworkElement view, string title = null, Action<object, System.Threading.CancellationToken> executeAction = null)
  171. {
  172. bool result = false;
  173. void ShowCore()
  174. {
  175. var dialog = new PluginViewDialog();
  176. SetOwner(dialog);
  177. if (executeAction != null)
  178. dialog.Setup(view, title, executeAction);
  179. else
  180. dialog.Setup(view, title);
  181. dialog.ShowDialog();
  182. result = dialog.Result;
  183. }
  184. InvokeOnUi(ShowCore);
  185. return result;
  186. }
  187. private static void ShowGrowl(Action action)
  188. {
  189. if (IsUiThread())
  190. {
  191. action();
  192. }
  193. else
  194. {
  195. Application.Current?.Dispatcher?.Invoke(action);
  196. }
  197. }
  198. private static bool ShowDialog(Action<MessageDialog> setup)
  199. {
  200. bool result = false;
  201. void ShowCore()
  202. {
  203. var dialog = new MessageDialog();
  204. SetOwner(dialog);
  205. setup(dialog);
  206. dialog.ShowDialog();
  207. result = dialog.Result;
  208. }
  209. if (IsUiThread())
  210. {
  211. ShowCore();
  212. }
  213. else
  214. {
  215. Application.Current?.Dispatcher?.Invoke(ShowCore);
  216. }
  217. return result;
  218. }
  219. private static void InvokeOnUi(Action action)
  220. {
  221. if (IsUiThread())
  222. {
  223. action();
  224. }
  225. else
  226. {
  227. Application.Current?.Dispatcher?.Invoke(action);
  228. }
  229. }
  230. private static void SetOwner(System.Windows.Window dialog)
  231. {
  232. if (Application.Current?.MainWindow != null && Application.Current.MainWindow != dialog)
  233. {
  234. dialog.Owner = Application.Current.MainWindow;
  235. }
  236. }
  237. private static bool IsUiThread()
  238. {
  239. return Application.Current?.Dispatcher?.CheckAccess() != false;
  240. }
  241. }
  242. }