2021-04-29 05:05:55 -07:00
|
|
|
using Microsoft.Win32;
|
|
|
|
using System;
|
2021-09-03 04:27:19 -07:00
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using System.Text;
|
2021-04-29 05:05:55 -07:00
|
|
|
using System.Windows;
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
|
|
|
namespace SourceGit.Views {
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 设置面板
|
|
|
|
/// </summary>
|
2021-06-17 18:26:19 -07:00
|
|
|
public partial class Preference : Controls.Window {
|
2021-04-29 05:05:55 -07:00
|
|
|
|
|
|
|
public string User { get; set; }
|
|
|
|
public string Email { get; set; }
|
|
|
|
public string CRLF { get; set; }
|
2021-09-06 19:52:48 -07:00
|
|
|
public string Version { get; set; }
|
2021-04-29 05:05:55 -07:00
|
|
|
|
2021-09-07 19:35:59 -07:00
|
|
|
const int MAX_PATH = 260;
|
2021-09-03 04:27:19 -07:00
|
|
|
// 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);
|
|
|
|
|
2021-09-07 19:35:59 -07:00
|
|
|
public bool EnableWindowsTerminal { get; set; } = PathFindOnPath(new StringBuilder("wt.exe", MAX_PATH), null);
|
2021-09-03 04:27:19 -07:00
|
|
|
|
2021-04-29 05:05:55 -07:00
|
|
|
public Preference() {
|
2021-09-06 19:52:48 -07:00
|
|
|
UpdateGitInfo(false);
|
2021-04-29 05:05:55 -07:00
|
|
|
|
2021-09-03 04:27:19 -07:00
|
|
|
if (!EnableWindowsTerminal) {
|
|
|
|
Models.Preference.Instance.General.UseWindowsTerminal = false;
|
|
|
|
}
|
|
|
|
|
2021-04-29 05:05:55 -07:00
|
|
|
InitializeComponent();
|
|
|
|
}
|
|
|
|
|
2021-09-06 19:52:48 -07:00
|
|
|
private bool UpdateGitInfo(bool updateUi) {
|
|
|
|
var isReady = Models.Preference.Instance.IsReady;
|
|
|
|
if (isReady) {
|
|
|
|
User = new Commands.Config().Get("user.name");
|
|
|
|
Email = new Commands.Config().Get("user.email");
|
|
|
|
CRLF = new Commands.Config().Get("core.autocrlf");
|
|
|
|
Version = new Commands.Version().Query();
|
|
|
|
if (string.IsNullOrEmpty(CRLF)) CRLF = "false";
|
|
|
|
} else {
|
|
|
|
User = "";
|
|
|
|
Email = "";
|
|
|
|
CRLF = "false";
|
|
|
|
Version = "Unknown";
|
|
|
|
}
|
|
|
|
if (updateUi) {
|
|
|
|
editGitUser?.GetBindingExpression(TextBox.TextProperty).UpdateTarget();
|
|
|
|
editGitEmail?.GetBindingExpression(TextBox.TextProperty).UpdateTarget();
|
|
|
|
editGitCrlf?.GetBindingExpression(ComboBox.SelectedValueProperty).UpdateTarget();
|
|
|
|
textGitVersion?.GetBindingExpression(TextBlock.TextProperty).UpdateTarget();
|
|
|
|
}
|
|
|
|
return isReady;
|
2021-09-06 04:58:41 -07:00
|
|
|
}
|
|
|
|
|
2021-04-29 05:05:55 -07:00
|
|
|
#region EVENTS
|
2021-07-20 01:26:10 -07:00
|
|
|
private void LocaleChanged(object sender, SelectionChangedEventArgs e) {
|
|
|
|
Models.Locale.Change();
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
|
2021-04-29 05:05:55 -07:00
|
|
|
private void SelectGitPath(object sender, RoutedEventArgs e) {
|
2021-09-07 19:35:59 -07:00
|
|
|
var sb = new StringBuilder("git.exe", MAX_PATH);
|
2021-09-03 07:22:53 -07:00
|
|
|
string dir = PathFindOnPath(sb, null)
|
|
|
|
? sb.ToString()
|
|
|
|
: Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
|
|
|
|
|
|
|
|
var dialog = new OpenFileDialog {
|
|
|
|
Filter = "Git Executable|git.exe",
|
|
|
|
FileName = "git.exe",
|
|
|
|
Title = App.Text("Preference.Dialog.GitExe"),
|
|
|
|
InitialDirectory = dir,
|
|
|
|
CheckFileExists = true,
|
|
|
|
};
|
2021-04-29 05:05:55 -07:00
|
|
|
|
|
|
|
if (dialog.ShowDialog() == true) {
|
|
|
|
Models.Preference.Instance.Git.Path = dialog.FileName;
|
|
|
|
editGitPath?.GetBindingExpression(TextBox.TextProperty).UpdateTarget();
|
2021-09-06 19:52:48 -07:00
|
|
|
UpdateGitInfo(true);
|
2021-04-29 05:05:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SelectGitCloneDir(object sender, RoutedEventArgs e) {
|
2021-07-19 18:13:07 -07:00
|
|
|
var dialog = new Controls.FolderDialog();
|
2021-05-25 20:08:31 -07:00
|
|
|
if (dialog.ShowDialog() == true) {
|
|
|
|
Models.Preference.Instance.Git.DefaultCloneDir = dialog.SelectedPath;
|
2021-04-29 05:05:55 -07:00
|
|
|
txtGitCloneDir?.GetBindingExpression(TextBox.TextProperty).UpdateTarget();
|
2021-05-25 20:08:31 -07:00
|
|
|
}
|
2021-04-29 05:05:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private void SelectMergeTool(object sender, RoutedEventArgs e) {
|
|
|
|
var type = Models.Preference.Instance.MergeTool.Type;
|
|
|
|
var tool = Models.MergeTool.Supported.Find(x => x.Type == type);
|
|
|
|
|
|
|
|
if (tool == null || tool.Type == 0) return;
|
|
|
|
|
|
|
|
var dialog = new OpenFileDialog();
|
|
|
|
dialog.Filter = $"{tool.Name} Executable|{tool.Exec}";
|
|
|
|
dialog.Title = App.Text("Preference.Dialog.Merger", tool.Name);
|
|
|
|
dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
|
|
|
|
dialog.CheckFileExists = true;
|
|
|
|
|
|
|
|
if (dialog.ShowDialog() == true) {
|
|
|
|
Models.Preference.Instance.MergeTool.Path = dialog.FileName;
|
|
|
|
txtMergeExec?.GetBindingExpression(TextBox.TextProperty).UpdateTarget();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void MergeToolChanged(object sender, SelectionChangedEventArgs e) {
|
2021-06-07 05:58:42 -07:00
|
|
|
var type = (int)(sender as ComboBox).SelectedValue;
|
2021-04-29 05:05:55 -07:00
|
|
|
var tool = Models.MergeTool.Supported.Find(x => x.Type == type);
|
|
|
|
if (tool == null) return;
|
|
|
|
|
2021-06-07 05:58:42 -07:00
|
|
|
if (IsLoaded) {
|
|
|
|
Models.Preference.Instance.MergeTool.Path = tool.Finder();
|
|
|
|
txtMergeExec?.GetBindingExpression(TextBox.TextProperty).UpdateTarget();
|
|
|
|
}
|
|
|
|
|
2021-04-29 05:05:55 -07:00
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Quit(object sender, RoutedEventArgs e) {
|
2021-05-20 21:43:41 -07:00
|
|
|
if (Models.Preference.Instance.IsReady) {
|
|
|
|
var cmd = new Commands.Config();
|
|
|
|
var oldUser = cmd.Get("user.name");
|
|
|
|
if (oldUser != User) cmd.Set("user.name", User);
|
2021-04-29 05:05:55 -07:00
|
|
|
|
2021-05-20 21:43:41 -07:00
|
|
|
var oldEmail = cmd.Get("user.email");
|
|
|
|
if (oldEmail != Email) cmd.Set("user.email", Email);
|
2021-04-29 05:05:55 -07:00
|
|
|
|
2021-05-20 21:43:41 -07:00
|
|
|
var oldCRLF = cmd.Get("core.autocrlf");
|
|
|
|
if (oldCRLF != CRLF) cmd.Set("core.autocrlf", CRLF);
|
|
|
|
}
|
2021-04-29 05:05:55 -07:00
|
|
|
|
|
|
|
Models.Preference.Save();
|
2021-07-20 01:26:10 -07:00
|
|
|
Close();
|
2021-04-29 05:05:55 -07:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|