From 94a9309367a26616429e5e395565ff7fae66ffea Mon Sep 17 00:00:00 2001 From: Ben Olden-Cooligan Date: Fri, 7 Feb 2025 18:13:08 -0800 Subject: [PATCH] Use GridView for keyboard shortcuts and polish #549 --- .../EtoForms/Ui/KeyboardShortcutsForm.cs | 60 ++++++++++++------- .../Lang/Resources/UiStrings.Designer.cs | 27 ++++++--- NAPS2.Lib/Lang/Resources/UiStrings.resx | 9 ++- NAPS2.Lib/Lang/po/templates.pot | 12 ++-- 4 files changed, 69 insertions(+), 39 deletions(-) diff --git a/NAPS2.Lib/EtoForms/Ui/KeyboardShortcutsForm.cs b/NAPS2.Lib/EtoForms/Ui/KeyboardShortcutsForm.cs index 7f8d56e199..703e383eef 100644 --- a/NAPS2.Lib/EtoForms/Ui/KeyboardShortcutsForm.cs +++ b/NAPS2.Lib/EtoForms/Ui/KeyboardShortcutsForm.cs @@ -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); @@ -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(GetLabel); - _listBox.SelectedIndexChanged += ListBox_SelectedIndexChanged; + _gridView = new() + { + DataStore = Shortcuts, + Columns = + { + new() + { + HeaderText = UiStrings.Action, + DataCell = new TextBoxCell { Binding = new PropertyBinding("Label") }, + Width = 280 + }, + new() + { + HeaderText = UiStrings.Shortcut, + DataCell = new TextBoxCell { Binding = new DelegateBinding(GetShortcutLabel) }, + Width = 150 + } + } + }; + _gridView.SelectionChanged += GridView_SelectionChanged; + _gridView.CellDoubleClick += Assign_Click; _shortcutText.KeyDown += ShortcutText_KeyDown; _assign.Click += Assign_Click; _unassign.Click += Unassign_Click; @@ -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), @@ -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(); @@ -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) @@ -179,18 +197,18 @@ 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; } @@ -198,11 +216,11 @@ private void UpdateUi() { 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) @@ -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() diff --git a/NAPS2.Lib/Lang/Resources/UiStrings.Designer.cs b/NAPS2.Lib/Lang/Resources/UiStrings.Designer.cs index 2b7ddc8b03..8050e45e2d 100644 --- a/NAPS2.Lib/Lang/Resources/UiStrings.Designer.cs +++ b/NAPS2.Lib/Lang/Resources/UiStrings.Designer.cs @@ -77,6 +77,15 @@ internal static string AboutFormTitle { } } + /// + /// Looks up a localized string similar to Action. + /// + internal static string Action { + get { + return ResourceManager.GetString("Action", resourceCulture); + } + } + /// /// Looks up a localized string similar to Advanced. /// @@ -1121,15 +1130,6 @@ internal static string KeepSession { } } - /// - /// Looks up a localized string similar to {0} ({1}). - /// - internal static string KeyboardShortcutLabelFormat { - get { - return ResourceManager.GetString("KeyboardShortcutLabelFormat", resourceCulture); - } - } - /// /// Looks up a localized string similar to Keyboard Shortcuts. /// @@ -2255,6 +2255,15 @@ internal static string Sharpen { } } + /// + /// Looks up a localized string similar to Shortcut. + /// + internal static string Shortcut { + get { + return ResourceManager.GetString("Shortcut", resourceCulture); + } + } + /// /// Looks up a localized string similar to Show. /// diff --git a/NAPS2.Lib/Lang/Resources/UiStrings.resx b/NAPS2.Lib/Lang/Resources/UiStrings.resx index 4139b7f3c3..1dd93aa953 100644 --- a/NAPS2.Lib/Lang/Resources/UiStrings.resx +++ b/NAPS2.Lib/Lang/Resources/UiStrings.resx @@ -957,13 +957,16 @@ Scan With New Profile - - {0} ({1}) - Assign Unassign + + Action + + + Shortcut + \ No newline at end of file diff --git a/NAPS2.Lib/Lang/po/templates.pot b/NAPS2.Lib/Lang/po/templates.pot index cfa8728013..5208f7baad 100644 --- a/NAPS2.Lib/Lang/po/templates.pot +++ b/NAPS2.Lib/Lang/po/templates.pot @@ -54,6 +54,10 @@ msgstr "" msgid "Acquiring data..." msgstr "" +#: UiStrings.resx$Action$Message +msgid "Action" +msgstr "" + #: UiStrings.resx$Advanced$Message msgid "Advanced" msgstr "" @@ -1431,6 +1435,10 @@ msgstr "" msgid "Sharpen" msgstr "" +#: UiStrings.resx$Shortcut$Message +msgid "Shortcut" +msgstr "" + #: UiStrings.resx$Show$Message msgid "Show" msgstr "" @@ -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 ""