code_review: PR #946

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2025-02-06 13:07:58 +08:00
parent 9aba737d9e
commit 38a8490d16
No known key found for this signature in database
5 changed files with 316 additions and 291 deletions

View file

@ -24,7 +24,7 @@ namespace SourceGit.Commands
Context = repo; Context = repo;
var based = string.IsNullOrEmpty(start) ? "-R" : start; var based = string.IsNullOrEmpty(start) ? "-R" : start;
Args = $"diff --name-status {based} {end} -- {path}"; Args = $"diff --name-status {based} {end} -- \"{path}\"";
} }
public List<Models.Change> Result() public List<Models.Change> Result()

View file

@ -22,9 +22,6 @@ namespace SourceGit.Converters
public static readonly FuncValueConverter<int, bool> IsNotOne = public static readonly FuncValueConverter<int, bool> IsNotOne =
new FuncValueConverter<int, bool>(v => v != 1); new FuncValueConverter<int, bool>(v => v != 1);
public static readonly FuncValueConverter<int, bool> IsTwo =
new FuncValueConverter<int, bool>(v => v == 2);
public static readonly FuncValueConverter<int, bool> IsSubjectLengthBad = public static readonly FuncValueConverter<int, bool> IsSubjectLengthBad =
new FuncValueConverter<int, bool>(v => v > ViewModels.Preferences.Instance.SubjectGuideLength); new FuncValueConverter<int, bool>(v => v > ViewModels.Preferences.Instance.SubjectGuideLength);

View file

@ -4,6 +4,7 @@ using System.IO;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using Avalonia.Collections;
using Avalonia.Media.Imaging; using Avalonia.Media.Imaging;
using Avalonia.Threading; using Avalonia.Threading;
@ -17,129 +18,60 @@ namespace SourceGit.ViewModels
public object Content { get; set; } = content; public object Content { get; set; } = content;
} }
public partial class FileHistories : ObservableObject public partial class FileHistoriesSingleRevision : ObservableObject
{ {
public bool IsLoading public bool IsDiffMode
{ {
get => _isLoading; get => _isDiffMode;
private set => SetProperty(ref _isLoading, value);
}
public List<Models.Commit> Commits
{
get => _commits;
set => SetProperty(ref _commits, value);
}
public List<Models.Commit> SelectedCommits
{
get => _selectedCommits;
set set
{ {
if (SetProperty(ref _selectedCommits, value)) if (SetProperty(ref _isDiffMode, value))
RefreshViewContent(); RefreshViewContent();
} }
} }
public bool IsViewContent
{
get => _isViewContent;
set
{
if (SetProperty(ref _isViewContent, value))
RefreshViewContent();
}
}
public Models.Commit StartPoint
{
get => _startPoint;
set => SetProperty(ref _startPoint, value);
}
public Models.Commit EndPoint
{
get => _endPoint;
set => SetProperty(ref _endPoint, value);
}
public object ViewContent public object ViewContent
{ {
get => _viewContent; get => _viewContent;
private set => SetProperty(ref _viewContent, value); set => SetProperty(ref _viewContent, value);
} }
public FileHistories(Repository repo, string file, string commit = null) public FileHistoriesSingleRevision(Repository repo, string file, Models.Commit revision, object prev)
{ {
_repo = repo; _repo = repo;
_file = file; _file = file;
_revision = revision;
Task.Run(() => if (prev is FileHistoriesSingleRevision singleRevision)
{ {
var based = commit ?? string.Empty; _isDiffMode = singleRevision._isDiffMode;
var commits = new Commands.QueryCommits(_repo.FullPath, $"--date-order -n 10000 {based} -- \"{file}\"", false).Result(); _viewContent = singleRevision._viewContent;
Dispatcher.UIThread.Invoke(() => }
{ else
IsLoading = false; {
Commits = commits; _isDiffMode = true;
if (commits.Count > 0) _viewContent = null;
SelectedCommits = [commits[0]]; }
});
});
}
public void NavigateToCommit(Models.Commit commit) RefreshViewContent();
{
_repo.NavigateToCommit(commit.SHA);
} }
public void ResetToSelectedRevision() public void ResetToSelectedRevision()
{ {
if (_selectedCommits is not { Count: 1 }) new Commands.Checkout(_repo.FullPath).FileWithRevision(_file, $"{_revision.SHA}");
return;
new Commands.Checkout(_repo.FullPath).FileWithRevision(_file, $"{_selectedCommits[0].SHA}");
}
public void Swap()
{
if (_selectedCommits is not { Count: 2 })
return;
(_selectedCommits[0], _selectedCommits[1]) = (_selectedCommits[1], _selectedCommits[0]);
RefreshViewContent();
}
public Task<bool> SaveAsPatch(string saveTo)
{
return Task.Run(() =>
{
Commands.SaveChangesAsPatch.ProcessRevisionCompareChanges(_repo.FullPath, _changes, GetSHA(_startPoint), GetSHA(_endPoint), saveTo);
return true;
});
} }
private void RefreshViewContent() private void RefreshViewContent()
{ {
if (_selectedCommits == null || _selectedCommits.Count == 0) if (_isDiffMode)
{
StartPoint = null;
EndPoint = null;
ViewContent = 0;
return;
}
if (_isViewContent && _selectedCommits.Count == 1)
SetViewContentAsRevisionFile();
else
SetViewContentAsDiff(); SetViewContentAsDiff();
else
SetViewContentAsRevisionFile();
} }
private void SetViewContentAsRevisionFile() private void SetViewContentAsRevisionFile()
{ {
StartPoint = null; var objs = new Commands.QueryRevisionObjects(_repo.FullPath, _revision.SHA, _file).Result();
EndPoint = null;
var selectedCommit = _selectedCommits[0];
var objs = new Commands.QueryRevisionObjects(_repo.FullPath, selectedCommit.SHA, _file).Result();
if (objs.Count == 0) if (objs.Count == 0)
{ {
ViewContent = new FileHistoriesRevisionFile(_file, null); ViewContent = new FileHistoriesRevisionFile(_file, null);
@ -152,13 +84,13 @@ namespace SourceGit.ViewModels
case Models.ObjectType.Blob: case Models.ObjectType.Blob:
Task.Run(() => Task.Run(() =>
{ {
var isBinary = new Commands.IsBinary(_repo.FullPath, selectedCommit.SHA, _file).Result(); var isBinary = new Commands.IsBinary(_repo.FullPath, _revision.SHA, _file).Result();
if (isBinary) if (isBinary)
{ {
var ext = Path.GetExtension(_file); var ext = Path.GetExtension(_file);
if (IMG_EXTS.Contains(ext)) if (IMG_EXTS.Contains(ext))
{ {
var stream = Commands.QueryFileContent.Run(_repo.FullPath, selectedCommit.SHA, _file); var stream = Commands.QueryFileContent.Run(_repo.FullPath, _revision.SHA, _file);
var fileSize = stream.Length; var fileSize = stream.Length;
var bitmap = fileSize > 0 ? new Bitmap(stream) : null; var bitmap = fileSize > 0 ? new Bitmap(stream) : null;
var imageType = Path.GetExtension(_file).TrimStart('.').ToUpper(CultureInfo.CurrentCulture); var imageType = Path.GetExtension(_file).TrimStart('.').ToUpper(CultureInfo.CurrentCulture);
@ -167,7 +99,7 @@ namespace SourceGit.ViewModels
} }
else else
{ {
var size = new Commands.QueryFileSize(_repo.FullPath, _file, selectedCommit.SHA).Result(); var size = new Commands.QueryFileSize(_repo.FullPath, _file, _revision.SHA).Result();
var binaryFile = new Models.RevisionBinaryFile() { Size = size }; var binaryFile = new Models.RevisionBinaryFile() { Size = size };
Dispatcher.UIThread.Invoke(() => ViewContent = new FileHistoriesRevisionFile(_file, binaryFile)); Dispatcher.UIThread.Invoke(() => ViewContent = new FileHistoriesRevisionFile(_file, binaryFile));
} }
@ -175,7 +107,7 @@ namespace SourceGit.ViewModels
return; return;
} }
var contentStream = Commands.QueryFileContent.Run(_repo.FullPath, selectedCommit.SHA, _file); var contentStream = Commands.QueryFileContent.Run(_repo.FullPath, _revision.SHA, _file);
var content = new StreamReader(contentStream).ReadToEnd(); var content = new StreamReader(contentStream).ReadToEnd();
var matchLFS = REG_LFS_FORMAT().Match(content); var matchLFS = REG_LFS_FORMAT().Match(content);
if (matchLFS.Success) if (matchLFS.Success)
@ -218,35 +150,8 @@ namespace SourceGit.ViewModels
private void SetViewContentAsDiff() private void SetViewContentAsDiff()
{ {
if (_selectedCommits is { Count: 1 }) var option = new Models.DiffOption(_revision, _file);
{ ViewContent = new DiffContext(_repo.FullPath, option, _viewContent as DiffContext);
StartPoint = null;
EndPoint = null;
var option = new Models.DiffOption(_selectedCommits[0], _file);
ViewContent = new DiffContext(_repo.FullPath, option, _viewContent as DiffContext);
}
else if (_selectedCommits is { Count: 2 })
{
StartPoint = _selectedCommits[0];
EndPoint = _selectedCommits[1];
_changes = new Commands.CompareRevisions(_repo.FullPath, GetSHA(_selectedCommits[0]), GetSHA(_selectedCommits[1]), _file).Result();
if (_changes.Count == 0)
{
ViewContent = null;
return;
}
var option = new Models.DiffOption(GetSHA(_selectedCommits[0]), GetSHA(_selectedCommits[1]), _changes[0]);
ViewContent = new DiffContext(_repo.FullPath, option, _viewContent as DiffContext);
}
else
{
ViewContent = _selectedCommits.Count;
}
}
private string GetSHA(object obj)
{
return obj is Models.Commit commit ? commit.SHA : string.Empty;
} }
[GeneratedRegex(@"^version https://git-lfs.github.com/spec/v\d+\r?\noid sha256:([0-9a-f]+)\r?\nsize (\d+)[\r\n]*$")] [GeneratedRegex(@"^version https://git-lfs.github.com/spec/v\d+\r?\noid sha256:([0-9a-f]+)\r?\nsize (\d+)[\r\n]*$")]
@ -257,15 +162,152 @@ namespace SourceGit.ViewModels
".ico", ".bmp", ".jpg", ".png", ".jpeg", ".webp" ".ico", ".bmp", ".jpg", ".png", ".jpeg", ".webp"
}; };
private Repository _repo = null;
private string _file = null;
private Models.Commit _revision = null;
private bool _isDiffMode = true;
private object _viewContent = null;
}
public class FileHistoriesCompareRevisions : ObservableObject
{
public Models.Commit StartPoint
{
get => _startPoint;
set => SetProperty(ref _startPoint, value);
}
public Models.Commit EndPoint
{
get => _endPoint;
set => SetProperty(ref _endPoint, value);
}
public DiffContext ViewContent
{
get => _viewContent;
set => SetProperty(ref _viewContent, value);
}
public FileHistoriesCompareRevisions(Repository repo, string file, Models.Commit start, Models.Commit end)
{
_repo = repo;
_file = file;
_startPoint = start;
_endPoint = end;
RefreshViewContent();
}
public void Swap()
{
(StartPoint, EndPoint) = (_endPoint, _startPoint);
RefreshViewContent();
}
public Task<bool> SaveAsPatch(string saveTo)
{
return Task.Run(() =>
{
Commands.SaveChangesAsPatch.ProcessRevisionCompareChanges(_repo.FullPath, _changes, _startPoint.SHA, _endPoint.SHA, saveTo);
return true;
});
}
private void RefreshViewContent()
{
Task.Run(() =>
{
_changes = new Commands.CompareRevisions(_repo.FullPath, _startPoint.SHA, _endPoint.SHA, _file).Result();
if (_changes.Count == 0)
{
Dispatcher.UIThread.Invoke(() => ViewContent = null);
return;
}
var option = new Models.DiffOption(_startPoint.SHA, _endPoint.SHA, _changes[0]);
Dispatcher.UIThread.Invoke(() => ViewContent = new DiffContext(_repo.FullPath, option, _viewContent));
});
}
private Repository _repo = null;
private string _file = null;
private Models.Commit _startPoint = null;
private Models.Commit _endPoint = null;
private List<Models.Change> _changes = [];
private DiffContext _viewContent = null;
}
public class FileHistories : ObservableObject
{
public bool IsLoading
{
get => _isLoading;
private set => SetProperty(ref _isLoading, value);
}
public List<Models.Commit> Commits
{
get => _commits;
set => SetProperty(ref _commits, value);
}
public AvaloniaList<Models.Commit> SelectedCommits
{
get;
set;
} = [];
public object ViewContent
{
get => _viewContent;
private set => SetProperty(ref _viewContent, value);
}
public FileHistories(Repository repo, string file, string commit = null)
{
_repo = repo;
_file = file;
Task.Run(() =>
{
var based = commit ?? string.Empty;
var commits = new Commands.QueryCommits(_repo.FullPath, $"--date-order -n 10000 {based} -- \"{file}\"", false).Result();
Dispatcher.UIThread.Invoke(() =>
{
IsLoading = false;
Commits = commits;
});
});
SelectedCommits.CollectionChanged += (_, _) =>
{
switch (SelectedCommits.Count)
{
case 0:
ViewContent = new Models.Null();
break;
case 1:
ViewContent = new FileHistoriesSingleRevision(_repo, _file, SelectedCommits[0], _viewContent);
break;
case 2:
ViewContent = new FileHistoriesCompareRevisions(_repo, _file, SelectedCommits[0], SelectedCommits[1]);
break;
default:
ViewContent = SelectedCommits.Count;
break;
}
};
}
public void NavigateToCommit(Models.Commit commit)
{
_repo.NavigateToCommit(commit.SHA);
}
private readonly Repository _repo = null; private readonly Repository _repo = null;
private readonly string _file = null; private readonly string _file = null;
private bool _isLoading = true; private bool _isLoading = true;
private List<Models.Commit> _commits = null; private List<Models.Commit> _commits = null;
private List<Models.Commit> _selectedCommits = [];
private bool _isViewContent = false;
private object _viewContent = null; private object _viewContent = null;
private Models.Commit _startPoint = null;
private Models.Commit _endPoint = null;
private List<Models.Change> _changes = null;
} }
} }

View file

@ -13,30 +13,6 @@
Icon="/App.ico" Icon="/App.ico"
Title="{DynamicResource Text.FileHistory}" Title="{DynamicResource Text.FileHistory}"
MinWidth="1280" MinHeight="720"> MinWidth="1280" MinHeight="720">
<v:ChromelessWindow.DataTemplates>
<DataTemplate DataType="m:Null">
<Border HorizontalAlignment="Center" VerticalAlignment="Center" Background="{DynamicResource Brush.Accent}" CornerRadius="4">
<TextBlock Text="{DynamicResource Text.Worktree}" Classes="primary" Margin="4,2" Foreground="#FFDDDDDD"/>
</Border>
</DataTemplate>
<DataTemplate DataType="m:Commit">
<Grid RowDefinitions="Auto,*">
<Grid Grid.Row="0" ColumnDefinitions="Auto,*,Auto,Auto,Auto">
<v:Avatar Width="16" Height="16" VerticalAlignment="Center" IsHitTestVisible="False" User="{Binding Author}"/>
<TextBlock Grid.Column="1" Classes="primary" Text="{Binding Author.Name}" Margin="8,0,0,0"/>
<Border Grid.Column="2" Background="{DynamicResource Brush.Accent}" CornerRadius="4" IsVisible="{Binding IsCurrentHead}">
<TextBlock Text="HEAD" Classes="primary" Margin="4,0" Foreground="#FFDDDDDD"/>
</Border>
<TextBlock Grid.Column="3" Classes="primary" Text="{Binding SHA, Converter={x:Static c:StringConverters.ToShortSHA}}" Foreground="DarkOrange" Margin="8,0,0,0" TextDecorations="Underline" Cursor="Hand" PointerPressed="OnPressCommitSHA" />
<TextBlock Grid.Column="4" Classes="primary" Text="{Binding CommitterTimeStr}" Foreground="{DynamicResource Brush.FG2}" Margin="8,0,0,0"/>
</Grid>
<TextBlock Grid.Row="1" Classes="primary" Text="{Binding Subject}" VerticalAlignment="Bottom"/>
</Grid>
</DataTemplate>
</v:ChromelessWindow.DataTemplates>
<Grid> <Grid>
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/>
@ -80,8 +56,8 @@
Margin="8,4,4,8" Margin="8,4,4,8"
BorderBrush="{DynamicResource Brush.Border2}" BorderBrush="{DynamicResource Brush.Border2}"
ItemsSource="{Binding Commits}" ItemsSource="{Binding Commits}"
SelectedItems="{Binding SelectedCommits, Mode=TwoWay}"
SelectionMode="Multiple" SelectionMode="Multiple"
SelectionChanged="OnRowSelectionChanged"
ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Auto"> ScrollViewer.VerticalScrollBarVisibility="Auto">
<ListBox.Styles> <ListBox.Styles>
@ -131,102 +107,136 @@
BorderThickness="1,0,0,0" BorderThickness="1,0,0,0"
BorderBrush="{DynamicResource Brush.Border0}"/> BorderBrush="{DynamicResource Brush.Border0}"/>
<v:LoadingIcon Grid.Column="2" <ContentControl Grid.Column="2" Content="{Binding ViewContent}">
Width="48" Height="48" <ContentControl.DataTemplates>
<DataTemplate DataType="m:Null">
<Border/>
</DataTemplate>
<DataTemplate DataType="vm:FileHistoriesSingleRevision">
<Grid RowDefinitions="Auto,*,Auto">
<StackPanel Grid.Row="0" Margin="0,8" Height="28" HorizontalAlignment="Center" Orientation="Horizontal">
<RadioButton Classes="switch_button"
GroupName="SearchGroup"
IsChecked="{Binding IsDiffMode, Mode=OneWay}">
<TextBlock Margin="16,0" Text="{DynamicResource Text.FileHistory.FileChange}" FontWeight="Bold"/>
</RadioButton>
<RadioButton Classes="switch_button"
GroupName="SearchGroup"
IsChecked="{Binding !IsDiffMode, Mode=TwoWay}">
<TextBlock Margin="16,0" Text="{DynamicResource Text.FileHistory.FileContent}" FontWeight="Bold"/>
</RadioButton>
</StackPanel>
<ContentControl Grid.Row="1" Margin="4,4,8,8" Content="{Binding ViewContent}">
<ContentControl.DataTemplates>
<DataTemplate DataType="vm:DiffContext">
<v:DiffView/>
</DataTemplate>
<DataTemplate DataType="vm:FileHistoriesRevisionFile">
<Border BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}">
<Grid RowDefinitions="26,*">
<Border Grid.Row="0"
BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border2}"
Background="{DynamicResource Brush.Window}">
<Grid ColumnDefinitions="Auto,*">
<Path Grid.Column="0" Width="12" Height="12" Data="{StaticResource Icons.File}" Margin="8,0,0,0"/>
<TextBlock Grid.Column="1"
Classes="primary"
Margin="4,0,0,0"
Text="{Binding Path}"
FontSize="11"
TextTrimming="CharacterEllipsis"/>
</Grid>
</Border>
<v:RevisionFileContentViewer Grid.Row="1" Content="{Binding Content}"/>
</Grid>
</Border>
</DataTemplate>
</ContentControl.DataTemplates>
</ContentControl>
<Button Grid.Row="2"
Classes="flat primary"
Margin="0,0,0,8"
HorizontalAlignment="Center"
Content="{DynamicResource Text.ChangeCM.CheckoutThisRevision}"
Click="OnResetToSelectedRevision"/>
</Grid>
</DataTemplate>
<DataTemplate DataType="vm:FileHistoriesCompareRevisions">
<Grid RowDefinitions="Auto,*">
<Grid Grid.Row="0" Margin="4,6" ColumnDefinitions="*,32,*,Auto">
<Grid.DataTemplates>
<DataTemplate DataType="m:Commit">
<Grid RowDefinitions="Auto,*">
<Grid Grid.Row="0" ColumnDefinitions="Auto,*,Auto,Auto,Auto">
<v:Avatar Width="16" Height="16" VerticalAlignment="Center" IsHitTestVisible="False" User="{Binding Author}"/>
<TextBlock Grid.Column="1" Classes="primary" Text="{Binding Author.Name}" Margin="8,0,0,0"/>
<Border Grid.Column="2" Background="{DynamicResource Brush.Accent}" CornerRadius="4" IsVisible="{Binding IsCurrentHead}">
<TextBlock Text="HEAD" Classes="primary" Margin="4,0" Foreground="#FFDDDDDD"/>
</Border>
<TextBlock Grid.Column="3" Classes="primary" Text="{Binding SHA, Converter={x:Static c:StringConverters.ToShortSHA}}" Foreground="DarkOrange" Margin="8,0,0,0" TextDecorations="Underline" Cursor="Hand" PointerPressed="OnPressCommitSHA" />
<TextBlock Grid.Column="4" Classes="primary" Text="{Binding CommitterTimeStr}" Foreground="{DynamicResource Brush.FG2}" Margin="8,0,0,0"/>
</Grid>
<TextBlock Grid.Row="1" Classes="primary" Text="{Binding Subject}" VerticalAlignment="Bottom"/>
</Grid>
</DataTemplate>
</Grid.DataTemplates>
<Border Grid.Column="0" BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="1" Background="{DynamicResource Brush.Contents}" CornerRadius="4" Padding="4">
<ContentControl Content="{Binding StartPoint}"/>
</Border>
<Button Grid.Column="1" Classes="icon_button" Command="{Binding Swap}" HorizontalAlignment="Center" ToolTip.Tip="{DynamicResource Text.Diff.SwapCommits}">
<Path Width="16" Height="16" Data="{DynamicResource Icons.Compare}"/>
</Button>
<Border Grid.Column="2" BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="1" Background="{DynamicResource Brush.Contents}" CornerRadius="4" Padding="4">
<ContentControl Content="{Binding EndPoint}"/>
</Border>
<Button Grid.Column="3" Classes="icon_button" Width="32" Click="OnSaveAsPatch" ToolTip.Tip="{DynamicResource Text.Diff.SaveAsPatch}">
<Path Width="16" Height="16" Data="{DynamicResource Icons.Diff}"/>
</Button>
</Grid>
<ContentControl Grid.Row="1" Margin="4,4,8,8" Content="{Binding ViewContent}">
<ContentControl.DataTemplates>
<DataTemplate DataType="vm:DiffContext">
<v:DiffView/>
</DataTemplate>
</ContentControl.DataTemplates>
</ContentControl>
</Grid>
</DataTemplate>
<DataTemplate DataType="x:Int32">
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
<Path Width="128" Height="128"
Data="{StaticResource Icons.Detail}"
HorizontalAlignment="Center"
Fill="{DynamicResource Brush.FG2}"/>
<TextBlock HorizontalAlignment="Center"
Margin="0,16"
FontSize="24" FontWeight="Bold"
Foreground="{DynamicResource Brush.FG2}"
Text="{Binding Converter={x:Static c:StringConverters.FormatByResourceKey}, ConverterParameter='Histories.Selected'}"/>
</StackPanel>
</DataTemplate>
</ContentControl.DataTemplates>
</ContentControl>
<v:LoadingIcon Grid.Column="2"
Width="48" Height="48"
HorizontalAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center"
IsVisible="{Binding IsLoading}"/> IsVisible="{Binding IsLoading}"/>
<Grid Grid.Column="2" RowDefinitions="Auto,*,Auto" IsVisible="{Binding !IsLoading}">
<StackPanel Grid.Row="0" Margin="0,8" Height="28" HorizontalAlignment="Center" Orientation="Horizontal" IsVisible="{Binding SelectedCommits.Count, Converter={x:Static c:IntConverters.IsOne}}">
<RadioButton Classes="switch_button"
GroupName="SearchGroup"
IsChecked="{Binding !IsViewContent, Mode=OneWay}">
<TextBlock Margin="16,0" Text="{DynamicResource Text.FileHistory.FileChange}" FontWeight="Bold"/>
</RadioButton>
<RadioButton Classes="switch_button"
GroupName="SearchGroup"
IsChecked="{Binding IsViewContent, Mode=TwoWay}">
<TextBlock Margin="16,0" Text="{DynamicResource Text.FileHistory.FileContent}" FontWeight="Bold"/>
</RadioButton>
</StackPanel>
<!-- Compare Revision Info -->
<Grid Grid.Row="0" Margin="0,0,0,6" ColumnDefinitions="*,32,*,Auto" IsVisible="{Binding SelectedCommits.Count, Converter={x:Static c:IntConverters.IsTwo}}">
<!-- Base Revision -->
<Border Grid.Column="0" BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="1" Background="{DynamicResource Brush.Contents}" CornerRadius="4" Padding="4">
<ContentControl Content="{Binding StartPoint}"/>
</Border>
<!-- Swap Buttons -->
<Button Grid.Column="1" Classes="icon_button" Command="{Binding Swap}" HorizontalAlignment="Center" ToolTip.Tip="{DynamicResource Text.Diff.SwapCommits}">
<Path Width="16" Height="16" Data="{DynamicResource Icons.Compare}"/>
</Button>
<!-- Right Revision -->
<Border Grid.Column="2" BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="1" Background="{DynamicResource Brush.Contents}" CornerRadius="4" Padding="4">
<ContentControl Content="{Binding EndPoint}"/>
</Border>
<!-- Save As Patch Button -->
<Button Grid.Column="3" Classes="icon_button" Width="32" Click="OnSaveAsPatch" ToolTip.Tip="{DynamicResource Text.Diff.SaveAsPatch}">
<Path Width="16" Height="16" Data="{DynamicResource Icons.Diff}"/>
</Button>
</Grid>
<ContentControl Grid.Row="1" Margin="4,4,8,8" Content="{Binding ViewContent}">
<ContentControl.DataTemplates>
<DataTemplate DataType="vm:DiffContext">
<v:DiffView/>
</DataTemplate>
<DataTemplate DataType="vm:FileHistoriesRevisionFile">
<Border BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}">
<Grid RowDefinitions="26,*">
<Border Grid.Row="0"
BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border2}"
Background="{DynamicResource Brush.Window}">
<Grid ColumnDefinitions="Auto,*">
<Path Grid.Column="0" Width="12" Height="12" Data="{StaticResource Icons.File}" Margin="8,0,0,0"/>
<TextBlock Grid.Column="1"
Classes="primary"
Margin="4,0,0,0"
Text="{Binding Path}"
FontSize="11"
TextTrimming="CharacterEllipsis"/>
</Grid>
</Border>
<v:RevisionFileContentViewer Grid.Row="1" Content="{Binding Content}"/>
</Grid>
</Border>
</DataTemplate>
<DataTemplate DataType="x:Int32">
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
<Path Width="128" Height="128"
Data="{StaticResource Icons.Detail}"
HorizontalAlignment="Center"
Fill="{DynamicResource Brush.FG2}"/>
<TextBlock HorizontalAlignment="Center"
Margin="0,16"
FontSize="24" FontWeight="Bold"
Foreground="{DynamicResource Brush.FG2}"
Text="{Binding Converter={x:Static c:StringConverters.FormatByResourceKey}, ConverterParameter='Histories.Selected'}"/>
</StackPanel>
</DataTemplate>
</ContentControl.DataTemplates>
</ContentControl>
<Button Grid.Row="2"
Classes="flat primary"
Margin="0,0,0,8"
HorizontalAlignment="Center"
Content="{DynamicResource Text.ChangeCM.CheckoutThisRevision}"
Click="OnResetToSelectedRevision"
IsVisible="{Binding SelectedCommits.Count, Converter={x:Static c:IntConverters.IsOne}}"/>
</Grid>
</Grid> </Grid>
<Border Grid.Row="1" x:Name="NotifyDonePanel" Background="Transparent" IsVisible="False" PointerPressed="OnCloseNotifyPanel"> <Border Grid.Row="1" x:Name="NotifyDonePanel" Background="Transparent" IsVisible="False" PointerPressed="OnCloseNotifyPanel">

View file

@ -3,7 +3,6 @@ using Avalonia.Controls;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Avalonia.Platform.Storage; using Avalonia.Platform.Storage;
using SourceGit.Models;
namespace SourceGit.Views namespace SourceGit.Views
{ {
@ -25,11 +24,11 @@ namespace SourceGit.Views
e.Handled = true; e.Handled = true;
} }
private void OnResetToSelectedRevision(object _, RoutedEventArgs e) private void OnResetToSelectedRevision(object sender, RoutedEventArgs e)
{ {
if (DataContext is ViewModels.FileHistories vm) if (sender is Button { DataContext: ViewModels.FileHistoriesSingleRevision single })
{ {
vm.ResetToSelectedRevision(); single.ResetToSelectedRevision();
NotifyDonePanel.IsVisible = true; NotifyDonePanel.IsVisible = true;
} }
@ -42,45 +41,22 @@ namespace SourceGit.Views
e.Handled = true; e.Handled = true;
} }
private void OnRowSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (DataContext is ViewModels.FileHistories vm && sender is ListBox { SelectedItems: IList<object> commits })
{
var selectedCommits = new List<Models.Commit>();
foreach (var commit in commits)
{
if (commit is Models.Commit modelCommit)
{
selectedCommits.Add(modelCommit);
}
}
vm.SelectedCommits = selectedCommits;
}
e.Handled = true;
}
private async void OnSaveAsPatch(object sender, RoutedEventArgs e) private async void OnSaveAsPatch(object sender, RoutedEventArgs e)
{ {
var topLevel = TopLevel.GetTopLevel(this); if (sender is Button { DataContext: ViewModels.FileHistoriesCompareRevisions compare })
if (topLevel == null) {
return; var options = new FilePickerSaveOptions();
options.Title = App.Text("FileCM.SaveAsPatch");
options.DefaultExtension = ".patch";
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
var vm = DataContext as ViewModels.FileHistories; var storageFile = await this.StorageProvider.SaveFilePickerAsync(options);
if (vm == null) if (storageFile != null)
return; await compare.SaveAsPatch(storageFile.Path.LocalPath);
var options = new FilePickerSaveOptions(); NotifyDonePanel.IsVisible = true;
options.Title = App.Text("FileCM.SaveAsPatch"); e.Handled = true;
options.DefaultExtension = ".patch"; }
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
var storageFile = await topLevel.StorageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
await vm.SaveAsPatch(storageFile.Path.LocalPath);
NotifyDonePanel.IsVisible = true;
e.Handled = true;
} }
} }
} }