From 49ba699155ecc9d00d8a4075753b4e60d58c3ce1 Mon Sep 17 00:00:00 2001 From: Vxrtrauter <101264710+Vxrtrauter@users.noreply.github.com> Date: Wed, 8 Apr 2026 01:44:21 +0200 Subject: [PATCH] fix: Debounce resize and cache loaded images Add a 100ms single-shot QTimer to debounce Resize events and avoid reloading/layout on every resize. Cache the loaded QImage in _cached_qimage/_cached_path and only reload from disk when the image path changes. Extract layout/apply logic into _apply_layout and clear cache in update_image_overlay when a new path is provided. These changes reduce disk I/O and repeated layout work during rapid resizes. --- src/interface/dialogs/image.py | 38 +++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/interface/dialogs/image.py b/src/interface/dialogs/image.py index f921958..922ce46 100644 --- a/src/interface/dialogs/image.py +++ b/src/interface/dialogs/image.py @@ -1,5 +1,5 @@ from PySide6.QtWidgets import QLabel, QGraphicsOpacityEffect, QStackedWidget -from PySide6.QtCore import Qt, QSize, QEvent, QObject +from PySide6.QtCore import Qt, QSize, QEvent, QObject, QTimer from PySide6.QtGui import QImage, QPixmap from utils.data.state import state import darkdetect @@ -17,8 +17,15 @@ class Image(QObject): self.overlay_label.setGraphicsEffect(self.opacity_effect) self._current_image_path = None + self._cached_qimage = None self._wallpaper_active = False self._original_stylesheets = {} + + self._resize_timer = QTimer(self) + self._resize_timer.setSingleShot(True) + self._resize_timer.setInterval(100) + self._resize_timer.timeout.connect(self._on_resize_finished) + parent.installEventFilter(self) state.image_changed.connect(self.update_image_overlay) @@ -28,9 +35,13 @@ class Image(QObject): def eventFilter(self, obj, event): if obj == self.application and event.type() == QEvent.Type.Resize: if self._current_image_path: - self._load_and_display(self._current_image_path) + self._resize_timer.start() return False + def _on_resize_finished(self): + if self._current_image_path: + self._apply_layout() + def _set_wallpaper_transparency(self, enabled): if self._wallpaper_active == enabled: return @@ -117,12 +128,27 @@ class Image(QObject): self._set_wallpaper_transparency(False) return self._current_image_path = image_path - parent = self.application - image = QImage(image_path) - if image.isNull(): + + # Only reload from disk when the path actually changed + if self._cached_qimage is None or image_path != getattr(self, '_cached_path', None): + image = QImage(image_path) + if image.isNull(): + self.overlay_label.hide() + self._cached_qimage = None + return + self._cached_qimage = image + self._cached_path = image_path + + self._apply_layout() + + def _apply_layout(self): + if self._cached_qimage is None or not state.image_enabled: self.overlay_label.hide() return + parent = self.application + image = self._cached_qimage + if getattr(state, "image_as_wallpaper", False): scaled_image = image.scaled( parent.size(), @@ -184,4 +210,6 @@ class Image(QObject): self.overlay_label.show() def update_image_overlay(self, new_image_path): + if new_image_path != getattr(self, '_cached_path', None): + self._cached_qimage = None self._load_and_display(new_image_path) \ No newline at end of file