Skip to content

Commit

Permalink
feat(PointerCursor): Support for WPF & GTK
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Sep 19, 2020
1 parent 272a487 commit eed57c3
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 9 deletions.
49 changes: 49 additions & 0 deletions src/Uno.UI.Runtime.Skia.Gtk/GTK/Extensions/CursorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Gdk;
using Windows.UI.Core;

namespace Uno.UI.Runtime.Skia.GTK.Extensions
{
internal static class CursorExtensions
{
public static CoreCursor ToCoreCursor(this Cursor cursor)
{
CoreCursorType GetCoreCursorType(CursorType? type) =>
type switch
{
CursorType.Arrow => CoreCursorType.Arrow,
CursorType.Cross => CoreCursorType.Cross,
CursorType.Hand1 => CoreCursorType.Hand,
CursorType.Hand2 => CoreCursorType.Hand,
CursorType.QuestionArrow => CoreCursorType.Help,
CursorType.Sizing => CoreCursorType.SizeAll,
CursorType.Watch => CoreCursorType.Wait,
_ => throw new NotImplementedException(),
};

return new CoreCursor(GetCoreCursorType(cursor?.CursorType), 0);
}

public static Cursor ToCursor(this CoreCursor coreCursor)
{
CursorType GetCursorType(CoreCursorType? coreCursorType) =>
coreCursorType switch
{
CoreCursorType.Arrow => CursorType.Arrow,
CoreCursorType.Cross => CursorType.Cross,
CoreCursorType.Hand => CursorType.Hand1,
CoreCursorType.Help => CursorType.QuestionArrow,
CoreCursorType.SizeAll => CursorType.Sizing,
CoreCursorType.Wait => CursorType.Watch,
_ => CursorType.Arrow,
};

return new Cursor(GetCursorType(coreCursor?.Type));
}

}
}
3 changes: 1 addition & 2 deletions src/Uno.UI.Runtime.Skia.Gtk/GTK/GtkHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public GtkHost(Func<WUX.Application> appBuilder, string[] args)

public void Run()
{
Gtk.Application.Init();

Gtk.Application.Init();
ApiExtensibility.Register(typeof(Windows.UI.Core.ICoreWindowExtension), o => new GtkUIElementPointersSupport(o));
ApiExtensibility.Register(typeof(Windows.UI.ViewManagement.IApplicationViewExtension), o => new GtkApplicationViewExtension(o));
ApiExtensibility.Register(typeof(IApplicationExtension), o => new GtkApplicationExtension(o));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Uno.Foundation.Extensibility;
using Uno.Logging;
using Uno.UI.Runtime.Skia;
using Uno.UI.Runtime.Skia.GTK.Extensions;
using Windows.ApplicationModel;
using Windows.Devices.Input;
using Windows.Foundation;
Expand All @@ -26,6 +27,12 @@ public class GtkUIElementPointersSupport : ICoreWindowExtension
private ICoreWindowEvents _ownerEvents;
private static int _currentFrameId;

public CoreCursor PointerCursor
{
get => GtkHost.Window.Window.Cursor.ToCoreCursor();
set => GtkHost.Window.Window.Cursor = value.ToCursor();
}

public GtkUIElementPointersSupport(object owner)
{
_owner = (CoreWindow)owner;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,22 @@ public class TizenUIElementPointersSupport : ICoreWindowExtension
private readonly GestureLayer _gestureLayer;
private readonly UnoCanvas _canvas;

public CoreCursor PointerCursor
{
get => new CoreCursor(CoreCursorType.Arrow, 0);
set
{
// ignored
}
}

public TizenUIElementPointersSupport(object owner, UnoCanvas canvas)
{
_owner = (CoreWindow)owner;
_ownerEvents = (ICoreWindowEvents)owner;
_canvas = canvas;
_displayInformation = DisplayInformation.GetForCurrentView();

_gestureLayer = new GestureLayer(canvas);
_gestureLayer.Attach(canvas);
_gestureLayer.IsEnabled = true;
Expand All @@ -54,7 +63,7 @@ private void SetupTapGesture()
}

private void SetupMomentumGesture()
{
{
_gestureLayer.SetMomentumCallback(GestureLayer.GestureState.Move, OnMove);
}

Expand Down Expand Up @@ -151,7 +160,7 @@ private void OnTapEnd(GestureLayer.TapData data)
}

private Windows.Foundation.Point GetPoint(int x, int y)
{
{
var scale = _displayInformation.LogicalDpi / 160f;
return new Windows.Foundation.Point(x / scale, y / scale);
}
Expand Down
77 changes: 77 additions & 0 deletions src/Uno.UI.Runtime.Skia.Wpf/WPF/Extensions/CursorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System.Windows.Input;
using Windows.UI.Core;

namespace Uno.UI.Skia.Platform.Extensions
{
internal static class CursorExtensions
{
public static CoreCursor ToCoreCursor(this Cursor cursor)
{
CoreCursorType cursorType = CoreCursorType.Arrow;
if (cursor == Cursors.Wait)
{
cursorType = CoreCursorType.Wait;
}
else if (cursor == Cursors.Hand)
{
cursorType = CoreCursorType.Hand;
}
else if (cursor == Cursors.Cross)
{
cursorType = CoreCursorType.Cross;
}
else if (cursor == Cursors.Help)
{
cursorType = CoreCursorType.Help;
}
else if (cursor == Cursors.IBeam)
{
cursorType = CoreCursorType.IBeam;
}
else if (cursor == Cursors.No)
{
cursorType = CoreCursorType.UniversalNo;
}
else if (cursor == Cursors.UpArrow)
{
cursorType = CoreCursorType.UpArrow;
}
else if (cursor == Cursors.SizeNESW)
{
cursorType = CoreCursorType.SizeNortheastSouthwest;
}
else if (cursor == Cursors.SizeNS)
{
cursorType = CoreCursorType.SizeNorthSouth;
}
else if (cursor == Cursors.SizeNWSE)
{
cursorType = CoreCursorType.SizeNorthwestSoutheast;
}
else if (cursor == Cursors.SizeWE)
{
cursorType = CoreCursorType.SizeWestEast;
}
return new CoreCursor(cursorType, 0);
}

public static Cursor ToCursor(this CoreCursor coreCursor) =>
coreCursor?.Type switch
{
CoreCursorType.Arrow => Cursors.Arrow,
CoreCursorType.Cross => Cursors.Cross,
CoreCursorType.Hand => Cursors.Hand,
CoreCursorType.Help => Cursors.Help,
CoreCursorType.IBeam => Cursors.IBeam,
CoreCursorType.SizeAll => Cursors.SizeAll,
CoreCursorType.SizeNortheastSouthwest => Cursors.SizeNESW,
CoreCursorType.SizeNorthSouth => Cursors.SizeNS,
CoreCursorType.SizeNorthwestSoutheast => Cursors.SizeNWSE,
CoreCursorType.SizeWestEast => Cursors.SizeWE,
CoreCursorType.UniversalNo => Cursors.No,
CoreCursorType.UpArrow => Cursors.UpArrow,
CoreCursorType.Wait => Cursors.Wait,
_ => Cursors.Arrow,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using MouseDevice = System.Windows.Input.MouseDevice;
using System.Reflection;
using Windows.System;
using Uno.UI.Skia.Platform.Extensions;

namespace Uno.UI.Skia.Platform
{
Expand All @@ -31,6 +32,12 @@ public class WpfUIElementPointersSupport : ICoreWindowExtension
private const int WM_MOUSEHWHEEL = 0x020E;
private const int WM_DPICHANGED = 0x02E0;

public CoreCursor PointerCursor
{
get => Mouse.OverrideCursor.ToCoreCursor();
set => Mouse.OverrideCursor = value.ToCursor();
}

[Flags]
private enum MouseModifierKeys : int
{
Expand Down
4 changes: 2 additions & 2 deletions src/Uno.UWP/UI/Core/CoreWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public Point PointerPosition
set => _pointerPosition = value;
}

#if !__WASM__ && !__MACOS__
[Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__SKIA__", "__NETSTD_REFERENCE__")]
#if !__WASM__ && !__MACOS__ && !__SKIA__
[Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__NETSTD_REFERENCE__")]
public CoreCursor PointerCursor { get; set; } = new CoreCursor(CoreCursorType.Arrow, 0);
#endif

Expand Down
10 changes: 8 additions & 2 deletions src/Uno.UWP/UI/Core/CoreWindow.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Windows.UI.Core
{
public interface ICoreWindowExtension
{

public CoreCursor PointerCursor { get; set; }
}

public partial class CoreWindow : ICoreWindowEvents
Expand All @@ -21,9 +21,15 @@ public partial class CoreWindow : ICoreWindowEvents
public event TypedEventHandler<CoreWindow, PointerEventArgs> PointerReleased;
public event TypedEventHandler<CoreWindow, PointerEventArgs> PointerWheelChanged;

public CoreCursor PointerCursor
{
get => _coreWindowExtension.PointerCursor ?? new CoreCursor(CoreCursorType.Arrow, 0);
set => _coreWindowExtension.PointerCursor = value;
}

partial void InitializePartial()
{
if(!ApiExtensibility.CreateInstance(this, out _coreWindowExtension))
if (!ApiExtensibility.CreateInstance(this, out _coreWindowExtension))
{
throw new InvalidOperationException($"Unable to find ICoreWindowExtension extension");
}
Expand Down

0 comments on commit eed57c3

Please sign in to comment.