optimize<DiffViewer>: supports to cancel last diff job

This commit is contained in:
leo 2021-04-30 09:25:52 +08:00
parent 5c4f36e4eb
commit ca0b4ba2f6
3 changed files with 45 additions and 38 deletions

View file

@ -104,6 +104,4 @@
</Setter.Value> </Setter.Value>
</Setter> </Setter>
</Style> </Style>
<PopupAnimation x:Key="{x:Static SystemParameters.MenuPopupAnimationKey}">None</PopupAnimation>
</ResourceDictionary> </ResourceDictionary>

View file

@ -75,7 +75,7 @@
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
</Grid> </Grid>
<Border x:Name="sizeChange" Grid.Row="1"> <Border x:Name="sizeChange" Grid.Row="1" Background="{StaticResource Brush.Window}">
<StackPanel Orientation="Vertical" VerticalAlignment="Center" TextElement.FontFamily="Consolas"> <StackPanel Orientation="Vertical" VerticalAlignment="Center" TextElement.FontFamily="Consolas">
<TextBlock <TextBlock
x:Name="txtSizeChangeTitle" x:Name="txtSizeChangeTitle"
@ -111,7 +111,7 @@
</StackPanel> </StackPanel>
</Border> </Border>
<Border x:Name="noChange" Grid.Row="1"> <Border x:Name="noChange" Grid.Row="1" Background="{StaticResource Brush.Window}">
<StackPanel Orientation="Vertical" VerticalAlignment="Center"> <StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Path Width="64" Height="64" Data="{StaticResource Icon.Check}" Fill="{StaticResource Brush.FG2}"/> <Path Width="64" Height="64" Data="{StaticResource Icon.Check}" Fill="{StaticResource Brush.FG2}"/>
<TextBlock <TextBlock
@ -123,7 +123,7 @@
</StackPanel> </StackPanel>
</Border> </Border>
<Border x:Name="mask" Grid.Row="0" Grid.RowSpan="2"> <Border x:Name="mask" Grid.Row="0" Grid.RowSpan="2" Background="{StaticResource Brush.Window}">
<StackPanel Orientation="Vertical" VerticalAlignment="Center"> <StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Path Width="64" Height="64" Data="{StaticResource Icon.Diff}" Fill="{StaticResource Brush.FG2}"/> <Path Width="64" Height="64" Data="{StaticResource Icon.Diff}" Fill="{StaticResource Brush.FG2}"/>
<TextBlock <TextBlock

View file

@ -53,6 +53,7 @@ namespace SourceGit.Views.Widgets {
} }
} }
private ulong seq = 0;
private string repo = null; private string repo = null;
private Option opt = null; private Option opt = null;
private List<Models.TextChanges.Line> cachedTextChanges = null; private List<Models.TextChanges.Line> cachedTextChanges = null;
@ -64,26 +65,26 @@ namespace SourceGit.Views.Widgets {
Reset(); Reset();
} }
public void Reload() {
if (repo == null || opt == null) {
Reset();
} else {
Diff(repo, opt);
}
}
public void Reset() { public void Reset() {
seq++;
mask.Visibility = Visibility.Visible; mask.Visibility = Visibility.Visible;
toolbar.Visibility = Visibility.Collapsed; toolbar.Visibility = Visibility.Collapsed;
noChange.Visibility = Visibility.Collapsed; noChange.Visibility = Visibility.Collapsed;
sizeChange.Visibility = Visibility.Collapsed; sizeChange.Visibility = Visibility.Collapsed;
ClearEditor();
ClearCache(); ClearCache();
} }
public void Reload() {
if (repo == null || opt == null) {
Reset();
return;
}
Diff(repo, opt);
}
public void Diff(string repo, Option opt) { public void Diff(string repo, Option opt) {
seq++;
mask.Visibility = Visibility.Collapsed; mask.Visibility = Visibility.Collapsed;
noChange.Visibility = Visibility.Collapsed; noChange.Visibility = Visibility.Collapsed;
sizeChange.Visibility = Visibility.Collapsed; sizeChange.Visibility = Visibility.Collapsed;
@ -97,6 +98,7 @@ namespace SourceGit.Views.Widgets {
this.repo = repo; this.repo = repo;
this.opt = opt; this.opt = opt;
var dummy = seq;
Task.Run(() => { Task.Run(() => {
var args = $"{opt.ExtraArgs} "; var args = $"{opt.ExtraArgs} ";
if (opt.RevisionRange.Length > 0) args += $"{opt.RevisionRange[0]} "; if (opt.RevisionRange.Length > 0) args += $"{opt.RevisionRange[0]} ";
@ -111,9 +113,9 @@ namespace SourceGit.Views.Widgets {
if (isLFSObject) { if (isLFSObject) {
var lc = new Commands.QueryLFSObjectChange(repo, args).Result(); var lc = new Commands.QueryLFSObjectChange(repo, args).Result();
if (lc.IsValid) { if (lc.IsValid) {
SetLFSChange(lc); SetLFSChange(lc, dummy);
} else { } else {
SetSame(); SetSame(dummy);
} }
return; return;
} }
@ -121,12 +123,12 @@ namespace SourceGit.Views.Widgets {
var rs = new Commands.Diff(repo, args).Result(); var rs = new Commands.Diff(repo, args).Result();
if (rs.IsBinary) { if (rs.IsBinary) {
var fsc = new Commands.QueryFileSizeChange(repo, opt.RevisionRange, opt.Path, opt.OrgPath).Result(); var fsc = new Commands.QueryFileSizeChange(repo, opt.RevisionRange, opt.Path, opt.OrgPath).Result();
SetSizeChange(fsc); SetSizeChange(fsc, dummy);
} else if (rs.Lines.Count > 0) { } else if (rs.Lines.Count > 0) {
cachedTextChanges = rs.Lines; cachedTextChanges = rs.Lines;
SetTextChange(); SetTextChange(dummy);
} else { } else {
SetSame(); SetSame(dummy);
} }
}); });
} }
@ -142,18 +144,20 @@ namespace SourceGit.Views.Widgets {
} }
} }
private void SetTextChange() { private void SetTextChange(ulong dummy) {
if (cachedTextChanges == null) return; if (cachedTextChanges == null) return;
if (Models.Preference.Instance.Window.UseCombinedDiff) { if (Models.Preference.Instance.Window.UseCombinedDiff) {
MakeCombinedViewer(); MakeCombinedViewer(dummy);
} else { } else {
MakeSideBySideViewer(); MakeSideBySideViewer(dummy);
} }
} }
private void SetSizeChange(Models.FileSizeChange fsc) { private void SetSizeChange(Models.FileSizeChange fsc, ulong dummy) {
Dispatcher.Invoke(() => { Dispatcher.Invoke(() => {
if (dummy != seq) return;
loading.Visibility = Visibility.Collapsed; loading.Visibility = Visibility.Collapsed;
mask.Visibility = Visibility.Collapsed; mask.Visibility = Visibility.Collapsed;
toolbarOptions.Visibility = Visibility.Collapsed; toolbarOptions.Visibility = Visibility.Collapsed;
@ -166,8 +170,10 @@ namespace SourceGit.Views.Widgets {
}); });
} }
private void SetLFSChange(Models.LFSChange lc) { private void SetLFSChange(Models.LFSChange lc, ulong dummy) {
Dispatcher.Invoke(() => { Dispatcher.Invoke(() => {
if (dummy != seq) return;
var oldSize = lc.Old == null ? 0 : lc.Old.Size; var oldSize = lc.Old == null ? 0 : lc.Old.Size;
var newSize = lc.New == null ? 0 : lc.New.Size; var newSize = lc.New == null ? 0 : lc.New.Size;
@ -183,8 +189,10 @@ namespace SourceGit.Views.Widgets {
}); });
} }
private void SetSame() { private void SetSame(ulong dummy) {
Dispatcher.Invoke(() => { Dispatcher.Invoke(() => {
if (dummy != seq) return;
loading.Visibility = Visibility.Collapsed; loading.Visibility = Visibility.Collapsed;
mask.Visibility = Visibility.Collapsed; mask.Visibility = Visibility.Collapsed;
toolbarOptions.Visibility = Visibility.Collapsed; toolbarOptions.Visibility = Visibility.Collapsed;
@ -192,7 +200,7 @@ namespace SourceGit.Views.Widgets {
}); });
} }
private void MakeCombinedViewer() { private void MakeCombinedViewer(ulong dummy) {
var fgCommon = FindResource("Brush.FG1") as Brush; var fgCommon = FindResource("Brush.FG1") as Brush;
var fgIndicator = FindResource("Brush.FG2") as Brush; var fgIndicator = FindResource("Brush.FG2") as Brush;
var lastOldLine = ""; var lastOldLine = "";
@ -216,6 +224,8 @@ namespace SourceGit.Views.Widgets {
} }
Dispatcher.Invoke(() => { Dispatcher.Invoke(() => {
if (dummy != seq) return;
loading.Visibility = Visibility.Collapsed; loading.Visibility = Visibility.Collapsed;
mask.Visibility = Visibility.Collapsed; mask.Visibility = Visibility.Collapsed;
toolbarOptions.Visibility = Visibility.Visible; toolbarOptions.Visibility = Visibility.Visible;
@ -244,11 +254,11 @@ namespace SourceGit.Views.Widgets {
editor.Columns[0].Width = new DataGridLength(lineNumberWidth, DataGridLengthUnitType.Pixel); editor.Columns[0].Width = new DataGridLength(lineNumberWidth, DataGridLengthUnitType.Pixel);
editor.Columns[1].Width = new DataGridLength(lineNumberWidth, DataGridLengthUnitType.Pixel); editor.Columns[1].Width = new DataGridLength(lineNumberWidth, DataGridLengthUnitType.Pixel);
editor.Columns[2].MinWidth = minWidth; editor.Columns[2].MinWidth = minWidth;
editor.ItemsSource = blocks; editor.SetBinding(DataGrid.ItemsSourceProperty, new Binding() { Source = blocks, IsAsync = true });
}); });
} }
private void MakeSideBySideViewer() { private void MakeSideBySideViewer(ulong dummy) {
var fgCommon = FindResource("Brush.FG1") as Brush; var fgCommon = FindResource("Brush.FG1") as Brush;
var fgIndicator = FindResource("Brush.FG2") as Brush; var fgIndicator = FindResource("Brush.FG2") as Brush;
var lastOldLine = ""; var lastOldLine = "";
@ -287,6 +297,8 @@ namespace SourceGit.Views.Widgets {
FillEmptyLines(oldSideBlocks, newSideBlocks); FillEmptyLines(oldSideBlocks, newSideBlocks);
Dispatcher.Invoke(() => { Dispatcher.Invoke(() => {
if (dummy != seq) return;
loading.Visibility = Visibility.Collapsed; loading.Visibility = Visibility.Collapsed;
mask.Visibility = Visibility.Collapsed; mask.Visibility = Visibility.Collapsed;
toolbarOptions.Visibility = Visibility.Visible; toolbarOptions.Visibility = Visibility.Visible;
@ -466,12 +478,6 @@ namespace SourceGit.Views.Widgets {
opt = null; opt = null;
cachedTextChanges = null; cachedTextChanges = null;
} }
private void ClearEditor() {
editors.Clear();
splitters.Clear();
textDiff.Children.Clear();
}
private T GetVisualChild<T>(DependencyObject parent) where T : Visual { private T GetVisualChild<T>(DependencyObject parent) where T : Visual {
T child = null; T child = null;
@ -497,8 +503,11 @@ namespace SourceGit.Views.Widgets {
#region EVENTS #region EVENTS
private void OnDiffViewModeChanged(object sender, RoutedEventArgs e) { private void OnDiffViewModeChanged(object sender, RoutedEventArgs e) {
if (editors.Count > 0) { if (editors.Count > 0) {
ClearEditor(); editors.Clear();
SetTextChange(); splitters.Clear();
textDiff.Children.Clear();
SetTextChange(seq);
} }
} }