mirror of
https://github.com/KeksPirates/SoftwareManager.git
synced 2026-08-03 17:39:42 +02:00
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.
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
from PySide6.QtWidgets import QLabel, QGraphicsOpacityEffect, QStackedWidget
|
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 PySide6.QtGui import QImage, QPixmap
|
||||||
from utils.data.state import state
|
from utils.data.state import state
|
||||||
import darkdetect
|
import darkdetect
|
||||||
@@ -17,8 +17,15 @@ class Image(QObject):
|
|||||||
self.overlay_label.setGraphicsEffect(self.opacity_effect)
|
self.overlay_label.setGraphicsEffect(self.opacity_effect)
|
||||||
|
|
||||||
self._current_image_path = None
|
self._current_image_path = None
|
||||||
|
self._cached_qimage = None
|
||||||
self._wallpaper_active = False
|
self._wallpaper_active = False
|
||||||
self._original_stylesheets = {}
|
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)
|
parent.installEventFilter(self)
|
||||||
state.image_changed.connect(self.update_image_overlay)
|
state.image_changed.connect(self.update_image_overlay)
|
||||||
|
|
||||||
@@ -28,9 +35,13 @@ class Image(QObject):
|
|||||||
def eventFilter(self, obj, event):
|
def eventFilter(self, obj, event):
|
||||||
if obj == self.application and event.type() == QEvent.Type.Resize:
|
if obj == self.application and event.type() == QEvent.Type.Resize:
|
||||||
if self._current_image_path:
|
if self._current_image_path:
|
||||||
self._load_and_display(self._current_image_path)
|
self._resize_timer.start()
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def _on_resize_finished(self):
|
||||||
|
if self._current_image_path:
|
||||||
|
self._apply_layout()
|
||||||
|
|
||||||
def _set_wallpaper_transparency(self, enabled):
|
def _set_wallpaper_transparency(self, enabled):
|
||||||
if self._wallpaper_active == enabled:
|
if self._wallpaper_active == enabled:
|
||||||
return
|
return
|
||||||
@@ -117,12 +128,27 @@ class Image(QObject):
|
|||||||
self._set_wallpaper_transparency(False)
|
self._set_wallpaper_transparency(False)
|
||||||
return
|
return
|
||||||
self._current_image_path = image_path
|
self._current_image_path = image_path
|
||||||
parent = self.application
|
|
||||||
image = QImage(image_path)
|
# Only reload from disk when the path actually changed
|
||||||
if image.isNull():
|
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()
|
self.overlay_label.hide()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
parent = self.application
|
||||||
|
image = self._cached_qimage
|
||||||
|
|
||||||
if getattr(state, "image_as_wallpaper", False):
|
if getattr(state, "image_as_wallpaper", False):
|
||||||
scaled_image = image.scaled(
|
scaled_image = image.scaled(
|
||||||
parent.size(),
|
parent.size(),
|
||||||
@@ -184,4 +210,6 @@ class Image(QObject):
|
|||||||
self.overlay_label.show()
|
self.overlay_label.show()
|
||||||
|
|
||||||
def update_image_overlay(self, new_image_path):
|
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)
|
self._load_and_display(new_image_path)
|
||||||
Reference in New Issue
Block a user