From 8bd5bd864e7b90cf8f2539b55fb72838bd446e69 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 21 Nov 2024 20:50:51 +0800 Subject: [PATCH] feature: add context menu to switch histories filter mode to selected commit Signed-off-by: leo --- src/Resources/Locales/en_US.axaml | 1 + src/Resources/Locales/zh_CN.axaml | 1 + src/Resources/Locales/zh_TW.axaml | 1 + src/ViewModels/Histories.cs | 111 ++++++++++++++++++++++++++++++ src/ViewModels/Repository.cs | 7 ++ 5 files changed, 121 insertions(+) diff --git a/src/Resources/Locales/en_US.axaml b/src/Resources/Locales/en_US.axaml index 462cdf90..705d7c6f 100644 --- a/src/Resources/Locales/en_US.axaml +++ b/src/Resources/Locales/en_US.axaml @@ -542,6 +542,7 @@ Enable '--reflog' Option Open in File Browser Search Branches/Tags/Submodules + Visibility in Graph Unset Hide in commit graph Filter in commit graph diff --git a/src/Resources/Locales/zh_CN.axaml b/src/Resources/Locales/zh_CN.axaml index 0c0ccf14..21837119 100644 --- a/src/Resources/Locales/zh_CN.axaml +++ b/src/Resources/Locales/zh_CN.axaml @@ -546,6 +546,7 @@ 启用 --reflog 选项 在文件浏览器中打开 快速查找分支/标签/子模块 + 设置在列表中的可见性 不指定 在提交列表中隐藏 使用其对提交列表过滤 diff --git a/src/Resources/Locales/zh_TW.axaml b/src/Resources/Locales/zh_TW.axaml index 31015ae7..59b0afe0 100644 --- a/src/Resources/Locales/zh_TW.axaml +++ b/src/Resources/Locales/zh_TW.axaml @@ -545,6 +545,7 @@ 啟用 [--reflog] 選項 在檔案瀏覽器中開啟 快速搜尋分支/標籤/子模組 + 設定在列表中的可視性 不指定 在提交清單中隱藏 使用其來篩選提交清單 diff --git a/src/ViewModels/Histories.cs b/src/ViewModels/Histories.cs index 2f597849..92524f62 100644 --- a/src/ViewModels/Histories.cs +++ b/src/ViewModels/Histories.cs @@ -703,12 +703,117 @@ namespace SourceGit.ViewModels return menu; } + private Models.FilterMode GetFilterMode(string pattern) + { + foreach (var filter in _repo.Settings.HistoriesFilters) + { + if (filter.Pattern.Equals(pattern, StringComparison.Ordinal)) + return filter.Mode; + } + + return Models.FilterMode.None; + } + + private void FillBranchVisibilityMenu(MenuItem submenu, Models.Branch branch) + { + var visibility = new MenuItem(); + visibility.Icon = App.CreateMenuIcon("Icons.Eye"); + visibility.Header = App.Text("Repository.FilterCommits"); + + var exclude = new MenuItem(); + exclude.Icon = App.CreateMenuIcon("Icons.EyeClose"); + exclude.Header = App.Text("Repository.FilterCommits.Exclude"); + exclude.Click += (_, e) => + { + _repo.SetBranchFilterMode(branch, Models.FilterMode.Excluded); + e.Handled = true; + }; + + var filterMode = GetFilterMode(branch.FullName); + if (filterMode == Models.FilterMode.None) + { + var include = new MenuItem(); + include.Icon = App.CreateMenuIcon("Icons.Filter"); + include.Header = App.Text("Repository.FilterCommits.Include"); + include.Click += (_, e) => + { + _repo.SetBranchFilterMode(branch, Models.FilterMode.Included); + e.Handled = true; + }; + visibility.Items.Add(include); + visibility.Items.Add(exclude); + } + else + { + var unset = new MenuItem(); + unset.Header = App.Text("Repository.FilterCommits.Default"); + unset.Click += (_, e) => + { + _repo.SetBranchFilterMode(branch, Models.FilterMode.None); + e.Handled = true; + }; + visibility.Items.Add(exclude); + visibility.Items.Add(unset); + } + + submenu.Items.Add(visibility); + submenu.Items.Add(new MenuItem() { Header = "-" }); + } + + private void FillTagVisibilityMenu(MenuItem submenu, Models.Tag tag) + { + var visibility = new MenuItem(); + visibility.Icon = App.CreateMenuIcon("Icons.Eye"); + visibility.Header = App.Text("Repository.FilterCommits"); + + var exclude = new MenuItem(); + exclude.Icon = App.CreateMenuIcon("Icons.EyeClose"); + exclude.Header = App.Text("Repository.FilterCommits.Exclude"); + exclude.Click += (_, e) => + { + _repo.SetTagFilterMode(tag, Models.FilterMode.Excluded); + e.Handled = true; + }; + + var filterMode = GetFilterMode(tag.Name); + if (filterMode == Models.FilterMode.None) + { + var include = new MenuItem(); + include.Icon = App.CreateMenuIcon("Icons.Filter"); + include.Header = App.Text("Repository.FilterCommits.Include"); + include.Click += (_, e) => + { + _repo.SetTagFilterMode(tag, Models.FilterMode.Included); + e.Handled = true; + }; + visibility.Items.Add(include); + visibility.Items.Add(exclude); + } + else + { + var unset = new MenuItem(); + unset.Header = App.Text("Repository.FilterCommits.Default"); + unset.Click += (_, e) => + { + _repo.SetTagFilterMode(tag, Models.FilterMode.None); + e.Handled = true; + }; + visibility.Items.Add(exclude); + visibility.Items.Add(unset); + } + + submenu.Items.Add(visibility); + submenu.Items.Add(new MenuItem() { Header = "-" }); + } + private void FillCurrentBranchMenu(ContextMenu menu, Models.Branch current) { var submenu = new MenuItem(); submenu.Icon = App.CreateMenuIcon("Icons.Branch"); submenu.Header = current.Name; + FillBranchVisibilityMenu(submenu, current); + if (!string.IsNullOrEmpty(current.Upstream)) { var upstream = current.Upstream.Substring(13); @@ -786,6 +891,8 @@ namespace SourceGit.ViewModels submenu.Icon = App.CreateMenuIcon("Icons.Branch"); submenu.Header = branch.Name; + FillBranchVisibilityMenu(submenu, branch); + var checkout = new MenuItem(); checkout.Header = new Views.NameHighlightedTextBlock("BranchCM.Checkout", branch.Name); checkout.Icon = App.CreateMenuIcon("Icons.Check"); @@ -858,6 +965,8 @@ namespace SourceGit.ViewModels submenu.Icon = App.CreateMenuIcon("Icons.Branch"); submenu.Header = name; + FillBranchVisibilityMenu(submenu, branch); + var checkout = new MenuItem(); checkout.Header = new Views.NameHighlightedTextBlock("BranchCM.Checkout", name); checkout.Icon = App.CreateMenuIcon("Icons.Check"); @@ -903,6 +1012,8 @@ namespace SourceGit.ViewModels submenu.Icon = App.CreateMenuIcon("Icons.Tag"); submenu.MinWidth = 200; + FillTagVisibilityMenu(submenu, tag); + var push = new MenuItem(); push.Header = new Views.NameHighlightedTextBlock("TagCM.Push", tag.Name); push.Icon = App.CreateMenuIcon("Icons.Push"); diff --git a/src/ViewModels/Repository.cs b/src/ViewModels/Repository.cs index ab7924d5..3182ab6f 100644 --- a/src/ViewModels/Repository.cs +++ b/src/ViewModels/Repository.cs @@ -707,6 +707,13 @@ namespace SourceGit.ViewModels RefreshHistoriesFilters(); } + public void SetBranchFilterMode(Models.Branch branch, Models.FilterMode mode) + { + var node = FindBranchNode(branch.IsLocal ? _localBranchTrees : _remoteBranchTrees, branch.FullName); + if (node != null) + SetBranchFilterMode(node, mode); + } + public void SetBranchFilterMode(BranchTreeNode node, Models.FilterMode mode) { var isLocal = node.Path.StartsWith("refs/heads/", StringComparison.Ordinal);