Give OperationProgress a static default

This commit is contained in:
Ben Olden-Cooligan 2018-12-03 20:45:22 -05:00
parent 0d8f393bc1
commit 83147b716f
20 changed files with 106 additions and 93 deletions

View File

@ -7,6 +7,7 @@ using NAPS2.ImportExport.Pdf;
using NAPS2.Operation;
using NAPS2.Images;
using NAPS2.Util;
using Ninject;
using Ninject.Modules;
namespace NAPS2.DI.Modules
@ -18,8 +19,10 @@ namespace NAPS2.DI.Modules
Bind<IPdfPasswordProvider>().To<ConsolePdfPasswordProvider>();
Bind<IErrorOutput>().To<ConsoleErrorOutput>();
Bind<IOverwritePrompt>().To<ConsoleOverwritePrompt>();
Bind<IOperationProgress>().To<ConsoleOperationProgress>();
Bind<OperationProgress>().To<ConsoleOperationProgress>();
Bind<IComponentInstallPrompt>().To<ConsoleComponentInstallPrompt>();
OperationProgress.Default = Kernel.Get<OperationProgress>();
}
}
}

View File

@ -6,6 +6,7 @@ using NAPS2.ImportExport.Pdf;
using NAPS2.Operation;
using NAPS2.Util;
using NAPS2.WinForms;
using Ninject;
using Ninject.Modules;
namespace NAPS2.DI.Modules
@ -17,8 +18,10 @@ namespace NAPS2.DI.Modules
Bind<IPdfPasswordProvider>().To<WinFormsPdfPasswordProvider>();
Bind<IErrorOutput>().To<MessageBoxErrorOutput>();
Bind<IOverwritePrompt>().To<WinFormsOverwritePrompt>();
Bind<IOperationProgress>().To<WinFormsOperationProgress>().InSingletonScope();
Bind<OperationProgress>().To<WinFormsOperationProgress>().InSingletonScope();
Bind<IComponentInstallPrompt>().To<WinFormsComponentInstallPrompt>();
OperationProgress.Default = Kernel.Get<OperationProgress>();
}
}
}

View File

@ -6,26 +6,26 @@ using NAPS2.Operation;
namespace NAPS2.Automation
{
public class ConsoleOperationProgress : IOperationProgress
public class ConsoleOperationProgress : OperationProgress
{
public void Attach(IOperation op)
public override void Attach(IOperation op)
{
}
public void ShowProgress(IOperation op)
public override void ShowProgress(IOperation op)
{
op.Wait();
}
public void ShowModalProgress(IOperation op)
public override void ShowModalProgress(IOperation op)
{
}
public void ShowBackgroundProgress(IOperation op) {
public override void ShowBackgroundProgress(IOperation op) {
}
public void RenderStatus(IOperation op, Label textLabel, Label numberLabel, ProgressBar progressBar) => throw new NotSupportedException();
public override void RenderStatus(IOperation op, Label textLabel, Label numberLabel, ProgressBar progressBar) => throw new NotSupportedException();
public List<IOperation> ActiveOperations => throw new NotSupportedException();
public override List<IOperation> ActiveOperations => throw new NotSupportedException();
}
}

View File

@ -103,25 +103,18 @@ namespace NAPS2.Images
return tempFilePath;
}
private readonly IOperationProgress operationProgress;
private readonly OperationProgress operationProgress;
private readonly OcrRequestQueue ocrRequestQueue;
private readonly BlankDetector blankDetector;
public ScannedImageHelper() : this(new StubOperationProgress())
public ScannedImageHelper()
{
}
public ScannedImageHelper(IOperationProgress operationProgress)
{
this.operationProgress = operationProgress;
if (OcrManager.HasDefault)
{
ocrRequestQueue = new OcrRequestQueue(OcrManager.Default, operationProgress);
}
operationProgress = OperationProgress.Default;
ocrRequestQueue = OcrRequestQueue.Default;
blankDetector = BlankDetector.Default;
}
public ScannedImageHelper(IOperationProgress operationProgress, OcrRequestQueue ocrRequestQueue, BlankDetector blankDetector)
public ScannedImageHelper(OperationProgress operationProgress, OcrRequestQueue ocrRequestQueue, BlankDetector blankDetector)
{
this.operationProgress = operationProgress;
this.ocrRequestQueue = ocrRequestQueue;

View File

@ -24,9 +24,9 @@ namespace NAPS2.ImportExport
private readonly OcrManager ocrManager;
private readonly IErrorOutput errorOutput;
private readonly DialogHelper dialogHelper;
private readonly IOperationProgress operationProgress;
private readonly OperationProgress operationProgress;
public AutoSave(IOperationFactory operationFactory, PdfSettingsContainer pdfSettingsContainer, OcrManager ocrManager, IErrorOutput errorOutput, DialogHelper dialogHelper, IOperationProgress operationProgress)
public AutoSave(IOperationFactory operationFactory, PdfSettingsContainer pdfSettingsContainer, OcrManager ocrManager, IErrorOutput errorOutput, DialogHelper dialogHelper, OperationProgress operationProgress)
{
this.operationFactory = operationFactory;
this.pdfSettingsContainer = pdfSettingsContainer;

View File

@ -349,7 +349,7 @@
</Compile>
<Compile Include="Dependencies\PlatformSupport.cs" />
<Compile Include="Images\AutoDeskew.cs" />
<Compile Include="Operation\IOperationProgress.cs" />
<Compile Include="Operation\OperationProgress.cs" />
<Compile Include="Images\ScannedImageRenderer.cs" />
<Compile Include="Dependencies\DownloadFormat.cs" />
<Compile Include="Dependencies\DownloadInfo.cs" />

View File

@ -13,17 +13,29 @@ namespace NAPS2.Ocr
{
public class OcrRequestQueue
{
private static OcrRequestQueue _default;
public static OcrRequestQueue Default
{
get => _default ?? (_default = new OcrRequestQueue());
set => _default = value ?? throw new ArgumentNullException(nameof(value));
}
private readonly Dictionary<OcrRequestParams, OcrRequest> requestCache = new Dictionary<OcrRequestParams, OcrRequest>();
private readonly Semaphore queueWaitHandle = new Semaphore(0, Int32.MaxValue);
private readonly Semaphore queueWaitHandle = new Semaphore(0, int.MaxValue);
private List<Task> workerTasks = new List<Task>();
private CancellationTokenSource workerCts = new CancellationTokenSource();
private readonly OcrManager ocrManager;
private readonly IOperationProgress operationProgress;
private readonly OperationProgress operationProgress;
private OcrOperation currentOp;
public OcrRequestQueue(OcrManager ocrManager, IOperationProgress operationProgress)
public OcrRequestQueue() : this(OcrManager.Default, OperationProgress.Default)
{
}
public OcrRequestQueue(OcrManager ocrManager, OperationProgress operationProgress)
{
this.ocrManager = ocrManager;
this.operationProgress = operationProgress;

View File

@ -1,27 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace NAPS2.Operation
{
/// <summary>
/// A base interface for objects capabable of displaying progress for an operation.
///
/// Implementors: WinFormsOperationProgress, ConsoleOperationProgress
/// </summary>
public interface IOperationProgress
{
void Attach(IOperation op);
void ShowProgress(IOperation op);
void ShowModalProgress(IOperation op);
void ShowBackgroundProgress(IOperation op);
void RenderStatus(IOperation op, Label textLabel, Label numberLabel, ProgressBar progressBar);
List<IOperation> ActiveOperations { get; }
}
}

View File

@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace NAPS2.Operation
{
/// <summary>
/// A base class for objects capabable of displaying progress for an operation.
/// </summary>
public abstract class OperationProgress
{
private static OperationProgress _default = new StubOperationProgress();
public static OperationProgress Default
{
get => _default;
set => _default = value ?? throw new ArgumentNullException(nameof(value));
}
public abstract void Attach(IOperation op);
public abstract void ShowProgress(IOperation op);
public abstract void ShowModalProgress(IOperation op);
public abstract void ShowBackgroundProgress(IOperation op);
public abstract void RenderStatus(IOperation op, Label textLabel, Label numberLabel, ProgressBar progressBar);
public abstract List<IOperation> ActiveOperations { get; }
}
}

View File

@ -5,26 +5,26 @@ using System.Windows.Forms;
namespace NAPS2.Operation
{
public class StubOperationProgress : IOperationProgress
public class StubOperationProgress : OperationProgress
{
public void Attach(IOperation op)
public override void Attach(IOperation op)
{
}
public void ShowProgress(IOperation op)
public override void ShowProgress(IOperation op)
{
}
public void ShowModalProgress(IOperation op)
public override void ShowModalProgress(IOperation op)
{
}
public void ShowBackgroundProgress(IOperation op)
public override void ShowBackgroundProgress(IOperation op)
{
}
public void RenderStatus(IOperation op, Label textLabel, Label numberLabel, ProgressBar progressBar) => throw new NotSupportedException();
public override void RenderStatus(IOperation op, Label textLabel, Label numberLabel, ProgressBar progressBar) => throw new NotSupportedException();
public List<IOperation> ActiveOperations => throw new NotSupportedException();
public override List<IOperation> ActiveOperations => throw new NotSupportedException();
}
}

View File

@ -20,9 +20,9 @@ namespace NAPS2.Recovery
{
private readonly IFormFactory formFactory;
private readonly ScannedImageRenderer scannedImageRenderer;
private readonly IOperationProgress operationProgress;
private readonly OperationProgress operationProgress;
public RecoveryManager(IFormFactory formFactory, ScannedImageRenderer scannedImageRenderer, IOperationProgress operationProgress)
public RecoveryManager(IFormFactory formFactory, ScannedImageRenderer scannedImageRenderer, OperationProgress operationProgress)
{
this.formFactory = formFactory;
this.scannedImageRenderer = scannedImageRenderer;

View File

@ -15,18 +15,16 @@ namespace NAPS2.Scan.Wia
{
public const string DRIVER_NAME = "wia";
private readonly IOperationProgress operationProgress;
private readonly OperationProgress operationProgress;
private readonly ScannedImageHelper scannedImageHelper;
public WiaScanDriver() : this(new StubOperationProgress())
public WiaScanDriver()
{
operationProgress = OperationProgress.Default;
scannedImageHelper = new ScannedImageHelper();
}
public WiaScanDriver(IOperationProgress operationProgress) : this(operationProgress, new ScannedImageHelper(operationProgress))
{
}
public WiaScanDriver(IOperationProgress operationProgress, ScannedImageHelper scannedImageHelper)
public WiaScanDriver(OperationProgress operationProgress, ScannedImageHelper scannedImageHelper)
{
this.operationProgress = operationProgress;
this.scannedImageHelper = scannedImageHelper;

View File

@ -21,9 +21,9 @@ namespace NAPS2.Update
#endif
private readonly IOperationFactory operationFactory;
private readonly IOperationProgress operationProgress;
private readonly OperationProgress operationProgress;
public UpdateChecker(IOperationFactory operationFactory, IOperationProgress operationProgress)
public UpdateChecker(IOperationFactory operationFactory, OperationProgress operationProgress)
{
this.operationFactory = operationFactory;
this.operationProgress = operationProgress;

View File

@ -57,7 +57,7 @@ namespace NAPS2.WinForms
private readonly NotificationManager notify;
private readonly CultureInitializer cultureInitializer;
private readonly IWorkerServiceFactory workerServiceFactory;
private readonly IOperationProgress operationProgress;
private readonly OperationProgress operationProgress;
private readonly UpdateChecker updateChecker;
#endregion
@ -74,7 +74,7 @@ namespace NAPS2.WinForms
#region Initialization and Culture
public FDesktop(StringWrapper stringWrapper, RecoveryManager recoveryManager, OcrManager ocrManager, IProfileManager profileManager, IScanPerformer scanPerformer, IScannedImagePrinter scannedImagePrinter, ChangeTracker changeTracker, StillImage stillImage, IOperationFactory operationFactory, KeyboardShortcutManager ksm, ThumbnailRenderer thumbnailRenderer, WinFormsExportHelper exportHelper, ScannedImageRenderer scannedImageRenderer, NotificationManager notify, CultureInitializer cultureInitializer, IWorkerServiceFactory workerServiceFactory, IOperationProgress operationProgress, UpdateChecker updateChecker)
public FDesktop(StringWrapper stringWrapper, RecoveryManager recoveryManager, OcrManager ocrManager, IProfileManager profileManager, IScanPerformer scanPerformer, IScannedImagePrinter scannedImagePrinter, ChangeTracker changeTracker, StillImage stillImage, IOperationFactory operationFactory, KeyboardShortcutManager ksm, ThumbnailRenderer thumbnailRenderer, WinFormsExportHelper exportHelper, ScannedImageRenderer scannedImageRenderer, NotificationManager notify, CultureInitializer cultureInitializer, IWorkerServiceFactory workerServiceFactory, OperationProgress operationProgress, UpdateChecker updateChecker)
{
this.stringWrapper = stringWrapper;
this.recoveryManager = recoveryManager;

View File

@ -11,13 +11,13 @@ namespace NAPS2.WinForms
public partial class FProgress : FormBase
{
private readonly IErrorOutput errorOutput;
private readonly IOperationProgress operationProgress;
private readonly OperationProgress operationProgress;
private volatile bool loaded;
private volatile bool background;
private IOperation operation;
public FProgress(IErrorOutput errorOutput, IOperationProgress operationProgress)
public FProgress(IErrorOutput errorOutput, OperationProgress operationProgress)
{
this.errorOutput = errorOutput;
this.operationProgress = operationProgress;

View File

@ -48,9 +48,9 @@ namespace NAPS2.WinForms
private ToolStripButton tsSharpen;
private readonly ScannedImageRenderer scannedImageRenderer;
private readonly KeyboardShortcutManager ksm;
private readonly IOperationProgress operationProgress;
private readonly OperationProgress operationProgress;
public FViewer(ChangeTracker changeTracker, IOperationFactory operationFactory, WinFormsExportHelper exportHelper, ScannedImageRenderer scannedImageRenderer, KeyboardShortcutManager ksm, IOperationProgress operationProgress)
public FViewer(ChangeTracker changeTracker, IOperationFactory operationFactory, WinFormsExportHelper exportHelper, ScannedImageRenderer scannedImageRenderer, KeyboardShortcutManager ksm, OperationProgress operationProgress)
{
this.changeTracker = changeTracker;
this.operationFactory = operationFactory;

View File

@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using NAPS2.Config;
using NAPS2.Operation;
using NAPS2.Update;
@ -50,7 +49,7 @@ namespace NAPS2.WinForms
Show(new DonatePromptNotifyWidget());
}
public void OperationProgress(IOperationProgress opModalProgress, IOperation op)
public void OperationProgress(OperationProgress opModalProgress, IOperation op)
{
Show(new OperationProgressNotifyWidget(opModalProgress, op));
}

View File

@ -8,10 +8,10 @@ namespace NAPS2.WinForms
{
public partial class OperationProgressNotifyWidget : NotifyWidgetBase
{
private readonly IOperationProgress operationProgress;
private readonly OperationProgress operationProgress;
private readonly IOperation op;
public OperationProgressNotifyWidget(IOperationProgress operationProgress, IOperation op)
public OperationProgressNotifyWidget(OperationProgress operationProgress, IOperation op)
{
InitializeComponent();

View File

@ -27,9 +27,9 @@ namespace NAPS2.WinForms
private readonly IOperationFactory operationFactory;
private readonly IFormFactory formFactory;
private readonly OcrManager ocrManager;
private readonly IOperationProgress operationProgress;
private readonly OperationProgress operationProgress;
public WinFormsExportHelper(PdfSettingsContainer pdfSettingsContainer, ImageSettingsContainer imageSettingsContainer, EmailSettingsContainer emailSettingsContainer, DialogHelper dialogHelper, ChangeTracker changeTracker, IOperationFactory operationFactory, IFormFactory formFactory, OcrManager ocrManager, IOperationProgress operationProgress)
public WinFormsExportHelper(PdfSettingsContainer pdfSettingsContainer, ImageSettingsContainer imageSettingsContainer, EmailSettingsContainer emailSettingsContainer, DialogHelper dialogHelper, ChangeTracker changeTracker, IOperationFactory operationFactory, IFormFactory formFactory, OcrManager ocrManager, OperationProgress operationProgress)
{
this.pdfSettingsContainer = pdfSettingsContainer;
this.imageSettingsContainer = imageSettingsContainer;

View File

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using NAPS2.Config;
using NAPS2.Lang.Resources;
@ -9,7 +8,7 @@ using NAPS2.Operation;
namespace NAPS2.WinForms
{
public class WinFormsOperationProgress : IOperationProgress
public class WinFormsOperationProgress : OperationProgress
{
private readonly IFormFactory formFactory;
private readonly NotificationManager notificationManager;
@ -22,7 +21,7 @@ namespace NAPS2.WinForms
this.notificationManager = notificationManager;
}
public void Attach(IOperation op)
public override void Attach(IOperation op)
{
lock (this)
{
@ -35,7 +34,7 @@ namespace NAPS2.WinForms
}
}
public void ShowProgress(IOperation op)
public override void ShowProgress(IOperation op)
{
if (UserConfig.Current.BackgroundOperations.Contains(op.GetType().Name))
{
@ -47,7 +46,7 @@ namespace NAPS2.WinForms
}
}
public void ShowModalProgress(IOperation op)
public override void ShowModalProgress(IOperation op)
{
Attach(op);
@ -67,7 +66,7 @@ namespace NAPS2.WinForms
}
}
public void ShowBackgroundProgress(IOperation op)
public override void ShowBackgroundProgress(IOperation op)
{
Attach(op);
@ -80,7 +79,7 @@ namespace NAPS2.WinForms
}
}
public void RenderStatus(IOperation op, Label textLabel, Label numberLabel, ProgressBar progressBar)
public override void RenderStatus(IOperation op, Label textLabel, Label numberLabel, ProgressBar progressBar)
{
var status = op.Status ?? new OperationStatus();
textLabel.Text = status.StatusText;
@ -119,7 +118,7 @@ namespace NAPS2.WinForms
}
}
public List<IOperation> ActiveOperations
public override List<IOperation> ActiveOperations
{
get
{