Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to choose if use native drawn APIs or SkiaSharp to draw #42

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/AlohaKit.UI.Gallery/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging;
using AlohaKit.UI.Hosting;
using Microsoft.Extensions.Logging;

namespace AlohaKit.UI.Gallery
{
Expand All @@ -7,8 +8,10 @@ public static class MauiProgram
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();

builder
.UseMauiApp<App>();
.ConfigureAlohaKitUI()
.UseMauiApp<App>();

#if DEBUG
builder.Logging.AddDebug();
Expand Down
4 changes: 4 additions & 0 deletions src/AlohaKit.UI/AlohaKit.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@
<Folder Include="Platforms\Windows\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Maui.Graphics.Skia" Version="6.0.501" />
</ItemGroup>

</Project>
2 changes: 0 additions & 2 deletions src/AlohaKit.UI/Controls/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ public interface IElement : IVisualElement
public class Element : VisualElement, IElement
{
IElement _parent;
RectF _childrenBounds;


public Element()
{
Expand Down
77 changes: 77 additions & 0 deletions src/AlohaKit.UI/Controls/SkiaCanvasView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using AlohaKit.UI.Extensions;

namespace AlohaKit.UI
{
class SkiaCanvasViewDrawable : IDrawable
{
readonly SkiaCanvasView _owner;

public SkiaCanvasViewDrawable(SkiaCanvasView owner)
{
_owner = owner;
}

public void Draw(ICanvas canvas, RectF bounds)
{
canvas.SaveState();

_owner.DrawCore(canvas, bounds);

canvas.RestoreState();
}
}

[ContentProperty(nameof(Children))]
public class SkiaCanvasView : SkiaGraphicsView
{
public SkiaCanvasView()
{
Children = new ElementsCollection();

Drawable = new SkiaCanvasViewDrawable(this);

StartInteraction += OnCanvasViewStartInteraction;
}

public ElementsCollection Children { get; internal set; }

internal void DrawCore(ICanvas canvas, RectF bounds)
{
Draw(canvas, bounds);
}

public virtual void Draw(ICanvas canvas, RectF bounds)
{
canvas.SaveState();

canvas.ClipRectangle(bounds);

foreach (var child in Children)
{
if (child.IsVisible)
{
child.Draw(canvas, bounds);
}
}

canvas.RestoreState();
}

void OnCanvasViewStartInteraction(object sender, TouchEventArgs e)
{
var touchPoint = e.Touches[0];

foreach (var child in Children)
{
if (child.IsVisible && child is View view && view.TouchInside(touchPoint))
{
foreach (var gesture in view.GestureRecognizers)
{
if (gesture is TapGestureRecognizer tapGestureRecognizer)
tapGestureRecognizer.SendTapped(view);
}
}
}
}
}
}
15 changes: 15 additions & 0 deletions src/AlohaKit.UI/Extensions/RectFExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace AlohaKit.UI.Extensions
{
public static class RectFExtensions
{
public static bool Contains(this RectF rect, Point point) =>
point.X >= 0 && point.X <= rect.Width &&
point.Y >= 0 && point.Y <= rect.Height;

public static bool ContainsAny(this RectF rect, Point[] points)
=> points.Any(x => rect.Contains(x));

public static bool ContainsAny(this RectF rect, PointF[] points)
=> points.Any(rect.Contains);
}
}
12 changes: 12 additions & 0 deletions src/AlohaKit.UI/Extensions/SkiaGraphicsViewExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#if IOS || MACCATALYST || ANDROID || WINDOWS
namespace AlohaKit.UI.Extensions
{
public static class SkiaGraphicsViewExtensions
{
public static void UpdateDrawable(this PlatformSkiaView PlatformGraphicsView, ISkiaGraphicsView graphicsView)
{
PlatformGraphicsView.Drawable = graphicsView.Drawable;
}
}
}
#endif
17 changes: 17 additions & 0 deletions src/AlohaKit.UI/Handlers/ISkiaGraphicsView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace AlohaKit.UI
{
public interface ISkiaGraphicsView : Microsoft.Maui.IView
{
IDrawable Drawable { get; }

void Invalidate();

void StartHoverInteraction(PointF[] points);
void MoveHoverInteraction(PointF[] points);
void EndHoverInteraction();
void StartInteraction(PointF[] points);
void DragInteraction(PointF[] points);
void EndInteraction(PointF[] points, bool isInsideBounds);
void CancelInteraction();
}
}
34 changes: 34 additions & 0 deletions src/AlohaKit.UI/Handlers/SkiaGraphicsView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace AlohaKit.UI
{
public class SkiaGraphicsView : Microsoft.Maui.Controls.View, ISkiaGraphicsView
{
public IDrawable Drawable { get; set; }

public event EventHandler<TouchEventArgs> StartHoverInteraction;
public event EventHandler<TouchEventArgs> MoveHoverInteraction;
public event EventHandler EndHoverInteraction;
public event EventHandler<TouchEventArgs> StartInteraction;
public event EventHandler<TouchEventArgs> DragInteraction;
public event EventHandler<TouchEventArgs> EndInteraction;
public event EventHandler CancelInteraction;

public void Invalidate()
{
Handler?.Invoke(nameof(IGraphicsView.Invalidate));
}

void ISkiaGraphicsView.CancelInteraction() => CancelInteraction?.Invoke(this, EventArgs.Empty);

void ISkiaGraphicsView.DragInteraction(PointF[] points) => DragInteraction?.Invoke(this, new TouchEventArgs(points, true));

void ISkiaGraphicsView.EndHoverInteraction() => EndHoverInteraction?.Invoke(this, EventArgs.Empty);

void ISkiaGraphicsView.EndInteraction(PointF[] points, bool isInsideBounds) => EndInteraction?.Invoke(this, new TouchEventArgs(points, isInsideBounds));

void ISkiaGraphicsView.StartHoverInteraction(PointF[] points) => StartHoverInteraction?.Invoke(this, new TouchEventArgs(points, true));

void ISkiaGraphicsView.MoveHoverInteraction(PointF[] points) => MoveHoverInteraction?.Invoke(this, new TouchEventArgs(points, true));

void ISkiaGraphicsView.StartInteraction(PointF[] points) => StartInteraction?.Invoke(this, new TouchEventArgs(points, true));
}
}
17 changes: 17 additions & 0 deletions src/AlohaKit.UI/Handlers/SkiaGraphicsViewHandler.Standard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#nullable enable
#if NET && !__IOS__ && !__ANDROID__ && !WINDOWS
using Microsoft.Maui.Handlers;
using System;

namespace AlohaKit.UI
{
public partial class SkiaGraphicsViewHandler : ViewHandler<ISkiaGraphicsView, object>
{
protected override object CreatePlatformView() => throw new NotImplementedException();

public static void MapDrawable(SkiaGraphicsViewHandler handler, ISkiaGraphicsView graphicsView) { }

public static void MapInvalidate(SkiaGraphicsViewHandler handler, ISkiaGraphicsView graphicsView, object? arg) { }
}
}
#endif
44 changes: 44 additions & 0 deletions src/AlohaKit.UI/Handlers/SkiaGraphicsViewHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#nullable enable
using Microsoft.Maui.Handlers;

namespace AlohaKit.UI
{
public partial class SkiaGraphicsViewHandler
{
public static IPropertyMapper<ISkiaGraphicsView, SkiaGraphicsViewHandler> Mapper = new PropertyMapper<ISkiaGraphicsView, SkiaGraphicsViewHandler>(ViewHandler.ViewMapper)
{
[nameof(ISkiaGraphicsView.Drawable)] = MapDrawable
};

public static CommandMapper<ISkiaGraphicsView, SkiaGraphicsViewHandler> CommandMapper = new(ViewHandler.ViewCommandMapper)
{
[nameof(ISkiaGraphicsView.Invalidate)] = MapInvalidate
};

public SkiaGraphicsViewHandler() : base(Mapper, CommandMapper)
{
}

public SkiaGraphicsViewHandler(IPropertyMapper? mapper = null, CommandMapper? commandMapper = null)
: base(mapper ?? Mapper, commandMapper ?? CommandMapper)
{

}

#if IOS || MACCATALYST || ANDROID || WINDOWS
protected override void ConnectHandler(PlatformSkiaView platformView)
{
platformView.Connect(VirtualView);

base.ConnectHandler(platformView);
}

protected override void DisconnectHandler(PlatformSkiaView platformView)
{
platformView.Disconnect();

base.DisconnectHandler(platformView);
}
#endif
}
}
18 changes: 18 additions & 0 deletions src/AlohaKit.UI/Hosting/AppHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using Microsoft.Maui.Handlers;

namespace AlohaKit.UI.Hosting
{
public static class AppHostBuilderExtensions
{
public static MauiAppBuilder ConfigureAlohaKitUI(this MauiAppBuilder builder)
{
builder.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler(typeof(SkiaGraphicsView), typeof(SkiaGraphicsViewHandler));
});

return builder;
}
}
}
Loading