Skip to content

Commit

Permalink
Use GridView for keyboard shortcuts and polish
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Feb 8, 2025
1 parent 068370e commit 94a9309
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 39 deletions.
60 changes: 37 additions & 23 deletions NAPS2.Lib/EtoForms/Ui/KeyboardShortcutsForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public class KeyboardShortcutsForm : EtoDialogBase

private readonly DesktopFormProvider _desktopFormProvider;

private readonly ListBox _listBox = new();
private readonly TextBox _shortcutText = new();
private readonly GridView _gridView;
private readonly TextBox _shortcutText = new() { ReadOnly = true };
private readonly Button _assign = C.Button(UiStrings.Assign);
private readonly Button _unassign = C.Button(UiStrings.Unassign);
private readonly Button _restoreDefaults = C.Button(UiStrings.RestoreDefaults);
Expand All @@ -100,9 +100,27 @@ public KeyboardShortcutsForm(Naps2Config config, KeyboardShortcutManager ksm,
_desktopFormProvider = desktopFormProvider;
_transact = Config.User.BeginTransaction();
_transactionConfig = Config.WithTransaction(_transact);
_listBox.DataStore = Shortcuts;
_listBox.ItemTextBinding = new DelegateBinding<Shortcut, string>(GetLabel);
_listBox.SelectedIndexChanged += ListBox_SelectedIndexChanged;
_gridView = new()
{
DataStore = Shortcuts,
Columns =
{
new()
{
HeaderText = UiStrings.Action,
DataCell = new TextBoxCell { Binding = new PropertyBinding<string>("Label") },
Width = 280
},
new()
{
HeaderText = UiStrings.Shortcut,
DataCell = new TextBoxCell { Binding = new DelegateBinding<Shortcut, string>(GetShortcutLabel) },
Width = 150
}
}
};
_gridView.SelectionChanged += GridView_SelectionChanged;
_gridView.CellDoubleClick += Assign_Click;
_shortcutText.KeyDown += ShortcutText_KeyDown;
_assign.Click += Assign_Click;
_unassign.Click += Unassign_Click;
Expand All @@ -117,7 +135,7 @@ protected override void BuildLayout()

LayoutController.Content = L.Column(
L.Row(
_listBox.NaturalSize(300, 500).Scale(),
_gridView.NaturalSize(450, 500).Scale(),
L.Column(
C.Filler(),
_shortcutText.Width(150),
Expand All @@ -136,15 +154,15 @@ protected override void BuildLayout()

private void Assign_Click(object? sender, EventArgs e)
{
var selected = (Shortcut?) _listBox.SelectedValue;
var selected = (Shortcut?) _gridView.SelectedItem;
if (selected == null) return;
_shortcutText.Enabled = true;
_shortcutText.ReadOnly = false;
_shortcutText.Focus();
}

private void Unassign_Click(object? sender, EventArgs e)
{
var selected = (Shortcut?) _listBox.SelectedValue;
var selected = (Shortcut?) _gridView.SelectedItem;
if (selected?.Accessor == null) return;
_transact.Set(selected.Accessor, "");
UpdateUi();
Expand All @@ -164,10 +182,10 @@ private void RestoreDefaults_Click(object? sender, EventArgs e)

private void ShortcutText_KeyDown(object? sender, KeyEventArgs e)
{
if (!_shortcutText.Enabled) return;
if (_shortcutText.ReadOnly) return;

e.Handled = true;
var selected = (Shortcut?) _listBox.SelectedValue;
var selected = (Shortcut?) _gridView.SelectedItem;
if (selected?.Accessor == null) return;
if (e.Key is Keys.LeftControl or Keys.LeftAlt or Keys.LeftShift or Keys.LeftApplication
or Keys.RightControl or Keys.RightAlt or Keys.RightShift or Keys.RightApplication)
Expand All @@ -179,30 +197,30 @@ private void ShortcutText_KeyDown(object? sender, KeyEventArgs e)
UpdateUi();
}

private void ListBox_SelectedIndexChanged(object? sender, EventArgs e)
private void GridView_SelectionChanged(object? sender, EventArgs e)
{
UpdateUi();
}

private void UpdateUi()
{
var selected = (Shortcut?) _listBox.SelectedValue;
var selected = (Shortcut?) _gridView.SelectedItem;
if (selected?.Accessor == null)
{
_shortcutText.Text = "";
_shortcutText.Enabled = false;
_shortcutText.ReadOnly = true;
_assign.Enabled = false;
_unassign.Enabled = false;
}
else
{
bool locked = _transactionConfig.AppLocked.Has(selected.Accessor);
_shortcutText.Text = GetKeyString(selected);
_shortcutText.Enabled = false;
_shortcutText.ReadOnly = true;
_assign.Enabled = !locked;
_unassign.Enabled = !locked && _shortcutText.Text != "";
}
_listBox.Invalidate();
_gridView.Invalidate();
}

private string GetKeyString(Shortcut shortcut)
Expand All @@ -213,16 +231,12 @@ private string GetKeyString(Shortcut shortcut)
return _ksm.Stringify(keys) ?? "";
}

private string GetLabel(Shortcut shortcut)
private string GetShortcutLabel(Shortcut shortcut)
{
if (shortcut.Accessor == null) return shortcut.Label;
if (shortcut.Accessor == null) return "";

var keys = _ksm.Parse(_transactionConfig.Get(shortcut.Accessor));
if (keys == Keys.None)
{
return shortcut.Label;
}
return string.Format(UiStrings.KeyboardShortcutLabelFormat, shortcut.Label, _ksm.Stringify(keys));
return _ksm.Stringify(keys) ?? "";
}

private void Save()
Expand Down
27 changes: 18 additions & 9 deletions NAPS2.Lib/Lang/Resources/UiStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions NAPS2.Lib/Lang/Resources/UiStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -957,13 +957,16 @@
<data name="ScanWithNewProfile" xml:space="preserve">
<value>Scan With New Profile</value>
</data>
<data name="KeyboardShortcutLabelFormat" xml:space="preserve">
<value>{0} ({1})</value>
</data>
<data name="Assign" xml:space="preserve">
<value>Assign</value>
</data>
<data name="Unassign" xml:space="preserve">
<value>Unassign</value>
</data>
<data name="Action" xml:space="preserve">
<value>Action</value>
</data>
<data name="Shortcut" xml:space="preserve">
<value>Shortcut</value>
</data>
</root>
12 changes: 8 additions & 4 deletions NAPS2.Lib/Lang/po/templates.pot
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ msgstr ""
msgid "Acquiring data..."
msgstr ""

#: UiStrings.resx$Action$Message
msgid "Action"
msgstr ""

#: UiStrings.resx$Advanced$Message
msgid "Advanced"
msgstr ""
Expand Down Expand Up @@ -1431,6 +1435,10 @@ msgstr ""
msgid "Sharpen"
msgstr ""

#: UiStrings.resx$Shortcut$Message
msgid "Shortcut"
msgstr ""

#: UiStrings.resx$Show$Message
msgid "Show"
msgstr ""
Expand Down Expand Up @@ -1749,10 +1757,6 @@ msgstr ""
msgid "x64"
msgstr ""

#: UiStrings.resx$KeyboardShortcutLabelFormat$Message
msgid "{0} ({1})"
msgstr ""

#: MiscResources.resx$NamedPageSizeFormat$Message
msgid "{0} ({1}x{2} {3})"
msgstr ""
Expand Down

0 comments on commit 94a9309

Please sign in to comment.