Skip to content

Commit

Permalink
[Tizen] Add BorderDrawable (dotnet#224)
Browse files Browse the repository at this point in the history
* Move to platform specific

* Fix border handler for Tizen

* Rename ToDrawable method

* Fix border content layout

* Fix BorderView

* Apply rebase
  • Loading branch information
sung-su authored and rookiejava committed Dec 7, 2021
1 parent 312137f commit 27ba1af
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 69 deletions.
5 changes: 5 additions & 0 deletions src/Core/src/Graphics/PaintExtensions.Tizen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
9 changes: 4 additions & 5 deletions src/Core/src/Handlers/Border/BorderHandler.Tizen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

namespace Microsoft.Maui.Handlers
{
public partial class BorderHandler : ViewHandler<IBorder, ContentCanvas>
public partial class BorderHandler : ViewHandler<IBorder, BorderView>
{
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
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Handlers/View/ViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public virtual bool NeedsContainer
{
get
{
#if WINDOWS || TIZEN
#if WINDOWS
if(VirtualView is IBorder border)
return border?.Shape != null || border?.Stroke != null;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Microsoft.Maui.Graphics
using Microsoft.Maui.Graphics;

namespace Microsoft.Maui
{
public class BackgroundDrawable : IDrawable
{
Expand Down Expand Up @@ -31,4 +33,4 @@ public void Draw(ICanvas canvas, RectangleF dirtyRect)
canvas.RestoreState();
}
}
}
}
39 changes: 39 additions & 0 deletions src/Core/src/Platform/Tizen/BorderDrawable.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
86 changes: 86 additions & 0 deletions src/Core/src/Platform/Tizen/BorderView.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Microsoft.Maui.Graphics
using Microsoft.Maui.Graphics;

namespace Microsoft.Maui
{
public class ShadowDrawable : IDrawable
{
Expand Down
94 changes: 34 additions & 60 deletions src/Core/src/Platform/Tizen/StrokeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,125 +3,99 @@

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);
}

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);
}

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);
}

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;
}
}
}
}

0 comments on commit 27ba1af

Please sign in to comment.