Skip to content

Commit

Permalink
perf(WindowManager): Add SetImageAsMonochrome binding
Browse files Browse the repository at this point in the history
  • Loading branch information
ebariche committed Mar 30, 2023
1 parent 3006ede commit aec6bcc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/Uno.UI/UI/Xaml/Controls/Image/Image.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Uno.Disposables;
using Windows.Storage.Streams;
using System.Runtime.InteropServices;
using Uno.UI.Xaml;
using Uno.UI.Xaml.Media;
using Windows.UI;

Expand Down Expand Up @@ -109,7 +110,7 @@ void OnSourceOpened(ImageData img)
default:
if (MonochromeColor != null)
{
WebAssemblyRuntime.InvokeJS("Uno.UI.WindowManager.current.setImageAsMonochrome(" + _htmlImage.HtmlId + ", \"" + img.Value + "\", \"" + MonochromeColor.Value.ToHexString() + "\");");
WindowManagerInterop.SetImageAsMonochrome(_htmlImage.HtmlId, img.Value, MonochromeColor.Value.ToHexString());
}
else
{
Expand Down
10 changes: 10 additions & 0 deletions src/Uno.UI/UI/Xaml/WindowManagerInterop.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,13 @@ internal static void SelectInputRange(IntPtr htmlId, int start, int length)
WebAssemblyRuntime.InvokeJS($"Uno.UI.WindowManager.current.selectInputRange({htmlId}, {start}, {length})");
#endif

internal static void SetImageAsMonochrome(IntPtr htmlId, string url, string color)
=>
#if NET7_0_OR_GREATER
NativeMethods.SetImageAsMonochrome(htmlId, url, color);
#else
WebAssemblyRuntime.InvokeJS($"Uno.UI.WindowManager.current.setImageAsMonochrome({htmlId}, \"{url}\", \"{color}\");");
#endif
internal static void SetRootElement(IntPtr htmlId)
{
#if NET7_0_OR_GREATER
Expand Down Expand Up @@ -1303,6 +1310,9 @@ internal static partial void ArrangeElement(
[JSImport("globalThis.Uno.UI.WindowManager.current.setElementTransformNativeFast")]
internal static partial void SetElementTransform(IntPtr htmlId, float m11, float m12, float m21, float m22, float m31, float m32);

[JSImport("globalThis.Uno.UI.WindowManager.current.setImageAsMonochrome")]
internal static partial void SetImageAsMonochrome(IntPtr htmlId, string url, string color);

[JSImport("globalThis.Uno.UI.WindowManager.current.setPointerCapture")]
internal static partial void SetPointerCapture(IntPtr htmlId, double pointerId);

Expand Down
4 changes: 1 addition & 3 deletions src/Uno.UI/ts/WindowManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1431,7 +1431,7 @@ namespace Uno.UI {
* @param url the source image
* @param color the color to apply to the monochrome pixels
*/
public setImageAsMonochrome(viewId: number, url: string, color: string): string {
public setImageAsMonochrome(viewId: number, url: string, color: string): void {
const element = this.getView(viewId);

if (element.tagName.toUpperCase() === "IMG") {
Expand All @@ -1458,8 +1458,6 @@ namespace Uno.UI {

imgElement.src = c.toDataURL();
}

return "ok";
}
else {
throw `setImageAsMonochrome: Element id ${viewId} is not an Img.`;
Expand Down

0 comments on commit aec6bcc

Please sign in to comment.