Browse Source

Editor zoom — Ctrl+Wheel scale + corner button overlay

Mirrors WPF's ZoomScrollViewer behaviour without templating AvaloniaEdit's
TextEditor. The Avalonia path scales the editor's FontSize (which feeds
DisplaySettings.SelectedFontSize) on Ctrl+Wheel; AvaloniaEdit lays out at the
new size on the next render. WPF's full-content-tree LayoutTransform approach
doesn't fit AvaloniaEdit's architecture, but the font-scale result is what
users actually expect from "editor zoom".

Assisted-by: Claude:claude-opus-4-7:Claude Code
pull/3755/head
Siegfried Pammer 2 months ago
parent
commit
05ae000bad
  1. 82
      ILSpy.Tests/Editor/EditorZoomTests.cs
  2. 4
      ILSpy/TextView/DecompilerTextView.axaml
  3. 54
      ILSpy/TextView/DecompilerTextView.axaml.cs
  4. 58
      ILSpy/TextView/EditorZoom.cs
  5. 19
      ILSpy/TextView/ZoomButtons.axaml
  6. 135
      ILSpy/TextView/ZoomButtons.axaml.cs

82
ILSpy.Tests/Editor/EditorZoomTests.cs

@ -0,0 +1,82 @@
// 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 AwesomeAssertions;
using ILSpy.TextView;
using NUnit.Framework;
namespace ICSharpCode.ILSpy.Tests;
[TestFixture]
public class EditorZoomTests
{
[Test]
public void ZoomIn_Multiplies_By_Factor()
{
EditorZoom.ZoomIn(10.0).Should().BeApproximately(11.0, 0.001);
}
[Test]
public void ZoomOut_Divides_By_Factor()
{
EditorZoom.ZoomOut(11.0).Should().BeApproximately(10.0, 0.001);
}
[Test]
public void Reset_Returns_Default_Font_Size()
{
EditorZoom.Reset().Should().Be(EditorZoom.DefaultFontSize);
}
[Test]
public void Zoom_Round_Trip_Returns_To_Default_Without_Floating_Point_Drift()
{
// 1.1 * (1/1.1) is bit-equal-ish but not exactly equal in float; without the
// snap-to-default heuristic, the rounded-trip value would be 13.3333000001
// instead of 13.3333333... and the user would see a sticky "non-100% zoom"
// even after they explicitly returned to default. The Reset path bypasses
// this entirely, but Ctrl+Wheel round trips need the snap.
var step1 = EditorZoom.ZoomIn(EditorZoom.DefaultFontSize);
var step2 = EditorZoom.ZoomOut(step1);
step2.Should().Be(EditorZoom.DefaultFontSize);
}
[Test]
public void ZoomIn_Clamps_At_Upper_Bound()
{
EditorZoom.ZoomIn(EditorZoom.MaxFontSize).Should().Be(EditorZoom.MaxFontSize);
}
[Test]
public void ZoomOut_Clamps_At_Lower_Bound()
{
EditorZoom.ZoomOut(EditorZoom.MinFontSize).Should().Be(EditorZoom.MinFontSize);
}
[Test]
public void ZoomOut_Of_Slightly_Above_Min_Saturates_Not_Below()
{
// Slightly above MinFontSize zoomed out by the standard factor should land
// AT min, not below. Guards against an off-by-clamp bug where the post-divide
// value is below min but clamping only runs on the multiplier output.
var justAbove = EditorZoom.MinFontSize * 1.05;
EditorZoom.ZoomOut(justAbove).Should().Be(EditorZoom.MinFontSize);
}
}

4
ILSpy/TextView/DecompilerTextView.axaml

@ -42,5 +42,9 @@
</StackPanel> </StackPanel>
</Grid> </Grid>
</Border> </Border>
<!-- Zoom-buttons overlay: bottom-right corner of the editor, auto-hides at default
font size unless DisplaySettings.AlwaysShowZoomButtons is on. Bound at code-
behind time once DisplaySettings is resolvable from composition. -->
<textView:ZoomButtons Name="ZoomButtons" />
</Grid> </Grid>
</UserControl> </UserControl>

54
ILSpy/TextView/DecompilerTextView.axaml.cs

@ -72,6 +72,7 @@ namespace ILSpy.TextView
readonly List<AvaloniaEdit.Rendering.VisualLineElementGenerator> activeCustomGenerators = new(); readonly List<AvaloniaEdit.Rendering.VisualLineElementGenerator> activeCustomGenerators = new();
RichTextColorizer? activeColorizer; RichTextColorizer? activeColorizer;
FoldingManager? activeFoldingManager; FoldingManager? activeFoldingManager;
DisplaySettings? currentDisplaySettings;
ReferenceSegment? lastTooltipSegment; ReferenceSegment? lastTooltipSegment;
ReferenceSegment? lastRightClickedSegment; ReferenceSegment? lastRightClickedSegment;
IReadOnlyList<IContextMenuEntryExport> contextMenuEntries = Array.Empty<IContextMenuEntryExport>(); IReadOnlyList<IContextMenuEntryExport> contextMenuEntries = Array.Empty<IContextMenuEntryExport>();
@ -173,6 +174,57 @@ namespace ILSpy.TextView
// integer / double assignments. // integer / double assignments.
Editor.TextArea.Caret.PositionChanged += OnCaretPositionChanged; Editor.TextArea.Caret.PositionChanged += OnCaretPositionChanged;
Editor.TextArea.TextView.ScrollOffsetChanged += OnScrollOffsetChanged; Editor.TextArea.TextView.ScrollOffsetChanged += OnScrollOffsetChanged;
// Ctrl+Wheel zoom (matches WPF's ZoomScrollViewer.OnPreviewMouseWheel). Tunnel
// routing so we see it before AvaloniaEdit's scroll handler; Handled=true on
// hit suppresses the scroll. Ctrl+0/Plus/Minus are Avalonia-side extras —
// keyboard zoom is a standard modern-editor expectation that WPF ILSpy doesn't
// happen to ship.
Editor.AddHandler(InputElement.PointerWheelChangedEvent,
OnEditorPointerWheelChanged,
RoutingStrategies.Tunnel,
handledEventsToo: false);
Editor.KeyDown += OnEditorKeyDownForZoom;
}
void OnEditorPointerWheelChanged(object? sender, PointerWheelEventArgs e)
{
if ((e.KeyModifiers & KeyModifiers.Control) != KeyModifiers.Control)
return;
if (currentDisplaySettings == null)
return;
var step = e.Delta.Y > 0 ? (System.Func<double, double>)EditorZoom.ZoomIn : EditorZoom.ZoomOut;
currentDisplaySettings.SelectedFontSize = step(currentDisplaySettings.SelectedFontSize);
e.Handled = true;
}
void OnEditorKeyDownForZoom(object? sender, KeyEventArgs e)
{
if ((e.KeyModifiers & KeyModifiers.Control) != KeyModifiers.Control)
return;
if (currentDisplaySettings == null)
return;
// OemPlus is the unmodified key on the same physical button as "+"; with Shift it
// produces "+", without it produces "=". Accept both so Ctrl+Plus and Ctrl+=
// (which most users actually type) both zoom in. Add (numeric keypad) covers numpad.
switch (e.Key)
{
case Key.OemPlus:
case Key.Add:
currentDisplaySettings.SelectedFontSize = EditorZoom.ZoomIn(currentDisplaySettings.SelectedFontSize);
e.Handled = true;
break;
case Key.OemMinus:
case Key.Subtract:
currentDisplaySettings.SelectedFontSize = EditorZoom.ZoomOut(currentDisplaySettings.SelectedFontSize);
e.Handled = true;
break;
case Key.D0:
case Key.NumPad0:
currentDisplaySettings.SelectedFontSize = EditorZoom.Reset();
e.Handled = true;
break;
}
} }
void OnCaretPositionChanged(object? sender, EventArgs e) void OnCaretPositionChanged(object? sender, EventArgs e)
@ -200,6 +252,8 @@ namespace ILSpy.TextView
var settings = TryGetDisplaySettings(); var settings = TryGetDisplaySettings();
if (settings == null) if (settings == null)
return; return;
currentDisplaySettings = settings;
ZoomButtons.Bind(settings);
ApplyAllDisplaySettings(settings); ApplyAllDisplaySettings(settings);
settings.PropertyChanged += (_, e) => ApplyDisplaySetting(settings, e.PropertyName); settings.PropertyChanged += (_, e) => ApplyDisplaySetting(settings, e.PropertyName);
} }

58
ILSpy/TextView/EditorZoom.cs

@ -0,0 +1,58 @@
// 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;
namespace ILSpy.TextView
{
/// <summary>
/// Pure-math helper for editor zoom (font-size scaling). The actual editor wiring
/// reads/writes <c>DisplaySettings.SelectedFontSize</c>; this class encapsulates
/// the step calculation and clamping so it's unit-testable without an editor.
/// </summary>
public static class EditorZoom
{
/// <summary>Multiplicative step per wheel tick / button press.</summary>
public const double Factor = 1.1;
/// <summary>Lower bound in points. Below ~8 pt the gutter glyphs collapse.</summary>
public const double MinFontSize = 8.0;
/// <summary>Upper bound in points. Above ~72 pt one glyph fills the viewport.</summary>
public const double MaxFontSize = 72.0;
/// <summary>Default font size in points, matching <c>DisplaySettings</c>'s initial value.</summary>
public const double DefaultFontSize = 10.0 * 4 / 3;
public static double ZoomIn(double currentFontSize)
=> Clamp(RoundToDefaultIfClose(currentFontSize * Factor));
public static double ZoomOut(double currentFontSize)
=> Clamp(RoundToDefaultIfClose(currentFontSize / Factor));
public static double Reset() => DefaultFontSize;
/// <summary>Snap to <see cref="DefaultFontSize"/> when within 0.001 pt — avoids
/// floating-point drift after zoom-in followed by zoom-out leaving the size stuck
/// at 13.3333000001 instead of the canonical 13.3333333.</summary>
static double RoundToDefaultIfClose(double size)
=> Math.Abs(size - DefaultFontSize) < 0.001 ? DefaultFontSize : size;
static double Clamp(double size) => Math.Max(MinFontSize, Math.Min(MaxFontSize, size));
}
}

19
ILSpy/TextView/ZoomButtons.axaml

@ -0,0 +1,19 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="ILSpy.TextView.ZoomButtons"
HorizontalAlignment="Right" VerticalAlignment="Bottom"
Margin="0,0,12,12">
<Border Background="#E0FFFFE1" BorderBrush="#80808080" BorderThickness="1"
CornerRadius="3" Padding="2">
<StackPanel Orientation="Horizontal" Spacing="2">
<Button Name="MinusButton" Content="&#8722;" Width="22" Height="22"
Padding="0" ToolTip.Tip="Zoom out" />
<TextBlock Name="PercentLabel" VerticalAlignment="Center"
MinWidth="42" TextAlignment="Center" FontSize="11" />
<Button Name="PlusButton" Content="&#43;" Width="22" Height="22"
Padding="0" ToolTip.Tip="Zoom in" />
<Button Name="ResetButton" Content="100%" Width="42" Height="22"
Padding="0" FontSize="11" ToolTip.Tip="Reset to default zoom" />
</StackPanel>
</Border>
</UserControl>

135
ILSpy/TextView/ZoomButtons.axaml.cs

@ -0,0 +1,135 @@
// 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;
using System.ComponentModel;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using ILSpy.Options;
namespace ILSpy.TextView
{
/// <summary>
/// Editor-corner overlay with zoom in / out / reset buttons plus a live "133%" label.
/// Bound to <see cref="DisplaySettings.SelectedFontSize"/>; the overlay auto-hides at
/// the default font size unless <see cref="AlwaysShowZoomButtons"/> is set to <c>true</c>.
/// </summary>
public partial class ZoomButtons : UserControl
{
/// <summary>
/// When <c>false</c> (the default), the overlay is hidden while the zoom level is at
/// 100% and only appears once the user actually zooms. When <c>true</c>, it stays
/// visible permanently. Matches WPF's <c>ZoomScrollViewer.AlwaysShowZoomButtons</c>.
/// </summary>
public static readonly StyledProperty<bool> AlwaysShowZoomButtonsProperty =
AvaloniaProperty.Register<ZoomButtons, bool>(nameof(AlwaysShowZoomButtons));
public bool AlwaysShowZoomButtons {
get => GetValue(AlwaysShowZoomButtonsProperty);
set => SetValue(AlwaysShowZoomButtonsProperty, value);
}
DisplaySettings? settings;
public ZoomButtons()
{
InitializeComponent();
RefreshVisibility();
// Defer named-control lookup to AttachedToVisualTree: at construction time the
// UserControl's own NameScope isn't yet attached to `this`, so FindControl walks
// upward and throws "Could not find parent name scope". After visual-tree attach
// the scope is in place and the named children resolve normally.
AttachedToVisualTree += (_, _) => AttachButtonHandlers();
}
void InitializeComponent() => AvaloniaXamlLoader.Load(this);
bool handlersAttached;
void AttachButtonHandlers()
{
if (handlersAttached)
return;
handlersAttached = true;
if (this.FindControl<Button>("MinusButton") is { } minus)
minus.Click += (_, _) => Zoom(EditorZoom.ZoomOut);
if (this.FindControl<Button>("PlusButton") is { } plus)
plus.Click += (_, _) => Zoom(EditorZoom.ZoomIn);
if (this.FindControl<Button>("ResetButton") is { } reset)
reset.Click += (_, _) => Zoom(_ => EditorZoom.Reset());
}
/// <summary>
/// Binds this widget to a live <see cref="DisplaySettings"/>. Re-renders the
/// percent label and visibility on each <c>SelectedFontSize</c> change. Safe to
/// call multiple times — the previous subscription is dropped before the new one
/// is wired.
/// </summary>
public void Bind(DisplaySettings settings)
{
if (this.settings != null)
this.settings.PropertyChanged -= OnSettingsChanged;
this.settings = settings;
if (settings != null)
settings.PropertyChanged += OnSettingsChanged;
RefreshLabel();
RefreshVisibility();
}
void OnSettingsChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(DisplaySettings.SelectedFontSize))
{
RefreshLabel();
RefreshVisibility();
}
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
if (e.Property == AlwaysShowZoomButtonsProperty)
RefreshVisibility();
}
void Zoom(Func<double, double> step)
{
if (settings == null)
return;
settings.SelectedFontSize = step(settings.SelectedFontSize);
}
void RefreshLabel()
{
if (settings == null || this.FindControl<TextBlock>("PercentLabel") is not { } label)
return;
var pct = (int)Math.Round(settings.SelectedFontSize / EditorZoom.DefaultFontSize * 100.0);
label.Text = pct + "%";
}
void RefreshVisibility()
{
IsVisible = settings != null
&& (AlwaysShowZoomButtons
|| Math.Abs(settings.SelectedFontSize - EditorZoom.DefaultFontSize) > 0.001);
}
}
}
Loading…
Cancel
Save