Skip to content

Commit

Permalink
Re-implement console --progress
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Oct 11, 2023
1 parent dfc9850 commit 485c446
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 13 deletions.
5 changes: 4 additions & 1 deletion NAPS2.Lib.Gtk/EntryPoints/GtkEntryPoint.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using NAPS2.Modules;
using NAPS2.EtoForms;
using NAPS2.EtoForms.Gtk;
using NAPS2.Modules;

namespace NAPS2.EntryPoints;

Expand All @@ -10,6 +12,7 @@ public static class GtkEntryPoint
public static int Run(string[] args)
{
GLib.ExceptionManager.UnhandledException += UnhandledGtkException;
EtoPlatform.Current = new GtkEtoPlatform();

if (args.Length > 0 && args[0] is "cli" or "console")
{
Expand Down
2 changes: 0 additions & 2 deletions NAPS2.Lib.Gtk/Modules/GtkModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,5 @@ protected override void Load(ContainerBuilder builder)

builder.RegisterType<GtkDesktopForm>().As<DesktopForm>();
builder.RegisterType<GtkPreviewForm>().As<PreviewForm>();

EtoPlatform.Current = new GtkEtoPlatform();
}
}
6 changes: 5 additions & 1 deletion NAPS2.Lib.Mac/EntryPoints/MacEntryPoint.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using NAPS2.Modules;
using NAPS2.EtoForms;
using NAPS2.EtoForms.Mac;
using NAPS2.Modules;

namespace NAPS2.EntryPoints;

Expand All @@ -19,6 +21,8 @@ public static int Run(string[] args)
Log.Error($"Marshalling ObjC exception: {eventArgs.Exception.Description}");
};

EtoPlatform.Current = new MacEtoPlatform();

if (args.Length > 0 && args[0] is "cli" or "console")
{
return ConsoleEntryPoint.Run(args.Skip(1).ToArray(), new MacImagesModule());
Expand Down
2 changes: 0 additions & 2 deletions NAPS2.Lib.Mac/Modules/MacModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@ protected override void Load(ContainerBuilder builder)

builder.RegisterType<MacDesktopForm>().As<DesktopForm>();
builder.RegisterType<MacPreviewForm>().As<PreviewForm>();

EtoPlatform.Current = new MacEtoPlatform();
}
}
6 changes: 5 additions & 1 deletion NAPS2.Lib.WinForms/EntryPoints/WinFormsEntryPoint.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using NAPS2.Modules;
using NAPS2.EtoForms;
using NAPS2.EtoForms.WinForms;
using NAPS2.Modules;

namespace NAPS2.EntryPoints;

Expand All @@ -9,6 +11,8 @@ public static class WinFormsEntryPoint
{
public static int Run(string[] args)
{
EtoPlatform.Current = new WinFormsEtoPlatform();

if (args.Length > 0 && args[0] == "worker")
{
return WindowsWorkerEntryPoint.Run(args.Skip(1).ToArray());
Expand Down
5 changes: 4 additions & 1 deletion NAPS2.Lib.WinForms/EntryPoints/WindowsConsoleEntryPoint.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using NAPS2.Modules;
using NAPS2.EtoForms;
using NAPS2.EtoForms.WinForms;
using NAPS2.Modules;

namespace NAPS2.EntryPoints;

Expand All @@ -9,6 +11,7 @@ public static class WindowsConsoleEntryPoint
{
public static int Run(string[] args)
{
EtoPlatform.Current = new WinFormsEtoPlatform();
return ConsoleEntryPoint.Run(args, new GdiModule());
}
}
1 change: 0 additions & 1 deletion NAPS2.Lib.WinForms/Modules/WinFormsModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ protected override void Load(ContainerBuilder builder)

builder.RegisterType<WinFormsDesktopForm>().As<DesktopForm>();

EtoPlatform.Current = new WinFormsEtoPlatform();
// TODO: Can we add a test for this?
builder.RegisterBuildCallback(ctx =>
Log.EventLogger = ctx.Resolve<WindowsEventLogger>());
Expand Down
24 changes: 23 additions & 1 deletion NAPS2.Lib/EntryPoints/ConsoleEntryPoint.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Autofac;
using CommandLine;
using NAPS2.Automation;
using NAPS2.EtoForms;
using NAPS2.Modules;
using NAPS2.Remoting.Worker;
using NAPS2.Scan;
Expand Down Expand Up @@ -35,7 +36,22 @@ public static int Run(string[] args, Module imageModule)

// Run the scan automation logic
var scanning = container.Resolve<AutomatedScanning>();
scanning.Execute().Wait();

if (options.Progress)
{
// We need to set up an Eto application in order to be able to display a progress GUI
EtoPlatform.Current.InitializePlatform();
container.Resolve<CultureHelper>().SetCulturesFromConfig();

var application = EtoPlatform.Current.CreateApplication();
application.UnhandledException += UnhandledException;
application.Initialized += (_, _) => scanning.Execute().ContinueWith(_ => application.Quit());
EtoPlatform.Current.RunApplication(application);
}
else
{
scanning.Execute().Wait();
}

return ((ConsoleErrorOutput) container.Resolve<ErrorOutput>()).HasError ? 1 : 0;
}
Expand All @@ -45,4 +61,10 @@ private static void UnhandledTaskException(object? sender, UnobservedTaskExcepti
Log.FatalException("An error occurred that caused the task to terminate.", e.Exception);
e.SetObserved();
}

private static void UnhandledException(object? sender, Eto.UnhandledExceptionEventArgs e)
{
Log.FatalException("An error occurred that caused the application to close.",
e.ExceptionObject as Exception ?? new Exception());
}
}
30 changes: 30 additions & 0 deletions NAPS2.Lib/EtoForms/EtoInvoker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Eto.Forms;

namespace NAPS2.EtoForms;

public class EtoInvoker : IInvoker
{
private readonly Application _application;

public EtoInvoker(Application application)
{
_application = application;
}

public void Invoke(Action action)
{
_application.Invoke(action);
}

public void InvokeDispatch(Action action)
{
_application.AsyncInvoke(action);
}

public T InvokeGet<T>(Func<T> func)
{
T value = default!;
_application.Invoke(() => value = func());
return value;
}
}
7 changes: 7 additions & 0 deletions NAPS2.Lib/EtoForms/EtoPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ public virtual void RunApplication(Application application, Form mainForm)
application.Run(mainForm);
}

public virtual void RunApplication(Application application)
{
// TODO: Can we use EtoInvoker more generally and get rid of some of the platform-specific stuff?
Invoker.Current = new EtoInvoker(application);
application.Run();
}

public virtual void SetContainerSize(Window window, Control container, Size size, int padding)
{
}
Expand Down
26 changes: 26 additions & 0 deletions NAPS2.Lib/EtoForms/Notifications/StubNotify.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using NAPS2.Update;

namespace NAPS2.EtoForms.Notifications;

public class StubNotify : INotify
{
public void PdfSaved(string path)
{
}

public void ImagesSaved(int imageCount, string path)
{
}

public void DonatePrompt()
{
}

public void OperationProgress(OperationProgress progress, IOperation op)
{
}

public void UpdateAvailable(IUpdateChecker updateChecker, UpdateInfo update)
{
}
}
7 changes: 5 additions & 2 deletions NAPS2.Lib/Modules/AutoFacHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ namespace NAPS2.Modules;

public class AutoFacHelper
{
public static IContainer FromModules(params IModule[] modules)
public static IContainer FromModules(params IModule?[] modules)
{
var builder = new ContainerBuilder();
builder.RegisterSource<AnyConcreteTypeNotAlreadyRegisteredSource>();
foreach (var module in modules)
{
builder.RegisterModule(module);
if (module != null)
{
builder.RegisterModule(module);
}
}
return builder.Build();
}
Expand Down
10 changes: 9 additions & 1 deletion NAPS2.Lib/Modules/ConsoleModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ protected override void Load(ContainerBuilder builder)
builder.RegisterType<ConsolePdfPasswordProvider>().As<IPdfPasswordProvider>();
builder.RegisterType<ConsoleErrorOutput>().As<ErrorOutput>().SingleInstance();
builder.RegisterType<ConsoleOverwritePrompt>().As<IOverwritePrompt>();
builder.RegisterType<ConsoleOperationProgress>().As<OperationProgress>();
if (_options.Progress)
{
builder.RegisterType<EtoOperationProgress>().As<OperationProgress>();
}
else
{
builder.RegisterType<ConsoleOperationProgress>().As<OperationProgress>();
}
builder.RegisterType<StubNotify>().As<INotify>();
// TODO: We might want an eto-based dialog helper, or at least handle dialogs in a more user-friendly way than just silently doing nothing
builder.RegisterType<StubDialogHelper>().As<DialogHelper>();
builder.RegisterType<ConsoleOutput>().AsSelf().WithParameter("writer", Console.Out);
Expand Down

0 comments on commit 485c446

Please sign in to comment.