8 changed files with 315 additions and 20 deletions
@ -0,0 +1,21 @@ |
|||
namespace Perspex |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Diagnostics.Contracts; |
|||
using System.Linq; |
|||
using System.Reactive.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Perspex.Controls; |
|||
|
|||
public static class Selectors |
|||
{ |
|||
public static IObservable<bool> OfType<T>(this Control control) |
|||
{ |
|||
Contract.Requires<ArgumentNullException>(control != null); |
|||
|
|||
return Observable.Return(control is T); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,38 @@ |
|||
namespace Perspex |
|||
{ |
|||
using System; |
|||
using System.Diagnostics.Contracts; |
|||
using Perspex.Controls; |
|||
|
|||
public class Setter |
|||
{ |
|||
private object oldValue; |
|||
|
|||
public PerspexProperty Property |
|||
{ |
|||
get; |
|||
set; |
|||
} |
|||
|
|||
public object Value |
|||
{ |
|||
get; |
|||
set; |
|||
} |
|||
|
|||
public void Apply(Control control) |
|||
{ |
|||
Contract.Requires<NullReferenceException>(control != null); |
|||
|
|||
this.oldValue = control.GetValue(this.Property); |
|||
control.SetValue(this.Property, this.Value); |
|||
} |
|||
|
|||
public void Detach(Control control) |
|||
{ |
|||
Contract.Requires<NullReferenceException>(control != null); |
|||
|
|||
control.SetValue(this.Property, this.oldValue); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,67 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Reactive.Disposables; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Perspex.Controls; |
|||
|
|||
namespace Perspex |
|||
{ |
|||
public class Style |
|||
{ |
|||
public Style() |
|||
{ |
|||
this.Setters = new List<Setter>(); |
|||
} |
|||
|
|||
public Func<Control, IObservable<bool>> Selector |
|||
{ |
|||
get; |
|||
set; |
|||
} |
|||
|
|||
public IEnumerable<Setter> Setters |
|||
{ |
|||
get; |
|||
set; |
|||
} |
|||
|
|||
public void Attach(Control control) |
|||
{ |
|||
this.Selector(control).Subscribe(x => |
|||
{ |
|||
if (x) |
|||
{ |
|||
this.Apply(control); |
|||
} |
|||
else |
|||
{ |
|||
this.Unapply(control); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
private void Apply(Control control) |
|||
{ |
|||
if (this.Setters != null) |
|||
{ |
|||
foreach (Setter setter in this.Setters) |
|||
{ |
|||
setter.Apply(control); |
|||
} |
|||
} |
|||
} |
|||
|
|||
private void Unapply(Control control) |
|||
{ |
|||
if (this.Setters != null) |
|||
{ |
|||
foreach (Setter setter in this.Setters) |
|||
{ |
|||
setter.Detach(control); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue