Browse Source

AutoCompleteBox don't lose text selection when contextmenu opens (#17462)

* fix: 17453 don't lose text selection when contextmenu opens on another control

* fix: 17453 don't effect other callers and move the check to FocusChanged method
pull/17483/head
Jan Karger ツ ☀ 1 year ago
committed by GitHub
parent
commit
0fdd84f594
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 6
      src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.cs
  2. 58
      tests/Avalonia.Controls.UnitTests/AutoCompleteBoxTests.cs

6
src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.cs

@ -812,7 +812,11 @@ namespace Avalonia.Controls
}
_userCalledPopulate = false;
ClearTextBoxSelection();
if (ContextMenu is not { IsOpen: true })
{
ClearTextBoxSelection();
}
}
_isFocused = hasFocus;

58
tests/Avalonia.Controls.UnitTests/AutoCompleteBoxTests.cs

@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Controls.Primitives;
@ -9,7 +9,10 @@ using Avalonia.UnitTests;
using Xunit;
using System.Collections.ObjectModel;
using System.Reactive.Subjects;
using Avalonia.Headless;
using Avalonia.Input;
using Avalonia.Platform;
using Moq;
namespace Avalonia.Controls.UnitTests
{
@ -520,6 +523,42 @@ namespace Avalonia.Controls.UnitTests
});
}
[Fact]
public void Opening_Context_Menu_Does_not_Lose_Selection()
{
using (UnitTestApplication.Start(FocusServices))
{
var target1 = CreateControl();
target1.ContextMenu = new TestContextMenu();
var textBox1 = GetTextBox(target1);
textBox1.Text = "1234";
var target2 = CreateControl();
var textBox2 = GetTextBox(target2);
textBox2.Text = "5678";
var sp = new StackPanel();
sp.Children.Add(target1);
sp.Children.Add(target2);
target1.ApplyTemplate();
target2.ApplyTemplate();
var root = new TestRoot() { Child = sp };
textBox1.SelectionStart = 0;
textBox1.SelectionEnd = 3;
target1.Focus();
Assert.False(target2.IsFocused);
Assert.True(target1.IsFocused);
target2.Focus();
Assert.Equal("123", textBox1.SelectedText);
}
}
/// <summary>
/// Retrieves a defined predicate filter through a new AutoCompleteBox
/// control instance.
@ -1198,5 +1237,22 @@ namespace Avalonia.Controls.UnitTests
return panel;
});
}
private static TestServices FocusServices => TestServices.MockThreadingInterface.With(
focusManager: new FocusManager(),
keyboardDevice: () => new KeyboardDevice(),
keyboardNavigation: () => new KeyboardNavigationHandler(),
inputManager: new InputManager(),
standardCursorFactory: Mock.Of<ICursorFactory>(),
textShaperImpl: new HeadlessTextShaperStub(),
fontManagerImpl: new HeadlessFontManagerStub());
private class TestContextMenu : ContextMenu
{
public TestContextMenu()
{
IsOpen = true;
}
}
}
}

Loading…
Cancel
Save