Skip to content

A Swiss Army knife for developers.

Improved Hot Reload support (#998)

Etienne BAUDOUXc4e9632914b1
diff --git a/src/app/dev/DevToys.Blazor/Pages/SubPages/ToolPage.razor b/src/app/dev/DevToys.Blazor/Pages/SubPages/ToolPage.razor
index 9f3ff2adb..94222d70e 100644
--- a/src/app/dev/DevToys.Blazor/Pages/SubPages/ToolPage.razor
+++ b/src/app/dev/DevToys.Blazor/Pages/SubPages/ToolPage.razor
@@ -61,12 +61,6 @@
<TextBlock Text="@ViewModel.GetFavoriteButtonText(ViewModel.IsSelectedMenuItemAFavoriteTool)" />
</StackPanel>
</Button>
- <Button IsVisible="@Debugger.IsAttached"
- Appearance="ButtonAppearance.Stealth"
- ToolTip="Hot reload"
- @onclick="@OnHotReloadButtonClick">
- <FontIcon Glyph="@('\uEF68')" Style="color: red;" />
- </Button>
</StackPanel>
</GridCell>
diff --git a/src/app/dev/DevToys.Blazor/Pages/SubPages/ToolPage.razor.cs b/src/app/dev/DevToys.Blazor/Pages/SubPages/ToolPage.razor.cs
index 37d6b8826..8ecf19561 100644
--- a/src/app/dev/DevToys.Blazor/Pages/SubPages/ToolPage.razor.cs
+++ b/src/app/dev/DevToys.Blazor/Pages/SubPages/ToolPage.razor.cs
@@ -1,6 +1,7 @@
using DevToys.Blazor.Components;
using DevToys.Blazor.Core.Services;
using DevToys.Business.ViewModels;
+using DevToys.Core.Debugger;
using DevToys.Core.Tools.ViewItems;
namespace DevToys.Blazor.Pages.SubPages;
@@ -16,6 +17,9 @@ public partial class ToolPage : MefComponentBase, IDisposable
[Import]
internal ToolPageViewModel ViewModel { get; set; } = default!;
+ [Import]
+ internal HotReloadService HotReloadService { get; set; } = default!;
+
[Parameter]
public GuiToolViewItem? GuiToolViewItem { get; set; }
@@ -29,6 +33,7 @@ public partial class ToolPage : MefComponentBase, IDisposable
{
base.OnInitialized();
DialogService.CloseDialogRequested += DialogService_CloseDialogRequested;
+ HotReloadService.HotReloadRequestUpdateApplication += OnHotReloadRequestUpdateApplication;
}
protected override void OnParametersSet()
@@ -50,6 +55,7 @@ public partial class ToolPage : MefComponentBase, IDisposable
public void Dispose()
{
+ HotReloadService.HotReloadRequestUpdateApplication -= OnHotReloadRequestUpdateApplication;
DialogService.CloseDialogRequested -= DialogService_CloseDialogRequested;
if (ViewModel.ToolView is not null)
{
@@ -100,7 +106,7 @@ public partial class ToolPage : MefComponentBase, IDisposable
IndexPage.StateHasChanged();
}
- private void OnHotReloadButtonClick()
+ private void OnHotReloadRequestUpdateApplication(object? sender, HotReloadEventArgs e)
{
ViewModel.RebuildViewCommand.Execute(null);
}
diff --git a/src/app/dev/DevToys.Core/Debugger/HotReloadEventArgs.cs b/src/app/dev/DevToys.Core/Debugger/HotReloadEventArgs.cs
new file mode 100644
index 000000000..cf0afe1ad
--- /dev/null
+++ b/src/app/dev/DevToys.Core/Debugger/HotReloadEventArgs.cs
@@ -0,0 +1,11 @@
+namespace DevToys.Core.Debugger;
+
+public sealed class HotReloadEventArgs : EventArgs
+{
+ internal HotReloadEventArgs(Type[]? types)
+ {
+ Types = types;
+ }
+
+ public Type[]? Types { get; }
+}
diff --git a/src/app/dev/DevToys.Core/Debugger/HotReloadService.cs b/src/app/dev/DevToys.Core/Debugger/HotReloadService.cs
new file mode 100644
index 000000000..1963e4aaf
--- /dev/null
+++ b/src/app/dev/DevToys.Core/Debugger/HotReloadService.cs
@@ -0,0 +1,49 @@
+// This attribute will make the .NET runtime invoke the ClearCache and UpdateApplication methods when a hot reload
+// is requested from Visual Studio / VS Code or Rider.
+// More info: https://learn.microsoft.com/en-us/visualstudio/debugger/hot-reload-metadataupdatehandler
+[assembly: System.Reflection.Metadata.MetadataUpdateHandler(typeof(DevToys.Core.Debugger.HotReloadService))]
+
+namespace DevToys.Core.Debugger;
+
+[Export(typeof(HotReloadService))]
+public sealed class HotReloadService
+{
+ [ImportingConstructor]
+ public HotReloadService()
+ {
+ internalHotReloadRequestClearCache += (sender, args) => HotReloadRequestClearCache?.Invoke(sender, args);
+ internalHotReloadRequestUpdateApplication += (sender, args) => HotReloadRequestUpdateApplication?.Invoke(sender, args);
+ }
+
+ public EventHandler<HotReloadEventArgs>? HotReloadRequestClearCache;
+
+ public EventHandler<HotReloadEventArgs>? HotReloadRequestUpdateApplication;
+
+ private static EventHandler<HotReloadEventArgs>? internalHotReloadRequestClearCache;
+
+ private static EventHandler<HotReloadEventArgs>? internalHotReloadRequestUpdateApplication;
+
+ /// <summary>
+ /// Hot reload handler invoked by thanks to the MetadataUpdateHandler attribute.
+ /// </summary>
+ [System.Diagnostics.CodeAnalysis.SuppressMessage("CodeQuality", "IDE0051:Remove unused private members", Justification = "Invoked by .NET runtime on Hot Reload.")]
+ private static void ClearCache(Type[]? types)
+ {
+ if (System.Diagnostics.Debugger.IsAttached)
+ {
+ internalHotReloadRequestClearCache?.Invoke(null, new HotReloadEventArgs(types));
+ }
+ }
+
+ /// <summary>
+ /// Hot reload handler invoked by thanks to the MetadataUpdateHandler attribute.
+ /// </summary>
+ [System.Diagnostics.CodeAnalysis.SuppressMessage("CodeQuality", "IDE0051:Remove unused private members", Justification = "Invoked by .NET runtime on Hot Reload.")]
+ private static void UpdateApplication(Type[]? types)
+ {
+ if (System.Diagnostics.Debugger.IsAttached)
+ {
+ internalHotReloadRequestUpdateApplication?.Invoke(null, new HotReloadEventArgs(types));
+ }
+ }
+}