Skip to content

Commit

Permalink
move the design mode styles into ConsoloniaApplication.
Browse files Browse the repository at this point in the history
cleanup background look and feel
  • Loading branch information
tomlm committed Nov 17, 2024
1 parent 3b2708a commit 9741315
Show file tree
Hide file tree
Showing 18 changed files with 172 additions and 100 deletions.
47 changes: 47 additions & 0 deletions src/Consolonia.Core/Infrastructure/ConsoloniaApplication.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
using Avalonia;
using Avalonia.Controls.Primitives;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Media;
using Avalonia.Styling;
using Consolonia.Core.Drawing;

namespace Consolonia.Core.Infrastructure
{
Expand All @@ -12,5 +17,47 @@ public override void RegisterServices()
AvaloniaLocator.CurrentMutable.Bind<IKeyboardNavigationHandler>()
.ToConstant(new ArrowsAndKeyboardNavigationHandler(keyboardNavigationHandler));
}

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.
if (!this.Styles.TryGetResource("ThemeForegroundBrush", null, out var foregroundBrush))
foregroundBrush = (ConsoleBrush)Brushes.White;

if (!this.Styles.TryGetResource("ThemeBackgroundBrush", null, out var backgroundBrush))
backgroundBrush = (ConsoleBrush)Brushes.Black;

this.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, new SolidColorBrush(((ConsoleBrush)foregroundBrush).Color)),
new Setter(TemplatedControl.BackgroundProperty, new SolidColorBrush(((ConsoleBrush)backgroundBrush).Color)),
}
});

// 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)),
// }
//});
}


}
}
}
25 changes: 20 additions & 5 deletions src/Consolonia.Designer/ConsolePreview.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
#nullable enable
using Avalonia;
using Avalonia.Controls;
#if DEBUG
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Layout;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
using Avalonia.Threading;
using Consolonia.Core.Drawing.PixelBufferImplementation;
using Newtonsoft.Json;
#endif

namespace Consolonia.Designer
{

/// <summary>
/// ConsolePreview is a control that can be used to preview the output of a console AXAML file in visual studio.
/// ConsolePreview is a control that can be used to preview the output of a console AXAML file in visual studio.
/// </summary>
/// <remarks>
/// This depends on Consolonia.PreviewHost application to render the view. It
/// launches it as a child process with a --buffer width height arguments to tell it
/// to output the PixelBuffer as json. The control then reads the json and renders it as a full pixel avalonia control.
/// </remarks>
public class ConsolePreview : UserControl
{
#if DEBUG
private object _lock = new object();
private Process? _process;
private Typeface _typeface = new Typeface("Cascadia Mono");
private double _charWidth;
private double _charHeight;
#endif
private bool _disposedValue;

public static readonly StyledProperty<string> FileNameProperty =
Expand All @@ -42,8 +52,8 @@ public class ConsolePreview : UserControl

public ConsolePreview()
{
#if DEBUG
this.FontFamily = FontFamily.Parse("Cascadia Mono");

Initialized += (sender, e) => LoadXaml();

this.PropertyChanged += (sender, e) =>
Expand All @@ -53,6 +63,7 @@ public ConsolePreview()
LoadXaml();
}
};
#endif
}


Expand All @@ -76,6 +87,8 @@ public ConsolePreview()
/// </summary>
public bool MonitorChanges { get => GetValue(MonitorChangesProperty); set => SetValue(MonitorChangesProperty, value); }

#if DEBUG

private void LoadXaml()
{
lock (_lock)
Expand Down Expand Up @@ -380,14 +393,15 @@ public void Flush(bool forceWidth = true)
_textRunCharWidth = 0;
}
}

#endif
protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
// TODO: dispose managed state (managed objects)
#if DEBUG
#pragma warning disable CA1416 // Validate platform compatibility
lock (_lock)
{
Expand All @@ -399,6 +413,7 @@ protected virtual void Dispose(bool disposing)
}
}
#pragma warning restore CA1416 // Validate platform compatibility
#endif
}

_disposedValue = true;
Expand Down
7 changes: 3 additions & 4 deletions src/Consolonia.Designer/Consolonia.Designer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@

<ItemGroup>
<PackageReference Include="Avalonia" Version="$(AvaloniaVersion)" />
<PackageReference Include="Avalonia.Desktop" Version="$(AvaloniaVersion)" />
<PackageReference Include="Avalonia.FreeDesktop" Version="$(AvaloniaVersion)" />
<PackageReference Include="Avalonia.Fonts.Inter" Version="$(AvaloniaVersion)" />
<PackageReference Include="Avalonia.Skia" Version="$(AvaloniaVersion)" />
<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" />
</ItemGroup>

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

</Project>
11 changes: 11 additions & 0 deletions src/Consolonia.Designer/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ namespace Consolonia.Designer
{
public static class Extensions
{
/// <summary>
/// This method is used to initialize consolonia
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
/// <remarks>
/// In Design mode it will load up avalonia instead of consolonia so that avalonia
/// previewer will attempt to render it. This mostly only works for text layout.
/// In release mode it will load consolonia, removing the dependency on the desktop avalonia subsystem.
/// NOTE: The package references for
/// </remarks>
public static AppBuilder UseConsoloniaDesigner(this AppBuilder builder)
{
#if DEBUG
Expand Down
34 changes: 1 addition & 33 deletions src/Consolonia.Gallery/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Avalonia.Styling;
using Consolonia.Core.Infrastructure;
using Consolonia.Gallery.View;
using Consolonia.Themes.TurboVision.Templates;
using Consolonia.Themes.TurboVision.Themes.TurboVisionBlack;

namespace Consolonia.Gallery
Expand All @@ -20,40 +19,9 @@ static App()

public App()
{
//Styles.Add(new TurboVisionTheme(new Uri("avares://Consolonia.Themes.TurboVision/Themes/TurboVisionDark/TurboVisionDark.axaml")));
// Styles.Add(new TurboVisionTheme(new Uri("avares://Consolonia.Themes.TurboVision/Themes/TurboVisionDark/TurboVisionDark.axaml")));
// Styles.Add(new MaterialTheme(new Uri("avares://Consolonia.Themes.TurboVision/Themes/Material/Material.axaml")));
Styles.Add(new MaterialTheme(new Uri("avares://Consolonia.Themes.TurboVision/Themes/Fluent/Fluent.axaml")));

//if (Design.IsDesignMode)
//{
// // For previewing in Visual Studio designer without Design.PreviewWith tag we need to set default font and colors
// // get antyhing to render
// turboVisionTheme.TryGetResource("ThemeForegroundBrush", null, out var foregroundBrush);
// turboVisionTheme.TryGetResource("ThemeBackgroundBrush", null, out var backgroundBrush);
// this.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, new SolidColorBrush(((ConsoleBrush)foregroundBrush).Color)),
// new Setter(TemplatedControl.BackgroundProperty, new SolidColorBrush(((ConsoleBrush)backgroundBrush).Color)),
// }
// });
// // 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)),
// // }
// //});
//}

//Styles.Add(turboVisionTheme);
}

public override void OnFrameworkInitializationCompleted()
Expand Down
2 changes: 1 addition & 1 deletion src/Consolonia.Gallery/Consolonia.Gallery.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<ProjectReference Include="..\Consolonia.Core\Consolonia.Core.csproj" />
<ProjectReference Include="..\Consolonia.PlatformSupport\Consolonia.PlatformSupport.csproj" />
<ProjectReference Include="..\Consolonia.Themes.TurboVision\Consolonia.Themes.TurboVision.csproj" />
<ProjectReference Condition="'$(Configuration)' == 'Debug'" Include="..\Consolonia.Designer\Consolonia.Designer.csproj" />
<ProjectReference Include="..\Consolonia.Designer\Consolonia.Designer.csproj" />
<ProjectReference Condition="'$(Configuration)' == 'Debug'" Include="..\Tools\Consolonia.PreviewHost\Consolonia.PreviewHost.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:drawing="clr-namespace:Consolonia.Core.Drawing;assembly=Consolonia.Core"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="100" d:DesignHeight="100"
mc:Ignorable="d" d:DesignWidth="80" d:DesignHeight="50"
x:Class="Consolonia.Gallery.Gallery.GalleryViews.GalleryGradientBrush">

<Grid x:Name="MyGrid" RowDefinitions="Auto *">
Expand All @@ -18,5 +18,7 @@
<Button Click="Radial_Click" Background="Gray">Radial</Button>
<Button Click="Conic_Click" Background="Gray">Conic</Button>
</StackPanel>

<StackPanel Grid.Row="1"/>
</Grid>
</UserControl>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:galleryViews="clr-namespace:Consolonia.Gallery.Gallery.GalleryViews"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Consolonia.Gallery.Gallery.GalleryViews.GalleryScrollViewer">
<Grid RowDefinitions="Auto, *">
<StackPanel Orientation="Vertical"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:drawing="clr-namespace:Consolonia.Core.Drawing;assembly=Consolonia.Core"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="600"
x:Class="Consolonia.Gallery.Gallery.GalleryViews.GalleryTextBlock">
<UserControl.Styles>
<Style Selector="StackPanel">
Expand Down
10 changes: 2 additions & 8 deletions src/Consolonia.Gallery/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,13 @@ private static void Main(string[] args)
}

public static AppBuilder BuildAvaloniaApp()
{
=> AppBuilder.Configure<App>()
#if DEBUG
return AppBuilder.Configure<App>()
.UseConsoloniaDesigner()
.UseAutoDetectedConsole()
.LogToException();
#else
return AppBuilder.Configure<App>()
.UseConsolonia()
#endif
.UseAutoDetectedConsole()
.LogToException();
#endif
}

}
}
18 changes: 10 additions & 8 deletions src/Tools/Avalonia.PreviewHost/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Icon="/Assets/avalonia-logo.ico"
Title="PreviewHost">

<Grid RowDefinitions="Auto *">
<Grid RowDefinitions="Auto *" Background="#ff09161e">
<Menu Background="LightGray" >
<MenuItem Header="_File">
<MenuItem Header="_Open..." Click="OnOpen" />
Expand All @@ -33,14 +33,16 @@

<GridSplitter Grid.Column="1"
Width="5" ShowsPreview="True"
HorizontalAlignment="Stretch" Background="#FF101040"/>
HorizontalAlignment="Stretch" Background="Black"/>

<v:ConsolePreview x:Name="PreviewPane"
Grid.Column="2"
DataContext="{Binding SelectedItem, ElementName=FilesListBox}"
FileName="{Binding FullName}"
Columns="80" Rows="80" MonitorChanges="true"
HorizontalAlignment="Center" VerticalAlignment="Center" />
<StackPanel Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Center">
<v:ConsolePreview x:Name="PreviewPane" Background="#FF101020"
Grid.Column="2"
DataContext="{Binding SelectedItem, ElementName=FilesListBox}"
FileName="{Binding FullName}"
Columns="80" Rows="40" MonitorChanges="true"
HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>

</Grid>
</Grid>
Expand Down
27 changes: 16 additions & 11 deletions src/Tools/Avalonia.PreviewHost/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using Avalonia.Controls;
using Avalonia.Platform.Storage;
using Avalonia.Threading;
using Consolonia.Designer;
using PreviewHost.ViewModels;

Expand Down Expand Up @@ -38,19 +39,23 @@ private async void OnOpen(object? sender, Avalonia.Interactivity.RoutedEventArgs
{
return;
}
Model.Files.Clear();
var projectFile = files[0].Path.AbsolutePath.Replace('/','\\');
this.Title = projectFile;

var folderRoot = Path.GetDirectoryName(projectFile)!;
foreach (var file in Directory.EnumerateFiles(folderRoot, "*.axaml", SearchOption.AllDirectories))

Dispatcher.UIThread.Invoke(() =>
{
Model.Files.Add(new FileViewModel()
Model.Files.Clear();
var projectFile = files[0].Path.AbsolutePath.Replace('/', '\\');
this.Title = projectFile;

var folderRoot = Path.GetDirectoryName(projectFile)!;
foreach (var file in Directory.EnumerateFiles(folderRoot, "*.axaml", SearchOption.AllDirectories))
{
Name = Path.GetFileName(file),
FullName = file
});
}
Model.Files.Add(new FileViewModel()
{
Name = Path.GetFileName(file),
FullName = file
});
}
});
}

public MainViewModel Model => (MainViewModel)DataContext!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackAsTool>true</PackAsTool>
<ToolCommandName>Consolonia.PreviewHost</ToolCommandName>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 9741315

Please sign in to comment.