Skip to content

Commit ac55bed

Browse files
committed
enhance: revision file viewer
- show current file path - add a toggle button to use global syntax highlighting setting (sometimes TextMateSharp will crash this app) Signed-off-by: leo <[email protected]>
1 parent f003f67 commit ac55bed

File tree

5 files changed

+71
-17
lines changed

5 files changed

+71
-17
lines changed

src/ViewModels/CommitDetail.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,16 @@ public string SearchChangeFilter
105105
}
106106
}
107107

108+
public string ViewRevisionFilePath
109+
{
110+
get => _viewRevisionFilePath;
111+
private set => SetProperty(ref _viewRevisionFilePath, value);
112+
}
113+
108114
public object ViewRevisionFileContent
109115
{
110116
get => _viewRevisionFileContent;
111-
set => SetProperty(ref _viewRevisionFileContent, value);
117+
private set => SetProperty(ref _viewRevisionFileContent, value);
112118
}
113119

114120
public string RevisionFileSearchFilter
@@ -189,10 +195,13 @@ public void ViewRevisionFile(Models.Object file)
189195
{
190196
if (file == null)
191197
{
198+
ViewRevisionFilePath = string.Empty;
192199
ViewRevisionFileContent = null;
193200
return;
194201
}
195202

203+
ViewRevisionFilePath = file.Path;
204+
196205
switch (file.Type)
197206
{
198207
case Models.ObjectType.Blob:
@@ -893,6 +902,7 @@ private Task ResetToParentRevision(Models.Change change)
893902
private List<Models.Change> _selectedChanges = null;
894903
private string _searchChangeFilter = string.Empty;
895904
private DiffContext _diffContext = null;
905+
private string _viewRevisionFilePath = string.Empty;
896906
private object _viewRevisionFileContent = null;
897907
private CancellationTokenSource _cancellationSource = null;
898908
private bool _requestingRevisionFiles = false;

src/Views/RevisionFileContentViewer.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<v:RevisionTextFileView FontFamily="{DynamicResource Fonts.Monospace}"
2424
FontSize="{Binding Source={x:Static vm:Preferences.Instance}, Path=EditorFontSize}"
2525
TabWidth="{Binding Source={x:Static vm:Preferences.Instance}, Path=EditorTabWidth}"
26+
UseSyntaxHighlighting="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseSyntaxHighlighting}"
2627
Background="{DynamicResource Brush.Contents}"/>
2728
</DataTemplate>
2829

src/Views/RevisionFileContentViewer.axaml.cs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ public int TabWidth
2424
set => SetValue(TabWidthProperty, value);
2525
}
2626

27+
public static readonly StyledProperty<bool> UseSyntaxHighlightingProperty =
28+
AvaloniaProperty.Register<RevisionTextFileView, bool>(nameof(UseSyntaxHighlighting));
29+
30+
public bool UseSyntaxHighlighting
31+
{
32+
get => GetValue(UseSyntaxHighlightingProperty);
33+
set => SetValue(UseSyntaxHighlightingProperty, value);
34+
}
35+
2736
protected override Type StyleKeyOverride => typeof(TextEditor);
2837

2938
public RevisionTextFileView() : base(new TextArea(), new TextDocument())
@@ -72,8 +81,8 @@ protected override void OnDataContextChanged(EventArgs e)
7281

7382
if (DataContext is Models.RevisionTextFile source)
7483
{
75-
UpdateTextMate();
7684
Text = source.Content;
85+
Models.TextMateHelper.SetGrammarByFileName(_textMate, source.FileName);
7786
}
7887
else
7988
{
@@ -86,9 +95,9 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
8695
base.OnPropertyChanged(change);
8796

8897
if (change.Property == TabWidthProperty)
89-
{
9098
Options.IndentationSize = TabWidth;
91-
}
99+
else if (change.Property == UseSyntaxHighlightingProperty)
100+
UpdateTextMate();
92101
}
93102

94103
private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs e)
@@ -124,11 +133,22 @@ private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs
124133

125134
private void UpdateTextMate()
126135
{
127-
if (_textMate == null)
128-
_textMate = Models.TextMateHelper.CreateForEditor(this);
136+
if (UseSyntaxHighlighting)
137+
{
138+
if (_textMate == null)
139+
_textMate = Models.TextMateHelper.CreateForEditor(this);
129140

130-
if (DataContext is Models.RevisionTextFile file)
131-
Models.TextMateHelper.SetGrammarByFileName(_textMate, file.FileName);
141+
if (DataContext is Models.RevisionTextFile file)
142+
Models.TextMateHelper.SetGrammarByFileName(_textMate, file.FileName);
143+
}
144+
else if (_textMate != null)
145+
{
146+
_textMate.Dispose();
147+
_textMate = null;
148+
GC.Collect();
149+
150+
TextArea.TextView.Redraw();
151+
}
132152
}
133153

134154
private TextMate.Installation _textMate = null;

src/Views/RevisionFileTreeView.axaml.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -313,16 +313,13 @@ private void OnTreeNodeDoubleTapped(object sender, TappedEventArgs e)
313313

314314
private void OnRowsSelectionChanged(object sender, SelectionChangedEventArgs _)
315315
{
316-
if (_disableSelectionChangingEvent)
316+
if (_disableSelectionChangingEvent || DataContext is not ViewModels.CommitDetail vm)
317317
return;
318318

319-
if (sender is ListBox { SelectedItem: ViewModels.RevisionFileTreeNode node } && DataContext is ViewModels.CommitDetail vm)
320-
{
321-
if (!node.IsFolder)
322-
vm.ViewRevisionFile(node.Backend);
323-
else
324-
vm.ViewRevisionFile(null);
325-
}
319+
if (sender is ListBox { SelectedItem: ViewModels.RevisionFileTreeNode { IsFolder: false } node })
320+
vm.ViewRevisionFile(node.Backend);
321+
else
322+
vm.ViewRevisionFile(null);
326323
}
327324

328325
private List<ViewModels.RevisionFileTreeNode> GetChildrenOfTreeNode(ViewModels.RevisionFileTreeNode node)

src/Views/RevisionFiles.axaml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,33 @@
113113
<!-- Right: File Content Viewer -->
114114
<Grid Grid.Column="2">
115115
<Border BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}">
116-
<v:RevisionFileContentViewer Content="{Binding ViewRevisionFileContent}"/>
116+
<Grid RowDefinitions="Auto,*">
117+
<Border Grid.Row="0"
118+
Height="26"
119+
BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="0,0,0,1"
120+
IsVisible="{Binding ViewRevisionFilePath, Converter={x:Static StringConverters.IsNotNullOrEmpty}}">
121+
<Grid ColumnDefinitions="Auto,*,Auto">
122+
<Path Grid.Column="0" Width="12" Height="12" Data="{StaticResource Icons.File}" Margin="8,0,0,0"/>
123+
<TextBlock Grid.Column="1"
124+
Classes="primary"
125+
Margin="4,0,0,0"
126+
Text="{Binding ViewRevisionFilePath}"
127+
FontSize="11"
128+
TextTrimming="CharacterEllipsis"/>
129+
130+
<ToggleButton Grid.Column="2"
131+
Classes="line_path"
132+
Width="28"
133+
Background="Transparent"
134+
IsChecked="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseSyntaxHighlighting, Mode=TwoWay}"
135+
ToolTip.Tip="{DynamicResource Text.Diff.SyntaxHighlight}">
136+
<Path Width="13" Height="13" Data="{StaticResource Icons.SyntaxHighlight}" Margin="0,3,0,0"/>
137+
</ToggleButton>
138+
</Grid>
139+
</Border>
140+
141+
<v:RevisionFileContentViewer Grid.Row="1" Content="{Binding ViewRevisionFileContent}"/>
142+
</Grid>
117143
</Border>
118144
</Grid>
119145
</Grid>

0 commit comments

Comments
 (0)