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 10 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
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</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
47 changes: 47 additions & 0 deletions src/Consolonia.Blazor/BuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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;

Check warning on line 27 in src/Consolonia.Blazor/BuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / build

"[PossibleNullReferenceException] Possible 'System.NullReferenceException'" on /home/runner/work/Consolonia/Consolonia/src/Consolonia.Blazor/BuilderExtensions.cs(27,81)
ArgumentNullException.ThrowIfNull(lifetime);
return lifetime;
});
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);
}
});
}
}
}
15 changes: 15 additions & 0 deletions src/Consolonia.Blazor/Consolonia.Blazor.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<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" />
<PackageReference Include="Consolonia.Core" Version="11.2" />
tomlm marked this conversation as resolved.
Show resolved Hide resolved
</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.Input;
using Avalonia;
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();
}
}
}
45 changes: 45 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.Primitives;
using Avalonia.Controls;
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,47 @@ 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,5 +1,4 @@
using System;
using System.Runtime.Serialization;

namespace Consolonia.Core.Infrastructure
{
Expand All @@ -11,12 +10,6 @@ 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; }
}
}
2 changes: 2 additions & 0 deletions src/Consolonia.Designer/Consolonia.Designer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Desktop" Version="$(AvaloniaVersion)" />
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Fonts.Inter" Version="$(AvaloniaVersion)" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Consolonia.Core" Version="11.2" />
tomlm marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Consolonia.PreviewHost" Version="11.2" />
tomlm marked this conversation as resolved.
Show resolved Hide resolved
tomlm marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 3 additions & 2 deletions src/Consolonia.GuiCS/binding.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma warning disable CS8981 // The type name only contains lower-cased ascii characters. Such names may become reserved for the language.
//
// TODO:
// * FindNCurses needs to remove the old probing code
Expand Down Expand Up @@ -328,8 +329,8 @@ static public int IsAlt (int key)
internal class Delegates {
#pragma warning restore RCS1102 // Make class static.
public delegate IntPtr initscr ();
public delegate int endwin ();
public delegate bool isendwin ();
public delegate int endwin ();
public delegate bool isendwin ();
public delegate int cbreak ();
public delegate int nocbreak ();
public delegate int echo ();
Expand Down
1 change: 1 addition & 0 deletions src/Consolonia.NUnit/Consolonia.NUnit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Consolonia.Core" Version="11.2" />
tomlm marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="NUnit" Version="3.13.1" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Core.Build.props', '$(MSBuildThisFileDirectory)../'))" />


<ItemGroup>
<PackageReference Include="Consolonia.Core" Version="11.2" />
<PackageReference Include="Consolonia.GuiCS" Version="11.2" />
tomlm marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>
tomlm marked this conversation as resolved.
Show resolved Hide resolved

<ItemGroup>
<ProjectReference Include="..\Consolonia.Core\Consolonia.Core.csproj" />
<ProjectReference Include="..\Consolonia.GuiCS\Consolonia.GuiCS.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Core.Build.props', '$(MSBuildThisFileDirectory)../'))" />

<ItemGroup>
<PackageReference Include="Avalonia" Version="$(AvaloniaVersion)" />
<PackageReference Include="Avalonia" Version="$(AvaloniaVersion)" />
<PackageReference Include="Consolonia.Core" Version="11.2" />
tomlm marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>

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

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ namespace Consolonia.Themes.TurboVision.Themes.Material
[SuppressMessage("ReSharper", "UnusedMember.Global")]
public class MaterialTheme : ResourceIncludeBase
{
public MaterialTheme() : base(new Uri("avares://Consolonia.Themes.TurboVision/Themes/Material/Material.axaml"))
{
}

public MaterialTheme(Uri baseUri) : base(baseUri)
{
}
Expand Down
20 changes: 19 additions & 1 deletion src/Consolonia.sln
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tools", "Tools", "{910B2845
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Avalonia.PreviewHost", "Tools\Avalonia.PreviewHost\Avalonia.PreviewHost.csproj", "{6D1DDDE9-34BE-47B8-88F2-F878AC2CC910}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Consolonia.Designer", "Consolonia.Designer\Consolonia.Designer.csproj", "{7F0EA682-C28C-4C1C-9D40-EC0561179505}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Consolonia.Designer", "Consolonia.Designer\Consolonia.Designer.csproj", "{7F0EA682-C28C-4C1C-9D40-EC0561179505}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Consolonia.Blazor", "Consolonia.Blazor\Consolonia.Blazor.csproj", "{66726106-2ED4-4516-A912-1C0FE0ACDCBD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.Blazor", "Example.Blazor\Example.Blazor.csproj", "{000ED32A-027D-4005-BDE0-4DBF1ABC0829}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.Blazor.Components", "Example.Blazor.Components\Example.Blazor.Components.csproj", "{F60897B7-2B53-4E2F-B7AE-C3706B928214}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -115,6 +121,18 @@ Global
{7F0EA682-C28C-4C1C-9D40-EC0561179505}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7F0EA682-C28C-4C1C-9D40-EC0561179505}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7F0EA682-C28C-4C1C-9D40-EC0561179505}.Release|Any CPU.Build.0 = Release|Any CPU
{66726106-2ED4-4516-A912-1C0FE0ACDCBD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{66726106-2ED4-4516-A912-1C0FE0ACDCBD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{66726106-2ED4-4516-A912-1C0FE0ACDCBD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{66726106-2ED4-4516-A912-1C0FE0ACDCBD}.Release|Any CPU.Build.0 = Release|Any CPU
{000ED32A-027D-4005-BDE0-4DBF1ABC0829}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{000ED32A-027D-4005-BDE0-4DBF1ABC0829}.Debug|Any CPU.Build.0 = Debug|Any CPU
{000ED32A-027D-4005-BDE0-4DBF1ABC0829}.Release|Any CPU.ActiveCfg = Release|Any CPU
{000ED32A-027D-4005-BDE0-4DBF1ABC0829}.Release|Any CPU.Build.0 = Release|Any CPU
{F60897B7-2B53-4E2F-B7AE-C3706B928214}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F60897B7-2B53-4E2F-B7AE-C3706B928214}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F60897B7-2B53-4E2F-B7AE-C3706B928214}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F60897B7-2B53-4E2F-B7AE-C3706B928214}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Loading
Loading