Add documentation to the conductors

This commit is contained in:
Antony Male 2014-03-15 14:09:38 +00:00
parent b279305958
commit 907c59acf1
7 changed files with 100 additions and 11 deletions

View File

@ -6,8 +6,16 @@ using System.Threading.Tasks;
namespace Stylet
{
/// <summary>
/// Conductor with a single active item, and no other items
/// </summary>
/// <typeparam name="T"></typeparam>
public partial class Conductor<T> : ConductorBaseWithActiveItem<T> where T : class
{
/// <summary>
/// Activate the given item, discarding the previous ActiveItem
/// </summary>
/// <param name="item"></param>
public override async void ActivateItem(T item)
{
if (item != null && item.Equals(this.ActiveItem))
@ -21,6 +29,11 @@ namespace Stylet
}
}
/// <summary>
/// Deactive the given item, optionally closing it as well
/// </summary>
/// <param name="item">Item to deactivate</param>
/// <param name="close">True to also close the item</param>
public override async void DeactivateItem(T item, bool close)
{
if (item == null || !item.Equals(this.ActiveItem))

View File

@ -12,6 +12,9 @@ namespace Stylet
{
public partial class Collections
{
/// <summary>
/// Conductor which has many items, all of which active at the same time
/// </summary>
public class AllActive : ConductorBase<T>
{
private BindableCollection<T> items = new BindableCollection<T>();
@ -78,6 +81,10 @@ namespace Stylet
return this.CanAllItemsCloseAsync(this.items);
}
/// <summary>
/// Activate the given item, and add it to the Items collection
/// </summary>
/// <param name="item">Item to activate</param>
public override void ActivateItem(T item)
{
if (item == null)
@ -89,6 +96,11 @@ namespace Stylet
ScreenExtensions.TryActivate(item);
}
/// <summary>
/// Deactive the given item, optionally closing it as well, and remove from the Items collection
/// </summary>
/// <param name="item">Item to deactivate</param>
/// <param name="close">True to close the item as well</param>
public override async void DeactivateItem(T item, bool close)
{
if (item == null)

View File

@ -6,13 +6,30 @@ using System.Threading.Tasks;
namespace Stylet
{
/// <summary>
/// Base class for all conductors
/// </summary>
/// <typeparam name="T">Type of item to be conducted</typeparam>
public abstract class ConductorBase<T> : Screen, IConductor<T>, IParent<T> where T : class
{
public abstract IEnumerable<T> GetChildren();
/// <summary>
/// Activate the given item
/// </summary>
/// <param name="item">Item to activate</param>
public abstract void ActivateItem(T item);
/// <summary>
/// Deactivate the given item, and optionally close it as well
/// </summary>
/// <param name="item">Item to deactivate</param>
/// <param name="close">True to also close the item</param>
public abstract void DeactivateItem(T item, bool close);
// Ensure an item is ready to be activated
/// <summary>
/// Ensure an item is ready to be activated
/// </summary>
protected virtual T EnsureItem(T newItem)
{
var newItemAsChild = newItem as IChild;
@ -22,19 +39,20 @@ namespace Stylet
return newItem;
}
protected virtual void CleanUpAfterClose(T item)
{
var itemAsChild = item as IChild;
if (itemAsChild != null && itemAsChild.Parent == this)
itemAsChild.Parent = null;
}
/// <summary>
/// Utility method to determine if all of the give items can close
/// </summary>
protected virtual async Task<bool> CanAllItemsCloseAsync(IEnumerable<T> toClose)
{
var results = await Task.WhenAll(toClose.Select(x => this.CanCloseItem(x)));
return results.All(x => x);
}
/// <summary>
/// Determine if the given item can be closed
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
protected virtual Task<bool> CanCloseItem(T item)
{
var itemAsGuardClose = item as IGuardClose;

View File

@ -6,20 +6,34 @@ using System.Threading.Tasks;
namespace Stylet
{
/// <summary>
/// Base class for all conductors which had a single active item
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class ConductorBaseWithActiveItem<T> : ConductorBase<T> where T : class
{
private T _activeItem;
/// <summary>
/// Item which is currently active
/// </summary>
public T ActiveItem
{
get { return this._activeItem; }
set { this.ActivateItem(value); }
}
/// <summary>
/// From IParent, fetch all items
/// </summary>
public override IEnumerable<T> GetChildren()
{
return new[] { ActiveItem };
}
/// <summary>
/// Switch the active item to the given item
/// </summary>
protected virtual void ChangeActiveItem(T newItem, bool closePrevious)
{
ScreenExtensions.TryDeactivate(this.ActiveItem, closePrevious);
@ -35,15 +49,31 @@ namespace Stylet
this.NotifyOfPropertyChange(() => this.ActiveItem);
}
/// <summary>
/// When we're activated, also activate the ActiveItem
/// </summary>
protected override void OnActivate()
{
ScreenExtensions.TryActivate(this.ActiveItem);
}
/// <summary>
/// When we're deactivate, also deactivate the ActiveItem
/// </summary>
protected override void OnDeactivate(bool close)
{
ScreenExtensions.TryDeactivate(this.ActiveItem, close);
}
/// <summary>
/// After an item's been closed, clean it up a bit
/// </summary>
protected virtual void CleanUpAfterClose(T item)
{
var itemAsChild = item as IChild;
if (itemAsChild != null && itemAsChild.Parent == this)
itemAsChild.Parent = null;
}
}
}

View File

@ -12,6 +12,9 @@ namespace Stylet
{
public partial class Collections
{
/// <summary>
/// Conductor with many items, only one of which is active
/// </summary>
public class OneActive : ConductorBaseWithActiveItem<T>
{
private BindableCollection<T> items = new BindableCollection<T>();
@ -59,6 +62,10 @@ namespace Stylet
return this.items;
}
/// <summary>
/// Activate the given item and set it as the ActiveItem, deactivating the previous ActiveItem
/// </summary>
/// <param name="item">Item to deactivate</param>
public override void ActivateItem(T item)
{
if (item != null && item.Equals(this.ActiveItem))
@ -72,6 +79,11 @@ namespace Stylet
}
}
/// <summary>
/// Deactive the given item, and choose another item to set as the ActiveItem, optionally closing this item
/// </summary>
/// <param name="item">Item to deactivate</param>
/// <param name="close">True to close the item as well</param>
public override async void DeactivateItem(T item, bool close)
{
if (item == null)

View File

@ -13,14 +13,18 @@ namespace Stylet
void AttachView(UIElement view);
}
public interface IActivate
public interface IHasActivationState
{
bool IsActive { get; }
}
public interface IActivate : IHasActivationState
{
void Activate();
event EventHandler<ActivationEventArgs> Activated;
}
public interface IDeactivate
public interface IDeactivate : IHasActivationState
{
void Deactivate(bool close);
event EventHandler<DeactivationEventArgs> Deactivated;

View File

@ -131,7 +131,7 @@ namespace StyletUnitTests
this.conductor.ActivateItem(screen.Object);
this.conductor.DeactivateItem(screen.Object, false);
screen.Verify(x => x.Deactivate(false));
Assert.That(this.conductor.Items, Is.EquivalentTo(new[] { screen.Object }));
Assert.That(this.conductor.Items, Is.EquivalentTo(new[] { screen.Object }))
}
[Test]