feature<VSCode>: supports to open repository with Visual Studio Code

This commit is contained in:
leo 2021-09-13 14:22:25 +08:00
parent 4a676e094d
commit 4456019968
8 changed files with 65 additions and 19 deletions

View file

@ -194,7 +194,7 @@
<CheckBox
Grid.Row="9" Grid.Column="1"
Content="{DynamicResource Text.Preference.UseWindowsTerminal}"
IsEnabled="{Binding ElementName=me, Path=EnableWindowsTerminal}"
IsEnabled="{Binding ElementName=me, Path=HasWindowsTerminal}"
IsChecked="{Binding Source={x:Static models:Preference.Instance}, Path=General.UseWindowsTerminal, Mode=TwoWay}"/>
<!-- Git Group -->

View file

@ -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,
};

View file

@ -30,6 +30,14 @@
Icon="{DynamicResource Icon.Folder.Open}"
ToolTip="{DynamicResource Text.Dashboard.Explore}"
Click="Explore"/>
<controls:IconButton
x:Name="btnOpenWithVSCode"
Visibility="Collapsed"
Margin="8,0"
Padding="0,9"
Icon="{DynamicResource Icon.VSCode}"
ToolTip="{DynamicResource Text.Dashboard.VSCode}"
Click="OpenInVSCode"/>
<controls:IconButton
Margin="8,0"
Padding="0,9"

View file

@ -56,6 +56,9 @@ namespace SourceGit.Views.Widgets {
InitializeComponent();
InitPages();
var vscode = Models.ExecutableFinder.Find("code.cmd");
if (vscode != null) btnOpenWithVSCode.Visibility = Visibility.Visible;
var watcher = Models.Watcher.Get(repo.Path);
watcher.Navigate += NavigateTo;
watcher.BranchChanged += UpdateBranches;
@ -309,6 +312,21 @@ namespace SourceGit.Views.Widgets {
e.Handled = true;
}
private void OpenInVSCode(object sender, RoutedEventArgs e) {
var vscode = Models.ExecutableFinder.Find("code.cmd");
if (vscode == null) return;
vscode = Path.Combine(Path.GetDirectoryName(vscode), "..", "Code.exe");
Process.Start(new ProcessStartInfo {
WorkingDirectory = repo.Path,
FileName = vscode,
Arguments = $"\"{repo.Path}\"",
UseShellExecute = false,
});
e.Handled = true;
}
private void Terminal(object sender, RoutedEventArgs e) {
var bash = Path.Combine(Models.Preference.Instance.Git.Path, "..", "bash.exe");
if (!File.Exists(bash)) {