Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Blazor support to Consolonia. #164

Merged
merged 26 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/editorconfig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.0.x
dotnet-version: 8.0.x

- name: Restore
run: dotnet restore
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/general_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.0.x
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -766,3 +766,4 @@ fabric.properties
.idea/
*.sln.iml

/src/Tools/Consolonia.PreviewHost/Properties/launchSettings.json
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
ο»Ώ<Project>
<PropertyGroup>
<!--todo: add mono-->
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>disable</Nullable>
</PropertyGroup>
<PropertyGroup>
<VersionPrefix>11.0.9</VersionPrefix>
<VersionPrefix>11.2.1</VersionPrefix>
<Authors>https://github.com/jinek/Consolonia/graphs/contributors</Authors>
<Description>Text User Interface implementation of Avalonia UI (GUI Framework)</Description>
<Copyright>Copyright Β© Evgeny Gorbovoy 2021 - 2022</Copyright>
Expand Down
54 changes: 54 additions & 0 deletions src/Consolonia.Blazor/BuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using BlazorBindingsAvalonia;
using Consolonia.Core;
using Microsoft.Extensions.DependencyInjection;

namespace Consolonia.Blazor
{
public static class BuilderExtensions
{
/// <summary>
/// Use Consolonize in Blazor mode
/// </summary>
/// <param name="builder"></param>
/// <param name="configureServices"></param>
/// <returns></returns>
public static AppBuilder UseConsoloniaBlazor(this AppBuilder builder,
Action<IServiceCollection> configureServices = null)
{
return builder
.UseConsolonia()
.UseAvaloniaBlazorBindings(sc =>
{
// Register services for injectoin
sc.AddSingleton(_ =>
{
var lifetime =
(IClassicDesktopStyleApplicationLifetime)Application.Current?.ApplicationLifetime;
ArgumentNullException.ThrowIfNull(lifetime);
return lifetime;
});
tomlm marked this conversation as resolved.
Show resolved Hide resolved
sc.AddTransient(sp =>
sp.GetRequiredService<IClassicDesktopStyleApplicationLifetime>().MainWindow?.StorageProvider);
sc.AddTransient(sp =>
sp.GetRequiredService<IClassicDesktopStyleApplicationLifetime>().MainWindow?.Clipboard);
sc.AddTransient(sp =>
sp.GetRequiredService<IClassicDesktopStyleApplicationLifetime>().MainWindow?.InsetsManager);
sc.AddTransient(sp =>
sp.GetRequiredService<IClassicDesktopStyleApplicationLifetime>().MainWindow?.InputPane);
sc.AddTransient(sp =>
sp.GetRequiredService<IClassicDesktopStyleApplicationLifetime>().MainWindow?.Launcher);
sc.AddTransient(sp =>
sp.GetRequiredService<IClassicDesktopStyleApplicationLifetime>().MainWindow?.Screens);
sc.AddTransient(sp =>
sp.GetRequiredService<IClassicDesktopStyleApplicationLifetime>().MainWindow?.FocusManager);
sc.AddTransient(sp =>
sp.GetRequiredService<IClassicDesktopStyleApplicationLifetime>().MainWindow?.PlatformSettings);

if (configureServices != null) configureServices(sc);
});
}
}
}
14 changes: 14 additions & 0 deletions src/Consolonia.Blazor/Consolonia.Blazor.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Core.Build.props', '$(MSBuildThisFileDirectory)../'))" />

<ItemGroup>
<ProjectReference Include="..\Consolonia.Core\Consolonia.Core.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Avalonia" Version="$(AvaloniaVersion)" />
<PackageReference Include="Avalonia.Desktop" Version="$(AvaloniaVersion)" />
<PackageReference Include="BlazorBindingsAvalonia" Version="0.1.2" />
</ItemGroup>
</Project>
32 changes: 32 additions & 0 deletions src/Consolonia.Blazor/ConsoloniaBlazorApplication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Avalonia;
using Avalonia.Input;
using BlazorBindingsAvalonia;
using Consolonia.Core.Helpers;
using Consolonia.Core.Infrastructure;
using Microsoft.AspNetCore.Components;

namespace Consolonia.Blazor
{
/// <summary>
/// Use this application as your app base class to use Consolonia.Blazor Blazor engine.
/// </summary>
/// <typeparam name="TComponent">The root window for the application.</typeparam>
public class ConsoloniaBlazorApplication<TComponent> : BlazorBindingsApplication<TComponent>
where TComponent : IComponent
{
public override void RegisterServices()
{
base.RegisterServices();

AvaloniaLocator.CurrentMutable.Bind<IKeyboardNavigationHandler>()
.ToTransient<ArrowsAndKeyboardNavigationHandler>();
}

public override void OnFrameworkInitializationCompleted()
{
base.OnFrameworkInitializationCompleted();

this.AddConsoloniaDesignMode();
}
}
}
44 changes: 44 additions & 0 deletions src/Consolonia.Core/Helpers/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
using System.Collections.Generic;
using System.Text;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Media;
using Avalonia.Reactive;
using Avalonia.Styling;
using Consolonia.Core.Drawing.PixelBufferImplementation;
using Consolonia.Core.Infrastructure;
using NeoSmart.Unicode;
Expand All @@ -19,6 +23,46 @@ public static void SubscribeAction<TValue>(
observable.Subscribe(new AnonymousObserver<AvaloniaPropertyChangedEventArgs<TValue>>(action));
}

public static void AddConsoloniaDesignMode(this Application application)
{
if (Design.IsDesignMode)
{
// For previewing in Visual Studio designer without Design.PreviewWith tag we need to set default font and colors
// get anything to render. This is not perfect, but nicer than getting a big error screen.
IBrush foregroundBrush = Brushes.White;
if (application.Styles.TryGetResource("ThemeForegroundBrush", null, out object brush))
foregroundBrush = (IBrush)brush;
tomlm marked this conversation as resolved.
Show resolved Hide resolved

IBrush backgroundBrush = Brushes.Black;
if (application.Styles.TryGetResource("ThemeBackgroundBrush", null, out brush))
backgroundBrush = (IBrush)brush;

application.Styles.Add(new Style(x => x.Is<TemplatedControl>())
{
Setters =
{
new Setter(TemplatedControl.FontSizeProperty, 16.0),
new Setter(TemplatedControl.FontFamilyProperty, new FontFamily("Cascadia Mono")),
new Setter(TemplatedControl.ForegroundProperty, foregroundBrush),
new Setter(TemplatedControl.BackgroundProperty, backgroundBrush)
}
});

// EXPERIMENTAL
// If you do RenderTRansform="scale(10.0,10.0) you can actually sort of see the UI get bigger
// but this doesn' seem to work when using these style setters. <sigh>
//this.Styles.Add(new Style(x => x.Is<Visual>())
//{
// Setters =
// {
// //new Setter(Visual.RenderTransformOriginProperty, RelativePoint.TopLeft),
// //new Setter(Visual.RenderTransformProperty, new ScaleTransform(2.0, 2.0)),
// //new Setter(Visual.RenderTransformProperty, new ScaleTransform(2.0, 2.0)),
// }
//});
}
}

public static void Print(this IConsole console, PixelBufferCoordinate point, Pixel pixel)
{
console.Print(point,
Expand Down
42 changes: 2 additions & 40 deletions src/Consolonia.Core/Infrastructure/ConsoloniaApplication.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Media;
using Avalonia.Styling;
using Consolonia.Core.Helpers;

namespace Consolonia.Core.Infrastructure
{
Expand All @@ -21,42 +18,7 @@ public override void OnFrameworkInitializationCompleted()
{
base.OnFrameworkInitializationCompleted();

if (Design.IsDesignMode)
{
// For previewing in Visual Studio designer without Design.PreviewWith tag we need to set default font and colors
// get anything to render. This is not perfect, but nicer than getting a big error screen.
IBrush foregroundBrush = Brushes.White;
if (Styles.TryGetResource("ThemeForegroundBrush", null, out object brush))
foregroundBrush = (IBrush)brush;

IBrush backgroundBrush = Brushes.Black;
if (Styles.TryGetResource("ThemeBackgroundBrush", null, out brush))
backgroundBrush = (IBrush)brush;

Styles.Add(new Style(x => x.Is<TemplatedControl>())
{
Setters =
{
new Setter(TemplatedControl.FontSizeProperty, 16.0),
new Setter(TemplatedControl.FontFamilyProperty, new FontFamily("Cascadia Mono")),
new Setter(TemplatedControl.ForegroundProperty, foregroundBrush),
new Setter(TemplatedControl.BackgroundProperty, backgroundBrush)
}
});

// EXPERIMENTAL
// If you do RenderTRansform="scale(10.0,10.0) you can actually sort of see the UI get bigger
// but this doesn' seem to work when using these style setters. <sigh>
//this.Styles.Add(new Style(x => x.Is<Visual>())
//{
// Setters =
// {
// //new Setter(Visual.RenderTransformOriginProperty, RelativePoint.TopLeft),
// //new Setter(Visual.RenderTransformProperty, new ScaleTransform(2.0, 2.0)),
// //new Setter(Visual.RenderTransformProperty, new ScaleTransform(2.0, 2.0)),
// }
//});
}
this.AddConsoloniaDesignMode();
}
}
}
6 changes: 0 additions & 6 deletions src/Consolonia.Core/Infrastructure/ConsoloniaException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Serialization;

namespace Consolonia.Core.Infrastructure
{
Expand All @@ -12,10 +10,6 @@ public ConsoloniaException()
{
}

protected ConsoloniaException([NotNull] SerializationInfo info, StreamingContext context) : base(info, context)
{
}

public ConsoloniaException(string message) : base(message)
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
using System;
using System.Runtime.Serialization;

namespace Consolonia.Core.Infrastructure
{
[Serializable]
public class ConsoloniaNotSupportedException : Exception
{
internal ConsoloniaNotSupportedException(NotSupportedRequest request)
{
Request = request;
}

protected ConsoloniaNotSupportedException(
tomlm marked this conversation as resolved.
Show resolved Hide resolved
SerializationInfo info,
StreamingContext context) : base(info, context)
{
}

public NotSupportedRequest Request { get; }

Check warning on line 12 in src/Consolonia.Core/Infrastructure/ConsoloniaNotSupportedException.cs

View workflow job for this annotation

GitHub Actions / build

"[UnusedAutoPropertyAccessor.Global] Auto-property accessor 'Request.get' is never used" on /home/runner/work/Consolonia/Consolonia/src/Consolonia.Core/Infrastructure/ConsoloniaNotSupportedException.cs(12,46)
}
}
11 changes: 0 additions & 11 deletions src/Consolonia.Designer/ConsolePreview.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

using Avalonia;
using Avalonia.Controls;
#if DEBUG
using Consolonia.PreviewHost;
using System;
using System.Diagnostics;
Expand All @@ -15,7 +14,6 @@
using Avalonia.Threading;
using Consolonia.Core.Drawing.PixelBufferImplementation;
using Newtonsoft.Json;
#endif

namespace Consolonia.Designer
{
Expand Down Expand Up @@ -45,7 +43,6 @@ public class ConsolePreview : UserControl

public ConsolePreview()
{
#if DEBUG
_process = null;
FontFamily = FontFamily.Parse("Cascadia Mono");
Initialized += (_, _) => LoadXaml();
Expand All @@ -54,7 +51,6 @@ public ConsolePreview()
{
if (e.Property == FileNameProperty) LoadXaml();
};
#endif
}


Expand Down Expand Up @@ -102,7 +98,6 @@ protected void Dispose(bool disposing)
if (disposing)
{
// TODO: dispose managed state (managed objects)
#if DEBUG
#pragma warning disable CA1416 // Validate platform compatibility
if (_process != null)
{
Expand All @@ -111,7 +106,6 @@ protected void Dispose(bool disposing)
_process = null;
}
#pragma warning restore CA1416 // Validate platform compatibility
#endif
}

_disposedValue = true;
Expand All @@ -125,14 +119,10 @@ public void Dispose()
Dispose(true);
}

#if DEBUG
private Process? _process;
private readonly Typeface _typeface = new("Cascadia Mono");
private double _charWidth;
private double _charHeight;
#endif

#if DEBUG

private void LoadXaml()
{
Expand Down Expand Up @@ -451,6 +441,5 @@ public void Flush()
_textRunCharWidth = 0;
}
}
#endif
}
}
6 changes: 3 additions & 3 deletions src/Consolonia.Designer/Consolonia.Designer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
<ItemGroup>
<PackageReference Include="Avalonia" Version="$(AvaloniaVersion)" />
<PackageReference Include="Avalonia.FreeDesktop" Version="$(AvaloniaVersion)" />
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Desktop" Version="$(AvaloniaVersion)" />
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Fonts.Inter" Version="$(AvaloniaVersion)" />
<PackageReference Include="Avalonia.Desktop" Version="$(AvaloniaVersion)" />
<PackageReference Include="Avalonia.Fonts.Inter" Version="$(AvaloniaVersion)" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Consolonia.Core\Consolonia.Core.csproj" />
<ProjectReference Condition="'$(Configuration)' == 'Debug'" Include="..\Tools\Consolonia.PreviewHost\Consolonia.PreviewHost.csproj" />
<ProjectReference Include="..\Tools\Consolonia.PreviewHost\Consolonia.PreviewHost.csproj" />
</ItemGroup>

</Project>
Loading
Loading