refactor<*>: rewrite all with AvaloniaUI

This commit is contained in:
leo 2024-02-06 15:08:37 +08:00
parent 0136904612
commit 2a62596999
521 changed files with 19780 additions and 23244 deletions

45
src/Native/MacOS.cs Normal file
View file

@ -0,0 +1,45 @@
using System.Diagnostics;
using System.IO;
using System.Runtime.Versioning;
namespace SourceGit.Native {
[SupportedOSPlatform("macOS")]
internal class MacOS : OS.IBackend {
public string FindGitInstallDir() {
if (File.Exists("/usr/bin/git")) return "/usr/bin/git";
return string.Empty;
}
public string FindVSCode() {
if (File.Exists("/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code")) {
return "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code";
}
return string.Empty;
}
public void OpenBrowser(string url) {
Process.Start("open", url);
}
public void OpenInFileManager(string path, bool select) {
if (Directory.Exists(path)) {
Process.Start("open", path);
} else if (File.Exists(path)) {
Process.Start("open", $"\"{path}\" -a Finder");
}
}
public void OpenTerminal(string workdir) {
Process.Start(new ProcessStartInfo() {
WorkingDirectory = workdir,
FileName = "/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal",
UseShellExecute = false,
});
}
public void OpenWithDefaultEditor(string file) {
Process.Start("open", file);
}
}
}

79
src/Native/OS.cs Normal file
View file

@ -0,0 +1,79 @@
using System;
using System.Diagnostics;
using System.IO;
namespace SourceGit.Native {
public static class OS {
public interface IBackend {
string FindGitInstallDir();
string FindVSCode();
void OpenTerminal(string workdir);
void OpenInFileManager(string path, bool select);
void OpenBrowser(string url);
void OpenWithDefaultEditor(string file);
}
public static string GitInstallDir {
get;
set;
}
public static string GitExecutableFile {
get => Path.Combine(GitInstallDir, "bin", OperatingSystem.IsWindows() ? "git.exe" : "git");
}
public static string VSCodeExecutableFile {
get;
set;
}
static OS() {
if (OperatingSystem.IsMacOS()) {
_backend = new MacOS();
VSCodeExecutableFile = _backend.FindVSCode();
} else if (OperatingSystem.IsWindows()) {
_backend = new Windows();
VSCodeExecutableFile = _backend.FindVSCode();
} else {
throw new Exception("Platform unsupported!!!");
}
}
public static string FindGitInstallDir() {
return _backend?.FindGitInstallDir();
}
public static void OpenInFileManager(string path, bool select = false) {
_backend?.OpenInFileManager(path, select);
}
public static void OpenBrowser(string url) {
_backend?.OpenBrowser(url);
}
public static void OpenTerminal(string workdir) {
_backend?.OpenTerminal(workdir);
}
public static void OpenWithDefaultEditor(string file) {
_backend?.OpenWithDefaultEditor(file);
}
public static void OpenInVSCode(string repo) {
if (string.IsNullOrEmpty(VSCodeExecutableFile)) {
App.RaiseException(repo, "Visual Studio Code can NOT be found in your system!!!");
return;
}
Process.Start(new ProcessStartInfo() {
WorkingDirectory = repo,
FileName = VSCodeExecutableFile,
Arguments = $"\"{repo}\"",
UseShellExecute = false,
});
}
private static IBackend _backend = null;
}
}

105
src/Native/Windows.cs Normal file
View file

@ -0,0 +1,105 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
namespace SourceGit.Native {
[SupportedOSPlatform("windows")]
internal class Windows : OS.IBackend {
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode, SetLastError = false)]
private static extern bool PathFindOnPath([In, Out] StringBuilder pszFile, [In] string[] ppszOtherDirs);
public string FindGitInstallDir() {
var reg = Microsoft.Win32.RegistryKey.OpenBaseKey(
Microsoft.Win32.RegistryHive.LocalMachine,
Microsoft.Win32.RegistryView.Registry64);
var git = reg.OpenSubKey("SOFTWARE\\GitForWindows");
if (git != null) {
return git.GetValue("InstallPath") as string;
}
var builder = new StringBuilder("git.exe", 259);
if (!PathFindOnPath(builder, null)) {
return null;
}
var exePath = builder.ToString();
if (string.IsNullOrEmpty(exePath)) return null;
var binDir = Path.GetDirectoryName(exePath);
var bashPath = Path.Combine(binDir, "bash.exe");
if (File.Exists(bashPath)) return Path.GetDirectoryName(binDir);
binDir = Path.Combine(Path.GetDirectoryName(binDir), "bin");
bashPath = Path.Combine(binDir, "bash.exe");
if (File.Exists(bashPath)) return Path.GetDirectoryName(binDir);
return null;
}
public string FindVSCode() {
var root = Microsoft.Win32.RegistryKey.OpenBaseKey(
Microsoft.Win32.RegistryHive.LocalMachine,
Environment.Is64BitOperatingSystem ? Microsoft.Win32.RegistryView.Registry64 : Microsoft.Win32.RegistryView.Registry32);
var vscode = root.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{C26E74D1-022E-4238-8B9D-1E7564A36CC9}_is1");
if (vscode != null) {
return vscode.GetValue("DisplayIcon") as string;
}
vscode = root.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{1287CAD5-7C8D-410D-88B9-0D1EE4A83FF2}_is1");
if (vscode != null) {
return vscode.GetValue("DisplayIcon") as string;
}
vscode = root.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{F8A2A208-72B3-4D61-95FC-8A65D340689B}_is1");
if (vscode != null) {
return vscode.GetValue("DisplayIcon") as string;
}
vscode = root.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{EA457B21-F73E-494C-ACAB-524FDE069978}_is1");
if (vscode != null) {
return vscode.GetValue("DisplayIcon") as string;
}
return string.Empty;
}
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 bash = Path.Combine(ViewModels.Preference.Instance.GitInstallDir, "bin", "bash.exe");
if (!File.Exists(bash)) {
App.RaiseException("", $"Can NOT found bash.exe under '{ViewModels.Preference.Instance.GitInstallDir}'");
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) {
if (select) {
Process.Start("explorer", $"/select,\"{path}\"");
} else {
Process.Start("explorer", path);
}
}
public void OpenWithDefaultEditor(string file) {
var info = new ProcessStartInfo("cmd", $"/c start {file}");
info.CreateNoWindow = true;
Process.Start(info);
}
}
}