Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 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
7 changes: 7 additions & 0 deletions .github/workflows/build-test-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ jobs:
run: dotnet build ${{ env.SOLUTION_NAME }} --configuration Release /p:TreatWarningsAsErrors=true
- name: Test
run: dotnet test ${{ env.SOLUTION_NAME }} --no-build --configuration Release
- name: Upload Test Results
if: failure()
uses: actions/upload-artifact@v4
with:
name: verify-test-results
path: |
**/*.received.*

- name: Check formatting using csharpier
run: |
Expand Down
3 changes: 3 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<PackageVersion Include="AsyncAwaitBestPractices" Version="9.0.0" />
<PackageVersion Include="Avalonia" Version="11.3.0" />
<PackageVersion Include="Avalonia.Desktop" Version="11.3.0" />
<PackageVersion Include="Avalonia.Diagnostics" Version="11.3.0" />
<PackageVersion Include="Avalonia.Headless.XUnit" Version="11.3.0" />
<PackageVersion Include="Avalonia.Themes.Fluent" Version="11.3.0" />
<PackageVersion Include="AvaloniaUI.DiagnosticsSupport" Version="2.0.3" />
Expand All @@ -26,8 +27,10 @@
<PackageVersion Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.756" />
<PackageVersion Include="NSubstitute" Version="5.3.0" />
<PackageVersion Include="NSubstitute.Analyzers.CSharp" Version="1.0.17" />
<PackageVersion Include="Shouldly" Version="4.3.0" />
<PackageVersion Include="Verify.Avalonia" Version="1.3.0" />
<PackageVersion Include="Verify.CommunityToolkit.Mvvm" Version="1.0.0" />
<PackageVersion Include="Verify.ImageMagick" Version="3.7.0" />
<PackageVersion Include="Verify.Xunit" Version="30.1.0" />
<PackageVersion Include="xunit" Version="2.9.3" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.0" />
Expand Down
13 changes: 5 additions & 8 deletions src/AvaloniaExampleProject/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System.Data;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Data.Core.Plugins;
using Avalonia.Input;
using Avalonia.Markup.Xaml;
using AvaloniaExampleProject.Views;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -30,10 +29,6 @@ public App(IServiceProvider provider)
[MemberNotNullWhen(true, nameof(IsDesignMode))]
public static IServiceProvider? ServiceProvider { get; private set; }

public static string Version { get; } =
typeof(App).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion
?? throw new VersionNotFoundException("Could not get version");

public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
Expand All @@ -45,8 +40,10 @@ public override void OnFrameworkInitializationCompleted()
{
#if DEBUG
this.AttachDeveloperTools(options =>
options.AddMicrosoftLoggerObservable(_provider.GetRequiredService<ILoggerFactory>())
);
{
options.Gesture = new KeyGesture(Key.F11);
options.AddMicrosoftLoggerObservable(_provider.GetRequiredService<ILoggerFactory>());
});
#endif
DisableAvaloniaDataAnnotationValidation();
desktop.MainWindow = new MainWindow(_provider);
Expand Down
5 changes: 5 additions & 0 deletions src/AvaloniaExampleProject/AvaloniaExampleProject.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<PackageReference Include="Avalonia.Desktop" />
<PackageReference Include="Avalonia.Fonts.Inter" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Include="AvaloniaUI.DiagnosticsSupport">
<IncludeAssets Condition="'$(Configuration)' != 'Debug'">None</IncludeAssets>
<PrivateAssets Condition="'$(Configuration)' != 'Debug'">All</PrivateAssets>
Expand All @@ -30,4 +32,7 @@
<ItemGroup>
<EmbeddedResource Update="Assets/Resources.resx" Public="true" EmitFormatMethods="true" />
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="AvaloniaExampleProject.Tests" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions src/AvaloniaExampleProject/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static IServiceCollection AddAppServices(this IServiceCollection serviceC
.AddConfigurationFile<MainConfig, IAppDataAssetsService>("config.json", JsonContext.Default.MainConfig)
.AddLocalization()
.AddSingleton<IThemeService, ThemeService>()
.AddSingleton<IAppInformationService, AppInformationService>()
// Configure ViewModels
.AddTransient<MainWindowViewModel>()
.AddTransient<MainViewModel>()
Expand Down
25 changes: 25 additions & 0 deletions src/AvaloniaExampleProject/Business/AppInformationService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Data;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

namespace AvaloniaExampleProject.Business;

public interface IAppInformationService
{
string Version { get; }
}

public sealed class AppInformationService : IAppInformationService
{
[field: AllowNull, MaybeNull]
public string Version
{
get
{
field ??=
typeof(App).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion
?? throw new VersionNotFoundException("Could not get version");
return field!;
}
}
}
2 changes: 0 additions & 2 deletions src/AvaloniaExampleProject/Business/ThemeService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.ComponentModel;
using System.Reactive.Disposables;
using Avalonia.Styling;
using AvaloniaExampleProject.Models;
using Darp.Utils.Configuration;
Expand Down
1 change: 1 addition & 0 deletions src/AvaloniaExampleProject/Models/DesignData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ public static IServiceProvider ServiceProvider
public static WelcomeViewModel WelcomeViewModel { get; } = ServiceProvider.GetRequiredService<WelcomeViewModel>();
public static SettingsViewModel SettingsViewModel { get; } =
ServiceProvider.GetRequiredService<SettingsViewModel>();
public static MainViewModel MainViewModel { get; } = ServiceProvider.GetRequiredService<MainViewModel>();
}
5 changes: 4 additions & 1 deletion src/AvaloniaExampleProject/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ public sealed partial class SettingsViewModel(
Resources i18N,
IConfigurationService<MainConfig> configurationService,
IDialogService dialogService,
IAppInformationService appInformationService,
ILogger<SettingsViewModel> logger
) : ViewModelBase
{
private readonly IConfigurationService<MainConfig> _configurationService = configurationService;
private readonly IDialogService _dialogService = dialogService;
private readonly IAppInformationService _appInformationService = appInformationService;
private readonly ILogger<SettingsViewModel> _logger = logger;

public IThemeService ThemeService { get; } = themeService;
public Resources I18N { get; } = i18N;
public IObservable<string> AppVersion => I18N.Observe(x => x.FormatSettings_About_Version(App.Version));
public IObservable<string> AppVersion =>
I18N.Observe(x => x.FormatSettings_About_Version(_appInformationService.Version));

[ObservableProperty]
public partial string SelectedLanguage { get; set; } = configurationService.Config.UserPreferences.SelectedLanguage;
Expand Down
17 changes: 11 additions & 6 deletions src/AvaloniaExampleProject/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,27 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:AvaloniaExampleProject.ViewModels"
xmlns:controls="clr-namespace:FluentAvalonia.UI.Controls;assembly=FluentAvalonia"
xmlns:models="clr-namespace:AvaloniaExampleProject.Models"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="AvaloniaExampleProject.Views.MainView"
x:DataType="vm:MainViewModel">
x:DataType="vm:MainViewModel"
Design.DataContext="{x:Static models:DesignData.MainViewModel}">
<controls:NavigationView Name="NavView"
IsSettingsVisible="False" IsPaneToggleButtonVisible="False"
SelectionFollowsFocus="True"
SelectedItem="Home"
CompactModeThresholdWidth="0"
SelectionChanged="TabStripControl_OnSelectionChanged">
<controls:NavigationView.Resources>
<!-- Override this so content doesn't appear in the TitleBar area -->
<Thickness x:Key="NavigationViewContentMargin">0,0,0,0</Thickness>
<Thickness x:Key="NavigationViewMinimalContentMargin">0,0,0,0</Thickness>
<!-- Override this so the header is shown correctly -->
<Thickness x:Key="NavigationViewHeaderMargin">8,8,0,0</Thickness>
</controls:NavigationView.Resources>
<controls:NavigationView.Header>
<TextBlock Text="{CompiledBinding #NavView.((controls:NavigationViewItem)SelectedItem).Content}"/>
</controls:NavigationView.Header>
<controls:NavigationView.MenuItems>
<controls:NavigationViewItem IconSource="Home" Tag="{x:Type vm:WelcomeViewModel}"
Content="{CompiledBinding I18N.Welcome_Title}" />
Expand All @@ -26,10 +34,7 @@
Content="{CompiledBinding I18N.Settings_Title}" />
</controls:NavigationView.FooterMenuItems>

<StackPanel Orientation="Vertical" Spacing="16" Margin="8,8,10,6">
<TextBlock Text="{CompiledBinding #NavView.((controls:NavigationViewItem)SelectedItem).Content}"
Theme="{StaticResource TitleTextBlockStyle}"/>
<controls:Frame Name="MainFrame" NavigationFailed="MainFrame_OnNavigationFailed"/>
</StackPanel>
<controls:Frame Name="MainFrame" NavigationFailed="MainFrame_OnNavigationFailed"
Margin="12,8,12,8"/>
</controls:NavigationView>
</UserControl>
3 changes: 2 additions & 1 deletion src/AvaloniaExampleProject/Views/SettingsView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
</StackPanel>
<TextBlock Text="{CompiledBinding I18N.Settings_About_Title}"
Theme="{StaticResource BodyStrongTextBlockStyle}"/>
<controls:SettingsExpander Header="{CompiledBinding I18N.App_Title}"
<controls:SettingsExpander Name="AboutSettingsExpander"
Header="{CompiledBinding I18N.App_Title}"
Description="{CompiledBinding I18N.Settings_About_ByCompany}">
<controls:SettingsExpander.Footer>
<SelectableTextBlock Text="{CompiledBinding AppVersion^}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Shouldly" />
<PackageReference Include="Verify.Avalonia" />
<PackageReference Include="Verify.CommunityToolkit.Mvvm" />
<PackageReference Include="Verify.ImageMagick" />
<PackageReference Include="Verify.Xunit" />
<!-- Upgrade to v3 when https://github.com/AvaloniaUI/Avalonia/issues/18356 is resolved -->
<PackageReference Include="xunit" />
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@
},
{
Type: SettingsExpander,
Header: Avalonia Example Project
Header: Avalonia Example Project,
Name: AboutSettingsExpander
}
]
}
},
FontFamily: Inter,
DataContext: {
ThemeService: {
AvailableThemes: [
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
Type: SettingsView,
Content: {
Type: ScrollViewer,
VerticalScrollBarVisibility: Auto,
Content: {
Type: StackPanel,
Spacing: 8.0,
MaxWidth: 1024.0,
Margin: 0,0,0,48,
Children: [
{
Type: TextBlock,
Text: Personalization,
Theme: {
Type: ControlTheme
}
},
{
Type: StackPanel,
Spacing: 4.0,
Children: [
{
Type: SettingsExpander,
Header: Language
},
{
Type: SettingsExpander,
Header: Theme
}
]
},
{
Type: TextBlock,
Text: About,
Theme: {
Type: ControlTheme
}
},
{
Type: SettingsExpander,
Header: Avalonia Example Project,
Name: AboutSettingsExpander
}
]
}
},
FontFamily: Inter,
DataContext: {
ThemeService: {
AvailableThemes: [
Default,
Dark,
Light
],
RequestedThemeVariant: {}
},
I18N: {Scrubbed},
AppVersion: {},
SelectedLanguage: en-EN,
SelectedTheme: Default,
ShowLicensesDialogCommand: SettingsViewModel.ShowLicensesDialogAsync(CancellationToken cancellationToken)
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
}
]
},
FontFamily: Inter,
DataContext: {
I18N: {Scrubbed},
ShowInputDialogCommand: WelcomeViewModel.ShowInputDialog(CancellationToken cancellationToken)
Expand Down
24 changes: 12 additions & 12 deletions test/AvaloniaExampleProject.Tests/TestAppBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using Avalonia;
using Avalonia.Fonts.Inter;
using Avalonia.Headless;
using Avalonia.Media;
using AvaloniaExampleProject.Business;
using AvaloniaExampleProject.Models;
using AvaloniaExampleProject.Tests;
using Darp.Utils.Configuration;
Expand All @@ -19,17 +21,8 @@ public class TestAppBuilder
[ModuleInitializer]
public static void Init()
{
VerifyImageMagick.RegisterComparers(0.1);
VerifierSettings.InitializePlugins();
RemovePngFileConverters();
}

private static void RemovePngFileConverters()
{
object? list = typeof(VerifierSettings)
.GetField("typedConverters", BindingFlags.NonPublic | BindingFlags.Static)
?.GetValue(null);
var clazz = typeof(VerifierSettings).Assembly.GetType("TypeConverter")!;
typeof(List<>).MakeGenericType(clazz).GetMethod("Clear")!.Invoke(list, null);
}

private static readonly MainConfig MainConfig = new()
Expand Down Expand Up @@ -57,21 +50,28 @@ public static IConfigurationService<MainConfig> SubstituteForMainConfigService()

public static AppBuilder BuildAvaloniaApp()
{
var appInformationService = Substitute.For<IAppInformationService>();
appInformationService.Version.Returns("1.2.3-aabbccdd");
IServiceProvider provider = new ServiceCollection()
.AddLogging(builder => builder.AddXUnit())
.AddSingleton(SubstituteForMainConfigService())
.AddAppServices()
.AddSingleton(appInformationService)
.BuildServiceProvider();
Services = provider;
return AppBuilder
.Configure(() => new App(provider))
.UseSkia()
.UseHeadless(new AvaloniaHeadlessPlatformOptions { UseHeadlessDrawing = false })
.WithInterFont()
.AfterSetup(builder =>
{
if (builder.Instance is null)
throw new Exception("Instance is null");
var themes = builder.Instance.Styles.OfType<FluentAvaloniaTheme>();
builder.Instance.Styles.RemoveAll(themes);
builder.Instance.Styles.Add(
new FluentAvaloniaTheme { PreferSystemTheme = true, PreferUserAccentColor = true }
new FluentAvaloniaTheme { PreferSystemTheme = false, PreferUserAccentColor = false }
);
});
}
Expand Down
11 changes: 9 additions & 2 deletions test/AvaloniaExampleProject.Tests/VerifyHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
using System.Runtime.CompilerServices;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Media;
using AvaloniaExampleProject.Assets;
using Shouldly;

namespace AvaloniaExampleProject.Tests;

public static class VerifyHelpers
{
public static SettingsTask VerifyControl(Control control, [CallerFilePath] string? callerFilePath = null)
public static SettingsTask VerifyControl(TemplatedControl control, [CallerFilePath] string? callerFilePath = null)
{
var fontFamily = new FontFamily("avares://Avalonia.Fonts.Inter/Assets/Inter-Regular.ttf#Inter");
control.FontFamily = fontFamily;
control.Resources.Add("ContentControlThemeFontFamily", fontFamily);
control.FontFamily.Name.ShouldBe("Inter");
control.FontFamily.FamilyTypefaces.Count.ShouldBeGreaterThan(0);
string directory =
Path.GetFileNameWithoutExtension(callerFilePath) ?? throw new ArgumentNullException(nameof(callerFilePath));
return Verify(control).ScrubMembersWithType<Resources>().UseDirectory(Path.Join("Snapshots", directory));
Expand Down
Loading