Skip to content

Commit

Permalink
Merge pull request #2494 from cwensley/curtis/mac-fix-segmentedbutton…
Browse files Browse the repository at this point in the history
…-inserting

Mac: Fix inserting items at the beginning of a SegmentedButton
  • Loading branch information
cwensley authored Jun 2, 2023
2 parents 394ca8d + 8772fb3 commit fd15dcb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/Eto.Mac/Forms/Controls/SegmentedButtonHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,16 +371,13 @@ public void ClearItems()

public void InsertItem(int index, SegmentedItem item)
{
var count = ++Control.SegmentCount;
var count = Control.SegmentCount++;

// need to copy items as we can't insert items
if (index < count - 1)
for (nint i = index; i < count; i++)
{
for (nint i = index; i < count; i++)
{
var next = i - 1;
CopyItem(i, next);
}
var next = i + 1;
CopyItem(i, next);
}
SetItem(index, item);
if (item.Selected)
Expand Down
17 changes: 17 additions & 0 deletions test/Eto.Test/UnitTests/Forms/Controls/SegmentedButtonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -517,5 +517,22 @@ public void SelectAllAndClearSelectionShouldTriggerSelectedChanges()
Assert.AreEqual(2, item2SelectedChanged, "#6.7.2");
Assert.AreEqual(2, item3SelectedChanged, "#6.7.3");
}

[Test]
[InvokeOnUI]
public void InsertingItemsShouldWork()
{
var segmentedButton = new SegmentedButton();
segmentedButton.Items.Add(new ButtonSegmentedItem { Text = "Item 1" });
segmentedButton.Items.Add(new ButtonSegmentedItem { Text = "Item 2" });
segmentedButton.Items.Add(new ButtonSegmentedItem { Text = "Item 3" });

// insert at beginning
segmentedButton.Items.Insert(0, new ButtonSegmentedItem { Text = "Item Beginning" });
// insert at middle
segmentedButton.Items.Insert(1, new ButtonSegmentedItem { Text = "Item Middle" });
// insert at end
segmentedButton.Items.Insert(segmentedButton.Items.Count, new ButtonSegmentedItem { Text = "Item End" });
}
}
}

0 comments on commit fd15dcb

Please sign in to comment.