From 8772fb3f93ff313a53fb7dd99a46e857ec2d3827 Mon Sep 17 00:00:00 2001 From: Curtis Wensley Date: Fri, 2 Jun 2023 14:58:40 -0700 Subject: [PATCH] Mac: Fix inserting items at the beginning of a SegmentedButton --- .../Forms/Controls/SegmentedButtonHandler.cs | 11 ++++------- .../Forms/Controls/SegmentedButtonTests.cs | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/Eto.Mac/Forms/Controls/SegmentedButtonHandler.cs b/src/Eto.Mac/Forms/Controls/SegmentedButtonHandler.cs index 802d103d37..b3ff05572b 100644 --- a/src/Eto.Mac/Forms/Controls/SegmentedButtonHandler.cs +++ b/src/Eto.Mac/Forms/Controls/SegmentedButtonHandler.cs @@ -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) diff --git a/test/Eto.Test/UnitTests/Forms/Controls/SegmentedButtonTests.cs b/test/Eto.Test/UnitTests/Forms/Controls/SegmentedButtonTests.cs index cc5e3996c2..e3604f97e8 100644 --- a/test/Eto.Test/UnitTests/Forms/Controls/SegmentedButtonTests.cs +++ b/test/Eto.Test/UnitTests/Forms/Controls/SegmentedButtonTests.cs @@ -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" }); + } } } \ No newline at end of file