Skip to content

Commit e25ab04

Browse files
committed
feature: only create a new FileHistoriesSingleRevision instance if it is necessary after selecting a single item in File History (#2192)
Signed-off-by: leo <longshuang@msn.cn>
1 parent 9d63078 commit e25ab04

File tree

3 files changed

+92
-31
lines changed

3 files changed

+92
-31
lines changed

src/ViewModels/FileHistories.cs

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Threading.Tasks;
5-
6-
using Avalonia.Collections;
75
using Avalonia.Threading;
8-
96
using CommunityToolkit.Mvvm.ComponentModel;
107

118
namespace SourceGit.ViewModels
@@ -17,15 +14,27 @@ public class FileHistoriesRevisionFile(string path, object content = null, bool
1714
public bool CanOpenWithDefaultEditor { get; set; } = canOpenWithDefaultEditor;
1815
}
1916

17+
public class FileHistoriesSingleRevisionViewMode
18+
{
19+
public bool IsDiff
20+
{
21+
get;
22+
set;
23+
} = true;
24+
}
25+
2026
public class FileHistoriesSingleRevision : ObservableObject
2127
{
2228
public bool IsDiffMode
2329
{
24-
get => _isDiffMode;
30+
get => _viewMode.IsDiff;
2531
set
2632
{
27-
if (SetProperty(ref _isDiffMode, value))
33+
if (_viewMode.IsDiff != value)
34+
{
35+
_viewMode.IsDiff = value;
2836
RefreshViewContent();
37+
}
2938
}
3039
}
3140

@@ -35,17 +44,24 @@ public object ViewContent
3544
set => SetProperty(ref _viewContent, value);
3645
}
3746

38-
public FileHistoriesSingleRevision(string repo, Models.FileVersion revision, bool prevIsDiffMode)
47+
public FileHistoriesSingleRevision(string repo, Models.FileVersion revision, FileHistoriesSingleRevisionViewMode viewMode)
3948
{
4049
_repo = repo;
4150
_file = revision.Path;
4251
_revision = revision;
43-
_isDiffMode = prevIsDiffMode;
52+
_viewMode = viewMode;
4453
_viewContent = null;
4554

4655
RefreshViewContent();
4756
}
4857

58+
public void SetRevision(Models.FileVersion revision)
59+
{
60+
_file = revision.Path;
61+
_revision = revision;
62+
RefreshViewContent();
63+
}
64+
4965
public async Task<bool> ResetToSelectedRevisionAsync()
5066
{
5167
return await new Commands.Checkout(_repo)
@@ -72,7 +88,7 @@ await Commands.SaveRevisionFile
7288

7389
private void RefreshViewContent()
7490
{
75-
if (_isDiffMode)
91+
if (_viewMode.IsDiff)
7692
{
7793
ViewContent = new DiffContext(_repo, new(_revision), _viewContent as DiffContext);
7894
return;
@@ -155,7 +171,7 @@ private async Task<object> GetRevisionFileContentAsync(Models.Object obj)
155171
private string _repo = null;
156172
private string _file = null;
157173
private Models.FileVersion _revision = null;
158-
private bool _isDiffMode = false;
174+
private FileHistoriesSingleRevisionViewMode _viewMode = null;
159175
private object _viewContent = null;
160176
}
161177

@@ -226,11 +242,15 @@ public List<Models.FileVersion> Revisions
226242
set => SetProperty(ref _revisions, value);
227243
}
228244

229-
public AvaloniaList<Models.FileVersion> SelectedRevisions
245+
public List<Models.FileVersion> SelectedRevisions
230246
{
231-
get;
232-
set;
233-
} = [];
247+
get => _selectedRevisions;
248+
set
249+
{
250+
if (SetProperty(ref _selectedRevisions, value))
251+
RefreshViewContent();
252+
}
253+
}
234254

235255
public object ViewContent
236256
{
@@ -257,23 +277,8 @@ public FileHistories(string repo, string file, string commit = null)
257277
{
258278
IsLoading = false;
259279
Revisions = revisions;
260-
if (revisions.Count > 0)
261-
SelectedRevisions.Add(revisions[0]);
262280
});
263281
});
264-
265-
SelectedRevisions.CollectionChanged += (_, _) =>
266-
{
267-
if (_viewContent is FileHistoriesSingleRevision singleRevision)
268-
_prevIsDiffMode = singleRevision.IsDiffMode;
269-
270-
ViewContent = SelectedRevisions.Count switch
271-
{
272-
1 => new FileHistoriesSingleRevision(_repo, SelectedRevisions[0], _prevIsDiffMode),
273-
2 => new FileHistoriesCompareRevisions(_repo, SelectedRevisions[0], SelectedRevisions[1]),
274-
_ => SelectedRevisions.Count,
275-
};
276-
};
277282
}
278283

279284
public void NavigateToCommit(Models.FileVersion revision)
@@ -303,10 +308,35 @@ public string GetCommitFullMessage(Models.FileVersion revision)
303308
return msg;
304309
}
305310

311+
private void RefreshViewContent()
312+
{
313+
var count = _selectedRevisions?.Count ?? 0;
314+
if (count == 0)
315+
{
316+
ViewContent = null;
317+
}
318+
else if (count == 1)
319+
{
320+
if (_viewContent is FileHistoriesSingleRevision single)
321+
single.SetRevision(_selectedRevisions[0]);
322+
else
323+
ViewContent = new FileHistoriesSingleRevision(_repo, _selectedRevisions[0], _viewMode);
324+
}
325+
else if (count == 2)
326+
{
327+
ViewContent = new FileHistoriesCompareRevisions(_repo, _selectedRevisions[0], _selectedRevisions[1]);
328+
}
329+
else
330+
{
331+
ViewContent = _selectedRevisions.Count;
332+
}
333+
}
334+
306335
private readonly string _repo = null;
307336
private bool _isLoading = true;
308-
private bool _prevIsDiffMode = true;
337+
private FileHistoriesSingleRevisionViewMode _viewMode = new();
309338
private List<Models.FileVersion> _revisions = null;
339+
private List<Models.FileVersion> _selectedRevisions = [];
310340
private Dictionary<string, string> _fullCommitMessages = new();
311341
private object _viewContent = null;
312342
}

src/Views/FileHistories.axaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@
5959
BorderThickness="1"
6060
Margin="8,4,4,8"
6161
BorderBrush="{DynamicResource Brush.Border2}"
62-
ItemsSource="{Binding Revisions}"
63-
SelectedItems="{Binding SelectedRevisions, Mode=TwoWay}"
62+
ItemsSource="{Binding Revisions, Mode=OneWay}"
6463
SelectionMode="Multiple"
64+
SelectionChanged="OnRevisionsSelectionChanged"
65+
PropertyChanged="OnRevisionsPropertyChanged"
6566
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
6667
ScrollViewer.VerticalScrollBarVisibility="Auto">
6768
<ListBox.Styles>

src/Views/FileHistories.axaml.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
2+
using System.Collections.Generic;
23

4+
using Avalonia;
35
using Avalonia.Controls;
46
using Avalonia.Input;
57
using Avalonia.Interactivity;
@@ -14,6 +16,34 @@ public FileHistories()
1416
InitializeComponent();
1517
}
1618

19+
private void OnRevisionsPropertyChanged(object sender, AvaloniaPropertyChangedEventArgs e)
20+
{
21+
if (e.Property == ListBox.ItemsSourceProperty &&
22+
sender is ListBox { Items: { Count: > 0 } } listBox)
23+
listBox.SelectedIndex = 0;
24+
}
25+
26+
private void OnRevisionsSelectionChanged(object sender, SelectionChangedEventArgs e)
27+
{
28+
if (sender is ListBox listBox && DataContext is ViewModels.FileHistories vm)
29+
{
30+
if (listBox.SelectedItems is { } selected)
31+
{
32+
var revs = new List<Models.FileVersion>();
33+
foreach (var item in listBox.SelectedItems)
34+
{
35+
if (item is Models.FileVersion ver)
36+
revs.Add(ver);
37+
}
38+
vm.SelectedRevisions = revs;
39+
}
40+
else
41+
{
42+
vm.SelectedRevisions = [];
43+
}
44+
}
45+
}
46+
1747
private void OnPressCommitSHA(object sender, PointerPressedEventArgs e)
1848
{
1949
if (sender is TextBlock { DataContext: Models.FileVersion ver } &&

0 commit comments

Comments
 (0)