ProgressForm.xaml.cs 2.5 KB

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