Browse Source

Restore the version combo selection after a language flip

Selecting a C# version, switching to a version-less language (IL), then back to
C# left the toolbar's version ComboBox blank even though LanguageService had
restored CurrentVersion. The MVVM-toolkit setter runs OnCurrentLanguageChanged
(which assigns CurrentVersion, pushing it onto the bound SelectedItem) before it
raises PropertyChanged for CurrentLanguage (which repopulates ItemsSource from
the new language's versions). So SelectedItem is set against the previous
language's still-stale, empty list, rejected, and never re-pulled once
ItemsSource updates.

Re-assert the version combo's SelectedItem from the bound CurrentVersion after
each ItemsSource swap settles, keeping the model untouched.

Assisted-by: Claude:claude-opus-4-8:Claude Code
pull/3756/head
Siegfried Pammer 4 weeks ago
parent
commit
59bedabb6f
  1. 80
      ILSpy.Tests/MainWindow/LanguageVersionPersistenceTests.cs
  2. 21
      ILSpy/Views/MainToolBar.axaml.cs

80
ILSpy.Tests/MainWindow/LanguageVersionPersistenceTests.cs

@ -0,0 +1,80 @@ @@ -0,0 +1,80 @@
// Copyright (c) 2026 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Headless.NUnit;
using Avalonia.Threading;
using Avalonia.VisualTree;
using AwesomeAssertions;
using ILSpy;
using ILSpy.AppEnv;
using ILSpy.Languages;
using NUnit.Framework;
namespace ICSharpCode.ILSpy.Tests;
[TestFixture]
public class LanguageVersionPersistenceTests
{
[AvaloniaTest]
public async Task Switching_Away_From_And_Back_To_A_Versioned_Language_Restores_The_Version()
{
// Regression: selecting C# x.y, switching to a language with no versions (IL), then back to
// C# must restore x.y in the version combo. The toolbar's version ComboBox is bound TwoWay
// to LanguageService.CurrentVersion; when the language flips, its ItemsSource swaps to the
// new (empty) language first and writes a null selection back, so the outgoing version has
// to be stashed before that happens or the restore reads back null.
var (window, _) = await TestHarness.BootAsync();
var toolbar = await window.WaitForComponent<MainToolBar>();
var languageCombo = toolbar.GetVisualDescendants().OfType<ComboBox>()
.Single(c => c.Name == "LanguageComboBox");
var versionCombo = toolbar.GetVisualDescendants().OfType<ComboBox>()
.Single(c => c.Name == "LanguageVersionComboBox");
var languageService = AppComposition.Current.GetExport<LanguageService>();
var csharp = languageService.Languages.Single(l => l.Name == "C#");
var noVersionLanguage = languageService.Languages.First(l => !l.HasLanguageVersions);
// Start on C# and pick a version that is NOT the default latest, so a reset is detectable.
languageCombo.SelectedItem = csharp;
Dispatcher.UIThread.RunJobs();
var chosen = csharp.LanguageVersions.First();
chosen.Should().NotBe(csharp.LanguageVersions.Last(), "the test needs a non-default version");
versionCombo.SelectedItem = chosen;
Dispatcher.UIThread.RunJobs();
languageService.CurrentVersion.Should().Be(chosen);
// Flip to a language with no versions, then back to C#.
languageCombo.SelectedItem = noVersionLanguage;
Dispatcher.UIThread.RunJobs();
languageCombo.SelectedItem = csharp;
Dispatcher.UIThread.RunJobs();
languageService.CurrentVersion.Should().Be(chosen,
"switching away from C# and back must restore the previously selected C# version");
versionCombo.SelectedItem.Should().Be(chosen,
"the version ComboBox must reflect the restored version");
}
}

21
ILSpy/Views/MainToolBar.axaml.cs

@ -24,6 +24,7 @@ using System.Reflection; @@ -24,6 +24,7 @@ using System.Reflection;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Threading;
using ILSpy.AppEnv;
using ILSpy.Commands;
@ -112,6 +113,26 @@ public partial class MainToolBar : UserControl @@ -112,6 +113,26 @@ public partial class MainToolBar : UserControl
if (menuRegistry != null)
WireManageAssemblyListsButton(menuRegistry);
WireLanguageVersionComboSync();
}
void WireLanguageVersionComboSync()
{
// The version ComboBox binds ItemsSource to CurrentLanguage.LanguageVersions and SelectedItem
// to CurrentVersion. On a language switch the model assigns CurrentVersion (pushing it onto
// SelectedItem) before the CurrentLanguage change propagates to ItemsSource, so SelectedItem
// is set against the previous language's list, rejected, and left null once ItemsSource
// repopulates. Re-assert the selection from the bound CurrentVersion after each ItemsSource
// swap has settled so flipping to a version-less language (e.g. IL) and back restores it.
LanguageVersionComboBox.PropertyChanged += (_, e) => {
if (e.Property != ItemsControl.ItemsSourceProperty)
return;
Dispatcher.UIThread.Post(() => {
if (DataContext is MainWindowViewModel vm)
LanguageVersionComboBox.SelectedItem = vm.LanguageService.CurrentVersion;
}, DispatcherPriority.Background);
};
}
void DispatchToolbarCommands(ToolbarCommandRegistry registry)

Loading…
Cancel
Save