Skip to content

Commit

Permalink
WinForms: Add access keys (aka "Alt" hotkeys)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Jan 15, 2024
1 parent 17849e7 commit ea46c1c
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion NAPS2.Lib.WinForms/WinForms/ToolbarFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Drawing;
using System.Windows.Forms;
using DockStyle = System.Windows.Forms.DockStyle;

Expand All @@ -11,6 +10,7 @@ public class ToolbarFormatter
{
private const int FORM_PIXEL_BUFFER = 30;
private readonly StringWrapper _stringWrapper;
private readonly HashSet<char> _charsUsedForAltHotkeys = [];

public ToolbarFormatter(StringWrapper stringWrapper)
{
Expand All @@ -24,6 +24,17 @@ public void RelayoutToolbar(ToolStrip tStrip)
foreach (var btn in tStrip.Items.OfType<ToolStripItem>())
{
btn.Text = _stringWrapper.Wrap(btn.Text ?? "", 80, btn.Font);
if (!btn.Text.Contains("&"))
{
var charToUse = btn.Text.Where(char.IsLetter)
.FirstOrDefault(c => !_charsUsedForAltHotkeys.Contains(char.ToLowerInvariant(c)));
if (charToUse != default)
{
_charsUsedForAltHotkeys.Add(char.ToLowerInvariant(charToUse));
var index = btn.Text.IndexOf(charToUse);
btn.Text = btn.Text.Substring(0, index) + @"&" + btn.Text.Substring(index);
}
}
}
ResetToolbarMargin(tStrip);
// Recalculate sizes
Expand Down

0 comments on commit ea46c1c

Please sign in to comment.