-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgressWindow.xaml.cs
More file actions
103 lines (91 loc) · 3.38 KB
/
ProgressWindow.xaml.cs
File metadata and controls
103 lines (91 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
namespace Trimly
{
public partial class ProgressWindow : Window
{
private MainWindow mainWindow;
public ProgressWindow()
{
InitializeComponent();
}
public void ChangeTitle(string title) => Title = title;
public void ChangeVideoTitle(string videoTitle)
{
VideoTitle.Text = videoTitle;
mainWindow.ChangeRenderText("[...%] " + videoTitle);
}
public async void UpdateProgress(double progress)
{
await Task.Run(() => Dispatcher.Invoke(() =>
{
double currentProgress = ProgressBar.Value;
if (progress > currentProgress)
{
AnimateProgressBar(currentProgress, progress, TimeSpan.FromSeconds(0.5));
}
else
{
AnimateProgressBar(currentProgress, progress, TimeSpan.FromSeconds(0));
}
AnimateText(currentProgress, progress, TimeSpan.FromSeconds(0.5));
}));
}
private void AnimateProgressBar(double fromValue, double toValue, TimeSpan duration)
{
DoubleAnimation progressBarAnimation = new DoubleAnimation
{
From = fromValue,
To = toValue,
Duration = duration,
EasingFunction = new QuadraticEase()
};
ProgressBar.BeginAnimation(ProgressBar.ValueProperty, progressBarAnimation);
}
private void AnimateText(double fromValue, double toValue, TimeSpan duration)
{
DoubleAnimation textAnimation = new DoubleAnimation
{
From = fromValue,
To = toValue,
Duration = duration,
EasingFunction = new QuadraticEase()
};
textAnimation.CurrentTimeInvalidated += (s, e) =>
{
if (s is Clock clock)
{
double currentProgress = ProgressBar.Value;
Percent.Text = $"{currentProgress:0.00}%";
}
};
Percent.BeginAnimation(OpacityProperty, textAnimation);
}
public void SetWindowParent(MainWindow window) => mainWindow = window;
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
mainWindow.EnableRenderButton();
mainWindow.ChangeRenderText("[100%] " + VideoTitle.Text);
Process[] ffmpegProcesses = Process.GetProcessesByName("ffmpeg");
foreach (Process process in ffmpegProcesses)
{
try
{
process.Kill();
process.WaitForExit();
ChangeTitle("Rendering [Cancel]");
mainWindow.StopRender();
mainWindow.ChangeRenderText("[Canceled] " + VideoTitle.Text);
}
catch (Exception ex)
{
MessageBox.Show($"Error when terminating ffmpeg process: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}
}