Skip to content

Commit

Permalink
feat: Added option to run an action before showing the main form (#35)
Browse files Browse the repository at this point in the history
* feat: Added option to run an action before showing the main form

* fix: Direct call for `GetRequiredService<T>`

* fix: Fixed flacky tests
  • Loading branch information
samtrion authored May 23, 2024
1 parent ef974ca commit 57ec059
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public T GetFormular<T>()
_semaphore.Wait();
try
{
return synchronizationContext.Invoke(() => serviceProvider.GetRequiredService<T>())!;
return serviceProvider.GetRequiredService<T>();
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,17 @@ private void OnApplicationStopping()
private void StartUIThread()
{
_ = Application.SetHighDpiMode(_options.HighDpiMode);
Application.SetCompatibleTextRenderingDefault(_options.CompatibleTextRenderingDefault);
if (_options.EnableVisualStyles)
{
Application.EnableVisualStyles();
}
Application.SetCompatibleTextRenderingDefault(_options.CompatibleTextRenderingDefault);

if (_options.DefaultFont is not null)
{
Application.SetDefaultFont(_options.DefaultFont);
}

Application.ApplicationExit += OnApplicationExit;

// Disable the auto install of the WindowsFormsSynchronizationContext.
Expand All @@ -98,6 +104,15 @@ private void StartUIThread()
synchronizationContextProvider.Context = new WindowsFormsSynchronizationContext();
SynchronizationContext.SetSynchronizationContext(synchronizationContextProvider.Context);

if (_options.PreloadAction is not null)
{
using (var scope = serviceProvider.CreateScope())
{
var formsProvider = scope.ServiceProvider.GetRequiredService<IFormularProvider>();
_options.PreloadAction.Invoke(scope.ServiceProvider, formsProvider);
}
}

var applicationContext = serviceProvider.GetRequiredService<ApplicationContext>();

Application.Run(applicationContext);
Expand Down
12 changes: 12 additions & 0 deletions src/NetEvolve.Extensions.Hosting.WinForms/WindowsFormsOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace NetEvolve.Extensions.Hosting.WinForms;

using System;
using System.Drawing;
using System.Windows.Forms;

/// <summary>
Expand Down Expand Up @@ -36,4 +38,14 @@ public sealed class WindowsFormsOptions
/// The default is <see langword="false"/>.
/// </summary>
public bool EnableConsoleShutdown { get; set; }

/// <summary>
/// The default font to use inside the WinForms host. If <see langword="null"/> the system default font is used.
/// </summary>
public Font? DefaultFont { get; set; }

/// <summary>
/// Optional action to run before the main form is created. Something like a splash screen, login form, etc.
/// </summary>
public Action<IServiceProvider, IFormularProvider>? PreloadAction { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public async Task InvokeAsync_Action_CancellationTokenCanceled_ThrowsTaskCancele
WindowsFormsSynchronizationContext.AutoInstall = false;
var provider = new WindowsFormsSynchronizationContextProvider
{
Context = SynchronizationContext.Current!
Context = new WindowsFormsSynchronizationContext()
};

// Act
Expand Down Expand Up @@ -264,7 +264,7 @@ public async Task InvokeAsync_Func_CancellationTokenCanceled_ThrowsTaskCanceledE
WindowsFormsSynchronizationContext.AutoInstall = false;
var provider = new WindowsFormsSynchronizationContextProvider
{
Context = SynchronizationContext.Current!
Context = new WindowsFormsSynchronizationContext()
};

// Act
Expand Down Expand Up @@ -349,7 +349,7 @@ public async Task InvokeAsync_FuncWithInput_CancellationTokenCanceled_ThrowsTask
WindowsFormsSynchronizationContext.AutoInstall = false;
var provider = new WindowsFormsSynchronizationContextProvider
{
Context = SynchronizationContext.Current!
Context = new WindowsFormsSynchronizationContext()
};

// Act
Expand Down

0 comments on commit 57ec059

Please sign in to comment.