2022-09-06 07:43:36 +03:00
|
|
|
using Eto.Forms;
|
2023-04-03 05:21:53 +03:00
|
|
|
using NAPS2.EtoForms.Notifications;
|
2022-09-06 07:43:36 +03:00
|
|
|
using NAPS2.EtoForms.Ui;
|
|
|
|
|
|
|
|
namespace NAPS2.EtoForms;
|
|
|
|
|
|
|
|
public class EtoOperationProgress : OperationProgress
|
|
|
|
{
|
|
|
|
private readonly IFormFactory _formFactory;
|
2023-04-04 06:56:26 +03:00
|
|
|
private readonly INotify _notify;
|
2022-09-06 07:43:36 +03:00
|
|
|
private readonly Naps2Config _config;
|
|
|
|
|
2023-12-07 07:17:50 +03:00
|
|
|
private readonly HashSet<IOperation> _activeOperations = [];
|
2022-09-06 07:43:36 +03:00
|
|
|
|
2023-04-04 06:56:26 +03:00
|
|
|
public EtoOperationProgress(IFormFactory formFactory, INotify notify, Naps2Config config)
|
2022-09-06 07:43:36 +03:00
|
|
|
{
|
|
|
|
_formFactory = formFactory;
|
2023-04-03 05:21:53 +03:00
|
|
|
_notify = notify;
|
2022-09-06 07:43:36 +03:00
|
|
|
_config = config;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Attach(IOperation op)
|
|
|
|
{
|
|
|
|
lock (this)
|
|
|
|
{
|
|
|
|
if (!_activeOperations.Contains(op))
|
|
|
|
{
|
|
|
|
_activeOperations.Add(op);
|
|
|
|
op.Finished += (sender, args) => _activeOperations.Remove(op);
|
|
|
|
if (op.IsFinished) _activeOperations.Remove(op);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void ShowProgress(IOperation op)
|
|
|
|
{
|
2022-12-31 08:22:07 +03:00
|
|
|
if (PlatformCompat.System.ShouldRememberBackgroundOperations &&
|
|
|
|
_config.Get(c => c.BackgroundOperations).Contains(op.GetType().Name))
|
2022-09-06 07:43:36 +03:00
|
|
|
{
|
|
|
|
ShowBackgroundProgress(op);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ShowModalProgress(op);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void ShowModalProgress(IOperation op)
|
|
|
|
{
|
|
|
|
Attach(op);
|
|
|
|
|
|
|
|
if (!op.IsFinished)
|
|
|
|
{
|
2023-02-06 02:01:22 +03:00
|
|
|
Invoker.Current.Invoke(() =>
|
|
|
|
{
|
|
|
|
var form = _formFactory.Create<ProgressForm>();
|
|
|
|
form.Operation = op;
|
|
|
|
form.ShowModal();
|
|
|
|
});
|
2022-09-06 07:43:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!op.IsFinished)
|
|
|
|
{
|
|
|
|
ShowBackgroundProgress(op);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void ShowBackgroundProgress(IOperation op)
|
|
|
|
{
|
|
|
|
Attach(op);
|
|
|
|
|
|
|
|
if (!op.IsFinished)
|
|
|
|
{
|
2023-04-03 05:21:53 +03:00
|
|
|
Invoker.Current.Invoke(() => _notify.OperationProgress(this, op));
|
2022-09-06 07:43:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void RenderStatus(IOperation op, Label textLabel, Label numberLabel, ProgressBar progressBar)
|
|
|
|
{
|
|
|
|
var status = op.Status ?? new OperationStatus();
|
|
|
|
textLabel.Text = status.StatusText;
|
|
|
|
progressBar.Indeterminate = status.MaxProgress == 1 || status.IndeterminateProgress;
|
|
|
|
// TODO: Continuous?
|
|
|
|
if (status.MaxProgress == 1 || status.ProgressType == OperationProgressType.None)
|
|
|
|
{
|
|
|
|
numberLabel.Text = "";
|
|
|
|
}
|
|
|
|
else if (status.MaxProgress == 0)
|
|
|
|
{
|
|
|
|
numberLabel.Text = "";
|
|
|
|
progressBar.MaxValue = 1;
|
|
|
|
progressBar.Value = 0;
|
|
|
|
}
|
|
|
|
else if (status.ProgressType == OperationProgressType.BarOnly)
|
|
|
|
{
|
|
|
|
numberLabel.Text = "";
|
|
|
|
progressBar.MaxValue = status.MaxProgress;
|
|
|
|
progressBar.Value = status.CurrentProgress;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
numberLabel.Text = status.ProgressType == OperationProgressType.MB
|
2022-12-31 08:22:07 +03:00
|
|
|
? string.Format(MiscResources.SizeProgress, (status.CurrentProgress / 1000000.0).ToString("f1"),
|
|
|
|
(status.MaxProgress / 1000000.0).ToString("f1"))
|
2022-09-06 07:43:36 +03:00
|
|
|
: string.Format(MiscResources.ProgressFormat, status.CurrentProgress, status.MaxProgress);
|
|
|
|
progressBar.MaxValue = status.MaxProgress;
|
|
|
|
progressBar.Value = status.CurrentProgress;
|
|
|
|
}
|
|
|
|
// Force the progress bar to render immediately
|
|
|
|
if (progressBar.Value < progressBar.MaxValue)
|
|
|
|
{
|
|
|
|
progressBar.Value += 1;
|
|
|
|
progressBar.Value -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override List<IOperation> ActiveOperations
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
lock (_activeOperations)
|
|
|
|
{
|
|
|
|
return _activeOperations.ToList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|