Skip to content

Commit 57ec059

Browse files
authored
feat: Added option to run an action before showing the main form (#35)
* feat: Added option to run an action before showing the main form * fix: Direct call for `GetRequiredService<T>` * fix: Fixed flacky tests
1 parent ef974ca commit 57ec059

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

src/NetEvolve.Extensions.Hosting.WinForms/Internals/FormularProvider.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public T GetFormular<T>()
4242
_semaphore.Wait();
4343
try
4444
{
45-
return synchronizationContext.Invoke(() => serviceProvider.GetRequiredService<T>())!;
45+
return serviceProvider.GetRequiredService<T>();
4646
}
4747
finally
4848
{

src/NetEvolve.Extensions.Hosting.WinForms/Internals/WindowsFormsHostedService.cs

+16-1
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,17 @@ private void OnApplicationStopping()
8484
private void StartUIThread()
8585
{
8686
_ = Application.SetHighDpiMode(_options.HighDpiMode);
87+
Application.SetCompatibleTextRenderingDefault(_options.CompatibleTextRenderingDefault);
8788
if (_options.EnableVisualStyles)
8889
{
8990
Application.EnableVisualStyles();
9091
}
91-
Application.SetCompatibleTextRenderingDefault(_options.CompatibleTextRenderingDefault);
92+
93+
if (_options.DefaultFont is not null)
94+
{
95+
Application.SetDefaultFont(_options.DefaultFont);
96+
}
97+
9298
Application.ApplicationExit += OnApplicationExit;
9399

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

107+
if (_options.PreloadAction is not null)
108+
{
109+
using (var scope = serviceProvider.CreateScope())
110+
{
111+
var formsProvider = scope.ServiceProvider.GetRequiredService<IFormularProvider>();
112+
_options.PreloadAction.Invoke(scope.ServiceProvider, formsProvider);
113+
}
114+
}
115+
101116
var applicationContext = serviceProvider.GetRequiredService<ApplicationContext>();
102117

103118
Application.Run(applicationContext);

src/NetEvolve.Extensions.Hosting.WinForms/WindowsFormsOptions.cs

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
namespace NetEvolve.Extensions.Hosting.WinForms;
22

3+
using System;
4+
using System.Drawing;
35
using System.Windows.Forms;
46

57
/// <summary>
@@ -36,4 +38,14 @@ public sealed class WindowsFormsOptions
3638
/// The default is <see langword="false"/>.
3739
/// </summary>
3840
public bool EnableConsoleShutdown { get; set; }
41+
42+
/// <summary>
43+
/// The default font to use inside the WinForms host. If <see langword="null"/> the system default font is used.
44+
/// </summary>
45+
public Font? DefaultFont { get; set; }
46+
47+
/// <summary>
48+
/// Optional action to run before the main form is created. Something like a splash screen, login form, etc.
49+
/// </summary>
50+
public Action<IServiceProvider, IFormularProvider>? PreloadAction { get; set; }
3951
}

tests/NetEvolve.Extensions.Hosting.WinForms.Tests.Unit/Internals/WindowsFormsSynchronizationContextProviderTests.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public async Task InvokeAsync_Action_CancellationTokenCanceled_ThrowsTaskCancele
182182
WindowsFormsSynchronizationContext.AutoInstall = false;
183183
var provider = new WindowsFormsSynchronizationContextProvider
184184
{
185-
Context = SynchronizationContext.Current!
185+
Context = new WindowsFormsSynchronizationContext()
186186
};
187187

188188
// Act
@@ -264,7 +264,7 @@ public async Task InvokeAsync_Func_CancellationTokenCanceled_ThrowsTaskCanceledE
264264
WindowsFormsSynchronizationContext.AutoInstall = false;
265265
var provider = new WindowsFormsSynchronizationContextProvider
266266
{
267-
Context = SynchronizationContext.Current!
267+
Context = new WindowsFormsSynchronizationContext()
268268
};
269269

270270
// Act
@@ -349,7 +349,7 @@ public async Task InvokeAsync_FuncWithInput_CancellationTokenCanceled_ThrowsTask
349349
WindowsFormsSynchronizationContext.AutoInstall = false;
350350
var provider = new WindowsFormsSynchronizationContextProvider
351351
{
352-
Context = SynchronizationContext.Current!
352+
Context = new WindowsFormsSynchronizationContext()
353353
};
354354

355355
// Act

0 commit comments

Comments
 (0)