Browse Source

Added Layoutable.ApplyTemplate.

Rather than each different presenter/templated control rolling their
own.
pull/5/head
Steven Kirk 12 years ago
parent
commit
3dbd669f7b
  1. 10
      Perspex.Controls/Presenters/ContentPresenter.cs
  2. 5
      Perspex.Controls/Presenters/ItemsPresenter.cs
  3. 46
      Perspex.Controls/Primitives/TemplatedControl.cs
  4. 6
      Perspex.Layout/Layoutable.cs

10
Perspex.Controls/Presenters/ContentPresenter.cs

@ -30,23 +30,21 @@ namespace Perspex.Controls.Presenters
set { this.SetValue(ContentProperty, value); } set { this.SetValue(ContentProperty, value); }
} }
protected override Size MeasureCore(Size availableSize) protected override sealed void ApplyTemplate()
{ {
if (!this.createdChild) if (!this.createdChild)
{ {
this.CreateChild(); this.CreateChild();
} }
}
protected override Size MeasureCore(Size availableSize)
{
return base.MeasureCore(availableSize); return base.MeasureCore(availableSize);
} }
protected override Size MeasureOverride(Size availableSize) protected override Size MeasureOverride(Size availableSize)
{ {
if (!this.createdChild)
{
this.CreateChild();
}
Control child = ((IVisual)this).VisualChildren.SingleOrDefault() as Control; Control child = ((IVisual)this).VisualChildren.SingleOrDefault() as Control;
if (child != null) if (child != null)

5
Perspex.Controls/Presenters/ItemsPresenter.cs

@ -41,13 +41,16 @@ namespace Perspex.Controls.Presenters
set { this.SetValue(ItemsPanelProperty, value); } set { this.SetValue(ItemsPanelProperty, value); }
} }
protected override Size MeasureOverride(Size availableSize) protected override sealed void ApplyTemplate()
{ {
if (!this.createdPanel) if (!this.createdPanel)
{ {
this.CreatePanel(); this.CreatePanel();
} }
}
protected override Size MeasureOverride(Size availableSize)
{
panel.Measure(availableSize); panel.Measure(availableSize);
return panel.DesiredSize.Value; return panel.DesiredSize.Value;
} }

46
Perspex.Controls/Primitives/TemplatedControl.cs

@ -30,6 +30,28 @@ namespace Perspex.Controls.Primitives
{ {
} }
protected sealed override void ApplyTemplate()
{
if (!this.templateApplied)
{
this.ClearVisualChildren();
if (this.Template != null)
{
this.Log().Debug(
"Creating template for {0} (#{1:x8})",
this.GetType().Name,
this.GetHashCode());
var child = this.Template.Build(this);
this.AddVisualChild(child);
this.OnTemplateApplied();
}
this.templateApplied = true;
}
}
protected override Size ArrangeOverride(Size finalSize) protected override Size ArrangeOverride(Size finalSize)
{ {
Control child = ((IVisual)this).VisualChildren.SingleOrDefault() as Control; Control child = ((IVisual)this).VisualChildren.SingleOrDefault() as Control;
@ -47,11 +69,6 @@ namespace Perspex.Controls.Primitives
protected override Size MeasureOverride(Size availableSize) protected override Size MeasureOverride(Size availableSize)
{ {
if (!this.templateApplied)
{
this.ApplyTemplate();
}
Control child = ((IVisual)this).VisualChildren.SingleOrDefault() as Control; Control child = ((IVisual)this).VisualChildren.SingleOrDefault() as Control;
if (child != null) if (child != null)
@ -86,24 +103,5 @@ namespace Perspex.Controls.Primitives
protected virtual void OnTemplateApplied() protected virtual void OnTemplateApplied()
{ {
} }
private void ApplyTemplate()
{
this.ClearVisualChildren();
if (this.Template != null)
{
this.Log().Debug(
"Creating template for {0} (#{1:x8})",
this.GetType().Name,
this.GetHashCode());
var child = this.Template.Build(this);
this.AddVisualChild(child);
this.OnTemplateApplied();
}
this.templateApplied = true;
}
} }
} }

6
Perspex.Layout/Layoutable.cs

@ -196,6 +196,10 @@ namespace Perspex.Layout
property.Changed.Subscribe(AffectsMeasureInvalidate); property.Changed.Subscribe(AffectsMeasureInvalidate);
} }
protected virtual void ApplyTemplate()
{
}
protected virtual void ArrangeCore(Rect finalRect) protected virtual void ArrangeCore(Rect finalRect)
{ {
if (this.IsVisible) if (this.IsVisible)
@ -259,6 +263,8 @@ namespace Perspex.Layout
{ {
if (this.IsVisible) if (this.IsVisible)
{ {
this.ApplyTemplate();
var constrained = LayoutHelper.ApplyLayoutConstraints(this, availableSize) var constrained = LayoutHelper.ApplyLayoutConstraints(this, availableSize)
.Deflate(this.Margin); .Deflate(this.Margin);

Loading…
Cancel
Save