Skip to content

Commit

Permalink
Refactor WrapperView to draw drawable features (#186)
Browse files Browse the repository at this point in the history
* Refactor WrapperView to draw drawable features

* Update class names and devide files

* Override NeesContainer to remove duplicated code

* Update class names
  • Loading branch information
JoonghyunCho authored and rookiejava committed Jan 27, 2022
1 parent 1d0c27c commit 8929b2d
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 17 deletions.
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 @@ -94,7 +94,7 @@ public virtual bool NeedsContainer
{
get
{
#if WINDOWS
#if WINDOWS || TIZEN
if(VirtualView is IBorderView border)
return border?.Shape != null || border?.Stroke != null;

Expand Down
6 changes: 6 additions & 0 deletions src/Core/src/Handlers/View/ViewHandlerOfT.Tizen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public abstract partial class ViewHandler<TVirtualView, TNativeView> : INativeVi
Dispose(disposing: false);
}

public override bool NeedsContainer =>
VirtualView?.Background != null ||
VirtualView?.Clip != null ||
VirtualView?.Shadow != null ||
base.NeedsContainer;

public override void NativeArrange(Rectangle frame)
{
if (NativeParent == null)
Expand Down
8 changes: 8 additions & 0 deletions src/Core/src/Platform/Tizen/IWrapperViewCanvas.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

namespace Microsoft.Maui
{
public interface IWrapperViewCanvas
{
public IWrapperViewDrawables Drawables { get; }
}
}
13 changes: 13 additions & 0 deletions src/Core/src/Platform/Tizen/IWrapperViewDrawables.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.Maui.Graphics;

namespace Microsoft.Maui
{
public interface IWrapperViewDrawables : IDrawable
{
IDrawable? ShadowDrawable { get; set; }

IDrawable? BackgroundDrawable { get; set; }

IDrawable? BorderDrawable { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/Core/src/Platform/Tizen/ViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public static bool ToNativeVisibility(this Visibility visibility)

public static void UpdateBackground(this EvasObject nativeView, IView view)
{
if (nativeView is IBackgroundCanvas canvas)
if (nativeView is IWrapperViewCanvas canvas)
{
canvas.BackgroundCanvas.Drawable = view.Background?.ToDrawable() ?? null;
canvas.Drawables.BackgroundDrawable = view.Background?.ToDrawable() ?? null;
}
else
{
Expand Down
35 changes: 21 additions & 14 deletions src/Core/src/Platform/Tizen/WrapperView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@

namespace Microsoft.Maui
{
public interface IBackgroundCanvas
public partial class WrapperView : Canvas, IWrapperViewCanvas
{
public SkiaGraphicsView BackgroundCanvas { get; }
}

public partial class WrapperView : Canvas, IBackgroundCanvas
{
Lazy<SkiaGraphicsView> _backgroundCanvas;
Lazy<SkiaGraphicsView> _drawableCanvas;
Lazy<SKClipperView> _clipperView;
EvasObject? _content;

public WrapperView(EvasObject parent) : base(parent)
{
_backgroundCanvas = new Lazy<SkiaGraphicsView>(() =>
_drawableCanvas = new Lazy<SkiaGraphicsView>(() =>
{
var view = new SkiaGraphicsView(parent);
var _drawables = new WrapperViewDrawables();
_drawables.Invalidated += (s, e) =>
{
view.Invalidate();
};
view.Drawable = _drawables;
view.Show();
Children.Add(view);
view.Lower();
Expand Down Expand Up @@ -74,22 +75,22 @@ void OnClipPaint(object? sender, DrawClipEventArgs e)

canvas.FillPath(clipPath);
Content?.SetClipperCanvas(_clipperView.Value);
if (_backgroundCanvas.IsValueCreated)
if (_drawableCanvas.IsValueCreated)
{
BackgroundCanvas.SetClipperCanvas(_clipperView.Value);
_drawableCanvas.Value.SetClipperCanvas(_clipperView.Value);
}
}

void OnLayout(object? sender, Tizen.UIExtensions.Common.LayoutEventArgs e)
void OnLayout(object? sender, LayoutEventArgs e)
{
if (Content != null)
{
Content.Geometry = Geometry;
}

if (_backgroundCanvas.IsValueCreated)
if (_drawableCanvas.IsValueCreated)
{
_backgroundCanvas.Value.Geometry = Geometry;
_drawableCanvas.Value.Geometry = Geometry;
}

if (_clipperView.IsValueCreated)
Expand Down Expand Up @@ -122,7 +123,13 @@ public EvasObject? Content

}

public SkiaGraphicsView BackgroundCanvas => _backgroundCanvas.Value;
public IWrapperViewDrawables Drawables
{
get
{
return (_drawableCanvas.Value.Drawable as IWrapperViewDrawables)!;
}
}
}

public class DrawClipEventArgs : EventArgs
Expand Down
65 changes: 65 additions & 0 deletions src/Core/src/Platform/Tizen/WrapperViewDrawables.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using Microsoft.Maui.Graphics;

namespace Microsoft.Maui
{
public class WrapperViewDrawables : IWrapperViewDrawables
{
public event EventHandler? Invalidated;

IDrawable? _shadowDrawable;
IDrawable? _backgroundDrawable;
IDrawable? _borderDrawable;

public IDrawable? ShadowDrawable
{
get
{
return _shadowDrawable;
}
set
{
_shadowDrawable = value;
SendInvalidated();
}
}

public IDrawable? BackgroundDrawable
{
get
{
return _backgroundDrawable;
}
set
{
_backgroundDrawable = value;
SendInvalidated();
}
}

public IDrawable? BorderDrawable
{
get
{
return _borderDrawable;
}
set
{
_borderDrawable = value;
SendInvalidated();
}
}

public void Draw(ICanvas canvas, RectangleF dirtyRect)
{
_shadowDrawable?.Draw(canvas, dirtyRect);
_backgroundDrawable?.Draw(canvas, dirtyRect);
_borderDrawable?.Draw(canvas, dirtyRect);
}

public void SendInvalidated()
{
Invalidated?.Invoke(this, EventArgs.Empty);
}
}
}

0 comments on commit 8929b2d

Please sign in to comment.