obsidianmd/obsidian-api

Bug: Modals in popups are not closed with the window, so their keyboard scope remains active

pjeby opened this issue · 4 comments

pjeby commented

Steps to reproduce:

  • In a popout, open a modal such as the command palette, moving the cursor to an item
  • Close the window, e.g. w/Alt-F4
  • Switch to the main window (e.g via alt-tab), then press enter
  • The command selected in step 1 will be executed

AFAICT, this is a characteristic of all Modal subclasses, whether core or plugins. Maybe modals need to hook beforeunload on this.modalEl.win when they open and unhook it when they close? That's what I'm doing now as a workaround in the modal I'm working on, in the onOpen/onClose methods. It seems to work well enough for at least that one modal... once I dealt with the second issue, anyway.

It seems that open and close of modals isn't idempotent: if you open or close something that's already open or closed, it's Not Good. This makes it potentially race-conditiony to have an event hook maybe closing an already-closed modal. I'm working around that by gating open and close in the subclass to only super-call if the state needs changing, but it would be nice if all modals had these behaviors (i.e. idempotent open/close as well as auto-close w/window close).

lishid commented

Thanks, that's indeed a bug we will look into!

I checked it again and would regard this as a related issue. This happens in the modal called by a button inside a custom view (extends ItemView), and things go smooth with the modals directly called by the ribbon icon.

Problem: If I assign a keyboard event handler on the input element in a modal aiming to close the modal down, the modal would be activated again by a further key press, causing close-reopen cycle. Glad that you have found this, and waiting for the fix.

Update: I published a demo plugin for this, you can check it here: https://github.com/Acylation/modal-bug-demo @lishid

This is fixed as of Obsidian Desktop v1.4.0

pjeby commented

A further thought here -- it seems like one could also register to have the modal close if there is an active-leaf change event that targets another Obsidian window. This would address the issue where keyboard commands don't work if you alt-tab or click to another Obsidian window with a modal active. (Alternately, one could forcibly re-focus the window with the modal, thereby indicating to the user the modal must be manually closed.)

I mention this because I still get bitten from time to time by the issue of "some hotkeys mysteriously don't work in my editor because another window has a modal I opened an hour ago and forgot to close". 😉