Skip to content

Commit 7c2053a

Browse files
committed
code_review: PR #2070
- Use `HotKey` binding for buttons - Better error handling Signed-off-by: leo <longshuang@msn.cn>
1 parent 54c733b commit 7c2053a

File tree

5 files changed

+33
-87
lines changed

5 files changed

+33
-87
lines changed

src/ViewModels/Conflict.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,7 @@ public async Task UseMineAsync()
121121
public async Task MergeAsync()
122122
{
123123
if (CanMerge)
124-
{
125-
var ctx = new MergeConflictEditor(_repo, _change.Path);
126-
await ctx.LoadAsync();
127-
await App.ShowDialog(ctx);
128-
}
124+
await App.ShowDialog(new MergeConflictEditor(_repo, _change.Path));
129125
}
130126

131127
public async Task MergeExternalAsync()

src/ViewModels/MergeConflictEditor.cs

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Threading.Tasks;
66

77
using Avalonia;
8-
using Avalonia.Threading;
98
using CommunityToolkit.Mvvm.ComponentModel;
109

1110
namespace SourceGit.ViewModels
@@ -17,12 +16,6 @@ public string FilePath
1716
get => _filePath;
1817
}
1918

20-
public bool IsLoading
21-
{
22-
get => _isLoading;
23-
private set => SetProperty(ref _isLoading, value);
24-
}
25-
2619
public string Error
2720
{
2821
get => _error;
@@ -78,43 +71,25 @@ public Models.ConflictSelectedChunk SelectedChunk
7871
public IReadOnlyList<Models.ConflictRegion> ConflictRegions => _conflictRegions;
7972
public bool HasUnresolvedConflicts => _unresolvedConflictCount > 0;
8073
public bool HasUnsavedChanges => _unresolvedConflictCount < _conflictRegions.Count;
81-
public bool CanSave => _unresolvedConflictCount == 0;
8274

8375
public MergeConflictEditor(Repository repo, string filePath)
8476
{
8577
_repo = repo;
8678
_filePath = filePath;
87-
}
8879

89-
public async Task LoadAsync()
90-
{
91-
IsLoading = true;
80+
var workingCopyPath = Path.Combine(_repo.FullPath, _filePath);
81+
var workingCopyContent = string.Empty;
82+
if (File.Exists(workingCopyPath))
83+
workingCopyContent = File.ReadAllText(workingCopyPath);
9284

93-
try
85+
if (workingCopyContent.IndexOf('\0', StringComparison.Ordinal) >= 0)
9486
{
95-
var workingCopyPath = Path.Combine(_repo.FullPath, _filePath);
96-
var workingCopyContent = string.Empty;
97-
if (File.Exists(workingCopyPath))
98-
workingCopyContent = await File.ReadAllTextAsync(workingCopyPath).ConfigureAwait(false);
99-
100-
if (workingCopyContent.IndexOf('\0', StringComparison.Ordinal) >= 0)
101-
throw new Exception("Binary file is not supported!!!");
102-
103-
Dispatcher.UIThread.Post(() =>
104-
{
105-
ParseOriginalContent(workingCopyContent);
106-
RefreshDisplayData();
107-
IsLoading = false;
108-
});
109-
}
110-
catch (Exception ex)
111-
{
112-
Dispatcher.UIThread.Post(() =>
113-
{
114-
IsLoading = false;
115-
Error = ex.Message;
116-
});
87+
_error = "Binary file is not supported.";
88+
return;
11789
}
90+
91+
ParseOriginalContent(workingCopyContent);
92+
RefreshDisplayData();
11893
}
11994

12095
public Models.ConflictLineState GetLineState(int line)
@@ -153,7 +128,7 @@ public async Task<bool> SaveAndStageAsync()
153128

154129
if (_unresolvedConflictCount > 0)
155130
{
156-
App.RaiseException(_repo.FullPath, "Cannot save: there are still unresolved conflicts.");
131+
Error = "Cannot save: there are still unresolved conflicts.";
157132
return false;
158133
}
159134

@@ -203,7 +178,7 @@ public async Task<bool> SaveAndStageAsync()
203178
{
204179
// Write merged content to file
205180
var fullPath = Path.Combine(_repo.FullPath, _filePath);
206-
await File.WriteAllTextAsync(fullPath, builder.ToString()).ConfigureAwait(false);
181+
await File.WriteAllTextAsync(fullPath, builder.ToString());
207182

208183
// Stage the file
209184
var pathSpecFile = Path.GetTempFileName();
@@ -216,11 +191,16 @@ public async Task<bool> SaveAndStageAsync()
216191
}
217192
catch (Exception ex)
218193
{
219-
App.RaiseException(_repo.FullPath, $"Failed to save and stage: {ex.Message}");
194+
Error = $"Failed to save and stage: {ex.Message}";
220195
return false;
221196
}
222197
}
223198

199+
public void ClearErrorMessage()
200+
{
201+
Error = string.Empty;
202+
}
203+
224204
private void ParseOriginalContent(string content)
225205
{
226206
_originalContent = content;
@@ -491,12 +471,10 @@ private void RefreshDisplayData()
491471
_unresolvedConflictCount = unresolved.Count;
492472
OnPropertyChanged(nameof(StatusText));
493473
OnPropertyChanged(nameof(HasUnresolvedConflicts));
494-
OnPropertyChanged(nameof(CanSave));
495474
}
496475

497476
private readonly Repository _repo;
498477
private readonly string _filePath;
499-
private bool _isLoading = false;
500478
private string _originalContent = string.Empty;
501479
private int _unresolvedConflictCount = 0;
502480
private int _diffMaxLineNumber = 0;

src/Views/MergeConflictEditor.axaml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
<StackPanel Grid.Column="0" Orientation="Horizontal" VerticalAlignment="Center">
4242
<Path Width="14" Height="14" Data="{StaticResource Icons.File}"/>
4343
<TextBlock Margin="8" Text="{Binding FilePath}" VerticalAlignment="Center"/>
44-
<v:LoadingIcon Width="14" Height="14" IsVisible="{Binding IsLoading}"/>
4544
</StackPanel>
4645

4746
<!-- Navigation and Actions -->
@@ -51,13 +50,15 @@
5150
Width="24"
5251
ToolTip.Tip="{DynamicResource Text.MergeConflictEditor.PrevConflict}"
5352
Click="OnGotoPrevConflict"
53+
HotKey="{OnPlatform Ctrl+Up, macOS=⌘+Up}"
5454
IsEnabled="{Binding HasUnresolvedConflicts, Mode=OneWay}">
5555
<Path Width="12" Height="12" Margin="0,6,0,0" Data="{StaticResource Icons.Up}"/>
5656
</Button>
5757
<Button Classes="icon_button"
5858
Width="24"
5959
ToolTip.Tip="{DynamicResource Text.MergeConflictEditor.NextConflict}"
6060
Click="OnGotoNextConflict"
61+
HotKey="{OnPlatform Ctrl+Down, macOS=⌘+Down}"
6162
IsEnabled="{Binding HasUnresolvedConflicts, Mode=OneWay}">
6263
<Path Width="12" Height="12" Margin="0,6,0,0" Data="{StaticResource Icons.Down}"/>
6364
</Button>
@@ -71,7 +72,7 @@
7172
Margin="0,2" Padding="6,3"
7273
Content="{DynamicResource Text.MergeConflictEditor.SaveAndStage}"
7374
Click="OnSaveAndStage"
74-
IsEnabled="{Binding CanSave, Mode=OneWay}"/>
75+
HotKey="{OnPlatform Ctrl+S, macOS=⌘+S}"/>
7576
</StackPanel>
7677
</Grid>
7778
</Border>
@@ -257,12 +258,18 @@
257258
Effect="drop-shadow(0 0 12 #60000000)"
258259
IsVisible="{Binding Error, Converter={x:Static StringConverters.IsNotNullOrEmpty}}">
259260
<StackPanel Orientation="Vertical">
260-
<Path Width="28" Height="28" Data="{StaticResource Icons.Error}"/>
261+
<Path Width="32" Height="32" Fill="Orange" Data="{StaticResource Icons.Error}"/>
261262
<TextBlock Text="{Binding Error, Mode=OneWay}"
262263
MaxWidth="500"
263-
Margin="0,12,0,0"
264-
FontSize="16"
264+
Margin="0,16"
265265
TextWrapping="Wrap"/>
266+
<Button Classes="flat primary"
267+
MinWidth="80"
268+
Margin="0"
269+
HorizontalAlignment="Center"
270+
Content="{DynamicResource Text.Sure}"
271+
Command="{Binding ClearErrorMessage}"
272+
HotKey="Escape"/>
266273
</StackPanel>
267274
</Border>
268275
</Grid>

src/Views/MergeConflictEditor.axaml.cs

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs
232232

233233
private void OnTextViewPointerChanged(object sender, PointerEventArgs e)
234234
{
235-
if (DataContext is not ViewModels.MergeConflictEditor { IsLoading: false } vm)
235+
if (DataContext is not ViewModels.MergeConflictEditor vm)
236236
return;
237237

238238
if (sender is not TextView view)
@@ -540,39 +540,6 @@ protected override void OnDataContextChanged(EventArgs e)
540540
vm.PropertyChanged += OnViewModelPropertyChanged;
541541
}
542542

543-
protected override void OnKeyDown(KeyEventArgs e)
544-
{
545-
base.OnKeyDown(e);
546-
547-
if (e.Handled)
548-
return;
549-
550-
var vm = DataContext as ViewModels.MergeConflictEditor;
551-
if (vm == null)
552-
return;
553-
554-
var modifier = OperatingSystem.IsMacOS() ? KeyModifiers.Meta : KeyModifiers.Control;
555-
556-
if (e.KeyModifiers == modifier)
557-
{
558-
if (e.Key == Key.S && vm.CanSave)
559-
{
560-
OnSaveAndStage(null, null);
561-
e.Handled = true;
562-
}
563-
else if (e.Key == Key.Up)
564-
{
565-
OnGotoPrevConflict(null, null);
566-
e.Handled = true;
567-
}
568-
else if (e.Key == Key.Down)
569-
{
570-
OnGotoNextConflict(null, null);
571-
e.Handled = true;
572-
}
573-
}
574-
}
575-
576543
protected override async void OnClosing(WindowClosingEventArgs e)
577544
{
578545
base.OnClosing(e);

src/Views/WorkingCopy.axaml.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,7 @@ private ContextMenu CreateContextMenuForUnstagedChanges(ViewModels.WorkingCopy v
343343
mergeBuiltin.Icon = App.CreateMenuIcon("Icons.Conflict");
344344
mergeBuiltin.Click += async (_, e) =>
345345
{
346-
var ctx = new ViewModels.MergeConflictEditor(repo, change.Path);
347-
await ctx.LoadAsync();
348-
await App.ShowDialog(ctx);
346+
await App.ShowDialog(new ViewModels.MergeConflictEditor(repo, change.Path));
349347
e.Handled = true;
350348
};
351349

0 commit comments

Comments
 (0)