Skip to content

Commit

Permalink
Merge pull request AvaloniaUI#8637 from AvaloniaUI/use-correct-toggle…
Browse files Browse the repository at this point in the history
…Modifier-for-listBox-on-macOS-with-multiselection

Use correct ToggleModifier in ListBox multiselection on MacOS.
  • Loading branch information
grokys committed Aug 3, 2022
1 parent fcada17 commit fb607cc
Show file tree
Hide file tree
Showing 4 changed files with 424 additions and 343 deletions.
3 changes: 2 additions & 1 deletion src/Avalonia.Controls/ListBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Avalonia.Controls.Selection;
using Avalonia.Controls.Templates;
using Avalonia.Input;
using Avalonia.Input.Platform;
using Avalonia.VisualTree;

namespace Avalonia.Controls
Expand Down Expand Up @@ -157,7 +158,7 @@ protected override void OnPointerPressed(PointerPressedEventArgs e)
e.Source,
true,
e.KeyModifiers.HasAllFlags(KeyModifiers.Shift),
e.KeyModifiers.HasAllFlags(KeyModifiers.Control),
e.KeyModifiers.HasAllFlags(AvaloniaLocator.Current.GetRequiredService<PlatformHotkeyConfiguration>().CommandModifiers),
point.Properties.IsRightButtonPressed);
}
}
Expand Down
133 changes: 77 additions & 56 deletions tests/Avalonia.Controls.UnitTests/ListBoxTests_Single.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
using Avalonia.Controls.Templates;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Input.Platform;
using Avalonia.LogicalTree;
using Avalonia.Styling;
using Avalonia.UnitTests;
using Avalonia.VisualTree;
using Moq;
using Xunit;

namespace Avalonia.Controls.UnitTests
Expand Down Expand Up @@ -60,104 +62,123 @@ public void Focusing_Item_With_Arrow_Key_Should_Select_It()
[Fact]
public void Clicking_Item_Should_Select_It()
{
var target = new ListBox
using (UnitTestApplication.Start())
{
Template = new FuncControlTemplate(CreateListBoxTemplate),
Items = new[] { "Foo", "Bar", "Baz " },
};

ApplyTemplate(target);
_mouse.Click(target.Presenter.Panel.Children[0]);

Assert.Equal(0, target.SelectedIndex);
var target = new ListBox
{
Template = new FuncControlTemplate(CreateListBoxTemplate),
Items = new[] { "Foo", "Bar", "Baz " },
};
AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
ApplyTemplate(target);
_mouse.Click(target.Presenter.Panel.Children[0]);

Assert.Equal(0, target.SelectedIndex);
}
}

[Fact]
public void Clicking_Selected_Item_Should_Not_Deselect_It()
{
var target = new ListBox
using (UnitTestApplication.Start())
{
Template = new FuncControlTemplate(CreateListBoxTemplate),
Items = new[] { "Foo", "Bar", "Baz " },
};

ApplyTemplate(target);
target.SelectedIndex = 0;
var target = new ListBox
{
Template = new FuncControlTemplate(CreateListBoxTemplate),
Items = new[] { "Foo", "Bar", "Baz " },
};
AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
ApplyTemplate(target);
target.SelectedIndex = 0;

_mouse.Click(target.Presenter.Panel.Children[0]);
_mouse.Click(target.Presenter.Panel.Children[0]);

Assert.Equal(0, target.SelectedIndex);
Assert.Equal(0, target.SelectedIndex);
}
}

[Fact]
public void Clicking_Item_Should_Select_It_When_SelectionMode_Toggle()
{
var target = new ListBox
using (UnitTestApplication.Start())
{
Template = new FuncControlTemplate(CreateListBoxTemplate),
Items = new[] { "Foo", "Bar", "Baz " },
SelectionMode = SelectionMode.Single | SelectionMode.Toggle,
};

ApplyTemplate(target);
var target = new ListBox
{
Template = new FuncControlTemplate(CreateListBoxTemplate),
Items = new[] { "Foo", "Bar", "Baz " },
SelectionMode = SelectionMode.Single | SelectionMode.Toggle,
};
AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
ApplyTemplate(target);

_mouse.Click(target.Presenter.Panel.Children[0]);
_mouse.Click(target.Presenter.Panel.Children[0]);

Assert.Equal(0, target.SelectedIndex);
Assert.Equal(0, target.SelectedIndex);
}
}

[Fact]
public void Clicking_Selected_Item_Should_Deselect_It_When_SelectionMode_Toggle()
{
var target = new ListBox
using (UnitTestApplication.Start())
{
Template = new FuncControlTemplate(CreateListBoxTemplate),
Items = new[] { "Foo", "Bar", "Baz " },
SelectionMode = SelectionMode.Toggle,
};
var target = new ListBox
{
Template = new FuncControlTemplate(CreateListBoxTemplate),
Items = new[] { "Foo", "Bar", "Baz " },
SelectionMode = SelectionMode.Toggle,
};

ApplyTemplate(target);
target.SelectedIndex = 0;
AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
ApplyTemplate(target);
target.SelectedIndex = 0;

_mouse.Click(target.Presenter.Panel.Children[0]);
_mouse.Click(target.Presenter.Panel.Children[0]);

Assert.Equal(-1, target.SelectedIndex);
Assert.Equal(-1, target.SelectedIndex);
}
}

[Fact]
public void Clicking_Selected_Item_Should_Not_Deselect_It_When_SelectionMode_ToggleAlwaysSelected()
{
var target = new ListBox
using (UnitTestApplication.Start())
{
Template = new FuncControlTemplate(CreateListBoxTemplate),
Items = new[] { "Foo", "Bar", "Baz " },
SelectionMode = SelectionMode.Toggle | SelectionMode.AlwaysSelected,
};

ApplyTemplate(target);
target.SelectedIndex = 0;
var target = new ListBox
{
Template = new FuncControlTemplate(CreateListBoxTemplate),
Items = new[] { "Foo", "Bar", "Baz " },
SelectionMode = SelectionMode.Toggle | SelectionMode.AlwaysSelected,
};
AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
ApplyTemplate(target);
target.SelectedIndex = 0;

_mouse.Click(target.Presenter.Panel.Children[0]);
_mouse.Click(target.Presenter.Panel.Children[0]);

Assert.Equal(0, target.SelectedIndex);
Assert.Equal(0, target.SelectedIndex);
}
}

[Fact]
public void Clicking_Another_Item_Should_Select_It_When_SelectionMode_Toggle()
{
var target = new ListBox
using (UnitTestApplication.Start())
{
Template = new FuncControlTemplate(CreateListBoxTemplate),
Items = new[] { "Foo", "Bar", "Baz " },
SelectionMode = SelectionMode.Single | SelectionMode.Toggle,
};

ApplyTemplate(target);
target.SelectedIndex = 1;
var target = new ListBox
{
Template = new FuncControlTemplate(CreateListBoxTemplate),
Items = new[] { "Foo", "Bar", "Baz " },
SelectionMode = SelectionMode.Single | SelectionMode.Toggle,
};
AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
ApplyTemplate(target);
target.SelectedIndex = 1;

_mouse.Click(target.Presenter.Panel.Children[0]);
_mouse.Click(target.Presenter.Panel.Children[0]);

Assert.Equal(0, target.SelectedIndex);
Assert.Equal(0, target.SelectedIndex);
}
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Avalonia.Controls.Templates;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Input.Platform;
using Avalonia.Interactivity;
using Avalonia.Markup.Data;
using Avalonia.Styling;
Expand Down Expand Up @@ -1112,42 +1113,48 @@ public void Nested_ListBox_Does_Not_Change_Parent_SelectedIndex()
[Fact]
public void Setting_SelectedItem_With_Pointer_Should_Set_TabOnceActiveElement()
{
var target = new ListBox
using (UnitTestApplication.Start())
{
Template = Template(),
Items = new[] { "Foo", "Bar", "Baz " },
};

Prepare(target);
_helper.Down((Interactive)target.Presenter.Panel.Children[1]);
var target = new ListBox
{
Template = Template(),
Items = new[] { "Foo", "Bar", "Baz " },
};
AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
Prepare(target);
_helper.Down((Interactive)target.Presenter.Panel.Children[1]);

var panel = target.Presenter.Panel;
var panel = target.Presenter.Panel;

Assert.Equal(
KeyboardNavigation.GetTabOnceActiveElement((InputElement)panel),
panel.Children[1]);
Assert.Equal(
KeyboardNavigation.GetTabOnceActiveElement((InputElement)panel),
panel.Children[1]);
}
}

[Fact]
public void Removing_SelectedItem_Should_Clear_TabOnceActiveElement()
{
var items = new ObservableCollection<string>(new[] { "Foo", "Bar", "Baz " });

var target = new ListBox
using (UnitTestApplication.Start())
{
Template = Template(),
Items = items,
};
var items = new ObservableCollection<string>(new[] { "Foo", "Bar", "Baz " });

Prepare(target);
var target = new ListBox
{
Template = Template(),
Items = items,
};
AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
Prepare(target);

_helper.Down(target.Presenter.Panel.Children[1]);
_helper.Down(target.Presenter.Panel.Children[1]);

items.RemoveAt(1);
items.RemoveAt(1);

var panel = target.Presenter.Panel;
var panel = target.Presenter.Panel;

Assert.Null(KeyboardNavigation.GetTabOnceActiveElement((InputElement)panel));
Assert.Null(KeyboardNavigation.GetTabOnceActiveElement((InputElement)panel));
}
}

[Fact]
Expand Down Expand Up @@ -1227,31 +1234,37 @@ public void Mode_For_SelectedIndex_Is_TwoWay_By_Default()
[Fact]
public void Should_Select_Correct_Item_When_Duplicate_Items_Are_Present()
{
var target = new ListBox
using (UnitTestApplication.Start())
{
Template = Template(),
Items = new[] { "Foo", "Bar", "Baz", "Foo", "Bar", "Baz" },
};

Prepare(target);
_helper.Down((Interactive)target.Presenter.Panel.Children[3]);
var target = new ListBox
{
Template = Template(),
Items = new[] { "Foo", "Bar", "Baz", "Foo", "Bar", "Baz" },
};
AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
Prepare(target);
_helper.Down((Interactive)target.Presenter.Panel.Children[3]);

Assert.Equal(3, target.SelectedIndex);
Assert.Equal(3, target.SelectedIndex);
}
}

[Fact]
public void Should_Apply_Selected_Pseudoclass_To_Correct_Item_When_Duplicate_Items_Are_Present()
{
var target = new ListBox
using (UnitTestApplication.Start())
{
Template = Template(),
Items = new[] { "Foo", "Bar", "Baz", "Foo", "Bar", "Baz" },
};

Prepare(target);
_helper.Down((Interactive)target.Presenter.Panel.Children[3]);
var target = new ListBox
{
Template = Template(),
Items = new[] { "Foo", "Bar", "Baz", "Foo", "Bar", "Baz" },
};
AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
Prepare(target);
_helper.Down((Interactive)target.Presenter.Panel.Children[3]);

Assert.Equal(new[] { ":pressed", ":selected" }, target.Presenter.Panel.Children[3].Classes);
Assert.Equal(new[] { ":pressed", ":selected" }, target.Presenter.Panel.Children[3].Classes);
}
}

[Fact]
Expand Down

0 comments on commit fb607cc

Please sign in to comment.