refactor: re-design conflict panel

This commit is contained in:
leo 2024-12-11 11:33:20 +08:00
parent 36ecbcc4e0
commit dcaeaef48a
No known key found for this signature in database
3 changed files with 187 additions and 74 deletions

View file

@ -0,0 +1,78 @@
namespace SourceGit.ViewModels
{
public class Conflict
{
public object Theirs
{
get;
private set;
}
public object Mine
{
get;
private set;
}
public bool IsResolved
{
get;
private set;
}
public Conflict(Repository repo, WorkingCopy wc, Models.Change change)
{
_wc = wc;
_change = change;
IsResolved = new Commands.IsConflictResolved(repo.FullPath, change).ReadToEnd().IsSuccess;
var context = wc.InProgressContext;
if (context is CherryPickInProgress cherryPick)
{
Theirs = cherryPick.Head;
Mine = repo.CurrentBranch;
}
else if (context is RebaseInProgress rebase)
{
Theirs = repo.Branches.Find(x => x.IsLocal && x.Name == rebase.HeadName) ??
new Models.Branch()
{
IsLocal = true,
Name = rebase.HeadName,
FullName = $"refs/heads/{rebase.HeadName}"
};
Mine = rebase.Onto;
}
else if (context is RevertInProgress revert)
{
Theirs = revert.Head;
Mine = repo.CurrentBranch;
}
else if (context is MergeInProgress merge)
{
Theirs = merge.Source;
Mine = repo.CurrentBranch;
}
}
public void UseTheirs()
{
_wc.UseTheirs([_change]);
}
public void UseMine()
{
_wc.UseMine([_change]);
}
public void OpenExternalMergeTool()
{
_wc.UseExternalMergeTool(_change);
}
private WorkingCopy _wc = null;
private Models.Change _change = null;
}
}

View file

@ -11,26 +11,6 @@ using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
{
public class ConflictContext : ObservableObject
{
public bool IsResolved
{
get => _isResolved;
set => SetProperty(ref _isResolved, value);
}
public ConflictContext(string repo, Models.Change change)
{
Task.Run(() =>
{
var result = new Commands.IsConflictResolved(repo, change).ReadToEnd().IsSuccess;
Dispatcher.UIThread.Post(() => IsResolved = result);
});
}
private bool _isResolved = false;
}
public class WorkingCopy : ObservableObject
{
public bool IncludeUntracked
@ -440,6 +420,54 @@ namespace SourceGit.ViewModels
}
}
public async void UseTheirs(List<Models.Change> changes)
{
var files = new List<string>();
foreach (var change in changes)
{
if (change.IsConflit)
files.Add(change.Path);
}
_repo.SetWatcherEnabled(false);
var succ = await Task.Run(() => new Commands.Checkout(_repo.FullPath).UseTheirs(files));
if (succ)
{
await Task.Run(() => new Commands.Add(_repo.FullPath, changes).Exec());
}
_repo.MarkWorkingCopyDirtyManually();
_repo.SetWatcherEnabled(true);
}
public async void UseMine(List<Models.Change> changes)
{
var files = new List<string>();
foreach (var change in changes)
{
if (change.IsConflit)
files.Add(change.Path);
}
_repo.SetWatcherEnabled(false);
var succ = await Task.Run(() => new Commands.Checkout(_repo.FullPath).UseMine(files));
if (succ)
{
await Task.Run(() => new Commands.Add(_repo.FullPath, changes).Exec());
}
_repo.MarkWorkingCopyDirtyManually();
_repo.SetWatcherEnabled(true);
}
public async void UseExternalMergeTool(Models.Change change)
{
var toolType = Preference.Instance.ExternalMergeToolType;
var toolPath = Preference.Instance.ExternalMergeToolPath;
_repo.SetWatcherEnabled(false);
await Task.Run(() => Commands.MergeTool.OpenForMerge(_repo.FullPath, toolType, toolPath, change.Path));
_repo.SetWatcherEnabled(true);
}
public void ContinueMerge()
{
if (_inProgressContext != null)
@ -1438,59 +1466,11 @@ namespace SourceGit.ViewModels
if (change == null)
DetailContext = null;
else if (change.IsConflit && isUnstaged)
DetailContext = new ConflictContext(_repo.FullPath, change);
DetailContext = new Conflict(_repo, this, change);
else
DetailContext = new DiffContext(_repo.FullPath, new Models.DiffOption(change, isUnstaged), _detailContext as DiffContext);
}
private async void UseTheirs(List<Models.Change> changes)
{
var files = new List<string>();
foreach (var change in changes)
{
if (change.IsConflit)
files.Add(change.Path);
}
_repo.SetWatcherEnabled(false);
var succ = await Task.Run(() => new Commands.Checkout(_repo.FullPath).UseTheirs(files));
if (succ)
{
await Task.Run(() => new Commands.Add(_repo.FullPath, changes).Exec());
}
_repo.MarkWorkingCopyDirtyManually();
_repo.SetWatcherEnabled(true);
}
private async void UseMine(List<Models.Change> changes)
{
var files = new List<string>();
foreach (var change in changes)
{
if (change.IsConflit)
files.Add(change.Path);
}
_repo.SetWatcherEnabled(false);
var succ = await Task.Run(() => new Commands.Checkout(_repo.FullPath).UseMine(files));
if (succ)
{
await Task.Run(() => new Commands.Add(_repo.FullPath, changes).Exec());
}
_repo.MarkWorkingCopyDirtyManually();
_repo.SetWatcherEnabled(true);
}
private async void UseExternalMergeTool(Models.Change change)
{
var toolType = Preference.Instance.ExternalMergeToolType;
var toolPath = Preference.Instance.ExternalMergeToolPath;
_repo.SetWatcherEnabled(false);
await Task.Run(() => Commands.MergeTool.OpenForMerge(_repo.FullPath, toolType, toolPath, change.Path));
_repo.SetWatcherEnabled(true);
}
private void DoCommit(bool autoStage, bool autoPush, bool allowEmpty)
{
if (!PopupHost.CanCreatePopup())

View file

@ -2,6 +2,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:m="using:SourceGit.Models"
xmlns:vm="using:SourceGit.ViewModels"
xmlns:v="using:SourceGit.Views"
xmlns:c="using:SourceGit.Converters"
@ -169,13 +170,67 @@
<ContentControl Content="{Binding DetailContext}">
<ContentControl.DataTemplates>
<DataTemplate DataType="vm:ConflictContext">
<DataTemplate DataType="vm:Conflict">
<Border Background="{DynamicResource Brush.Window}" BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid VerticalAlignment="Center">
<StackPanel Orientation="Vertical" IsVisible="{Binding !IsResolved}">
<Path Width="64" Height="64" Data="{StaticResource Icons.Conflict}" Fill="{DynamicResource Brush.FG2}"/>
<TextBlock Margin="0,16,0,8" FontSize="20" FontWeight="Bold" Text="{DynamicResource Text.WorkingCopy.Conflicts}" Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Center"/>
<TextBlock Text="{DynamicResource Text.WorkingCopy.ResolveTip}" Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Center"/>
<StackPanel.DataTemplates>
<DataTemplate DataType="m:Branch">
<StackPanel Orientation="Horizontal">
<Path Width="12" Height="12" Data="{StaticResource Icons.Branch}"/>
<TextBlock Margin="4,0,0,0" Text="{Binding FriendlyName}"/>
<TextBlock Margin="4,0,0,0" Text="{Binding Head, Converter={x:Static c:StringConverters.ToShortSHA}}" Foreground="DarkOrange"/>
</StackPanel>
</DataTemplate>
<DataTemplate DataType="m:Commit">
<StackPanel Orientation="Horizontal">
<Path Width="12" Height="12" Margin="0,6,0,0" Data="{StaticResource Icons.Commit}"/>
<v:CommitRefsPresenter Margin="8,0,0,0"
TagBackground="{DynamicResource Brush.DecoratorTag}"
Foreground="{DynamicResource Brush.FG1}"
FontFamily="{DynamicResource Fonts.Primary}"
FontSize="11"
VerticalAlignment="Center"
UseGraphColor="False"/>
<TextBlock Margin="4,0,0,0" Text="{Binding SHA, Converter={x:Static c:StringConverters.ToShortSHA}}" Foreground="DarkOrange"/>
<TextBlock Margin="4,0,0,0" Text="{Binding Subject}"/>
</StackPanel>
</DataTemplate>
<DataTemplate DataType="m:Tag">
<StackPanel Orientation="Horizontal">
<Path Width="12" Height="12" Data="{StaticResource Icons.Tag}"/>
<TextBlock Margin="4,0,0,0" Text="{Binding Name}"/>
<TextBlock Margin="4,0,0,0" Text="{Binding SHA, Converter={x:Static c:StringConverters.ToShortSHA}}" Foreground="DarkOrange"/>
</StackPanel>
</DataTemplate>
</StackPanel.DataTemplates>
<Path Width="64" Height="64" Data="{StaticResource Icons.Conflict}" Fill="{DynamicResource Brush.FG2}" HorizontalAlignment="Center"/>
<TextBlock Margin="0,16" FontSize="20" FontWeight="Bold" Text="{DynamicResource Text.WorkingCopy.Conflicts}" Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Center"/>
<Border Margin="16,0" Padding="8" CornerRadius="4" BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}">
<Border.IsVisible>
<MultiBinding Converter="{x:Static BoolConverters.And}">
<Binding Path="Theirs" Converter="{x:Static ObjectConverters.IsNotNull}"/>
<Binding Path="Mine" Converter="{x:Static ObjectConverters.IsNotNull}"/>
</MultiBinding>
</Border.IsVisible>
<Grid Margin="8,0,0,0" RowDefinitions="32,32" ColumnDefinitions="Auto,*">
<TextBlock Grid.Row="0" Grid.Column="0" Classes="info_label" Text="THEIRS"/>
<ContentControl Grid.Row="0" Grid.Column="1" Margin="16,0,0,0" Content="{Binding Theirs}"/>
<TextBlock Grid.Row="1" Grid.Column="0" Classes="info_label" Text="MINE"/>
<ContentControl Grid.Row="1" Grid.Column="1" Margin="16,0,0,0" Content="{Binding Mine}"/>
</Grid>
</Border>
<StackPanel Margin="0,8,0,0" Orientation="Horizontal" HorizontalAlignment="Center">
<Button Classes="flat" Content="USE THEIRS" Command="{Binding UseTheirs}"/>
<Button Classes="flat" Margin="8,0,0,0" Content="USE MINE" Command="{Binding UseMine}"/>
<Button Classes="flat" Margin="8,0,0,0" Content="OPEN EXTERNAL MERGETOOL" Command="{Binding OpenExternalMergeTool}"/>
</StackPanel>
</StackPanel>
<StackPanel Orientation="Vertical" IsVisible="{Binding IsResolved}">