diff --git a/src/ViewModels/Launcher.cs b/src/ViewModels/Launcher.cs index 38d684d4..e53171f6 100644 --- a/src/ViewModels/Launcher.cs +++ b/src/ViewModels/Launcher.cs @@ -21,6 +21,27 @@ namespace SourceGit.ViewModels { public Launcher() { Pages = new AvaloniaList(); AddNewTab(); + + if (Preference.Instance.RestoreTabs) { + foreach (var id in Preference.Instance.OpenedTabs) { + var node = Preference.FindNode(id); + if (node == null) continue; + + OpenRepositoryInTab(node, null); + } + } + } + + public void Quit() { + Preference.Instance.OpenedTabs.Clear(); + + if (Preference.Instance.RestoreTabs) { + foreach (var page in Pages) { + if (page.Node.IsRepository) Preference.Instance.OpenedTabs.Add(page.Node.Id); + } + } + + Preference.Save(); } public void AddNewTab() { @@ -130,6 +151,7 @@ namespace SourceGit.ViewModels { page = new LauncherPage(node, repo); Pages.Add(page); } else { + page = ActivePage; page.Node = node; page.View = new Views.Repository() { DataContext = repo }; } diff --git a/src/ViewModels/Preference.cs b/src/ViewModels/Preference.cs index a09b6530..d80d1fb3 100644 --- a/src/ViewModels/Preference.cs +++ b/src/ViewModels/Preference.cs @@ -158,6 +158,11 @@ namespace SourceGit.ViewModels { set => SetProperty(ref _repositoryNodes, value); } + public List OpenedTabs { + get; + set; + } = new List(); + public static void AddNode(RepositoryNode node, RepositoryNode to = null) { var collection = to == null ? _instance._repositoryNodes : to.SubNodes; var list = new List(); @@ -177,6 +182,10 @@ namespace SourceGit.ViewModels { } } + public static RepositoryNode FindNode(string id) { + return FindNodeRecursive(id, _instance.RepositoryNodes); + } + public static void MoveNode(RepositoryNode node, RepositoryNode to = null) { if (to == null && _instance._repositoryNodes.Contains(node)) return; if (to != null && to.SubNodes.Contains(node)) return; @@ -222,6 +231,17 @@ namespace SourceGit.ViewModels { File.WriteAllText(_savePath, data); } + private static RepositoryNode FindNodeRecursive(string id, AvaloniaList collection) { + foreach (var node in collection) { + if (node.Id == id) return node; + + var sub = FindNodeRecursive(id, node.SubNodes); + if (sub != null) return sub; + } + + return null; + } + private static bool RemoveNodeRecursive(RepositoryNode node, AvaloniaList collection) { if (collection.Contains(node)) { collection.Remove(node); diff --git a/src/Views/Launcher.axaml.cs b/src/Views/Launcher.axaml.cs index 9ff8841a..a3b6501d 100644 --- a/src/Views/Launcher.axaml.cs +++ b/src/Views/Launcher.axaml.cs @@ -43,7 +43,9 @@ namespace SourceGit.Views { } protected override void OnClosing(WindowClosingEventArgs e) { - ViewModels.Preference.Save(); + var vm = DataContext as ViewModels.Launcher; + vm.Quit(); + base.OnClosing(e); }