| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- using System;
- using System.Collections;
- using System.Windows;
- using HandyControl.Controls;
- using HandyControl.Data;
- using TeamAAS.Dialogs.Dialogs;
- namespace TeamAAS
- {
- /// <summary>
- /// Unified notification helper for toasts, dialogs, and small input flows.
- /// </summary>
- public static class DialogHelper
- {
- public static void Success(string message)
- {
- AppLogger.Info(message, nameof(DialogHelper));
- ShowGrowl(() => Growl.Success(message));
- }
- public static void Success(string message, int waitTime)
- {
- AppLogger.Info(message, nameof(DialogHelper));
- ShowGrowl(() => Growl.Success(new GrowlInfo { Message = message, WaitTime = waitTime }));
- }
- public static void Info(string message)
- {
- AppLogger.Info(message, nameof(DialogHelper));
- ShowGrowl(() => Growl.Info(message));
- }
- public static void Info(string message, int waitTime)
- {
- AppLogger.Info(message, nameof(DialogHelper));
- ShowGrowl(() => Growl.Info(new GrowlInfo { Message = message, WaitTime = waitTime }));
- }
- public static void Warning(string message)
- {
- AppLogger.Warning(message, nameof(DialogHelper));
- ShowGrowl(() => Growl.Warning(message));
- }
- public static void Warning(string message, int waitTime)
- {
- AppLogger.Warning(message, nameof(DialogHelper));
- ShowGrowl(() => Growl.Warning(new GrowlInfo { Message = message, WaitTime = waitTime }));
- }
- public static void Error(string message)
- {
- AppLogger.Error(message, nameof(DialogHelper));
- ShowGrowl(() => Growl.Error(message));
- }
- public static void Error(string message, int waitTime)
- {
- AppLogger.Error(message, nameof(DialogHelper));
- ShowGrowl(() => Growl.Error(new GrowlInfo { Message = message, WaitTime = waitTime }));
- }
- public static void Error(string prefix, Exception ex)
- {
- var msg = ex != null ? $"{prefix}: {ex.Message}" : prefix;
- AppLogger.Error(prefix, ex, nameof(DialogHelper));
- ShowGrowl(() => Growl.Error(msg));
- }
- public static void Fatal(string message)
- {
- AppLogger.Error(message, nameof(DialogHelper));
- ShowGrowl(() => Growl.Fatal(message));
- }
- public static void Show(string message, string title = "提示")
- {
- AppLogger.Info($"{title}: {message}", nameof(DialogHelper));
- ShowDialog(dialog => dialog.SetupInfo(message, title));
- }
- public static void ShowWarning(string message, string title = "警告")
- {
- AppLogger.Warning($"{title}: {message}", nameof(DialogHelper));
- ShowDialog(dialog => dialog.SetupWarning(message, title));
- }
- public static void ShowError(string message, string title = "错误")
- {
- AppLogger.Error($"{title}: {message}", nameof(DialogHelper));
- ShowDialog(dialog => dialog.SetupError(message, title));
- }
- public static void ShowError(string prefix, Exception ex, string title = "错误")
- {
- var msg = ex != null ? $"{prefix}: {ex.Message}" : prefix;
- AppLogger.Error(prefix, ex, nameof(DialogHelper));
- ShowDialog(dialog => dialog.SetupError(msg, title, ex?.ToString()));
- }
- public static bool Confirm(string message, string title = "确认")
- {
- AppLogger.Info($"{title}: {message}", nameof(DialogHelper));
- return ShowDialog(dialog => dialog.SetupConfirm(message, title));
- }
- public static bool ConfirmYesNo(string message, string title = "确认")
- {
- AppLogger.Info($"{title}: {message}", nameof(DialogHelper));
- return ShowDialog(dialog => dialog.SetupAskYesNo(message, title));
- }
- /// <summary>是/否/取消三键确认。返回 true=是, false=否, null=取消。</summary>
- public static bool? ConfirmYesNoCancel(string message, string title = "确认")
- {
- AppLogger.Info($"{title}: {message}", nameof(DialogHelper));
- bool? result = null;
- void ShowCore()
- {
- var dialog = new MessageDialog();
- SetOwner(dialog);
- dialog.SetupAskYesNoCancel(message, title);
- dialog.ShowDialog();
- result = dialog.YesNoCancelResult;
- }
- InvokeOnUi(ShowCore);
- return result;
- }
- public static string Input(string label, string title = "输入", string defaultText = "", string placeholder = "请输入...")
- {
- string result = null;
- void ShowCore()
- {
- var dialog = new InputDialog();
- SetOwner(dialog);
- dialog.Setup(label, title, defaultText, placeholder);
- dialog.ShowDialog();
- if (dialog.Result)
- result = dialog.InputText;
- }
- InvokeOnUi(ShowCore);
- return result;
- }
- public static object Select(string label, IEnumerable items, string title = "选择", object defaultItem = null)
- {
- object result = null;
- void ShowCore()
- {
- var dialog = new SelectDialog();
- SetOwner(dialog);
- dialog.Setup(label, items, title, defaultItem);
- dialog.ShowDialog();
- if (dialog.Result)
- result = dialog.SelectedItem;
- }
- InvokeOnUi(ShowCore);
- return result;
- }
- public static bool EditProperties(object obj, string title = null)
- {
- bool result = false;
- void ShowCore()
- {
- var dialog = new PropertyGridDialog();
- SetOwner(dialog);
- dialog.Setup(obj, title);
- dialog.ShowDialog();
- result = dialog.Result;
- }
- InvokeOnUi(ShowCore);
- return result;
- }
- public static bool EditProperties(object obj, string title, Action<object, System.Threading.CancellationToken> executeAction)
- {
- bool result = false;
- void ShowCore()
- {
- var dialog = new PropertyGridDialog();
- SetOwner(dialog);
- dialog.Setup(obj, title, executeAction);
- dialog.ShowDialog();
- result = dialog.Result;
- }
- InvokeOnUi(ShowCore);
- return result;
- }
- public static bool ShowPluginView(FrameworkElement view, string title = null, Action<object, System.Threading.CancellationToken> executeAction = null)
- {
- bool result = false;
- void ShowCore()
- {
- var dialog = new PluginViewDialog();
- SetOwner(dialog);
- if (executeAction != null)
- dialog.Setup(view, title, executeAction);
- else
- dialog.Setup(view, title);
- dialog.ShowDialog();
- result = dialog.Result;
- }
- InvokeOnUi(ShowCore);
- return result;
- }
- private static void ShowGrowl(Action action)
- {
- if (IsUiThread())
- {
- action();
- }
- else
- {
- Application.Current?.Dispatcher?.Invoke(action);
- }
- }
- private static bool ShowDialog(Action<MessageDialog> setup)
- {
- bool result = false;
- void ShowCore()
- {
- var dialog = new MessageDialog();
- SetOwner(dialog);
- setup(dialog);
- dialog.ShowDialog();
- result = dialog.Result;
- }
- if (IsUiThread())
- {
- ShowCore();
- }
- else
- {
- Application.Current?.Dispatcher?.Invoke(ShowCore);
- }
- return result;
- }
- private static void InvokeOnUi(Action action)
- {
- if (IsUiThread())
- {
- action();
- }
- else
- {
- Application.Current?.Dispatcher?.Invoke(action);
- }
- }
- private static void SetOwner(System.Windows.Window dialog)
- {
- if (Application.Current?.MainWindow != null && Application.Current.MainWindow != dialog)
- {
- dialog.Owner = Application.Current.MainWindow;
- }
- }
- private static bool IsUiThread()
- {
- return Application.Current?.Dispatcher?.CheckAccess() != false;
- }
- }
- }
|