mirror of
https://github.com/chibicitiberiu/rainmeter-studio.git
synced 2024-02-24 04:33:31 +00:00
Rewrote references
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@ -10,14 +11,16 @@ namespace RainmeterStudio.UI.ViewModel
|
||||
/// <summary>
|
||||
/// Contains the view model of a reference
|
||||
/// </summary>
|
||||
public class ReferenceViewModel : INotifyPropertyChanged
|
||||
public class ReferenceViewModel : INotifyPropertyChanged, INotifyCollectionChanged
|
||||
{
|
||||
private List<ReferenceViewModel> _children = null;
|
||||
|
||||
#region Properties
|
||||
|
||||
/// <summary>
|
||||
/// Gets the linked reference
|
||||
/// </summary>
|
||||
public Tree<Reference> Reference { get; private set; }
|
||||
public Reference Reference { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the name
|
||||
@ -26,7 +29,7 @@ namespace RainmeterStudio.UI.ViewModel
|
||||
{
|
||||
get
|
||||
{
|
||||
return Reference.Data.Name;
|
||||
return Reference.Name;
|
||||
}
|
||||
}
|
||||
|
||||
@ -37,10 +40,20 @@ namespace RainmeterStudio.UI.ViewModel
|
||||
{
|
||||
get
|
||||
{
|
||||
return Reference.Data.StoragePath;
|
||||
return Reference.StoragePath;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an enumerable of this object's children
|
||||
/// </summary>
|
||||
public IEnumerable<ReferenceViewModel> Children
|
||||
{
|
||||
get
|
||||
{
|
||||
return _children;
|
||||
}
|
||||
}
|
||||
|
||||
private bool _isExpanded = true;
|
||||
|
||||
@ -101,11 +114,70 @@ namespace RainmeterStudio.UI.ViewModel
|
||||
/// Creates a new instance of reference view model
|
||||
/// </summary>
|
||||
/// <param name="reference">Reference</param>
|
||||
public ReferenceViewModel(Tree<Reference> reference)
|
||||
public ReferenceViewModel(Reference reference)
|
||||
{
|
||||
Reference = reference;
|
||||
Reference.CollectionChanged += Reference_CollectionChanged;
|
||||
UpdateChildren();
|
||||
}
|
||||
|
||||
void Reference_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
|
||||
{
|
||||
List<ReferenceViewModel> newItems = new List<ReferenceViewModel>();
|
||||
List<ReferenceViewModel> oldItems = new List<ReferenceViewModel>();
|
||||
|
||||
// Update collection
|
||||
switch (e.Action)
|
||||
{
|
||||
case NotifyCollectionChangedAction.Add:
|
||||
newItems = e.NewItems.Cast<Reference>().Select(x => new ReferenceViewModel(x)).ToList();
|
||||
break;
|
||||
|
||||
case NotifyCollectionChangedAction.Remove:
|
||||
oldItems = _children.Where(x => e.OldItems.Contains(x.Reference)).ToList();
|
||||
break;
|
||||
|
||||
case NotifyCollectionChangedAction.Replace:
|
||||
newItems = e.NewItems.Cast<Reference>().Select(x => new ReferenceViewModel(x)).ToList();
|
||||
oldItems = _children.Where(x => e.OldItems.Contains(x.Reference)).ToList();
|
||||
break;
|
||||
|
||||
default:
|
||||
UpdateChildren();
|
||||
break;
|
||||
}
|
||||
|
||||
_children.RemoveAll(oldItems.Contains);
|
||||
_children.AddRange(newItems);
|
||||
|
||||
// Pass event
|
||||
if (CollectionChanged != null)
|
||||
CollectionChanged(this, new NotifyCollectionChangedEventArgs(e.Action, newItems, oldItems));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Operations
|
||||
|
||||
/// <summary>
|
||||
/// Sets the 'IsExpanded' property for the entire subtree
|
||||
/// </summary>
|
||||
/// <param name="value">Value to set</param>
|
||||
public void TreeExpand(bool value)
|
||||
{
|
||||
IsExpanded = value;
|
||||
}
|
||||
|
||||
private void UpdateChildren()
|
||||
{
|
||||
_children = Reference.Children.Select(x => new ReferenceViewModel(x)).ToList();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Triggered when the linked reference collection changes
|
||||
/// </summary>
|
||||
public event NotifyCollectionChangedEventHandler CollectionChanged;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user