code_review: PR #480

* remove `string.IsNullOrEmpty(OS.ShellOrTerminal)` checking from detail backend since it has been checked by `Native.OS.OpenTerminal`
* remove `ViewModels.Preference.IsTerminalConfigured` because it is only used by itself (Unlike `IsGitConfigured`).
* add `Native.OS.TestShellOrTerminal` and use it instead of setting `Preference.ShellOrTerminal` directly, because we have binded a callback that will save preference into file when its property changed. It may cause lag in some enviroments if we saving preference into file too frequently (see PR #436).
* run `dotnet format`
This commit is contained in:
leo 2024-09-17 22:09:51 +08:00
parent 65c2087f6b
commit bc633e4085
No known key found for this signature in database
5 changed files with 44 additions and 61 deletions

View file

@ -27,9 +27,7 @@ namespace SourceGit.Native
public string FindTerminal(Models.ShellOrTerminal shell) public string FindTerminal(Models.ShellOrTerminal shell)
{ {
if (string.IsNullOrEmpty(shell.Exec)) if (string.IsNullOrEmpty(shell.Exec))
{
return string.Empty; return string.Empty;
}
var pathVariable = Environment.GetEnvironmentVariable("PATH") ?? string.Empty; var pathVariable = Environment.GetEnvironmentVariable("PATH") ?? string.Empty;
var pathes = pathVariable.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries); var pathes = pathVariable.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries);
@ -76,23 +74,18 @@ namespace SourceGit.Native
public void OpenTerminal(string workdir) public void OpenTerminal(string workdir)
{ {
if (string.IsNullOrEmpty(OS.ShellOrTerminal))
{
App.RaiseException(workdir, $"Terminal is not specified! Please confirm that the correct shell/terminal has been configured.");
return;
}
var startInfo = new ProcessStartInfo(); var startInfo = new ProcessStartInfo();
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
startInfo.WorkingDirectory = string.IsNullOrEmpty(workdir) ? home : workdir; startInfo.WorkingDirectory = string.IsNullOrEmpty(workdir) ? home : workdir;
startInfo.FileName = OS.ShellOrTerminal; startInfo.FileName = OS.ShellOrTerminal;
try try
{ {
Process.Start(startInfo); Process.Start(startInfo);
} }
catch (Exception e) catch (Exception e)
{ {
App.RaiseException(workdir, e.Message); App.RaiseException(workdir, $"Failed to start '{OS.ShellOrTerminal}'. Reason: {e.Message}");
} }
} }

View file

@ -74,12 +74,17 @@ namespace SourceGit.Native
return _backend.FindGitExecutable(); return _backend.FindGitExecutable();
} }
public static void SetShellOrTerminal(Models.ShellOrTerminal shellOrTerminal) public static bool TestShellOrTerminal(Models.ShellOrTerminal shell)
{ {
if (shellOrTerminal == null) return !string.IsNullOrEmpty(_backend.FindTerminal(shell));
}
public static void SetShellOrTerminal(Models.ShellOrTerminal shell)
{
if (shell == null)
ShellOrTerminal = string.Empty; ShellOrTerminal = string.Empty;
else else
ShellOrTerminal = _backend.FindTerminal(shellOrTerminal); ShellOrTerminal = _backend.FindTerminal(shell);
} }
public static void OpenInFileManager(string path, bool select = false) public static void OpenInFileManager(string path, bool select = false)

View file

@ -158,7 +158,7 @@ namespace SourceGit.Native
public void OpenTerminal(string workdir) public void OpenTerminal(string workdir)
{ {
if (string.IsNullOrEmpty(OS.ShellOrTerminal) || !File.Exists(OS.ShellOrTerminal)) if (!File.Exists(OS.ShellOrTerminal))
{ {
App.RaiseException(workdir, $"Terminal is not specified! Please confirm that the correct shell/terminal has been configured."); App.RaiseException(workdir, $"Terminal is not specified! Please confirm that the correct shell/terminal has been configured.");
return; return;

View file

@ -18,44 +18,15 @@ namespace SourceGit.ViewModels
if (_instance == null) if (_instance == null)
{ {
_isLoading = true; _isLoading = true;
_instance = Load();
var path = Path.Combine(Native.OS.DataDir, "preference.json");
if (!File.Exists(path))
{
_instance = new Preference();
}
else
{
try
{
_instance = JsonSerializer.Deserialize(File.ReadAllText(path), JsonCodeGen.Default.Preference);
}
catch
{
_instance = new Preference();
}
}
_isLoading = false; _isLoading = false;
} }
if (!_instance.IsGitConfigured()) if (!_instance.IsGitConfigured())
_instance.GitInstallPath = Native.OS.FindGitExecutable(); _instance.GitInstallPath = Native.OS.FindGitExecutable();
if (!_instance.IsTerminalConfigured()) if (_instance._shellOrTerminal == -1)
{ _instance.AutoSelectShellOrTerminal();
for (var i = 0; i < Models.ShellOrTerminal.Supported.Count; i++)
{
_instance.ShellOrTerminal = i;
if (string.IsNullOrEmpty(_instance.ShellOrTerminalPath))
{
_instance.ShellOrTerminal = -1;
}
else
{
break;
}
}
}
if (_instance.Workspaces.Count == 0) if (_instance.Workspaces.Count == 0)
_instance.Workspaces.Add(new Workspace() { Name = "Default", Color = 4278221015 }); _instance.Workspaces.Add(new Workspace() { Name = "Default", Color = 4278221015 });
@ -396,11 +367,6 @@ namespace SourceGit.ViewModels
return !string.IsNullOrEmpty(path) && File.Exists(path); return !string.IsNullOrEmpty(path) && File.Exists(path);
} }
public bool IsTerminalConfigured()
{
return ShellOrTerminal != -1;
}
public bool ShouldCheck4UpdateOnStartup() public bool ShouldCheck4UpdateOnStartup()
{ {
if (!_check4UpdatesOnStartup) if (!_check4UpdatesOnStartup)
@ -519,6 +485,35 @@ namespace SourceGit.ViewModels
File.WriteAllText(file, data); File.WriteAllText(file, data);
} }
private static Preference Load()
{
var path = Path.Combine(Native.OS.DataDir, "preference.json");
if (!File.Exists(path))
return new Preference();
try
{
return JsonSerializer.Deserialize(File.ReadAllText(path), JsonCodeGen.Default.Preference);
}
catch
{
return new Preference();
}
}
private void AutoSelectShellOrTerminal()
{
for (int i = 0; i < Models.ShellOrTerminal.Supported.Count; i++)
{
var shell = Models.ShellOrTerminal.Supported[i];
if (Native.OS.TestShellOrTerminal(shell))
{
ShellOrTerminal = i;
break;
}
}
}
private RepositoryNode FindNodeRecursive(string id, List<RepositoryNode> collection) private RepositoryNode FindNodeRecursive(string id, List<RepositoryNode> collection)
{ {
foreach (var node in collection) foreach (var node in collection)
@ -557,7 +552,7 @@ namespace SourceGit.ViewModels
return true; return true;
} }
foreach (RepositoryNode one in collection) foreach (var one in collection)
{ {
if (RemoveNodeRecursive(node, one.SubNodes)) if (RemoveNodeRecursive(node, one.SubNodes))
return true; return true;

View file

@ -1,17 +1,7 @@
using System;
using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Media;
using Avalonia.Media.Imaging;
namespace SourceGit.Views namespace SourceGit.Views
{ {
public partial class ImageDiffView : UserControl public partial class ImageDiffView : UserControl
{ {
public ImageDiffView() public ImageDiffView()