Skip to content

Commit

Permalink
Add Create and CreateLite helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Kir-Antipov committed Dec 27, 2024
1 parent 7805397 commit fced7b6
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
8 changes: 5 additions & 3 deletions src/HotAvalonia.Extensions/AvaloniaHotReloadExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@ private static void EnableHotReload(Application app, AvaloniaProjectLocator proj
{
if (!s_apps.TryGetValue(app, out IHotReloadContext? context))
{
IHotReloadContext appDomainContext = AvaloniaHotReloadContext.FromAppDomain();
IHotReloadContext assetContext = AvaloniaHotReloadContext.ForAssets();
context = HotReloadContext.Combine(appDomainContext, assetContext);
#if ENABLE_LITE_XAML_HOT_RELOAD
context = AvaloniaHotReloadContext.CreateLite(projectLocator);
#else
context = AvaloniaHotReloadContext.Create(projectLocator);
#endif
s_apps.Add(app, context);
}

Expand Down
8 changes: 5 additions & 3 deletions src/HotAvalonia.Extensions/AvaloniaHotReloadExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ type internal AvaloniaHotReloadExtensions =
match s_apps.TryGetValue(app) with
| true, context -> context.EnableHotReload()
| _ ->
let appDomainContext = AvaloniaHotReloadContext.FromAppDomain()
let assetContext = AvaloniaHotReloadContext.ForAssets()
let context = HotReloadContext.Combine(appDomainContext, assetContext)
#if ENABLE_LITE_XAML_HOT_RELOAD
let context = AvaloniaHotReloadContext.CreateLite(projectLocator)
#else
let context = AvaloniaHotReloadContext.Create(projectLocator)
#endif
s_apps.Add(app, context)

context.EnableHotReload()
Expand Down
8 changes: 5 additions & 3 deletions src/HotAvalonia.Extensions/AvaloniaHotReloadExtensions.vb
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,11 @@ Namespace Global.HotAvalonia
Private Sub EnableHotReload(ByVal app As Application, ByVal projectLocator As AvaloniaProjectLocator)
Dim context As IHotReloadContext = Nothing
If Not s_apps.TryGetValue(app, context) Then
Dim appDomainContext As IHotReloadContext = AvaloniaHotReloadContext.FromAppDomain()
Dim assetContext As IHotReloadContext = AvaloniaHotReloadContext.ForAssets()
context = HotReloadContext.Combine(appDomainContext, assetContext)
#If ENABLE_LITE_XAML_HOT_RELOAD Then
context = AvaloniaHotReloadContext.CreateLite(projectLocator)
#Else
context = AvaloniaHotReloadContext.Create(projectLocator)
#End If
s_apps.Add(app, context)
End If

Expand Down
46 changes: 46 additions & 0 deletions src/HotAvalonia/AvaloniaHotReloadContext.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.ComponentModel;
using System.Reflection;
using Avalonia.Controls;
using Avalonia.Markup.Xaml.Converters;
Expand All @@ -15,6 +16,51 @@ namespace HotAvalonia;
/// </summary>
public static class AvaloniaHotReloadContext
{
/// <inheritdoc cref="Create(AvaloniaProjectLocator)"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public static IHotReloadContext Create()
=> Create(new AvaloniaProjectLocator());

/// <summary>
/// Creates a hot reload context for the current environment.
/// </summary>
/// <remarks>
/// This method is opinionated and represents the "best" way to create
/// a hot reload context for the current environment.
/// However, the specific details of what constitutes "best" are subject to change.
/// </remarks>
/// <param name="projectLocator">The project locator used to find source directories of assemblies.</param>
/// <returns>A hot reload context for the current environment.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public static IHotReloadContext Create(AvaloniaProjectLocator projectLocator)
{
IHotReloadContext appDomainContext = FromAppDomain(AppDomain.CurrentDomain, projectLocator);
IHotReloadContext assetContext = ForAssets(AvaloniaServiceProvider.Current, projectLocator);
return HotReloadContext.Combine([appDomainContext, assetContext]);
}

/// <inheritdoc cref="CreateLite(AvaloniaProjectLocator)"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public static IHotReloadContext CreateLite()
=> CreateLite(new AvaloniaProjectLocator());

/// <summary>
/// Creates a lightweight hot reload context for the current environment.
/// </summary>
/// <remarks>
/// This method is opinionated and represents the "best" lightweight way to create
/// a hot reload context for the current environment. However, the specific details
/// of what constitutes "best" are subject to change.
/// </remarks>
/// <param name="projectLocator">The project locator used to find source directories of assemblies.</param>
/// <returns>A lightweight hot reload context for the current environment.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public static IHotReloadContext CreateLite(AvaloniaProjectLocator projectLocator)
{
IHotReloadContext appDomainContext = FromAppDomain(AppDomain.CurrentDomain, projectLocator);
return appDomainContext;
}

/// <inheritdoc cref="ForAssets(IServiceProvider)"/>
public static IHotReloadContext ForAssets()
=> ForAssets(AvaloniaServiceProvider.Current);
Expand Down

0 comments on commit fced7b6

Please sign in to comment.