From 4456019968e34277186a184a5a597a5846fa9a9f Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 13 Sep 2021 14:22:25 +0800 Subject: [PATCH] feature: supports to open repository with Visual Studio Code --- src/Models/ExecutableFinder.cs | 26 ++++++++++++++++++++++++++ src/Resources/Icons.xaml | 2 ++ src/Resources/Locales/en_US.xaml | 1 + src/Resources/Locales/zh_CN.xaml | 1 + src/Views/Preference.xaml | 2 +- src/Views/Preference.xaml.cs | 26 ++++++++------------------ src/Views/Widgets/Dashboard.xaml | 8 ++++++++ src/Views/Widgets/Dashboard.xaml.cs | 18 ++++++++++++++++++ 8 files changed, 65 insertions(+), 19 deletions(-) create mode 100644 src/Models/ExecutableFinder.cs diff --git a/src/Models/ExecutableFinder.cs b/src/Models/ExecutableFinder.cs new file mode 100644 index 00000000..7f36522e --- /dev/null +++ b/src/Models/ExecutableFinder.cs @@ -0,0 +1,26 @@ +using System.Runtime.InteropServices; +using System.Text; + +namespace SourceGit.Models { + /// + /// 用于在PATH中检测可执行文件 + /// + public class ExecutableFinder { + + // https://docs.microsoft.com/en-us/windows/desktop/api/shlwapi/nf-shlwapi-pathfindonpathw + // https://www.pinvoke.net/default.aspx/shlwapi.PathFindOnPath + [DllImport("shlwapi.dll", CharSet = CharSet.Unicode, SetLastError = false)] + private static extern bool PathFindOnPath([In, Out] StringBuilder pszFile, [In] string[] ppszOtherDirs); + + /// + /// 从PATH中找到可执行文件路径 + /// + /// + /// + public static string Find(string exec) { + var builder = new StringBuilder(exec, 259); + var rs = PathFindOnPath(builder, null); + return rs ? builder.ToString() : null; + } + } +} diff --git a/src/Resources/Icons.xaml b/src/Resources/Icons.xaml index f4c4a582..bcb0857d 100644 --- a/src/Resources/Icons.xaml +++ b/src/Resources/Icons.xaml @@ -70,4 +70,6 @@ M364 512h67v108h108v67h-108v108h-67v-108h-108v-67h108v-108zm298-64A107 107 0 01768 555C768 614 720 660 660 660h-108v-54h-108v-108h-94v108h-94c4-21 22-47 44-51l-1-12a75 75 0 0171-75a128 128 0 01239-7a106 106 0 0153-14z M177 156c-22 5-33 17-36 37c-10 57-33 258-13 278l445 445c23 23 61 23 84 0l246-246c23-23 23-61 0-84l-445-445C437 120 231 145 177 156zM331 344c-26 26-69 26-95 0c-26-26-26-69 0-95s69-26 95 0C357 276 357 318 331 344z M683 537h-144v-142h-142V283H239a44 44 0 00-41 41v171a56 56 0 0014 34l321 321a41 41 0 0058 0l174-174a41 41 0 000-58zm-341-109a41 41 0 110-58a41 41 0 010 58zM649 284V142h-69v142h-142v68h142v142h69v-142h142v-68h-142z + + M719 85 388 417l-209-165L87 299v427l92 47 210-164L720 939 939 850V171zM186 610V412l104 104zm526 55L514 512l198-153z \ No newline at end of file diff --git a/src/Resources/Locales/en_US.xaml b/src/Resources/Locales/en_US.xaml index 2adc8524..878a6364 100644 --- a/src/Resources/Locales/en_US.xaml +++ b/src/Resources/Locales/en_US.xaml @@ -115,6 +115,7 @@ Optional. Open In File Browser + Open In Visual Studio Code Open Git Bash Search Commit Configure this repository diff --git a/src/Resources/Locales/zh_CN.xaml b/src/Resources/Locales/zh_CN.xaml index 71e1b3c1..e6d8d12f 100644 --- a/src/Resources/Locales/zh_CN.xaml +++ b/src/Resources/Locales/zh_CN.xaml @@ -114,6 +114,7 @@ 选填 在文件浏览器中打开 + 在Visual Studio Code中打开 打开GIT终端 查找提交 配置本仓库 diff --git a/src/Views/Preference.xaml b/src/Views/Preference.xaml index 0fa7a85d..2d757b48 100644 --- a/src/Views/Preference.xaml +++ b/src/Views/Preference.xaml @@ -194,7 +194,7 @@ diff --git a/src/Views/Preference.xaml.cs b/src/Views/Preference.xaml.cs index eb54cb44..b01aae40 100644 --- a/src/Views/Preference.xaml.cs +++ b/src/Views/Preference.xaml.cs @@ -1,7 +1,6 @@ using Microsoft.Win32; using System; -using System.Runtime.InteropServices; -using System.Text; +using System.IO; using System.Windows; using System.Windows.Controls; @@ -16,21 +15,13 @@ namespace SourceGit.Views { public string Email { get; set; } public string CRLF { get; set; } public string Version { get; set; } - - const int MAX_PATH = 260; - // https://docs.microsoft.com/en-us/windows/desktop/api/shlwapi/nf-shlwapi-pathfindonpathw - // https://www.pinvoke.net/default.aspx/shlwapi.PathFindOnPath - [DllImport("shlwapi.dll", CharSet = CharSet.Unicode, SetLastError = false)] - private static extern bool PathFindOnPath([In, Out] StringBuilder pszFile, [In] string[] ppszOtherDirs); - - public bool EnableWindowsTerminal { get; set; } = PathFindOnPath(new StringBuilder("wt.exe", MAX_PATH), null); + public bool HasWindowsTerminal { get; set; } public Preference() { UpdateGitInfo(false); - if (!EnableWindowsTerminal) { - Models.Preference.Instance.General.UseWindowsTerminal = false; - } + HasWindowsTerminal = Models.ExecutableFinder.Find("wt.exe") != null; + if (HasWindowsTerminal) Models.Preference.Instance.General.UseWindowsTerminal = false; InitializeComponent(); } @@ -65,16 +56,15 @@ namespace SourceGit.Views { } private void SelectGitPath(object sender, RoutedEventArgs e) { - var sb = new StringBuilder("git.exe", MAX_PATH); - string dir = PathFindOnPath(sb, null) - ? sb.ToString() - : Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); + var initDir = Models.ExecutableFinder.Find("git.exe"); + if (initDir == null) initDir = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); + else initDir = Path.GetDirectoryName(initDir); var dialog = new OpenFileDialog { Filter = "Git Executable|git.exe", FileName = "git.exe", Title = App.Text("Preference.Dialog.GitExe"), - InitialDirectory = dir, + InitialDirectory = initDir, CheckFileExists = true, }; diff --git a/src/Views/Widgets/Dashboard.xaml b/src/Views/Widgets/Dashboard.xaml index dc7277e8..21dc72cb 100644 --- a/src/Views/Widgets/Dashboard.xaml +++ b/src/Views/Widgets/Dashboard.xaml @@ -30,6 +30,14 @@ Icon="{DynamicResource Icon.Folder.Open}" ToolTip="{DynamicResource Text.Dashboard.Explore}" Click="Explore"/> +