Skip to content

Commit

Permalink
enhance: allow to configure editor tab width in preferences window (#…
Browse files Browse the repository at this point in the history
…1048)

Signed-off-by: leo <longshuang@msn.cn>
  • Loading branch information
love-linger committed Mar 4, 2025
1 parent 11af5d9 commit 2137ad9
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/Resources/Locales/en_US.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@
<x:String x:Key="Text.Preferences.AI.Streaming" xml:space="preserve">Enable Streaming</x:String>
<x:String x:Key="Text.Preferences.Appearance" xml:space="preserve">APPEARANCE</x:String>
<x:String x:Key="Text.Preferences.Appearance.DefaultFont" xml:space="preserve">Default Font</x:String>
<x:String x:Key="Text.Preferences.Appearance.EditorTabWidth" xml:space="preserve">Editor Tab Width</x:String>
<x:String x:Key="Text.Preferences.Appearance.FontSize" xml:space="preserve">Font Size</x:String>
<x:String x:Key="Text.Preferences.Appearance.FontSize.Default" xml:space="preserve">Default</x:String>
<x:String x:Key="Text.Preferences.Appearance.FontSize.Editor" xml:space="preserve">Editor</x:String>
Expand Down
1 change: 1 addition & 0 deletions src/Resources/Locales/zh_CN.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@
<x:String x:Key="Text.Preferences.AI.Streaming" xml:space="preserve">启用流式输出</x:String>
<x:String x:Key="Text.Preferences.Appearance" xml:space="preserve">外观配置</x:String>
<x:String x:Key="Text.Preferences.Appearance.DefaultFont" xml:space="preserve">缺省字体</x:String>
<x:String x:Key="Text.Preferences.Appearance.EditorTabWidth" xml:space="preserve">编辑器制表符宽度</x:String>
<x:String x:Key="Text.Preferences.Appearance.FontSize" xml:space="preserve">字体大小</x:String>
<x:String x:Key="Text.Preferences.Appearance.FontSize.Default" xml:space="preserve">默认</x:String>
<x:String x:Key="Text.Preferences.Appearance.FontSize.Editor" xml:space="preserve">代码编辑器</x:String>
Expand Down
1 change: 1 addition & 0 deletions src/Resources/Locales/zh_TW.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@
<x:String x:Key="Text.Preferences.AI.Streaming" xml:space="preserve">啟用串流輸出</x:String>
<x:String x:Key="Text.Preferences.Appearance" xml:space="preserve">外觀設定</x:String>
<x:String x:Key="Text.Preferences.Appearance.DefaultFont" xml:space="preserve">預設字型</x:String>
<x:String x:Key="Text.Preferences.Appearance.EditorTabWidth" xml:space="preserve">編輯器制表符寬度</x:String>
<x:String x:Key="Text.Preferences.Appearance.FontSize" xml:space="preserve">字型大小</x:String>
<x:String x:Key="Text.Preferences.Appearance.FontSize.Default" xml:space="preserve">預設</x:String>
<x:String x:Key="Text.Preferences.Appearance.FontSize.Editor" xml:space="preserve">程式碼</x:String>
Expand Down
7 changes: 7 additions & 0 deletions src/ViewModels/Preferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ public double EditorFontSize
set => SetProperty(ref _editorFontSize, value);
}

public int EditorTabWidth
{
get => _editorTabWidth;
set => SetProperty(ref _editorTabWidth, value);
}

public LayoutInfo Layout
{
get => _layout;
Expand Down Expand Up @@ -649,6 +655,7 @@ private string FixFontFamilyName(string name)
private bool _useSystemWindowFrame = false;
private double _defaultFontSize = 13;
private double _editorFontSize = 13;
private int _editorTabWidth = 4;
private LayoutInfo _layout = new LayoutInfo();

private int _maxHistoryCommits = 20000;
Expand Down
1 change: 1 addition & 0 deletions src/Views/Blame.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
Foreground="{DynamicResource Brush.FG1}"
FontFamily="{DynamicResource Fonts.Monospace}"
FontSize="{Binding Source={x:Static vm:Preferences.Instance}, Path=EditorFontSize}"
TabWidth="{Binding Source={x:Static vm:Preferences.Instance}, Path=EditorTabWidth}"
BlameData="{Binding Data}"/>

<!-- Not supported mask (for binary files) -->
Expand Down
19 changes: 17 additions & 2 deletions src/Views/Blame.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,15 @@ public Models.BlameData BlameData
set => SetValue(BlameDataProperty, value);
}

public static readonly StyledProperty<int> TabWidthProperty =
AvaloniaProperty.Register<BlameTextEditor, int>(nameof(TabWidth), 4);

public int TabWidth
{
get => GetValue(TabWidthProperty);
set => SetValue(TabWidthProperty, value);
}

protected override Type StyleKeyOverride => typeof(TextEditor);

public BlameTextEditor() : base(new TextArea(), new TextDocument())
Expand All @@ -268,6 +277,10 @@ public Models.BlameData BlameData
ShowLineNumbers = false;
WordWrap = false;

Options.IndentationSize = TabWidth;
Options.EnableHyperlinks = false;
Options.EnableEmailHyperlinks = false;

_textMate = Models.TextMateHelper.CreateForEditor(this);

TextArea.LeftMargins.Add(new LineNumberMargin() { Margin = new Thickness(8, 0) });
Expand All @@ -280,8 +293,6 @@ public Models.BlameData BlameData
TextArea.TextView.ContextRequested += OnTextViewContextRequested;
TextArea.TextView.VisualLinesChanged += OnTextViewVisualLinesChanged;
TextArea.TextView.Margin = new Thickness(4, 0);
TextArea.TextView.Options.EnableHyperlinks = false;
TextArea.TextView.Options.EnableEmailHyperlinks = false;
}

public override void Render(DrawingContext context)
Expand Down Expand Up @@ -350,6 +361,10 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
Text = string.Empty;
}
}
else if (change.Property == TabWidthProperty)
{
Options.IndentationSize = TabWidth;
}
else if (change.Property.Name == "ActualThemeVariant" && change.NewValue != null)
{
Models.TextMateHelper.SetThemeByApp(_textMate);
Expand Down
35 changes: 24 additions & 11 deletions src/Views/Preferences.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
<TabItem.Header>
<TextBlock Classes="tab_header" Text="{DynamicResource Text.Preferences.Appearance}"/>
</TabItem.Header>
<Grid Margin="8" RowDefinitions="32,32,32,32,32,32,32,Auto" ColumnDefinitions="Auto,*">
<Grid Margin="8" RowDefinitions="32,32,32,32,32,32,32,32,Auto" ColumnDefinitions="Auto,*">
<TextBlock Grid.Row="0" Grid.Column="0"
Text="{DynamicResource Text.Preferences.Appearance.Theme}"
HorizontalAlignment="Right"
Expand Down Expand Up @@ -190,12 +190,12 @@
Margin="0,0,16,0"/>
<Grid Grid.Row="3" Grid.Column="1" ColumnDefinitions="*,8,*">
<NumericUpDown Grid.Column="0"
Minimum="10" Maximum="18" Increment="0.5"
Height="28"
Padding="4"
BorderThickness="1" BorderBrush="{DynamicResource Brush.Border1}"
CornerRadius="3"
Value="{Binding DefaultFontSize, Mode=TwoWay}">
Minimum="10" Maximum="18" Increment="0.5"
Height="28"
Padding="4"
BorderThickness="1" BorderBrush="{DynamicResource Brush.Border1}"
CornerRadius="3"
Value="{Binding DefaultFontSize, Mode=TwoWay}">
<NumericUpDown.InnerLeftContent>
<Border BorderThickness="0,0,1,0" BorderBrush="{DynamicResource Brush.Border1}">
<TextBlock Margin="4,0" Text="{DynamicResource Text.Preferences.Appearance.FontSize.Default}"/>
Expand All @@ -218,10 +218,23 @@
</Grid>

<TextBlock Grid.Row="4" Grid.Column="0"
Text="{DynamicResource Text.Preferences.Appearance.EditorTabWidth}"
HorizontalAlignment="Right"
Margin="0,0,16,0"/>
<Grid Grid.Row="4" Grid.Column="1">
<NumericUpDown Minimum="1" Maximum="16" Increment="1"
Height="28"
Padding="4"
BorderThickness="1" BorderBrush="{DynamicResource Brush.Border1}"
CornerRadius="3"
Value="{Binding EditorTabWidth, Mode=TwoWay}"/>
</Grid>

<TextBlock Grid.Row="5" Grid.Column="0"
Text="{DynamicResource Text.Preferences.Appearance.ThemeOverrides}"
HorizontalAlignment="Right"
Margin="0,0,16,0"/>
<TextBox Grid.Row="4" Grid.Column="1"
<TextBox Grid.Row="5" Grid.Column="1"
Height="28"
CornerRadius="3"
Text="{Binding ThemeOverrides, Mode=TwoWay}">
Expand All @@ -232,16 +245,16 @@
</TextBox.InnerRightContent>
</TextBox>

<CheckBox Grid.Row="5" Grid.Column="1"
<CheckBox Grid.Row="6" Grid.Column="1"
Content="{DynamicResource Text.Preferences.Appearance.OnlyUseMonoFontInEditor}"
IsChecked="{Binding OnlyUseMonoFontInEditor, Mode=TwoWay}"/>

<CheckBox Grid.Row="6" Grid.Column="1"
<CheckBox Grid.Row="7" Grid.Column="1"
Height="32"
Content="{DynamicResource Text.Preferences.Appearance.UseFixedTabWidth}"
IsChecked="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseFixedTabWidth, Mode=TwoWay}"/>

<CheckBox Grid.Row="7" Grid.Column="1"
<CheckBox Grid.Row="8" Grid.Column="1"
Height="32"
Content="{DynamicResource Text.Preferences.Appearance.UseNativeWindowFrame}"
IsChecked="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseSystemWindowFrame, Mode=OneTime}"
Expand Down
1 change: 1 addition & 0 deletions src/Views/RevisionFileContentViewer.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<DataTemplate DataType="m:RevisionTextFile">
<v:RevisionTextFileView FontFamily="{DynamicResource Fonts.Monospace}"
FontSize="{Binding Source={x:Static vm:Preferences.Instance}, Path=EditorFontSize}"
TabWidth="{Binding Source={x:Static vm:Preferences.Instance}, Path=EditorTabWidth}"
Background="{DynamicResource Brush.Contents}"/>
</DataTemplate>

Expand Down
26 changes: 24 additions & 2 deletions src/Views/RevisionFileContentViewer.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,32 @@ namespace SourceGit.Views
{
public class RevisionTextFileView : TextEditor
{
public static readonly StyledProperty<int> TabWidthProperty =
AvaloniaProperty.Register<RevisionTextFileView, int>(nameof(TabWidth), 4);

public int TabWidth
{
get => GetValue(TabWidthProperty);
set => SetValue(TabWidthProperty, value);
}

protected override Type StyleKeyOverride => typeof(TextEditor);

public RevisionTextFileView() : base(new TextArea(), new TextDocument())
{
IsReadOnly = true;
ShowLineNumbers = true;
WordWrap = false;

Options.IndentationSize = TabWidth;
Options.EnableHyperlinks = false;
Options.EnableEmailHyperlinks = false;

HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
VerticalScrollBarVisibility = ScrollBarVisibility.Auto;

TextArea.LeftMargins[0].Margin = new Thickness(8, 0);
TextArea.TextView.Margin = new Thickness(4, 0);
TextArea.TextView.Options.EnableHyperlinks = false;
TextArea.TextView.Options.EnableEmailHyperlinks = false;
}

protected override void OnLoaded(RoutedEventArgs e)
Expand Down Expand Up @@ -69,6 +81,16 @@ protected override void OnDataContextChanged(EventArgs e)
}
}

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);

if (change.Property == TabWidthProperty)
{
Options.IndentationSize = TabWidth;
}
}

private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs e)
{
var selected = SelectedText;
Expand Down
3 changes: 3 additions & 0 deletions src/Views/TextDiffView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
IndicatorForeground="{DynamicResource Brush.FG2}"
FontFamily="{DynamicResource Fonts.Monospace}"
FontSize="{Binding Source={x:Static vm:Preferences.Instance}, Path=EditorFontSize}"
TabWidth="{Binding Source={x:Static vm:Preferences.Instance}, Path=EditorTabWidth}"
UseSyntaxHighlighting="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseSyntaxHighlighting}"
WordWrap="{Binding Source={x:Static vm:Preferences.Instance}, Path=EnableDiffViewWordWrap}"
ShowHiddenSymbols="{Binding Source={x:Static vm:Preferences.Instance}, Path=ShowHiddenSymbolsInDiffView}"
Expand Down Expand Up @@ -59,6 +60,7 @@
IndicatorForeground="{DynamicResource Brush.FG2}"
FontFamily="{DynamicResource Fonts.Monospace}"
FontSize="{Binding Source={x:Static vm:Preferences.Instance}, Path=EditorFontSize}"
TabWidth="{Binding Source={x:Static vm:Preferences.Instance}, Path=EditorTabWidth}"
UseSyntaxHighlighting="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseSyntaxHighlighting}"
WordWrap="False"
ShowHiddenSymbols="{Binding Source={x:Static vm:Preferences.Instance}, Path=ShowHiddenSymbolsInDiffView}"
Expand All @@ -81,6 +83,7 @@
IndicatorForeground="{DynamicResource Brush.FG2}"
FontFamily="{DynamicResource Fonts.Monospace}"
FontSize="{Binding Source={x:Static vm:Preferences.Instance}, Path=EditorFontSize}"
TabWidth="{Binding Source={x:Static vm:Preferences.Instance}, Path=EditorTabWidth}"
UseSyntaxHighlighting="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseSyntaxHighlighting}"
WordWrap="False"
ShowHiddenSymbols="{Binding Source={x:Static vm:Preferences.Instance}, Path=ShowHiddenSymbolsInDiffView}"
Expand Down
22 changes: 18 additions & 4 deletions src/Views/TextDiffView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,15 @@ public bool ShowHiddenSymbols
set => SetValue(ShowHiddenSymbolsProperty, value);
}

public static readonly StyledProperty<int> TabWidthProperty =
AvaloniaProperty.Register<ThemedTextDiffPresenter, int>(nameof(TabWidth), 4);

public int TabWidth
{
get => GetValue(TabWidthProperty);
set => SetValue(TabWidthProperty, value);
}

public static readonly StyledProperty<bool> EnableChunkSelectionProperty =
AvaloniaProperty.Register<ThemedTextDiffPresenter, bool>(nameof(EnableChunkSelection));

Expand Down Expand Up @@ -519,12 +528,13 @@ public ThemedTextDiffPresenter(TextArea area, TextDocument doc) : base(area, doc
ShowLineNumbers = false;
BorderThickness = new Thickness(0);

Options.IndentationSize = TabWidth;
Options.EnableHyperlinks = false;
Options.EnableEmailHyperlinks = false;

_lineStyleTransformer = new LineStyleTransformer(this);

TextArea.TextView.Margin = new Thickness(2, 0);
TextArea.TextView.Options.EnableHyperlinks = false;
TextArea.TextView.Options.EnableEmailHyperlinks = false;

TextArea.TextView.BackgroundRenderers.Add(new LineBackgroundRenderer(this));
TextArea.TextView.LineTransformers.Add(_lineStyleTransformer);
}
Expand Down Expand Up @@ -734,10 +744,14 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
}
else if (change.Property == ShowHiddenSymbolsProperty)
{
var val = change.NewValue is true;
var val = ShowHiddenSymbols;
Options.ShowTabs = val;
Options.ShowSpaces = val;
}
else if (change.Property == TabWidthProperty)
{
Options.IndentationSize = TabWidth;
}
else if (change.Property == FileNameProperty)
{
Models.TextMateHelper.SetGrammarByFileName(_textMate, FileName);
Expand Down

0 comments on commit 2137ad9

Please sign in to comment.