Skip to content

Commit

Permalink
Mac: Fix setting tracking rectangles for mouse enter/move/leave events
Browse files Browse the repository at this point in the history
This is a regression on Monterey (caused by #2505) where the bounds of the tracking rectangles aren't set up correctly since the visibleRect of views can now be larger than the bounds of the control itself.
  • Loading branch information
Local Admin authored and Local Admin committed Jun 28, 2023
1 parent e5216b6 commit c94005e
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/Eto.Mac/Forms/MacView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public interface IMacViewHandler : IMacControlHandler
bool TriggerMouseCallback();
MouseEventArgs TriggerMouseDown(NSObject obj, IntPtr sel, NSEvent theEvent);
MouseEventArgs TriggerMouseUp(NSObject obj, IntPtr sel, NSEvent theEvent);
void UpdateTrackingAreas();
}

static partial class MacView
Expand Down Expand Up @@ -176,6 +177,7 @@ static partial class MacView
public static readonly IntPtr selPerformZoom = Selector.GetHandle("performZoom:");
public static readonly IntPtr selArrangeInFront = Selector.GetHandle("arrangeInFront:");
public static readonly IntPtr selPerformMiniaturize = Selector.GetHandle("performMiniaturize:");
public static readonly IntPtr selUpdateTrackingAreas = Selector.GetHandle("updateTrackingAreas");
public static readonly Dictionary<string, IntPtr> systemActionSelectors = new Dictionary<string, IntPtr>
{
{ "cut", selCut },
Expand Down Expand Up @@ -208,6 +210,20 @@ static partial class MacView
// before 10.12, we have to call base.Layout() AFTER we do our layout otherwise it doesn't work correctly..
// however, that causes (temporary) glitches when resizing especially with Scrollable >= 10.12
public static readonly bool NewLayout = MacVersion.IsAtLeast(10, 12);

internal static MarshalDelegates.Action_IntPtr_IntPtr TriggerUpdateTrackingAreas_Delegate = TriggerUpdateTrackingAreas;
static void TriggerUpdateTrackingAreas(IntPtr sender, IntPtr sel)
{
var obj = Runtime.GetNSObject(sender);

Messaging.void_objc_msgSendSuper(obj.SuperHandle, sel);

if (MacBase.GetHandler(obj) is IMacViewHandler handler)
{
handler.UpdateTrackingAreas();
}
}

internal static MarshalDelegates.Action_IntPtr_IntPtr_IntPtr TriggerMouseDragged_Delegate = TriggerMouseDragged;
static void TriggerMouseDragged(IntPtr sender, IntPtr sel, IntPtr e)
{
Expand Down Expand Up @@ -636,7 +652,6 @@ public virtual Size Size
if (oldFrame.Size != newFrame.Size)
Callback.OnSizeChanged(Widget, EventArgs.Empty);

CreateTracking();
InvalidateMeasure();
}
}
Expand Down Expand Up @@ -722,8 +737,8 @@ public virtual SizeF GetPreferredSize(SizeF availableSize)

return size;
}

void CreateTracking()
public virtual void UpdateTrackingAreas()
{
if (!mouseMove)
return;
Expand All @@ -742,8 +757,6 @@ void CreateTracking()
frame = GetAlignmentRectForFrame(frame);

var options = mouseOptions | NSTrackingAreaOptions.ActiveAlways | NSTrackingAreaOptions.EnabledDuringMouseDrag;
if (!UseAlignmentFrame)
options |= NSTrackingAreaOptions.InVisibleRect;

tracking = new NSTrackingArea(frame, options, mouseDelegate, null);
EventControl.AddTrackingArea(tracking);
Expand All @@ -768,14 +781,14 @@ public override void AttachEvent(string id)
case Eto.Forms.Control.MouseLeaveEvent:
mouseOptions |= NSTrackingAreaOptions.MouseEnteredAndExited;
mouseMove = true;
HandleEvent(Eto.Forms.Control.SizeChangedEvent);
CreateTracking();
AddMethod(MacView.selUpdateTrackingAreas, MacView.TriggerUpdateTrackingAreas_Delegate, "v@:@", EventControl);
EventControl.UpdateTrackingAreas();
break;
case Eto.Forms.Control.MouseMoveEvent:
mouseOptions |= NSTrackingAreaOptions.MouseMoved;
mouseMove = true;
HandleEvent(Eto.Forms.Control.SizeChangedEvent);
CreateTracking();
AddMethod(MacView.selUpdateTrackingAreas, MacView.TriggerUpdateTrackingAreas_Delegate, "v@:@", EventControl);
EventControl.UpdateTrackingAreas();
AddMethod(MacView.selMouseDragged, MacView.TriggerMouseDragged_Delegate, "v@:@");
AddMethod(MacView.selRightMouseDragged, MacView.TriggerMouseDragged_Delegate, "v@:@");
AddMethod(MacView.selOtherMouseDragged, MacView.TriggerMouseDragged_Delegate, "v@:@");
Expand Down Expand Up @@ -896,7 +909,6 @@ public bool TriggerMouseCallback()

public virtual void OnSizeChanged(EventArgs e)
{
CreateTracking();
}

public virtual void Invalidate(bool invalidateChildren)
Expand Down

0 comments on commit c94005e

Please sign in to comment.