From faf2c39056c61f33cb2607214f891466a5c67c1e Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 20 May 2024 17:36:43 +0800 Subject: [PATCH] refactor: rewrite `Preference.FindOrAddNodeByRepositoryPath` * prevent moving node while opening repository directly from commandline * supports to set parent node while dropping folder to initialize repository --- src/ViewModels/Clone.cs | 2 +- src/ViewModels/Init.cs | 11 +++++++---- src/ViewModels/Launcher.cs | 2 +- src/ViewModels/Preference.cs | 4 ++-- src/ViewModels/Welcome.cs | 4 ++-- src/Views/Welcome.axaml.cs | 4 ++-- 6 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/ViewModels/Clone.cs b/src/ViewModels/Clone.cs index 103f0505..1e9bcb63 100644 --- a/src/ViewModels/Clone.cs +++ b/src/ViewModels/Clone.cs @@ -113,7 +113,7 @@ namespace SourceGit.ViewModels CallUIThread(() => { var repo = Preference.AddRepository(path, Path.Combine(path, ".git")); - var node = Preference.FindOrAddNodeByRepositoryPath(repo.FullPath, null); + var node = Preference.FindOrAddNodeByRepositoryPath(repo.FullPath, null, true); _launcher.OpenRepositoryInTab(node, _page); }); diff --git a/src/ViewModels/Init.cs b/src/ViewModels/Init.cs index b2c48fc7..a4e27341 100644 --- a/src/ViewModels/Init.cs +++ b/src/ViewModels/Init.cs @@ -11,9 +11,11 @@ namespace SourceGit.ViewModels set => SetProperty(ref _targetPath, value); } - public Init(string path) + public Init(string path, RepositoryNode parent) { - TargetPath = path; + _targetPath = path; + _parentNode = parent; + View = new Views.Init() { DataContext = this }; } @@ -31,13 +33,14 @@ namespace SourceGit.ViewModels CallUIThread(() => { var repo = Preference.AddRepository(_targetPath, gitDir); - Preference.FindOrAddNodeByRepositoryPath(repo.FullPath, null); + Preference.FindOrAddNodeByRepositoryPath(repo.FullPath, _parentNode, true); }); return true; }); } - private string _targetPath; + private string _targetPath = string.Empty; + private RepositoryNode _parentNode = null; } } diff --git a/src/ViewModels/Launcher.cs b/src/ViewModels/Launcher.cs index bf3ff28d..5bdd3c18 100644 --- a/src/ViewModels/Launcher.cs +++ b/src/ViewModels/Launcher.cs @@ -49,7 +49,7 @@ namespace SourceGit.ViewModels var gitDir = new Commands.QueryGitDir(root).Result(); var repo = Preference.AddRepository(root, gitDir); - var node = Preference.FindOrAddNodeByRepositoryPath(repo.FullPath, null); + var node = Preference.FindOrAddNodeByRepositoryPath(repo.FullPath, null, false); OpenRepositoryInTab(node, null); } else if (Preference.Instance.RestoreTabs) diff --git a/src/ViewModels/Preference.cs b/src/ViewModels/Preference.cs index 85f26611..ee387e35 100644 --- a/src/ViewModels/Preference.cs +++ b/src/ViewModels/Preference.cs @@ -363,7 +363,7 @@ namespace SourceGit.ViewModels return FindNodeRecursive(id, _instance.RepositoryNodes); } - public static RepositoryNode FindOrAddNodeByRepositoryPath(string repo, RepositoryNode parent) + public static RepositoryNode FindOrAddNodeByRepositoryPath(string repo, RepositoryNode parent, bool shouldMoveNode) { var node = FindNodeRecursive(repo, _instance.RepositoryNodes); if (node == null) @@ -378,7 +378,7 @@ namespace SourceGit.ViewModels AddNode(node, parent); } - else + else if (shouldMoveNode) { MoveNode(node, parent); } diff --git a/src/ViewModels/Welcome.cs b/src/ViewModels/Welcome.cs index d4569d05..45bf7614 100644 --- a/src/ViewModels/Welcome.cs +++ b/src/ViewModels/Welcome.cs @@ -26,7 +26,7 @@ namespace SourceGit.ViewModels } } - public void InitRepository(string path) + public void InitRepository(string path, RepositoryNode parent) { if (!Preference.Instance.IsGitConfigured) { @@ -36,7 +36,7 @@ namespace SourceGit.ViewModels if (PopupHost.CanCreatePopup()) { - PopupHost.ShowPopup(new Init(path)); + PopupHost.ShowPopup(new Init(path, parent)); } } diff --git a/src/Views/Welcome.axaml.cs b/src/Views/Welcome.axaml.cs index f0e3a1fc..d57c50f8 100644 --- a/src/Views/Welcome.axaml.cs +++ b/src/Views/Welcome.axaml.cs @@ -248,7 +248,7 @@ namespace SourceGit.Views { Dispatcher.UIThread.Invoke(() => { - (DataContext as ViewModels.Welcome).InitRepository(path); + (DataContext as ViewModels.Welcome).InitRepository(path, parent); }); return; } @@ -257,7 +257,7 @@ namespace SourceGit.Views Dispatcher.UIThread.Invoke(() => { var repo = ViewModels.Preference.AddRepository(root, gitDir); - var node = ViewModels.Preference.FindOrAddNodeByRepositoryPath(repo.FullPath, parent); + var node = ViewModels.Preference.FindOrAddNodeByRepositoryPath(repo.FullPath, parent, true); launcher.OpenRepositoryInTab(node, page); }); });