Browse Source

Handle VisualBrush.Visual == null.

pull/145/head
Steven Kirk 11 years ago
parent
commit
063c81ec13
  1. BIN
      Tests/TestFiles/Direct2D1/Media/VisualBrush/VisualBrush_No_Visual.expected.png
  2. 102
      src/Windows/Perspex.Direct2D1/Media/DrawingContext.cs
  3. 8
      src/Windows/Perspex.Direct2D1/Media/VisualBrushImpl.cs
  4. 22
      tests/Perspex.RenderTests/Media/VisualBrushTests.cs

BIN
Tests/TestFiles/Direct2D1/Media/VisualBrush/VisualBrush_No_Visual.expected.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 263 B

102
src/Windows/Perspex.Direct2D1/Media/DrawingContext.cs

@ -90,12 +90,15 @@ namespace Perspex.Direct2D1.Media
using (var d2dBrush = CreateBrush(pen.Brush, size)) using (var d2dBrush = CreateBrush(pen.Brush, size))
using (var d2dStroke = pen.ToDirect2DStrokeStyle(_renderTarget)) using (var d2dStroke = pen.ToDirect2DStrokeStyle(_renderTarget))
{ {
_renderTarget.DrawLine( if (d2dBrush.PlatformBrush != null)
p1.ToSharpDX(), {
p2.ToSharpDX(), _renderTarget.DrawLine(
d2dBrush.PlatformBrush, p1.ToSharpDX(),
(float)pen.Thickness, p2.ToSharpDX(),
d2dStroke); d2dBrush.PlatformBrush,
(float)pen.Thickness,
d2dStroke);
}
} }
} }
} }
@ -112,8 +115,11 @@ namespace Perspex.Direct2D1.Media
{ {
using (var d2dBrush = CreateBrush(brush, geometry.Bounds.Size)) using (var d2dBrush = CreateBrush(brush, geometry.Bounds.Size))
{ {
GeometryImpl impl = (GeometryImpl)geometry.PlatformImpl; if (d2dBrush.PlatformBrush != null)
_renderTarget.FillGeometry(impl.Geometry, d2dBrush.PlatformBrush); {
var impl = (GeometryImpl)geometry.PlatformImpl;
_renderTarget.FillGeometry(impl.Geometry, d2dBrush.PlatformBrush);
}
} }
} }
@ -122,8 +128,11 @@ namespace Perspex.Direct2D1.Media
using (var d2dBrush = CreateBrush(pen.Brush, geometry.GetRenderBounds(pen.Thickness).Size)) using (var d2dBrush = CreateBrush(pen.Brush, geometry.GetRenderBounds(pen.Thickness).Size))
using (var d2dStroke = pen.ToDirect2DStrokeStyle(_renderTarget)) using (var d2dStroke = pen.ToDirect2DStrokeStyle(_renderTarget))
{ {
GeometryImpl impl = (GeometryImpl)geometry.PlatformImpl; if (d2dBrush.PlatformBrush != null)
_renderTarget.DrawGeometry(impl.Geometry, d2dBrush.PlatformBrush, (float)pen.Thickness, d2dStroke); {
var impl = (GeometryImpl)geometry.PlatformImpl;
_renderTarget.DrawGeometry(impl.Geometry, d2dBrush.PlatformBrush, (float)pen.Thickness, d2dStroke);
}
} }
} }
} }
@ -139,21 +148,24 @@ namespace Perspex.Direct2D1.Media
using (var brush = CreateBrush(pen.Brush, rect.Size)) using (var brush = CreateBrush(pen.Brush, rect.Size))
using (var d2dStroke = pen.ToDirect2DStrokeStyle(_renderTarget)) using (var d2dStroke = pen.ToDirect2DStrokeStyle(_renderTarget))
{ {
if (cornerRadius == 0) if (brush.PlatformBrush != null)
{ {
_renderTarget.DrawRectangle( if (cornerRadius == 0)
rect.ToDirect2D(), {
brush.PlatformBrush, _renderTarget.DrawRectangle(
(float)pen.Thickness, rect.ToDirect2D(),
d2dStroke); brush.PlatformBrush,
} (float)pen.Thickness,
else d2dStroke);
{ }
_renderTarget.DrawRoundedRectangle( else
new RoundedRectangle { Rect = rect.ToDirect2D(), RadiusX = cornerRadius, RadiusY = cornerRadius }, {
brush.PlatformBrush, _renderTarget.DrawRoundedRectangle(
(float)pen.Thickness, new RoundedRectangle { Rect = rect.ToDirect2D(), RadiusX = cornerRadius, RadiusY = cornerRadius },
d2dStroke); brush.PlatformBrush,
(float)pen.Thickness,
d2dStroke);
}
} }
} }
} }
@ -173,7 +185,10 @@ namespace Perspex.Direct2D1.Media
using (var brush = CreateBrush(foreground, impl.Measure())) using (var brush = CreateBrush(foreground, impl.Measure()))
using (var renderer = new PerspexTextRenderer(this, _renderTarget, brush.PlatformBrush)) using (var renderer = new PerspexTextRenderer(this, _renderTarget, brush.PlatformBrush))
{ {
impl.TextLayout.Draw(renderer, (float)origin.X, (float)origin.Y); if (brush.PlatformBrush != null)
{
impl.TextLayout.Draw(renderer, (float)origin.X, (float)origin.Y);
}
} }
} }
} }
@ -188,24 +203,27 @@ namespace Perspex.Direct2D1.Media
{ {
using (var b = CreateBrush(brush, rect.Size)) using (var b = CreateBrush(brush, rect.Size))
{ {
if (cornerRadius == 0) if (b.PlatformBrush != null)
{
_renderTarget.FillRectangle(rect.ToDirect2D(), b.PlatformBrush);
}
else
{ {
_renderTarget.FillRoundedRectangle( if (cornerRadius == 0)
new RoundedRectangle {
{ _renderTarget.FillRectangle(rect.ToDirect2D(), b.PlatformBrush);
Rect = new RectangleF( }
(float)rect.X, else
(float)rect.Y, {
(float)rect.Width, _renderTarget.FillRoundedRectangle(
(float)rect.Height), new RoundedRectangle
RadiusX = cornerRadius, {
RadiusY = cornerRadius Rect = new RectangleF(
}, (float)rect.X,
b.PlatformBrush); (float)rect.Y,
(float)rect.Width,
(float)rect.Height),
RadiusX = cornerRadius,
RadiusY = cornerRadius
},
b.PlatformBrush);
}
} }
} }
} }

8
src/Windows/Perspex.Direct2D1/Media/VisualBrushImpl.cs

@ -16,6 +16,12 @@ namespace Perspex.Direct2D1.Media
Size targetSize) Size targetSize)
{ {
var visual = brush.Visual; var visual = brush.Visual;
if (visual == null)
{
return;
}
var layoutable = visual as ILayoutable; var layoutable = visual as ILayoutable;
if (layoutable?.IsArrangeValid == false) if (layoutable?.IsArrangeValid == false)
@ -102,7 +108,7 @@ namespace Perspex.Direct2D1.Media
public override void Dispose() public override void Dispose()
{ {
((BitmapBrush)PlatformBrush).Bitmap.Dispose(); ((BitmapBrush)PlatformBrush)?.Bitmap.Dispose();
base.Dispose(); base.Dispose();
} }
} }

22
tests/Perspex.RenderTests/Media/VisualBrushTests.cs

@ -650,6 +650,28 @@ namespace Perspex.Direct2D1.RenderTests.Media
} }
}; };
RenderToFile(target);
CompareImages();
}
#if PERSPEX_CAIRO
[Fact(Skip = "VisualBrush not yet implemented on Cairo")]
#else
[Fact]
#endif
public void VisualBrush_No_Visual()
{
Decorator target = new Decorator
{
Padding = new Thickness(8),
Width = 200,
Height = 200,
Child = new Rectangle
{
Fill = new VisualBrush(),
}
};
RenderToFile(target); RenderToFile(target);
CompareImages(); CompareImages();
} }

Loading…
Cancel
Save