Skip to content

Commit

Permalink
Mac: Fixed sending WM_KILLFOCUS and WM_SETFOCUS. Brings back context …
Browse files Browse the repository at this point in the history
…menus of native text fields and text views.
  • Loading branch information
aconcagua21 committed Mar 19, 2019
1 parent 49c5884 commit 7e01729
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,10 @@ static class NSViewExtensions
{
public static T ClosestParentOfType<T>(this NSView view) where T : NSView
{
while (view != null && !(view is T))
view = view.Superview;
return view as T;
while (null != (view = view?.Superview))
if (view is T t)
return t;
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,49 +35,44 @@ namespace System.Windows.Forms.CocoaInternal
{
class MonoWindow : NSWindow
{
private XplatUICocoa driver;
XplatUICocoa driver;
IntPtr prevFocus;

public MonoWindow(IntPtr handle) : base(handle)
{
}

//[Export ("initWithContentRect:styleMask:backing:defer:"), CompilerGenerated]
}

//[Export ("initWithContentRect:styleMask:backing:defer:"), CompilerGenerated]
internal MonoWindow(NSRect contentRect, NSWindowStyle aStyle, NSBackingStore bufferingType, bool deferCreation, XplatUICocoa driver)
: base(contentRect, aStyle, bufferingType, deferCreation)
{
this.driver = driver;

// Disable tabbing on Sierra until we properly support it

// Disable tabbing on Sierra until we properly support it
var setTabbingModeSelector = new ObjCRuntime.Selector("setTabbingMode:");
if (this.RespondsToSelector(setTabbingModeSelector))
this.SetValueForKey(NSNumber.FromInt32(2), new NSString("tabbingMode"));
}

public override bool MakeFirstResponder(NSResponder aResponder)
{
var prevFirstResponder = FirstResponder;
if (base.MakeFirstResponder(aResponder) && IsKeyWindow && prevFirstResponder != FirstResponder)
{
var prev = XplatUICocoa.GetHandle(prevFirstResponder);
var next = XplatUICocoa.GetHandle(aResponder);
if (prev != IntPtr.Zero)
driver.SendMessage(prev, Msg.WM_KILLFOCUS, next, IntPtr.Zero);
if (next != IntPtr.Zero && FirstResponder == aResponder)
driver.SendMessage(next, Msg.WM_SETFOCUS, prev, IntPtr.Zero);
return true;
var ok = base.MakeFirstResponder(aResponder);
var next = FirstResponder;
if (ok)
{
var c = XplatUICocoa.GetHandle(next);
if (prevFocus != c && !(next is MonoWindow))
{
if (prevFocus != IntPtr.Zero)
driver.SendMessage(prevFocus, Msg.WM_KILLFOCUS, c, IntPtr.Zero);
if (c != IntPtr.Zero)
driver.SendMessage(c, Msg.WM_SETFOCUS, prevFocus, IntPtr.Zero);
}
prevFocus = c;
}
return false;
return ok;
}

public override void EndEditingFor(NSObject anObject)
{
// Commenting this out prevents calling Window.MakeFirstResponder(Window),
// which would cause infinite switching focus back and forth when pressing tab key
// under certain circumstances.

//base.EndEditingFor(anObject);
}

public override bool CanBecomeKeyWindow
{
get
Expand Down

0 comments on commit 7e01729

Please sign in to comment.