Skip to content

Commit

Permalink
Merge pull request #2533 from cwensley/curtis/mac-fix-possible-nre-wi…
Browse files Browse the repository at this point in the history
…th-mouseenter

Mac: Fix possible NRE when firing MouseEnter/Leave
  • Loading branch information
cwensley committed Jul 25, 2023
2 parents 0efebc6 + 0e22201 commit 193de2d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
16 changes: 8 additions & 8 deletions src/Eto.Mac/Forms/MacView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,20 @@ public void FireMouseLeaveIfNeeded(bool async)
var h = Handler;
if (h == null || !entered) return;
entered = false;
var theEvent = NSApplication.SharedApplication.CurrentEvent;
var args = MacConversions.GetMouseEvent(h, theEvent, false);
if (async)
{
Application.Instance.AsyncInvoke(() =>
{
if (h.Widget.IsDisposed)
return;
var theEvent = NSApplication.SharedApplication.CurrentEvent;
h.Callback.OnMouseLeave(h.Widget, MacConversions.GetMouseEvent(h, theEvent, false));
h.Callback.OnMouseLeave(h.Widget, args);
});
}
else
{
var theEvent = NSApplication.SharedApplication.CurrentEvent;
h.Callback.OnMouseLeave(h.Widget, MacConversions.GetMouseEvent(h, theEvent, false));
h.Callback.OnMouseLeave(h.Widget, args);
}
}

Expand All @@ -80,20 +80,20 @@ public void FireMouseEnterIfNeeded(bool async)
var h = Handler;
if (h == null || entered) return;
entered = true;
var theEvent = NSApplication.SharedApplication.CurrentEvent;
var args = MacConversions.GetMouseEvent(h, theEvent, false);
if (async)
{
Application.Instance.AsyncInvoke(() =>
{
if (h.Widget.IsDisposed)
return;
var theEvent = NSApplication.SharedApplication.CurrentEvent;
h.Callback.OnMouseEnter(h.Widget, MacConversions.GetMouseEvent(h, theEvent, false));
h.Callback.OnMouseEnter(h.Widget, args);
});
}
else
{
var theEvent = NSApplication.SharedApplication.CurrentEvent;
h.Callback.OnMouseEnter(h.Widget, MacConversions.GetMouseEvent(h, theEvent, false));
h.Callback.OnMouseEnter(h.Widget, args);
}
}

Expand Down
29 changes: 21 additions & 8 deletions src/Eto.Mac/MacConversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,28 @@ public static GridCellMouseEventArgs CreateCellMouseEventArgs(GridColumn column,

public static MouseEventArgs GetMouseEvent(IMacViewHandler handler, NSEvent theEvent, bool includeWheel)
{
var view = handler.ContainerControl;
var pt = theEvent.LocationInWindow;
pt = handler.GetAlignmentPointForFramePoint(pt);
Keys modifiers = theEvent.ModifierFlags.ToEto();
MouseButtons buttons = theEvent.GetMouseButtons();
SizeF? delta = null;
if (includeWheel)
delta = new SizeF((float)theEvent.DeltaX, (float)theEvent.DeltaY);
return new MouseEventArgs(buttons, modifiers, pt.ToEto(view), delta);
PointF point;
Keys modifiers;
MouseButtons buttons;
if (theEvent != null)
{
var view = handler.ContainerControl;
var pt = theEvent.LocationInWindow;
pt = handler.GetAlignmentPointForFramePoint(pt);
point = pt.ToEto(view);
if (includeWheel)
delta = new SizeF((float)theEvent.DeltaX, (float)theEvent.DeltaY);
modifiers = theEvent.ModifierFlags.ToEto();
buttons = theEvent.GetMouseButtons();
}
else
{
point = handler.Widget.PointFromScreen(Mouse.Position);
modifiers = Keyboard.Modifiers;
buttons = Mouse.Buttons;
}
return new MouseEventArgs(buttons, modifiers, point, delta);
}

public static MouseButtons GetMouseButtons(this NSEvent theEvent)
Expand Down

0 comments on commit 193de2d

Please sign in to comment.