Skip to content

Commit 8ac5896

Browse files
committed
refactor: text diff navigation
- Remove navigation by prev/next change of first/last visible block. Always use Block-Navigation - Remove auto-updating current actived block on text view scrolling - Auto update current active block when caret position changed - Clean up unused resources Signed-off-by: leo <[email protected]>
1 parent 2da8a1d commit 8ac5896

File tree

9 files changed

+92
-297
lines changed

9 files changed

+92
-297
lines changed

src/Resources/Icons.axaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
<StreamGeometry x:Key="Icons.Clone">M1280 704c0 141-115 256-256 256H288C129 960 0 831 0 672c0-126 80-232 192-272A327 327 0 01192 384c0-177 143-320 320-320 119 0 222 64 277 160C820 204 857 192 896 192c106 0 192 86 192 192 0 24-5 48-13 69C1192 477 1280 580 1280 704zm-493-128H656V352c0-18-14-32-32-32h-96c-18 0-32 14-32 32v224h-131c-29 0-43 34-23 55l211 211c12 12 33 12 45 0l211-211c20-20 6-55-23-55z</StreamGeometry>
2222
<StreamGeometry x:Key="Icons.Close">M523 398 918 3l113 113-396 396 397 397-113 113-397-397-397 397-113-113 397-397L14 116l113-113 396 396z</StreamGeometry>
2323
<StreamGeometry x:Key="Icons.Code">M853 102H171C133 102 102 133 102 171v683C102 891 133 922 171 922h683C891 922 922 891 922 853V171C922 133 891 102 853 102zM390 600l-48 48L205 512l137-137 48 48L301 512l88 88zM465 819l-66-18L559 205l66 18L465 819zm218-171L634 600 723 512l-88-88 48-48L819 512 683 649z</StreamGeometry>
24-
<StreamGeometry x:Key="Icons.CodeBlock">M684 736 340 736l0-53 344 1-0 53zM552 565l-213-2 0-53 212 2-0 53zM684 392 340 392l0-53 344 1-0 53zM301 825c-45 0-78-9-100-27-22-18-33-43-33-75v-116c0-22-4-37-12-45-7-9-20-13-40-13v-61c19 0 32-4 40-12 8-9 12-24 12-46v-116c0-32 11-57 33-75 22-18 56-27 100-27h24v61h-24a35 35 0 00-27 12 41 41 0 00-11 29v116c0 35-10 60-31 75a66 66 0 01-31 14c11 2 22 6 31 14 20 17 31 42 31 75v116c0 12 4 22 11 29 7 8 16 12 27 12h24v61h-24zM701 764h24c10 0 19-4 27-12a41 41 0 0011-29v-116c0-33 10-58 31-75 9-7 19-12 31-14a66 66 0 01-31-14c-20-15-31-40-31-75v-116a41 41 0 00-11-29 35 35 0 00-27-12h-24v-61h24c45 0 78 9 100 27 22 18 33 43 33 75v116c0 22 4 37 11 46 8 8 21 12 40 12v61c-19 0-33 4-40 13-7 8-11 23-11 45v116c0 32-11 57-33 75-22 18-55 27-100 27h-24v-61z</StreamGeometry>
2524
<StreamGeometry x:Key="Icons.ColorPicker">M128 854h768v86H128zM390 797c13 13 29 19 48 19s35-6 45-19l291-288c26-22 26-64 0-90L435 83l-61 61L426 192l-272 269c-22 22-22 64 0 90l237 246zm93-544 211 211-32 32H240l243-243zM707 694c0 48 38 86 86 86 48 0 86-38 86-86 0-22-10-45-26-61L794 576l-61 61c-13 13-26 35-26 58z</StreamGeometry>
2625
<StreamGeometry x:Key="Icons.Commit">M0 512M1024 512M512 0M512 1024M796 471A292 292 0 00512 256a293 293 0 00-284 215H0v144h228A293 293 0 00512 832a291 291 0 00284-217H1024V471h-228M512 688A146 146 0 01366 544A145 145 0 01512 400c80 0 146 63 146 144A146 146 0 01512 688</StreamGeometry>
2726
<StreamGeometry x:Key="Icons.CommitMessageGenerator">M796 561a5 5 0 014 7l-39 90a5 5 0 004 7h100a5 5 0 014 8l-178 247a5 5 0 01-9-4l32-148a5 5 0 00-5-6h-89a5 5 0 01-4-7l86-191a5 5 0 014-3h88zM731 122a73 73 0 0173 73v318a54 54 0 00-8-1H731V195H244v634h408l-16 73H244a73 73 0 01-73-73V195a73 73 0 0173-73h488zm-219 366v73h-195v-73h195zm146-146v73H317v-73h341z</StreamGeometry>

src/Resources/Locales/en_US.axaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,6 @@
328328
<x:String x:Key="Text.Diff.SwapCommits" xml:space="preserve">Swap</x:String>
329329
<x:String x:Key="Text.Diff.SyntaxHighlight" xml:space="preserve">Syntax Highlighting</x:String>
330330
<x:String x:Key="Text.Diff.ToggleWordWrap" xml:space="preserve">Line Word Wrap</x:String>
331-
<x:String x:Key="Text.Diff.UseBlockNavigation" xml:space="preserve">Enable Block-Navigation</x:String>
332331
<x:String x:Key="Text.Diff.UseMerger" xml:space="preserve">Open in Merge Tool</x:String>
333332
<x:String x:Key="Text.Diff.VisualLines.All" xml:space="preserve">Show All Lines</x:String>
334333
<x:String x:Key="Text.Diff.VisualLines.Decr" xml:space="preserve">Decrease Number of Visible Lines</x:String>
@@ -560,7 +559,6 @@
560559
<x:String x:Key="Text.Preferences.General.ShowChildren" xml:space="preserve">Show children in the commit details</x:String>
561560
<x:String x:Key="Text.Preferences.General.ShowTagsInGraph" xml:space="preserve">Show tags in commit graph</x:String>
562561
<x:String x:Key="Text.Preferences.General.SubjectGuideLength" xml:space="preserve">Subject Guide Length</x:String>
563-
<x:String x:Key="Text.Preferences.General.UpdateBlockNavigationOnScroll" xml:space="preserve">Update current block while scrolling in diff view (Block-Navigation)</x:String>
564562
<x:String x:Key="Text.Preferences.Git" xml:space="preserve">GIT</x:String>
565563
<x:String x:Key="Text.Preferences.Git.CRLF" xml:space="preserve">Enable Auto CRLF</x:String>
566564
<x:String x:Key="Text.Preferences.Git.DefaultCloneDir" xml:space="preserve">Default Clone Dir</x:String>

src/ViewModels/BlockNavigation.cs

Lines changed: 43 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,36 @@
11
using System.Collections.Generic;
2-
using Avalonia.Collections;
32
using CommunityToolkit.Mvvm.ComponentModel;
43

54
namespace SourceGit.ViewModels
65
{
76
public class BlockNavigation : ObservableObject
87
{
9-
public class Block
8+
public record Block(int Start, int End)
109
{
11-
public int Start { get; set; } = 0;
12-
public int End { get; set; } = 0;
13-
14-
public Block(int start, int end)
15-
{
16-
Start = start;
17-
End = end;
18-
}
19-
2010
public bool IsInRange(int line)
2111
{
2212
return line >= Start && line <= End;
2313
}
2414
}
2515

26-
public AvaloniaList<Block> Blocks
27-
{
28-
get;
29-
} = [];
30-
31-
public int Current
32-
{
33-
get => _current;
34-
private set => SetProperty(ref _current, value);
35-
}
36-
3716
public string Indicator
3817
{
3918
get
4019
{
41-
if (Blocks.Count == 0)
20+
if (_blocks.Count == 0)
4221
return "-/-";
4322

44-
if (_current >= 0 && _current < Blocks.Count)
45-
return $"{_current + 1}/{Blocks.Count}";
23+
if (_current >= 0 && _current < _blocks.Count)
24+
return $"{_current + 1}/{_blocks.Count}";
4625

47-
return $"-/{Blocks.Count}";
26+
return $"-/{_blocks.Count}";
4827
}
4928
}
5029

5130
public BlockNavigation(List<Models.TextDiffLine> lines)
5231
{
53-
Blocks.Clear();
54-
Current = -1;
32+
_blocks.Clear();
33+
_current = -1;
5534

5635
if (lines.Count == 0)
5736
return;
@@ -85,90 +64,89 @@ public BlockNavigation(List<Models.TextDiffLine> lines)
8564
if (!isNewBlock)
8665
blocks.Add(new Block(blockStartIdx, lines.Count - 1));
8766

88-
Blocks.AddRange(blocks);
67+
_blocks.AddRange(blocks);
8968
}
9069

9170
public Block GetCurrentBlock()
9271
{
93-
if (_current >= 0 && _current < Blocks.Count)
94-
return Blocks[_current];
72+
if (_current >= 0 && _current < _blocks.Count)
73+
return _blocks[_current];
9574

9675
return null;
9776
}
9877

9978
public Block GotoFirst()
10079
{
101-
if (Blocks.Count == 0)
80+
if (_blocks.Count == 0)
10281
return null;
10382

104-
Current = 0;
83+
_current = 0;
10584
OnPropertyChanged(nameof(Indicator));
106-
return Blocks[_current];
85+
return _blocks[_current];
10786
}
10887

10988
public Block GotoPrev()
11089
{
111-
if (Blocks.Count == 0)
90+
if (_blocks.Count == 0)
11291
return null;
11392

11493
if (_current == -1)
115-
Current = 0;
94+
_current = 0;
11695
else if (_current > 0)
117-
Current = _current - 1;
96+
_current--;
11897

11998
OnPropertyChanged(nameof(Indicator));
120-
return Blocks[_current];
99+
return _blocks[_current];
121100
}
122101

123102
public Block GotoNext()
124103
{
125-
if (Blocks.Count == 0)
104+
if (_blocks.Count == 0)
126105
return null;
127106

128-
if (_current < Blocks.Count - 1)
129-
Current = _current + 1;
107+
if (_current < _blocks.Count - 1)
108+
_current++;
130109

131110
OnPropertyChanged(nameof(Indicator));
132-
return Blocks[_current];
111+
return _blocks[_current];
133112
}
134113

135114
public Block GotoLast()
136115
{
137-
if (Blocks.Count == 0)
116+
if (_blocks.Count == 0)
138117
return null;
139118

140-
Current = Blocks.Count - 1;
119+
_current = _blocks.Count - 1;
141120
OnPropertyChanged(nameof(Indicator));
142-
return Blocks[_current];
121+
return _blocks[_current];
143122
}
144123

145-
public bool AutoUpdate(int start, int end)
124+
public void UpdateByCaretPosition(int caretLine)
146125
{
147-
if (_current >= 0 && _current < Blocks.Count)
126+
if (_current >= 0 && _current < _blocks.Count)
148127
{
149-
var block = Blocks[_current];
150-
if ((block.Start >= start && block.Start <= end) ||
151-
(block.End >= start && block.End <= end) ||
152-
(block.Start <= start && block.End >= end))
153-
return false;
128+
var block = _blocks[_current];
129+
if (block.IsInRange(caretLine))
130+
return;
154131
}
155132

156-
for (var i = 0; i < Blocks.Count; i++)
133+
for (var i = 0; i < _blocks.Count; i++)
157134
{
158-
var block = Blocks[i];
159-
if ((block.Start >= start && block.Start <= end) ||
160-
(block.End >= start && block.End <= end) ||
161-
(block.Start <= start && block.End >= end))
162-
{
163-
Current = i;
164-
OnPropertyChanged(nameof(Indicator));
165-
return true;
166-
}
167-
}
135+
var block = _blocks[i];
136+
if (block.End < caretLine)
137+
continue;
168138

169-
return false;
139+
if (block.Start > caretLine)
140+
_current = i - 1;
141+
else
142+
_current = i;
143+
144+
OnPropertyChanged(nameof(Indicator));
145+
break;
146+
}
170147
}
171148

172-
private int _current = -1;
149+
private int _current;
150+
private List<Block> _blocks = [];
173151
}
174152
}

src/ViewModels/DiffContext.cs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,6 @@ public bool ShowEntireFile
4646
}
4747
}
4848

49-
public bool UseBlockNavigation
50-
{
51-
get => Preferences.Instance.UseBlockNavigationInDiffView;
52-
set
53-
{
54-
if (value != Preferences.Instance.UseBlockNavigationInDiffView)
55-
{
56-
Preferences.Instance.UseBlockNavigationInDiffView = value;
57-
OnPropertyChanged();
58-
(Content as TextDiffContext)?.ResetBlockNavigation(value);
59-
}
60-
}
61-
}
62-
6349
public bool UseSideBySide
6450
{
6551
get => Preferences.Instance.UseSideBySideDiff;
@@ -156,8 +142,6 @@ public void CheckSettings()
156142
ctx = ctx.SwitchMode();
157143
Content = ctx;
158144
}
159-
160-
ctx.ResetBlockNavigation(UseBlockNavigation);
161145
}
162146
}
163147

@@ -297,11 +281,10 @@ private void LoadContent()
297281
{
298282
IsTextDiff = true;
299283

300-
var hasBlockNavigation = Preferences.Instance.UseBlockNavigationInDiffView;
301284
if (Preferences.Instance.UseSideBySideDiff)
302-
Content = new TwoSideTextDiff(cur, hasBlockNavigation, _content as TwoSideTextDiff);
285+
Content = new TwoSideTextDiff(cur, _content as TwoSideTextDiff);
303286
else
304-
Content = new CombinedTextDiff(cur, hasBlockNavigation, _content as CombinedTextDiff);
287+
Content = new CombinedTextDiff(cur, _content as CombinedTextDiff);
305288
}
306289
else
307290
{

src/ViewModels/Preferences.cs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -261,18 +261,6 @@ public bool UseFullTextDiff
261261
set => SetProperty(ref _useFullTextDiff, value);
262262
}
263263

264-
public bool UseBlockNavigationInDiffView
265-
{
266-
get => _useBlockNavigationInDiffView;
267-
set => SetProperty(ref _useBlockNavigationInDiffView, value);
268-
}
269-
270-
public bool UpdateBlockNavigationOnScroll
271-
{
272-
get => _updateBlockNavigationOnScroll;
273-
set => SetProperty(ref _updateBlockNavigationOnScroll, value);
274-
}
275-
276264
public int LFSImageActiveIdx
277265
{
278266
get => _lfsImageActiveIdx;
@@ -713,7 +701,7 @@ private bool RemoveInvalidRepositoriesRecursive(List<RepositoryNode> collection)
713701
private double _defaultFontSize = 13;
714702
private double _editorFontSize = 13;
715703
private int _editorTabWidth = 4;
716-
private LayoutInfo _layout = new LayoutInfo();
704+
private LayoutInfo _layout = new();
717705

718706
private int _maxHistoryCommits = 20000;
719707
private int _subjectGuideLength = 50;
@@ -734,8 +722,6 @@ private bool RemoveInvalidRepositoriesRecursive(List<RepositoryNode> collection)
734722
private bool _enableDiffViewWordWrap = false;
735723
private bool _showHiddenSymbolsInDiffView = false;
736724
private bool _useFullTextDiff = false;
737-
private bool _useBlockNavigationInDiffView = false;
738-
private bool _updateBlockNavigationOnScroll = true;
739725
private int _lfsImageActiveIdx = 0;
740726
private bool _enableCompactFoldersInChangesTree = false;
741727

0 commit comments

Comments
 (0)