refactor<*>: move SourceGit.Git.Preference to SourceGit.Preference

This commit is contained in:
leo 2020-12-17 11:55:06 +08:00
parent d248394e42
commit 3e6c837916
27 changed files with 236 additions and 260 deletions

View file

@ -1,6 +1,7 @@
using Microsoft.Win32;
using System;
using System.IO;
using System.Text.Json;
using System.Windows;
namespace SourceGit {
@ -9,22 +10,18 @@ namespace SourceGit {
/// Application.
/// </summary>
public partial class App : Application {
/// <summary>
/// Getter/Setter for Git preference.
/// Getter/Setter for application user setting.
/// </summary>
public static Git.Preference Preference {
get { return Git.Preference.Instance; }
set { Git.Preference.Instance = value; }
}
public static Preference Setting { get; set; }
/// <summary>
/// Check if GIT has been configured.
/// </summary>
public static bool IsGitConfigured {
get {
return !string.IsNullOrEmpty(Preference.GitExecutable)
&& File.Exists(Preference.GitExecutable);
return !string.IsNullOrEmpty(Setting.Tools.GitExecutable)
&& File.Exists(Setting.Tools.GitExecutable);
}
}
@ -60,6 +57,17 @@ namespace SourceGit {
return;
}
// Load settings.
var settingFile = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"SourceGit",
"preference.json");
if (!File.Exists(settingFile)) {
Setting = new Preference();
} else {
Setting = JsonSerializer.Deserialize<Preference>(File.ReadAllText(settingFile));
}
// Try auto configure git via registry.
if (!IsGitConfigured) {
var root = RegistryKey.OpenBaseKey(
@ -68,7 +76,7 @@ namespace SourceGit {
var git = root.OpenSubKey("SOFTWARE\\GitForWindows");
if (git != null) {
Preference.GitExecutable = Path.Combine(
Setting.Tools.GitExecutable = Path.Combine(
git.GetValue("InstallPath") as string,
"bin",
"git.exe");
@ -76,7 +84,7 @@ namespace SourceGit {
}
// Apply themes
if (Preference.UseLightTheme) {
if (Setting.UI.UseLightTheme) {
foreach (var rs in Current.Resources.MergedDictionaries) {
if (rs.Source != null && rs.Source.OriginalString.StartsWith("pack://application:,,,/Resources/Themes/")) {
rs.Source = new Uri("pack://application:,,,/Resources/Themes/Light.xaml", UriKind.Absolute);
@ -98,14 +106,30 @@ namespace SourceGit {
(Current.MainWindow as UI.Launcher).Open(repo);
}
/// <summary>
/// Save settings.
/// </summary>
public static void SaveSetting() {
var settingFile = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"SourceGit",
"preference.json");
var dir = Path.GetDirectoryName(settingFile);
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
var data = JsonSerializer.Serialize(Setting, new JsonSerializerOptions() { WriteIndented = true });
File.WriteAllText(settingFile, data);
}
/// <summary>
/// Deactivated event.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnAppDeactivated(object sender, EventArgs e) {
Git.Preference.Save();
GC.Collect();
SaveSetting();
}
}
}