JD2022-TU1/main/tools/framework/JD.Sequencers/Windowlesses/Selection.cs

120 lines
3 KiB
C#

using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using JD.Collections;
using JD.Collections.Extensions;
using JD.Collections.Interfaces;
using JD.Kernel.StaticHelpers;
namespace JD.Sequencers.Windowlesses
{
/// <summary>
/// List of selected controls.
/// </summary>
public class Selection : ObservableSet<WindowlessControl>
{
#region Constructor
internal Selection()
{
}
#endregion
#region Properties
/// <summary>
/// Primary selection.
/// </summary>
public WindowlessControl Primary
{
get
{
if (Count == 0)
{
return null;
}
return this.First();
}
}
#endregion
#region Methods
/// <summary>
/// Toggles the selection for the given <paramref name="control"/>.
/// </summary>
/// <param name="control"></param>
public void Toggle(WindowlessControl control)
{
Throw.IfNull(control, "control");
if (Contains(control))
{
Remove(control);
}
else
{
Add(control);
}
}
/// <summary>
/// Selects the given <paramref name="control"/> (deselecting everything else).
/// </summary>
/// <param name="control"></param>
public void Select(WindowlessControl control)
{
Throw.IfNull(control, "control");
Select(new[] { control });
}
public void PerformMultiSelect()
{
using (this.BeginScopedUpdate())
{
this.AddRange(this.ToArray());
}
}
/// <summary>
/// Selects the given <paramref name="controls"/> (deselecting everything else).
/// </summary>
public void Select(IEnumerable<WindowlessControl> controls)
{
var toSelect = controls.ToArray();
if (!toSelect.IsEquivalentTo(this))
{
using (this.BeginScopedUpdate())
{
Clear();
this.AddRange(toSelect);
}
}
}
protected override void OnCollectionChanged(CollectionChangedEventArgs<WindowlessControl> e)
{
base.OnCollectionChanged(e);
ObservableList<WindowlessControl>.Synchronize(this, e, RegisterControl, UnregisterControl);
}
private static void UnregisterControl(WindowlessControl control)
{
Debug.Assert(control.Selected, "Control is in two windowless containers at the same time!");
control.Selected = false;
}
private static void RegisterControl(WindowlessControl control)
{
Debug.Assert(!control.Selected, "Control is in two windowless containers at the same time!");
control.Selected = true;
}
#endregion
}
}