Fix winforms mnemonic handler being called when textbox is focused. (#20091)
* fix winforms pneumonic handler being called when textbox is focused. * use GetService instead
This commit is contained in:
+35
-1
@@ -4,9 +4,12 @@ using System.Drawing;
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.Windows.Forms;
|
||||
using Avalonia.Controls.Embedding;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Input.Raw;
|
||||
using Avalonia.Reactive;
|
||||
using Avalonia.Win32.Interop;
|
||||
using WinFormsControl = System.Windows.Forms.Control;
|
||||
using AvControl = Avalonia.Controls.Control;
|
||||
using WinFormsControl = System.Windows.Forms.Control;
|
||||
|
||||
namespace Avalonia.Win32.Interoperability;
|
||||
|
||||
@@ -18,6 +21,7 @@ public class WinFormsAvaloniaControlHost : WinFormsControl, IMessageFilter
|
||||
{
|
||||
private AvControl? _content;
|
||||
private EmbeddableControlRoot? _root;
|
||||
private IDisposable? _postProcessInputDisposable;
|
||||
|
||||
private IntPtr WindowHandle => _root?.TryGetPlatformHandle()?.Handle ?? IntPtr.Zero;
|
||||
|
||||
@@ -27,6 +31,29 @@ public class WinFormsAvaloniaControlHost : WinFormsControl, IMessageFilter
|
||||
public WinFormsAvaloniaControlHost()
|
||||
{
|
||||
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
|
||||
|
||||
var inputManager = AvaloniaLocator.Current.GetService<IInputManager>();
|
||||
|
||||
if(inputManager != null )
|
||||
{
|
||||
_postProcessInputDisposable = inputManager.PostProcess.Subscribe(new AnonymousObserver<RawInputEventArgs>(PostProcessInput));
|
||||
}
|
||||
}
|
||||
|
||||
private void PostProcessInput(RawInputEventArgs args)
|
||||
{
|
||||
if (_root?.IsKeyboardFocusWithin == false)
|
||||
return;
|
||||
|
||||
if(!args.Handled && args is RawTextInputEventArgs textArgs)
|
||||
{
|
||||
var text = textArgs.Text;
|
||||
|
||||
if (!string.IsNullOrEmpty(text))
|
||||
{
|
||||
args.Handled = this.ProcessDialogChar(text[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -89,6 +116,8 @@ public class WinFormsAvaloniaControlHost : WinFormsControl, IMessageFilter
|
||||
_root?.Dispose();
|
||||
_root = null;
|
||||
}
|
||||
_postProcessInputDisposable?.Dispose();
|
||||
_postProcessInputDisposable = null;
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
@@ -140,6 +169,11 @@ public class WinFormsAvaloniaControlHost : WinFormsControl, IMessageFilter
|
||||
}
|
||||
}
|
||||
|
||||
protected override bool IsInputChar(char charCode)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool PreFilterMessage(ref Message m)
|
||||
{
|
||||
var message = (UnmanagedMethods.WindowsMessage)m.Msg;
|
||||
|
||||
Reference in New Issue
Block a user