3 changed files with 132 additions and 2 deletions
@ -0,0 +1,35 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
using Avalonia.Input; |
||||
|
using Avalonia.Input.Platform; |
||||
|
|
||||
|
namespace Avalonia.Web.Blazor |
||||
|
{ |
||||
|
internal class ClipboardImpl : IClipboard |
||||
|
{ |
||||
|
public async Task<string> GetTextAsync() |
||||
|
{ |
||||
|
throw new NotImplementedException(); |
||||
|
//return await AvaloniaLocator.Current.GetRequiredService<IJSInProcessRuntime>().
|
||||
|
// InvokeAsync<string>("navigator.clipboard.readText");
|
||||
|
} |
||||
|
|
||||
|
public async Task SetTextAsync(string text) |
||||
|
{ |
||||
|
throw new NotImplementedException(); |
||||
|
//await AvaloniaLocator.Current.GetRequiredService<IJSInProcessRuntime>().
|
||||
|
// InvokeAsync<string>("navigator.clipboard.writeText",text);
|
||||
|
} |
||||
|
|
||||
|
public async Task ClearAsync() => await SetTextAsync(""); |
||||
|
|
||||
|
public Task SetDataObjectAsync(IDataObject data) => Task.CompletedTask; |
||||
|
|
||||
|
public Task<string[]> GetFormatsAsync() => Task.FromResult(Array.Empty<string>()); |
||||
|
|
||||
|
public Task<object> GetDataAsync(string format) => Task.FromResult<object>(new()); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,95 @@ |
|||||
|
using System; |
||||
|
using System.IO; |
||||
|
using Avalonia.Input; |
||||
|
using Avalonia.Platform; |
||||
|
|
||||
|
namespace Avalonia.Web.Blazor |
||||
|
{ |
||||
|
public class CssCursor : ICursorImpl |
||||
|
{ |
||||
|
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); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
Loading…
Reference in new issue