5 public partial class TabsView
7 private string[] commandMenuNames = Array.Empty<
string>();
8 private bool commandMenuBuilt;
10 private void InitializeCommandMenu()
12 commandToolStripMenuItem.DropDownOpening += (_, _) => RefreshCommandMenu();
16 private void RefreshCommandMenu()
20 IEnumerable<string> names = Fiji.Macro.Commands.Values
21 .Where(command => command !=
null)
22 .Select(command => command.Name);
23 if (Fiji.Macro.Commands.Count == 0)
24 names = ReadBundledCommandNames();
26 string[] commands = names.Where(name => !
string.IsNullOrWhiteSpace(name))
27 .Distinct(StringComparer.Ordinal)
28 .OrderBy(name => name, StringComparer.OrdinalIgnoreCase)
29 .ThenBy(name => name, StringComparer.Ordinal)
31 if (commandMenuBuilt && commandMenuNames.SequenceEqual(commands, StringComparer.Ordinal))
33 commandMenuBuilt =
true;
34 commandMenuNames = commands;
35 commandToolStripMenuItem.DropDownItems.Clear();
37 var groups =
new Dictionary<char, ToolStripMenuItem>();
38 foreach (
char letter
in "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
40 var group =
new ToolStripMenuItem(letter.ToString());
41 groups.Add(letter, group);
42 commandToolStripMenuItem.DropDownItems.Add(group);
44 ToolStripMenuItem? numbers =
null, other =
null;
45 foreach (
string command
in commands)
47 char first =
char.ToUpperInvariant(command[0]);
48 ToolStripMenuItem group;
49 if (groups.TryGetValue(first, out var letterGroup))
51 else if (
char.IsDigit(first))
55 numbers =
new ToolStripMenuItem(
"0–9");
56 commandToolStripMenuItem.DropDownItems.Insert(0, numbers);
64 other =
new ToolStripMenuItem(
"Other");
65 commandToolStripMenuItem.DropDownItems.Add(other);
69 var item =
new ToolStripMenuItem(command.Replace(
"&",
"&&")) { Tag = command };
70 item.Click += MenuItem_Click;
71 group.DropDownItems.Add(item);
75 private static IEnumerable<string> ReadBundledCommandNames()
77 string path = Path.Combine(AppContext.BaseDirectory,
"macro-commands.csv");
78 if (!File.Exists(path))
79 path = Path.Combine(Path.GetDirectoryName(typeof(
TabsView).Assembly.Location) ??
"",
"macro-commands.csv");
80 if (!File.Exists(path))
81 return Array.Empty<
string>();
86 return File.ReadLines(path).Skip(1)
87 .Select(line => line.IndexOf(
',') is
int comma && comma > 0 ? line[..comma].Trim() :
"")
88 .Where(name => name.Length > 0).ToArray();
90 catch (IOException ex)
92 System.Diagnostics.Trace.WriteLine($
"Could not load Fiji commands: {ex.Message}");
93 return Array.Empty<
string>();
95 catch (UnauthorizedAccessException ex)
97 System.Diagnostics.Trace.WriteLine($
"Could not load Fiji commands: {ex.Message}");
98 return Array.Empty<
string>();