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

[WIP] [Feature] Grupping Column Header #240

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 12 additions & 8 deletions samples/TreeDataGridDemo/ViewModels/CountriesPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ public CountriesPageViewModel()
{
new TextColumn<Country, string>("Country", x => x.Name, (r, v) => r.Name = v, new GridLength(6, GridUnitType.Star), new()
{
IsTextSearchEnabled = true,
IsTextSearchEnabled = true
}),
new TemplateColumn<Country>("Region", "RegionCell", "RegionEditCell"),
new TextColumn<Country, int>("Population", x => x.Population, new GridLength(3, GridUnitType.Star)),
new TextColumn<Country, int>("Area", x => x.Area, new GridLength(3, GridUnitType.Star)),
new TextColumn<Country, int>("GDP", x => x.GDP, new GridLength(3, GridUnitType.Star), new()
new TextColumn<Country, string>("Region", x => x.Region, new GridLength(4, GridUnitType.Star)),
new GrouppedColumnn<Country>("Stats")
{
TextAlignment = Avalonia.Media.TextAlignment.Right,
MaxWidth = new GridLength(150)
}),
new TextColumn<Country, int>("Population", x => x.Population, new GridLength(3, GridUnitType.Star)),
new TextColumn<Country, int>("Area", x => x.Area, new GridLength(3, GridUnitType.Star)),
new TextColumn<Country, int>("GDP", x => x.GDP, new GridLength(3, GridUnitType.Star), new()
{
TextAlignment = Avalonia.Media.TextAlignment.Right,
MaxWidth = new GridLength(150),
})
}
,
}
};
Source.RowSelection!.SingleSelect = false;
Expand Down
26 changes: 26 additions & 0 deletions src/Avalonia.Controls.TreeDataGrid/Models/NotifyingListBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Avalonia.Controls.Models.TreeDataGrid;

namespace Avalonia.Controls.Models
Expand Down Expand Up @@ -249,5 +250,30 @@ private enum BatchUpdateType
Remove,
Reset,
}

protected bool RaiseAndSetIfChanged<TField>(
ref TField field,
TField value,
[CallerMemberName] string? propertyName = null)
{
if (!EqualityComparer<TField>.Default.Equals(field, value))
{
field = value;
RaisePropertyChanged(propertyName);
return true;
}

return false;
}

protected void RaisePropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

protected void RaisePropertyChanged(PropertyChangedEventArgs e)
{
PropertyChanged?.Invoke(this, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ColumnBase(
object? header,
GridLength? width,
ColumnOptions<TModel> options)
{
{
_header = header;
Options = options;
SetWidth(width ?? GridLength.Auto);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
using System;
using System.ComponentModel;
using System.Linq;
using Avalonia.Utilities;

namespace Avalonia.Controls.Models.TreeDataGrid
{
public class GrouppedColumnn<TModel> : ColumnList<TModel>, IGruppedColumn, IColumn<TModel>, IUpdateColumnLayout
where TModel : class
{
private static readonly ColumnOptions<TModel> _defaultColumnOptions = new();
private double _actualWidth = double.NaN;
private GridLength _width;
private double _autoWidth = double.NaN;
private double _starWidth = double.NaN;
private bool _starWidthWasConstrained;
private object? _header;
private ListSortDirection? _sortDirection;
private readonly Comparison<TModel?> _comparison;

public GrouppedColumnn(
object? header,
GridLength? width = default,
ColumnOptions<TModel>? options = default)
{
_header = header;
Options = options ?? new();
_comparison = GrouppedColumnnComparison;
SetWidth(width ?? GridLength.Auto);
}

/// <summary>
/// Gets the actual width of the column after measurement.
/// </summary>
public double ActualWidth
{
get => _actualWidth;
private set => RaiseAndSetIfChanged(ref _actualWidth, value);
}

/// <summary>
/// Gets the width of the column.
/// </summary>
/// <remarks>
/// To set the column width use <see cref="IColumns.SetColumnWidth(int, GridLength)"/>.
/// </remarks>
public GridLength Width
{
get => _width;
private set => RaiseAndSetIfChanged(ref _width, value);
}

/// <summary>
/// Gets or sets the column header.
/// </summary>
public object? Header
{
get => _header;
set => RaiseAndSetIfChanged(ref _header, value);
}

/// <summary>
/// Gets the column options.
/// </summary>
public ColumnOptions<TModel> Options { get; }

/// <summary>
/// Gets or sets the sort direction indicator that will be displayed on the column.
/// </summary>
/// <remarks>
/// Note that changing this property does not change the sorting of the data, it is only
/// used to display a sort direction indicator. To sort data according to a column use
/// <see cref="ITreeDataGridSource.SortBy(IColumn, ListSortDirection)"/>.
/// </remarks>
public ListSortDirection? SortDirection
{
get => _sortDirection;
set => RaiseAndSetIfChanged(ref _sortDirection, value);
}

/// <summary>
/// Gets or sets a user-defined object attached to the column.
/// </summary>
public object? Tag { get; set; }

bool? IColumn.CanUserResize => Options.CanUserResizeColumn;
double IUpdateColumnLayout.MinActualWidth => CoerceActualWidth(0);
double IUpdateColumnLayout.MaxActualWidth => CoerceActualWidth(double.PositiveInfinity);
bool IUpdateColumnLayout.StarWidthWasConstrained => _starWidthWasConstrained;

/// <summary>
/// Creates a cell for this column on the specified row.
/// </summary>
/// <param name="row">The row.</param>
/// <returns>The cell.</returns>
public ICell CreateCell(IRow<TModel> row) =>
new GruppedCell<TModel>(row, this);

public Comparison<TModel?>? GetComparison(ListSortDirection direction) =>
_comparison;

double IUpdateColumnLayout.CellMeasured(double width, int rowIndex)
{
double autoWidth = 0.0;
for (int i = 0; i < this.Count; i++)
{
if (this[i] is IUpdateColumnLayout columnLayout)
{
var w = columnLayout.CellMeasured(width, rowIndex);
autoWidth += w;
}
}
_autoWidth = Math.Max(NonNaN(_autoWidth), CoerceActualWidth(width));
return Width.GridUnitType == GridUnitType.Auto || double.IsNaN(ActualWidth) ?
_autoWidth : ActualWidth;
}

void IUpdateColumnLayout.CalculateStarWidth(double availableWidth, double totalStars)
{
if (!Width.IsStar)
throw new InvalidOperationException("Attempt to calculate star width on a non-star column.");

var width = (availableWidth / totalStars) * Width.Value;
_starWidth = CoerceActualWidth(width);
_starWidthWasConstrained = !MathUtilities.AreClose(_starWidth, width);
}

bool IUpdateColumnLayout.CommitActualWidth()
{
var width = Width.GridUnitType switch
{
GridUnitType.Auto => _autoWidth,
GridUnitType.Pixel => CoerceActualWidth(Width.Value),
GridUnitType.Star => _starWidth,
_ => throw new NotSupportedException(),
};

var oldWidth = ActualWidth;
ActualWidth = width;
_starWidthWasConstrained = false;
return !MathUtilities.AreClose(oldWidth, ActualWidth);
}

void IUpdateColumnLayout.SetWidth(GridLength width) => SetWidth(width);

private double CoerceActualWidth(double width)
{
width = Options.MinWidth.GridUnitType switch
{
GridUnitType.Auto => Math.Max(width, _autoWidth),
GridUnitType.Pixel => Math.Max(width, Options.MinWidth.Value),
GridUnitType.Star => throw new NotImplementedException(),
_ => width
};

return Options.MaxWidth?.GridUnitType switch
{
GridUnitType.Auto => Math.Min(width, _autoWidth),
GridUnitType.Pixel => Math.Min(width, Options.MaxWidth.Value.Value),
GridUnitType.Star => throw new NotImplementedException(),
_ => width
};
}

private void SetWidth(GridLength width)
{
_width = width;

if (width.IsAbsolute)
ActualWidth = width.Value;
}

private static double NonNaN(double v) => double.IsNaN(v) ? 0 : v;

private int GrouppedColumnnComparison(TModel? x, TModel? y)
{
var result = 0;
var direction = SortDirection ?? ListSortDirection.Ascending;
foreach (var item in this.OfType<ColumnBase<TModel>>())
{
if (item.GetComparison(direction) is { } comparer)
{
result = comparer(x, y);
if (result != 0)
{
break;
}
}
}
return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Collections.Generic;

namespace Avalonia.Controls.Models.TreeDataGrid
{
public interface IGruppedCell : ICell
{
IColumns Columns { get; }
}
public class GruppedCell<T> : IGruppedCell
{
private readonly IRow<T> _row;
private readonly IColumns _columns;
public GruppedCell(IRow<T> row, IColumns columns)
{
_row = row;
_columns = columns;
}

public bool CanEdit => false;

public object? Value => default;

public BeginEditGestures EditGestures => BeginEditGestures.None;

public IColumns Columns => _columns;

internal IEnumerable<ICell> CreateCells()
{
foreach (var column in _columns)
{
if (column is ColumnBase<T> cb)
{
yield return cb.CreateCell(_row);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Avalonia.Controls.Models.TreeDataGrid;

public interface IGruppedColumn : IColumn, IColumns
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ public abstract class TreeDataGridCell : TemplatedControl, ITreeDataGridCell
nameof(IsSelected),
o => o.IsSelected);

private static readonly DirectProperty<TreeDataGridCell, ICell?> ModelProperty =
AvaloniaProperty.RegisterDirect<TreeDataGridCell, ICell?>(nameof(Model)
, o => o.Model);

private static readonly Point s_invalidPoint = new Point(double.NaN, double.NaN);
private bool _isSelected;
private TreeDataGrid? _treeDataGrid;
private Point _pressedPoint = s_invalidPoint;
private ICell? _model;

static TreeDataGridCell()
{
Expand All @@ -32,7 +37,7 @@ static TreeDataGridCell()
public int ColumnIndex { get; private set; } = -1;
public int RowIndex { get; private set; } = -1;
public bool IsEditing { get; private set; }
public ICell? Model { get; private set; }
public ICell? Model { get => _model; private set => SetAndRaise(ModelProperty, ref _model , value); }

public bool IsSelected
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.Xml.Linq;
using Avalonia.Controls.Models.TreeDataGrid;
using Avalonia.Controls.Selection;
using Avalonia.Layout;
using Avalonia.LogicalTree;
using Avalonia.Media;
using Avalonia.VisualTree;

namespace Avalonia.Controls.Primitives
Expand Down
Loading
Loading