2024-02-05 23:08:37 -08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Text.Json;
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
using Avalonia.Collections;
|
|
|
|
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
|
|
|
namespace SourceGit.ViewModels
|
|
|
|
{
|
|
|
|
public class Preference : ObservableObject
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
[JsonIgnore]
|
2024-03-17 18:37:06 -07:00
|
|
|
public static Preference Instance
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (_instance == null)
|
|
|
|
{
|
|
|
|
if (!File.Exists(_savePath))
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
_instance = new Preference();
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
_instance = JsonSerializer.Deserialize(File.ReadAllText(_savePath), JsonSerializationCodeGen.Default.Preference);
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
_instance = new Preference();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_instance.Repositories.RemoveAll(x => !Directory.Exists(x.FullPath));
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
if (!_instance.IsGitConfigured)
|
|
|
|
{
|
2024-02-20 19:29:28 -08:00
|
|
|
_instance.GitInstallPath = Native.OS.FindGitExecutable();
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return _instance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public string Locale
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
get => _locale;
|
2024-03-17 18:37:06 -07:00
|
|
|
set
|
|
|
|
{
|
|
|
|
if (SetProperty(ref _locale, value))
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
App.SetLocale(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public string Theme
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
get => _theme;
|
2024-03-17 18:37:06 -07:00
|
|
|
set
|
|
|
|
{
|
|
|
|
if (SetProperty(ref _theme, value))
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
App.SetTheme(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public string AvatarServer
|
|
|
|
{
|
2024-02-22 19:39:05 -08:00
|
|
|
get => Models.AvatarManager.SelectedServer;
|
2024-03-17 18:37:06 -07:00
|
|
|
set
|
|
|
|
{
|
|
|
|
if (Models.AvatarManager.SelectedServer != value)
|
|
|
|
{
|
2024-02-22 19:39:05 -08:00
|
|
|
Models.AvatarManager.SelectedServer = value;
|
|
|
|
OnPropertyChanged(nameof(AvatarServer));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public int MaxHistoryCommits
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
get => _maxHistoryCommits;
|
|
|
|
set => SetProperty(ref _maxHistoryCommits, value);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public bool RestoreTabs
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
get => _restoreTabs;
|
|
|
|
set => SetProperty(ref _restoreTabs, value);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public bool UseFixedTabWidth
|
|
|
|
{
|
2024-02-29 22:09:39 -08:00
|
|
|
get => _useFixedTabWidth;
|
|
|
|
set => SetProperty(ref _useFixedTabWidth, value);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public bool UseTwoColumnsLayoutInHistories
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
get => _useTwoColumnsLayoutInHistories;
|
|
|
|
set => SetProperty(ref _useTwoColumnsLayoutInHistories, value);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public bool UseCombinedTextDiff
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
get => _useCombinedTextDiff;
|
|
|
|
set => SetProperty(ref _useCombinedTextDiff, value);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public Models.ChangeViewMode UnstagedChangeViewMode
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
get => _unstagedChangeViewMode;
|
|
|
|
set => SetProperty(ref _unstagedChangeViewMode, value);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public Models.ChangeViewMode StagedChangeViewMode
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
get => _stagedChangeViewMode;
|
|
|
|
set => SetProperty(ref _stagedChangeViewMode, value);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public Models.ChangeViewMode CommitChangeViewMode
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
get => _commitChangeViewMode;
|
|
|
|
set => SetProperty(ref _commitChangeViewMode, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
[JsonIgnore]
|
2024-03-17 18:37:06 -07:00
|
|
|
public bool IsGitConfigured
|
|
|
|
{
|
2024-02-20 19:29:28 -08:00
|
|
|
get => !string.IsNullOrEmpty(GitInstallPath) && File.Exists(GitInstallPath);
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public string GitInstallPath
|
|
|
|
{
|
2024-02-20 19:29:28 -08:00
|
|
|
get => Native.OS.GitInstallPath;
|
2024-03-17 18:37:06 -07:00
|
|
|
set
|
|
|
|
{
|
|
|
|
if (Native.OS.GitInstallPath != value)
|
|
|
|
{
|
2024-02-20 19:29:28 -08:00
|
|
|
Native.OS.GitInstallPath = value;
|
|
|
|
OnPropertyChanged(nameof(GitInstallPath));
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public string GitDefaultCloneDir
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
get => _gitDefaultCloneDir;
|
|
|
|
set => SetProperty(ref _gitDefaultCloneDir, value);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public bool GitAutoFetch
|
|
|
|
{
|
2024-02-18 00:26:07 -08:00
|
|
|
get => Commands.AutoFetch.IsEnabled;
|
2024-03-17 18:37:06 -07:00
|
|
|
set
|
|
|
|
{
|
|
|
|
if (Commands.AutoFetch.IsEnabled != value)
|
|
|
|
{
|
2024-02-18 00:26:07 -08:00
|
|
|
Commands.AutoFetch.IsEnabled = value;
|
|
|
|
OnPropertyChanged(nameof(GitAutoFetch));
|
|
|
|
}
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public int ExternalMergeToolType
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
get => _externalMergeToolType;
|
2024-03-17 18:37:06 -07:00
|
|
|
set
|
|
|
|
{
|
2024-02-19 00:19:12 -08:00
|
|
|
var changed = SetProperty(ref _externalMergeToolType, value);
|
2024-03-17 18:37:06 -07:00
|
|
|
if (changed && !OperatingSystem.IsWindows() && value > 0 && value < Models.ExternalMergeTools.Supported.Count)
|
|
|
|
{
|
2024-02-19 00:19:12 -08:00
|
|
|
var tool = Models.ExternalMergeTools.Supported[value];
|
|
|
|
if (File.Exists(tool.Exec)) ExternalMergeToolPath = tool.Exec;
|
|
|
|
else ExternalMergeToolPath = string.Empty;
|
|
|
|
}
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public string ExternalMergeToolPath
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
get => _externalMergeToolPath;
|
|
|
|
set => SetProperty(ref _externalMergeToolPath, value);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public string ExternalMergeToolCmd
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
get => _externalMergeToolCmd;
|
|
|
|
set => SetProperty(ref _externalMergeToolCmd, value);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public string ExternalMergeToolDiffCmd
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
get => _externalMergeToolDiffCmd;
|
|
|
|
set => SetProperty(ref _externalMergeToolDiffCmd, value);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public List<Repository> Repositories
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
get;
|
|
|
|
set;
|
|
|
|
} = new List<Repository>();
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public AvaloniaList<RepositoryNode> RepositoryNodes
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
get => _repositoryNodes;
|
|
|
|
set => SetProperty(ref _repositoryNodes, value);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public List<string> OpenedTabs
|
|
|
|
{
|
2024-02-19 03:54:53 -08:00
|
|
|
get;
|
|
|
|
set;
|
|
|
|
} = new List<string>();
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public int LastActiveTabIdx
|
|
|
|
{
|
2024-03-06 03:46:19 -08:00
|
|
|
get;
|
|
|
|
set;
|
|
|
|
} = 0;
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public static void AddNode(RepositoryNode node, RepositoryNode to = null)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var collection = to == null ? _instance._repositoryNodes : to.SubNodes;
|
|
|
|
var list = new List<RepositoryNode>();
|
|
|
|
list.AddRange(collection);
|
|
|
|
list.Add(node);
|
2024-03-17 18:37:06 -07:00
|
|
|
list.Sort((l, r) =>
|
|
|
|
{
|
|
|
|
if (l.IsRepository != r.IsRepository)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
return l.IsRepository ? 1 : -1;
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
return l.Name.CompareTo(r.Name);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
collection.Clear();
|
2024-03-17 18:37:06 -07:00
|
|
|
foreach (var one in list)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
collection.Add(one);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public static RepositoryNode FindNode(string id)
|
|
|
|
{
|
2024-02-19 03:54:53 -08:00
|
|
|
return FindNodeRecursive(id, _instance.RepositoryNodes);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public static void MoveNode(RepositoryNode node, RepositoryNode to = null)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
if (to == null && _instance._repositoryNodes.Contains(node)) return;
|
|
|
|
if (to != null && to.SubNodes.Contains(node)) return;
|
|
|
|
|
|
|
|
RemoveNode(node);
|
|
|
|
AddNode(node, to);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public static void RemoveNode(RepositoryNode node)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
RemoveNodeRecursive(node, _instance._repositoryNodes);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public static Repository FindRepository(string path)
|
|
|
|
{
|
|
|
|
foreach (var repo in _instance.Repositories)
|
|
|
|
{
|
2024-02-29 21:40:12 -08:00
|
|
|
if (repo.FullPath == path) return repo;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public static Repository AddRepository(string rootDir, string gitDir)
|
|
|
|
{
|
2024-02-29 21:40:12 -08:00
|
|
|
var normalized = rootDir.Replace('\\', '/');
|
|
|
|
var repo = FindRepository(normalized);
|
2024-03-17 18:37:06 -07:00
|
|
|
if (repo != null)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
repo.GitDir = gitDir;
|
|
|
|
return repo;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
repo = new Repository()
|
|
|
|
{
|
2024-02-29 21:40:12 -08:00
|
|
|
FullPath = normalized,
|
2024-02-05 23:08:37 -08:00
|
|
|
GitDir = gitDir
|
|
|
|
};
|
|
|
|
|
|
|
|
_instance.Repositories.Add(repo);
|
|
|
|
return repo;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public static void Save()
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var dir = Path.GetDirectoryName(_savePath);
|
|
|
|
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
|
|
|
|
|
|
|
|
var data = JsonSerializer.Serialize(_instance, JsonSerializationCodeGen.Default.Preference);
|
|
|
|
File.WriteAllText(_savePath, data);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private static RepositoryNode FindNodeRecursive(string id, AvaloniaList<RepositoryNode> collection)
|
|
|
|
{
|
|
|
|
foreach (var node in collection)
|
|
|
|
{
|
2024-02-19 03:54:53 -08:00
|
|
|
if (node.Id == id) return node;
|
|
|
|
|
|
|
|
var sub = FindNodeRecursive(id, node.SubNodes);
|
|
|
|
if (sub != null) return sub;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private static bool RemoveNodeRecursive(RepositoryNode node, AvaloniaList<RepositoryNode> collection)
|
|
|
|
{
|
|
|
|
if (collection.Contains(node))
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
collection.Remove(node);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
foreach (RepositoryNode one in collection)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
if (RemoveNodeRecursive(node, one.SubNodes)) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Preference _instance = null;
|
|
|
|
private static readonly string _savePath = Path.Combine(
|
|
|
|
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
|
|
|
|
"SourceGit",
|
|
|
|
"preference.json");
|
|
|
|
|
|
|
|
private string _locale = "en_US";
|
|
|
|
private string _theme = "Default";
|
|
|
|
private int _maxHistoryCommits = 20000;
|
|
|
|
private bool _restoreTabs = false;
|
2024-02-29 22:09:39 -08:00
|
|
|
private bool _useFixedTabWidth = true;
|
2024-02-05 23:08:37 -08:00
|
|
|
private bool _useTwoColumnsLayoutInHistories = false;
|
|
|
|
private bool _useCombinedTextDiff = true;
|
|
|
|
|
|
|
|
private Models.ChangeViewMode _unstagedChangeViewMode = Models.ChangeViewMode.List;
|
|
|
|
private Models.ChangeViewMode _stagedChangeViewMode = Models.ChangeViewMode.List;
|
|
|
|
private Models.ChangeViewMode _commitChangeViewMode = Models.ChangeViewMode.List;
|
|
|
|
|
|
|
|
private string _gitDefaultCloneDir = string.Empty;
|
|
|
|
|
|
|
|
private int _externalMergeToolType = 0;
|
|
|
|
private string _externalMergeToolPath = string.Empty;
|
|
|
|
private string _externalMergeToolCmd = string.Empty;
|
|
|
|
private string _externalMergeToolDiffCmd = string.Empty;
|
|
|
|
|
|
|
|
private AvaloniaList<RepositoryNode> _repositoryNodes = new AvaloniaList<RepositoryNode>();
|
|
|
|
}
|
|
|
|
|
|
|
|
[JsonSourceGenerationOptions(WriteIndented = true, IgnoreReadOnlyFields = true, IgnoreReadOnlyProperties = true)]
|
|
|
|
[JsonSerializable(typeof(Preference))]
|
|
|
|
internal partial class JsonSerializationCodeGen : JsonSerializerContext { }
|
|
|
|
}
|