From 34d73b58280cd451050635e8116aec5535bd21dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B1=EC=88=98/Common=20Platform=20Lab=28SR?= =?UTF-8?q?=29/Staff=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Fri, 29 Oct 2021 12:06:26 +0900 Subject: [PATCH] [Tizen] Add BorderDrawable (#224) * Move to platform specific * Fix border handler for Tizen * Rename ToDrawable method * Fix border content layout * Fix BorderView * Apply rebase --- .../src/Graphics/PaintExtensions.Tizen.cs | 5 + .../Handlers/Border/BorderHandler.Tizen.cs | 9 +- src/Core/src/Handlers/View/ViewHandler.cs | 2 +- .../Tizen/BackgroundDrawable.cs} | 6 +- src/Core/src/Platform/Tizen/BorderDrawable.cs | 39 ++++++++ src/Core/src/Platform/Tizen/BorderView.cs | 86 +++++++++++++++++ .../Tizen/ShadowDrawable.cs} | 4 +- .../src/Platform/Tizen/StrokeExtensions.cs | 94 +++++++------------ 8 files changed, 176 insertions(+), 69 deletions(-) rename src/Core/src/{Graphics/BackgroundDrawable.Tizen.cs => Platform/Tizen/BackgroundDrawable.cs} (88%) create mode 100644 src/Core/src/Platform/Tizen/BorderDrawable.cs create mode 100644 src/Core/src/Platform/Tizen/BorderView.cs rename src/Core/src/{Graphics/ShadowDrawable.Tizen.cs => Platform/Tizen/ShadowDrawable.cs} (93%) diff --git a/src/Core/src/Graphics/PaintExtensions.Tizen.cs b/src/Core/src/Graphics/PaintExtensions.Tizen.cs index f4c96b23dd84..90e10ac19973 100644 --- a/src/Core/src/Graphics/PaintExtensions.Tizen.cs +++ b/src/Core/src/Graphics/PaintExtensions.Tizen.cs @@ -6,5 +6,10 @@ public static IDrawable ToDrawable(this Paint paint, PathF path) { return new BackgroundDrawable(paint, path); } + + public static IDrawable ToDrawable(this Paint paint, IBorder border) + { + return new BorderDrawable(paint, border); + } } } diff --git a/src/Core/src/Handlers/Border/BorderHandler.Tizen.cs b/src/Core/src/Handlers/Border/BorderHandler.Tizen.cs index ccd9e46ac273..342ea1aa773a 100644 --- a/src/Core/src/Handlers/Border/BorderHandler.Tizen.cs +++ b/src/Core/src/Handlers/Border/BorderHandler.Tizen.cs @@ -2,16 +2,16 @@ namespace Microsoft.Maui.Handlers { - public partial class BorderHandler : ViewHandler + public partial class BorderHandler : ViewHandler { INativeViewHandler? _contentHandler; - protected override ContentCanvas CreateNativeView() + protected override BorderView CreateNativeView() { _ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} must be set to create a Page"); _ = NativeParent ?? throw new InvalidOperationException($"{nameof(NativeParent)} cannot be null"); - var view = new ContentCanvas(NativeParent, VirtualView) + var view = new BorderView(NativeParent, VirtualView) { CrossPlatformMeasure = VirtualView.CrossPlatformMeasure, CrossPlatformArrange = VirtualView.CrossPlatformArrange @@ -46,13 +46,12 @@ void UpdateContent() _ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} should have been set by base class."); _ = MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} should have been set by base class."); - NativeView.Children.Clear(); _contentHandler?.Dispose(); _contentHandler = null; if (VirtualView.PresentedContent is IView view) { - NativeView.Children.Add(view.ToNative(MauiContext)); + NativeView.Content = view.ToNative(MauiContext); if (view.Handler is INativeViewHandler thandler) { thandler?.SetParent(this); diff --git a/src/Core/src/Handlers/View/ViewHandler.cs b/src/Core/src/Handlers/View/ViewHandler.cs index 5a72e9996e97..6a57f3dce4b9 100644 --- a/src/Core/src/Handlers/View/ViewHandler.cs +++ b/src/Core/src/Handlers/View/ViewHandler.cs @@ -83,7 +83,7 @@ public virtual bool NeedsContainer { get { -#if WINDOWS || TIZEN +#if WINDOWS if(VirtualView is IBorder border) return border?.Shape != null || border?.Stroke != null; diff --git a/src/Core/src/Graphics/BackgroundDrawable.Tizen.cs b/src/Core/src/Platform/Tizen/BackgroundDrawable.cs similarity index 88% rename from src/Core/src/Graphics/BackgroundDrawable.Tizen.cs rename to src/Core/src/Platform/Tizen/BackgroundDrawable.cs index 8c9fa3fc3401..eca0551560bd 100644 --- a/src/Core/src/Graphics/BackgroundDrawable.Tizen.cs +++ b/src/Core/src/Platform/Tizen/BackgroundDrawable.cs @@ -1,4 +1,6 @@ -namespace Microsoft.Maui.Graphics +using Microsoft.Maui.Graphics; + +namespace Microsoft.Maui { public class BackgroundDrawable : IDrawable { @@ -31,4 +33,4 @@ public void Draw(ICanvas canvas, RectangleF dirtyRect) canvas.RestoreState(); } } -} \ No newline at end of file +} diff --git a/src/Core/src/Platform/Tizen/BorderDrawable.cs b/src/Core/src/Platform/Tizen/BorderDrawable.cs new file mode 100644 index 000000000000..915fd337e932 --- /dev/null +++ b/src/Core/src/Platform/Tizen/BorderDrawable.cs @@ -0,0 +1,39 @@ +using Microsoft.Maui.Graphics; + +namespace Microsoft.Maui +{ + public class BorderDrawable : IDrawable + { + Paint _paint; + IBorder _border; + + public BorderDrawable(Paint paint, IBorder border) + { + _paint = paint; + _border = border; + } + + public void Draw(ICanvas canvas, RectangleF dirtyRect) + { + canvas.SaveState(); + + var borderPath = _border.Shape?.PathForBounds(dirtyRect) ?? null; + if (borderPath != null) + { + canvas.MiterLimit = _border.StrokeMiterLimit; + canvas.StrokeColor = _border.Stroke.ToColor(); + canvas.StrokeDashPattern = _border.StrokeDashPattern; + canvas.StrokeLineCap = _border.StrokeLineCap; + canvas.StrokeLineJoin = _border.StrokeLineJoin; + canvas.StrokeSize = (float)_border.StrokeThickness; + + canvas.DrawPath(borderPath); + + canvas.SetFillPaint(_paint, dirtyRect); + canvas.FillPath(borderPath); + } + + canvas.RestoreState(); + } + } +} diff --git a/src/Core/src/Platform/Tizen/BorderView.cs b/src/Core/src/Platform/Tizen/BorderView.cs new file mode 100644 index 000000000000..06c36a54829b --- /dev/null +++ b/src/Core/src/Platform/Tizen/BorderView.cs @@ -0,0 +1,86 @@ +using System; +using ElmSharp; +using Microsoft.Maui.Graphics; +using Microsoft.Maui.Graphics.Skia.Views; +using Tizen.UIExtensions.Common; + +namespace Microsoft.Maui +{ + public class BorderView : ContentCanvas, IWrapperViewCanvas + { + WrapperView _wrapperView; + + public BorderView(EvasObject parent, IView view) : base(parent, view) + { + _wrapperView = new WrapperView(parent); + _wrapperView.Show(); + Children.Add(_wrapperView); + _wrapperView.Lower(); + Content?.RaiseTop(); + + LayoutUpdated += OnLayout; + + } + + public IShape? Clip + { + get + { + return _wrapperView.Clip; + } + set + { + _wrapperView.Clip = value; + } + } + + + public IShadow? Shadow + { + get + { + return _wrapperView.Shadow; + } + set + { + _wrapperView.Shadow = value; + } + } + + public EvasObject? Content + { + get + { + return _wrapperView.Content; + } + set + { + if (_wrapperView.Content != value) + { + if (_wrapperView.Content != null) + { + Children.Remove(_wrapperView); + _wrapperView.Content = null; + } + _wrapperView.Content = value; + if (_wrapperView.Content != null) + { + Children.Add(_wrapperView); + _wrapperView.RaiseTop(); + } + } + _wrapperView.Content = value; + } + } + + public IWrapperViewDrawables Drawables => _wrapperView.Drawables; + + void OnLayout(object? sender, LayoutEventArgs e) + { + if (Content != null) + { + _wrapperView.Geometry = Geometry; + } + } + } +} diff --git a/src/Core/src/Graphics/ShadowDrawable.Tizen.cs b/src/Core/src/Platform/Tizen/ShadowDrawable.cs similarity index 93% rename from src/Core/src/Graphics/ShadowDrawable.Tizen.cs rename to src/Core/src/Platform/Tizen/ShadowDrawable.cs index 7dc746fa5031..62a56575db8b 100644 --- a/src/Core/src/Graphics/ShadowDrawable.Tizen.cs +++ b/src/Core/src/Platform/Tizen/ShadowDrawable.cs @@ -1,4 +1,6 @@ -namespace Microsoft.Maui.Graphics +using Microsoft.Maui.Graphics; + +namespace Microsoft.Maui { public class ShadowDrawable : IDrawable { diff --git a/src/Core/src/Platform/Tizen/StrokeExtensions.cs b/src/Core/src/Platform/Tizen/StrokeExtensions.cs index 4de1ad022e82..c050e19012e6 100644 --- a/src/Core/src/Platform/Tizen/StrokeExtensions.cs +++ b/src/Core/src/Platform/Tizen/StrokeExtensions.cs @@ -3,16 +3,14 @@ namespace Microsoft.Maui { - // TODO : Need to impl public static class StrokeExtensions { public static void UpdateStrokeShape(this EvasObject nativeView, IBorder border) { var borderShape = border.Shape; - //MauiDrawable? background = nativeView.Background as MauiDrawable; - - //if (background == null && borderShape == null) - // return; + var canvas = nativeView as IWrapperViewCanvas; + if (canvas == null && borderShape == null) + return; nativeView.UpdateMauiDrawable(border); } @@ -20,21 +18,19 @@ public static void UpdateStrokeShape(this EvasObject nativeView, IBorder border) public static void UpdateStroke(this EvasObject nativeView, IBorder border) { var stroke = border.Stroke; - //MauiDrawable? background = nativeView.Background as MauiDrawable; - - //if (background == null && stroke.IsNullOrEmpty()) - // return; + var canvas = nativeView as IWrapperViewCanvas; + if (canvas == null && stroke.IsNullOrEmpty()) + return; nativeView.UpdateMauiDrawable(border); } public static void UpdateStrokeThickness(this EvasObject nativeView, IBorder border) { - //MauiDrawable? background = nativeView.Background as MauiDrawable; - //bool hasBorder = border.Shape != null && border.Stroke != null; - - //if (background == null && !hasBorder) - // return; + var canvas = nativeView as IWrapperViewCanvas; + bool hasBorder = border.Shape != null && border.Stroke != null; + if (canvas == null && !hasBorder) + return; nativeView.UpdateMauiDrawable(border); } @@ -42,58 +38,50 @@ public static void UpdateStrokeThickness(this EvasObject nativeView, IBorder bor public static void UpdateStrokeDashPattern(this EvasObject nativeView, IBorder border) { var strokeDashPattern = border.StrokeDashPattern; - //MauiDrawable? background = nativeView.Background as MauiDrawable; - - //bool hasBorder = border.Shape != null && border.Stroke != null; - - //if (background == null && !hasBorder && (strokeDashPattern == null || strokeDashPattern.Length == 0)) - // return; + var canvas = nativeView as IWrapperViewCanvas; + bool hasBorder = border.Shape != null && border.Stroke != null; + if (canvas == null && !hasBorder && (strokeDashPattern == null || strokeDashPattern.Length == 0)) + return; nativeView.UpdateMauiDrawable(border); } public static void UpdateStrokeDashOffset(this EvasObject nativeView, IBorder border) { - //MauiDrawable? background = nativeView.Background as MauiDrawable; - - //bool hasBorder = border.Shape != null && border.Stroke != null; - - //if (background == null && !hasBorder) - // return; + var canvas = nativeView as IWrapperViewCanvas; + bool hasBorder = border.Shape != null && border.Stroke != null; + if (canvas == null && !hasBorder) + return; nativeView.UpdateMauiDrawable(border); } public static void UpdateStrokeMiterLimit(this EvasObject nativeView, IBorder border) { - //MauiDrawable? background = nativeView.Background as MauiDrawable; - - //bool hasBorder = border.Shape != null && border.Stroke != null; - - //if (background == null && !hasBorder) - // return; + var canvas = nativeView as IWrapperViewCanvas; + bool hasBorder = border.Shape != null && border.Stroke != null; + if (canvas == null && !hasBorder) + return; nativeView.UpdateMauiDrawable(border); } public static void UpdateStrokeLineCap(this EvasObject nativeView, IBorder border) { - //MauiDrawable? background = nativeView.Background as MauiDrawable; - //bool hasBorder = border.Shape != null && border.Stroke != null; - - //if (background == null && !hasBorder) - // return; + var canvas = nativeView as IWrapperViewCanvas; + bool hasBorder = border.Shape != null && border.Stroke != null; + if (canvas == null && !hasBorder) + return; nativeView.UpdateMauiDrawable(border); } public static void UpdateStrokeLineJoin(this EvasObject nativeView, IBorder border) { - //MauiDrawable? background = nativeView.Background as MauiDrawable; - //bool hasBorder = border.Shape != null && border.Stroke != null; - - //if (background == null && !hasBorder) - // return; + var canvas = nativeView as IWrapperViewCanvas; + bool hasBorder = border.Shape != null && border.Stroke != null; + if (canvas == null && !hasBorder) + return; nativeView.UpdateMauiDrawable(border); } @@ -101,27 +89,13 @@ public static void UpdateStrokeLineJoin(this EvasObject nativeView, IBorder bord internal static void UpdateMauiDrawable(this EvasObject nativeView, IBorder border) { bool hasBorder = border.Shape != null && border.Stroke != null; - if (!hasBorder) return; - //MauiDrawable? mauiDrawable = nativeView.Background as MauiDrawable; - - //if (mauiDrawable == null) - //{ - // mauiDrawable = new MauiDrawable(nativeView.Context); - - // nativeView.Background = mauiDrawable; - //} - - //mauiDrawable.SetBackground(border.Background); - //mauiDrawable.SetBorderBrush(border.Stroke); - //mauiDrawable.SetBorderWidth(border.StrokeThickness); - //mauiDrawable.SetBorderDash(border.StrokeDashPattern, border.StrokeDashOffset); - //mauiDrawable.SetBorderMiterLimit(border.StrokeMiterLimit); - //mauiDrawable.SetBorderLineJoin(border.StrokeLineJoin); - //mauiDrawable.SetBorderLineCap(border.StrokeLineCap); - //mauiDrawable.SetBorderShape(border.Shape); + if (nativeView is IWrapperViewCanvas canvas) + { + canvas.Drawables.BorderDrawable = border.Background?.ToDrawable(border) ?? null; + } } } } \ No newline at end of file