Skip to content
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

Mac: Fix MouseUp on many controls that use event loops with MouseDown. #1991

Merged
merged 1 commit into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions src/Eto.Mac/Forms/Controls/ColorPickerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,6 @@ public override void Activate(bool exclusive)
if (handler != null)
{
NSColorPanel.SharedColorPanel.ShowsAlpha = handler.AllowAlpha;
handler.TriggerMouseCallback();
}
}

public override void Deactivate()
{
base.Deactivate();
var handler = Handler;
if (handler != null)
{
handler.TriggerMouseCallback();
}
}

Expand Down
13 changes: 11 additions & 2 deletions src/Eto.Mac/Forms/Controls/GridViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@ public override void RightMouseDown(NSEvent theEvent)
if (HandleMouseEvent(theEvent))
return;
base.RightMouseDown(theEvent);
Handler?.TriggerMouseCallback();
}

public override void OtherMouseDown(NSEvent theEvent)
{
if (HandleMouseEvent(theEvent))
return;
base.OtherMouseDown(theEvent);
Handler?.TriggerMouseCallback();
}

public override void MouseDown(NSEvent theEvent)
Expand All @@ -97,6 +99,7 @@ public override void MouseDown(NSEvent theEvent)
h.IsMouseDragging = true;
base.MouseDown(theEvent);
h.IsMouseDragging = false;
h.TriggerMouseCallback();
}

bool HandleMouseEvent(NSEvent theEvent)
Expand All @@ -106,11 +109,17 @@ bool HandleMouseEvent(NSEvent theEvent)
{
var args = MacConversions.GetMouseEvent(handler, theEvent, false);
if (theEvent.ClickCount >= 2)
{
handler.Callback.OnMouseDoubleClick(handler.Widget, args);
if (args.Handled)
return false;
}
else
{
handler.Callback.OnMouseDown(handler.Widget, args);
if (args.Handled)
return true;
if (args.Handled)
return true;
}

var point = ConvertPointFromView(theEvent.LocationInWindow, null);

Expand Down
10 changes: 9 additions & 1 deletion src/Eto.Mac/Forms/Controls/GroupBoxHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ namespace Eto.Mac.Forms.Controls
public class GroupBoxHandler : MacPanel<NSBox, GroupBox, GroupBox.ICallback>, GroupBox.IHandler
{
SizeF? borderSize;

/// <summary>
/// Use a separate class so it doesn't get event methods added
/// </summary>
public class EtoContentView : MacPanelView
{

}

public class EtoBox : NSBox, IMacControl
{
Expand All @@ -53,7 +61,7 @@ public EtoBox(GroupBoxHandler handler)
{
Title = string.Empty;
TitlePosition = NSTitlePosition.NoTitle;
ContentView = new MacPanelView { Handler = handler };
ContentView = new EtoContentView { Handler = handler };
}
}

Expand Down
19 changes: 11 additions & 8 deletions src/Eto.Mac/Forms/Controls/LinkButtonHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,21 +145,24 @@ public override void AttachEvent(string id)
switch (id)
{
case LinkButton.ClickEvent:
Widget.MouseDown += (sender, e) =>
{
if (Enabled && e.Buttons == MouseButtons.Primary)
{
Callback.OnClick(Widget, EventArgs.Empty);
e.Handled = true;
}
};
HandleEvent(LinkButton.MouseUpEvent);
break;
default:
base.AttachEvent(id);
break;
}
}

public override MouseEventArgs TriggerMouseUp(NSObject obj, IntPtr sel, NSEvent theEvent)
{
var args = base.TriggerMouseUp(obj, sel, theEvent);
if (!args.Handled && Enabled && args.Buttons == MouseButtons.Primary)
{
Callback.OnClick(Widget, EventArgs.Empty);
}
return args;
}

static readonly object DisabledTextColorKey = new object();

public Color DisabledTextColor
Expand Down
6 changes: 5 additions & 1 deletion src/Eto.Mac/Forms/Controls/PanelHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ namespace Eto.Mac.Forms.Controls
{
public class PanelHandler : MacPanel<NSView, Panel, Panel.ICallback>, Panel.IHandler
{
protected override NSView CreateControl() => new MacPanelView();
public class EtoPanelView : MacPanelView
{
}

protected override NSView CreateControl() => new EtoPanelView();

public override NSView ContainerControl => Control;
}
Expand Down
23 changes: 22 additions & 1 deletion src/Eto.Mac/Forms/Controls/TextBoxHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public class EtoTextField : NSTextField, IMacControl, ITextBoxWithMaxLength
{
public WeakReference WeakHandler { get; set; }

IMacViewHandler Handler => WeakHandler.Target as IMacViewHandler;
IMacText TextHandler => WeakHandler.Target as IMacText;
ITextBoxWithMaxLength MaxLengthHandler => WeakHandler.Target as ITextBoxWithMaxLength;

Expand Down Expand Up @@ -145,12 +146,32 @@ public void TextViewDidChangeSelection(NSNotification notification)

public override void MouseDown(NSEvent theEvent)
{
base.MouseDown(theEvent);
var h = TextHandler;
if (h != null && h.AutoSelectMode == AutoSelectMode.Always && CurrentEditor?.SelectedRange.Length == 0)
{
CurrentEditor?.SelectAll(this);
}

var handler = Handler;
if (handler == null)
return;
var args = MacConversions.GetMouseEvent(handler, theEvent, false);
if (theEvent.ClickCount >= 2)
handler.Callback.OnMouseDoubleClick(handler.Widget, args);

if (!args.Handled)
{
handler.Callback.OnMouseDown(handler.Widget, args);
}
if (!args.Handled)
{
handler.SuppressMouseEvents++;
base.MouseDown(theEvent);
handler.SuppressMouseEvents--;

// some controls use event loops until mouse up, so we need to trigger the mouse up here.
handler.TriggerMouseCallback();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Eto.Mac/Forms/Controls/TreeGridViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ public override void MouseDown(NSEvent theEvent)
handler.IsMouseDragging = false;

// NSOutlineView uses an event loop and MouseUp() does not get called
handler.Callback.OnMouseUp(handler.Widget, args);
handler.TriggerMouseCallback();

return;
}
Expand Down
32 changes: 22 additions & 10 deletions src/Eto.Mac/Forms/MacFieldEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public MacFieldEditor(IntPtr handle)

public IMacControl MacControl => WeakDelegate as IMacControl;
public object Handler => MacControl?.WeakHandler?.Target;

public IMacViewHandler MacViewHandler => Handler as IMacViewHandler;

WeakReference IMacControl.WeakHandler
{
Expand All @@ -69,12 +71,19 @@ public override void FlagsChanged(NSEvent theEvent)
base.FlagsChanged(theEvent);
}

bool MouseDownEvent(NSEvent theEvent)
void MouseDownEvent(NSEvent theEvent, Action<NSEvent> baseMethod)
{
var handler = Handler as IMacViewHandler;
if (handler == null)
return false;
return;

if (handler.SuppressMouseEvents > 0)
{
// we can get called from a MouseDown from the owning object
baseMethod(theEvent);
return;
}

var args = MacConversions.GetMouseEvent(handler, theEvent, false);
if (theEvent.ClickCount >= 2)
handler.Callback.OnMouseDoubleClick(handler.Widget, args);
Expand All @@ -83,8 +92,14 @@ bool MouseDownEvent(NSEvent theEvent)
{
handler.Callback.OnMouseDown(handler.Widget, args);
}

if (!args.Handled)
{
baseMethod(theEvent);

return args.Handled;
// trigger mouse up here, if needed
handler.TriggerMouseCallback();
}
}

bool MouseUpEvent(NSEvent theEvent)
Expand All @@ -100,20 +115,18 @@ bool MouseUpEvent(NSEvent theEvent)

public override void MouseDown(NSEvent theEvent)
{
if (!MouseDownEvent(theEvent))
base.MouseDown(theEvent);
MouseDownEvent(theEvent, base.MouseDown);
}

public override void MouseUp(NSEvent theEvent)
{
if (!MouseUpEvent(theEvent))
base.MouseUp(theEvent);
}

public override void RightMouseDown(NSEvent theEvent)
{
if (!MouseDownEvent(theEvent))
base.RightMouseDown(theEvent);
MouseDownEvent(theEvent, base.RightMouseDown);
}

public override void RightMouseUp(NSEvent theEvent)
Expand All @@ -124,8 +137,7 @@ public override void RightMouseUp(NSEvent theEvent)

public override void OtherMouseDown(NSEvent theEvent)
{
if (!MouseDownEvent(theEvent))
base.OtherMouseDown(theEvent);
MouseDownEvent(theEvent, base.OtherMouseDown);
}

public override void OtherMouseUp(NSEvent theEvent)
Expand Down
Loading