From f6005f1481f58695e33296540aa4376fd3b38402 Mon Sep 17 00:00:00 2001 From: m00ndark Date: Thu, 14 Jan 2021 15:45:33 +0100 Subject: [PATCH] Fixed issues with building quick launch file menu --- src/GitBranchView/Controls/FolderEntry.cs | 81 ++++++++++++-------- src/GitBranchView/Properties/AssemblyInfo.cs | 4 +- 2 files changed, 52 insertions(+), 33 deletions(-) diff --git a/src/GitBranchView/Controls/FolderEntry.cs b/src/GitBranchView/Controls/FolderEntry.cs index 0c85464..e85cbf5 100644 --- a/src/GitBranchView/Controls/FolderEntry.cs +++ b/src/GitBranchView/Controls/FolderEntry.cs @@ -12,11 +12,13 @@ using GitBranchView.Forms; using GitBranchView.Model; using ToolComponents.Core.Extensions; +using ToolComponents.Core.Logging; namespace GitBranchView.Controls { public partial class FolderEntry : UserControl { + private const int MAX_QUICK_LAUNCH_ITEMS = 50; private const string MENU_ITEM_REFRESH = "Refresh"; public event EventHandler WidthChanged; @@ -136,49 +138,66 @@ public void ResetContextMenu() contextMenuStrip.Items.Clear(); if (!string.IsNullOrWhiteSpace(Settings.Default.QuickLaunchFilesFilter)) - { - List> groupedQuickLaunchPaths = Directory.EnumerateFiles(Path, "*", SearchOption.AllDirectories) - .Where(filePath => Regex.IsMatch(filePath.RelativeTo(Path), Settings.Default.QuickLaunchFilesFilter, RegexOptions.IgnoreCase)) - .GroupBy(filePath => Settings.Default.QuickLaunchFilesGrouping == QuickLaunchFilesGrouping.ByExtension - ? System.IO.Path.GetExtension(filePath) - : Settings.Default.QuickLaunchFilesGrouping == QuickLaunchFilesGrouping.ByPath - ? System.IO.Path.GetDirectoryName(filePath)?.RelativeTo(Path) ?? string.Empty - : string.Empty) - .ToList(); - - if (groupedQuickLaunchPaths.Any()) + { + try { - Settings.Default.SelectedQuickLaunchFiles.TryGetValue(Path.ToLower(), out string selectedQuickLaunchPath); - - foreach (IGrouping quickLaunchPaths in groupedQuickLaunchPaths) + List> groupedQuickLaunchPaths = Directory.EnumerateFiles(Path, "*", SearchOption.AllDirectories) + .Where(filePath => Regex.IsMatch(filePath.RelativeTo(Path), Settings.Default.QuickLaunchFilesFilter, RegexOptions.IgnoreCase)) + .GroupBy(filePath => Settings.Default.QuickLaunchFilesGrouping == QuickLaunchFilesGrouping.ByExtension + ? System.IO.Path.GetExtension(filePath) + : Settings.Default.QuickLaunchFilesGrouping == QuickLaunchFilesGrouping.ByPath + ? System.IO.Path.GetDirectoryName(filePath)?.RelativeTo(Path) ?? string.Empty + : string.Empty) + .ToList(); + + if (groupedQuickLaunchPaths.Any()) { - ToolStripItemCollection menuItems = contextMenuStrip.Items; + Settings.Default.SelectedQuickLaunchFiles.TryGetValue(Path.ToLower(), out string selectedQuickLaunchPath); - if (groupedQuickLaunchPaths.Count > 1 - && Settings.Default.QuickLaunchFilesGrouping != QuickLaunchFilesGrouping.None - && quickLaunchPaths.Key != Extensions.ROOT_PATH_RELATIVE) + foreach (IGrouping quickLaunchPaths in groupedQuickLaunchPaths) { - menuItems = Settings.Default.QuickLaunchFilesGrouping == QuickLaunchFilesGrouping.ByExtension - ? contextMenuStrip.AddItem($"Extension: {quickLaunchPaths.Key.Replace(".", string.Empty)}").DropDownItems - : contextMenuStrip.AddItem(quickLaunchPaths.Key).DropDownItems; - } + ToolStripItemCollection menuItems = contextMenuStrip.Items; - foreach (string quickLaunchPath in quickLaunchPaths) - { - string menuItemText = groupedQuickLaunchPaths.Count > 1 && Settings.Default.QuickLaunchFilesGrouping == QuickLaunchFilesGrouping.ByPath - ? System.IO.Path.GetFileName(quickLaunchPath) - : quickLaunchPath.RelativeTo(Path); + if (groupedQuickLaunchPaths.Count > 1 + && Settings.Default.QuickLaunchFilesGrouping != QuickLaunchFilesGrouping.None + && quickLaunchPaths.Key != Extensions.ROOT_PATH_RELATIVE) + { + menuItems = Settings.Default.QuickLaunchFilesGrouping == QuickLaunchFilesGrouping.ByExtension + ? contextMenuStrip.AddItem($"Extension: {quickLaunchPaths.Key.Replace(".", string.Empty)}").DropDownItems + : contextMenuStrip.AddItem(quickLaunchPaths.Key).DropDownItems; + } - ToolStripMenuItem item = menuItems.Add(menuItemText, QuickLaunchPathMenuItem_Click, quickLaunchPath); + foreach (string quickLaunchPath in quickLaunchPaths.Take(MAX_QUICK_LAUNCH_ITEMS)) + { + string menuItemText = groupedQuickLaunchPaths.Count > 1 && Settings.Default.QuickLaunchFilesGrouping == QuickLaunchFilesGrouping.ByPath + ? System.IO.Path.GetFileName(quickLaunchPath) + : quickLaunchPath.RelativeTo(Path); - if (Settings.Default.RepositoryLinkBehavior == RepositoryLinkBehavior.LaunchSelectedQuickLaunchFile - && (selectedQuickLaunchPath?.Equals(quickLaunchPath, StringComparison.InvariantCultureIgnoreCase) ?? false)) + ToolStripMenuItem item = menuItems.Add(menuItemText, QuickLaunchPathMenuItem_Click, quickLaunchPath); + + if (Settings.Default.RepositoryLinkBehavior == RepositoryLinkBehavior.LaunchSelectedQuickLaunchFile + && (selectedQuickLaunchPath?.Equals(quickLaunchPath, StringComparison.InvariantCultureIgnoreCase) ?? false)) + { + item.Checked = true; + } + } + + int itemCount = quickLaunchPaths.Count(); + if (itemCount > MAX_QUICK_LAUNCH_ITEMS) { - item.Checked = true; + contextMenuStrip.AddItem( + $"-- Found {itemCount} matching quick launch files, limiting to first {MAX_QUICK_LAUNCH_ITEMS} --").Enabled = false; } } + + contextMenuStrip.AddItem(); } + } + catch (Exception ex) + { + Logger.Add("Error while building quick launch files context menu", ex); + contextMenuStrip.AddItem($"Error: {ex.Message}").Enabled = false; contextMenuStrip.AddItem(); } } diff --git a/src/GitBranchView/Properties/AssemblyInfo.cs b/src/GitBranchView/Properties/AssemblyInfo.cs index 4cf672b..3a8cff1 100644 --- a/src/GitBranchView/Properties/AssemblyInfo.cs +++ b/src/GitBranchView/Properties/AssemblyInfo.cs @@ -9,7 +9,7 @@ [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("Git Branch View")] -[assembly: AssemblyCopyright("Copyright © Mattias Wijkström 2019")] +[assembly: AssemblyCopyright("Copyright © Mattias Wijkström 2021")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] @@ -31,4 +31,4 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("2.2.*")] +[assembly: AssemblyVersion("2.3.*")]