Skip to content

Commit

Permalink
Merge pull request #419 from garstenauer/remove-margin-around-texteditor
Browse files Browse the repository at this point in the history
Remove margin around TextEditor
  • Loading branch information
maxkatz6 authored Dec 16, 2024
2 parents ddaf39e + 6ebeddc commit 73382a7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/AvaloniaEdit.Demo/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
xmlns:AvalonEdit="using:AvaloniaEdit"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:AvaloniaEdit.Demo.ViewModels"
xmlns:editing="using:AvaloniaEdit.Editing"
MinWidth="0"
MinHeight="300"
Width="1000"
Expand Down Expand Up @@ -47,7 +48,6 @@
</StackPanel>
<AvalonEdit:TextEditor Name="Editor"
FontFamily="Cascadia Code,Consolas,Menlo,Monospace"
Margin="30"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Visible"
FontWeight="Light"
Expand All @@ -62,6 +62,11 @@
<MenuItem Header="Select All" InputGesture="ctrl+A" Command="{Binding SelectAllMouseCommmand}" CommandParameter="{Binding #Editor.TextArea}"></MenuItem>
</MenuFlyout>
</AvalonEdit:TextEditor.ContextFlyout>
<AvalonEdit:TextEditor.Styles>
<Style Selector="editing|LineNumberMargin">
<Setter Property="MinWidthInDigits" Value="3" />
</Style>
</AvalonEdit:TextEditor.Styles>
</AvalonEdit:TextEditor>
</DockPanel>
</Window>
38 changes: 32 additions & 6 deletions src/AvaloniaEdit/Editing/LineNumberMargin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ public class LineNumberMargin : AbstractMargin
private AnchorSegment _selectionStart;
private bool _selecting;

/// <summary>
/// Identifies the <see cref="MinWidthInDigits"/> styled Avalonia property.
/// </summary>
public static readonly StyledProperty<int> MinWidthInDigitsProperty =
AvaloniaProperty.Register<LineNumberMargin, int>(nameof(MinWidthInDigits), 2);

/// <summary>
/// Gets or sets the minimum width of the line number margin measured in "number of digits".
/// This is a styled Avalonia property.
/// </summary>
/// <value>The minimum width in number of digits. The default value is 2.</value>
/// <remarks>
/// The line number margin may appear too small when there is only one digit. This property
/// allows to reserve additional space.
/// </remarks>
public int MinWidthInDigits
{
get => GetValue(MinWidthInDigitsProperty);
set => SetValue(MinWidthInDigitsProperty, value);
}

/// <summary>
/// The typeface used for rendering the line number margin.
/// This field is calculated in MeasureOverride() based on the FontFamily etc. properties.
Expand All @@ -48,7 +69,16 @@ public class LineNumberMargin : AbstractMargin
/// </summary>
protected double EmSize { get; set; }

/// <inheritdoc/>
/// <inheritdoc/>
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);

if (change.Property == MinWidthInDigitsProperty)
OnDocumentLineCountChanged();
}

/// <inheritdoc/>
protected override Size MeasureOverride(Size availableSize)
{
Typeface = this.CreateTypeface();
Expand Down Expand Up @@ -134,11 +164,7 @@ private void OnDocumentLineCountChanged()
{
var documentLineCount = Document?.LineCount ?? 1;
var newLength = documentLineCount.ToString(CultureInfo.CurrentCulture).Length;

// The margin looks too small when there is only one digit, so always reserve space for
// at least two digits
if (newLength < 2)
newLength = 2;
newLength = Math.Max(newLength, MinWidthInDigits);

if (newLength != MaxLineNumberLength)
{
Expand Down

0 comments on commit 73382a7

Please sign in to comment.