Skip to content

Commit

Permalink
Added MauiReactor features, working on Balance template
Browse files Browse the repository at this point in the history
  • Loading branch information
adospace committed Dec 10, 2024
1 parent 4635f1c commit 1dc9282
Show file tree
Hide file tree
Showing 32 changed files with 763 additions and 290 deletions.
2 changes: 1 addition & 1 deletion samples/Calculator/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static MauiApp CreateMauiApp()
app.UseTheme<AppTheme>();
})
#if DEBUG
.EnableMauiReactorHotReload()
//.EnableMauiReactorHotReload()
#endif
.ConfigureFonts(fonts =>
{
Expand Down
2 changes: 1 addition & 1 deletion samples/ChartApp/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static MauiApp CreateMauiApp()
//.AddLightTheme()
);
})
.EnableMauiReactorHotReload()
//.EnableMauiReactorHotReload()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
Expand Down
2 changes: 1 addition & 1 deletion samples/Contentics/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static MauiApp CreateMauiApp()
builder
.UseMauiReactorApp<Main>()
#if DEBUG
.EnableMauiReactorHotReload()
//.EnableMauiReactorHotReload()
#endif
.ConfigureFonts(fonts =>
{
Expand Down
2 changes: 1 addition & 1 deletion samples/IntegrationTest/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static MauiApp CreateMauiApp()


#if DEBUG
builder.EnableMauiReactorHotReload();
//builder.EnableMauiReactorHotReload();
builder.Logging.AddDebug();
#endif

Expand Down
2 changes: 1 addition & 1 deletion samples/MauiReactor.Calendar/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static MauiApp CreateMauiApp()
app.SetWindowsSpecificAssetsDirectory("Assets");
})
#if DEBUG
.EnableMauiReactorHotReload()
//.EnableMauiReactorHotReload()
#endif
.ConfigureFonts(fonts =>
{
Expand Down
2 changes: 1 addition & 1 deletion samples/MauiReactor.MapsDemo/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static MauiApp CreateMauiApp()
builder
.UseMauiReactorApp<MainPage>()
#if DEBUG
.EnableMauiReactorHotReload()
//.EnableMauiReactorHotReload()
#endif
.ConfigureFonts(fonts =>
{
Expand Down
22 changes: 1 addition & 21 deletions samples/MauiReactor.TestApp/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,7 @@ public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
//.UseMauiReactorApp<HomePage>(app =>
//{
// //we can mix styles from xaml dictionary with...
// app.AddResource("Resources/Styles/DefaultTheme.xaml");

// app.SetWindowsSpecificAssetsDirectory("Assets");

// //... the MauiReactor theming, but often it's easier to just manage styles in either XAML or c#
// app.UseTheme<AppTheme>();
//})
#if DEBUG
.EnableMauiReactorHotReload()
.OnMauiReactorUnhandledException((e) =>
{
System.Diagnostics.Debug.WriteLine(e.ExceptionObject);
})
//This will enable the FrameRateIndicator widget
//Disable before publishing the app
.EnableFrameRateIndicator()
#endif
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
Expand Down
4 changes: 4 additions & 0 deletions samples/MauiReactor.TestApp/MauiReactor.TestApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
<MauiFont Include="Resources\Fonts\*" />
</ItemGroup>

<ItemGroup>
<RuntimeHostConfigurationOption Include="MauiReactor.HotReload" Value="true" Trim="true" />
</ItemGroup>

<ItemGroup>
<None Remove="Resources\Images\tab_favorites.svg" />
<None Remove="Resources\Images\tab_home.svg" />
Expand Down
2 changes: 1 addition & 1 deletion samples/MauiReactor.TestAppWindow/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static MauiApp CreateMauiApp()
app.SetWindowsSpecificAssetsDirectory("Assets");
})
#if DEBUG
.EnableMauiReactorHotReload()
//.EnableMauiReactorHotReload()
#endif
.ConfigureFonts(fonts =>
{
Expand Down
2 changes: 1 addition & 1 deletion samples/MauiReactor.WeatherTwentyOne/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static MauiApp CreateMauiApp()
app.SetWindowsSpecificAssetsDirectory("Assets");
})
#if DEBUG
.EnableMauiReactorHotReload()
//.EnableMauiReactorHotReload()
#endif
.ConfigureFonts(fonts => {
fonts.AddFont("fa-solid-900.ttf", "FontAwesome");
Expand Down
2 changes: 1 addition & 1 deletion samples/SlidingPuzzle/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static MauiApp CreateMauiApp()
app.SetWindowsSpecificAssetsDirectory("Assets");
})
#if DEBUG
.EnableMauiReactorHotReload()
//.EnableMauiReactorHotReload()
#endif
.ConfigureFonts(fonts =>
{
Expand Down
2 changes: 1 addition & 1 deletion src/MauiReactor.Blazor.TestApp/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static MauiApp CreateMauiApp()
builder
.UseMauiReactorApp<MainPage>()
#if DEBUG
.EnableMauiReactorHotReload()
//.EnableMauiReactorHotReload()
#endif
.ConfigureFonts(fonts =>
{
Expand Down
2 changes: 1 addition & 1 deletion src/MauiReactor.FigmaPlugin/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static MauiApp CreateMauiApp()
builder
.UseMauiReactorApp<MainPage>()
#if DEBUG
.EnableMauiReactorHotReload()
//.EnableMauiReactorHotReload()
#endif
.ConfigureFonts(fonts =>
{
Expand Down
26 changes: 15 additions & 11 deletions src/MauiReactor/Grid.partial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ public Grid(IEnumerable<RowDefinition> rows, IEnumerable<ColumnDefinition> colum
string? IGrid.ColumnDefinitions { get; set; }
string? IGrid.RowDefinitions { get; set; }

private static readonly BindablePropertyKey _mauiReactorGridRowsKey = BindableProperty.CreateAttachedReadOnly(
nameof(_mauiReactorGridRowsKey),
typeof(HashSet<BindableProperty>), typeof(Grid<T>), null);

private static readonly BindablePropertyKey _mauiReactorGridColumnsKey = BindableProperty.CreateAttachedReadOnly(
nameof(_mauiReactorGridColumnsKey),
typeof(HashSet<BindableProperty>), typeof(Grid<T>), null);
private static readonly BindableProperty _mauiReactorGridRows = BindableProperty.CreateAttached(
nameof(_mauiReactorGridRows),
typeof(string),
typeof(Grid<T>),
null);

private static readonly BindableProperty _mauiReactorGridColumns = BindableProperty.CreateAttached(
nameof(_mauiReactorGridColumns),
typeof(string),
typeof(Grid<T>),
null);

protected override void OnUpdate()
{
Expand All @@ -54,18 +58,18 @@ protected override void OnUpdate()
// grid.RowDefinitions.Add(rowDefinition);
//}

var rowsOnNativeControl = (string?)NativeControl.GetValue(_mauiReactorGridRowsKey.BindableProperty);
var rowsOnNativeControl = (string?)NativeControl.GetValue(_mauiReactorGridRows);
if (rowsOnNativeControl != thisAsIGrid.RowDefinitions)
{
GridExtensions.SetRowDefinitions(NativeControl, thisAsIGrid.RowDefinitions);
NativeControl.SetValue(_mauiReactorGridColumnsKey.BindableProperty, thisAsIGrid.RowDefinitions);
NativeControl.SetValue(_mauiReactorGridColumns, thisAsIGrid.RowDefinitions);
}

var columnsOnNativeControl = (string?)NativeControl.GetValue(_mauiReactorGridColumnsKey.BindableProperty);
var columnsOnNativeControl = (string?)NativeControl.GetValue(_mauiReactorGridColumns);
if (columnsOnNativeControl != thisAsIGrid.ColumnDefinitions)
{
GridExtensions.SetColumnDefinitions(NativeControl, thisAsIGrid.ColumnDefinitions);
NativeControl.SetValue(_mauiReactorGridColumnsKey.BindableProperty, thisAsIGrid.ColumnDefinitions);
NativeControl.SetValue(_mauiReactorGridColumns, thisAsIGrid.ColumnDefinitions);
}

base.OnUpdate();
Expand Down
117 changes: 117 additions & 0 deletions src/MauiReactor/HotReload/HotReloadTypeLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using MauiReactor.Internals;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Metadata;
using System.Text;

[assembly: MetadataUpdateHandler(typeof(MauiReactor.HotReload.HotReloadTypeLoader))]

namespace MauiReactor.HotReload;

internal interface ITypeLoaderEventConsumer
{
void OnAssemblyChanged();
}

internal class HotReloadTypeLoader
{
private readonly HotReloadServer _server;

private Assembly? _assembly;

private bool _running;

private static HotReloadTypeLoader? _instance;

public HotReloadTypeLoader()
{
_server = new HotReloadServer(ReceivedAssemblyFromHost);
}

public static HotReloadTypeLoader Instance
{
get => MauiReactorFeatures.HotReloadIsEnabled ? (_instance ??= new()) : throw new InvalidOperationException();
}

public Assembly? LastLoadedAssembly => _assembly;

public Action? OnHotReloadCompleted { get; set; }

public WeakProducer<ITypeLoaderEventConsumer>? AssemblyChangedEvent { get; } = new();


public T LoadObject<T>(Type type)
{
if (_assembly == null)
{
return (T)(Activator.CreateInstance(type) ?? throw new InvalidOperationException());
}

var objectTypeInAssembly = _assembly.GetType(type.FullName ?? throw new InvalidOperationException()) ?? throw new InvalidOperationException();

try
{
return (T)(Activator.CreateInstance(objectTypeInAssembly) ?? throw new InvalidOperationException());
}
catch (Exception ex)
{
Debug.WriteLine($"[MauiReactor] Unable to hot reload component {objectTypeInAssembly.FullName}:{Environment.NewLine}{ex}");
throw new InvalidOperationException($"Unable to hot reload component {objectTypeInAssembly.FullName}", ex);
}
}

private void ReceivedAssemblyFromHost(Assembly? newAssembly)
{
_assembly = newAssembly;
AssemblyChangedEvent?.Raise(consumer => consumer.OnAssemblyChanged());
OnHotReloadCompleted?.Invoke();
}

public void Run()
{
if (_running)
{
return;
}

_running = true;
DeviceDisplay.Current.MainDisplayInfoChanged += OnMainDisplayInfoChanged;
_server.Run();
}

static float? _lastRefreshRate;

private void OnMainDisplayInfoChanged(object? sender, DisplayInfoChangedEventArgs e)
{
if (_lastRefreshRate == null ||
_lastRefreshRate != e.DisplayInfo.RefreshRate)
{
_lastRefreshRate = e.DisplayInfo.RefreshRate;
Debug.WriteLine($"[MauiReactor] FPS: {_lastRefreshRate}");
}
}

public void Stop()
{
DeviceDisplay.Current.MainDisplayInfoChanged -= OnMainDisplayInfoChanged;
_server.Stop();
_running = false;
}

#pragma warning disable IDE0051 // Remove unused private members
static void UpdateApplication(Type[]? _)
#pragma warning restore IDE0051 // Remove unused private members
{
if (Instance == null)
{
Debug.WriteLine($"[MauiReactor] Hot-Reload is not enabled, please call EnableMauiReactorHotReload() on your AppBuilder");
return;
}

//Debug.WriteLine($"[MauiReactor] Hot-Reload triggered");

Instance.ReceivedAssemblyFromHost(null);
}
}
39 changes: 0 additions & 39 deletions src/MauiReactor/HotReload/ITypeLoader.cs

This file was deleted.

40 changes: 20 additions & 20 deletions src/MauiReactor/HotReload/LocalTypeLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@
using System.Reflection;
using System.Text;

namespace MauiReactor.HotReload
{
internal class LocalTypeLoader : ITypeLoader
{
#pragma warning disable CS0067
public WeakProducer<ITypeLoaderEventConsumer>? AssemblyChangedEvent { get; }
public Assembly? LastLoadedAssembly { get; }
#pragma warning restore CS0067
//namespace MauiReactor.HotReload
//{
// internal class LocalTypeLoader : ITypeLoader
// {
//#pragma warning disable CS0067
// public WeakProducer<ITypeLoaderEventConsumer>? AssemblyChangedEvent { get; }
// public Assembly? LastLoadedAssembly { get; }
//#pragma warning restore CS0067

public T LoadObject<T>(Type type)
{
return (T)(Activator.CreateInstance(type) ?? throw new InvalidOperationException());
}
// public T LoadObject<T>(Type type)
// {
// return (T)(Activator.CreateInstance(type) ?? throw new InvalidOperationException());
// }

public void Run()
{
// public void Run()
// {

}
// }

public void Stop()
{
// public void Stop()
// {

}
}
}
// }
// }
//}
Loading

0 comments on commit 1dc9282

Please sign in to comment.