refactor: load external tool's icon on startup

This commit is contained in:
leo 2024-05-17 12:01:29 +08:00
parent 0da067caa3
commit a976f007f5
3 changed files with 25 additions and 43 deletions

View file

@ -266,6 +266,7 @@ namespace SourceGit
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{ {
BindingPlugins.DataValidators.RemoveAt(0); BindingPlugins.DataValidators.RemoveAt(0);
Native.OS.SetupEnternalTools();
var launcher = new Views.Launcher(); var launcher = new Views.Launcher();
_notificationReceiver = launcher; _notificationReceiver = launcher;

View file

@ -12,34 +12,23 @@ namespace SourceGit.Models
{ {
public class ExternalTool public class ExternalTool
{ {
public string Name { get; set; } = string.Empty; public string Name { get; private set; } = string.Empty;
public string Icon { get; set; } = string.Empty; public string Executable { get; private set; } = string.Empty;
public string Executable { get; set; } = string.Empty; public string OpenCmdArgs { get; private set; } = string.Empty;
public string OpenCmdArgs { get; set; } = string.Empty; public Bitmap IconImage { get; private set; } = null;
public Bitmap IconImage public ExternalTool(string name, string icon, string executable, string openCmdArgs)
{ {
get Name = name;
{ Executable = executable;
if (_isFirstTimeGetIcon) OpenCmdArgs = openCmdArgs;
{
_isFirstTimeGetIcon = false;
if (!string.IsNullOrWhiteSpace(Icon))
{
try try
{ {
var icon = AssetLoader.Open(new Uri($"avares://SourceGit/Resources/ExternalToolIcons/{Icon}.png", UriKind.RelativeOrAbsolute)); var asset = AssetLoader.Open(new Uri($"avares://SourceGit/Resources/ExternalToolIcons/{icon}.png", UriKind.RelativeOrAbsolute));
_iconImage = new Bitmap(icon); IconImage = new Bitmap(asset);
}
catch
{
}
}
}
return _iconImage;
} }
catch { }
} }
public void Open(string repo) public void Open(string repo)
@ -52,9 +41,6 @@ namespace SourceGit.Models
UseShellExecute = false, UseShellExecute = false,
}); });
} }
private bool _isFirstTimeGetIcon = true;
private Bitmap _iconImage = null;
} }
public class JetBrainsState public class JetBrainsState
@ -107,13 +93,7 @@ namespace SourceGit.Models
return; return;
} }
Founded.Add(new ExternalTool Founded.Add(new ExternalTool(name, icon, path, args));
{
Name = name,
Icon = icon,
OpenCmdArgs = args,
Executable = path
});
} }
public void VSCode(Func<string> platformFinder) public void VSCode(Func<string> platformFinder)
@ -154,13 +134,11 @@ namespace SourceGit.Models
if (exclude.Contains(tool.ToolId.ToLowerInvariant())) if (exclude.Contains(tool.ToolId.ToLowerInvariant()))
continue; continue;
Founded.Add(new ExternalTool Founded.Add(new ExternalTool(
{ $"{tool.DisplayName} {tool.DisplayVersion}",
Name = $"{tool.DisplayName} {tool.DisplayVersion}", supported_icons.Contains(tool.ProductCode) ? $"JetBrains/{tool.ProductCode}" : "JetBrains/JB",
Icon = supported_icons.Contains(tool.ProductCode) ? $"JetBrains/{tool.ProductCode}" : $"JetBrains/JB", Path.Combine(tool.InstallLocation, tool.LaunchCommand),
OpenCmdArgs = "\"{0}\"", "\"{0}\""));
Executable = Path.Combine(tool.InstallLocation, tool.LaunchCommand),
});
} }
} }
} }

View file

@ -41,8 +41,6 @@ namespace SourceGit.Native
{ {
throw new Exception("Platform unsupported!!!"); throw new Exception("Platform unsupported!!!");
} }
ExternalTools = _backend.FindExternalTools();
} }
public static Models.Shell GetShell() public static Models.Shell GetShell()
@ -77,6 +75,11 @@ namespace SourceGit.Native
_backend.SetupApp(builder); _backend.SetupApp(builder);
} }
public static void SetupEnternalTools()
{
ExternalTools = _backend.FindExternalTools();
}
public static string FindGitExecutable() public static string FindGitExecutable()
{ {
return _backend.FindGitExecutable(); return _backend.FindGitExecutable();