-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
[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
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
{ | ||
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); | ||
} | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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;