Browse Source

Merge branch 'master' into PopupFocusFixes

pull/4632/head
Steven Kirk 5 years ago
committed by GitHub
parent
commit
0d6114977f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      .gitignore
  2. 1
      .ncrunch/Avalonia.Controls.UnitTests.net47.v3.ncrunchproject
  3. 5
      .ncrunch/Sandbox.v3.ncrunchproject
  4. 52
      Avalonia.sln
  5. 3
      Avalonia.v3.ncrunchsolution
  6. 4
      build/SkiaSharp.props
  7. 1
      nukebuild/Build.cs
  8. 5
      samples/ControlCatalog/MainView.xaml
  9. 5
      samples/ControlCatalog/Pages/AutoCompleteBoxPage.xaml
  10. 58
      samples/ControlCatalog/Pages/AutoCompleteBoxPage.xaml.cs
  11. 35
      samples/ControlCatalog/Pages/DataGridPage.xaml.cs
  12. 50
      samples/ControlCatalog/Pages/ListBoxPage.xaml
  13. 57
      samples/ControlCatalog/ViewModels/ListBoxPageViewModel.cs
  14. 8
      samples/RenderDemo/MainWindow.xaml
  15. 48
      samples/RenderDemo/ViewModels/MainWindowViewModel.cs
  16. 8
      samples/Sandbox/App.axaml
  17. 22
      samples/Sandbox/App.axaml.cs
  18. 4
      samples/Sandbox/MainWindow.axaml
  19. 20
      samples/Sandbox/MainWindow.axaml.cs
  20. 17
      samples/Sandbox/Program.cs
  21. 18
      samples/Sandbox/Sandbox.csproj
  22. 68
      src/Avalonia.Base/Collections/AvaloniaList.cs
  23. 54
      src/Avalonia.Controls.DataGrid/Collections/DataGridSortDescription.cs
  24. 7
      src/Avalonia.Controls.DataGrid/DataGrid.cs
  25. 8
      src/Avalonia.Controls.DataGrid/DataGridBoundColumn.cs
  26. 2
      src/Avalonia.Controls.DataGrid/DataGridCell.cs
  27. 2
      src/Avalonia.Controls.DataGrid/DataGridColumn.cs
  28. 99
      src/Avalonia.Controls.DataGrid/DataGridColumnHeader.cs
  29. 5
      src/Avalonia.Controls.DataGrid/DataGridColumns.cs
  30. 2
      src/Avalonia.Controls.DataGrid/DataGridRow.cs
  31. 2
      src/Avalonia.Controls.DataGrid/DataGridRowGroupHeader.cs
  32. 2
      src/Avalonia.Controls.DataGrid/DataGridRowHeader.cs
  33. 4
      src/Avalonia.Controls.DataGrid/EventArgs.cs
  34. 96
      src/Avalonia.Controls/AutoCompleteBox.cs
  35. 2
      src/Avalonia.Controls/Button.cs
  36. 2
      src/Avalonia.Controls/ButtonSpinner.cs
  37. 2
      src/Avalonia.Controls/Calendar/CalendarButton.cs
  38. 2
      src/Avalonia.Controls/Calendar/CalendarDayButton.cs
  39. 2
      src/Avalonia.Controls/Calendar/CalendarItem.cs
  40. 2
      src/Avalonia.Controls/Chrome/CaptionButtons.cs
  41. 2
      src/Avalonia.Controls/Chrome/TitleBar.cs
  42. 2
      src/Avalonia.Controls/DataValidationErrors.cs
  43. 4
      src/Avalonia.Controls/DateTimePickers/DatePicker.cs
  44. 4
      src/Avalonia.Controls/DateTimePickers/TimePicker.cs
  45. 3
      src/Avalonia.Controls/Expander.cs
  46. 2
      src/Avalonia.Controls/ItemsControl.cs
  47. 1
      src/Avalonia.Controls/ListBox.cs
  48. 2
      src/Avalonia.Controls/ListBoxItem.cs
  49. 2
      src/Avalonia.Controls/MenuItem.cs
  50. 2
      src/Avalonia.Controls/Notifications/NotificationCard.cs
  51. 2
      src/Avalonia.Controls/Notifications/WindowNotificationManager.cs
  52. 2
      src/Avalonia.Controls/Primitives/PopupPositioning/ManagedPopupPositioner.cs
  53. 2
      src/Avalonia.Controls/Primitives/ScrollBar.cs
  54. 339
      src/Avalonia.Controls/Primitives/SelectingItemsControl.cs
  55. 2
      src/Avalonia.Controls/Primitives/Thumb.cs
  56. 2
      src/Avalonia.Controls/Primitives/ToggleButton.cs
  57. 2
      src/Avalonia.Controls/Primitives/Track.cs
  58. 2
      src/Avalonia.Controls/ProgressBar.cs
  59. 36
      src/Avalonia.Controls/ScrollViewer.cs
  60. 278
      src/Avalonia.Controls/Selection/InternalSelectionModel.cs
  61. 113
      src/Avalonia.Controls/Selection/SelectionModel.cs
  62. 2
      src/Avalonia.Controls/Slider.cs
  63. 7
      src/Avalonia.Controls/SplitView.cs
  64. 55
      src/Avalonia.Controls/TabControl.cs
  65. 2
      src/Avalonia.Controls/TabItem.cs
  66. 2
      src/Avalonia.Controls/TextBox.cs
  67. 4
      src/Avalonia.Controls/ToggleSwitch.cs
  68. 2
      src/Avalonia.Controls/ToolTip.cs
  69. 2
      src/Avalonia.Controls/TreeViewItem.cs
  70. 283
      src/Avalonia.Controls/Utils/SelectedItemsSync.cs
  71. 2
      src/Avalonia.Input/InputElement.cs
  72. 9
      src/Avalonia.Input/KeyboardNavigation.cs
  73. 2
      src/Avalonia.Layout/FlowLayoutAlgorithm.cs
  74. 2
      src/Avalonia.ReactiveUI.Events/Avalonia.ReactiveUI.Events.csproj
  75. 18
      src/Avalonia.Styling/Controls/Metadata/PseudoClassesAttribute.cs
  76. 4
      src/Avalonia.Themes.Fluent/Button.xaml
  77. 9
      src/Avalonia.Visuals/Rendering/SleepLoopRenderTimer.cs
  78. 23
      tests/Avalonia.Base.UnitTests/Collections/AvaloniaListTests.cs
  79. 9
      tests/Avalonia.Controls.DataGrid.UnitTests/Collections/DataGridSortDescriptionTests.cs
  80. 34
      tests/Avalonia.Controls.UnitTests/AutoCompleteBoxTests.cs
  81. 529
      tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs
  82. 254
      tests/Avalonia.Controls.UnitTests/Selection/InternalSelectionModelTests.cs
  83. 56
      tests/Avalonia.Controls.UnitTests/Selection/SelectionModelTests_Multiple.cs
  84. 56
      tests/Avalonia.Controls.UnitTests/Selection/SelectionModelTests_Single.cs
  85. 84
      tests/Avalonia.Controls.UnitTests/TabControlTests.cs
  86. 278
      tests/Avalonia.Controls.UnitTests/Utils/SelectedItemsSyncTests.cs
  87. 15
      tests/Avalonia.ReactiveUI.Events.UnitTests/Avalonia.ReactiveUI.Events.UnitTests.csproj
  88. 44
      tests/Avalonia.ReactiveUI.Events.UnitTests/BasicControlEventsTest.cs

1
.gitignore

@ -117,6 +117,7 @@ ClientBin/
*.[Pp]ublish.xml
*.pfx
*.publishsettings
Events_Avalonia.cs
# RIA/Silverlight projects
Generated_Code/

1
.ncrunch/Avalonia.Controls.UnitTests.net47.v3.ncrunchproject

@ -3,5 +3,6 @@
<HiddenComponentWarnings>
<Value>MissingOrIgnoredProjectReference</Value>
</HiddenComponentWarnings>
<FixtureName>Avalonia.Controls.UnitTests.TimePickerTests</FixtureName>
</Settings>
</ProjectConfiguration>

5
.ncrunch/Sandbox.v3.ncrunchproject

@ -0,0 +1,5 @@
<ProjectConfiguration>
<Settings>
<IgnoreThisComponentCompletely>True</IgnoreThisComponentCompletely>
</Settings>
</ProjectConfiguration>

52
Avalonia.sln

@ -224,7 +224,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Avalonia.Markup.Xaml.Loader
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Avalonia.ReactiveUI.Events", "src\Avalonia.ReactiveUI.Events\Avalonia.ReactiveUI.Events.csproj", "{28F18757-C3E6-4BBE-A37D-11BA2AB9177C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Avalonia.ReactiveUI.Events.UnitTests", "tests\Avalonia.ReactiveUI.Events.UnitTests\Avalonia.ReactiveUI.Events.UnitTests.csproj", "{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sandbox", "samples\Sandbox\Sandbox.csproj", "{11BE52AF-E2DD-4CF0-B19A-05285ACAF571}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
@ -2040,30 +2040,30 @@ Global
{28F18757-C3E6-4BBE-A37D-11BA2AB9177C}.Release|iPhone.Build.0 = Release|Any CPU
{28F18757-C3E6-4BBE-A37D-11BA2AB9177C}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{28F18757-C3E6-4BBE-A37D-11BA2AB9177C}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU
{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU
{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU
{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU
{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7}.Ad-Hoc|iPhoneSimulator.Build.0 = Debug|Any CPU
{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU
{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7}.AppStore|Any CPU.Build.0 = Debug|Any CPU
{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7}.AppStore|iPhone.ActiveCfg = Debug|Any CPU
{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7}.AppStore|iPhone.Build.0 = Debug|Any CPU
{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7}.AppStore|iPhoneSimulator.Build.0 = Debug|Any CPU
{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7}.Debug|iPhone.Build.0 = Debug|Any CPU
{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7}.Release|Any CPU.Build.0 = Release|Any CPU
{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7}.Release|iPhone.ActiveCfg = Release|Any CPU
{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7}.Release|iPhone.Build.0 = Release|Any CPU
{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{11BE52AF-E2DD-4CF0-B19A-05285ACAF571}.Ad-Hoc|Any CPU.ActiveCfg = Release|Any CPU
{11BE52AF-E2DD-4CF0-B19A-05285ACAF571}.Ad-Hoc|Any CPU.Build.0 = Release|Any CPU
{11BE52AF-E2DD-4CF0-B19A-05285ACAF571}.Ad-Hoc|iPhone.ActiveCfg = Release|Any CPU
{11BE52AF-E2DD-4CF0-B19A-05285ACAF571}.Ad-Hoc|iPhone.Build.0 = Release|Any CPU
{11BE52AF-E2DD-4CF0-B19A-05285ACAF571}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Release|Any CPU
{11BE52AF-E2DD-4CF0-B19A-05285ACAF571}.Ad-Hoc|iPhoneSimulator.Build.0 = Release|Any CPU
{11BE52AF-E2DD-4CF0-B19A-05285ACAF571}.AppStore|Any CPU.ActiveCfg = Release|Any CPU
{11BE52AF-E2DD-4CF0-B19A-05285ACAF571}.AppStore|Any CPU.Build.0 = Release|Any CPU
{11BE52AF-E2DD-4CF0-B19A-05285ACAF571}.AppStore|iPhone.ActiveCfg = Release|Any CPU
{11BE52AF-E2DD-4CF0-B19A-05285ACAF571}.AppStore|iPhone.Build.0 = Release|Any CPU
{11BE52AF-E2DD-4CF0-B19A-05285ACAF571}.AppStore|iPhoneSimulator.ActiveCfg = Release|Any CPU
{11BE52AF-E2DD-4CF0-B19A-05285ACAF571}.AppStore|iPhoneSimulator.Build.0 = Release|Any CPU
{11BE52AF-E2DD-4CF0-B19A-05285ACAF571}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{11BE52AF-E2DD-4CF0-B19A-05285ACAF571}.Debug|Any CPU.Build.0 = Debug|Any CPU
{11BE52AF-E2DD-4CF0-B19A-05285ACAF571}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{11BE52AF-E2DD-4CF0-B19A-05285ACAF571}.Debug|iPhone.Build.0 = Debug|Any CPU
{11BE52AF-E2DD-4CF0-B19A-05285ACAF571}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{11BE52AF-E2DD-4CF0-B19A-05285ACAF571}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{11BE52AF-E2DD-4CF0-B19A-05285ACAF571}.Release|Any CPU.ActiveCfg = Release|Any CPU
{11BE52AF-E2DD-4CF0-B19A-05285ACAF571}.Release|Any CPU.Build.0 = Release|Any CPU
{11BE52AF-E2DD-4CF0-B19A-05285ACAF571}.Release|iPhone.ActiveCfg = Release|Any CPU
{11BE52AF-E2DD-4CF0-B19A-05285ACAF571}.Release|iPhone.Build.0 = Release|Any CPU
{11BE52AF-E2DD-4CF0-B19A-05285ACAF571}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{11BE52AF-E2DD-4CF0-B19A-05285ACAF571}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -2122,7 +2122,7 @@ Global
{351337F5-D66F-461B-A957-4EF60BDB4BA6} = {C5A00AC3-B34C-4564-9BDD-2DA473EF4D8B}
{3C84E04B-36CF-4D0D-B965-C26DD649D1F3} = {A0CC0258-D18C-4AB3-854F-7101680FC3F9}
{909A8CBD-7D0E-42FD-B841-022AD8925820} = {8B6A8209-894F-4BA1-B880-965FD453982C}
{780AC0FE-8DD2-419F-A1DC-AC7E3EB393F7} = {C5A00AC3-B34C-4564-9BDD-2DA473EF4D8B}
{11BE52AF-E2DD-4CF0-B19A-05285ACAF571} = {9B9E3891-2366-4253-A952-D08BCEB71098}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {87366D66-1391-4D90-8999-95A620AD786A}

3
Avalonia.v3.ncrunchsolution

@ -6,6 +6,9 @@
<Value>src\Avalonia.Build.Tasks\bin\Debug\netstandard2.0\Mono.Cecil.dll</Value>
</AdditionalFilesToIncludeForSolution>
<AllowParallelTestExecution>True</AllowParallelTestExecution>
<CustomBuildProperties>
<Value>RunApiCompat = false</Value>
</CustomBuildProperties>
<ProjectConfigStoragePathRelativeToSolutionDir>.ncrunch</ProjectConfigStoragePathRelativeToSolutionDir>
<SolutionConfigured>True</SolutionConfigured>
</Settings>

4
build/SkiaSharp.props

@ -1,6 +1,6 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<PackageReference Include="SkiaSharp" Version="2.80.2-preview.33" />
<PackageReference Condition="'$(IncludeLinuxSkia)' == 'true'" Include="SkiaSharp.NativeAssets.Linux" Version="2.80.2-preview.33" />
<PackageReference Include="SkiaSharp" Version="2.80.2" />
<PackageReference Condition="'$(IncludeLinuxSkia)' == 'true'" Include="SkiaSharp.NativeAssets.Linux" Version="2.80.2" />
</ItemGroup>
</Project>

1
nukebuild/Build.cs

@ -241,7 +241,6 @@ partial class Build : NukeBuild
RunCoreTest("Avalonia.Visuals.UnitTests");
RunCoreTest("Avalonia.Skia.UnitTests");
RunCoreTest("Avalonia.ReactiveUI.UnitTests");
RunCoreTest("Avalonia.ReactiveUI.Events.UnitTests");
});
Target RunRenderTests => _ => _

5
samples/ControlCatalog/MainView.xaml

@ -45,7 +45,10 @@
<pages:ItemsRepeaterPage/>
</TabItem>
<TabItem Header="LayoutTransformControl"><pages:LayoutTransformControlPage/></TabItem>
<TabItem Header="ListBox"><pages:ListBoxPage/></TabItem>
<TabItem Header="ListBox"
ScrollViewer.VerticalScrollBarVisibility="Disabled">
<pages:ListBoxPage/>
</TabItem>
<TabItem Header="Menu"><pages:MenuPage/></TabItem>
<TabItem Header="Notifications"><pages:NotificationsPage/></TabItem>
<TabItem Header="NumericUpDown"><pages:NumericUpDownPage/></TabItem>

5
samples/ControlCatalog/Pages/AutoCompleteBoxPage.xaml

@ -51,6 +51,11 @@
Width="200"
Margin="0,0,0,8"
FilterMode="None"/>
<TextBlock Text="Custom Autocomplete"/>
<AutoCompleteBox Name="CustomAutocompleteBox"
Width="200"
Margin="0,0,0,8"
FilterMode="None"/>
</StackPanel>
</StackPanel>
</StackPanel>

58
samples/ControlCatalog/Pages/AutoCompleteBoxPage.xaml.cs

@ -92,13 +92,28 @@ namespace ControlCatalog.Pages
}
public StateData[] States { get; private set; }
private LinkedList<string>[] BuildAllSentences()
{
return new string[]
{
"Hello world",
"No this is Patrick",
"Never gonna give you up",
"How does one patch KDE2 under FreeBSD"
}
.Select(x => new LinkedList<string>(x.Split(' ')))
.ToArray();
}
public LinkedList<string>[] Sentences { get; private set; }
public AutoCompleteBoxPage()
{
this.InitializeComponent();
States = BuildAllStates();
Sentences = BuildAllSentences();
foreach (AutoCompleteBox box in GetAllAutoCompleteBox())
foreach (AutoCompleteBox box in GetAllAutoCompleteBox().Where(x => x.Name != "CustomAutocompleteBox"))
{
box.Items = States;
}
@ -116,6 +131,11 @@ namespace ControlCatalog.Pages
var asyncBox = this.FindControl<AutoCompleteBox>("AsyncBox");
asyncBox.AsyncPopulator = PopulateAsync;
var customAutocompleteBox = this.FindControl<AutoCompleteBox>("CustomAutocompleteBox");
customAutocompleteBox.Items = Sentences.SelectMany(x => x);
customAutocompleteBox.TextFilter = LastWordContains;
customAutocompleteBox.TextSelector = AppendWord;
}
private IEnumerable<AutoCompleteBox> GetAllAutoCompleteBox()
{
@ -137,6 +157,42 @@ namespace ControlCatalog.Pages
.ToList();
}
private bool LastWordContains(string searchText, string item)
{
var words = searchText.Split(' ');
var options = Sentences.Select(x => x.First).ToArray();
for (var i = 0; i < words.Length; ++i)
{
var word = words[i];
for (var j = 0; j < options.Length; ++j)
{
var option = options[j];
if (option == null)
continue;
if (i == words.Length - 1)
{
options[j] = option.Value.ToLower().Contains(word.ToLower()) ? option : null;
}
else
{
options[j] = option.Value.Equals(word, StringComparison.InvariantCultureIgnoreCase) ? option.Next : null;
}
}
}
return options.Any(x => x != null && x.Value == item);
}
private string AppendWord(string text, string item)
{
string[] parts = text.Split(' ');
if (parts.Length == 0)
return item;
parts[parts.Length - 1] = item;
return string.Join(" ", parts);
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);

35
samples/ControlCatalog/Pages/DataGridPage.xaml.cs

@ -1,8 +1,12 @@
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using ControlCatalog.Models;
using Avalonia.Collections;
using Avalonia.Data;
namespace ControlCatalog.Pages
{
@ -11,12 +15,22 @@ namespace ControlCatalog.Pages
public DataGridPage()
{
this.InitializeComponent();
var dataGridSortDescription = DataGridSortDescription.FromPath(nameof(Country.Region), ListSortDirection.Ascending, new ReversedStringComparer());
var collectionView1 = new DataGridCollectionView(Countries.All);
collectionView1.SortDescriptions.Add(dataGridSortDescription);
var dg1 = this.FindControl<DataGrid>("dataGrid1");
dg1.IsReadOnly = true;
dg1.LoadingRow += Dg1_LoadingRow;
var collectionView1 = new DataGridCollectionView(Countries.All);
//collectionView.GroupDescriptions.Add(new PathGroupDescription("Region"));
dg1.Sorting += (s, a) =>
{
var property = ((a.Column as DataGridBoundColumn)?.Binding as Binding).Path;
if (property == dataGridSortDescription.PropertyPath
&& !collectionView1.SortDescriptions.Contains(dataGridSortDescription))
{
collectionView1.SortDescriptions.Add(dataGridSortDescription);
}
};
dg1.Items = collectionView1;
var dg2 = this.FindControl<DataGrid>("dataGridGrouping");
@ -53,5 +67,20 @@ namespace ControlCatalog.Pages
{
AvaloniaXamlLoader.Load(this);
}
private class ReversedStringComparer : IComparer<object>, IComparer
{
public int Compare(object x, object y)
{
if (x is string left && y is string right)
{
var reversedLeft = new string(left.Reverse().ToArray());
var reversedRight = new string(right.Reverse().ToArray());
return reversedLeft.CompareTo(reversedRight);
}
return Comparer.Default.Compare(x, y);
}
}
}
}

50
samples/ControlCatalog/Pages/ListBoxPage.xaml

@ -1,35 +1,25 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="ControlCatalog.Pages.ListBoxPage">
<StackPanel Orientation="Vertical" Spacing="4">
<TextBlock Classes="h1">ListBox</TextBlock>
<TextBlock Classes="h2">Hosts a collection of ListBoxItem.</TextBlock>
<StackPanel Orientation="Horizontal"
Margin="0,16,0,0"
HorizontalAlignment="Center"
Spacing="16">
<StackPanel Orientation="Vertical" Spacing="8">
<ListBox Items="{Binding Items}"
Selection="{Binding Selection}"
AutoScrollToSelectedItem="True"
SelectionMode="{Binding SelectionMode}"
Width="250"
Height="350"/>
<Button Command="{Binding AddItemCommand}">Add</Button>
<Button Command="{Binding RemoveItemCommand}">Remove</Button>
<Button Command="{Binding SelectRandomItemCommand}">Select Random Item</Button>
<ComboBox SelectedIndex="{Binding SelectionMode, Mode=TwoWay}">
<ComboBoxItem>Single</ComboBoxItem>
<ComboBoxItem>Multiple</ComboBoxItem>
<ComboBoxItem>Toggle</ComboBoxItem>
<ComboBoxItem>AlwaysSelected</ComboBoxItem>
</ComboBox>
</StackPanel>
<DockPanel>
<StackPanel DockPanel.Dock="Top" Margin="4">
<TextBlock Classes="h1">ListBox</TextBlock>
<TextBlock Classes="h2">Hosts a collection of ListBoxItem.</TextBlock>
</StackPanel>
</StackPanel>
<StackPanel DockPanel.Dock="Right" Margin="4">
<CheckBox IsChecked="{Binding Multiple}">Multiple</CheckBox>
<CheckBox IsChecked="{Binding Toggle}">Toggle</CheckBox>
<CheckBox IsChecked="{Binding AlwaysSelected}">AlwaysSelected</CheckBox>
<CheckBox IsChecked="{Binding AutoScrollToSelectedItem}">AutoScrollToSelectedItem</CheckBox>
</StackPanel>
<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" Margin="4">
<Button Command="{Binding AddItemCommand}">Add</Button>
<Button Command="{Binding RemoveItemCommand}">Remove</Button>
<Button Command="{Binding SelectRandomItemCommand}">Select Random Item</Button>
</StackPanel>
<ListBox Items="{Binding Items}"
Selection="{Binding Selection}"
AutoScrollToSelectedItem="{Binding AutoScrollToSelectedItem}"
SelectionMode="{Binding SelectionMode}"/>
</DockPanel>
</UserControl>

57
samples/ControlCatalog/ViewModels/ListBoxPageViewModel.cs

@ -10,22 +10,39 @@ namespace ControlCatalog.ViewModels
{
public class ListBoxPageViewModel : ReactiveObject
{
private bool _multiple;
private bool _toggle;
private bool _alwaysSelected;
private bool _autoScrollToSelectedItem = true;
private int _counter;
private SelectionMode _selectionMode;
private ObservableAsPropertyHelper<SelectionMode> _selectionMode;
public ListBoxPageViewModel()
{
Items = new ObservableCollection<string>(Enumerable.Range(1, 10000).Select(i => GenerateItem()));
Selection = new SelectionModel<string>();
Selection.Select(1);
_selectionMode = this.WhenAnyValue(
x => x.Multiple,
x => x.Toggle,
x => x.AlwaysSelected,
(m, t, a) =>
(m ? SelectionMode.Multiple : 0) |
(t ? SelectionMode.Toggle : 0) |
(a ? SelectionMode.AlwaysSelected : 0))
.ToProperty(this, x => x.SelectionMode);
AddItemCommand = ReactiveCommand.Create(() => Items.Add(GenerateItem()));
RemoveItemCommand = ReactiveCommand.Create(() =>
{
while (Selection.Count > 0)
var items = Selection.SelectedItems.ToList();
foreach (var item in items)
{
Items.Remove(Selection.SelectedItems.First());
Items.Remove(item);
}
});
@ -42,25 +59,37 @@ namespace ControlCatalog.ViewModels
}
public ObservableCollection<string> Items { get; }
public SelectionModel<string> Selection { get; }
public SelectionMode SelectionMode => _selectionMode.Value;
public ReactiveCommand<Unit, Unit> AddItemCommand { get; }
public bool Multiple
{
get => _multiple;
set => this.RaiseAndSetIfChanged(ref _multiple, value);
}
public ReactiveCommand<Unit, Unit> RemoveItemCommand { get; }
public bool Toggle
{
get => _toggle;
set => this.RaiseAndSetIfChanged(ref _toggle, value);
}
public ReactiveCommand<Unit, Unit> SelectRandomItemCommand { get; }
public bool AlwaysSelected
{
get => _alwaysSelected;
set => this.RaiseAndSetIfChanged(ref _alwaysSelected, value);
}
public SelectionMode SelectionMode
public bool AutoScrollToSelectedItem
{
get => _selectionMode;
set
{
Selection.Clear();
this.RaiseAndSetIfChanged(ref _selectionMode, value);
}
get => _autoScrollToSelectedItem;
set => this.RaiseAndSetIfChanged(ref _autoScrollToSelectedItem, value);
}
public ReactiveCommand<Unit, Unit> AddItemCommand { get; }
public ReactiveCommand<Unit, Unit> RemoveItemCommand { get; }
public ReactiveCommand<Unit, Unit> SelectRandomItemCommand { get; }
private string GenerateItem() => $"Item {_counter++.ToString()}";
}
}

8
samples/RenderDemo/MainWindow.xaml

@ -3,8 +3,8 @@
x:Class="RenderDemo.MainWindow"
Title="AvaloniaUI Rendering Test"
xmlns:pages="clr-namespace:RenderDemo.Pages"
Width="800"
Height="600">
Width="{Binding Width, Mode=TwoWay}"
Height="{Binding Height, Mode=TwoWay}">
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="Rendering">
@ -24,6 +24,10 @@
</MenuItem.Icon>
</MenuItem>
</MenuItem>
<MenuItem Header="Tests">
<MenuItem Header="Resize window"
Command="{Binding ResizeWindow}"/>
</MenuItem>
</Menu>
<TabControl Classes="sidebar">
<TabItem Header="Animations">

48
samples/RenderDemo/ViewModels/MainWindowViewModel.cs

@ -1,5 +1,6 @@
using System;
using System.Reactive;
using System.Reactive;
using System.Threading.Tasks;
using ReactiveUI;
namespace RenderDemo.ViewModels
@ -8,26 +9,61 @@ namespace RenderDemo.ViewModels
{
private bool drawDirtyRects = false;
private bool drawFps = true;
private double width = 800;
private double height = 600;
public MainWindowViewModel()
{
ToggleDrawDirtyRects = ReactiveCommand.Create(() => DrawDirtyRects = !DrawDirtyRects);
ToggleDrawFps = ReactiveCommand.Create(() => DrawFps = !DrawFps);
ResizeWindow = ReactiveCommand.CreateFromTask(ResizeWindowAsync);
}
public bool DrawDirtyRects
{
get { return drawDirtyRects; }
set { this.RaiseAndSetIfChanged(ref drawDirtyRects, value); }
get => drawDirtyRects;
set => this.RaiseAndSetIfChanged(ref drawDirtyRects, value);
}
public bool DrawFps
{
get { return drawFps; }
set { this.RaiseAndSetIfChanged(ref drawFps, value); }
get => drawFps;
set => this.RaiseAndSetIfChanged(ref drawFps, value);
}
public double Width
{
get => width;
set => this.RaiseAndSetIfChanged(ref width, value);
}
public double Height
{
get => height;
set => this.RaiseAndSetIfChanged(ref height, value);
}
public ReactiveCommand<Unit, bool> ToggleDrawDirtyRects { get; }
public ReactiveCommand<Unit, bool> ToggleDrawFps { get; }
public ReactiveCommand<Unit, Unit> ResizeWindow { get; }
private async Task ResizeWindowAsync()
{
for (int i = 0; i < 30; i++)
{
Width += 10;
Height += 5;
await Task.Delay(10);
}
await Task.Delay(10);
for (int i = 0; i < 30; i++)
{
Width -= 10;
Height -= 5;
await Task.Delay(10);
}
}
}
}

8
samples/Sandbox/App.axaml

@ -0,0 +1,8 @@
<Application
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Sandbox.App">
<Application.Styles>
<StyleInclude Source="avares://Avalonia.Themes.Fluent/Accents/FluentDark.xaml"/>
</Application.Styles>
</Application>

22
samples/Sandbox/App.axaml.cs

@ -0,0 +1,22 @@
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
namespace Sandbox
{
public class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}
public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime)
{
desktopLifetime.MainWindow = new MainWindow();
}
}
}
}

4
samples/Sandbox/MainWindow.axaml

@ -0,0 +1,4 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
x:Class="Sandbox.MainWindow">
</Window>

20
samples/Sandbox/MainWindow.axaml.cs

@ -0,0 +1,20 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace Sandbox
{
public class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
this.AttachDevTools();
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
}
}

17
samples/Sandbox/Program.cs

@ -0,0 +1,17 @@
using Avalonia;
using Avalonia.ReactiveUI;
namespace Sandbox
{
public class Program
{
static void Main(string[] args)
{
AppBuilder.Configure<App>()
.UsePlatformDetect()
.UseReactiveUI()
.LogToDebug()
.StartWithClassicDesktopLifetime(args);
}
}
}

18
samples/Sandbox/Sandbox.csproj

@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Avalonia.Diagnostics\Avalonia.Diagnostics.csproj" />
<ProjectReference Include="..\..\src\Avalonia.ReactiveUI\Avalonia.ReactiveUI.csproj" />
<ProjectReference Include="..\..\src\Avalonia.Controls.DataGrid\Avalonia.Controls.DataGrid.csproj" />
</ItemGroup>
<Import Project="..\..\build\SampleApp.props" />
<Import Project="..\..\build\ReferenceCoreLibraries.props" />
<Import Project="..\..\build\BuildTargets.targets" />
</Project>

68
src/Avalonia.Base/Collections/AvaloniaList.cs

@ -543,7 +543,73 @@ namespace Avalonia.Collections
/// <inheritdoc/>
void ICollection.CopyTo(Array array, int index)
{
_inner.CopyTo((T[])array, index);
if (array == null)
{
throw new ArgumentNullException(nameof(array));
}
if (array.Rank != 1)
{
throw new ArgumentException("Multi-dimensional arrays are not supported.");
}
if (array.GetLowerBound(0) != 0)
{
throw new ArgumentException("Non-zero lower bounds are not supported.");
}
if (index < 0)
{
throw new ArgumentException("Invalid index.");
}
if (array.Length - index < Count)
{
throw new ArgumentException("The target array is too small.");
}
if (array is T[] tArray)
{
_inner.CopyTo(tArray, index);
}
else
{
//
// Catch the obvious case assignment will fail.
// We can't find all possible problems by doing the check though.
// For example, if the element type of the Array is derived from T,
// we can't figure out if we can successfully copy the element beforehand.
//
Type targetType = array.GetType().GetElementType()!;
Type sourceType = typeof(T);
if (!(targetType.IsAssignableFrom(sourceType) || sourceType.IsAssignableFrom(targetType)))
{
throw new ArgumentException("Invalid array type");
}
//
// We can't cast array of value type to object[], so we don't support
// widening of primitive types here.
//
object[] objects = array as object[];
if (objects == null)
{
throw new ArgumentException("Invalid array type");
}
int count = _inner.Count;
try
{
for (int i = 0; i < count; i++)
{
objects[index++] = _inner[i];
}
}
catch (ArrayTypeMismatchException)
{
throw new ArgumentException("Invalid array type");
}
}
}
/// <inheritdoc/>

54
src/Avalonia.Controls.DataGrid/Collections/DataGridSortDescription.cs

@ -1,19 +1,21 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using Avalonia.Controls;
using Avalonia.Controls.Utils;
using Avalonia.Utilities;
namespace Avalonia.Collections
{
public abstract class DataGridSortDescription
{
public virtual string PropertyPath => null;
public virtual bool Descending => false;
[Obsolete("Use Direction property to read or override sorting direction.")]
public virtual bool Descending => Direction == ListSortDirection.Descending;
public virtual ListSortDirection Direction => ListSortDirection.Ascending;
public bool HasPropertyPath => !String.IsNullOrEmpty(PropertyPath);
public abstract IComparer<object> Comparer { get; }
@ -26,7 +28,7 @@ namespace Avalonia.Collections
return seq.ThenBy(o => o, Comparer);
}
internal virtual DataGridSortDescription SwitchSortDirection()
public virtual DataGridSortDescription SwitchSortDirection()
{
return this;
}
@ -105,7 +107,7 @@ namespace Avalonia.Collections
private class DataGridPathSortDescription : DataGridSortDescription
{
private readonly bool _descending;
private readonly ListSortDirection _direction;
private readonly string _propertyPath;
private readonly Lazy<CultureSensitiveComparer> _cultureSensitiveComparer;
private readonly Lazy<IComparer<object>> _comparer;
@ -118,7 +120,7 @@ namespace Avalonia.Collections
{
if (_internalComparerTyped == null && _internalComparer != null)
{
if (_internalComparerTyped is IComparer<object> c)
if (_internalComparer is IComparer<object> c)
_internalComparerTyped = c;
else
_internalComparerTyped = Comparer<object>.Create((x, y) => _internalComparer.Compare(x, y));
@ -130,19 +132,20 @@ namespace Avalonia.Collections
public override string PropertyPath => _propertyPath;
public override IComparer<object> Comparer => _comparer.Value;
public override bool Descending => _descending;
public override ListSortDirection Direction => _direction;
public DataGridPathSortDescription(string propertyPath, bool descending, CultureInfo culture)
public DataGridPathSortDescription(string propertyPath, ListSortDirection direction, IComparer internalComparer, CultureInfo culture)
{
_propertyPath = propertyPath;
_descending = descending;
_direction = direction;
_cultureSensitiveComparer = new Lazy<CultureSensitiveComparer>(() => new CultureSensitiveComparer(culture ?? CultureInfo.CurrentCulture));
_internalComparer = internalComparer;
_comparer = new Lazy<IComparer<object>>(() => Comparer<object>.Create((x, y) => Compare(x, y)));
}
private DataGridPathSortDescription(DataGridPathSortDescription inner, bool descending)
private DataGridPathSortDescription(DataGridPathSortDescription inner, ListSortDirection direction)
{
_propertyPath = inner._propertyPath;
_descending = descending;
_direction = direction;
_propertyType = inner._propertyType;
_cultureSensitiveComparer = inner._cultureSensitiveComparer;
_internalComparer = inner._internalComparer;
@ -201,7 +204,7 @@ namespace Avalonia.Collections
result = _internalComparer?.Compare(v1, v2) ?? 0;
if (_descending)
if (Direction == ListSortDirection.Descending)
return -result;
else
return result;
@ -218,7 +221,7 @@ namespace Avalonia.Collections
}
public override IOrderedEnumerable<object> OrderBy(IEnumerable<object> seq)
{
if(_descending)
if (Direction == ListSortDirection.Descending)
{
return seq.OrderByDescending(o => GetValue(o), InternalComparer);
}
@ -229,7 +232,7 @@ namespace Avalonia.Collections
}
public override IOrderedEnumerable<object> ThenBy(IOrderedEnumerable<object> seq)
{
if (_descending)
if (Direction == ListSortDirection.Descending)
{
return seq.ThenByDescending(o => GetValue(o), InternalComparer);
}
@ -239,15 +242,28 @@ namespace Avalonia.Collections
}
}
internal override DataGridSortDescription SwitchSortDirection()
public override DataGridSortDescription SwitchSortDirection()
{
return new DataGridPathSortDescription(this, !_descending);
var newDirection = _direction == ListSortDirection.Ascending ? ListSortDirection.Descending : ListSortDirection.Ascending;
return new DataGridPathSortDescription(this, newDirection);
}
}
public static DataGridSortDescription FromPath(string propertyPath, bool descending = false, CultureInfo culture = null)
public static DataGridSortDescription FromPath(string propertyPath, ListSortDirection direction = ListSortDirection.Ascending, CultureInfo culture = null)
{
return new DataGridPathSortDescription(propertyPath, direction, null, culture);
}
[Obsolete("Use overload taking a ListSortDirection.")]
public static DataGridSortDescription FromPath(string propertyPath, bool descending, CultureInfo culture = null)
{
return new DataGridPathSortDescription(propertyPath, descending ? ListSortDirection.Descending : ListSortDirection.Ascending, null, culture);
}
public static DataGridSortDescription FromPath(string propertyPath, ListSortDirection direction, IComparer comparer)
{
return new DataGridPathSortDescription(propertyPath, descending, culture);
return new DataGridPathSortDescription(propertyPath, direction, comparer, null);
}
}

7
src/Avalonia.Controls.DataGrid/DataGrid.cs

@ -24,12 +24,14 @@ using Avalonia.Input.Platform;
using System.ComponentModel.DataAnnotations;
using Avalonia.Controls.Utils;
using Avalonia.Layout;
using Avalonia.Controls.Metadata;
namespace Avalonia.Controls
{
/// <summary>
/// Displays data in a customizable grid.
/// </summary>
[PseudoClasses(":invalid")]
public partial class DataGrid : TemplatedControl
{
private const string DATAGRID_elementRowsPresenterName = "PART_RowsPresenter";
@ -1229,6 +1231,11 @@ namespace Avalonia.Controls
remove { AddHandler(SelectionChangedEvent, value); }
}
/// <summary>
/// Occurs when the <see cref="DataGridColumn"/> sorting request is triggered.
/// </summary>
public event EventHandler<DataGridColumnEventArgs> Sorting;
/// <summary>
/// Occurs when a <see cref="T:Avalonia.Controls.DataGridRow" />
/// object becomes available for reuse.

8
src/Avalonia.Controls.DataGrid/DataGridBoundColumn.cs

@ -49,8 +49,12 @@ namespace Avalonia.Controls
{
if(_binding is Avalonia.Data.Binding binding)
{
// Force the TwoWay binding mode if there is a Path present. TwoWay binding requires a Path.
if (!String.IsNullOrEmpty(binding.Path))
if (binding.Mode == BindingMode.OneWayToSource)
{
throw new InvalidOperationException("DataGridColumn doesn't support BindingMode.OneWayToSource. Use BindingMode.TwoWay instead.");
}
if (!String.IsNullOrEmpty(binding.Path) && binding.Mode == BindingMode.Default)
{
binding.Mode = BindingMode.TwoWay;
}

2
src/Avalonia.Controls.DataGrid/DataGridCell.cs

@ -3,6 +3,7 @@
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Shapes;
using Avalonia.Input;
@ -12,6 +13,7 @@ namespace Avalonia.Controls
/// <summary>
/// Represents an individual <see cref="T:Avalonia.Controls.DataGrid" /> cell.
/// </summary>
[PseudoClasses(":selected", ":current", ":edited", ":invalid")]
public class DataGridCell : ContentControl
{
private const string DATAGRIDCELL_elementRightGridLine = "PART_RightGridLine";

2
src/Avalonia.Controls.DataGrid/DataGridColumn.cs

@ -1047,4 +1047,4 @@ namespace Avalonia.Controls
}
}
}

99
src/Avalonia.Controls.DataGrid/DataGridColumnHeader.cs

@ -14,12 +14,14 @@ using Avalonia.Utilities;
using System;
using Avalonia.Controls.Utils;
using Avalonia.Controls.Mixins;
using Avalonia.Controls.Metadata;
namespace Avalonia.Controls
{
/// <summary>
/// Represents an individual <see cref="T:Avalonia.Controls.DataGrid" /> column header.
/// </summary>
[PseudoClasses(":dragIndicator", ":pressed", ":sortascending", ":sortdescending")]
public class DataGridColumnHeader : ContentControl
{
private enum DragMode
@ -161,13 +163,14 @@ namespace Avalonia.Controls
var sort = OwningColumn.GetSortDescription();
if (sort != null)
{
CurrentSortingState = sort.Descending ? ListSortDirection.Descending : ListSortDirection.Ascending;
CurrentSortingState = sort.Direction;
}
}
PseudoClasses.Set(":sortascending",
CurrentSortingState.HasValue && CurrentSortingState.Value == ListSortDirection.Ascending);
CurrentSortingState == ListSortDirection.Ascending);
PseudoClasses.Set(":sortdescending",
CurrentSortingState.HasValue && CurrentSortingState.Value == ListSortDirection.Descending);
CurrentSortingState == ListSortDirection.Descending);
}
internal void UpdateSeparatorVisibility(DataGridColumn lastVisibleColumn)
@ -215,70 +218,76 @@ namespace Avalonia.Controls
internal void ProcessSort(KeyModifiers keyModifiers)
{
// if we can sort:
// - DataConnection.AllowSort is true, and
// - AllowUserToSortColumns and CanSort are true, and
// - OwningColumn is bound, and
// - SortDescriptionsCollection exists, and
// - the column's data type is comparable
// - OwningColumn is bound
// then try to sort
if (OwningColumn != null
&& OwningGrid != null
&& OwningGrid.EditingRow == null
&& OwningColumn != OwningGrid.ColumnsInternal.FillerColumn
&& OwningGrid.DataConnection.AllowSort
&& OwningGrid.CanUserSortColumns
&& OwningColumn.CanUserSort
&& OwningGrid.DataConnection.SortDescriptions != null)
&& OwningColumn.CanUserSort)
{
DataGrid owningGrid = OwningGrid;
var ea = new DataGridColumnEventArgs(OwningColumn);
OwningGrid.OnColumnSorting(ea);
DataGridSortDescription newSort;
if (!ea.Handled && OwningGrid.DataConnection.AllowSort && OwningGrid.DataConnection.SortDescriptions != null)
{
// - DataConnection.AllowSort is true, and
// - SortDescriptionsCollection exists, and
// - the column's data type is comparable
KeyboardHelper.GetMetaKeyState(keyModifiers, out bool ctrl, out bool shift);
DataGrid owningGrid = OwningGrid;
DataGridSortDescription newSort;
DataGridSortDescription sort = OwningColumn.GetSortDescription();
IDataGridCollectionView collectionView = owningGrid.DataConnection.CollectionView;
Debug.Assert(collectionView != null);
using (collectionView.DeferRefresh())
{
// if shift is held down, we multi-sort, therefore if it isn't, we'll clear the sorts beforehand
if (!shift || owningGrid.DataConnection.SortDescriptions.Count == 0)
{
owningGrid.DataConnection.SortDescriptions.Clear();
}
KeyboardHelper.GetMetaKeyState(keyModifiers, out bool ctrl, out bool shift);
DataGridSortDescription sort = OwningColumn.GetSortDescription();
IDataGridCollectionView collectionView = owningGrid.DataConnection.CollectionView;
Debug.Assert(collectionView != null);
// if ctrl is held down, we only clear the sort directions
if (!ctrl)
using (collectionView.DeferRefresh())
{
if (sort != null)
// if shift is held down, we multi-sort, therefore if it isn't, we'll clear the sorts beforehand
if (!shift || owningGrid.DataConnection.SortDescriptions.Count == 0)
{
newSort = sort.SwitchSortDirection();
owningGrid.DataConnection.SortDescriptions.Clear();
}
// changing direction should not affect sort order, so we replace this column's
// sort description instead of just adding it to the end of the collection
int oldIndex = owningGrid.DataConnection.SortDescriptions.IndexOf(sort);
if (oldIndex >= 0)
// if ctrl is held down, we only clear the sort directions
if (!ctrl)
{
if (sort != null)
{
owningGrid.DataConnection.SortDescriptions.Remove(sort);
owningGrid.DataConnection.SortDescriptions.Insert(oldIndex, newSort);
newSort = sort.SwitchSortDirection();
// changing direction should not affect sort order, so we replace this column's
// sort description instead of just adding it to the end of the collection
int oldIndex = owningGrid.DataConnection.SortDescriptions.IndexOf(sort);
if (oldIndex >= 0)
{
owningGrid.DataConnection.SortDescriptions.Remove(sort);
owningGrid.DataConnection.SortDescriptions.Insert(oldIndex, newSort);
}
else
{
owningGrid.DataConnection.SortDescriptions.Add(newSort);
}
}
else
{
string propertyName = OwningColumn.GetSortPropertyName();
// no-opt if we couldn't find a property to sort on
if (string.IsNullOrEmpty(propertyName))
{
return;
}
newSort = DataGridSortDescription.FromPath(propertyName, culture: collectionView.Culture);
owningGrid.DataConnection.SortDescriptions.Add(newSort);
}
}
else
{
string propertyName = OwningColumn.GetSortPropertyName();
// no-opt if we couldn't find a property to sort on
if (string.IsNullOrEmpty(propertyName))
{
return;
}
newSort = DataGridSortDescription.FromPath(propertyName, culture: collectionView.Culture);
owningGrid.DataConnection.SortDescriptions.Add(newSort);
}
}
}
}

5
src/Avalonia.Controls.DataGrid/DataGridColumns.cs

@ -33,6 +33,11 @@ namespace Avalonia.Controls
ColumnReordering?.Invoke(this, e);
}
protected internal virtual void OnColumnSorting(DataGridColumnEventArgs e)
{
Sorting?.Invoke(this, e);
}
/// <summary>
/// Adjusts the widths of all columns with DisplayIndex >= displayIndex such that the total
/// width is adjusted by the given amount, if possible. If the total desired adjustment amount

2
src/Avalonia.Controls.DataGrid/DataGridRow.cs

@ -3,6 +3,7 @@
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Shapes;
using Avalonia.Controls.Templates;
@ -20,6 +21,7 @@ namespace Avalonia.Controls
/// <summary>
/// Represents a <see cref="T:Avalonia.Controls.DataGrid" /> row.
/// </summary>
[PseudoClasses(":selected", ":editing", ":invalid")]
public class DataGridRow : TemplatedControl
{

2
src/Avalonia.Controls.DataGrid/DataGridRowGroupHeader.cs

@ -3,6 +3,7 @@
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Mixins;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
@ -13,6 +14,7 @@ using System.Reactive.Linq;
namespace Avalonia.Controls
{
[PseudoClasses(":pressed", ":current", ":expanded")]
public class DataGridRowGroupHeader : TemplatedControl
{
private const string DATAGRIDROWGROUPHEADER_expanderButton = "ExpanderButton";

2
src/Avalonia.Controls.DataGrid/DataGridRowHeader.cs

@ -3,6 +3,7 @@
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
using Avalonia.Controls.Metadata;
using Avalonia.Input;
using Avalonia.Media;
using System.Diagnostics;
@ -12,6 +13,7 @@ namespace Avalonia.Controls.Primitives
/// <summary>
/// Represents an individual <see cref="T:Avalonia.Controls.DataGrid" /> row header.
/// </summary>
[PseudoClasses(":invalid", ":selected", ":editing", ":current")]
public class DataGridRowHeader : ContentControl
{
private const string DATAGRIDROWHEADER_elementRootName = "PART_Root";

4
src/Avalonia.Controls.DataGrid/EventArgs.cs

@ -289,7 +289,7 @@ namespace Avalonia.Controls
/// <summary>
/// Provides data for <see cref="T:Avalonia.Controls.DataGrid" /> column-related events.
/// </summary>
public class DataGridColumnEventArgs : EventArgs
public class DataGridColumnEventArgs : HandledEventArgs
{
/// <summary>
/// Initializes a new instance of the <see cref="T:Avalonia.Controls.DataGridColumnEventArgs" /> class.
@ -566,4 +566,4 @@ namespace Avalonia.Controls
private set;
}
}
}
}

96
src/Avalonia.Controls/AutoCompleteBox.cs

@ -14,6 +14,7 @@ using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Collections;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Controls.Utils;
@ -30,6 +31,7 @@ namespace Avalonia.Controls
/// <see cref="E:Avalonia.Controls.AutoCompleteBox.Populated" />
/// event.
/// </summary>
[PseudoClasses(":dropdownopen")]
public class PopulatedEventArgs : EventArgs
{
/// <summary>
@ -225,6 +227,27 @@ namespace Avalonia.Controls
Custom = 13,
}
/// <summary>
/// Represents the selector used by the
/// <see cref="T:Avalonia.Controls.AutoCompleteBox" /> control to
/// determine how the specified text should be modified with an item.
/// </summary>
/// <returns>
/// Modified text that will be used by the
/// <see cref="T:Avalonia.Controls.AutoCompleteBox" />.
/// </returns>
/// <param name="search">The string used as the basis for filtering.</param>
/// <param name="item">
/// The selected item that should be combined with the
/// <paramref name="search" /> parameter.
/// </param>
/// <typeparam name="T">
/// The type used for filtering the
/// <see cref="T:Avalonia.Controls.AutoCompleteBox" />.
/// This type can be either a string or an object.
/// </typeparam>
public delegate string AutoCompleteSelector<T>(string search, T item);
/// <summary>
/// Represents a control that provides a text box for user input and a
/// drop-down that contains possible matches based on the input in the text
@ -362,6 +385,9 @@ namespace Avalonia.Controls
private AutoCompleteFilterPredicate<object> _itemFilter;
private AutoCompleteFilterPredicate<string> _textFilter = AutoCompleteSearch.GetFilter(AutoCompleteFilterMode.StartsWith);
private AutoCompleteSelector<object> _itemSelector;
private AutoCompleteSelector<string> _textSelector;
public static readonly RoutedEvent<SelectionChangedEventArgs> SelectionChangedEvent =
RoutedEvent.Register<SelectionChangedEventArgs>(nameof(SelectionChanged), RoutingStrategies.Bubble, typeof(AutoCompleteBox));
@ -528,6 +554,34 @@ namespace Avalonia.Controls
(o, v) => o.TextFilter = v,
unsetValue: AutoCompleteSearch.GetFilter(AutoCompleteFilterMode.StartsWith));
/// <summary>
/// Identifies the
/// <see cref="P:Avalonia.Controls.AutoCompleteBox.ItemSelector" />
/// dependency property.
/// </summary>
/// <value>The identifier for the
/// <see cref="P:Avalonia.Controls.AutoCompleteBox.ItemSelector" />
/// dependency property.</value>
public static readonly DirectProperty<AutoCompleteBox, AutoCompleteSelector<object>> ItemSelectorProperty =
AvaloniaProperty.RegisterDirect<AutoCompleteBox, AutoCompleteSelector<object>>(
nameof(ItemSelector),
o => o.ItemSelector,
(o, v) => o.ItemSelector = v);
/// <summary>
/// Identifies the
/// <see cref="P:Avalonia.Controls.AutoCompleteBox.TextSelector" />
/// dependency property.
/// </summary>
/// <value>The identifier for the
/// <see cref="P:Avalonia.Controls.AutoCompleteBox.TextSelector" />
/// dependency property.</value>
public static readonly DirectProperty<AutoCompleteBox, AutoCompleteSelector<string>> TextSelectorProperty =
AvaloniaProperty.RegisterDirect<AutoCompleteBox, AutoCompleteSelector<string>>(
nameof(TextSelector),
o => o.TextSelector,
(o, v) => o.TextSelector = v);
/// <summary>
/// Identifies the
/// <see cref="P:Avalonia.Controls.AutoCompleteBox.ItemsSource" />
@ -1061,6 +1115,40 @@ namespace Avalonia.Controls
set { SetAndRaise(TextFilterProperty, ref _textFilter, value); }
}
/// <summary>
/// Gets or sets the custom method that combines the user-entered
/// text and one of the items specified by the
/// <see cref="P:Avalonia.Controls.AutoCompleteBox.ItemsSource" />.
/// </summary>
/// <value>
/// The custom method that combines the user-entered
/// text and one of the items specified by the
/// <see cref="P:Avalonia.Controls.AutoCompleteBox.ItemsSource" />.
/// </value>
public AutoCompleteSelector<object> ItemSelector
{
get { return _itemSelector; }
set { SetAndRaise(ItemSelectorProperty, ref _itemSelector, value); }
}
/// <summary>
/// Gets or sets the custom method that combines the user-entered
/// text and one of the items specified by the
/// <see cref="P:Avalonia.Controls.AutoCompleteBox.ItemsSource" />
/// in a text-based way.
/// </summary>
/// <value>
/// The custom method that combines the user-entered
/// text and one of the items specified by the
/// <see cref="P:Avalonia.Controls.AutoCompleteBox.ItemsSource" />
/// in a text-based way.
/// </value>
public AutoCompleteSelector<string> TextSelector
{
get { return _textSelector; }
set { SetAndRaise(TextSelectorProperty, ref _textSelector, value); }
}
public Func<string, CancellationToken, Task<IEnumerable<object>>> AsyncPopulator
{
get { return _asyncPopulator; }
@ -2329,6 +2417,14 @@ namespace Avalonia.Controls
{
text = SearchText;
}
else if (TextSelector != null)
{
text = TextSelector(SearchText, FormatValue(newItem, true));
}
else if (ItemSelector != null)
{
text = ItemSelector(SearchText, newItem);
}
else
{
text = FormatValue(newItem, true);

2
src/Avalonia.Controls/Button.cs

@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Windows.Input;
using Avalonia.Controls.Metadata;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Interactivity;
@ -28,6 +29,7 @@ namespace Avalonia.Controls
/// <summary>
/// A button control.
/// </summary>
[PseudoClasses(":pressed")]
public class Button : ContentControl
{
/// <summary>

2
src/Avalonia.Controls/ButtonSpinner.cs

@ -1,4 +1,5 @@
using System;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Data;
using Avalonia.Input;
@ -15,6 +16,7 @@ namespace Avalonia.Controls
/// <summary>
/// Represents a spinner control that includes two Buttons.
/// </summary>
[PseudoClasses(":left", ":right")]
public class ButtonSpinner : Spinner
{
/// <summary>

2
src/Avalonia.Controls/Calendar/CalendarButton.cs

@ -3,6 +3,7 @@
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
using Avalonia.Controls.Metadata;
using Avalonia.Input;
using System;
@ -12,6 +13,7 @@ namespace Avalonia.Controls.Primitives
/// Represents a button on a
/// <see cref="T:Avalonia.Controls.Calendar" />.
/// </summary>
[PseudoClasses(":selected", ":inactive", ":btnfocused")]
public sealed class CalendarButton : Button
{
/// <summary>

2
src/Avalonia.Controls/Calendar/CalendarDayButton.cs

@ -5,10 +5,12 @@
using System;
using System.Globalization;
using Avalonia.Controls.Metadata;
using Avalonia.Input;
namespace Avalonia.Controls.Primitives
{
[PseudoClasses(":pressed", ":disabled", ":selected", ":inactive", ":today", ":blackout", ":dayfocused")]
public sealed class CalendarDayButton : Button
{
/// <summary>

2
src/Avalonia.Controls/Calendar/CalendarItem.cs

@ -7,6 +7,7 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using Avalonia.Controls.Metadata;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Interactivity;
@ -18,6 +19,7 @@ namespace Avalonia.Controls.Primitives
/// Represents the currently displayed month or year on a
/// <see cref="T:Avalonia.Controls.Calendar" />.
/// </summary>
[PseudoClasses(":calendardisabled")]
public sealed class CalendarItem : TemplatedControl
{
/// <summary>

2
src/Avalonia.Controls/Chrome/CaptionButtons.cs

@ -1,5 +1,6 @@
using System;
using System.Reactive.Disposables;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
#nullable enable
@ -9,6 +10,7 @@ namespace Avalonia.Controls.Chrome
/// <summary>
/// Draws window minimize / maximize / close buttons in a <see cref="TitleBar"/> when managed client decorations are enabled.
/// </summary>
[PseudoClasses(":minimized", ":normal", ":maximized", ":fullscreen")]
public class CaptionButtons : TemplatedControl
{
private CompositeDisposable? _disposables;

2
src/Avalonia.Controls/Chrome/TitleBar.cs

@ -1,5 +1,6 @@
using System;
using System.Reactive.Disposables;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
#nullable enable
@ -9,6 +10,7 @@ namespace Avalonia.Controls.Chrome
/// <summary>
/// Draws a titlebar when managed client decorations are enabled.
/// </summary>
[PseudoClasses(":minimized", ":normal", ":maximized", ":fullscreen")]
public class TitleBar : TemplatedControl
{
private CompositeDisposable? _disposables;

2
src/Avalonia.Controls/DataValidationErrors.cs

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Templates;
using Avalonia.Data;
@ -14,6 +15,7 @@ namespace Avalonia.Controls
/// <remarks>
/// You will probably only want to create instances inside of control templates.
/// </remarks>
[PseudoClasses(":error")]
public class DataValidationErrors : ContentControl
{
/// <summary>

4
src/Avalonia.Controls/DateTimePickers/DatePicker.cs

@ -1,4 +1,5 @@
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Shapes;
using Avalonia.Controls.Templates;
using Avalonia.Interactivity;
@ -11,6 +12,7 @@ namespace Avalonia.Controls
/// <summary>
/// A control to allow the user to select a date
/// </summary>
[PseudoClasses(":hasnodate")]
public class DatePicker : TemplatedControl
{
/// <summary>

4
src/Avalonia.Controls/DateTimePickers/TimePicker.cs

@ -1,4 +1,5 @@
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Shapes;
using Avalonia.Controls.Templates;
using System;
@ -9,6 +10,7 @@ namespace Avalonia.Controls
/// <summary>
/// A control to allow the user to select a time
/// </summary>
[PseudoClasses(":hasnotime")]
public class TimePicker : TemplatedControl
{
/// <summary>

3
src/Avalonia.Controls/Expander.cs

@ -1,6 +1,6 @@
using Avalonia.Animation;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Data;
namespace Avalonia.Controls
{
@ -12,6 +12,7 @@ namespace Avalonia.Controls
Right
}
[PseudoClasses(":expanded", ":up", ":down", ":left", ":right")]
public class Expander : HeaderedContentControl
{
public static readonly StyledProperty<IPageTransition> ContentTransitionProperty =

2
src/Avalonia.Controls/ItemsControl.cs

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Collections.Specialized;
using Avalonia.Collections;
using Avalonia.Controls.Generators;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
@ -18,6 +19,7 @@ namespace Avalonia.Controls
/// <summary>
/// Displays a collection of items.
/// </summary>
[PseudoClasses(":empty", ":singleitem")]
public class ItemsControl : TemplatedControl, IItemsPresenterHost, ICollectionChangedListener
{
/// <summary>

1
src/Avalonia.Controls/ListBox.cs

@ -163,6 +163,7 @@ namespace Avalonia.Controls
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
Scroll = e.NameScope.Find<IScrollable>("PART_ScrollViewer");
}
}

2
src/Avalonia.Controls/ListBoxItem.cs

@ -1,3 +1,4 @@
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Mixins;
using Avalonia.Input;
@ -6,6 +7,7 @@ namespace Avalonia.Controls
/// <summary>
/// A selectable item in a <see cref="ListBox"/>.
/// </summary>
[PseudoClasses(":pressed", ":selected")]
public class ListBoxItem : ContentControl, ISelectable
{
/// <summary>

2
src/Avalonia.Controls/MenuItem.cs

@ -4,6 +4,7 @@ using System.Linq;
using System.Reactive.Linq;
using System.Windows.Input;
using Avalonia.Controls.Generators;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Mixins;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
@ -20,6 +21,7 @@ namespace Avalonia.Controls
/// <summary>
/// A menu item control.
/// </summary>
[PseudoClasses(":separator", ":icon", ":open", ":pressed", ":selected")]
public class MenuItem : HeaderedSelectingItemsControl, IMenuItem, ISelectable
{
/// <summary>

2
src/Avalonia.Controls/Notifications/NotificationCard.cs

@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Reactive.Linq;
using Avalonia.Controls.Metadata;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
@ -9,6 +10,7 @@ namespace Avalonia.Controls.Notifications
/// <summary>
/// Control that represents and displays a notification.
/// </summary>
[PseudoClasses(":error", ":information", ":success", ":warning")]
public class NotificationCard : ContentControl
{
private bool _isClosed;

2
src/Avalonia.Controls/Notifications/WindowNotificationManager.cs

@ -7,12 +7,14 @@ using Avalonia.Controls.Primitives;
using Avalonia.Rendering;
using Avalonia.Data;
using Avalonia.VisualTree;
using Avalonia.Controls.Metadata;
namespace Avalonia.Controls.Notifications
{
/// <summary>
/// An <see cref="INotificationManager"/> that displays notifications in a <see cref="Window"/>.
/// </summary>
[PseudoClasses(":topleft", ":topright", ":bottomleft", ":bottomright")]
public class WindowNotificationManager : TemplatedControl, IManagedNotificationManager, ICustomSimpleHitTest
{
private IList _items;

2
src/Avalonia.Controls/Primitives/PopupPositioning/ManagedPopupPositioner.cs

@ -221,7 +221,7 @@ namespace Avalonia.Controls.Primitives.PopupPositioning
if (!FitsInBounds(unconstrainedRect, PopupAnchor.Bottom))
{
unconstrainedRect = unconstrainedRect.WithHeight(bounds.Height - unconstrainedRect.Y);
unconstrainedRect = unconstrainedRect.WithHeight(bounds.Bottom - unconstrainedRect.Y);
}
if (IsValid(unconstrainedRect))

2
src/Avalonia.Controls/Primitives/ScrollBar.cs

@ -4,6 +4,7 @@ using Avalonia.Interactivity;
using Avalonia.Input;
using Avalonia.Layout;
using Avalonia.Threading;
using Avalonia.Controls.Metadata;
namespace Avalonia.Controls.Primitives
{
@ -21,6 +22,7 @@ namespace Avalonia.Controls.Primitives
/// <summary>
/// A scrollbar control.
/// </summary>
[PseudoClasses(":vertical", ":horizontal")]
public class ScrollBar : RangeBase
{
/// <summary>

339
src/Avalonia.Controls/Primitives/SelectingItemsControl.cs

@ -6,7 +6,6 @@ using System.ComponentModel;
using System.Linq;
using Avalonia.Controls.Generators;
using Avalonia.Controls.Selection;
using Avalonia.Controls.Utils;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Input.Platform;
@ -70,8 +69,8 @@ namespace Avalonia.Controls.Primitives
/// <summary>
/// Defines the <see cref="SelectedItems"/> property.
/// </summary>
protected static readonly DirectProperty<SelectingItemsControl, IList> SelectedItemsProperty =
AvaloniaProperty.RegisterDirect<SelectingItemsControl, IList>(
protected static readonly DirectProperty<SelectingItemsControl, IList?> SelectedItemsProperty =
AvaloniaProperty.RegisterDirect<SelectingItemsControl, IList?>(
nameof(SelectedItems),
o => o.SelectedItems,
(o, v) => o.SelectedItems = v);
@ -111,12 +110,13 @@ namespace Avalonia.Controls.Primitives
RoutingStrategies.Bubble);
private static readonly IList Empty = Array.Empty<object>();
private SelectedItemsSync? _selectedItemsSync;
private ISelectionModel? _selection;
private int _oldSelectedIndex;
private object? _oldSelectedItem;
private int _initializing;
private IList? _oldSelectedItems;
private bool _ignoreContainerSelectionChanged;
private UpdateState? _updateState;
private bool _hasScrolledToSelectedItem;
/// <summary>
/// Initializes static members of the <see cref="SelectingItemsControl"/> class.
@ -149,8 +149,27 @@ namespace Avalonia.Controls.Primitives
/// </summary>
public int SelectedIndex
{
get => Selection.SelectedIndex;
set => Selection.SelectedIndex = value;
get
{
// When a Begin/EndInit/DataContext update is in place we return the value to be
// updated here, even though it's not yet active and the property changed notification
// has not yet been raised. If we don't do this then the old value will be written back
// to the source when two-way bound, and the update value will be lost.
return _updateState?.SelectedIndex.HasValue == true ?
_updateState.SelectedIndex.Value :
Selection.SelectedIndex;
}
set
{
if (_updateState is object)
{
_updateState.SelectedIndex = value;
}
else
{
Selection.SelectedIndex = value;
}
}
}
/// <summary>
@ -158,17 +177,67 @@ namespace Avalonia.Controls.Primitives
/// </summary>
public object? SelectedItem
{
get => Selection.SelectedItem;
set => Selection.SelectedItem = value;
get
{
// See SelectedIndex setter for more information.
return _updateState?.SelectedItem.HasValue == true ?
_updateState.SelectedItem.Value :
Selection.SelectedItem;
}
set
{
if (_updateState is object)
{
_updateState.SelectedItem = value;
}
else
{
Selection.SelectedItem = value;
}
}
}
/// <summary>
/// Gets or sets the selected items.
/// </summary>
protected IList SelectedItems
/// <remarks>
/// By default returns a collection that can be modified in order to manipulate the control
/// selection, however this property will return null if <see cref="Selection"/> is
/// re-assigned; you should only use _either_ Selection or SelectedItems.
/// </remarks>
protected IList? SelectedItems
{
get => SelectedItemsSync.SelectedItems;
set => SelectedItemsSync.SelectedItems = value;
get
{
// See SelectedIndex setter for more information.
if (_updateState?.SelectedItems.HasValue == true)
{
return _updateState.SelectedItems.Value;
}
else if (Selection is InternalSelectionModel ism)
{
var result = ism.WritableSelectedItems;
_oldSelectedItems = result;
return result;
}
return null;
}
set
{
if (_updateState is object)
{
_updateState.SelectedItems = new Optional<IList?>(value);
}
else if (Selection is InternalSelectionModel i)
{
i.WritableSelectedItems = value;
}
else
{
throw new InvalidOperationException("Cannot set both Selection and SelectedItems.");
}
}
}
/// <summary>
@ -178,19 +247,30 @@ namespace Avalonia.Controls.Primitives
{
get
{
if (_selection is null)
if (_updateState?.Selection.HasValue == true)
{
_selection = CreateDefaultSelectionModel();
InitializeSelectionModel(_selection);
return _updateState.Selection.Value;
}
else
{
if (_selection is null)
{
_selection = CreateDefaultSelectionModel();
InitializeSelectionModel(_selection);
}
return _selection;
return _selection;
}
}
set
{
value ??= CreateDefaultSelectionModel();
if (_selection != value)
if (_updateState is object)
{
_updateState.Selection = new Optional<ISelectionModel>(value);
}
else if (_selection != value)
{
if (value.Source != null && value.Source != Items)
{
@ -212,6 +292,15 @@ namespace Avalonia.Controls.Primitives
}
InitializeSelectionModel(_selection);
if (_oldSelectedItems != SelectedItems)
{
RaisePropertyChanged(
SelectedItemsProperty,
new Optional<IList?>(_oldSelectedItems),
new BindingValue<IList?>(SelectedItems));
_oldSelectedItems = SelectedItems;
}
}
}
}
@ -234,20 +323,18 @@ namespace Avalonia.Controls.Primitives
/// </summary>
protected bool AlwaysSelected => (SelectionMode & SelectionMode.AlwaysSelected) != 0;
private SelectedItemsSync SelectedItemsSync => _selectedItemsSync ??= new SelectedItemsSync(Selection);
/// <inheritdoc/>
public override void BeginInit()
{
base.BeginInit();
++_initializing;
BeginUpdating();
}
/// <inheritdoc/>
public override void EndInit()
{
base.EndInit();
--_initializing;
EndUpdating();
}
/// <summary>
@ -295,6 +382,28 @@ namespace Avalonia.Controls.Primitives
}
}
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
AutoScrollToSelectedItemIfNecessary();
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
void ExecuteScrollWhenLayoutUpdated(object sender, EventArgs e)
{
LayoutUpdated -= ExecuteScrollWhenLayoutUpdated;
AutoScrollToSelectedItemIfNecessary();
}
if (AutoScrollToSelectedItem)
{
LayoutUpdated += ExecuteScrollWhenLayoutUpdated;
}
}
/// <inheritdoc/>
protected override void OnContainersMaterialized(ItemContainerEventArgs e)
{
@ -351,30 +460,14 @@ namespace Avalonia.Controls.Primitives
protected override void OnDataContextBeginUpdate()
{
base.OnDataContextBeginUpdate();
++_initializing;
if (_selection is object)
{
_selection.Source = null;
}
BeginUpdating();
}
/// <inheritdoc/>
protected override void OnDataContextEndUpdate()
{
base.OnDataContextEndUpdate();
--_initializing;
if (_selection is object && _initializing == 0)
{
_selection.Source = Items;
if (Items is null)
{
_selection.Clear();
_selectedItemsSync?.SelectedItems?.Clear();
}
}
EndUpdating();
}
protected override void OnInitialized()
@ -398,8 +491,7 @@ namespace Avalonia.Controls.Primitives
if (ItemCount > 0 &&
Match(keymap.SelectAll) &&
(((SelectionMode & SelectionMode.Multiple) != 0) ||
(SelectionMode & SelectionMode.Toggle) != 0))
SelectionMode.HasFlag(SelectionMode.Multiple))
{
Selection.SelectAll();
e.Handled = true;
@ -411,9 +503,11 @@ namespace Avalonia.Controls.Primitives
{
base.OnPropertyChanged(change);
if (change.Property == ItemsProperty &&
_initializing == 0 &&
_selection is object)
if (change.Property == AutoScrollToSelectedItemProperty)
{
AutoScrollToSelectedItemIfNecessary();
}
if (change.Property == ItemsProperty && _updateState is null && _selection is object)
{
var newValue = change.NewValue.GetValueOrDefault<IEnumerable>();
_selection.Source = newValue;
@ -601,23 +695,30 @@ namespace Avalonia.Controls.Primitives
/// <param name="e">The event args.</param>
private void OnSelectionModelPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(ISelectionModel.AnchorIndex) && AutoScrollToSelectedItem)
if (e.PropertyName == nameof(ISelectionModel.AnchorIndex))
{
if (Selection.AnchorIndex > 0)
{
ScrollIntoView(Selection.AnchorIndex);
}
_hasScrolledToSelectedItem = false;
AutoScrollToSelectedItemIfNecessary();
}
else if (e.PropertyName == nameof(ISelectionModel.SelectedIndex))
else if (e.PropertyName == nameof(ISelectionModel.SelectedIndex) && _oldSelectedIndex != SelectedIndex)
{
RaisePropertyChanged(SelectedIndexProperty, _oldSelectedIndex, SelectedIndex);
_oldSelectedIndex = SelectedIndex;
}
else if (e.PropertyName == nameof(ISelectionModel.SelectedItem))
else if (e.PropertyName == nameof(ISelectionModel.SelectedItem) && _oldSelectedItem != SelectedItem)
{
RaisePropertyChanged(SelectedItemProperty, _oldSelectedItem, SelectedItem);
_oldSelectedItem = SelectedItem;
}
else if (e.PropertyName == nameof(InternalSelectionModel.WritableSelectedItems) &&
_oldSelectedItems != (Selection as InternalSelectionModel)?.SelectedItems)
{
RaisePropertyChanged(
SelectedItemsProperty,
new Optional<IList?>(_oldSelectedItems),
new BindingValue<IList?>(SelectedItems));
_oldSelectedItems = SelectedItems;
}
}
/// <summary>
@ -674,6 +775,19 @@ namespace Avalonia.Controls.Primitives
}
}
private void AutoScrollToSelectedItemIfNecessary()
{
if (AutoScrollToSelectedItem &&
!_hasScrolledToSelectedItem &&
Presenter is object &&
Selection.AnchorIndex >= 0 &&
((IVisual)this).IsAttachedToVisualTree)
{
ScrollIntoView(Selection.AnchorIndex);
_hasScrolledToSelectedItem = true;
}
}
/// <summary>
/// Called when a container raises the <see cref="IsSelectedChangedEvent"/>.
/// </summary>
@ -734,14 +848,6 @@ namespace Avalonia.Controls.Primitives
}
}
private void MarkContainersUnselected()
{
foreach (var container in ItemContainerGenerator.Containers)
{
MarkContainerSelected(container.ContainerControl, false);
}
}
/// <summary>
/// Sets an item container's 'selected' class or <see cref="ISelectable.IsSelected"/>.
/// </summary>
@ -757,23 +863,6 @@ namespace Avalonia.Controls.Primitives
}
}
/// <summary>
/// Sets an item container's 'selected' class or <see cref="ISelectable.IsSelected"/>.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="selected">Whether the item should be selected or deselected.</param>
private int MarkItemSelected(object item, bool selected)
{
var index = IndexOf(Items, item);
if (index != -1)
{
MarkItemSelected(index, selected);
}
return index;
}
private void UpdateContainerSelection()
{
if (Presenter?.Panel is IPanel panel)
@ -789,7 +878,7 @@ namespace Avalonia.Controls.Primitives
private ISelectionModel CreateDefaultSelectionModel()
{
return new SelectionModel<object>
return new InternalSelectionModel
{
SingleSelect = !SelectionMode.HasFlagCustom(SelectionMode.Multiple),
};
@ -797,7 +886,7 @@ namespace Avalonia.Controls.Primitives
private void InitializeSelectionModel(ISelectionModel model)
{
if (_initializing == 0)
if (_updateState is null)
{
model.Source = Items;
}
@ -825,9 +914,6 @@ namespace Avalonia.Controls.Primitives
UpdateContainerSelection();
_selectedItemsSync ??= new SelectedItemsSync(model);
_selectedItemsSync.SelectionModel = model;
if (SelectedIndex != -1)
{
RaiseEvent(new SelectionChangedEventArgs(
@ -845,5 +931,96 @@ namespace Avalonia.Controls.Primitives
model.SelectionChanged -= OnSelectionModelSelectionChanged;
}
}
private void BeginUpdating()
{
_updateState ??= new UpdateState();
_updateState.UpdateCount++;
}
private void EndUpdating()
{
if (_updateState is object && --_updateState.UpdateCount == 0)
{
var state = _updateState;
_updateState = null;
if (state.Selection.HasValue)
{
Selection = state.Selection.Value;
}
if (state.SelectedItems.HasValue)
{
SelectedItems = state.SelectedItems.Value;
}
Selection.Source = Items;
if (Items is null)
{
Selection.Clear();
}
if (state.SelectedIndex.HasValue)
{
SelectedIndex = state.SelectedIndex.Value;
}
else if (state.SelectedItem.HasValue)
{
SelectedItem = state.SelectedItem.Value;
}
}
}
// When in a BeginInit..EndInit block, or when the DataContext is updating, we need to
// defer changes to the selection model because we have no idea in which order properties
// will be set. Consider:
//
// - Both Items and SelectedItem are bound
// - The DataContext changes
// - The binding for SelectedItem updates first, producing an item
// - Items is searched to find the index of the new selected item
// - However Items isn't yet updated; the item is not found
// - SelectedIndex is incorrectly set to -1
//
// This logic cannot be encapsulated in SelectionModel because the selection model can also
// be bound, consider:
//
// - Both Items and Selection are bound
// - The DataContext changes
// - The binding for Items updates first
// - The new items are assigned to Selection.Source
// - The binding for Selection updates, producing a new SelectionModel
// - Both the old and new SelectionModels have the incorrect Source
private class UpdateState
{
private Optional<int> _selectedIndex;
private Optional<object?> _selectedItem;
public int UpdateCount { get; set; }
public Optional<ISelectionModel> Selection { get; set; }
public Optional<IList?> SelectedItems { get; set; }
public Optional<int> SelectedIndex
{
get => _selectedIndex;
set
{
_selectedIndex = value;
_selectedItem = default;
}
}
public Optional<object?> SelectedItem
{
get => _selectedItem;
set
{
_selectedItem = value;
_selectedIndex = default;
}
}
}
}
}

2
src/Avalonia.Controls/Primitives/Thumb.cs

@ -1,9 +1,11 @@
using System;
using Avalonia.Controls.Metadata;
using Avalonia.Input;
using Avalonia.Interactivity;
namespace Avalonia.Controls.Primitives
{
[PseudoClasses(":pressed")]
public class Thumb : TemplatedControl
{
public static readonly RoutedEvent<VectorEventArgs> DragStartedEvent =

2
src/Avalonia.Controls/Primitives/ToggleButton.cs

@ -1,4 +1,5 @@
using System;
using Avalonia.Controls.Metadata;
using Avalonia.Data;
using Avalonia.Interactivity;
@ -7,6 +8,7 @@ namespace Avalonia.Controls.Primitives
/// <summary>
/// Represents a control that a user can select (check) or clear (uncheck). Base class for controls that can switch states.
/// </summary>
[PseudoClasses(":checked", ":unchecked", ":indeterminate")]
public class ToggleButton : Button
{
/// <summary>

2
src/Avalonia.Controls/Primitives/Track.cs

@ -4,6 +4,7 @@
// Licensed to The Avalonia Project under MIT License, courtesy of The .NET Foundation.
using System;
using Avalonia.Controls.Metadata;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Layout;
@ -12,6 +13,7 @@ using Avalonia.Utilities;
namespace Avalonia.Controls.Primitives
{
[PseudoClasses(":vertical", ":horizontal")]
public class Track : Control
{
public static readonly DirectProperty<Track, double> MinimumProperty =

2
src/Avalonia.Controls/ProgressBar.cs

@ -1,4 +1,5 @@
using System;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Layout;
using Avalonia.Media;
@ -8,6 +9,7 @@ namespace Avalonia.Controls
/// <summary>
/// A control used to indicate the progress of an operation.
/// </summary>
[PseudoClasses(":vertical", ":horizontal", ":indeterminate")]
public class ProgressBar : RangeBase
{
public class ProgressBarTemplateProperties : AvaloniaObject

36
src/Avalonia.Controls/ScrollViewer.cs

@ -448,6 +448,38 @@ namespace Avalonia.Controls
Offset += new Vector(_smallChange.Width, 0);
}
/// <summary>
/// Scrolls the content upward by one page.
/// </summary>
public void PageUp()
{
VerticalScrollBarValue = Math.Max(_offset.Y - _viewport.Height, 0);
}
/// <summary>
/// Scrolls the content downward by one page.
/// </summary>
public void PageDown()
{
VerticalScrollBarValue = Math.Min(_offset.Y + _viewport.Height, VerticalScrollBarMaximum);
}
/// <summary>
/// Scrolls the content left by one page.
/// </summary>
public void PageLeft()
{
HorizontalScrollBarValue = Math.Max(_offset.X - _viewport.Width, 0);
}
/// <summary>
/// Scrolls the content tight by one page.
/// </summary>
public void PageRight()
{
HorizontalScrollBarValue = Math.Min(_offset.X + _viewport.Width, HorizontalScrollBarMaximum);
}
/// <summary>
/// Scrolls to the top-left corner of the content.
/// </summary>
@ -623,12 +655,12 @@ namespace Avalonia.Controls
{
if (e.Key == Key.PageUp)
{
VerticalScrollBarValue = Math.Max(_offset.Y - _viewport.Height, 0);
PageUp();
e.Handled = true;
}
else if (e.Key == Key.PageDown)
{
VerticalScrollBarValue = Math.Min(_offset.Y + _viewport.Height, VerticalScrollBarMaximum);
PageDown();
e.Handled = true;
}
}

278
src/Avalonia.Controls/Selection/InternalSelectionModel.cs

@ -0,0 +1,278 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Avalonia.Collections;
#nullable enable
namespace Avalonia.Controls.Selection
{
internal class InternalSelectionModel : SelectionModel<object?>
{
private IList? _writableSelectedItems;
private bool _ignoreModelChanges;
private bool _ignoreSelectedItemsChanges;
public InternalSelectionModel()
{
SelectionChanged += OnSelectionChanged;
SourceReset += OnSourceReset;
}
[AllowNull]
public IList WritableSelectedItems
{
get
{
if (_writableSelectedItems is null)
{
_writableSelectedItems = new AvaloniaList<object?>();
SubscribeToSelectedItems();
}
return _writableSelectedItems;
}
set
{
value ??= new AvaloniaList<object?>();
if (value.IsFixedSize)
{
throw new NotSupportedException("Cannot assign fixed size selection to SelectedItems.");
}
if (_writableSelectedItems != value)
{
UnsubscribeFromSelectedItems();
_writableSelectedItems = value;
SyncFromSelectedItems();
SubscribeToSelectedItems();
if (ItemsView is null)
{
SetInitSelectedItems(value);
}
RaisePropertyChanged(nameof(WritableSelectedItems));
}
}
}
private protected override void SetSource(IEnumerable? value)
{
if (Source == value)
{
return;
}
object?[]? oldSelection = null;
if (Source is object && value is object)
{
oldSelection = new object?[WritableSelectedItems.Count];
WritableSelectedItems.CopyTo(oldSelection, 0);
}
try
{
_ignoreSelectedItemsChanges = true;
base.SetSource(value);
}
finally
{
_ignoreSelectedItemsChanges = false;
}
if (oldSelection is null)
{
SyncToSelectedItems();
}
else
{
foreach (var i in oldSelection)
{
var index = ItemsView!.IndexOf(i);
Select(index);
}
}
}
private void SyncToSelectedItems()
{
if (_writableSelectedItems is object)
{
try
{
_ignoreSelectedItemsChanges = true;
_writableSelectedItems.Clear();
foreach (var i in base.SelectedItems)
{
_writableSelectedItems.Add(i);
}
}
finally
{
_ignoreSelectedItemsChanges = false;
}
}
}
private void SyncFromSelectedItems()
{
if (Source is null || _writableSelectedItems is null)
{
return;
}
try
{
_ignoreModelChanges = true;
using (BatchUpdate())
{
Clear();
Add(_writableSelectedItems);
}
}
finally
{
_ignoreModelChanges = false;
}
}
private void SubscribeToSelectedItems()
{
if (_writableSelectedItems is INotifyCollectionChanged incc)
{
incc.CollectionChanged += OnSelectedItemsCollectionChanged;
}
}
private void UnsubscribeFromSelectedItems()
{
if (_writableSelectedItems is INotifyCollectionChanged incc)
{
incc.CollectionChanged += OnSelectedItemsCollectionChanged;
}
}
private void OnSelectionChanged(object sender, SelectionModelSelectionChangedEventArgs e)
{
if (_ignoreModelChanges)
{
return;
}
try
{
var items = WritableSelectedItems;
var deselected = e.DeselectedItems.ToList();
var selected = e.SelectedItems.ToList();
_ignoreSelectedItemsChanges = true;
foreach (var i in deselected)
{
items.Remove(i);
}
foreach (var i in selected)
{
items.Add(i);
}
}
finally
{
_ignoreSelectedItemsChanges = false;
}
}
private void OnSourceReset(object sender, EventArgs e) => SyncFromSelectedItems();
private void OnSelectedItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (_ignoreSelectedItemsChanges)
{
return;
}
if (_writableSelectedItems == null)
{
throw new AvaloniaInternalException("CollectionChanged raised but we don't have items.");
}
void Remove()
{
foreach (var i in e.OldItems)
{
var index = IndexOf(Source, i);
if (index != -1)
{
Deselect(index);
}
}
}
try
{
using var operation = BatchUpdate();
_ignoreModelChanges = true;
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
Add(e.NewItems);
break;
case NotifyCollectionChangedAction.Remove:
Remove();
break;
case NotifyCollectionChangedAction.Replace:
Remove();
Add(e.NewItems);
break;
case NotifyCollectionChangedAction.Reset:
Clear();
Add(_writableSelectedItems);
break;
}
}
finally
{
_ignoreModelChanges = false;
}
}
private void Add(IList newItems)
{
foreach (var i in newItems)
{
var index = IndexOf(Source, i);
if (index != -1)
{
Select(index);
}
}
}
private static int IndexOf(object? source, object? item)
{
if (source is IList l)
{
return l.IndexOf(item);
}
else if (source is ItemsSourceView v)
{
return v.IndexOf(item);
}
return -1;
}
}
}

113
src/Avalonia.Controls/Selection/SelectionModel.cs

@ -20,8 +20,7 @@ namespace Avalonia.Controls.Selection
private SelectedItems<T>? _selectedItems;
private SelectedItems<T>.Untyped? _selectedItemsUntyped;
private EventHandler<SelectionModelSelectionChangedEventArgs>? _untypedSelectionChanged;
[AllowNull] private T _initSelectedItem = default;
private bool _hasInitSelectedItem;
private IList? _initSelectedItems;
public SelectionModel()
{
@ -82,7 +81,19 @@ namespace Avalonia.Controls.Selection
[MaybeNull, AllowNull]
public T SelectedItem
{
get => ItemsView is object ? GetItemAt(_selectedIndex) : _initSelectedItem;
get
{
if (ItemsView is object)
{
return GetItemAt(_selectedIndex);
}
else if (_initSelectedItems is object && _initSelectedItems.Count > 0)
{
return (T)_initSelectedItems[0];
}
return default;
}
set
{
if (ItemsView is object)
@ -92,8 +103,9 @@ namespace Avalonia.Controls.Selection
else
{
Clear();
_initSelectedItem = value;
_hasInitSelectedItem = true;
#pragma warning disable CS8601
SetInitSelectedItems(new T[] { value });
#pragma warning restore CS8601
}
}
}
@ -102,9 +114,10 @@ namespace Avalonia.Controls.Selection
{
get
{
if (ItemsView is null && _hasInitSelectedItem)
if (ItemsView is null && _initSelectedItems is object)
{
return new[] { _initSelectedItem };
return _initSelectedItems is IReadOnlyList<T> i ?
i : _initSelectedItems.Cast<T>().ToList();
}
return _selectedItems ??= new SelectedItems<T>(this);
@ -229,12 +242,7 @@ namespace Avalonia.Controls.Selection
{
using var update = BatchUpdate();
var o = update.Operation;
var range = CoerceRange(start, end);
if (range.Begin == -1)
{
return;
}
var range = new IndexRange(Math.Max(0, start), end);
if (RangesEnabled)
{
@ -258,8 +266,7 @@ namespace Avalonia.Controls.Selection
o.SelectedIndex = -1;
}
_initSelectedItem = default;
_hasInitSelectedItem = false;
_initSelectedItems = null;
}
public void SelectAll() => SelectRange(0, int.MaxValue);
@ -270,7 +277,7 @@ namespace Avalonia.Controls.Selection
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void SetSource(IEnumerable? value)
private protected virtual void SetSource(IEnumerable? value)
{
if (base.Source != value)
{
@ -292,11 +299,14 @@ namespace Avalonia.Controls.Selection
{
update.Operation.IsSourceUpdate = true;
if (_hasInitSelectedItem)
if (_initSelectedItems is object && ItemsView is object)
{
SelectedItem = _initSelectedItem;
_initSelectedItem = default;
_hasInitSelectedItem = false;
foreach (T i in _initSelectedItems)
{
Select(ItemsView.IndexOf(i));
}
_initSelectedItems = null;
}
else
{
@ -345,7 +355,9 @@ namespace Avalonia.Controls.Selection
LostSelection(this, EventArgs.Empty);
}
CommitOperation(update.Operation);
// Don't raise PropertyChanged events here as the OnSourceCollectionChanged event that
// let to this method being called will raise them if necessary.
CommitOperation(update.Operation, raisePropertyChanged: false);
}
private protected override CollectionChangeState OnItemsAdded(int index, IList items)
@ -430,6 +442,11 @@ namespace Avalonia.Controls.Selection
RaisePropertyChanged(nameof(SelectedIndex));
}
if (e.Action == NotifyCollectionChangedAction.Remove && e.OldStartingIndex <= oldSelectedIndex)
{
RaisePropertyChanged(nameof(SelectedItem));
}
if (oldAnchorIndex != _anchorIndex)
{
RaisePropertyChanged(nameof(AnchorIndex));
@ -459,6 +476,16 @@ namespace Avalonia.Controls.Selection
return true;
}
private protected void SetInitSelectedItems(IList items)
{
if (Source is object)
{
throw new InvalidOperationException("Cannot set init selected items when Source is set.");
}
_initSelectedItems = items;
}
protected override void OnSourceCollectionChangeFinished()
{
if (_operation is object)
@ -532,8 +559,7 @@ namespace Avalonia.Controls.Selection
o.SelectedIndex = o.AnchorIndex = start;
}
_initSelectedItem = default;
_hasInitSelectedItem = false;
_initSelectedItems = null;
}
[return: MaybeNull]
@ -611,7 +637,7 @@ namespace Avalonia.Controls.Selection
}
}
private void CommitOperation(Operation operation)
private void CommitOperation(Operation operation, bool raisePropertyChanged = true)
{
try
{
@ -679,23 +705,34 @@ namespace Avalonia.Controls.Selection
}
}
if (oldSelectedIndex != _selectedIndex)
if (raisePropertyChanged)
{
indexesChanged = true;
RaisePropertyChanged(nameof(SelectedIndex));
RaisePropertyChanged(nameof(SelectedItem));
}
if (oldSelectedIndex != _selectedIndex)
{
indexesChanged = true;
RaisePropertyChanged(nameof(SelectedIndex));
}
if (oldAnchorIndex != _anchorIndex)
{
indexesChanged = true;
RaisePropertyChanged(nameof(AnchorIndex));
}
if (oldSelectedIndex != _selectedIndex || operation.IsSourceUpdate)
{
RaisePropertyChanged(nameof(SelectedItem));
}
if (indexesChanged)
{
RaisePropertyChanged(nameof(SelectedIndexes));
RaisePropertyChanged(nameof(SelectedItems));
if (oldAnchorIndex != _anchorIndex)
{
indexesChanged = true;
RaisePropertyChanged(nameof(AnchorIndex));
}
if (indexesChanged)
{
RaisePropertyChanged(nameof(SelectedIndexes));
}
if (indexesChanged || operation.IsSourceUpdate)
{
RaisePropertyChanged(nameof(SelectedItems));
}
}
}
finally

2
src/Avalonia.Controls/Slider.cs

@ -1,5 +1,6 @@
using System;
using Avalonia.Collections;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Mixins;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
@ -39,6 +40,7 @@ namespace Avalonia.Controls
/// <summary>
/// A control that lets the user select from a range of values by moving a Thumb control along a Track.
/// </summary>
[PseudoClasses(":vertical", ":horizontal", ":pressed")]
public class Slider : RangeBase
{
/// <summary>

7
src/Avalonia.Controls/SplitView.cs

@ -1,4 +1,5 @@
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Input.Raw;
using Avalonia.Interactivity;
@ -73,6 +74,10 @@ namespace Avalonia.Controls
/// <summary>
/// A control with two views: A collapsible pane and an area for content
/// </summary>
[PseudoClasses(":open", ":closed")]
[PseudoClasses(":compactoverlay", ":compactinline", ":overlay", ":inline")]
[PseudoClasses(":left", ":right")]
[PseudoClasses(":lightdismiss")]
public class SplitView : TemplatedControl
{
/*

55
src/Avalonia.Controls/TabControl.cs

@ -1,3 +1,4 @@
using System.ComponentModel;
using System.Linq;
using Avalonia.Collections;
using Avalonia.Controls.Generators;
@ -66,7 +67,7 @@ namespace Avalonia.Controls
SelectionModeProperty.OverrideDefaultValue<TabControl>(SelectionMode.AlwaysSelected);
ItemsPanelProperty.OverrideDefaultValue<TabControl>(DefaultPanel);
AffectsMeasure<TabControl>(TabStripPlacementProperty);
SelectedIndexProperty.Changed.AddClassHandler<TabControl>((x, e) => x.UpdateSelectedContent(e));
SelectedItemProperty.Changed.AddClassHandler<TabControl>((x, e) => x.UpdateSelectedContent());
}
/// <summary>
@ -145,55 +146,27 @@ namespace Avalonia.Controls
protected override void OnContainersMaterialized(ItemContainerEventArgs e)
{
base.OnContainersMaterialized(e);
if (SelectedContent != null || SelectedIndex == -1)
{
return;
}
var container = (TabItem)ItemContainerGenerator.ContainerFromIndex(SelectedIndex);
if (container == null)
{
return;
}
UpdateSelectedContent(container);
UpdateSelectedContent();
}
private void UpdateSelectedContent(AvaloniaPropertyChangedEventArgs e)
protected override void OnContainersRecycled(ItemContainerEventArgs e)
{
var index = (int)e.NewValue;
if (index == -1)
{
SelectedContentTemplate = null;
SelectedContent = null;
return;
}
var container = (TabItem)ItemContainerGenerator.ContainerFromIndex(index);
if (container == null)
{
return;
}
UpdateSelectedContent(container);
base.OnContainersRecycled(e);
UpdateSelectedContent();
}
private void UpdateSelectedContent(IContentControl item)
private void UpdateSelectedContent()
{
if (SelectedContentTemplate != item.ContentTemplate)
if (SelectedIndex == -1)
{
SelectedContentTemplate = item.ContentTemplate;
SelectedContent = SelectedContentTemplate = null;
}
if (SelectedContent != item.Content)
else
{
SelectedContent = item.Content;
var container = SelectedItem as IContentControl ??
ItemContainerGenerator.ContainerFromIndex(SelectedIndex) as IContentControl;
SelectedContentTemplate = container?.ContentTemplate;
SelectedContent = container?.Content;
}
}

2
src/Avalonia.Controls/TabItem.cs

@ -1,3 +1,4 @@
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Mixins;
using Avalonia.Controls.Primitives;
@ -6,6 +7,7 @@ namespace Avalonia.Controls
/// <summary>
/// An item in a <see cref="TabStrip"/> or <see cref="TabControl"/>.
/// </summary>
[PseudoClasses(":pressed", ":selected")]
public class TabItem : HeaderedContentControl, ISelectable
{
/// <summary>

2
src/Avalonia.Controls/TextBox.cs

@ -13,9 +13,11 @@ using Avalonia.Metadata;
using Avalonia.Data;
using Avalonia.Layout;
using Avalonia.Utilities;
using Avalonia.Controls.Metadata;
namespace Avalonia.Controls
{
[PseudoClasses(":empty")]
public class TextBox : TemplatedControl, UndoRedoHelper<TextBox.UndoRedoState>.IUndoRedoHost
{
public static KeyGesture CutGesture { get; } = AvaloniaLocator.Current

4
src/Avalonia.Controls/ToggleSwitch.cs

@ -1,4 +1,5 @@
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.LogicalTree;
@ -8,6 +9,7 @@ namespace Avalonia.Controls
/// <summary>
/// A Toggle Switch control.
/// </summary>
[PseudoClasses(":dragging")]
public class ToggleSwitch : ToggleButton
{
private Panel _knobsPanel;

2
src/Avalonia.Controls/ToolTip.cs

@ -1,5 +1,6 @@
using System;
using System.Reactive.Linq;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.VisualTree;
@ -14,6 +15,7 @@ namespace Avalonia.Controls
/// To add a tooltip to a control, use the <see cref="TipProperty"/> attached property,
/// assigning the content that you want displayed.
/// </remarks>
[PseudoClasses(":open")]
public class ToolTip : ContentControl
{
/// <summary>

2
src/Avalonia.Controls/TreeViewItem.cs

@ -1,5 +1,6 @@
using System.Linq;
using Avalonia.Controls.Generators;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Mixins;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
@ -11,6 +12,7 @@ namespace Avalonia.Controls
/// <summary>
/// An item in a <see cref="TreeView"/>.
/// </summary>
[PseudoClasses(":pressed", ":selected")]
public class TreeViewItem : HeaderedItemsControl, ISelectable
{
/// <summary>

283
src/Avalonia.Controls/Utils/SelectedItemsSync.cs

@ -1,283 +0,0 @@
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using Avalonia.Collections;
using Avalonia.Controls.Selection;
#nullable enable
namespace Avalonia.Controls.Utils
{
/// <summary>
/// Synchronizes an <see cref="ISelectionModel"/> with a list of SelectedItems.
/// </summary>
internal class SelectedItemsSync : IDisposable
{
private ISelectionModel _selectionModel;
private IList _selectedItems;
private bool _updatingItems;
private bool _updatingModel;
public SelectedItemsSync(ISelectionModel model)
{
_selectionModel = model ?? throw new ArgumentNullException(nameof(model));
_selectedItems = new AvaloniaList<object?>();
SyncSelectedItemsWithSelectionModel();
SubscribeToSelectedItems(_selectedItems);
SubscribeToSelectionModel(model);
}
public ISelectionModel SelectionModel
{
get => _selectionModel;
set
{
if (_selectionModel != value)
{
value = value ?? throw new ArgumentNullException(nameof(value));
UnsubscribeFromSelectionModel(_selectionModel);
_selectionModel = value;
SubscribeToSelectionModel(_selectionModel);
SyncSelectedItemsWithSelectionModel();
}
}
}
public IList SelectedItems
{
get => _selectedItems;
set
{
value ??= new AvaloniaList<object?>();
if (_selectedItems != value)
{
if (value.IsFixedSize)
{
throw new NotSupportedException(
"Cannot assign fixed size selection to SelectedItems.");
}
UnsubscribeFromSelectedItems(_selectedItems);
_selectedItems = value;
SubscribeToSelectedItems(_selectedItems);
SyncSelectionModelWithSelectedItems();
}
}
}
public void Dispose()
{
UnsubscribeFromSelectedItems(_selectedItems);
UnsubscribeFromSelectionModel(_selectionModel);
}
private void SyncSelectedItemsWithSelectionModel()
{
_updatingItems = true;
try
{
_selectedItems.Clear();
if (_selectionModel.Source is object)
{
foreach (var i in _selectionModel.SelectedItems)
{
_selectedItems.Add(i);
}
}
}
finally
{
_updatingItems = false;
}
}
private void SyncSelectionModelWithSelectedItems()
{
_updatingModel = true;
try
{
if (_selectionModel.Source is object)
{
using (_selectionModel.BatchUpdate())
{
SelectionModel.Clear();
Add(_selectedItems);
}
}
}
finally
{
_updatingModel = false;
}
}
private void SelectedItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (_updatingItems)
{
return;
}
if (_selectedItems == null)
{
throw new AvaloniaInternalException("CollectionChanged raised but we don't have items.");
}
void Remove()
{
foreach (var i in e.OldItems)
{
var index = IndexOf(SelectionModel.Source, i);
if (index != -1)
{
SelectionModel.Deselect(index);
}
}
}
try
{
using var operation = SelectionModel.BatchUpdate();
_updatingModel = true;
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
Add(e.NewItems);
break;
case NotifyCollectionChangedAction.Remove:
Remove();
break;
case NotifyCollectionChangedAction.Replace:
Remove();
Add(e.NewItems);
break;
case NotifyCollectionChangedAction.Reset:
SelectionModel.Clear();
Add(_selectedItems);
break;
}
}
finally
{
_updatingModel = false;
}
}
private void Add(IList newItems)
{
foreach (var i in newItems)
{
var index = IndexOf(SelectionModel.Source, i);
if (index != -1)
{
SelectionModel.Select(index);
}
}
}
private void SelectionModelPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(ISelectionModel.Source))
{
if (_selectedItems.Count > 0)
{
SyncSelectionModelWithSelectedItems();
}
else
{
SyncSelectedItemsWithSelectionModel();
}
}
}
private void SelectionModelSelectionChanged(object sender, SelectionModelSelectionChangedEventArgs e)
{
if (_updatingModel || _selectionModel.Source is null)
{
return;
}
try
{
var deselected = e.DeselectedItems.ToList();
var selected = e.SelectedItems.ToList();
_updatingItems = true;
foreach (var i in deselected)
{
_selectedItems.Remove(i);
}
foreach (var i in selected)
{
_selectedItems.Add(i);
}
}
finally
{
_updatingItems = false;
}
}
private void SelectionModelSourceReset(object sender, EventArgs e)
{
SyncSelectionModelWithSelectedItems();
}
private void SubscribeToSelectedItems(IList selectedItems)
{
if (selectedItems is INotifyCollectionChanged incc)
{
incc.CollectionChanged += SelectedItemsCollectionChanged;
}
}
private void SubscribeToSelectionModel(ISelectionModel model)
{
model.PropertyChanged += SelectionModelPropertyChanged;
model.SelectionChanged += SelectionModelSelectionChanged;
model.SourceReset += SelectionModelSourceReset;
}
private void UnsubscribeFromSelectedItems(IList selectedItems)
{
if (selectedItems is INotifyCollectionChanged incc)
{
incc.CollectionChanged -= SelectedItemsCollectionChanged;
}
}
private void UnsubscribeFromSelectionModel(ISelectionModel model)
{
model.PropertyChanged -= SelectionModelPropertyChanged;
model.SelectionChanged -= SelectionModelSelectionChanged;
model.SourceReset -= SelectionModelSourceReset;
}
private static int IndexOf(object? source, object? item)
{
if (source is IList l)
{
return l.IndexOf(item);
}
else if (source is ItemsSourceView v)
{
return v.IndexOf(item);
}
return -1;
}
}
}

2
src/Avalonia.Input/InputElement.cs

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Data;
using Avalonia.Input.GestureRecognizers;
using Avalonia.Interactivity;
@ -12,6 +13,7 @@ namespace Avalonia.Input
/// <summary>
/// Implements input-related functionality for a control.
/// </summary>
[PseudoClasses(":disabled", ":focus", ":focus-visible", ":pointerover")]
public class InputElement : Interactive, IInputElement
{
/// <summary>

9
src/Avalonia.Input/KeyboardNavigation.cs

@ -25,12 +25,11 @@ namespace Avalonia.Input
/// attached property set to <see cref="KeyboardNavigationMode.Once"/>, this property
/// defines to which child the focus should move.
/// </remarks>
public static readonly AttachedProperty<IInputElement> TabOnceActiveElementProperty =
AvaloniaProperty.RegisterAttached<InputElement, IInputElement>(
public static readonly AttachedProperty<IInputElement?> TabOnceActiveElementProperty =
AvaloniaProperty.RegisterAttached<InputElement, IInputElement?>(
"TabOnceActiveElement",
typeof(KeyboardNavigation));
/// <summary>
/// Defines the IsTabStop attached property.
/// </summary>
@ -68,7 +67,7 @@ namespace Avalonia.Input
/// </summary>
/// <param name="element">The container.</param>
/// <returns>The active element for the container.</returns>
public static IInputElement GetTabOnceActiveElement(InputElement element)
public static IInputElement? GetTabOnceActiveElement(InputElement element)
{
return element.GetValue(TabOnceActiveElementProperty);
}
@ -78,7 +77,7 @@ namespace Avalonia.Input
/// </summary>
/// <param name="element">The container.</param>
/// <param name="value">The active element for the container.</param>
public static void SetTabOnceActiveElement(InputElement element, IInputElement value)
public static void SetTabOnceActiveElement(InputElement element, IInputElement? value)
{
element.SetValue(TabOnceActiveElementProperty, value);
}

2
src/Avalonia.Layout/FlowLayoutAlgorithm.cs

@ -211,7 +211,7 @@ namespace Avalonia.Layout
anchorPosition = new Point(anchorBounds.X, anchorBounds.Y);
}
}
else
else if (anchorIndex >= 0)
{
// It is possible to end up in a situation during a collection change where GetAnchorForTargetElement returns an index
// which is not in the realized range. Eg. insert one item at index 0 for a grid layout.

2
src/Avalonia.ReactiveUI.Events/Avalonia.ReactiveUI.Events.csproj

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>Avalonia.ReactiveUI</PackageId>
<PackageId>Avalonia.ReactiveUI.Events</PackageId>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\packages\Avalonia\Avalonia.csproj" />

18
src/Avalonia.Styling/Controls/Metadata/PseudoClassesAttribute.cs

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
#nullable enable
namespace Avalonia.Controls.Metadata
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public sealed class PseudoClassesAttribute : Attribute
{
public PseudoClassesAttribute(params string[] pseudoClasses)
{
PseudoClasses = pseudoClasses;
}
public IReadOnlyList<string> PseudoClasses { get; }
}
}

4
src/Avalonia.Themes.Fluent/Button.xaml

@ -77,7 +77,7 @@
<Setter Property="TextBlock.Foreground" Value="{DynamicResource AccentButtonForegroundPressed}" />
</Style>
<Style Selector="Button">
<Style Selector="Button, RepeatButton, ToggleButton">
<Setter Property="RenderTransform" Value="none" />
<Setter Property="Transitions">
<Transitions>
@ -86,7 +86,7 @@
</Setter>
</Style>
<Style Selector="Button:pressed">
<Style Selector="Button:pressed, RepeatButton:pressed, ToggleButton:pressed">
<Setter Property="RenderTransform" Value="scale(0.98)" />
</Style>

9
src/Avalonia.Visuals/Rendering/SleepLoopRenderTimer.cs

@ -45,14 +45,13 @@ namespace Avalonia.Rendering
void LoopProc()
{
var now = _st.Elapsed;
var lastTick = now;
var lastTick = _st.Elapsed;
while (true)
{
var now = _st.Elapsed;
var timeTillNextTick = lastTick + _timeBetweenTicks - now;
if (timeTillNextTick.TotalMilliseconds > 1) Thread.Sleep(timeTillNextTick);
lastTick = now;
lock (_lock)
{
if (_count == 0)
@ -63,7 +62,7 @@ namespace Avalonia.Rendering
}
_tick?.Invoke(now);
now = _st.Elapsed;
}
}

23
tests/Avalonia.Base.UnitTests/Collections/AvaloniaListTests.cs

@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
@ -334,5 +335,27 @@ namespace Avalonia.Base.UnitTests.Collections
Assert.True(raised);
}
[Fact]
public void Can_CopyTo_Array_Of_Same_Type()
{
var target = new AvaloniaList<string> { "foo", "bar", "baz" };
var result = new string[3];
target.CopyTo(result, 0);
Assert.Equal(target, result);
}
[Fact]
public void Can_CopyTo_Array_Of_Base_Type()
{
var target = new AvaloniaList<string> { "foo", "bar", "baz" };
var result = new object[3];
((IList)target).CopyTo(result, 0);
Assert.Equal(target, result);
}
}
}

9
tests/Avalonia.Controls.DataGrid.UnitTests/Collections/DataGridSortDescriptionTests.cs

@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using System.Linq;
using Avalonia.Collections;
using Xunit;
@ -18,7 +19,7 @@ namespace Avalonia.Controls.DataGrid.UnitTests.Collections
new Item("c", "c"),
};
var expectedResult = items.OrderBy(i => i.Prop1).ToList();
var sortDescription = DataGridSortDescription.FromPath(nameof(Item.Prop1), @descending: false);
var sortDescription = DataGridSortDescription.FromPath(nameof(Item.Prop1), ListSortDirection.Ascending);
sortDescription.Initialize(typeof(Item));
var result = sortDescription.OrderBy(items).ToList();
@ -36,7 +37,7 @@ namespace Avalonia.Controls.DataGrid.UnitTests.Collections
new Item("c", "c"),
};
var expectedResult = items.OrderByDescending(i => i.Prop1).ToList();
var sortDescription = DataGridSortDescription.FromPath(nameof(Item.Prop1), @descending: true);
var sortDescription = DataGridSortDescription.FromPath(nameof(Item.Prop1), ListSortDirection.Descending);
sortDescription.Initialize(typeof(Item));
var result = sortDescription.OrderBy(items).ToList();
@ -61,7 +62,7 @@ namespace Avalonia.Controls.DataGrid.UnitTests.Collections
new Item("a", "b"),
new Item("a", "c"),
};
var sortDescription = DataGridSortDescription.FromPath(nameof(Item.Prop2), @descending: false);
var sortDescription = DataGridSortDescription.FromPath(nameof(Item.Prop2), ListSortDirection.Ascending);
sortDescription.Initialize(typeof(Item));
var result = sortDescription.ThenBy(items).ToList();
@ -86,7 +87,7 @@ namespace Avalonia.Controls.DataGrid.UnitTests.Collections
new Item("a", "b"),
new Item("a", "a"),
};
var sortDescription = DataGridSortDescription.FromPath(nameof(Item.Prop2), @descending: true);
var sortDescription = DataGridSortDescription.FromPath(nameof(Item.Prop2), ListSortDirection.Descending);
sortDescription.Initialize(typeof(Item));
var result = sortDescription.ThenBy(items).ToList();

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

@ -363,6 +363,40 @@ namespace Avalonia.Controls.UnitTests
});
}
[Fact]
public void Custom_TextSelector()
{
RunTest((control, textbox) =>
{
object selectedItem = control.Items.Cast<object>().First();
string input = "42";
control.TextSelector = (text, item) => text + item;
Assert.Equal(control.TextSelector("4", "2"), "42");
control.Text = input;
control.SelectedItem = selectedItem;
Assert.Equal(control.Text, control.TextSelector(input, selectedItem.ToString()));
});
}
[Fact]
public void Custom_ItemSelector()
{
RunTest((control, textbox) =>
{
object selectedItem = control.Items.Cast<object>().First();
string input = "42";
control.ItemSelector = (text, item) => text + item;
Assert.Equal(control.ItemSelector("4", 2), "42");
control.Text = input;
control.SelectedItem = selectedItem;
Assert.Equal(control.Text, control.ItemSelector(input, selectedItem));
});
}
/// <summary>
/// Retrieves a defined predicate filter through a new AutoCompleteBox
/// control instance.

529
tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs

@ -554,6 +554,44 @@ namespace Avalonia.Controls.UnitTests.Primitives
Assert.False(items.Single().IsSelected);
}
[Fact]
public void Removing_Selected_Item_Should_Update_Selection_With_AlwaysSelected()
{
var item0 = new Item();
var item1 = new Item();
var items = new AvaloniaList<Item>
{
item0,
item1,
};
var target = new TestSelector
{
Items = items,
Template = Template(),
SelectionMode = SelectionMode.AlwaysSelected,
};
Prepare(target);
target.SelectedIndex = 1;
Assert.Equal(items[1], target.SelectedItem);
Assert.Equal(1, target.SelectedIndex);
SelectionChangedEventArgs receivedArgs = null;
target.SelectionChanged += (_, args) => receivedArgs = args;
items.RemoveAt(1);
Assert.Same(item0, target.SelectedItem);
Assert.Equal(0, target.SelectedIndex);
Assert.NotNull(receivedArgs);
Assert.Equal(new[] { item0 }, receivedArgs.AddedItems);
Assert.Equal(new[] { item1 }, receivedArgs.RemovedItems);
Assert.True(items.Single().IsSelected);
}
[Fact]
public void Removing_Selected_Item_Should_Clear_Selection_With_BeginInit()
{
@ -771,6 +809,186 @@ namespace Avalonia.Controls.UnitTests.Primitives
Assert.True(called);
}
[Fact]
public void Setting_SelectedIndex_Should_Raise_PropertyChanged_Events()
{
var items = new ObservableCollection<string> { "foo", "bar", "baz" };
var target = new TestSelector
{
Items = items,
Template = Template(),
};
var selectedIndexRaised = 0;
var selectedItemRaised = 0;
target.PropertyChanged += (s, e) =>
{
if (e.Property == SelectingItemsControl.SelectedIndexProperty)
{
Assert.Equal(-1, e.OldValue);
Assert.Equal(1, e.NewValue);
++selectedIndexRaised;
}
else if (e.Property == SelectingItemsControl.SelectedItemProperty)
{
Assert.Null(e.OldValue);
Assert.Equal("bar", e.NewValue);
++selectedItemRaised;
}
};
target.SelectedIndex = 1;
Assert.Equal(1, selectedIndexRaised);
Assert.Equal(1, selectedItemRaised);
}
[Fact]
public void Removing_Selected_Item_Should_Raise_PropertyChanged_Events()
{
var items = new ObservableCollection<string> { "foo", "bar", "baz" };
var target = new TestSelector
{
Items = items,
Template = Template(),
};
var selectedIndexRaised = 0;
var selectedItemRaised = 0;
target.SelectedIndex = 1;
target.PropertyChanged += (s, e) =>
{
if (e.Property == SelectingItemsControl.SelectedIndexProperty)
{
Assert.Equal(1, e.OldValue);
Assert.Equal(-1, e.NewValue);
++selectedIndexRaised;
}
else if (e.Property == SelectingItemsControl.SelectedItemProperty)
{
Assert.Equal("bar", e.OldValue);
Assert.Null(e.NewValue);
}
};
items.RemoveAt(1);
Assert.Equal(1, selectedIndexRaised);
Assert.Equal(0, selectedItemRaised);
}
[Fact]
public void Removing_Selected_Item0_Should_Raise_PropertyChanged_Events_With_AlwaysSelected()
{
var items = new ObservableCollection<string> { "foo", "bar", "baz" };
var target = new TestSelector
{
Items = items,
Template = Template(),
SelectionMode = SelectionMode.AlwaysSelected,
};
var selectedIndexRaised = 0;
var selectedItemRaised = 0;
target.SelectedIndex = 0;
target.PropertyChanged += (s, e) =>
{
if (e.Property == SelectingItemsControl.SelectedIndexProperty)
{
++selectedIndexRaised;
}
else if (e.Property == SelectingItemsControl.SelectedItemProperty)
{
Assert.Equal("foo", e.OldValue);
Assert.Equal("bar", e.NewValue);
++selectedItemRaised;
}
};
items.RemoveAt(0);
Assert.Equal(0, selectedIndexRaised);
Assert.Equal(1, selectedItemRaised);
}
[Fact]
public void Removing_Selected_Item1_Should_Raise_PropertyChanged_Events_With_AlwaysSelected()
{
var items = new ObservableCollection<string> { "foo", "bar", "baz" };
var target = new TestSelector
{
Items = items,
Template = Template(),
SelectionMode = SelectionMode.AlwaysSelected,
};
var selectedIndexRaised = 0;
var selectedItemRaised = 0;
target.SelectedIndex = 1;
target.PropertyChanged += (s, e) =>
{
if (e.Property == SelectingItemsControl.SelectedIndexProperty)
{
Assert.Equal(1, e.OldValue);
Assert.Equal(0, e.NewValue);
++selectedIndexRaised;
}
else if (e.Property == SelectingItemsControl.SelectedItemProperty)
{
Assert.Equal("bar", e.OldValue);
Assert.Equal("foo", e.NewValue);
}
};
items.RemoveAt(1);
Assert.Equal(1, selectedIndexRaised);
Assert.Equal(0, selectedItemRaised);
}
[Fact]
public void Removing_Item_Before_Selection_Should_Raise_PropertyChanged_Events()
{
var items = new ObservableCollection<string> { "foo", "bar", "baz" };
var target = new SelectingItemsControl
{
Items = items,
Template = Template(),
};
var selectedIndexRaised = 0;
var selectedItemRaised = 0;
target.SelectedIndex = 1;
target.PropertyChanged += (s, e) =>
{
if (e.Property == SelectingItemsControl.SelectedIndexProperty)
{
Assert.Equal(1, e.OldValue);
Assert.Equal(0, e.NewValue);
++selectedIndexRaised;
}
else if (e.Property == SelectingItemsControl.SelectedItemProperty)
{
++selectedItemRaised;
}
};
items.RemoveAt(0);
Assert.Equal(1, selectedIndexRaised);
Assert.Equal(0, selectedItemRaised);
}
[Fact]
public void Order_Of_Setting_Items_And_SelectedIndex_During_Initialization_Should_Not_Matter()
{
@ -1184,12 +1402,36 @@ namespace Avalonia.Controls.UnitTests.Primitives
Items = items,
};
var raised = false;
Prepare(target);
target.AddHandler(Control.RequestBringIntoViewEvent, (s, e) => raised = true);
target.SelectedIndex = 2;
Assert.True(raised);
}
[Fact]
public void AutoScrollToSelectedItem_Causes_Scroll_To_Initial_SelectedItem()
{
var items = new ObservableCollection<string>
{
"Foo",
"Bar",
"Baz"
};
var target = new ListBox
{
Template = Template(),
Items = items,
};
var raised = false;
target.AddHandler(Control.RequestBringIntoViewEvent, (s, e) => raised = true);
target.AddHandler(Control.RequestBringIntoViewEvent, (s, e) => raised = true);
target.SelectedIndex = 2;
Prepare(target);
Assert.True(raised);
}
@ -1233,6 +1475,99 @@ namespace Avalonia.Controls.UnitTests.Primitives
}
}
[Fact]
public void AutoScrollToSelectedItem_Scrolls_When_Reattached_To_Visual_Tree_If_Selection_Changed_While_Detached_From_Visual_Tree()
{
var items = new ObservableCollection<string>
{
"Foo",
"Bar",
"Baz"
};
var target = new ListBox
{
Template = Template(),
Items = items,
SelectedIndex = 2,
};
var raised = false;
Prepare(target);
var root = (TestRoot)target.Parent;
target.AddHandler(Control.RequestBringIntoViewEvent, (s, e) => raised = true);
root.Child = null;
target.SelectedIndex = 1;
root.Child = target;
Assert.True(raised);
}
[Fact]
public void AutoScrollToSelectedItem_Doesnt_Scroll_If_Reattached_To_Visual_Tree_With_No_Selection_Change()
{
var items = new ObservableCollection<string>
{
"Foo",
"Bar",
"Baz"
};
var target = new ListBox
{
Template = Template(),
Items = items,
SelectedIndex = 2,
};
var raised = false;
Prepare(target);
var root = (TestRoot)target.Parent;
target.AddHandler(Control.RequestBringIntoViewEvent, (s, e) => raised = true);
root.Child = null;
root.Child = target;
Assert.False(raised);
}
[Fact]
public void AutoScrollToSelectedItem_Causes_Scroll_When_Turned_On()
{
var items = new ObservableCollection<string>
{
"Foo",
"Bar",
"Baz"
};
var target = new ListBox
{
Template = Template(),
Items = items,
AutoScrollToSelectedItem = false,
};
Prepare(target);
var raised = false;
target.AddHandler(Control.RequestBringIntoViewEvent, (s, e) => raised = true);
target.SelectedIndex = 2;
Assert.False(raised);
target.AutoScrollToSelectedItem = true;
Assert.True(raised);
}
[Fact]
public void Can_Set_Both_SelectedItem_And_SelectedItems_During_Initialization()
{
@ -1376,6 +1711,190 @@ namespace Avalonia.Controls.UnitTests.Primitives
Assert.Equal(new[] { "foo" }, target.SelectedItems);
}
[Fact]
public void Preserves_Initial_SelectedItems_When_Bound()
{
// Issue #4272 (there are two issues there, this addresses the second one).
var vm = new SelectionViewModel
{
Items = { "foo", "bar", "baz" },
SelectedItems = { "bar" },
};
var target = new ListBox
{
[!ListBox.ItemsProperty] = new Binding("Items"),
[!ListBox.SelectedItemsProperty] = new Binding("SelectedItems"),
DataContext = vm,
};
Prepare(target);
Assert.Equal(1, target.SelectedIndex);
Assert.Equal(new[] { 1 }, target.Selection.SelectedIndexes);
Assert.Equal("bar", target.SelectedItem);
Assert.Equal(new[] { "bar" }, target.SelectedItems);
}
[Fact]
public void Preserves_SelectedItem_When_Items_Changed()
{
// Issue #4048
var target = new SelectingItemsControl
{
Items = new[] { "foo", "bar", "baz"},
SelectedItem = "bar",
};
Prepare(target);
Assert.Equal(1, target.SelectedIndex);
Assert.Equal("bar", target.SelectedItem);
target.Items = new[] { "qux", "foo", "bar" };
Assert.Equal(2, target.SelectedIndex);
Assert.Equal("bar", target.SelectedItem);
}
[Fact]
public void Setting_SelectedItems_Raises_PropertyChanged()
{
var target = new TestSelector
{
Items = new[] { "foo", "bar", "baz" },
};
var raised = 0;
var newValue = new AvaloniaList<object>();
Prepare(target);
target.PropertyChanged += (s, e) =>
{
if (e.Property == ListBox.SelectedItemsProperty)
{
Assert.Null(e.OldValue);
Assert.Same(newValue, e.NewValue);
++raised;
}
};
target.SelectedItems = newValue;
Assert.Equal(1, raised);
}
[Fact]
public void Setting_Selection_Raises_SelectedItems_PropertyChanged()
{
var target = new TestSelector
{
Items = new[] { "foo", "bar", "baz" },
};
var raised = 0;
var oldValue = target.SelectedItems;
Prepare(target);
target.PropertyChanged += (s, e) =>
{
if (e.Property == ListBox.SelectedItemsProperty)
{
Assert.Same(oldValue, e.OldValue);
Assert.Null(e.NewValue);
++raised;
}
};
target.Selection = new SelectionModel<int>();
Assert.Equal(1, raised);
}
[Fact]
public void Handles_Removing_Last_Item_In_Two_Controls_With_Bound_SelectedIndex()
{
var items = new ObservableCollection<string> { "foo" };
// Simulates problem with TabStrip and Carousel with bound SelectedIndex.
var tabStrip = new TestSelector
{
Items = items,
SelectionMode = SelectionMode.AlwaysSelected,
};
var carousel = new TestSelector
{
Items = items,
[!Carousel.SelectedIndexProperty] = tabStrip[!TabStrip.SelectedIndexProperty],
};
var tabStripRaised = 0;
var carouselRaised = 0;
tabStrip.SelectionChanged += (s, e) =>
{
Assert.Equal(new[] { "foo" }, e.RemovedItems);
Assert.Empty(e.AddedItems);
++tabStripRaised;
};
carousel.SelectionChanged += (s, e) =>
{
Assert.Equal(new[] { "foo" }, e.RemovedItems);
Assert.Empty(e.AddedItems);
++carouselRaised;
};
items.RemoveAt(0);
Assert.Equal(1, tabStripRaised);
Assert.Equal(1, carouselRaised);
}
[Fact]
public void Handles_Removing_Last_Item_In_Controls_With_Bound_SelectedItem()
{
var items = new ObservableCollection<string> { "foo" };
// Simulates problem with TabStrip and Carousel with bound SelectedItem.
var tabStrip = new TestSelector
{
Items = items,
SelectionMode = SelectionMode.AlwaysSelected,
};
var carousel = new TestSelector
{
Items = items,
[!Carousel.SelectedItemProperty] = tabStrip[!TabStrip.SelectedItemProperty],
};
var tabStripRaised = 0;
var carouselRaised = 0;
tabStrip.SelectionChanged += (s, e) =>
{
Assert.Equal(new[] { "foo" }, e.RemovedItems);
Assert.Empty(e.AddedItems);
++tabStripRaised;
};
carousel.SelectionChanged += (s, e) =>
{
Assert.Equal(new[] { "foo" }, e.RemovedItems);
Assert.Empty(e.AddedItems);
++carouselRaised;
};
items.RemoveAt(0);
Assert.Equal(1, tabStripRaised);
Assert.Equal(1, carouselRaised);
}
private static void Prepare(SelectingItemsControl target)
{
var root = new TestRoot
@ -1445,6 +1964,7 @@ namespace Avalonia.Controls.UnitTests.Primitives
public SelectionViewModel()
{
Items = new ObservableCollection<string>();
SelectedItems = new ObservableCollection<string>();
}
public int SelectedIndex
@ -1458,6 +1978,7 @@ namespace Avalonia.Controls.UnitTests.Primitives
}
public ObservableCollection<string> Items { get; }
public ObservableCollection<string> SelectedItems { get; }
}
private class RootWithItems : TestRoot
@ -1484,6 +2005,12 @@ namespace Avalonia.Controls.UnitTests.Primitives
set => base.Selection = value;
}
public new IList SelectedItems
{
get => base.SelectedItems;
set => base.SelectedItems = value;
}
public new SelectionMode SelectionMode
{
get => base.SelectionMode;

254
tests/Avalonia.Controls.UnitTests/Selection/InternalSelectionModelTests.cs

@ -0,0 +1,254 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using Avalonia.Collections;
using Avalonia.Controls.Selection;
using Xunit;
namespace Avalonia.Controls.UnitTests.Selection
{
public class InternalSelectionModelTests
{
[Fact]
public void Selecting_Item_Adds_To_WritableSelectedItems()
{
var target = CreateTarget();
target.Select(0);
Assert.Equal(new[] { "foo" }, target.WritableSelectedItems);
}
[Fact]
public void Selecting_Duplicate_On_Model_Adds_To_WritableSelectedItems()
{
var target = CreateTarget(source: new[] { "foo", "bar", "baz", "foo", "bar", "baz" });
target.SelectRange(1, 4);
Assert.Equal(new[] { "bar", "baz", "foo", "bar" }, target.WritableSelectedItems);
}
[Fact]
public void Deselecting_On_Model_Removes_SelectedItem()
{
var target = CreateTarget();
target.SelectRange(1, 2);
target.Deselect(1);
Assert.Equal(new[] { "baz" }, target.WritableSelectedItems);
}
[Fact]
public void Deselecting_Duplicate_On_Model_Removes_SelectedItem()
{
var target = CreateTarget(source: new[] { "foo", "bar", "baz", "foo", "bar", "baz" });
target.SelectRange(1, 2);
target.Select(4);
target.Deselect(4);
Assert.Equal(new[] { "baz", "bar" }, target.WritableSelectedItems);
}
[Fact]
public void Adding_To_WritableSelectedItems_Selects_On_Model()
{
var target = CreateTarget();
target.SelectRange(1, 2);
target.WritableSelectedItems.Add("foo");
Assert.Equal(new[] { 0, 1, 2 }, target.SelectedIndexes);
Assert.Equal(new[] { "bar", "baz", "foo" }, target.WritableSelectedItems);
}
[Fact]
public void Removing_From_WritableSelectedItems_Deselects_On_Model()
{
var target = CreateTarget();
target.SelectRange(1, 2);
target.WritableSelectedItems.Remove("baz");
Assert.Equal(new[] { 1 }, target.SelectedIndexes);
Assert.Equal(new[] { "bar" }, target.WritableSelectedItems);
}
[Fact]
public void Replacing_SelectedItem_Updates_Model()
{
var target = CreateTarget();
target.SelectRange(1, 2);
target.WritableSelectedItems[0] = "foo";
Assert.Equal(new[] { 0, 2 }, target.SelectedIndexes);
Assert.Equal(new[] { "foo", "baz" }, target.WritableSelectedItems);
}
[Fact]
public void Clearing_WritableSelectedItems_Updates_Model()
{
var target = CreateTarget();
target.WritableSelectedItems.Clear();
Assert.Empty(target.SelectedIndexes);
}
[Fact]
public void Setting_WritableSelectedItems_Updates_Model()
{
var target = CreateTarget();
var oldItems = target.WritableSelectedItems;
var newItems = new AvaloniaList<string> { "foo", "baz" };
target.WritableSelectedItems = newItems;
Assert.Equal(new[] { 0, 2 }, target.SelectedIndexes);
Assert.Same(newItems, target.WritableSelectedItems);
Assert.NotSame(oldItems, target.WritableSelectedItems);
Assert.Equal(new[] { "foo", "baz" }, newItems);
}
[Fact]
public void Setting_Items_To_Null_Clears_Selection()
{
var target = CreateTarget();
target.SelectRange(1, 2);
target.WritableSelectedItems = null;
Assert.Empty(target.SelectedIndexes);
}
[Fact]
public void Setting_Items_To_Null_Creates_Empty_Items()
{
var target = CreateTarget();
var oldItems = target.WritableSelectedItems;
target.WritableSelectedItems = null;
Assert.NotNull(target.WritableSelectedItems);
Assert.NotSame(oldItems, target.WritableSelectedItems);
Assert.IsType<AvaloniaList<object>>(target.WritableSelectedItems);
}
[Fact]
public void Adds_Null_WritableSelectedItems_When_Source_Is_Null()
{
var target = CreateTarget(nullSource: true);
target.SelectRange(1, 2);
Assert.Equal(new object[] { null, null }, target.WritableSelectedItems);
}
[Fact]
public void Updates_WritableSelectedItems_When_Source_Changes_From_Null()
{
var target = CreateTarget(nullSource: true);
target.SelectRange(1, 2);
Assert.Equal(new object[] { null, null }, target.WritableSelectedItems);
target.Source = new[] { "foo", "bar", "baz" };
Assert.Equal(new[] { "bar", "baz" }, target.WritableSelectedItems);
}
[Fact]
public void Updates_WritableSelectedItems_When_Source_Changes_To_Null()
{
var target = CreateTarget();
target.SelectRange(1, 2);
Assert.Equal(new[] { "bar", "baz" }, target.WritableSelectedItems);
target.Source = null;
Assert.Equal(new object[] { null, null }, target.WritableSelectedItems);
}
[Fact]
public void WritableSelectedItems_Can_Be_Set_Before_Source()
{
var target = CreateTarget(nullSource: true);
var items = new AvaloniaList<string> { "foo", "bar", "baz" };
var WritableSelectedItems = new AvaloniaList<string> { "bar" };
target.WritableSelectedItems = WritableSelectedItems;
target.Source = items;
Assert.Equal(1, target.SelectedIndex);
}
[Fact]
public void Does_Not_Accept_Fixed_Size_Items()
{
var target = CreateTarget();
Assert.Throws<NotSupportedException>(() =>
target.WritableSelectedItems = new[] { "foo", "bar", "baz" });
}
[Fact]
public void Restores_Selection_On_Items_Reset()
{
var items = new ResettingCollection(new[] { "foo", "bar", "baz" });
var target = CreateTarget(source: items);
target.SelectedIndex = 1;
items.Reset(new[] { "baz", "foo", "bar" });
Assert.Equal(2, target.SelectedIndex);
}
[Fact]
public void Preserves_Selection_On_Source_Changed()
{
var target = CreateTarget();
target.SelectedIndex = 1;
target.Source = new[] { "baz", "foo", "bar" };
Assert.Equal(2, target.SelectedIndex);
}
private static InternalSelectionModel CreateTarget(
bool singleSelect = false,
IList source = null,
bool nullSource = false)
{
source ??= !nullSource ? new[] { "foo", "bar", "baz" } : null;
var result = new InternalSelectionModel
{
SingleSelect = singleSelect,
};
((ISelectionModel)result).Source = source;
return result;
}
private class ResettingCollection : List<string>, INotifyCollectionChanged
{
public ResettingCollection(IEnumerable<string> items)
{
AddRange(items);
}
public void Reset(IEnumerable<string> items)
{
Clear();
AddRange(items);
CollectionChanged?.Invoke(
this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
public event NotifyCollectionChangedEventHandler CollectionChanged;
}
}
}

56
tests/Avalonia.Controls.UnitTests/Selection/SelectionModelTests_Multiple.cs

@ -121,6 +121,34 @@ namespace Avalonia.Controls.UnitTests.Selection
Assert.Equal(0, raised);
}
[Fact]
public void Initializing_Source_Raises_SelectedItems_PropertyChanged()
{
var target = CreateTarget(false);
var selectedItemRaised = 0;
var selectedItemsRaised = 0;
target.Select(1);
target.Select(2);
target.PropertyChanged += (s, e) =>
{
if (e.PropertyName == nameof(target.SelectedItem))
{
++selectedItemRaised;
}
else if (e.PropertyName == nameof(target.SelectedItems))
{
++selectedItemsRaised;
}
};
target.Source = new[] { "foo", "bar", "baz" };
Assert.Equal(1, selectedItemRaised);
Assert.Equal(1, selectedItemsRaised);
}
[Fact]
public void Initializing_Source_Respects_Range_SourceItem_Order()
{
@ -152,6 +180,34 @@ namespace Avalonia.Controls.UnitTests.Selection
Assert.Equal("bar", target.SelectedItem);
Assert.Equal(new[] { "bar" }, target.SelectedItems);
}
[Fact]
public void Changing_Source_To_Null_Raises_SelectedItems_PropertyChanged()
{
var target = CreateTarget();
var selectedItemRaised = 0;
var selectedItemsRaised = 0;
target.Select(1);
target.Select(2);
target.PropertyChanged += (s, e) =>
{
if (e.PropertyName == nameof(target.SelectedItem))
{
++selectedItemRaised;
}
else if (e.PropertyName == nameof(target.SelectedItems))
{
++selectedItemsRaised;
}
};
target.Source = null;
Assert.Equal(1, selectedItemRaised);
Assert.Equal(1, selectedItemsRaised);
}
}
public class SelectedIndex

56
tests/Avalonia.Controls.UnitTests/Selection/SelectionModelTests_Single.cs

@ -174,6 +174,33 @@ namespace Avalonia.Controls.UnitTests.Selection
Assert.Equal(new[] { "bar" }, target.SelectedItems);
}
[Fact]
public void Initializing_Source_Raises_SelectedItems_PropertyChanged()
{
var target = CreateTarget(false);
var selectedItemRaised = 0;
var selectedItemsRaised = 0;
target.Select(1);
target.PropertyChanged += (s, e) =>
{
if (e.PropertyName == nameof(target.SelectedItem))
{
++selectedItemRaised;
}
else if (e.PropertyName == nameof(target.SelectedItems))
{
++selectedItemsRaised;
}
};
target.Source = new[] { "foo", "bar", "baz" };
Assert.Equal(1, selectedItemRaised);
Assert.Equal(1, selectedItemsRaised);
}
[Fact]
public void Changing_Source_To_Null_Doesnt_Clear_Selection()
{
@ -194,7 +221,7 @@ namespace Avalonia.Controls.UnitTests.Selection
}
[Fact]
public void Changing_Source_To_NonNUll_First_Clears_Old_Selection()
public void Changing_Source_To_NonNull_First_Clears_Old_Selection()
{
var target = CreateTarget();
var raised = 0;
@ -219,6 +246,33 @@ namespace Avalonia.Controls.UnitTests.Selection
Assert.Equal(1, raised);
}
[Fact]
public void Changing_Source_To_Null_Raises_SelectedItems_PropertyChanged()
{
var target = CreateTarget();
var selectedItemRaised = 0;
var selectedItemsRaised = 0;
target.Select(1);
target.PropertyChanged += (s, e) =>
{
if (e.PropertyName == nameof(target.SelectedItem))
{
++selectedItemRaised;
}
else if (e.PropertyName == nameof(target.SelectedItems))
{
++selectedItemsRaised;
}
};
target.Source = null;
Assert.Equal(1, selectedItemRaised);
Assert.Equal(1, selectedItemsRaised);
}
[Fact]
public void Raises_PropertyChanged()
{

84
tests/Avalonia.Controls.UnitTests/TabControlTests.cs

@ -122,11 +122,86 @@ namespace Avalonia.Controls.UnitTests
Items = collection,
};
target.ApplyTemplate();
Prepare(target);
target.SelectedItem = collection[1];
Assert.Same(collection[1], target.SelectedItem);
Assert.Equal(collection[1].Content, target.SelectedContent);
collection.RemoveAt(1);
Assert.Same(collection[0], target.SelectedItem);
Assert.Equal(collection[0].Content, target.SelectedContent);
}
[Fact]
public void Removal_Should_Set_New_Item0_When_Item0_Selected()
{
var collection = new ObservableCollection<TabItem>()
{
new TabItem
{
Name = "first",
Content = "foo",
},
new TabItem
{
Name = "second",
Content = "bar",
},
new TabItem
{
Name = "3rd",
Content = "barf",
},
};
var target = new TabControl
{
Template = TabControlTemplate(),
Items = collection,
};
Prepare(target);
target.SelectedItem = collection[0];
Assert.Same(collection[0], target.SelectedItem);
Assert.Equal(collection[0].Content, target.SelectedContent);
collection.RemoveAt(0);
Assert.Same(collection[0], target.SelectedItem);
Assert.Equal(collection[0].Content, target.SelectedContent);
}
[Fact]
public void Removal_Should_Set_New_Item0_When_Item0_Selected_With_DataTemplate()
{
using var app = UnitTestApplication.Start(TestServices.StyledWindow);
var collection = new ObservableCollection<Item>()
{
new Item("first"),
new Item("second"),
new Item("3rd"),
};
var target = new TabControl
{
Template = TabControlTemplate(),
Items = collection,
};
Prepare(target);
target.SelectedItem = collection[0];
Assert.Same(collection[0], target.SelectedItem);
Assert.Equal(collection[0], target.SelectedContent);
collection.RemoveAt(0);
Assert.Same(collection[0], target.SelectedItem);
Assert.Equal(collection[0], target.SelectedContent);
}
[Fact]
@ -383,6 +458,13 @@ namespace Avalonia.Controls.UnitTests
}.RegisterInNameScope(scope));
}
private void Prepare(TabControl target)
{
ApplyTemplate(target);
target.Measure(Size.Infinity);
target.Arrange(new Rect(target.DesiredSize));
}
private void ApplyTemplate(TabControl target)
{
target.ApplyTemplate();

278
tests/Avalonia.Controls.UnitTests/Utils/SelectedItemsSyncTests.cs

@ -1,278 +0,0 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using Avalonia.Collections;
using Avalonia.Controls.Selection;
using Avalonia.Controls.Utils;
using Xunit;
namespace Avalonia.Controls.UnitTests.Utils
{
public class SelectedItemsSyncTests
{
[Fact]
public void Initial_Items_Are_From_Model()
{
var target = CreateTarget();
var items = target.SelectedItems;
Assert.Equal(new[] { "bar", "baz" }, items);
}
[Fact]
public void Selecting_On_Model_Adds_Item()
{
var target = CreateTarget();
var items = target.SelectedItems;
target.SelectionModel.Select(0);
Assert.Equal(new[] { "bar", "baz", "foo" }, items);
}
[Fact]
public void Selecting_Duplicate_On_Model_Adds_Item()
{
var target = CreateTarget(new[] { "foo", "bar", "baz", "foo", "bar", "baz" });
var items = target.SelectedItems;
target.SelectionModel.Select(4);
Assert.Equal(new[] { "bar", "baz", "bar" }, items);
}
[Fact]
public void Deselecting_On_Model_Removes_Item()
{
var target = CreateTarget();
var items = target.SelectedItems;
target.SelectionModel.Deselect(1);
Assert.Equal(new[] { "baz" }, items);
}
[Fact]
public void Deselecting_Duplicate_On_Model_Removes_Item()
{
var target = CreateTarget(new[] { "foo", "bar", "baz", "foo", "bar", "baz" });
var items = target.SelectedItems;
target.SelectionModel.Select(4);
target.SelectionModel.Deselect(4);
Assert.Equal(new[] { "baz", "bar" }, items);
}
[Fact]
public void Reassigning_Model_Resets_Items()
{
var target = CreateTarget();
var items = target.SelectedItems;
var newModel = new SelectionModel<string>
{
Source = (string[])target.SelectionModel.Source,
SingleSelect = false
};
newModel.Select(0);
newModel.Select(1);
target.SelectionModel = newModel;
Assert.Equal(new[] { "foo", "bar" }, items);
}
[Fact]
public void Reassigning_Model_Tracks_New_Model()
{
var target = CreateTarget();
var items = target.SelectedItems;
var newModel = new SelectionModel<string>
{
Source = (string[])target.SelectionModel.Source,
SingleSelect = false
};
target.SelectionModel = newModel;
newModel.Select(0);
newModel.Select(1);
Assert.Equal(new[] { "foo", "bar" }, items);
}
[Fact]
public void Adding_To_Items_Selects_On_Model()
{
var target = CreateTarget();
var items = target.SelectedItems;
items.Add("foo");
Assert.Equal(new[] { 0, 1, 2 }, target.SelectionModel.SelectedIndexes);
Assert.Equal(new[] { "bar", "baz", "foo" }, items);
}
[Fact]
public void Removing_From_Items_Deselects_On_Model()
{
var target = CreateTarget();
var items = target.SelectedItems;
items.Remove("baz");
Assert.Equal(new[] { 1 }, target.SelectionModel.SelectedIndexes);
Assert.Equal(new[] { "bar" }, items);
}
[Fact]
public void Replacing_Item_Updates_Model()
{
var target = CreateTarget();
var items = target.SelectedItems;
items[0] = "foo";
Assert.Equal(new[] { 0, 2 }, target.SelectionModel.SelectedIndexes);
Assert.Equal(new[] { "foo", "baz" }, items);
}
[Fact]
public void Clearing_Items_Updates_Model()
{
var target = CreateTarget();
var items = target.SelectedItems;
items.Clear();
Assert.Empty(target.SelectionModel.SelectedIndexes);
}
[Fact]
public void Setting_Items_Updates_Model()
{
var target = CreateTarget();
var oldItems = target.SelectedItems;
var newItems = new AvaloniaList<string> { "foo", "baz" };
target.SelectedItems = newItems;
Assert.Equal(new[] { 0, 2 }, target.SelectionModel.SelectedIndexes);
Assert.Same(newItems, target.SelectedItems);
Assert.NotSame(oldItems, target.SelectedItems);
Assert.Equal(new[] { "foo", "baz" }, newItems);
}
[Fact]
public void Setting_Items_Subscribes_To_Model()
{
var target = CreateTarget();
var items = new AvaloniaList<string> { "foo", "baz" };
target.SelectedItems = items;
target.SelectionModel.Select(1);
Assert.Equal(new[] { "foo", "baz", "bar" }, items);
}
[Fact]
public void Setting_Items_To_Null_Creates_Empty_Items()
{
var target = CreateTarget();
var oldItems = target.SelectedItems;
target.SelectedItems = null;
var newItems = Assert.IsType<AvaloniaList<object>>(target.SelectedItems);
Assert.NotSame(oldItems, newItems);
}
[Fact]
public void Handles_Null_Model_Source()
{
var model = new SelectionModel<string> { SingleSelect = false };
model.Select(1);
var target = new SelectedItemsSync(model);
var items = target.SelectedItems;
Assert.Empty(items);
model.Select(2);
model.Source = new[] { "foo", "bar", "baz" };
Assert.Equal(new[] { "bar", "baz" }, items);
}
[Fact]
public void Does_Not_Accept_Fixed_Size_Items()
{
var target = CreateTarget();
Assert.Throws<NotSupportedException>(() =>
target.SelectedItems = new[] { "foo", "bar", "baz" });
}
[Fact]
public void Selected_Items_Can_Be_Set_Before_SelectionModel_Source()
{
var model = new SelectionModel<string>();
var target = new SelectedItemsSync(model);
var items = new AvaloniaList<string> { "foo", "bar", "baz" };
var selectedItems = new AvaloniaList<string> { "bar" };
target.SelectedItems = selectedItems;
model.Source = items;
Assert.Equal(1, model.SelectedIndex);
}
[Fact]
public void Restores_Selection_On_Items_Reset()
{
var items = new ResettingCollection(new[] { "foo", "bar", "baz" });
var model = new SelectionModel<string> { Source = items };
var target = new SelectedItemsSync(model);
model.SelectedIndex = 1;
items.Reset(new[] { "baz", "foo", "bar" });
Assert.Equal(2, model.SelectedIndex);
}
private static SelectedItemsSync CreateTarget(
IEnumerable<string> items = null)
{
items ??= new[] { "foo", "bar", "baz" };
var model = new SelectionModel<string> { Source = items, SingleSelect = false };
model.SelectRange(1, 2);
var target = new SelectedItemsSync(model);
return target;
}
private class ResettingCollection : List<string>, INotifyCollectionChanged
{
public ResettingCollection(IEnumerable<string> items)
{
AddRange(items);
}
public void Reset(IEnumerable<string> items)
{
Clear();
AddRange(items);
CollectionChanged?.Invoke(
this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
public event NotifyCollectionChangedEventHandler CollectionChanged;
}
}
}

15
tests/Avalonia.ReactiveUI.Events.UnitTests/Avalonia.ReactiveUI.Events.UnitTests.csproj

@ -1,15 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<Import Project="..\..\build\UnitTests.NetCore.targets" />
<Import Project="..\..\build\Moq.props" />
<Import Project="..\..\build\XUnit.props" />
<Import Project="..\..\build\Rx.props" />
<Import Project="..\..\build\Microsoft.Reactive.Testing.props" />
<ItemGroup>
<ProjectReference Include="..\..\src\Markup\Avalonia.Markup.Xaml.Loader\Avalonia.Markup.Xaml.Loader.csproj" />
<ProjectReference Include="..\Avalonia.UnitTests\Avalonia.UnitTests.csproj" />
<ProjectReference Include="..\..\src\Avalonia.ReactiveUI.Events\Avalonia.ReactiveUI.Events.csproj" />
</ItemGroup>
</Project>

44
tests/Avalonia.ReactiveUI.Events.UnitTests/BasicControlEventsTest.cs

@ -1,44 +0,0 @@
using System;
using System.Reactive.Linq;
using Avalonia.Controls;
using Avalonia.UnitTests;
using Xunit;
namespace Avalonia.ReactiveUI.Events.UnitTests
{
public class BasicControlEventsTest
{
public class EventsControl : UserControl
{
public bool IsAttached { get; private set; }
public EventsControl()
{
var attached = this
.Events()
.AttachedToVisualTree
.Select(args => true);
this.Events()
.DetachedFromVisualTree
.Select(args => false)
.Merge(attached)
.Subscribe(marker => IsAttached = marker);
}
}
[Fact]
public void Should_Generate_Events_Wrappers()
{
var root = new TestRoot();
var control = new EventsControl();
Assert.False(control.IsAttached);
root.Child = control;
Assert.True(control.IsAttached);
root.Child = null;
Assert.False(control.IsAttached);
}
}
}
Loading…
Cancel
Save