Progress.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Windows.Media.Imaging;
  6. namespace InABox.WPF
  7. {
  8. public class ProgressSection
  9. {
  10. public ProgressSection(string message, Action action)
  11. {
  12. Message = message;
  13. Action = action;
  14. }
  15. public string Message { get; set; }
  16. public Action Action { get; set; }
  17. }
  18. public static class Progress
  19. {
  20. private static ProgressForm form;
  21. public static BitmapImage DisplayImage { get; set; }
  22. public static bool IsVisible => form != null;
  23. public static string Message => form?.GetMessage();
  24. public static void ShowModal(params ProgressSection[] sections)
  25. {
  26. if (!sections.Any())
  27. return;
  28. var progress = new ProgressForm();
  29. progress.DisplayImage = DisplayImage;
  30. progress.Activated += (_, args) =>
  31. {
  32. progress.Dispatcher.Invoke(() =>
  33. {
  34. foreach (var section in sections)
  35. {
  36. progress.Progress.Content = section.Message;
  37. section.Action();
  38. }
  39. progress.Close();
  40. });
  41. };
  42. progress.ShowActivated = true;
  43. progress.ShowDialog();
  44. }
  45. private static void RunShowModal(ProgressForm progress, Action<IProgress<string>> work)
  46. {
  47. }
  48. /// <summary>
  49. /// Shows progress dialog modally, with a cancel button.
  50. /// </summary>
  51. /// <param name="message"></param>
  52. /// <param name="cancelButtonText"></param>
  53. /// <param name="work"></param>
  54. /// <returns>Whether the progress was completed without cancelling.</returns>
  55. public static bool ShowModal(string message, string cancelButtonText, Action<IProgress<string>, CancellationToken> work)
  56. {
  57. var cancellationTokenSource = new CancellationTokenSource();
  58. var progress = new ProgressForm(cancelButtonText)
  59. {
  60. DisplayImage = DisplayImage
  61. };
  62. progress.Progress.Content = message;
  63. progress.Loaded += (_, args) =>
  64. {
  65. var worker = new BackgroundWorker();
  66. var update = new Progress<string>(data => progress.Progress.Content = data);
  67. progress.OnCancelled += () =>
  68. {
  69. cancellationTokenSource.Cancel();
  70. progress.Close();
  71. };
  72. worker.DoWork += (o, e) => work(update, cancellationTokenSource.Token);
  73. worker.RunWorkerCompleted +=
  74. (o, e) => progress.Close();
  75. worker.RunWorkerAsync();
  76. };
  77. progress.ShowDialog();
  78. return !cancellationTokenSource.IsCancellationRequested;
  79. }
  80. public static void ShowModal(string message, Action<IProgress<string>> work)
  81. {
  82. var progress = new ProgressForm
  83. {
  84. DisplayImage = DisplayImage
  85. };
  86. progress.Progress.Content = message;
  87. progress.Loaded += (_, args) =>
  88. {
  89. var worker = new BackgroundWorker();
  90. var update = new Progress<string>(data => progress.Progress.Content = data);
  91. progress.OnCancelled += () =>
  92. {
  93. worker.CancelAsync();
  94. };
  95. worker.DoWork += (o, e) => work(update);
  96. worker.RunWorkerCompleted +=
  97. (o, e) => progress.Close();
  98. worker.RunWorkerAsync();
  99. };
  100. progress.ShowDialog();
  101. }
  102. public static void Show(string message)
  103. {
  104. if (form == null)
  105. {
  106. form = new ProgressForm();
  107. form.DisplayImage = DisplayImage;
  108. form.Show();
  109. }
  110. form.UpdateWindow(message);
  111. }
  112. public static void SetMessage(string message)
  113. {
  114. form.UpdateWindow(message);
  115. }
  116. public static void Close()
  117. {
  118. form?.CloseWindow();
  119. form = null;
  120. }
  121. }
  122. }