From 0fce7e8150c66b84c6a9e8977ed1adc3a484f67e Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sun, 27 Mar 2016 12:10:45 +0200 Subject: [PATCH] Started fixing VisualBrush. Make sure control is initialized before it is used in a visual brush. Previously the colored rectangle in the DropDown in ControlCatalog didn't have the correct color until the DropDown was opened. This fixes the color, although initial measurement is still off. --- src/Gtk/Perspex.Cairo/Perspex.Cairo.csproj | 16 +++++ .../RenderHelpers/TileBrushImplHelper.cs | 64 +++++++++++-------- .../Perspex.Direct2D1/Media/TileBrushImpl.cs | 7 +- .../Perspex.Direct2D1.csproj | 16 +++++ 4 files changed, 69 insertions(+), 34 deletions(-) diff --git a/src/Gtk/Perspex.Cairo/Perspex.Cairo.csproj b/src/Gtk/Perspex.Cairo/Perspex.Cairo.csproj index a27683de49..6e72118062 100644 --- a/src/Gtk/Perspex.Cairo/Perspex.Cairo.csproj +++ b/src/Gtk/Perspex.Cairo/Perspex.Cairo.csproj @@ -80,6 +80,18 @@ {B09B78D8-9B26-48B0-9149-D64A2F120F3F} Perspex.Base + + {d2221c82-4a25-4583-9b43-d791e3f6820c} + Perspex.Controls + + + {62024b2d-53eb-4638-b26b-85eeaa54866e} + Perspex.Input + + + {6b0ed19d-a08b-461c-a9d9-a9ee40b0c06b} + Perspex.Interactivity + {42472427-4774-4C81-8AFF-9F27B8E31721} Perspex.Layout @@ -88,6 +100,10 @@ {EB582467-6ABB-43A1-B052-E981BA910E3A} Perspex.SceneGraph + + {f1baa01a-f176-4c6a-b39d-5b40bb1b148f} + Perspex.Styling + diff --git a/src/Shared/RenderHelpers/TileBrushImplHelper.cs b/src/Shared/RenderHelpers/TileBrushImplHelper.cs index 663bc7ca9b..9b31b6c437 100644 --- a/src/Shared/RenderHelpers/TileBrushImplHelper.cs +++ b/src/Shared/RenderHelpers/TileBrushImplHelper.cs @@ -1,18 +1,14 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using Perspex; +// Copyright (c) The Perspex Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using Perspex.Controls; using Perspex.Media; using Perspex.Rendering; -using Perspex.Layout; -using Perspex.Media.Imaging; -using Perspex.Platform; - +using Perspex.VisualTree; namespace Perspex.RenderHelpers { - class TileBrushImplHelper + internal class TileBrushImplHelper { public Size IntermediateSize { get; } public Rect DestinationRect { get; } @@ -27,8 +23,7 @@ namespace Perspex.RenderHelpers private readonly Rect _drawRect; public bool IsValid { get; } - - + public TileBrushImplHelper(TileBrush brush, Size targetSize) { _imageBrush = brush as ImageBrush; @@ -42,20 +37,19 @@ namespace Perspex.RenderHelpers } else if (_visualBrush != null) { - var visual = _visualBrush.Visual; - if (visual == null) - return; - var layoutable = visual as ILayoutable; + var control = _visualBrush.Visual as IControl; - if (layoutable != null) + if (control != null) { - if (layoutable.IsArrangeValid == false) + EnsureInitialized(control); + + if (control.IsArrangeValid == false) { - layoutable.Measure(Size.Infinity); - layoutable.Arrange(new Rect(layoutable.DesiredSize)); + control.Measure(Size.Infinity); + control.Arrange(new Rect(control.DesiredSize)); } - _imageSize = layoutable.Bounds.Size; + _imageSize = control.Bounds.Size; IsValid = true; } } @@ -118,8 +112,8 @@ namespace Perspex.RenderHelpers /// - /// Calculates a _translate based on a , a source and destination - /// rectangle and a _scale. + /// Calculates a translate based on a , a source and destination + /// rectangle and a scale. /// /// The brush. /// The source rectangle. @@ -160,7 +154,6 @@ namespace Perspex.RenderHelpers return new Vector(x, y); } - public static Matrix CalculateIntermediateTransform( TileMode tileMode, Rect sourceRect, @@ -189,13 +182,28 @@ namespace Perspex.RenderHelpers return transform; } - - - - static Size CalculateIntermediateSize( + private static Size CalculateIntermediateSize( TileMode tileMode, Size targetSize, Size destinationSize) => tileMode == TileMode.None ? targetSize : destinationSize; + private static void EnsureInitialized(IControl control) + { + foreach (var i in control.GetSelfAndVisualDescendents()) + { + var c = i as IControl; + + if (c?.IsInitialized == false) + { + var init = c as ISupportInitialize; + + if (init != null) + { + init.BeginInit(); + init.EndInit(); + } + } + } + } } } \ No newline at end of file diff --git a/src/Windows/Perspex.Direct2D1/Media/TileBrushImpl.cs b/src/Windows/Perspex.Direct2D1/Media/TileBrushImpl.cs index ab803e473e..2818e68627 100644 --- a/src/Windows/Perspex.Direct2D1/Media/TileBrushImpl.cs +++ b/src/Windows/Perspex.Direct2D1/Media/TileBrushImpl.cs @@ -1,13 +1,9 @@ // Copyright (c) The Perspex Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. -using System; -using Perspex.Layout; using Perspex.Media; -using SharpDX; -using SharpDX.Direct2D1; using Perspex.RenderHelpers; - +using SharpDX.Direct2D1; namespace Perspex.Direct2D1.Media { @@ -38,7 +34,6 @@ namespace Perspex.Direct2D1.Media } } - private static BrushProperties GetBrushProperties(TileBrush brush, Rect destinationRect) { var tileTransform = diff --git a/src/Windows/Perspex.Direct2D1/Perspex.Direct2D1.csproj b/src/Windows/Perspex.Direct2D1/Perspex.Direct2D1.csproj index c091a58c34..dc1e582bf1 100644 --- a/src/Windows/Perspex.Direct2D1/Perspex.Direct2D1.csproj +++ b/src/Windows/Perspex.Direct2D1/Perspex.Direct2D1.csproj @@ -107,6 +107,18 @@ {B09B78D8-9B26-48B0-9149-D64A2F120F3F} Perspex.Base + + {d2221c82-4a25-4583-9b43-d791e3f6820c} + Perspex.Controls + + + {62024b2d-53eb-4638-b26b-85eeaa54866e} + Perspex.Input + + + {6b0ed19d-a08b-461c-a9d9-a9ee40b0c06b} + Perspex.Interactivity + {42472427-4774-4C81-8AFF-9F27B8E31721} Perspex.Layout @@ -115,6 +127,10 @@ {EB582467-6ABB-43A1-B052-E981BA910E3A} Perspex.SceneGraph + + {f1baa01a-f176-4c6a-b39d-5b40bb1b148f} + Perspex.Styling +