Skip to content

Keyboard: fix shortcut activation and input focus issues#1256

Merged
scrubbbbs merged 14 commits intonomacs:masterfrom
scrubbbbs:shortcut-context
May 15, 2025
Merged

Keyboard: fix shortcut activation and input focus issues#1256
scrubbbbs merged 14 commits intonomacs:masterfrom
scrubbbbs:shortcut-context

Conversation

@scrubbbbs
Copy link
Copy Markdown
Collaborator

@scrubbbbs scrubbbbs commented Jan 16, 2025

See commit messages.

Summary:

Shortcut activation problems can be fixed by removing setShortcutContext(Qt::WidgetAndChidrenShortcut). This flag requires widget focus for the shortcut to activate. Which is why we have to click in the main view for most shortcuts to work. I have been able to remove most instances of this and it fixes most of the activation issues.

However it leaves us with new issues to resolve:


  • Conflicts with built-in keys

There are Qt controls that use built-in keys that might be defined in a shortcut; these shortcuts need to be "overridden" explicitly, for example pressing the "e" key when explorer has focus should not hide the explorer, it should jump to the next file/folder starting with "e". Mostly this is QTreeView, and the fix is to add a handler for QEvent::ShortCutOverride.


  • Ambiguous shortcuts.

These come from swidgets that define their own QAction outside of menus and/or toolbars. These are resolved by:

  • Using the new DkActionEventHandler to bypass Qt shortcuts system for those actions
  • Allowing shortcut to be resolved via DkShortcutEventHandler which has a method to resolve the ambiguity. In some case actions will have to be copied around to give them higher priority.

  • Conflicts with plugins

For example control-Z in paint-on-image. This is address in two ways

  • disabling certain actions while the plugin is active
  • they can rely on the ambiguous resolution mechanism

@scrubbbbs scrubbbbs force-pushed the shortcut-context branch 4 times, most recently from f6e2f2f to f4eda83 Compare January 19, 2025 03:06
@scrubbbbs scrubbbbs mentioned this pull request Feb 25, 2025
@scrubbbbs scrubbbbs force-pushed the shortcut-context branch 4 times, most recently from 1192ec4 to 6dc0f13 Compare March 29, 2025 14:53
@scrubbbbs scrubbbbs force-pushed the shortcut-context branch 2 times, most recently from b4e7f0d to 760ad04 Compare March 30, 2025 13:37
@scrubbbbs scrubbbbs marked this pull request as ready for review March 30, 2025 18:21
@scrubbbbs scrubbbbs requested review from leejuyuu and novomesk March 30, 2025 18:22
@leejuyuu
Copy link
Copy Markdown
Collaborator

leejuyuu commented Apr 3, 2025

I don't really use shortcuts so can't test very thoroughly, but the mentioned issues seems to have been fixed.

@scrubbbbs
Copy link
Copy Markdown
Collaborator Author

I don't really use shortcuts so can't test very thoroughly, but the mentioned issues seems to have been fixed.

A good test is to bind the "About Nomacs" dialog shortcut to a reserved key (like up/down or left/right) and see if we can break anything.

scrubbbbs added 5 commits May 15, 2025 15:15
Shortcuts now get Window context, which makes them available
all of the time unless their actions are disabled.

Fixes: nomacs#599
Fixes: nomacs#653
Fixes: nomacs#994
Fixes: nomacs#1190
Fixes: nomacs#1198
Fixes: nomacs#1234
Not configurable and conflicts with the default Quick Look key.
When hiding/showing docks they tend to steal keyboard focus. Now
require user to click in the widget in order to have input focus.

This breaks tab navigation into dockwidgets, however tab navigation in
general is not working properly right now and would be much more
difficult to fix.
Resolves shortcut conflict with pageup/pagedown and also disallows
viewport actions such as rename (F2) etc. which is fine as the
viewport is obscured (thumbs view) or not applicable (settings,batch).

Removes adding of actions as it has no effect.
Plugins require a dummy/stub menu item at startup because
they are lazy-loaded. It seems there was an effort to do this
but it was never used/finished. Seems to work fine now.

Also removes widget context from plugin actions, no longer required.
scrubbbbs added 9 commits May 15, 2025 15:15
Add menubar actions to main window so they work when the menu bar
is hidden, regardless of the viewport state.

Remove menubar actions from viewport, which was the hack to do this
before and is no longer needed.
- disable undo/redo actions so plugins can have their own
- disable copy/paste actions since they work on original image
- workaround open new tab unloading the plugin w/o saving
- workaround open settings/batch changes action enablement
- prevent loading two plugins at the same time

Fixes: nomacs#571
The change to use default shortcut context (Window context) solves our
shortcut activation problems, however comes with some drawbacks:

1) widgets that have non-action keybinds like keyPressEvent() can have
their keys overriden by a shortcut

2) actions that bind the same key that worked before, now get the
"ambiguous shortcut" error from Qt

To solve (1), DkShortcutEventFilter is added as an application event
filter. Widgets which have known keybinds (reserved keys) are defined in
a table and we can reserve additional keys or release the default
keybinds wit DkShortcutEventFilter::reserveKeys()

To solve (2), DkShortcutEventFilter also traps ambiguous shortcut events
and resolves them by walking the widget heirarchy for a bound action.
This is similar to Qt::WidgetWithChildrenShortcut, but will also walk
*up* from the widget to its parent if nothing is found in itself or
children.

As an alternate solution to (2), DkActionEventFilter is added, which
allows widgets to binding actions without going through Qt shortcut
system, which will override any global actions/shortcuts.

With these solutions users and plugins should be able to bind more keys
than previously possible, and without having conflicts that break the
application.
Notes buttons used to activate on esc/ctrl+enter whether focused or not.
Now can only work when typing in notes/focused.
Reserve keys used by keyPressEvent() so shortcuts binding them will be
overriden
Actions can have multiple keys assigned, check them all. Also do not
consider disabled actions.
If explorer is visible, now-ambiguous "return" key resolves to explorer
action instead of crop toolbar action.

Fix this by copying crop toolbar actions so they have a higher priority
when resolving conflicts.
- move a static_cast<> near its type assertion
- remove semantic inversion of "enabled" argument to
enableViewPortPluginActions()
- remove some indentation
- improve comments
- fix shortcut conflicts in floating docks
@scrubbbbs scrubbbbs merged commit 06d80b5 into nomacs:master May 15, 2025
12 checks passed
@scrubbbbs scrubbbbs deleted the shortcut-context branch May 15, 2025 20:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants