BioCore  5.0.0
A Windows .NET microscopy library and application for viewing, editing, and annotating images.
Loading...
Searching...
No Matches
TabsView.Commands.cs
Go to the documentation of this file.
1using BioLib;
2
3namespace BioCore
4{
5 public partial class TabsView
6 {
7 private string[] commandMenuNames = Array.Empty<string>();
8 private bool commandMenuBuilt;
9
10 private void InitializeCommandMenu()
11 {
12 commandToolStripMenuItem.DropDownOpening += (_, _) => RefreshCommandMenu();
13 RefreshCommandMenu();
14 }
15
16 private void RefreshCommandMenu()
17 {
18 // Fiji can initialize after this window was constructed. Its live
19 // command table reflects installed plugins when it is available.
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();
25
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)
30 .ToArray();
31 if (commandMenuBuilt && commandMenuNames.SequenceEqual(commands, StringComparer.Ordinal))
32 return;
33 commandMenuBuilt = true;
34 commandMenuNames = commands;
35 commandToolStripMenuItem.DropDownItems.Clear();
36
37 var groups = new Dictionary<char, ToolStripMenuItem>();
38 foreach (char letter in "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
39 {
40 var group = new ToolStripMenuItem(letter.ToString());
41 groups.Add(letter, group);
42 commandToolStripMenuItem.DropDownItems.Add(group);
43 }
44 ToolStripMenuItem? numbers = null, other = null;
45 foreach (string command in commands)
46 {
47 char first = char.ToUpperInvariant(command[0]);
48 ToolStripMenuItem group;
49 if (groups.TryGetValue(first, out var letterGroup))
50 group = letterGroup;
51 else if (char.IsDigit(first))
52 {
53 if (numbers == null)
54 {
55 numbers = new ToolStripMenuItem("0–9");
56 commandToolStripMenuItem.DropDownItems.Insert(0, numbers);
57 }
58 group = numbers;
59 }
60 else
61 {
62 if (other == null)
63 {
64 other = new ToolStripMenuItem("Other");
65 commandToolStripMenuItem.DropDownItems.Add(other);
66 }
67 group = other;
68 }
69 var item = new ToolStripMenuItem(command.Replace("&", "&&")) { Tag = command };
70 item.Click += MenuItem_Click;
71 group.DropDownItems.Add(item);
72 }
73 }
74
75 private static IEnumerable<string> ReadBundledCommandNames()
76 {
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>();
82 try
83 {
84 // The bundled file has an unquoted command name in the first
85 // column; plugin arguments may contain commas in later columns.
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();
89 }
90 catch (IOException ex)
91 {
92 System.Diagnostics.Trace.WriteLine($"Could not load Fiji commands: {ex.Message}");
93 return Array.Empty<string>();
94 }
95 catch (UnauthorizedAccessException ex)
96 {
97 System.Diagnostics.Trace.WriteLine($"Could not load Fiji commands: {ex.Message}");
98 return Array.Empty<string>();
99 }
100 }
101 }
102}