Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove margin around TextEditor #419

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading