Skip to content

Commit

Permalink
Merge pull request #2715 from cwensley/curtis/fix-SelectableFilterCol…
Browse files Browse the repository at this point in the history
…lection

Fix issue mapping view to model in SelectableFilterCollection
  • Loading branch information
cwensley authored Dec 11, 2024
2 parents d93e8ce + a773336 commit 1b5b7f5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
11 changes: 8 additions & 3 deletions src/Eto/Forms/FilterCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,20 @@ protected override void Rebuild()
viewToModel = new Dictionary<T, int>(Count);
for (int i = 0; i < Count; i++)
{
// used for lookup below, this index is not correct yet
viewToModel.Add(this[i], i);
}

modelToView = new Dictionary<int, int>(Count);
for (int i = 0; i < Items.Count; i++)
for (int j = 0; j < Items.Count; j++)
{
int index;
if (viewToModel.TryGetValue(Items[i], out index))
modelToView.Add(i, index);
if (viewToModel.TryGetValue(Items[j], out index))
{
modelToView.Add(j, index);
// update index with real index in model
viewToModel[Items[j]] = j;
}
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Eto.Test.UnitTests.Forms.Controls
/// <copyright>(c) 2014 by Vivek Jhaveri</copyright>
/// <license type="BSD-3">See LICENSE for full terms</license>
[TestFixture]
public class GridViewSelectableFilterTests
public class GridViewSelectableFilterTests : TestBase
{
GridView grid;
ObservableCollection<DataItem> model;
Expand All @@ -21,7 +21,7 @@ public class GridViewSelectableFilterTests
[SetUp]
public void Setup()
{
TestBase.Invoke(() =>
Invoke(() =>
{
grid = new GridView();
// Some platforms need at least one column for selection to work
Expand All @@ -40,7 +40,7 @@ public void Setup()
[Test]
public void InsertItemShouldNotChangeSelection()
{
TestBase.Invoke(() =>
Invoke(() =>
{
grid.SelectRow(0);
var selectedItem = grid.SelectedItem;
Expand All @@ -52,7 +52,7 @@ public void InsertItemShouldNotChangeSelection()
[Test]
public void DeleteSelectedItemsShouldRemoveSelectedItems()
{
TestBase.Invoke(() =>
Invoke(() =>
{
grid.AllowMultipleSelection = true;

Expand Down Expand Up @@ -88,7 +88,7 @@ public void DeleteSelectedItemsShouldRemoveSelectedItems()
[TestCase(2)]
public void SortItemsShouldNotChangeSelection(int rowToSelect)
{
TestBase.Invoke(() =>
Invoke(() =>
{
filtered.Sort = GridViewUtils.SortItemsAscending;
grid.SelectRow(rowToSelect);
Expand All @@ -111,7 +111,7 @@ public void SortItemsShouldNotChangeSelection(int rowToSelect)
[Test]
public void FilterItemsShouldNotChangeSelection()
{
TestBase.Invoke(() =>
Invoke(() =>
{
grid.AllowMultipleSelection = true;

Expand All @@ -137,5 +137,47 @@ public void FilterItemsShouldNotChangeSelection()
Assert.AreEqual(0, modelSelectionChangedCount, "Model SelectionChanged event should not fire when changing filter");
});
}

[Test]
public void SortedCollectionShouldGetCorrectRow()
{
GridView grid = null;
Shown(form =>
{
grid = new GridView { Size = new Size(200, 200) };
var collection = new SelectableFilterCollection<GridItem>(grid)
{
new("Hello"),
new("There"),
new("Fine"),
new("World"),
new("Of"),
new("Eto")
};

grid.Columns.Add(new GridColumn
{
DataCell = new TextBoxCell(0)
});
grid.DataStore = collection;
collection.Sort = (x, y) => string.Compare((string)x.Values[0], (string)y.Values[0], StringComparison.Ordinal);
// goes to this order:
// Eto
// Fine
// Hello
// Of
// There
// World

collection.SelectRow(4);
form.Content = grid;
}, () => {

Assert.AreEqual(3, grid.SelectedRow, "#1");
Assert.NotNull(grid.SelectedItem, "#2");
Assert.AreEqual("Of", ((GridItem)grid.SelectedItem).Values[0], "#3");
});
}

}
}

0 comments on commit 1b5b7f5

Please sign in to comment.