From 899cb899905fa75de41d808ceea02bca8ba0821e Mon Sep 17 00:00:00 2001 From: Luca Auer Date: Sat, 18 Jan 2025 05:54:20 +0100 Subject: [PATCH] Prevent stale frames from being drawn / always ensure up-to-date contents in webview (#3668) * Prevent stale frames being drawn. At key points where external changes enter the webview, stale images might get rendered. This ensures that a frame showing current state is always shown. * Only stage single redraw * Remove potentially superfluous calls to `self.update()` * Remove potentially superfluous calls to `self.update()`. I lost this one during some git troubles. * Revert unrelated change The function is supposed to take a boolean telling it whether or not the loading succeeded, which it doesn't as is. However, this is unrelated and works either way so I also reverted it again. * chore: code cleanup * cleanup: Remove redundant check for presence of callback A callback will be used either way for this call, so it can be simplified. The check happens inside the handler. * Add comment explaining why this change is necessary, referencing the relevant PR. * Clarify comment to answer the why, not the what. One can see what is being done, why is probably more important. --- CONTRIBUTORS | 1 + qt/aqt/webview.py | 18 +++++++++--------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index a8342d123..201224633 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -203,6 +203,7 @@ hideo aoyama Ross Brown 🦙 Lukas Sommer +Luca Auer Lukas Sommer Niclas Heinz Omar Kohl diff --git a/qt/aqt/webview.py b/qt/aqt/webview.py index 980e7a989..b5b50ad72 100644 --- a/qt/aqt/webview.py +++ b/qt/aqt/webview.py @@ -659,17 +659,17 @@ html {{ {font} }} page = self.page() assert page is not None - if cb: - - def handler(val: Any) -> None: - if self._shouldIgnoreWebEvent(): - print("ignored late js callback", cb) - return + def handler(val: Any) -> None: + if self._shouldIgnoreWebEvent(): + print("ignored late js callback", cb) + return + if cb: cb(val) - page.runJavaScript(js, handler) - else: - page.runJavaScript(js) + # Without the following, stale frames showing previous or corrupt content get occasionally displayed. (see #3668 for more details) + self.update() + + page.runJavaScript(js, handler) def _queueAction(self, name: str, *args: Any) -> None: self._pendingActions.append((name, args))