Skip to content

Commit 1a4e1a7

Browse files
klormanNikita ManchukSKProCH
authored
Add Material.Avalonia.TreeDataGrid project with TreeDataGrid styles (#467)
* Add `Material.Avalonia.TreeDataGrid` project with TreeDataGrid styles and functionality. * Fix: Added fixed width to `PART_ExpanderPanel` to prevent toggle button (chevron) visibility changes from misaligning content. * Refactor: Split `TreeDataGrid` styles into separate axaml files for better organization and maintainability. * Add TreeDataGridStyles auto importing --------- Co-authored-by: Nikita Manchuk <n.manchuk@okbsapr.ru> Co-authored-by: SKProCH <skproch@yandex.ru>
1 parent 53d8f11 commit 1a4e1a7

20 files changed

+645
-3
lines changed

Directory.Packages.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<PackageVersion Include="Avalonia" Version="$(AvaloniaVersion)" />
1313
<PackageVersion Include="Avalonia.Diagnostics" Version="$(AvaloniaVersion)" />
1414
<PackageVersion Include="Avalonia.Controls.DataGrid" Version="$(AvaloniaVersion)" />
15+
<PackageVersion Include="Avalonia.Controls.TreeDataGrid" Version="11.1.0" />
1516
<PackageVersion Include="Avalonia.Controls.ItemsRepeater" Version="11.0.9" />
1617
<PackageVersion Include="Avalonia.Desktop" Version="$(AvaloniaVersion)" />
1718
<PackageVersion Include="Avalonia.Themes.Simple" Version="$(AvaloniaVersion)" />
@@ -33,4 +34,4 @@
3334
<!--For _build-->
3435
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
3536
</ItemGroup>
36-
</Project>
37+
</Project>

Material.Avalonia.Demo.Browser/Material.Avalonia.Demo.Browser.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<!--Since Material.Avalonia.DataGrid included as a ProjectReference, auto-imports doesn't work properly-->
3333
<ItemGroup>
3434
<RuntimeHostConfigurationOption Include="MaterialThemeIncludeDataGrid" Value="true"/>
35+
<RuntimeHostConfigurationOption Include="MaterialThemeIncludeTreeDataGrid" Value="true"/>
3536
<TrimmerRootAssembly Include="Material.Avalonia.DataGrid"/>
3637
</ItemGroup>
3738
</Project>

Material.Avalonia.Demo.Desktop/Material.Avalonia.Demo.Desktop.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<!--Since Material.Avalonia.DataGrid included as a ProjectReference, auto-imports doesn't work properly-->
3333
<ItemGroup>
3434
<RuntimeHostConfigurationOption Include="MaterialThemeIncludeDataGrid" Value="true"/>
35+
<RuntimeHostConfigurationOption Include="MaterialThemeIncludeTreeDataGrid" Value="true"/>
3536
<TrimmerRootAssembly Include="Material.Avalonia.DataGrid"/>
3637
</ItemGroup>
3738
</Project>

Material.Avalonia.Demo/Material.Avalonia.Demo.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<PackageReference Include="ShowMeTheXaml.Avalonia.Generator" PrivateAssets="all" />
1717
</ItemGroup>
1818
<ItemGroup>
19+
<ProjectReference Include="..\Material.Avalonia.TreeDataGrid\Material.Avalonia.TreeDataGrid.csproj" />
1920
<ProjectReference Include="..\Material.Avalonia\Material.Avalonia.csproj" />
2021
<ProjectReference Include="..\Material.Avalonia.Dialogs\Material.Avalonia.Dialogs.csproj" />
2122
<ProjectReference Include="..\Material.Avalonia.DataGrid\Material.Avalonia.DataGrid.csproj" />
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using Avalonia.Controls;
5+
using Avalonia.Data.Converters;
6+
using Avalonia.Media;
7+
8+
namespace Material.Avalonia.TreeDataGrid.Converters;
9+
10+
public sealed class TreeDataGridSourceColumnAlignmentToDockConverter : IMultiValueConverter
11+
{
12+
public object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
13+
{
14+
if (values[0] is not ITreeDataGridSource source ||
15+
values[1] is not int columnIndex ||
16+
columnIndex < 0 ||
17+
columnIndex >= source.Columns.Count)
18+
return Dock.Left;
19+
20+
var column = source.Columns[columnIndex];
21+
var align = TextColumnAlignmentProvider.GetTextAlignment(column);
22+
23+
return align == TextAlignment.Right ? Dock.Right : Dock.Left;
24+
}
25+
26+
public object[] ConvertBack(object? value, Type[] targetTypes, object? parameter, CultureInfo culture)
27+
=> throw new NotSupportedException();
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<Description>TreeDataGrid styles library of Material.Avalonia.</Description>
5+
<PackageTags>avalonia xaml material design theme treedatagrid colour color ui ux material-design google-material</PackageTags>
6+
<PackageIcon>FavIcon.png</PackageIcon>
7+
8+
<TargetFrameworks>net8.0</TargetFrameworks>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<None Include="../wiki/FavIcon.png" Pack="true" PackagePath="/" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="Avalonia.Controls.TreeDataGrid" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<ProjectReference Include="..\Material.Avalonia\Material.Avalonia.csproj"/>
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<Content Include="*.props">
25+
<Pack>true</Pack>
26+
<PackagePath>build\;buildTransitive\</PackagePath>
27+
</Content>
28+
</ItemGroup>
29+
30+
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<ItemGroup>
3+
<RuntimeHostConfigurationOption Include="MaterialThemeIncludeTreeDataGrid" Value="true"/>
4+
<!--To exclude this library from trimming-->
5+
<TrimmerRootAssembly Include="Material.Avalonia.TreeDataGrid" RootMode="library"/>
6+
</ItemGroup>
7+
</Project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<treeDataGrid:MaterialTreeDataGridStyles xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:treeDataGrid="clr-namespace:Material.Avalonia.TreeDataGrid"
4+
x:Class="Material.Avalonia.TreeDataGrid.MaterialTreeDataGridStyles">
5+
<treeDataGrid:MaterialTreeDataGridStyles.Resources>
6+
<ResourceDictionary>
7+
<ResourceDictionary.MergedDictionaries>
8+
<ResourceInclude Source="avares://Material.Avalonia.TreeDataGrid/TreeDataGridCheckBoxCell.axaml" />
9+
<ResourceInclude Source="avares://Material.Avalonia.TreeDataGrid/TreeDataGridExpanderCell.axaml" />
10+
<ResourceInclude Source="avares://Material.Avalonia.TreeDataGrid/TreeDataGridTemplateCell.axaml" />
11+
<ResourceInclude Source="avares://Material.Avalonia.TreeDataGrid/TreeDataGridTextCell.axaml" />
12+
<ResourceInclude Source="avares://Material.Avalonia.TreeDataGrid/TreeDataGridRow.axaml" />
13+
<ResourceInclude Source="avares://Material.Avalonia.TreeDataGrid/TreeDataGridColumnHeader.axaml" />
14+
<ResourceInclude Source="avares://Material.Avalonia.TreeDataGrid/TreeDataGrid.axaml" />
15+
</ResourceDictionary.MergedDictionaries>
16+
</ResourceDictionary>
17+
</treeDataGrid:MaterialTreeDataGridStyles.Resources>
18+
</treeDataGrid:MaterialTreeDataGridStyles>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
namespace Material.Avalonia.TreeDataGrid;
2+
3+
public class MaterialTreeDataGridStyles : global::Avalonia.Styling.Styles {
4+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.Diagnostics.CodeAnalysis;
4+
using System.Reflection;
5+
using Avalonia.Controls.Models.TreeDataGrid;
6+
using Avalonia.Media;
7+
8+
namespace Material.Avalonia.TreeDataGrid;
9+
10+
internal static class TextColumnAlignmentProvider
11+
{
12+
private static readonly ConcurrentDictionary<Type, Func<object, TextAlignment?>> _cache = new();
13+
14+
public static TextAlignment? GetTextAlignment(object column)
15+
{
16+
ArgumentNullException.ThrowIfNull(column);
17+
18+
var colType = column.GetType();
19+
if (!colType.IsGenericType
20+
|| colType.GetGenericTypeDefinition() != typeof(TextColumn<,>))
21+
return null;
22+
23+
var getter = _cache.GetOrAdd(colType, BuildGetter);
24+
return getter(column);
25+
}
26+
27+
[UnconditionalSuppressMessage("AOT", "IL3050:Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.", Justification = "<Pending>")]
28+
private static Func<object, TextAlignment?> BuildGetter(Type closedColumnType)
29+
{
30+
var helperMethod = typeof(TextColumnHelper)
31+
.GetMethod(nameof(TextColumnHelper.GetTextAlignmentGeneric),
32+
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)!;
33+
34+
var genArgs = closedColumnType.GetGenericArguments();
35+
var closedMethod = helperMethod.MakeGenericMethod(genArgs);
36+
37+
return (Func<object, TextAlignment?>)closedMethod
38+
.CreateDelegate(typeof(Func<object, TextAlignment?>));
39+
}
40+
}

0 commit comments

Comments
 (0)