Merge pull request #415 from gadfly3173/fix/file-ops

enhancement: Some file operation optimizations
This commit is contained in:
leo 2024-08-27 11:00:50 +08:00 committed by GitHub
commit 6cf9448313
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 74 additions and 22 deletions

View file

@ -390,12 +390,19 @@ namespace SourceGit.ViewModels
return; return;
var options = new FolderPickerOpenOptions() { AllowMultiple = false }; var options = new FolderPickerOpenOptions() { AllowMultiple = false };
try
{
var selected = await storageProvider.OpenFolderPickerAsync(options); var selected = await storageProvider.OpenFolderPickerAsync(options);
if (selected.Count == 1) if (selected.Count == 1)
{ {
var saveTo = Path.Combine(selected[0].Path.LocalPath, Path.GetFileName(file.Path)); var saveTo = Path.Combine(selected[0].Path.LocalPath, Path.GetFileName(file.Path));
Commands.SaveRevisionFile.Run(_repo.FullPath, _commit.SHA, file.Path, saveTo); Commands.SaveRevisionFile.Run(_repo.FullPath, _commit.SHA, file.Path, saveTo);
} }
}
catch (Exception e)
{
App.RaiseException(_repo.FullPath, $"Failed to save file: {e.Message}");
}
ev.Handled = true; ev.Handled = true;
}; };

View file

@ -454,6 +454,8 @@ namespace SourceGit.ViewModels
return; return;
var options = new FolderPickerOpenOptions() { AllowMultiple = false }; var options = new FolderPickerOpenOptions() { AllowMultiple = false };
try
{
var selected = await storageProvider.OpenFolderPickerAsync(options); var selected = await storageProvider.OpenFolderPickerAsync(options);
if (selected.Count == 1) if (selected.Count == 1)
{ {
@ -461,6 +463,11 @@ namespace SourceGit.ViewModels
if (succ) if (succ)
App.SendNotification(_repo.FullPath, App.Text("SaveAsPatchSuccess")); App.SendNotification(_repo.FullPath, App.Text("SaveAsPatchSuccess"));
} }
}
catch (Exception exception)
{
App.RaiseException(_repo.FullPath, $"Failed to save as patch: {exception.Message}");
}
e.Handled = true; e.Handled = true;
}; };

View file

@ -356,7 +356,14 @@ namespace SourceGit.ViewModels
SelectedView = null; // Do NOT modify. Used to remove exists widgets for GC.Collect SelectedView = null; // Do NOT modify. Used to remove exists widgets for GC.Collect
var settingsSerialized = JsonSerializer.Serialize(_settings, JsonCodeGen.Default.RepositorySettings); var settingsSerialized = JsonSerializer.Serialize(_settings, JsonCodeGen.Default.RepositorySettings);
try
{
File.WriteAllText(Path.Combine(_gitDir, "sourcegit.settings"), settingsSerialized); File.WriteAllText(Path.Combine(_gitDir, "sourcegit.settings"), settingsSerialized);
}
catch (DirectoryNotFoundException)
{
// Ignore
}
_settings = null; _settings = null;
_watcher?.Dispose(); _watcher?.Dispose();

View file

@ -1,3 +1,4 @@
using System;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Avalonia.Platform.Storage; using Avalonia.Platform.Storage;
@ -18,9 +19,16 @@ namespace SourceGit.Views
return; return;
var options = new FolderPickerOpenOptions() { AllowMultiple = false }; var options = new FolderPickerOpenOptions() { AllowMultiple = false };
try
{
var selected = await toplevel.StorageProvider.OpenFolderPickerAsync(options); var selected = await toplevel.StorageProvider.OpenFolderPickerAsync(options);
if (selected.Count == 1) if (selected.Count == 1)
TxtLocation.Text = selected[0].Path.LocalPath; TxtLocation.Text = selected[0].Path.LocalPath;
}
catch (Exception exception)
{
App.RaiseException(string.Empty, $"Failed to select location: {exception.Message}");
}
e.Handled = true; e.Handled = true;
} }

View file

@ -1,3 +1,4 @@
using System;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Avalonia.Platform.Storage; using Avalonia.Platform.Storage;
@ -18,9 +19,16 @@ namespace SourceGit.Views
if (toplevel == null) if (toplevel == null)
return; return;
try
{
var selected = await toplevel.StorageProvider.OpenFolderPickerAsync(options); var selected = await toplevel.StorageProvider.OpenFolderPickerAsync(options);
if (selected.Count == 1) if (selected.Count == 1)
TxtParentFolder.Text = selected[0].Path.LocalPath; TxtParentFolder.Text = selected[0].Path.LocalPath;
}
catch (Exception exception)
{
App.RaiseException(string.Empty, $"Failed to select parent folder: {exception.Message}");
}
e.Handled = true; e.Handled = true;
} }

View file

@ -186,12 +186,19 @@ namespace SourceGit.Views
private async void SelectDefaultCloneDir(object _1, RoutedEventArgs _2) private async void SelectDefaultCloneDir(object _1, RoutedEventArgs _2)
{ {
var options = new FolderPickerOpenOptions() { AllowMultiple = false }; var options = new FolderPickerOpenOptions() { AllowMultiple = false };
try
{
var selected = await StorageProvider.OpenFolderPickerAsync(options); var selected = await StorageProvider.OpenFolderPickerAsync(options);
if (selected.Count == 1) if (selected.Count == 1)
{ {
ViewModels.Preference.Instance.GitDefaultCloneDir = selected[0].Path.LocalPath; ViewModels.Preference.Instance.GitDefaultCloneDir = selected[0].Path.LocalPath;
} }
} }
catch (Exception e)
{
App.RaiseException(string.Empty, $"Failed to select default clone directory: {e.Message}");
}
}
private async void SelectGPGExecutable(object _1, RoutedEventArgs _2) private async void SelectGPGExecutable(object _1, RoutedEventArgs _2)
{ {

View file

@ -1,3 +1,4 @@
using System;
using System.IO; using System.IO;
using Avalonia.Controls; using Avalonia.Controls;
@ -30,9 +31,16 @@ namespace SourceGit.Views
options.SuggestedStartLocation = folder; options.SuggestedStartLocation = folder;
} }
try
{
var selected = await topLevel.StorageProvider.OpenFolderPickerAsync(options); var selected = await topLevel.StorageProvider.OpenFolderPickerAsync(options);
if (selected.Count == 1) if (selected.Count == 1)
OpenOrInitRepository(selected[0].Path.LocalPath); OpenOrInitRepository(selected[0].Path.LocalPath);
}
catch (Exception exception)
{
App.RaiseException(string.Empty, $"Failed to open repository: {exception.Message}");
}
e.Handled = true; e.Handled = true;
} }