Browse Source
Added interfaces, moved them to Controls.Templates namespace and added base classes for common Func<> templates.pull/69/head
64 changed files with 537 additions and 247 deletions
@ -1,39 +0,0 @@ |
|||||
// -----------------------------------------------------------------------
|
|
||||
// <copyright file="ControlTemplate.cs" company="Steven Kirk">
|
|
||||
// Copyright 2014 MIT Licence. See licence.md for more information.
|
|
||||
// </copyright>
|
|
||||
// -----------------------------------------------------------------------
|
|
||||
|
|
||||
namespace Perspex.Controls |
|
||||
{ |
|
||||
using System; |
|
||||
using Perspex.Styling; |
|
||||
|
|
||||
public class ControlTemplate |
|
||||
{ |
|
||||
private Func<ITemplatedControl, Control> build; |
|
||||
|
|
||||
public ControlTemplate(Func<ITemplatedControl, Control> build) |
|
||||
{ |
|
||||
Contract.Requires<NullReferenceException>(build != null); |
|
||||
|
|
||||
this.build = build; |
|
||||
} |
|
||||
|
|
||||
public static ControlTemplate Create<TControl>(Func<TControl, Control> build) |
|
||||
where TControl : ITemplatedControl |
|
||||
{ |
|
||||
Contract.Requires<NullReferenceException>(build != null); |
|
||||
|
|
||||
return new ControlTemplate(c => build((TControl)c)); |
|
||||
} |
|
||||
|
|
||||
public Control Build(ITemplatedControl templatedParent) |
|
||||
{ |
|
||||
Contract.Requires<NullReferenceException>(templatedParent != null); |
|
||||
|
|
||||
var root = this.build(templatedParent); |
|
||||
return root; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,80 +0,0 @@ |
|||||
// -----------------------------------------------------------------------
|
|
||||
// <copyright file="DataTemplate.cs" company="Steven Kirk">
|
|
||||
// Copyright 2014 MIT Licence. See licence.md for more information.
|
|
||||
// </copyright>
|
|
||||
// -----------------------------------------------------------------------
|
|
||||
|
|
||||
namespace Perspex.Controls |
|
||||
{ |
|
||||
using System; |
|
||||
using System.Reflection; |
|
||||
|
|
||||
public class DataTemplate : IDataTemplate |
|
||||
{ |
|
||||
public static readonly DataTemplate Default = |
|
||||
new DataTemplate(typeof(object), o => (o != null) ? new TextBlock { Text = o.ToString() } : null); |
|
||||
|
|
||||
public DataTemplate(Func<object, Control> build) |
|
||||
: this(o => true, build) |
|
||||
{ |
|
||||
} |
|
||||
|
|
||||
public DataTemplate(Type type, Func<object, Control> build) |
|
||||
: this(o => IsInstance(o, type), build) |
|
||||
{ |
|
||||
} |
|
||||
|
|
||||
public DataTemplate(Func<object, bool> match, Func<object, Control> build) |
|
||||
{ |
|
||||
Contract.Requires<ArgumentNullException>(match != null); |
|
||||
Contract.Requires<ArgumentNullException>(build != null); |
|
||||
|
|
||||
this.Match = match; |
|
||||
this.Build = build; |
|
||||
} |
|
||||
|
|
||||
public Func<object, bool> Match { get; private set; } |
|
||||
|
|
||||
public Func<object, Control> Build { get; private set; } |
|
||||
|
|
||||
public static bool IsInstance(object o, Type t) |
|
||||
{ |
|
||||
return (o != null) ? |
|
||||
t.GetTypeInfo().IsAssignableFrom(o.GetType().GetTypeInfo()) : |
|
||||
false; |
|
||||
} |
|
||||
|
|
||||
bool IDataTemplate.Match(object data) |
|
||||
{ |
|
||||
return this.Match(data); |
|
||||
} |
|
||||
|
|
||||
Control IDataTemplate.Build(object data) |
|
||||
{ |
|
||||
return this.Build(data); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public class DataTemplate<T> : DataTemplate |
|
||||
{ |
|
||||
public DataTemplate(Func<T, Control> build) |
|
||||
: base(typeof(T), DataTemplate<T>.Cast(build)) |
|
||||
{ |
|
||||
} |
|
||||
|
|
||||
public DataTemplate(Func<T, bool> match, Func<T, Control> build) |
|
||||
: base(CastMatch(match), DataTemplate<T>.Cast(build)) |
|
||||
{ |
|
||||
} |
|
||||
|
|
||||
private static Func<object, bool> CastMatch(Func<T, bool> f) |
|
||||
{ |
|
||||
return o => (o is T) ? f((T)o) : false; |
|
||||
} |
|
||||
|
|
||||
private static Func<object, TResult> Cast<TResult>(Func<T, TResult> f) |
|
||||
{ |
|
||||
return o => f((T)o); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,15 +0,0 @@ |
|||||
// -----------------------------------------------------------------------
|
|
||||
// <copyright file="IDataTemplate.cs" company="Steven Kirk">
|
|
||||
// Copyright 2014 MIT Licence. See licence.md for more information.
|
|
||||
// </copyright>
|
|
||||
// -----------------------------------------------------------------------
|
|
||||
|
|
||||
namespace Perspex.Controls |
|
||||
{ |
|
||||
public interface IDataTemplate |
|
||||
{ |
|
||||
bool Match(object data); |
|
||||
|
|
||||
Control Build(object data); |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,27 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="ControlTemplate.cs" company="Steven Kirk">
|
||||
|
// Copyright 2015 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex.Controls.Templates |
||||
|
{ |
||||
|
using System; |
||||
|
using Perspex.Controls.Primitives; |
||||
|
using Perspex.Styling; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// A template for a <see cref="TemplatedControl"/>.
|
||||
|
/// </summary>
|
||||
|
public class ControlTemplate : FuncTemplate<ITemplatedControl, Control>, IControlTemplate |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="ControlTemplate"/> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="build">The build function.</param>
|
||||
|
public ControlTemplate(Func<ITemplatedControl, Control> build) |
||||
|
: base(build) |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="ControlTemplate`2.cs" company="Steven Kirk">
|
||||
|
// Copyright 2015 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex.Controls.Templates |
||||
|
{ |
||||
|
using System; |
||||
|
using Perspex.Controls.Primitives; |
||||
|
using Styling; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// A template for a <see cref="TemplatedControl"/>.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="T">The type of the lookless control.</typeparam>
|
||||
|
public class ControlTemplate<T> : ControlTemplate where T : ITemplatedControl |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="ControlTemplate{T}"/> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="build">The build function.</param>
|
||||
|
public ControlTemplate(Func<T, Control> build) |
||||
|
: base(x => build((T)x)) |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,84 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="DataTemplate.cs" company="Steven Kirk">
|
||||
|
// Copyright 2015 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex.Controls.Templates |
||||
|
{ |
||||
|
using System; |
||||
|
using System.Reflection; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Builds a control for a piece of data.
|
||||
|
/// </summary>
|
||||
|
public class DataTemplate : FuncTemplate<object, IControl>, IDataTemplate |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// The default data template used in the case where not matching data template is found.
|
||||
|
/// </summary>
|
||||
|
public static readonly DataTemplate Default = |
||||
|
new DataTemplate(typeof(object), o => (o != null) ? new TextBlock { Text = o.ToString() } : null); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The implementation of the <see cref="Match"/> method.
|
||||
|
/// </summary>
|
||||
|
private Func<object, bool> match; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="DataTemplate"/> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="type">The type of data which the data template matches.</param>
|
||||
|
/// <param name="build">
|
||||
|
/// A function which when passed an object of <paramref name="type"/> returns a control.
|
||||
|
/// </param>
|
||||
|
public DataTemplate(Type type, Func<object, IControl> build) |
||||
|
: this(o => IsInstance(o, type), build) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="DataTemplate"/> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="match">
|
||||
|
/// A function which determines whether the data template matches the specified data.
|
||||
|
/// </param>
|
||||
|
/// <param name="build">
|
||||
|
/// A function which returns a control for matching data.
|
||||
|
/// </param>
|
||||
|
public DataTemplate(Func<object, bool> match, Func<object, IControl> build) |
||||
|
: base(build) |
||||
|
{ |
||||
|
Contract.Requires<ArgumentNullException>(match != null); |
||||
|
|
||||
|
this.match = match; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Checks to see if this data template matches the specified data.
|
||||
|
/// </summary>
|
||||
|
/// <param name="data">The data.</param>
|
||||
|
/// <returns>
|
||||
|
/// True if the data template can build a control for the data, otherwise false.
|
||||
|
/// </returns>
|
||||
|
public bool Match(object data) |
||||
|
{ |
||||
|
return this.match(data); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Determines of an object is of the specified type.
|
||||
|
/// </summary>
|
||||
|
/// <param name="o">The object.</param>
|
||||
|
/// <param name="t">The type.</param>
|
||||
|
/// <returns>
|
||||
|
/// True if <paramref name="o"/> is of type <paramref name="t"/>, otherwise false.
|
||||
|
/// </returns>
|
||||
|
private static bool IsInstance(object o, Type t) |
||||
|
{ |
||||
|
return (o != null) ? |
||||
|
t.GetTypeInfo().IsAssignableFrom(o.GetType().GetTypeInfo()) : |
||||
|
false; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,63 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="DataTemplate`1.cs" company="Steven Kirk">
|
||||
|
// Copyright 2015 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex.Controls.Templates |
||||
|
{ |
||||
|
using System; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Builds a control for a piece of data of specified type.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="T">The type of the template's data.</typeparam>
|
||||
|
public class DataTemplate<T> : DataTemplate |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="DataTemplate{T}"/> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="build">
|
||||
|
/// A function which when passed an object of <typeparamref name="T"/> returns a control.
|
||||
|
/// </param>
|
||||
|
public DataTemplate(Func<T, Control> build) |
||||
|
: base(typeof(T), CastBuild(build)) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="DataTemplate{T}"/> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="match">
|
||||
|
/// A function which determines whether the data template matches the specified data.
|
||||
|
/// </param>
|
||||
|
/// <param name="build">
|
||||
|
/// A function which when passed an object of <typeparamref name="T"/> returns a control.
|
||||
|
/// </param>
|
||||
|
public DataTemplate(Func<T, bool> match, Func<T, Control> build) |
||||
|
: base(CastMatch(match), CastBuild(build)) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Casts a stongly typed match function to a weakly typed one.
|
||||
|
/// </summary>
|
||||
|
/// <param name="f">The strongly typed function.</param>
|
||||
|
/// <returns>The weakly typed function.</returns>
|
||||
|
private static Func<object, bool> CastMatch(Func<T, bool> f) |
||||
|
{ |
||||
|
return o => (o is T) ? f((T)o) : false; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Casts a stongly typed build function to a weakly typed one.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TResult">The strong data type.</typeparam>
|
||||
|
/// <param name="f">The strongly typed function.</param>
|
||||
|
/// <returns>The weakly typed function.</returns>
|
||||
|
private static Func<object, TResult> CastBuild<TResult>(Func<T, TResult> f) |
||||
|
{ |
||||
|
return o => f((T)o); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,14 +1,17 @@ |
|||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||
// <copyright file="DataTemplates.cs" company="Steven Kirk">
|
// <copyright file="DataTemplates.cs" company="Steven Kirk">
|
||||
// Copyright 2014 MIT Licence. See licence.md for more information.
|
// Copyright 2015 MIT Licence. See licence.md for more information.
|
||||
// </copyright>
|
// </copyright>
|
||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
namespace Perspex.Controls |
namespace Perspex.Controls.Templates |
||||
{ |
{ |
||||
using Perspex.Collections; |
using Perspex.Collections; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// A collection of <see cref="IDataTemplate"/>s.
|
||||
|
/// </summary>
|
||||
public class DataTemplates : PerspexList<IDataTemplate> |
public class DataTemplates : PerspexList<IDataTemplate> |
||||
{ |
{ |
||||
} |
} |
||||
} |
} |
||||
@ -0,0 +1,41 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="FuncTemplate`1.cs" company="Steven Kirk">
|
||||
|
// Copyright 2015 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex.Controls.Templates |
||||
|
{ |
||||
|
using System; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Creates a control from a <see cref="Func{TControl}"/>.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TControl">The type of control.</typeparam>
|
||||
|
public class FuncTemplate<TControl> : ITemplate<TControl> where TControl : IControl |
||||
|
{ |
||||
|
private Func<TControl> func; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="FuncTemplate{TControl}"/> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="func">The function used to create the control.</param>
|
||||
|
public FuncTemplate(Func<TControl> func) |
||||
|
{ |
||||
|
Contract.Requires<ArgumentNullException>(func != null); |
||||
|
|
||||
|
this.func = func; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Creates the control.
|
||||
|
/// </summary>
|
||||
|
/// <returns>
|
||||
|
/// The created control.
|
||||
|
/// </returns>
|
||||
|
public TControl Build() |
||||
|
{ |
||||
|
return this.func(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,44 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="FuncTemplate`2.cs" company="Steven Kirk">
|
||||
|
// Copyright 2015 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex.Controls.Templates |
||||
|
{ |
||||
|
using System; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Creates a control from a <see cref="Func{TParam, TControl}"/>.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TParam">The type of the parameter.</typeparam>
|
||||
|
/// <typeparam name="TControl">The type of control.</typeparam>
|
||||
|
public class FuncTemplate<TParam, TControl> : ITemplate<TParam, TControl> |
||||
|
where TControl : IControl |
||||
|
{ |
||||
|
private Func<TParam, TControl> func; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="FuncTemplate{TControl, TParam}"/> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="func">The function used to create the control.</param>
|
||||
|
public FuncTemplate(Func<TParam, TControl> func) |
||||
|
{ |
||||
|
Contract.Requires<ArgumentNullException>(func != null); |
||||
|
|
||||
|
this.func = func; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Creates the control.
|
||||
|
/// </summary>
|
||||
|
/// <param name="param">The parameter.</param>
|
||||
|
/// <returns>
|
||||
|
/// The created control.
|
||||
|
/// </returns>
|
||||
|
public TControl Build(TParam param) |
||||
|
{ |
||||
|
return this.func(param); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,18 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="IControlTemplate.cs" company="Steven Kirk">
|
||||
|
// Copyright 2015 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex.Controls.Templates |
||||
|
{ |
||||
|
using Perspex.Controls.Primitives; |
||||
|
using Perspex.Styling; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Interface representing a template used to build a <see cref="TemplatedControl"/>.
|
||||
|
/// </summary>
|
||||
|
public interface IControlTemplate : ITemplate<ITemplatedControl, Control> |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,23 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="IDataTemplate.cs" company="Steven Kirk">
|
||||
|
// Copyright 2015 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex.Controls.Templates |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Interface representing a template used to build a control for a piece of data.
|
||||
|
/// </summary>
|
||||
|
public interface IDataTemplate : ITemplate<object, IControl> |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Checks to see if this data template matches the specified data.
|
||||
|
/// </summary>
|
||||
|
/// <param name="data">The data.</param>
|
||||
|
/// <returns>
|
||||
|
/// True if the data template can build a control for the data, otherwise false.
|
||||
|
/// </returns>
|
||||
|
bool Match(object data); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,23 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="ITemplate`1.cs" company="Steven Kirk">
|
||||
|
// Copyright 2015 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex.Controls |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Creates a control.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TControl">The type of control.</typeparam>
|
||||
|
public interface ITemplate<TControl> where TControl : IControl |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Creates the control.
|
||||
|
/// </summary>
|
||||
|
/// <returns>
|
||||
|
/// The created control.
|
||||
|
/// </returns>
|
||||
|
TControl Build(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,25 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="ITemplate`2.cs" company="Steven Kirk">
|
||||
|
// Copyright 2015 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex.Controls.Templates |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Creates a control based on a parameter.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TParam">The type of the parameter.</typeparam>
|
||||
|
/// <typeparam name="TControl">The type of control.</typeparam>
|
||||
|
public interface ITemplate<TParam, TControl> where TControl : IControl |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Creates the control.
|
||||
|
/// </summary>
|
||||
|
/// <param name="param">The parameter.</param>
|
||||
|
/// <returns>
|
||||
|
/// The created control.
|
||||
|
/// </returns>
|
||||
|
TControl Build(TParam param); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue