Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@beyondcompute
Copy link
Contributor

@beyondcompute beyondcompute commented Apr 7, 2025

As I’ve been editing decks more often, I noticed that editor dialogs don’t respond to Cmd+W keyboard shortcut. Here’s an attempt to add the shortcut to “Edit Current”, “Fields” and “Cards” editor dialogs. And also to several modals.

UI of “Edit Current” dialog with several input fields

@GithubAnon0000
Copy link
Contributor

GithubAnon0000 commented Apr 7, 2025

ctrl+w to close a window would be nice to have on other OSs as well, not just for mac in my opinion.

@beyondcompute
Copy link
Contributor Author

beyondcompute commented Apr 7, 2025

ctrl+w to close a window would be nice to have on other OSs as well, not just for mac in my opinion.

Actually that sounds more than reasonable!

I am not very knowledgeable about other operating systems and their UI patterns (and it would be a bit harder for me to test) but perhaps we could un-Mac-ify this one (and remove explicit is_mac checks elsewhere):

anki/qt/aqt/utils.py

Lines 991 to 996 in ccab18b

def addCloseShortcut(widg: QDialog) -> None:
if not is_mac:
return
shortcut = QShortcut(QKeySequence("Ctrl+W"), widg)
qconnect(shortcut.activated, widg.reject)
setattr(widg, "_closeShortcut", shortcut)

@GithubAnon0000
Copy link
Contributor

I probably can't really help with the programming site of things, but I can test your proposed solution on debian linux (sid).

@beyondcompute beyondcompute force-pushed the add-mac-close-shortcut-to-more-dialogs branch from 3bc253d to 8a6e4ef Compare April 8, 2025 18:37
@beyondcompute
Copy link
Contributor Author

I probably can't really help with the programming site of things, but I can test your proposed solution on debian linux (sid).

Here’s the branch: https://github.com/beyondcompute/anki/tree/make-ctrl_w-keyboard-shorcut-work-on-win-and-linux . (It includes changes for this PR as well. 🤷‍♂️)

I hope, someone who is really familiar with Anki and its multiple views and dialogs can test those thoroughly. Also, we’d obviously need a Windows tester. 🪟😳

@GithubAnon0000
Copy link
Contributor

I tested it on debian linux (sid) using this branch:

Here’s the branch: https://github.com/beyondcompute/anki/tree/make-ctrl_w-keyboard-shorcut-work-on-win-and-linux

Results:

Dialog Ctrl+w works?
Add dialog Yes
Browse dialog No
Stats dialog Yes
Choose note type No
choose deck No
Export dialog No
Create filtered deck No
Check media No
Addons dialog Yes
Manage note types / Note types No
Preferences No
Fields for X (e.g. add dialog > Fields...) Yes
Card Types for X (e.g. add dialog > Cards...) Yes
Preview No
Sync (Account Required Dialog) Yes
Deck options Yes
Create Deck No

(7 work, 11 do not work)

I think I tested all dialogs.

@beyondcompute
Copy link
Contributor Author

Some investigation notes. 📝

  1. Places where we use addCloseShortcut seem to work
  2. In Browse tab’s window we do this:
    if not is_mac:
    f.actionClose.setVisible(False)
    I am not sure, is it better to show it on all systems or should I just add the shortcut “manually” (add_close_shortcut or whatever) and keep the action hidden (whatever that means 🤔)
  3. There’s also that weird behavior: Cmd+W in a sub-window (like Preview) of the Browse view closes both the “child” Preview window and the “parent” Browse window. I tried to address that here: Close only “child” window (e.g. Preview) inside Browser on Cmd+W #3913
  4. I am adding more closing shortcuts in this PR (overall, there are many small dialogs for renaming things, etc… 🤔)

@beyondcompute
Copy link
Contributor Author

beyondcompute commented Apr 9, 2025

@GithubAnon0000 I’ve added the shortcut to more dialogs and updated the branch. I also removed hiding of “close action” on other (non-Mac) systems. Let’s see, how that looks.

Not sure if any of this will get merged. 😄

Also something like #3913 perhaps would be nice to have.

@beyondcompute beyondcompute force-pushed the add-mac-close-shortcut-to-more-dialogs branch from d91015f to fefdb7e Compare April 10, 2025 16:34
@GithubAnon0000
Copy link
Contributor

I can test it again if you want me to. Just keep in mind that I'd need a week or two, as my schedule is quite busy right now.

@dae
Copy link
Member

dae commented Apr 11, 2025

Thank you! Happy to add the shortcut to any screens that are missing it, provided we don't introduce regressions. Some notes:

  • I don't think we should be adding the shortcut to the other platforms. It may conflict with something else, and it's not a standard shortcut on those platforms. Across all platforms, you can already use Esc to close most Anki windows, and those other platforms tend to have some other shortcut like Alt+F4 that is provided by the desktop environment instead of the app itself.
  • The browser change is superfluous - cmd+w already works there.
  • Agreed on it being confusing that the camelCase version of the function behaves differently. Digging through the history, the setattr() line appears to have been added to make the typechecker happy, and appears it can be safely removed. Furthermore, a brief test of switching from the camelCase to the snake_case version for the deck options seems to indicate they're interchangeable. I wonder if we could remove one?

@GithubAnon0000
Copy link
Contributor

GithubAnon0000 commented Apr 11, 2025

  • I don't think we should be adding the shortcut to the other platforms. It may conflict with something else, and it's not a standard shortcut on those platforms. Across all platforms, you can already use Esc to close most Anki windows, and those other platforms tend to have some other shortcut like Alt+F4 that is provided by the desktop environment instead of the app itself.

Is that the default for windows? In linux Ctrl+w is pretty much normal (I never even heard of Alt+F4).

Maybe the is_mac code could be updated to is_mac or is_linux (or !is_windows)?

Edit: Alt+F4 does exist on linux (at least on gnome). It closes the entire window though, unlike Ctrl+w. That's especially noticable when apps have tabs.

@beyondcompute
Copy link
Contributor Author

beyondcompute commented Apr 11, 2025

@dae the change in browser.py is for the grade_now modal (or not sure that I am understanding correctly about the change in the browser) and I see the behavior difference between main and this branch. 😳

@derivativeoflog7
Copy link
Contributor

derivativeoflog7 commented Apr 11, 2025

  • I don't think we should be adding the shortcut to the other platforms. It may conflict with something else, and it's not a standard shortcut on those platforms. Across all platforms, you can already use Esc to close most Anki windows, and those other platforms tend to have some other shortcut like Alt+F4 that is provided by the desktop environment instead of the app itself.

Is that the default for windows? In linux Ctrl+w is pretty much normal (I never even heard of Alt+F4).

Maybe the is_mac code could be updated to is_mac or is_linux (or !is_windows)?

Edit: Alt+F4 does exist on linux (at least on gnome). It closes the entire window though, unlike Ctrl+w. That's especially noticable when apps have tabs.

In Linux (and Windows), Ctrl+W is usually reserved for application with tabbed views for closing a single tab (which may cause the whole application to be closed when the last open tab is closed, depending on how it's implemented)

- The camelCase variant will remain accessible with a warning.
- The removed setattr line is legacy cruft, and wasn't doing anything.
@dae
Copy link
Member

dae commented Apr 24, 2025

Merging this in for now, but a global approach like mentioned on #3913 (review) is probably the better longer-term solution.

@dae dae merged commit fe2c151 into ankitects:main Apr 24, 2025
1 check passed
@beyondcompute beyondcompute deleted the add-mac-close-shortcut-to-more-dialogs branch April 25, 2025 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants