-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAvailableCommandsForm.cs
More file actions
336 lines (306 loc) · 14.9 KB
/
AvailableCommandsForm.cs
File metadata and controls
336 lines (306 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace DictationBoxMSP
{
public class AvailableCommandsForm : Form
{
private TextBox txtSearch = null!;
private ListBox lstResults = null!;
private Label lblHint = null!;
public AvailableCommandsForm()
{
InitializeComponents();
ApplySharedStyles();
Load += AvailableCommandsForm_Load;
Shown += AvailableCommandsForm_Shown;
}
private void InitializeComponents()
{
this.txtSearch = new TextBox() { Dock = DockStyle.Top, Margin = new Padding(8), Height = (int)(28 * 1.2) };
this.lstResults = new ListBox() { Dock = DockStyle.Fill };
// Move the hint label to the bottom so it doesn't overlap top items
this.lblHint = new Label() { Dock = DockStyle.Bottom, Height = 22, Text = "Start typing to filter commands...", TextAlign = System.Drawing.ContentAlignment.MiddleLeft };
// Add controls in an order that yields predictable docking without needing BringToFront
// Add hint (bottom), then list (fill), then search (top)
this.Controls.Add(lblHint);
this.Controls.Add(lstResults);
this.Controls.Add(txtSearch);
this.Text = "Available Commands";
this.StartPosition = FormStartPosition.CenterScreen;
// Make the form bigger for easier reading
this.Size = new Size(900, 700);
txtSearch.TextChanged += TxtSearch_TextChanged;
lstResults.DoubleClick += LstResults_DoubleClick;
// Ensure the textbox can receive focus/tab stop
txtSearch.TabIndex = 0;
txtSearch.TabStop = true;
txtSearch.ReadOnly = false;
txtSearch.Enabled = true;
}
private void ApplySharedStyles()
{
this.BackColor = DisplayMessage.SharedBackColor;
this.ForeColor = DisplayMessage.SharedForeColor;
// Increase font size for accessibility (40% larger than shared font)
var sharedFont = DisplayMessage.SharedFont;
float baseSize = SystemFonts.MessageBoxFont?.Size ?? 12f;
FontFamily fontFamily = SystemFonts.MessageBoxFont?.FontFamily ?? FontFamily.GenericSansSerif;
FontStyle fontStyle = SystemFonts.MessageBoxFont?.Style ?? FontStyle.Regular;
if (sharedFont != null)
{
baseSize = sharedFont.Size;
fontFamily = sharedFont.FontFamily ?? fontFamily;
fontStyle = sharedFont.Style;
}
float largerSize = Math.Max(baseSize * 1.4f, baseSize + 4f);
var largerFont = new Font(fontFamily, largerSize, fontStyle);
this.Font = largerFont;
if (txtSearch != null)
{
txtSearch.BackColor = ControlPaint.Dark(DisplayMessage.SharedBackColor);
txtSearch.ForeColor = DisplayMessage.SharedForeColor;
txtSearch.Font = largerFont;
}
if (lstResults != null)
{
lstResults.BackColor = DisplayMessage.SharedBackColor;
lstResults.ForeColor = DisplayMessage.SharedForeColor;
lstResults.Font = largerFont;
// Avoid the ListBox clipping its first/last item by disabling IntegralHeight
lstResults.IntegralHeight = false;
// Use normal drawing mode to avoid owner-draw inconsistencies
lstResults.DrawMode = DrawMode.Normal;
// Set a generous item height based on the chosen font to avoid clipping
try { lstResults.ItemHeight = (int)Math.Ceiling(largerFont.GetHeight()) + 8; } catch { }
lstResults.BorderStyle = BorderStyle.FixedSingle;
}
if (lblHint != null)
{
lblHint.BackColor = DisplayMessage.SharedBackColor;
lblHint.ForeColor = Color.LightGray;
lblHint.Font = largerFont;
}
}
private void AvailableCommandsForm_Load(object? sender, EventArgs e)
{
// Start with no results for performance; user begins typing to populate.
lstResults.Items.Clear();
// No BringToFront calls — control docking/order set in InitializeComponents
}
private void AvailableCommandsForm_Shown(object? sender, EventArgs e)
{
// Give keyboard focus to the search textbox when the form appears
try
{
this.ActiveControl = txtSearch;
txtSearch.Focus();
txtSearch.Select();
// Populate with a random sample of commands so the user sees examples immediately
PopulateRandomSampleItems(20);
}
catch { }
}
private void PopulateRandomSampleItems(int sampleSize)
{
try
{
var items = new List<(string Command, string Description)>();
try { items.AddRange(NaturalCommands.NaturalLanguageInterpreter.AvailableCommands); } catch { }
try { items.AddRange(NaturalCommands.NaturalLanguageInterpreter.VisualStudioCommands); } catch { }
try { items.AddRange(NaturalCommands.NaturalLanguageInterpreter.VSCodeCommands); } catch { }
try { items.AddRange(NaturalCommands.CommandDefinitions.WindowsTerminalCommands); } catch { }
try { items.AddRange(NaturalCommands.CommandDefinitions.WindowsExplorerCommands); } catch { }
// Deduplicate by Command+Description
var distinct = items
.Where(i => !string.IsNullOrEmpty(i.Command))
.GroupBy(i => (i.Command ?? string.Empty) + "|" + (i.Description ?? string.Empty))
.Select(g => g.First())
.ToList();
if (distinct.Count == 0)
{
lstResults.Items.Clear();
return;
}
var rng = new Random();
var sample = distinct.OrderBy(_ => rng.Next()).Take(sampleSize).Select(i =>
{
try
{
var emoji = NaturalCommands.EmojiManager.GetCommandEmoji(i.Command);
if (!string.IsNullOrEmpty(emoji))
return $"{emoji} {i.Command} — {i.Description}";
}
catch { }
return $"{i.Command} — {i.Description}";
}).ToArray();
lstResults.BeginUpdate();
lstResults.Items.Clear();
lstResults.Items.AddRange(sample);
lstResults.EndUpdate();
}
catch { }
}
private void TxtSearch_TextChanged(object? sender, EventArgs e)
{
var q = txtSearch.Text?.Trim() ?? string.Empty;
// Only start searching when the user has entered 3 or more characters.
if (q.Length < 3)
{
lblHint.Text = "Type 3+ characters to search...";
// Keep the list empty until sufficient input is provided
lstResults.Items.Clear();
return;
}
lblHint.Text = string.Empty;
// Aggregate available commands from interpreter's public lists
var items = new List<(string Command, string Description)>();
try
{
items.AddRange(NaturalCommands.NaturalLanguageInterpreter.AvailableCommands);
}
catch { }
try
{
items.AddRange(NaturalCommands.NaturalLanguageInterpreter.VisualStudioCommands);
}
catch { }
try
{
items.AddRange(NaturalCommands.NaturalLanguageInterpreter.VSCodeCommands);
}
catch { }
try
{
items.AddRange(NaturalCommands.CommandDefinitions.WindowsTerminalCommands);
}
catch { }
try
{
items.AddRange(NaturalCommands.CommandDefinitions.WindowsExplorerCommands);
}
catch { }
// Diagnostic logging: write combined items count and presence of 'natural dictate'
try
{
var logPath = System.IO.Path.GetFullPath(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "bin", "app.log"));
var found = items.Any(i => string.Equals(i.Command?.Trim(), "natural dictate", StringComparison.OrdinalIgnoreCase));
NaturalCommands.Helpers.Logger.LogDebug($"AvailableCommandsForm: combined items count={items.Count}, contains 'natural dictate'={found}");
}
catch { }
// Log control bounds so we can see if the ListBox is overlapped/offset
try
{
var logPath2 = System.IO.Path.GetFullPath(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "bin", "app.log"));
NaturalCommands.Helpers.Logger.LogDebug($"AvailableCommandsForm.Bounds: Form.ClientSize={this.ClientSize}, txtSearch.Bounds={txtSearch.Bounds}, lblHint.Bounds={lblHint.Bounds}, lstResults.Bounds={lstResults.Bounds}");
}
catch { }
// More flexible matching: split the query into terms and require all terms
// to be present in either the command or the description (order-insensitive).
// Tokenize and only keep terms of length >= 3 to avoid matching on tiny fragments
var terms = q.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Trim().ToLowerInvariant())
.Where(s => s.Length >= 3)
.ToArray();
if (terms.Length == 0)
{
lblHint.Text = "Type 3+ characters to search...";
lstResults.Items.Clear();
return;
}
// Diagnostic logging: query terms
try
{
var logPath = System.IO.Path.GetFullPath(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "bin", "app.log"));
NaturalCommands.Helpers.Logger.LogDebug($"AvailableCommandsForm: query='{q}', terms=[{string.Join(',', terms)}]");
}
catch { }
var filtered = items
.Where(i =>
{
var hay = (i.Command + " " + (i.Description ?? string.Empty)).ToLowerInvariant();
return terms.All(t => hay.Contains(t));
})
.Select(i =>
{
try
{
var emoji = NaturalCommands.EmojiManager.GetCommandEmoji(i.Command);
if (!string.IsNullOrEmpty(emoji))
return $"{emoji} {i.Command} — {i.Description}";
}
catch { }
return $"{i.Command} — {i.Description}";
})
// Limit results to 4 items for quick, focused suggestions
.Take(4)
.ToArray();
// Also include any configured emoji mappings that match the query (show name -> emoji)
try
{
var mappings = NaturalCommands.EmojiManager.GetAllEmojiMappings();
var mappingMatches = mappings
.Where(m => (m.Name ?? string.Empty).IndexOf(q, StringComparison.OrdinalIgnoreCase) >= 0 || (m.Emoji ?? string.Empty).IndexOf(q, StringComparison.OrdinalIgnoreCase) >= 0)
.Select(m => $"{m.Emoji} {m.Name} — Emoji mapping")
.ToArray();
// Prepend mapping results so they are visible first
if (mappingMatches.Length > 0)
{
var combined = new string[mappingMatches.Length + filtered.Length];
mappingMatches.CopyTo(combined, 0);
filtered.CopyTo(combined, mappingMatches.Length);
filtered = combined;
}
}
catch { }
// Diagnostic logging: filtered count
try
{
var logPath = System.IO.Path.GetFullPath(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "bin", "app.log"));
NaturalCommands.Helpers.Logger.LogDebug($"AvailableCommandsForm: filtered count={filtered.Length}");
}
catch { }
lstResults.BeginUpdate();
lstResults.Items.Clear();
lstResults.Items.AddRange(filtered);
// Diagnostic: log the actual strings being added so we can confirm visibility
try
{
var logPath = System.IO.Path.GetFullPath(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "bin", "app.log"));
NaturalCommands.Helpers.Logger.LogDebug($"AvailableCommandsForm: adding filtered items: {string.Join(" | ", filtered)}");
}
catch { }
// Ensure colors are explicit so items are visible regardless of shared theme
try { lstResults.BackColor = Color.Black; } catch { }
try { lstResults.ForeColor = Color.White; } catch { }
try { lstResults.Visible = true; lstResults.Refresh(); lstResults.Invalidate(); lstResults.Update(); } catch { }
try
{
var logPath = System.IO.Path.GetFullPath(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "bin", "app.log"));
NaturalCommands.Helpers.Logger.LogDebug($"AvailableCommandsForm: ListBox.Items.Count={lstResults.Items.Count}");
}
catch { }
try
{
if (lstResults.Items.Count > 0)
{
lstResults.SelectedIndex = 0; // select first to make it visible
// Ensure the selected index is scrolled into view
lstResults.TopIndex = Math.Max(0, lstResults.SelectedIndex);
NaturalCommands.Helpers.Logger.LogDebug($"AvailableCommandsForm: SelectedIndex={lstResults.SelectedIndex}, TopIndex={lstResults.TopIndex}");
}
}
catch { }
lstResults.EndUpdate();
}
private void LstResults_DoubleClick(object? sender, EventArgs e)
{
if (lstResults.SelectedItem == null) return;
// For now, copy the selected command text to clipboard to let the user paste or use it.
Clipboard.SetText(lstResults.SelectedItem.ToString() ?? string.Empty);
MessageBox.Show("Command copied to clipboard.", "Copied", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}