2025-01-04 15:11:13 +00:00
|
|
|
|
using System;
|
2025-01-09 08:37:52 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2025-01-04 15:11:13 +00:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
|
|
2025-01-09 13:48:13 +00:00
|
|
|
|
namespace Flow.Launcher
|
2025-01-04 15:11:13 +00:00
|
|
|
|
{
|
2025-01-10 11:13:19 +00:00
|
|
|
|
public partial class ProgressBoxEx : Window
|
2025-01-04 15:11:13 +00:00
|
|
|
|
{
|
2025-04-13 09:50:44 +00:00
|
|
|
|
private static readonly string ClassName = nameof(ProgressBoxEx);
|
|
|
|
|
|
|
2025-02-26 12:11:27 +00:00
|
|
|
|
private readonly Action _cancelProgress;
|
2025-01-04 16:08:56 +00:00
|
|
|
|
|
2025-02-26 12:11:27 +00:00
|
|
|
|
private ProgressBoxEx(Action cancelProgress)
|
2025-01-04 15:11:13 +00:00
|
|
|
|
{
|
2025-02-26 12:11:27 +00:00
|
|
|
|
_cancelProgress = cancelProgress;
|
2025-01-04 15:11:13 +00:00
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-26 12:11:27 +00:00
|
|
|
|
public static async Task ShowAsync(string caption, Func<Action<double>, Task> reportProgressAsync, Action cancelProgress = null)
|
2025-01-04 15:11:13 +00:00
|
|
|
|
{
|
2025-07-07 11:35:12 +00:00
|
|
|
|
ProgressBoxEx progressBox = null;
|
2025-01-04 15:11:13 +00:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-01-10 11:13:19 +00:00
|
|
|
|
if (!Application.Current.Dispatcher.CheckAccess())
|
2025-01-04 15:11:13 +00:00
|
|
|
|
{
|
2025-01-10 11:13:19 +00:00
|
|
|
|
await Application.Current.Dispatcher.InvokeAsync(() =>
|
|
|
|
|
|
{
|
2025-07-07 11:35:12 +00:00
|
|
|
|
progressBox = new ProgressBoxEx(cancelProgress)
|
2025-01-10 11:13:19 +00:00
|
|
|
|
{
|
|
|
|
|
|
Title = caption
|
|
|
|
|
|
};
|
2025-07-07 11:35:12 +00:00
|
|
|
|
progressBox.TitleTextBlock.Text = caption;
|
|
|
|
|
|
progressBox.Show();
|
2025-01-10 11:13:19 +00:00
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-01-11 05:19:45 +00:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-07-07 11:35:12 +00:00
|
|
|
|
progressBox = new ProgressBoxEx(cancelProgress)
|
2025-01-11 05:19:45 +00:00
|
|
|
|
{
|
|
|
|
|
|
Title = caption
|
|
|
|
|
|
};
|
2025-07-07 11:35:12 +00:00
|
|
|
|
progressBox.TitleTextBlock.Text = caption;
|
|
|
|
|
|
progressBox.Show();
|
2025-01-11 05:19:45 +00:00
|
|
|
|
}
|
2025-01-10 11:13:19 +00:00
|
|
|
|
|
2025-07-07 11:35:12 +00:00
|
|
|
|
await reportProgressAsync(progressBox.ReportProgress).ConfigureAwait(false);
|
2025-01-04 15:11:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2025-04-13 09:50:44 +00:00
|
|
|
|
App.API.LogError(ClassName, $"An error occurred: {e.Message}");
|
2025-01-10 11:13:19 +00:00
|
|
|
|
|
|
|
|
|
|
await reportProgressAsync(null).ConfigureAwait(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!Application.Current.Dispatcher.CheckAccess())
|
|
|
|
|
|
{
|
2025-01-11 04:38:08 +00:00
|
|
|
|
await Application.Current.Dispatcher.InvokeAsync(() =>
|
2025-01-10 11:13:19 +00:00
|
|
|
|
{
|
2025-07-07 11:35:12 +00:00
|
|
|
|
progressBox?.Close();
|
2025-01-10 11:13:19 +00:00
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-01-11 05:19:45 +00:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-07-07 11:35:12 +00:00
|
|
|
|
progressBox?.Close();
|
2025-01-11 05:19:45 +00:00
|
|
|
|
}
|
2025-01-04 15:11:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-10 11:13:19 +00:00
|
|
|
|
private void ReportProgress(double progress)
|
2025-01-04 15:11:13 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (!Application.Current.Dispatcher.CheckAccess())
|
|
|
|
|
|
{
|
|
|
|
|
|
Application.Current.Dispatcher.Invoke(() => ReportProgress(progress));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (progress < 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
ProgressBar.Value = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (progress >= 100)
|
|
|
|
|
|
{
|
|
|
|
|
|
ProgressBar.Value = 100;
|
2025-01-04 16:08:56 +00:00
|
|
|
|
Close();
|
2025-01-04 15:11:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
ProgressBar.Value = progress;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void KeyEsc_OnPress(object sender, ExecutedRoutedEventArgs e)
|
|
|
|
|
|
{
|
2025-01-04 16:08:56 +00:00
|
|
|
|
ForceClose();
|
2025-01-04 15:11:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-22 08:12:41 +00:00
|
|
|
|
private void Button_Cancel(object sender, RoutedEventArgs e)
|
2025-01-04 15:11:13 +00:00
|
|
|
|
{
|
2025-01-04 16:08:56 +00:00
|
|
|
|
ForceClose();
|
2025-01-04 15:11:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-22 08:12:41 +00:00
|
|
|
|
private void Button_Minimize(object sender, RoutedEventArgs e)
|
2025-01-04 15:11:13 +00:00
|
|
|
|
{
|
2025-02-22 08:12:41 +00:00
|
|
|
|
WindowState = WindowState.Minimized;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void Button_Background(object sender, RoutedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Hide();
|
2025-01-04 16:08:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ForceClose()
|
|
|
|
|
|
{
|
2025-01-10 05:06:37 +00:00
|
|
|
|
Close();
|
2025-02-26 12:11:27 +00:00
|
|
|
|
_cancelProgress?.Invoke();
|
2025-01-04 15:11:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|