ProgressForm.xaml.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using InABox.Wpf;
  2. using System;
  3. using System.ComponentModel;
  4. using System.Runtime.CompilerServices;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Media;
  8. using System.Windows.Media.Animation;
  9. namespace InABox.WPF
  10. {
  11. /// <summary>
  12. /// Interaction logic for ProgressForm.xaml
  13. /// </summary>
  14. public partial class ProgressForm : ThemableWindow, INotifyPropertyChanged
  15. {
  16. private ImageSource? _image = null;
  17. private DoubleAnimation _fader = new DoubleAnimation(1d, 0.3d, new Duration(TimeSpan.FromMilliseconds(3000))) { AutoReverse = true };
  18. public string CancelText { get; init; }
  19. public bool HasCancelButton { get; init; }
  20. public delegate void CancelledEvent();
  21. public event CancelledEvent? OnCancelled;
  22. public Visibility CancelButtonVisibility => HasCancelButton ? Visibility.Visible : Visibility.Collapsed;
  23. private string _message;
  24. public string Message
  25. {
  26. get => _message;
  27. set
  28. {
  29. _message = value;
  30. Progress.Content = _message;
  31. }
  32. }
  33. private ProgressForm(string cancelText, bool hasCancelButton)
  34. {
  35. CancelText = cancelText;
  36. HasCancelButton = hasCancelButton;
  37. InitializeComponent();
  38. Topmost = true;
  39. Loaded += (sender, args) =>
  40. {
  41. _fader.Completed += (o, e) => Splash.BeginAnimation(Image.OpacityProperty, _fader);
  42. Splash.BeginAnimation(Image.OpacityProperty, _fader);
  43. };
  44. }
  45. public ProgressForm(): this("", false) { }
  46. public ProgressForm(string cancelText): this(cancelText, true) { }
  47. public ImageSource? DisplayImage
  48. {
  49. get => Splash.Source;
  50. set => Splash.Source = value;
  51. }
  52. public void UpdateWindow(string message)
  53. {
  54. if (Progress.Dispatcher.CheckAccess())
  55. Message = message;
  56. else
  57. Progress.Dispatcher.Invoke(() => { Message = message; });
  58. }
  59. public void CloseWindow()
  60. {
  61. if (Progress.Dispatcher.CheckAccess())
  62. Close();
  63. else
  64. Progress.Dispatcher.Invoke(() => { Close(); });
  65. }
  66. public string GetMessage()
  67. {
  68. return Message;
  69. }
  70. private void Window_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
  71. {
  72. if(e.ChangedButton == System.Windows.Input.MouseButton.Left)
  73. {
  74. DragMove();
  75. }
  76. }
  77. private void CancelButton_Click(object sender, RoutedEventArgs e)
  78. {
  79. OnCancelled?.Invoke();
  80. }
  81. public event PropertyChangedEventHandler? PropertyChanged;
  82. protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
  83. {
  84. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  85. }
  86. }
  87. }