citation dialog: fix unhandled click on accept button of unfocused dialog (#5113)

Fix issue where clicking accept button would not work
when the dialog itself is unfocused. Add a small delay in the
focus handler of the dialog to allow accept button clicks
to be handled before rerunning the search.

Fixes: #5110
This commit is contained in:
abaevbog 2025-03-14 04:49:57 -07:00 • committed by GitHub
parent 583fbf4e95
commit 4915f1fadf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1543,13 +1543,19 @@ let windowLostFocusOn = 0;
window.addEventListener("blur", () => {
windowLostFocusOn = (new Date()).getTime();
});
window.addEventListener("focus", () => {
window.addEventListener("focus", async () => {
let now = (new Date()).getTime();
// On linux, resizing the dialog causes the window to loose and immediately regain focus.
// Do not run the search if the window lost focus less than 100 ms ago.
if (Zotero.isLinux && now - windowLostFocusOn < 100) {
return;
}
// Wait a moment to allow accept button click event to fire.
// Without this, clicking accept button when the dialog is not focused
// would refocus the dialog, run the search below,
// which replaces accept button with the spinner and interrupts the click event.
await Zotero.Promise.delay(100);
if (accepted) return;
SearchHandler.clearNonLibraryItemsCache();
currentLayout?.search(SearchHandler.searchValue);
});