Skip to content

Commit

Permalink
Added PointerPressed and PointerReleased events to DataGrid header (#…
Browse files Browse the repository at this point in the history
…12595)

* Added PointerPressed and PointerReleased events to the header in a DataGridColumn

* update refs

* Update Avalonia.Controls.DataGrid.csproj

* revert project file changes

* more reversion

* removed spaces

---------

Co-authored-by: Max Katz <maxkatz6@outlook.com>
Co-authored-by: Tim <47110241+timunie@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 2, 2023
1 parent 39ce0c2 commit 53c646b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
28 changes: 20 additions & 8 deletions src/Avalonia.Controls.DataGrid/DataGridColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using Avalonia.Data;
using Avalonia.Interactivity;
using Avalonia.VisualTree;
using Avalonia.Collections;
using System;
using System.ComponentModel;
using System.Linq;
using System.Diagnostics;
using System.Linq;
using Avalonia.Collections;
using Avalonia.Controls.Templates;
using Avalonia.Controls.Utils;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Layout;
using Avalonia.Markup.Xaml.MarkupExtensions;
using Avalonia.Styling;
using Avalonia.VisualTree;

namespace Avalonia.Controls
{
Expand All @@ -38,6 +39,15 @@ public abstract class DataGridColumn : AvaloniaObject
private Classes _cellStyleClasses;
private bool _setWidthInternalNoCallback;

/// <summary>
/// Occurs when the pointer is pressed over the column's header
/// </summary>
public event EventHandler<PointerPressedEventArgs> HeaderPointerPressed;
/// <summary>
/// Occurs when the pointer is released over the column's header
/// </summary>
public event EventHandler<PointerReleasedEventArgs> HeaderPointerReleased;

/// <summary>
/// Initializes a new instance of the <see cref="T:Avalonia.Controls.DataGridColumn" /> class.
/// </summary>
Expand Down Expand Up @@ -331,7 +341,7 @@ public bool CanUserSort
}

// return whether or not the property type can be compared
return (typeof(IComparable).IsAssignableFrom(propertyType)) ? true : false;
return typeof(IComparable).IsAssignableFrom(propertyType) ? true : false;
}
else
{
Expand Down Expand Up @@ -810,7 +820,7 @@ internal void EndCellEditInternal()
/// <param name="source"></param>
/// <param name="width">The DataGridLength to coerce.</param>
/// <returns>The resultant (coerced) DataGridLength.</returns>
static DataGridLength CoerceWidth(AvaloniaObject source, DataGridLength width)
private static DataGridLength CoerceWidth(AvaloniaObject source, DataGridLength width)
{
var target = (DataGridColumn)source;

Expand Down Expand Up @@ -911,6 +921,8 @@ internal virtual DataGridColumnHeader CreateHeader()
result.SetValue(StyledElement.ThemeProperty, columnTheme, BindingPriority.Template);
}

result.PointerPressed += (s, e) => { HeaderPointerPressed?.Invoke(this, e); };
result.PointerReleased += (s, e) => { HeaderPointerReleased?.Invoke(this, e); };
return result;
}

Expand Down Expand Up @@ -1008,7 +1020,7 @@ internal void Resize(DataGridLength oldWidth, DataGridLength newWidth, bool user
{
// Recalculate star weight of this column based on the new desired value
InheritsWidth = false;
newValue = (Width.Value * newDisplayValue) / ActualWidth;
newValue = Width.Value * newDisplayValue / ActualWidth;
}
}

Expand Down
25 changes: 14 additions & 11 deletions src/Avalonia.Controls.DataGrid/DataGridColumnHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using Avalonia.Controls.Primitives;
using System;
using System.ComponentModel;
using System.Diagnostics;
using Avalonia.Collections;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Mixins;
using Avalonia.Controls.Utils;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Collections;
using Avalonia.Media;
using System.ComponentModel;
using System.Diagnostics;
using Avalonia.Utilities;
using System;
using Avalonia.Controls.Utils;
using Avalonia.Controls.Mixins;
using Avalonia.Controls.Metadata;

namespace Avalonia.Controls
{
Expand Down Expand Up @@ -195,8 +194,12 @@ internal void UpdateSeparatorVisibility(DataGridColumn lastVisibleColumn)
}
}

public event EventHandler<KeyModifiers> LeftClick;

internal void OnMouseLeftButtonUp_Click(KeyModifiers keyModifiers, ref bool handled)
{
LeftClick?.Invoke(this, keyModifiers);

// completed a click without dragging, so we're sorting
InvokeProcessSort(keyModifiers);
handled = true;
Expand Down Expand Up @@ -441,7 +444,7 @@ internal void OnMouseMove(PointerEventArgs args, Point mousePosition, Point mous
}

Debug.Assert(OwningGrid.Parent is InputElement);

OnMouseMove_Resize(ref handled, mousePositionHeaders);

OnMouseMove_Reorder(ref handled, mousePosition, mousePositionHeaders);
Expand Down Expand Up @@ -669,7 +672,7 @@ private void OnMouseMove_BeginReorder(Point mousePosition)
Content = Content,
ContentTemplate = ContentTemplate
};
if (OwningGrid.ColumnHeaderTheme is {} columnHeaderTheme)
if (OwningGrid.ColumnHeaderTheme is { } columnHeaderTheme)
{
dragIndicator.SetValue(ThemeProperty, columnHeaderTheme, BindingPriority.Template);
}
Expand Down Expand Up @@ -786,7 +789,7 @@ private void OnMouseMove_Resize(ref bool handled, Point mousePositionHeaders)

desiredWidth = Math.Max(_dragColumn.ActualMinWidth, Math.Min(_dragColumn.ActualMaxWidth, desiredWidth));
_dragColumn.Resize(_dragColumn.Width,
new(_dragColumn.Width.Value, _dragColumn.Width.UnitType, _dragColumn.Width.DesiredValue, desiredWidth),
new(_dragColumn.Width.Value, _dragColumn.Width.UnitType, _dragColumn.Width.DesiredValue, desiredWidth),
true);

OwningGrid.UpdateHorizontalOffset(_originalHorizontalOffset);
Expand Down

1 comment on commit 53c646b

@Coloryr
Copy link
Contributor

@Coloryr Coloryr commented on 53c646b Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could add PointerReleased to Cell too?
Like DataGrid.CellPointerReleased

Please sign in to comment.