Skip to content

WebUI: Allow closing modals with Escape #22920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

userwiths
Copy link
Contributor

Closes #13891

Tested on the following WebUI Modals:

  1. Delete
  2. Rename files
  3. Settings
  4. About
  5. Statistics
  6. Add torrent

Pressing Escape without an active modal/dialog should not do anything, in my tests it did not, but i might have missed some strange cases. If any such is reported, im gonna can care of it.

@xavier2k6 xavier2k6 added the WebUI WebUI-related issues/changes label Jun 25, 2025
@thalieht
Copy link
Contributor

I think i've tested every dialog and the only one in which it doesn't apply is Remove tracker dialog in filters sidebar.

Pressing escape when 2+ dialogs are active closes everything.

@userwiths
Copy link
Contributor Author

userwiths commented Jun 25, 2025

I think i've tested every dialog and the only one in which it doesn't apply is Remove tracker dialog in filters sidebar.

Latest commit should take care of this. A bunch of modals are iframed it seems and do not inherit the window.MochaUI object or/and the event handler.

Same approach was done before for ./private/addpeers.html,./private/addtrackers.html,./private/addwebseeds.html and more.

(You can also see a bunch of other deeply hidden modals that would not close, changed them too)

Pressing escape when 2+ dialogs are active closes everything.

Is this an issue ?

I see that it happens, and currently it should depend on focus & modal type. If the focus is on the main window, it will close all modals. If the focus is on a modal in an iframe, it would close only this modal.

Checking the code i see an easy fix for 2 modals, but when we have 2+ i doubt that we can guarantee the same order of closing.

Just tell me if its a blocker, and if it is im gonna put some more time into it.

@thalieht
Copy link
Contributor

Pressing escape when 2+ dialogs are active closes everything.

Is this an issue ?

As a user this would bother me. For example in settings > RSS > Edit auto downloading rules, after closing that dialog i'd expect to go back to settings and in general i'd expect only the focused dialog to close.

Comment on lines 1781 to 1782
Object.values(MochaUI.Windows.instances).forEach((modal) => {
modal.close();
Copy link
Member

@Piccirello Piccirello Jun 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to only close the modal in the immediate foreground, as opposed to all? I previously worked on addressing a similar issue in #21804.

Edit: I see that @thalieht shared similar feedback

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated it with a check/sort based on the instance's timestamp so that we always close the latest one.
Tested with the described scenario from above and a few more, seems to work.

I also took a look at the PR you linked, and see that till now the idea was to add the event handler on each dialog's src/webui/www/private/views/*.html separately. Personally I dont think this is a good approach, but i can be persuaded into following it.


It would help me understand the code base better if someone has an idea how it was decided which dialog should load by xhr and which by iframe thats declared in src/webui/www/private/scripts/mocha-init.js with some modals calling new MochaUI.Window with loadMethod: "xhr", and some with loadMethod: "iframe",

I see that there are files from both load methods that declare their own event handler for escape, and I understand that for the iframe ones, but what is the reasoning for not using xhr only ?

Using xhr only, we could have a centralized event handler (in client.js as i attempt to do) without the need to re-declare the handler for each dialog.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated it with a check/sort based on the instance's timestamp so that we always close the latest one. Tested with the described scenario from above and a few more, seems to work.

This is a clever solution, however the most recently opened window isn't necessarily the one on top. A user can click on a different window to bring it to the foreground.

It does seem that the focused window is assigned the class isFocused. Could you use that?

I also took a look at the PR you linked, and see that till now the idea was to add the event handler on each dialog's src/webui/www/private/views/*.html separately. Personally I dont think this is a good approach, but i can be persuaded into following it.

You're likely right that there's a better way. I followed this approach since it was what was already in the codebase (which of course isn't a great reason). I do like your approach of having a single handler for this (the code in client.js).

It would help me understand the code base better if someone has an idea how it was decided which dialog should load by xhr and which by iframe thats declared in src/webui/www/private/scripts/mocha-init.js with some modals calling new MochaUI.Window with loadMethod: "xhr", and some with loadMethod: "iframe",

I see that there are files from both load methods that declare their own event handler for escape, and I understand that for the iframe ones, but what is the reasoning for not using xhr only ?

Using xhr only, we could have a centralized event handler (in client.js as i attempt to do) without the need to re-declare the handler for each dialog.

I can look into this further, but off the top of my head I can't remember an exact reason. @Chocobo1 might have more context.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didnt think that through it seems.

Latest commit uses the isFocused class as you suggested. Only thing that I notice is that if the user opens a bunch of modals and then (accidentally or deliberately) focuses the main window, escape would do nothing. But thats logical, because there is no focused modal if the user focuses the main window, so i take it thats intended behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • When closing "Enable automatic torrent management" with Esc, it also closes the last focused dialog.
  • Closing any dialog with Esc while "Rename files..." is also open, will close that too.

Copy link
Member

@Chocobo1 Chocobo1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments on coding style.

@Chocobo1 Chocobo1 requested a review from a team July 12, 2025 10:40
@Chocobo1
Copy link
Member

@userwiths
FYI there are merge conflicts now.

@userwiths userwiths force-pushed the fix/allow-closing-modals-with-escape branch from 5f527c2 to 8dd1662 Compare July 12, 2025 12:57
@userwiths
Copy link
Contributor Author

Rebased and fixed the conflicts, should be fine now.

@userwiths userwiths force-pushed the fix/allow-closing-modals-with-escape branch from 8dd1662 to d09070d Compare July 14, 2025 14:23
@Chocobo1 Chocobo1 added this to the 5.2 milestone Jul 14, 2025
@Chocobo1 Chocobo1 requested a review from a team July 14, 2025 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
WebUI WebUI-related issues/changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow Escape key to close dialog boxes in web ui
5 participants