Skip to content

Commit

Permalink
Fixed issues with building quick launch file menu
Browse files Browse the repository at this point in the history
  • Loading branch information
m00ndark committed Jan 14, 2021
1 parent 154c6f5 commit f6005f1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 33 deletions.
81 changes: 50 additions & 31 deletions src/GitBranchView/Controls/FolderEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -136,49 +138,66 @@ public void ResetContextMenu()
contextMenuStrip.Items.Clear();

if (!string.IsNullOrWhiteSpace(Settings.Default.QuickLaunchFilesFilter))
{
List<IGrouping<string, string>> 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<string, string> quickLaunchPaths in groupedQuickLaunchPaths)
List<IGrouping<string, string>> 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<string, string> quickLaunchPaths in groupedQuickLaunchPaths)
{
menuItems = Settings.Default.QuickLaunchFilesGrouping == QuickLaunchFilesGrouping.ByExtension
? contextMenuStrip.AddItem<ToolStripMenuItem>($"Extension: {quickLaunchPaths.Key.Replace(".", string.Empty)}").DropDownItems
: contextMenuStrip.AddItem<ToolStripMenuItem>(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<ToolStripMenuItem>($"Extension: {quickLaunchPaths.Key.Replace(".", string.Empty)}").DropDownItems
: contextMenuStrip.AddItem<ToolStripMenuItem>(quickLaunchPaths.Key).DropDownItems;
}

ToolStripMenuItem item = menuItems.Add<ToolStripMenuItem>(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<ToolStripMenuItem>(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<ToolStripMenuItem>(
$"-- Found {itemCount} matching quick launch files, limiting to first {MAX_QUICK_LAUNCH_ITEMS} --").Enabled = false;
}
}

contextMenuStrip.AddItem<ToolStripSeparator>();
}

}
catch (Exception ex)
{
Logger.Add("Error while building quick launch files context menu", ex);
contextMenuStrip.AddItem<ToolStripMenuItem>($"Error: {ex.Message}").Enabled = false;
contextMenuStrip.AddItem<ToolStripSeparator>();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/GitBranchView/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("")]

Expand All @@ -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.*")]

0 comments on commit f6005f1

Please sign in to comment.