Browse Source

Fix #3996: edit the display font size in points, not pixels

The options dialog bound DisplaySettings.SelectedFontSize (device-independent
pixels) straight into a NumericUpDown, so a fresh profile showed 13.33 and the
6-72 bounds were pixels. The WPF host presented points via FontSizeConverter;
this restores that behavior on Avalonia with an editable size ComboBox (like
the Windows font dialogs) backed by a pt/px proxy on the viewmodel. The stored
value stays pixels so settings files keep round-tripping with ILSpy 9.x.

Assisted-by: Claude:claude-fable-5:Claude Code
pull/4002/head
Christoph Wille 1 month ago
parent
commit
03e5e5ece2
  1. 216
      ILSpy.Tests/Options/DisplayFontSizeTests.cs
  2. 15
      ILSpy/Options/DisplaySettingsPanel.axaml
  3. 39
      ILSpy/Options/DisplaySettingsViewModel.cs

216
ILSpy.Tests/Options/DisplayFontSizeTests.cs

@ -0,0 +1,216 @@ @@ -0,0 +1,216 @@
// Copyright (c) 2026 Christoph Wille
//
// 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.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Headless.NUnit;
using Avalonia.Threading;
using AwesomeAssertions;
using ICSharpCode.ILSpy.AppEnv;
using ICSharpCode.ILSpy.Commands;
using ICSharpCode.ILSpy.Docking;
using ICSharpCode.ILSpy.Options;
using ICSharpCode.ILSpy.Options.Panels;
using ICSharpCode.ILSpy.Properties;
using ICSharpCode.ILSpy.ViewModels;
using ICSharpCode.ILSpy.Views;
using NUnit.Framework;
namespace ICSharpCode.ILSpy.Tests;
/// <summary>
/// The options dialog edits the font size in points (like the Windows font dialogs and the
/// WPF host's FontSizeConverter), while <see cref="DisplaySettings.SelectedFontSize"/> keeps
/// storing device-independent pixels so persisted settings round-trip with ILSpy 9.x.
/// The pt/px conversion lives in <see cref="DisplaySettingsViewModel.SelectedFontSizePoints"/>.
/// </summary>
[TestFixture]
public class DisplayFontSizeTests
{
static (DisplaySettingsViewModel page, DisplaySettings settings) CreatePage()
{
var service = AppComposition.Current.GetExport<SettingsService>();
var page = new DisplaySettingsViewModel();
page.Load(service);
return (page, service.DisplaySettings);
}
[AvaloniaTest]
public void Default_Pixel_Size_Displays_As_10_Points()
{
var (page, settings) = CreatePage();
var original = settings.SelectedFontSize;
try
{
settings.SelectedFontSize = 10.0 * 4 / 3;
page.SelectedFontSizePoints.Should().Be("10",
"the default 13.33 px must be presented as the 10 pt it actually is");
}
finally
{
settings.SelectedFontSize = original;
}
}
[AvaloniaTest]
public void Typed_Point_Size_Is_Stored_As_Pixels()
{
var (page, settings) = CreatePage();
var original = settings.SelectedFontSize;
try
{
page.SelectedFontSizePoints = "12";
settings.SelectedFontSize.Should().BeApproximately(12.0 * 4 / 3, 1e-9,
"the stored value stays device-independent pixels for 9.x round-tripping");
page.SelectedFontSizePoints.Should().Be("12");
}
finally
{
settings.SelectedFontSize = original;
}
}
[AvaloniaTest]
public void External_Pixel_Change_Refreshes_The_Points_Text()
{
// Reset-to-defaults and LoadFromXml write SelectedFontSize directly; the dialog text
// must follow via PropertyChanged on SelectedFontSizePoints.
var (page, settings) = CreatePage();
var original = settings.SelectedFontSize;
try
{
var notified = new List<string?>();
page.PropertyChanged += (_, e) => notified.Add(e.PropertyName);
settings.SelectedFontSize = 20;
notified.Should().Contain(nameof(DisplaySettingsViewModel.SelectedFontSizePoints));
page.SelectedFontSizePoints.Should().Be("15");
}
finally
{
settings.SelectedFontSize = original;
}
}
[AvaloniaTest]
public void Setting_Points_Through_The_Dialog_Does_Not_Echo_A_Text_Notification()
{
// While the user is typing in the size box, the setter must not raise PropertyChanged
// for SelectedFontSizePoints - the binding would rewrite the box mid-keystroke
// (typing "10." would snap back to "10" before the fraction can be completed).
var (page, settings) = CreatePage();
var original = settings.SelectedFontSize;
try
{
var notified = new List<string?>();
page.PropertyChanged += (_, e) => notified.Add(e.PropertyName);
page.SelectedFontSizePoints = "14";
notified.Should().NotContain(nameof(DisplaySettingsViewModel.SelectedFontSizePoints));
}
finally
{
settings.SelectedFontSize = original;
}
}
[AvaloniaTest]
public void NonNumeric_Input_Is_Ignored()
{
var (page, settings) = CreatePage();
var original = settings.SelectedFontSize;
try
{
settings.SelectedFontSize = 16;
page.SelectedFontSizePoints = "abc";
settings.SelectedFontSize.Should().Be(16,
"transient garbage while typing must not move the stored size");
}
finally
{
settings.SelectedFontSize = original;
}
}
[AvaloniaTest]
public void Typed_Sizes_Are_Clamped_To_The_6_To_72_Point_Range()
{
var (page, settings) = CreatePage();
var original = settings.SelectedFontSize;
try
{
page.SelectedFontSizePoints = "1";
settings.SelectedFontSize.Should().BeApproximately(6.0 * 4 / 3, 1e-9);
page.SelectedFontSizePoints = "500";
settings.SelectedFontSize.Should().BeApproximately(72.0 * 4 / 3, 1e-9);
}
finally
{
settings.SelectedFontSize = original;
}
}
[AvaloniaTest]
public void Size_List_Offers_6_Through_24_Points_Like_The_WPF_Host()
{
var (page, _) = CreatePage();
page.FontSizes.Should().Equal(Enumerable.Range(6, 24 - 6 + 1));
}
[AvaloniaTest]
public async Task Display_Panel_Size_Box_Is_An_Editable_ComboBox_Showing_Points()
{
var window = AppComposition.Current.GetExport<MainWindow>();
window.Show();
var settings = AppComposition.Current.GetExport<SettingsService>();
var original = settings.DisplaySettings.SelectedFontSize;
try
{
settings.DisplaySettings.SelectedFontSize = 10.0 * 4 / 3;
AppComposition.Current.GetExport<MainMenuCommandRegistry>()
.GetCommand(nameof(Resources._Options)).Execute(null);
var vm = (MainWindowViewModel)window.DataContext!;
var model = (OptionsPageModel)vm.DockWorkspace.Documents!.VisibleDockables!
.OfType<ContentTabPage>().First(t => t.Content is OptionsPageModel).Content!;
model.SelectedPage = model.Pages.OfType<DisplaySettingsViewModel>().Single();
TestCapture.Step("display-page-selected");
var panel = await window.WaitForComponent<DisplaySettingsPanel>();
var box = panel.FindControl<ComboBox>("fontSizeComboBox");
((object?)box).Should().NotBeNull("the size box must be the named editable ComboBox");
Dispatcher.UIThread.RunJobs();
box!.IsEditable.Should().BeTrue("custom sizes must be typeable, like Notepad's font page");
box.Text.Should().Be("10", "the box shows points, not device-independent pixels");
}
finally
{
settings.DisplaySettings.SelectedFontSize = original;
}
}
}

15
ILSpy/Options/DisplaySettingsPanel.axaml

@ -20,16 +20,21 @@ @@ -20,16 +20,21 @@
<ComboBox MinWidth="120" ItemsSource="{Binding AllThemes}"
SelectedItem="{Binding SessionSettings.Theme, Mode=TwoWay}" />
</StackPanel>
<Grid ColumnDefinitions="Auto,*,Auto" RowDefinitions="Auto,Auto" ColumnSpacing="8" RowSpacing="4">
<Grid ColumnDefinitions="Auto,*,Auto,Auto" RowDefinitions="Auto,Auto" ColumnSpacing="8" RowSpacing="4">
<TextBlock Grid.Row="0" Grid.Column="0" VerticalAlignment="Center"
Text="{x:Static res:Resources.Font}" />
<ComboBox Grid.Row="0" Grid.Column="1" MinWidth="200"
ItemsSource="{Binding AvailableFonts}"
SelectedItem="{Binding Settings.SelectedFont, Mode=TwoWay}" />
<NumericUpDown Grid.Row="0" Grid.Column="2" Width="100"
Minimum="6" Maximum="72" Increment="1"
Value="{Binding Settings.SelectedFontSize, Mode=TwoWay}" />
<Border Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"
<TextBlock Grid.Row="0" Grid.Column="2" VerticalAlignment="Center"
Text="{x:Static res:Resources.Size}" />
<!-- Edits in points (the unit every Windows font dialog uses); the viewmodel
converts to the device-independent pixels DisplaySettings stores. -->
<ComboBox Grid.Row="0" Grid.Column="3" Width="100" x:Name="fontSizeComboBox"
IsEditable="True"
ItemsSource="{Binding FontSizes}"
Text="{Binding SelectedFontSizePoints, Mode=TwoWay}" />
<Border Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="3"
BorderBrush="{DynamicResource ILSpy.ChromeBorder}" BorderThickness="1" Padding="6" Margin="0,2,0,0">
<!-- FontFamily binds to a derived FontFamily property because Avalonia's
runtime binding pipeline doesn't coerce string → FontFamily (the

39
ILSpy/Options/DisplaySettingsViewModel.cs

@ -16,8 +16,10 @@ @@ -16,8 +16,10 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Xml.Linq;
@ -55,6 +57,40 @@ namespace ICSharpCode.ILSpy.Options @@ -55,6 +57,40 @@ namespace ICSharpCode.ILSpy.Options
.OrderBy(n => n, System.StringComparer.OrdinalIgnoreCase)
.ToArray();
/// <summary>Point sizes offered in the size dropdown; same list as the WPF host.</summary>
public IReadOnlyList<int> FontSizes { get; } = Enumerable.Range(6, 24 - 6 + 1).ToArray();
/// <summary>
/// The font size as the user sees and edits it: points, like the Windows font dialogs.
/// <see cref="DisplaySettings.SelectedFontSize"/> itself stays in device-independent
/// pixels (1 pt = 4/3 px) so persisted settings round-trip with the WPF host; the
/// conversion happens only at this dialog boundary. Non-numeric input is ignored
/// (it is usually a transient typing state), numeric input is clamped to 6-72 pt.
/// </summary>
public string SelectedFontSizePoints {
get => Settings == null
? string.Empty
: Math.Round(Settings.SelectedFontSize * 3 / 4).ToString(CultureInfo.CurrentCulture);
set {
if (Settings == null || !double.TryParse(value, NumberStyles.Float, CultureInfo.CurrentCulture, out double points))
return;
points = Math.Clamp(points, 6, 72);
// Suppress the PropertyChanged echo for this property: the binding would
// immediately rewrite the size box with the rounded value mid-keystroke.
updatingFontSizeFromText = true;
try
{
Settings.SelectedFontSize = points * 4 / 3;
}
finally
{
updatingFontSizeFromText = false;
}
}
}
bool updatingFontSizeFromText;
/// <summary>
/// Derived FontFamily for the preview TextBlock. Avalonia's runtime binding pipeline
/// doesn't auto-coerce a <c>string</c> source to <see cref="FontFamily"/> (the implicit
@ -75,12 +111,15 @@ namespace ICSharpCode.ILSpy.Options @@ -75,12 +111,15 @@ namespace ICSharpCode.ILSpy.Options
Settings.PropertyChanged += OnSettingsPropertyChanged;
SessionSettings = service.SessionSettings;
OnPropertyChanged(nameof(CurrentFontFamily));
OnPropertyChanged(nameof(SelectedFontSizePoints));
}
void OnSettingsPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(DisplaySettings.SelectedFont))
OnPropertyChanged(nameof(CurrentFontFamily));
if (e.PropertyName == nameof(DisplaySettings.SelectedFontSize) && !updatingFontSizeFromText)
OnPropertyChanged(nameof(SelectedFontSizePoints));
}
public void LoadDefaults()

Loading…
Cancel
Save