using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; using System.Runtime.Versioning; using System.Text; using Avalonia; using Avalonia.Controls; using Avalonia.Media; namespace SourceGit.Native { [SupportedOSPlatform("windows")] internal class Windows : OS.IBackend { [StructLayout(LayoutKind.Sequential)] internal struct RTL_OSVERSIONINFOEX { internal uint dwOSVersionInfoSize; internal uint dwMajorVersion; internal uint dwMinorVersion; internal uint dwBuildNumber; internal uint dwPlatformId; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] internal string szCSDVersion; } [StructLayout(LayoutKind.Sequential)] internal struct MARGINS { public int cxLeftWidth; public int cxRightWidth; public int cyTopHeight; public int cyBottomHeight; } [DllImport("shlwapi.dll", CharSet = CharSet.Unicode, SetLastError = false)] private static extern bool PathFindOnPath([In, Out] StringBuilder pszFile, [In] string[] ppszOtherDirs); [DllImport("ntdll")] private static extern int RtlGetVersion(ref RTL_OSVERSIONINFOEX lpVersionInformation); [DllImport("dwmapi.dll")] private static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margins); [DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = false)] private static extern IntPtr ILCreateFromPathW(string pszPath); [DllImport("shell32.dll", SetLastError = false)] private static extern void ILFree(IntPtr pidl); [DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = false)] private static extern int SHOpenFolderAndSelectItems(IntPtr pidlFolder, int cild, IntPtr apidl, int dwFlags); public void SetupApp(AppBuilder builder) { builder.With(new FontManagerOptions() { DefaultFamilyName = "Microsoft YaHei UI", FontFallbacks = [new FontFallback { FontFamily = "Microsoft YaHei" }], }); // Fix drop shadow issue on Windows 10 RTL_OSVERSIONINFOEX v = new RTL_OSVERSIONINFOEX(); v.dwOSVersionInfoSize = (uint)Marshal.SizeOf(); if (RtlGetVersion(ref v) == 0 && (v.dwMajorVersion < 10 || v.dwBuildNumber < 22000)) { Window.WindowStateProperty.Changed.AddClassHandler((w, e) => { if (w.WindowState != WindowState.Maximized) { var margins = new MARGINS { cxLeftWidth = 1, cxRightWidth = 1, cyTopHeight = 1, cyBottomHeight = 1 }; DwmExtendFrameIntoClientArea(w.TryGetPlatformHandle().Handle, ref margins); } }); Window.LoadedEvent.AddClassHandler((w, e) => { if (w.WindowState != WindowState.Maximized) { var margins = new MARGINS { cxLeftWidth = 1, cxRightWidth = 1, cyTopHeight = 1, cyBottomHeight = 1 }; DwmExtendFrameIntoClientArea(w.TryGetPlatformHandle().Handle, ref margins); } }); } } public string FindGitExecutable() { var reg = Microsoft.Win32.RegistryKey.OpenBaseKey( Microsoft.Win32.RegistryHive.LocalMachine, Microsoft.Win32.RegistryView.Registry64); var git = reg.OpenSubKey("SOFTWARE\\GitForWindows"); if (git != null) { return Path.Combine(git.GetValue("InstallPath") as string, "bin", "git.exe"); } var builder = new StringBuilder("git.exe", 259); if (!PathFindOnPath(builder, null)) { return null; } var exePath = builder.ToString(); if (!string.IsNullOrEmpty(exePath)) { return exePath; } return null; } public List FindExternalEditors() { var editors = new List(); var vscode = FindVSCode(); if (!string.IsNullOrEmpty(vscode) && File.Exists(vscode)) { editors.Add(new Models.ExternalEditor { Name = "Visual Studio Code", Icon = "vscode.png", Executable = vscode, OpenCmdArgs = "\"{0}\"", }); } var vscodeInsiders = FindVSCodeInsiders(); if (!string.IsNullOrEmpty(vscodeInsiders) && File.Exists(vscodeInsiders)) { editors.Add(new Models.ExternalEditor { Name = "Visual Studio Code - Insiders", Icon = "vscode_insiders.png", Executable = vscodeInsiders, OpenCmdArgs = "\"{0}\"", }); } var fleet = FindFleet(); if (!string.IsNullOrEmpty(fleet) && File.Exists(fleet)) { editors.Add(new Models.ExternalEditor { Name = "JetBrains Fleet", Icon = "fleet.png", Executable = fleet, OpenCmdArgs = "\"{0}\"", }); } var sublime = FindSublimeText(); if (!string.IsNullOrEmpty(sublime) && File.Exists(sublime)) { editors.Add(new Models.ExternalEditor { Name = "Sublime Text", Icon = "sublime_text.png", Executable = sublime, OpenCmdArgs = "\"{0}\"", }); } return editors; } public void OpenBrowser(string url) { var info = new ProcessStartInfo("cmd", $"/c start {url}"); info.CreateNoWindow = true; Process.Start(info); } public void OpenTerminal(string workdir) { var binDir = Path.GetDirectoryName(OS.GitExecutable); var bash = Path.Combine(binDir, "bash.exe"); if (!File.Exists(bash)) { App.RaiseException(string.IsNullOrEmpty(workdir) ? "" : workdir, $"Can NOT found bash.exe under '{binDir}'"); return; } var startInfo = new ProcessStartInfo(); startInfo.UseShellExecute = true; startInfo.FileName = bash; if (!string.IsNullOrEmpty(workdir) && Path.Exists(workdir)) startInfo.WorkingDirectory = workdir; Process.Start(startInfo); } public void OpenInFileManager(string path, bool select) { var fullpath = string.Empty; if (File.Exists(path)) { fullpath = new FileInfo(path).FullName; // For security reason, we never execute a file. // Instead, we open the folder and select it. select = true; } else { fullpath = new DirectoryInfo(path).FullName; } if (select) { // The fullpath here may be a file or a folder. OpenFolderAndSelectFile(fullpath); } else { // The fullpath here is always a folder. Process.Start(new ProcessStartInfo(fullpath) { UseShellExecute = true, CreateNoWindow = true, }); } } public void OpenWithDefaultEditor(string file) { var info = new FileInfo(file); var start = new ProcessStartInfo("cmd", $"/c start {info.FullName}"); start.CreateNoWindow = true; Process.Start(start); } #region EXTERNAL_EDITOR_FINDER private string FindVSCode() { var localMachine = Microsoft.Win32.RegistryKey.OpenBaseKey( Microsoft.Win32.RegistryHive.LocalMachine, Microsoft.Win32.RegistryView.Registry64); // VSCode (system) var systemVScode = localMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{EA457B21-F73E-494C-ACAB-524FDE069978}_is1"); if (systemVScode != null) { return systemVScode.GetValue("DisplayIcon") as string; } var currentUser = Microsoft.Win32.RegistryKey.OpenBaseKey( Microsoft.Win32.RegistryHive.CurrentUser, Microsoft.Win32.RegistryView.Registry64); // VSCode (user) var vscode = currentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{771FD6B0-FA20-440A-A002-3B3BAC16DC50}_is1"); if (vscode != null) { return vscode.GetValue("DisplayIcon") as string; } // ENV var customPath = Environment.GetEnvironmentVariable("VSCODE_PATH"); if (!string.IsNullOrEmpty(customPath)) { return customPath; } return string.Empty; } private string FindVSCodeInsiders() { var localMachine = Microsoft.Win32.RegistryKey.OpenBaseKey( Microsoft.Win32.RegistryHive.LocalMachine, Microsoft.Win32.RegistryView.Registry64); // VSCode - Insiders (system) var systemVScodeInsiders = localMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{1287CAD5-7C8D-410D-88B9-0D1EE4A83FF2}_is1"); if (systemVScodeInsiders != null) { return systemVScodeInsiders.GetValue("DisplayIcon") as string; } var currentUser = Microsoft.Win32.RegistryKey.OpenBaseKey( Microsoft.Win32.RegistryHive.CurrentUser, Microsoft.Win32.RegistryView.Registry64); // VSCode - Insiders (user) var vscodeInsiders = currentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{217B4C08-948D-4276-BFBB-BEE930AE5A2C}_is1"); if (vscodeInsiders != null) { return vscodeInsiders.GetValue("DisplayIcon") as string; } // ENV var customPath = Environment.GetEnvironmentVariable("VSCODE_INSIDERS_PATH"); if (!string.IsNullOrEmpty(customPath)) { return customPath; } return string.Empty; } private string FindFleet() { var toolPath = Environment.ExpandEnvironmentVariables($"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}\\AppData\\Local\\Programs\\Fleet\\Fleet.exe"); if (File.Exists(toolPath)) return toolPath; var customPath = Environment.GetEnvironmentVariable("FLEET_PATH"); if (!string.IsNullOrEmpty(customPath)) return customPath; return string.Empty; } private string FindSublimeText() { var localMachine = Microsoft.Win32.RegistryKey.OpenBaseKey( Microsoft.Win32.RegistryHive.LocalMachine, Microsoft.Win32.RegistryView.Registry64); var sublime = localMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Sublime Text_is1"); if (sublime != null) { var icon = sublime.GetValue("DisplayIcon") as string; return Path.Combine(Path.GetDirectoryName(icon), "subl.exe"); } var sublime3 = localMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Sublime Text 3_is1"); if (sublime3 != null) { var icon = sublime3.GetValue("DisplayIcon") as string; return Path.Combine(Path.GetDirectoryName(icon), "subl.exe"); } var customPath = Environment.GetEnvironmentVariable("SUBLIME_TEXT_PATH"); if (!string.IsNullOrEmpty(customPath)) return customPath; return string.Empty; } #endregion private void OpenFolderAndSelectFile(string folderPath) { var pidl = ILCreateFromPathW(folderPath); try { SHOpenFolderAndSelectItems(pidl, 0, 0, 0); } finally { ILFree(pidl); } } } }