Progress.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. Exception? exception = null;
  83. var progress = new ProgressForm
  84. {
  85. DisplayImage = DisplayImage
  86. };
  87. progress.Progress.Content = message;
  88. progress.Loaded += (_, args) =>
  89. {
  90. var worker = new BackgroundWorker();
  91. var update = new Progress<string>(data => progress.Progress.Content = data);
  92. progress.OnCancelled += () =>
  93. {
  94. worker.CancelAsync();
  95. };
  96. worker.DoWork += (o, e) =>
  97. {
  98. try
  99. {
  100. work(update);
  101. }
  102. catch (Exception ex)
  103. {
  104. exception = ex;
  105. }
  106. };
  107. worker.RunWorkerCompleted +=
  108. (o, e) => progress.Close();
  109. worker.RunWorkerAsync();
  110. };
  111. progress.ShowDialog();
  112. if(exception is not null)
  113. {
  114. throw exception;
  115. }
  116. }
  117. public static void Show(string message)
  118. {
  119. if (form == null)
  120. {
  121. form = new ProgressForm();
  122. form.DisplayImage = DisplayImage;
  123. form.Show();
  124. }
  125. form.UpdateWindow(message);
  126. }
  127. public static void SetMessage(string message)
  128. {
  129. form.UpdateWindow(message);
  130. }
  131. public static void Close()
  132. {
  133. form?.CloseWindow();
  134. form = null;
  135. }
  136. }
  137. }