56 lines
1.4 KiB
C#
56 lines
1.4 KiB
C#
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using System.Collections.ObjectModel;
|
|
|
|
namespace CodeContextGenerator.Models;
|
|
|
|
public partial class FileItem : ObservableObject
|
|
{
|
|
[ObservableProperty]
|
|
private string name;
|
|
|
|
[ObservableProperty]
|
|
private string fullName;
|
|
|
|
[ObservableProperty]
|
|
private bool isDirectory;
|
|
|
|
[ObservableProperty]
|
|
private bool? isSelected;
|
|
|
|
[ObservableProperty]
|
|
private FileItem parent;
|
|
|
|
public ObservableCollection<FileItem> Children { get; } = new ObservableCollection<FileItem>();
|
|
|
|
partial void OnIsSelectedChanged(bool? oldValue, bool? newValue)
|
|
{
|
|
if (newValue.HasValue)
|
|
{
|
|
UpdateChildrenSelection(newValue.Value);
|
|
}
|
|
UpdateParentSelection();
|
|
}
|
|
|
|
private void UpdateChildrenSelection(bool value)
|
|
{
|
|
if (!IsDirectory) return;
|
|
|
|
foreach (var child in Children)
|
|
{
|
|
child.IsSelected = value;
|
|
}
|
|
}
|
|
|
|
private void UpdateParentSelection()
|
|
{
|
|
if (Parent == null) return;
|
|
|
|
var children = Parent.Children;
|
|
var allSelected = children.All(c => c.IsSelected == true);
|
|
var noneSelected = children.All(c => c.IsSelected == false);
|
|
var hasIndeterminate = children.Any(c => c.IsSelected == null);
|
|
|
|
Parent.IsSelected = hasIndeterminate ? null : (allSelected ? true : (noneSelected ? false : null));
|
|
Parent.UpdateParentSelection();
|
|
}
|
|
} |