Skip to content

[WASM] Implement Cursors API #7295

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

Merged
merged 3 commits into from
Jan 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 4 additions & 1 deletion src/Web/Avalonia.Web.Blazor/AvaloniaView.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public partial class AvaloniaView : ITextInputMethodImpl
private DpiWatcherInterop _dpiWatcher = null!;
private SKHtmlCanvasInterop.GLInfo? _jsGlInfo = null!;
private InputHelperInterop _inputHelper = null!;
private InputHelperInterop _canvasHelper = null!;
private ElementReference _htmlCanvas;
private ElementReference _inputElement;
private double _dpi;
Expand Down Expand Up @@ -254,9 +255,11 @@ protected override void OnAfterRender(bool firstRender)
Threading.Dispatcher.UIThread.Post(async () =>
{
_inputHelper = await InputHelperInterop.ImportAsync(Js, _inputElement);
_canvasHelper = await InputHelperInterop.ImportAsync(Js, _htmlCanvas);
Copy link
Contributor Author

@Mikolaytis Mikolaytis Jan 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's required to set cursor to _htmlCanvas, because setting cursor to the _inputElement is not working - because it's always display:none;


_inputHelper.Hide();
_inputHelper.SetCursor("default");
_canvasHelper.SetCursor("default");
_topLevelImpl.SetCssCursor = _canvasHelper.SetCursor;

Console.WriteLine("starting html canvas setup");
_interop = await SKHtmlCanvasInterop.ImportAsync(Js, _htmlCanvas, OnRenderFrame);
Expand Down
93 changes: 93 additions & 0 deletions src/Web/Avalonia.Web.Blazor/Cursor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using Avalonia.Input;
using Avalonia.Platform;

namespace Avalonia.Web.Blazor
{
public class CssCursor : ICursorImpl
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this class is public, not internal, to allow end users to use some custom constructors that will allow to use cursors that's is not in the StandardCursorType enum values list

{
public const string Default = "default";
public string? Value { get; set; }

public CssCursor(StandardCursorType type)
{
Value = ToKeyword(type);
}

/// <summary>
/// Create a cursor from base64 image
/// </summary>
public CssCursor(string base64, string format, PixelPoint hotspot, StandardCursorType fallback)
{
Value = $"url(\"data:image/{format};base64,{base64}\") {hotspot.X} {hotspot.Y}, {ToKeyword(fallback)}";
}

/// <summary>
/// Create a cursor from url to *.cur file.
/// </summary>
public CssCursor(string url, StandardCursorType fallback)
{
Value = $"url('{url}'), {ToKeyword(fallback)}";
}

/// <summary>
/// Create a cursor from png/svg and hotspot position
/// </summary>
public CssCursor(string url, PixelPoint hotSpot, StandardCursorType fallback)
{
Value = $"url('{url}') {hotSpot.X} {hotSpot.Y}, {ToKeyword(fallback)}";
}

private static string ToKeyword(StandardCursorType type) => type switch
{
StandardCursorType.Hand => "pointer",
StandardCursorType.Cross => "crosshair",
StandardCursorType.Help => "help",
StandardCursorType.Ibeam => "text",
StandardCursorType.No => "not-allowed",
StandardCursorType.None => "none",
StandardCursorType.Wait => "progress",
StandardCursorType.AppStarting => "wait",

StandardCursorType.DragMove => "move",
StandardCursorType.DragCopy => "copy",
StandardCursorType.DragLink => "alias",

StandardCursorType.UpArrow => "default",/*not found matching one*/
StandardCursorType.SizeWestEast => "ew-resize",
StandardCursorType.SizeNorthSouth => "ns-resize",
StandardCursorType.SizeAll => "move",

StandardCursorType.TopSide => "n-resize",
StandardCursorType.BottomSide => "s-resize",
StandardCursorType.LeftSide => "w-resize",
StandardCursorType.RightSide => "e-resize",
StandardCursorType.TopLeftCorner => "nw-resize",
StandardCursorType.TopRightCorner => "ne-resize",
StandardCursorType.BottomLeftCorner => "sw-resize",
StandardCursorType.BottomRightCorner => "se-resize",

_ => Default,
};

public void Dispose() {}
}

internal class CssCursorFactory : ICursorFactory
{
public ICursorImpl CreateCursor(IBitmapImpl cursor, PixelPoint hotSpot)
{
using var imageStream = new MemoryStream();
cursor.Save(imageStream);

//not memory optimized because CryptoStream with ToBase64Transform is not supported in the browser.
var base64String = Convert.ToBase64String(imageStream.ToArray());
return new CssCursor(base64String, "png", hotSpot, StandardCursorType.Arrow);
}

ICursorImpl ICursorFactory.GetCursor(StandardCursorType cursorType)
{
return new CssCursor(cursorType);
}
}
}

14 changes: 12 additions & 2 deletions src/Web/Avalonia.Web.Blazor/RazorViewTopLevelImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal class RazorViewTopLevelImpl : ITopLevelImplWithTextInputMethod
private readonly Stopwatch _sw = Stopwatch.StartNew();
private readonly ITextInputMethodImpl _textInputMethod;
private readonly TouchDevice _touchDevice;
private string _currentCursor = CssCursor.Default;

public RazorViewTopLevelImpl(ITextInputMethodImpl textInputMethod)
{
Expand Down Expand Up @@ -126,8 +127,16 @@ public void SetInputRoot(IInputRoot inputRoot)

public void SetCursor(ICursorImpl cursor)
{
// nop

var cur = cursor as CssCursor;
var val = CssCursor.Default;
if (cur != null && cur.Value != null)
{
val = cur.Value;
}
if (_currentCursor != val)
{
SetCssCursor?.Invoke(val);
}
}

public IPopupImpl? CreatePopup()
Expand All @@ -146,6 +155,7 @@ public void SetTransparencyLevelHint(WindowTransparencyLevel transparencyLevel)

public IEnumerable<object> Surfaces => new object[] { _currentSurface! };

public Action<string>? SetCssCursor { get; set; }
public Action<RawInputEventArgs>? Input { get; set; }
public Action<Rect>? Paint { get; set; }
public Action<Size, PlatformResizeReason>? Resized { get; set; }
Expand Down
21 changes: 0 additions & 21 deletions src/Web/Avalonia.Web.Blazor/WinStubs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,6 @@ internal class ClipboardStub : IClipboard
public Task<object> GetDataAsync(string format) => Task.FromResult<object>(new ());
}

internal class CursorStub : ICursorImpl
{
public void Dispose()
{

}
}

internal class CursorFactoryStub : ICursorFactory
{
public ICursorImpl CreateCursor(IBitmapImpl cursor, PixelPoint hotSpot)
{
return new CursorStub();
}

ICursorImpl ICursorFactory.GetCursor(StandardCursorType cursorType)
{
return new CursorStub();
}
}

internal class IconLoaderStub : IPlatformIconLoader
{
private class IconStub : IWindowIconImpl
Expand Down
2 changes: 1 addition & 1 deletion src/Web/Avalonia.Web.Blazor/WindowingPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void Register()
s_keyboard = new KeyboardDevice();
AvaloniaLocator.CurrentMutable
.Bind<IClipboard>().ToSingleton<ClipboardStub>()
.Bind<ICursorFactory>().ToSingleton<CursorFactoryStub>()
.Bind<ICursorFactory>().ToSingleton<CssCursorFactory>()
.Bind<IKeyboardDevice>().ToConstant(s_keyboard)
.Bind<IPlatformSettings>().ToConstant(instance)
.Bind<IPlatformThreadingInterface>().ToConstant(instance)
Expand Down