From 88d9166ac4db7697a2cebd709963146143ddce05 Mon Sep 17 00:00:00 2001 From: "atanas.popatanasov" Date: Mon, 22 Mar 2021 19:51:57 +0200 Subject: [PATCH] Added corner radius for RadShadow --- .../Primitives.UWP/Shadow/RadShadow.cs | 95 +- .../Primitives.UWP/Themes/Generic.xaml | 2551 +++++++++-------- 2 files changed, 1362 insertions(+), 1284 deletions(-) diff --git a/Controls/Primitives/Primitives.UWP/Shadow/RadShadow.cs b/Controls/Primitives/Primitives.UWP/Shadow/RadShadow.cs index 9814666..4d26d44 100644 --- a/Controls/Primitives/Primitives.UWP/Shadow/RadShadow.cs +++ b/Controls/Primitives/Primitives.UWP/Shadow/RadShadow.cs @@ -1,9 +1,11 @@ -using System.Numerics; +using System; +using System.Numerics; using Windows.Foundation; using Windows.UI; using Windows.UI.Composition; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Hosting; using Windows.UI.Xaml.Markup; using Windows.UI.Xaml.Shapes; @@ -51,7 +53,13 @@ namespace Telerik.UI.Xaml.Controls.Primitives /// Identifies the dependency property. /// public static readonly DependencyProperty ContentProperty = - DependencyProperty.Register(nameof(Content), typeof(object), typeof(RadShadow), new PropertyMetadata(null, new PropertyChangedCallback((d, e) => ((RadShadow)d).OnContentPropertyChanged()))); + DependencyProperty.Register(nameof(Content), typeof(object), typeof(RadShadow), new PropertyMetadata(null, new PropertyChangedCallback((d, e) => ((RadShadow)d).Invalidate()))); + + /// + /// Identifies the dependency property. + /// + public static readonly DependencyProperty CornerRadiusProperty = + DependencyProperty.Register(nameof(CornerRadius), typeof(int), typeof(RadShadow), new PropertyMetadata(0, new PropertyChangedCallback((d, e) => ((RadShadow)d).OnCornerRadiusPropertyChanged()))); private const string PartShadowName = "PART_Shadow"; @@ -59,6 +67,7 @@ namespace Telerik.UI.Xaml.Controls.Primitives private SpriteVisual shadowVisual; private DropShadow dropShadow; private Canvas shadowView; + private Rectangle radiusMask; /// /// Initializes a new instance of the class. @@ -122,6 +131,15 @@ namespace Telerik.UI.Xaml.Controls.Primitives set { this.SetValue(ContentProperty, value); } } + /// + /// Gets or sets the corner radius of the shadow. + /// + public int CornerRadius + { + get { return (int)this.GetValue(CornerRadiusProperty); } + set { this.SetValue(CornerRadiusProperty, value); } + } + /// /// Use to get the shadow mask if the Content has such mask. Views like Shapes, TextBlock and Image provide masks - you can get them through the GetAlphaMask methods. /// @@ -147,7 +165,7 @@ namespace Telerik.UI.Xaml.Controls.Primitives return image.GetAlphaMask(); } - return null; + return this.radiusMask?.GetAlphaMask(); } /// @@ -158,13 +176,27 @@ namespace Telerik.UI.Xaml.Controls.Primitives var content = this.GetVisualContent(); if (content != null) { - this.ApplyShadowMaskIfNeeded(content); - var contentPosition = content.TransformToVisual(this); var offset = contentPosition.TransformPoint(new Point(0, 0)); - this.shadowVisual.Offset = new Vector3((float)offset.X - (float)this.BorderThickness.Left, (float)offset.Y - (float)this.BorderThickness.Top, 0); - this.shadowVisual.Size = new Vector2((float)content.ActualWidth, (float)content.ActualHeight); + var x = (float)offset.X - (float)this.BorderThickness.Left; + var y = (float)offset.Y - (float)this.BorderThickness.Top; + this.shadowVisual.Offset = new Vector3(x, y, 0); + + var width = content.ActualWidth; + var height = content.ActualHeight; + this.shadowVisual.Size = new Vector2((float)width, (float)height); + + if (this.radiusMask != null) + { + this.radiusMask.Width = width; + this.radiusMask.Height = height; + + Canvas.SetTop(this.radiusMask, y); + Canvas.SetLeft(this.radiusMask, x); + } + + this.ApplyShadowMaskIfNeeded(content); } return size; @@ -196,6 +228,7 @@ namespace Telerik.UI.Xaml.Controls.Primitives this.OnOffsetPropertyChanged(); this.OnBlurRadiusPropertyChanged(this.BlurRadius); this.OnShadowOpacityPropertyChanged(this.ShadowOpacity); + this.OnCornerRadiusPropertyChanged(); this.shadowVisual.Shadow = this.dropShadow; @@ -263,10 +296,54 @@ namespace Telerik.UI.Xaml.Controls.Primitives } } - private void OnContentPropertyChanged() + private void OnCornerRadiusPropertyChanged() + { + if (this.shadowView == null) + { + return; + } + + var cornerRadius = this.CornerRadius; + if (cornerRadius < 0) + { + throw new ArgumentException($"{cornerRadius} is an invalid value for {nameof(this.CornerRadius)}"); + } + + if (cornerRadius > 0) + { + if (this.radiusMask == null) + { + this.radiusMask = new Rectangle(); + this.radiusMask.SetBinding(Rectangle.FillProperty, new Binding() + { + Path = new PropertyPath(nameof(this.Background)), + Source = this, Mode = BindingMode.TwoWay + }); + + this.shadowView.Children.Add(this.radiusMask); + } + + this.radiusMask.RadiusY = cornerRadius; + this.radiusMask.RadiusX = cornerRadius; + } + else + { + if (this.shadowView.Children.Contains(this.radiusMask)) + { + this.shadowView.Children.Remove(this.radiusMask); + } + + this.radiusMask.ClearValue(Rectangle.FillProperty); + this.radiusMask = null; + } + + this.Invalidate(); + } + + private void Invalidate() { this.invalidateShadowMask = true; this.InvalidateArrange(); } } -} +} \ No newline at end of file diff --git a/Controls/Primitives/Primitives.UWP/Themes/Generic.xaml b/Controls/Primitives/Primitives.UWP/Themes/Generic.xaml index 577c065..3cc0492 100644 --- a/Controls/Primitives/Primitives.UWP/Themes/Generic.xaml +++ b/Controls/Primitives/Primitives.UWP/Themes/Generic.xaml @@ -6,1279 +6,1280 @@ xmlns:localCommon="using:Telerik.UI.Xaml.Controls.Primitives.Common" xmlns:localHubTile="using:Telerik.UI.Xaml.Controls.Primitives.HubTile" xmlns:primitivesLoopingList="using:Telerik.UI.Xaml.Controls.Primitives.LoopingList" - xmlns:sideDrawer="using:Telerik.UI.Xaml.Controls.Primitives.SideDrawer"> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + xmlns:sideDrawer="using:Telerik.UI.Xaml.Controls.Primitives.SideDrawer"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file