mirror of https://github.com/icsharpcode/ILSpy.git
Browse Source
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 Codepull/3755/head
6 changed files with 352 additions and 0 deletions
@ -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); |
||||||
|
} |
||||||
|
} |
||||||
@ -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)); |
||||||
|
} |
||||||
|
} |
||||||
@ -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="−" 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="+" 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> |
||||||
@ -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…
Reference in new issue