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

Pin properties dev tool update #12315

Closed
Closed
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion nukebuild/Numerge
2 changes: 1 addition & 1 deletion src/Avalonia.Controls.DataGrid/DataGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4161,7 +4161,7 @@ void SetValidationStatus(ICellEditBinding binding)

ResetValidationStatus();

if (exitEditingMode)
if (exitEditingMode && CurrentColumn != null)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this change should be included in a separate PR where the reason of change is explained.

{
CurrentColumn.EndCellEditInternal();
_editingColumnIndex = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ internal class AvaloniaPropertyViewModel : PropertyViewModel
{
private readonly AvaloniaObject _target;
private Type _assignedType;
private bool _isPinned;
private object? _value;
private string _priority;
private string _group;
Expand All @@ -19,6 +20,7 @@ public AvaloniaPropertyViewModel(AvaloniaObject o, AvaloniaProperty property)
{
_target = o;
Property = property;
_isPinned = false;

Name = property.IsAttached ?
$"[{property.OwnerType.Name}.{property.Name}]" :
Expand All @@ -33,6 +35,19 @@ public AvaloniaPropertyViewModel(AvaloniaObject o, AvaloniaProperty property)
public override string Name { get; }
public override bool? IsAttached => Property.IsAttached;
public override string Priority => _priority;
public override bool IsPinned
{
get => _isPinned;
set
{
try
{
_isPinned = value;
OnIsPinnedChanged();
}
catch { }
}
}
public override Type AssignedType => _assignedType;

public override object? Value
Expand All @@ -56,6 +71,12 @@ public override object? Value
public override bool IsReadonly => Property.IsReadOnly;

// [MemberNotNull(nameof(_type), nameof(_group), nameof(_priority))]
public override event EventHandler IsPinnedChanged;

protected virtual void OnIsPinnedChanged()
{
IsPinnedChanged?.Invoke(this, EventArgs.Empty);
}
public override void Update()
{
if (Property.IsDirect)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ internal class ClrPropertyViewModel : PropertyViewModel
{
private readonly object _target;
private Type _assignedType;
private bool _isPinned;
private object? _value;
private readonly Type _propertyType;

Expand All @@ -17,6 +18,7 @@ public ClrPropertyViewModel(object o, PropertyInfo property)
{
_target = o;
Property = property;
_isPinned = false;

if (property.DeclaringType == null || !property.DeclaringType.IsInterface)
{
Expand All @@ -42,6 +44,28 @@ public ClrPropertyViewModel(object o, PropertyInfo property)
public override Type PropertyType => _propertyType;
public override bool IsReadonly => !Property.CanWrite;

//determines if property is pinned to top in dev tools
public override bool IsPinned
{
get => _isPinned;
set
{
try
{
_isPinned = value;
OnIsPinnedChanged();
}
catch { }
}
}

public override event EventHandler IsPinnedChanged;

protected virtual void OnIsPinnedChanged()
{
IsPinnedChanged?.Invoke(this, EventArgs.Empty);
}

public override object? Value
{
get => _value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ namespace Avalonia.Diagnostics.ViewModels
internal class ControlDetailsViewModel : ViewModelBase, IDisposable, IClassesChangedListener
{
private readonly AvaloniaObject _avaloniaObject;
private int _floatingGridHeight;
private List<PropertyViewModel> _pinnedProperties;
private DataGridCollectionView? _pinnedPropertiesView;
private List<PropertyViewModel> _properties;
private DataGridCollectionView? _propertiesView;
private IDictionary<object, PropertyViewModel[]>? _propertyIndex;
private PropertyViewModel? _selectedProperty;
private DataGridCollectionView? _propertiesView;
private bool _snapshotStyles;
private bool _showInactiveStyles;
private string? _styleStatus;
Expand All @@ -32,6 +36,9 @@ internal class ControlDetailsViewModel : ViewModelBase, IDisposable, IClassesCha
public ControlDetailsViewModel(TreePageViewModel treePage, AvaloniaObject avaloniaObject)
{
_avaloniaObject = avaloniaObject;
_floatingGridHeight = 0;
_pinnedProperties = new List<PropertyViewModel>();
_properties = new List<PropertyViewModel>();

TreePage = treePage;
Layout = avaloniaObject is Visual visual
Expand Down Expand Up @@ -160,6 +167,18 @@ public DataGridCollectionView? PropertiesView
private set => RaiseAndSetIfChanged(ref _propertiesView, value);
}

public DataGridCollectionView? PinnedPropertiesView
{
get => _pinnedPropertiesView;
private set => RaiseAndSetIfChanged(ref _pinnedPropertiesView, value);
}

public int FloatingGridHeight
{
get => _floatingGridHeight;
private set => RaiseAndSetIfChanged(ref _floatingGridHeight, value);
}

public ObservableCollection<StyleViewModel> AppliedStyles { get; }

public ObservableCollection<PseudoClassViewModel> PseudoClasses { get; }
Expand Down Expand Up @@ -481,7 +500,6 @@ public void NavigateToSelectedProperty()

RaisePropertyChanged(nameof(CanNavigateToParentProperty));
}

public void NavigateToParentProperty()
{
if (_selectedEntitiesStack.Count > 0)
Expand All @@ -492,7 +510,43 @@ public void NavigateToParentProperty()
RaisePropertyChanged(nameof(CanNavigateToParentProperty));
}
}

private void OnPropertyViewModelIsPinnedChanged(object? sender, EventArgs e)
{
if (sender is PropertyViewModel propertyViewModel)
{
// CustomPropertyViewModel's IsPinned has changed, handle the change here
if (propertyViewModel.IsPinned)
{
PinProperty(propertyViewModel);
}
else
{
UnpinProperty(propertyViewModel);
}
}
}

private void UnpinProperty(PropertyViewModel property)
{
if (_pinnedProperties.Contains(property))
{
_pinnedProperties.Remove(property);
_properties.Add(property);
_pinnedProperties.ToArray();
UpdatePropertyViews(_properties.ToArray(),_pinnedProperties.ToArray());
}
}

private void PinProperty(PropertyViewModel property)
{
if (_properties.Contains(property))
{
_properties.Remove(property);
_pinnedProperties.Add(property);
UpdatePropertyViews(_properties.ToArray(), _pinnedProperties.ToArray());
}
}

protected void NavigateToProperty(object o, string? entityName)
{
var oldSelectedEntity = SelectedEntity;
Expand All @@ -518,15 +572,21 @@ protected void NavigateToProperty(object o, string? entityName)
.ThenBy(x => x.Name)
.ToArray();

_propertyIndex = properties
_properties = properties.ToList();

_propertyIndex = _properties
.GroupBy(x => x.Key)
.ToDictionary(x => x.Key, x => x.ToArray());

foreach (var propertyViewModelArray in _propertyIndex.Values)
{
foreach (var propertyViewModel in propertyViewModelArray)
{
propertyViewModel.IsPinnedChanged += OnPropertyViewModelIsPinnedChanged;
}
}

var view = new DataGridCollectionView(properties);
view.GroupDescriptions.Add(new DataGridPathGroupDescription(nameof(AvaloniaPropertyViewModel.Group)));
view.Filter = FilterProperty;
PropertiesView = view;
UpdatePropertyViews(properties, _pinnedProperties.ToArray());

switch (o)
{
Expand All @@ -539,6 +599,35 @@ protected void NavigateToProperty(object o, string? entityName)
break;
}
}

private void UpdatePropertyViews(PropertyViewModel?[] unpinnedProperties, PropertyViewModel?[] pinnedProperties)
{
var view = new DataGridCollectionView(unpinnedProperties);
view.GroupDescriptions.Add(new DataGridPathGroupDescription(nameof(AvaloniaPropertyViewModel.Group)));
view.Filter = FilterProperty;
PropertiesView = view;

var pinnedView = new DataGridCollectionView(pinnedProperties);
pinnedView.GroupDescriptions.Add(new DataGridPathGroupDescription(nameof(AvaloniaPropertyViewModel.Group)));
pinnedView.Filter = FilterProperty;
PinnedPropertiesView = pinnedView;
switch (pinnedProperties.Length)
{
case 0:
//hide grid
FloatingGridHeight = 0;
break;
case 1:
//enough height to show one entry and headers
FloatingGridHeight = 80;
break;
default:
//enough height to show two entries and scroll the rest
FloatingGridHeight = 120;
break;
}

}

internal void SelectProperty(AvaloniaProperty property)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ internal abstract class PropertyViewModel : ViewModelBase
public abstract string Name { get; }
public abstract string Group { get; }
public abstract Type AssignedType { get; }
public abstract bool IsPinned { get; set; }
public abstract Type? DeclaringType { get; }
public abstract object? Value { get; set; }
public abstract string Priority { get; }
public abstract bool? IsAttached { get; }
public abstract void Update();
public abstract Type PropertyType { get; }
public abstract event EventHandler IsPinnedChanged;

public string Type => PropertyType == AssignedType ?
PropertyType.GetTypeName() :
Expand Down
Loading