From a976f007f542bc18f2836db66c2b88ed1384f5de Mon Sep 17 00:00:00 2001 From: leo Date: Fri, 17 May 2024 12:01:29 +0800 Subject: [PATCH] refactor: load external tool's icon on startup --- src/App.axaml.cs | 1 + src/Models/ExternalTool.cs | 60 ++++++++++++-------------------------- src/Native/OS.cs | 7 +++-- 3 files changed, 25 insertions(+), 43 deletions(-) diff --git a/src/App.axaml.cs b/src/App.axaml.cs index ed92b621..151c6e92 100644 --- a/src/App.axaml.cs +++ b/src/App.axaml.cs @@ -266,6 +266,7 @@ namespace SourceGit if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { BindingPlugins.DataValidators.RemoveAt(0); + Native.OS.SetupEnternalTools(); var launcher = new Views.Launcher(); _notificationReceiver = launcher; diff --git a/src/Models/ExternalTool.cs b/src/Models/ExternalTool.cs index 1489e1cf..98351cd6 100644 --- a/src/Models/ExternalTool.cs +++ b/src/Models/ExternalTool.cs @@ -12,34 +12,23 @@ namespace SourceGit.Models { public class ExternalTool { - public string Name { get; set; } = string.Empty; - public string Icon { get; set; } = string.Empty; - public string Executable { get; set; } = string.Empty; - public string OpenCmdArgs { get; set; } = string.Empty; + public string Name { get; private set; } = string.Empty; + public string Executable { get; private set; } = string.Empty; + public string OpenCmdArgs { get; private 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; + OpenCmdArgs = openCmdArgs; + + try { - if (_isFirstTimeGetIcon) - { - _isFirstTimeGetIcon = false; - - if (!string.IsNullOrWhiteSpace(Icon)) - { - try - { - var icon = AssetLoader.Open(new Uri($"avares://SourceGit/Resources/ExternalToolIcons/{Icon}.png", UriKind.RelativeOrAbsolute)); - _iconImage = new Bitmap(icon); - } - catch - { - } - } - } - - return _iconImage; + var asset = AssetLoader.Open(new Uri($"avares://SourceGit/Resources/ExternalToolIcons/{icon}.png", UriKind.RelativeOrAbsolute)); + IconImage = new Bitmap(asset); } + catch { } } public void Open(string repo) @@ -52,9 +41,6 @@ namespace SourceGit.Models UseShellExecute = false, }); } - - private bool _isFirstTimeGetIcon = true; - private Bitmap _iconImage = null; } public class JetBrainsState @@ -107,13 +93,7 @@ namespace SourceGit.Models return; } - Founded.Add(new ExternalTool - { - Name = name, - Icon = icon, - OpenCmdArgs = args, - Executable = path - }); + Founded.Add(new ExternalTool(name, icon, path, args)); } public void VSCode(Func platformFinder) @@ -154,13 +134,11 @@ namespace SourceGit.Models if (exclude.Contains(tool.ToolId.ToLowerInvariant())) continue; - Founded.Add(new ExternalTool - { - Name = $"{tool.DisplayName} {tool.DisplayVersion}", - Icon = supported_icons.Contains(tool.ProductCode) ? $"JetBrains/{tool.ProductCode}" : $"JetBrains/JB", - OpenCmdArgs = "\"{0}\"", - Executable = Path.Combine(tool.InstallLocation, tool.LaunchCommand), - }); + Founded.Add(new ExternalTool( + $"{tool.DisplayName} {tool.DisplayVersion}", + supported_icons.Contains(tool.ProductCode) ? $"JetBrains/{tool.ProductCode}" : "JetBrains/JB", + Path.Combine(tool.InstallLocation, tool.LaunchCommand), + "\"{0}\"")); } } } diff --git a/src/Native/OS.cs b/src/Native/OS.cs index c3a94037..46a49e6a 100644 --- a/src/Native/OS.cs +++ b/src/Native/OS.cs @@ -41,8 +41,6 @@ namespace SourceGit.Native { throw new Exception("Platform unsupported!!!"); } - - ExternalTools = _backend.FindExternalTools(); } public static Models.Shell GetShell() @@ -77,6 +75,11 @@ namespace SourceGit.Native _backend.SetupApp(builder); } + public static void SetupEnternalTools() + { + ExternalTools = _backend.FindExternalTools(); + } + public static string FindGitExecutable() { return _backend.FindGitExecutable();