22import re
33
44from PyQt6 .QtCore import (
5+ QEvent ,
56 QObject ,
67 QPropertyAnimation ,
78 QRect ,
1415 pyqtProperty ,
1516 pyqtSignal ,
1617)
17- from PyQt6 .QtGui import QImageReader , QKeySequence , QPainter , QPainterPath , QPixmap , QShortcut
18+ from PyQt6 .QtGui import QCursor , QImageReader , QKeySequence , QPainter , QPainterPath , QPixmap , QShortcut
1819from PyQt6 .QtWidgets import (
1920 QApplication ,
2021 QGraphicsOpacityEffect ,
@@ -48,6 +49,22 @@ def apply_stylesheet(self):
4849 "wallpapers-gallery-image:hover" ,
4950 ]
5051 filtered_stylesheet = self .extract_class_styles (stylesheet , classes_to_include )
52+
53+ # Add default background if .wallpapers-gallery-window doesn't have one
54+ if ".wallpapers-gallery-window" in filtered_stylesheet :
55+ # Check if background property exists in the wallpapers-gallery-window class
56+ # We need to check that to prevent clicking issues on transparent areas where click goes through
57+ window_style_match = re .search (r"\.wallpapers-gallery-window\s*\{([^}]*)\}" , filtered_stylesheet , re .DOTALL )
58+ if window_style_match :
59+ style_content = window_style_match .group (1 )
60+ if not re .search (r"background(-color)?\s*:" , style_content , re .IGNORECASE ):
61+ filtered_stylesheet = re .sub (
62+ r"(\.wallpapers-gallery-window\s*\{)" , r"\1 background: rgba(0,0,0,0.01);" , filtered_stylesheet
63+ )
64+ else :
65+ # Class doesn't exist, add it with default background
66+ filtered_stylesheet += "\n .wallpapers-gallery-window { background: rgba(0,0,0,0.01); }"
67+
5168 self .setStyleSheet (filtered_stylesheet )
5269
5370 def extract_class_styles (self , stylesheet , classes ):
@@ -232,6 +249,7 @@ def __init__(self, image_folder, gallery):
232249 self .threadpool = QThreadPool ()
233250 self .threadpool .setMaxThreadCount (self .images_per_page )
234251 self .apply_stylesheet ()
252+ self .is_closing = False
235253
236254 def initUI (self , parent = None ):
237255 """Initialize the UI components and layout for the wallpapers gallery window."""
@@ -523,6 +541,10 @@ def fade_in_gallery(self, parent=None):
523541
524542 def fade_out_and_close_gallery (self ):
525543 """Close the gallery with a fade-out animation."""
544+ if self .is_closing :
545+ return
546+ self .is_closing = True
547+
526548 self .fade_out_animation = QPropertyAnimation (self , b"windowOpacity" )
527549 self .fade_out_animation .setDuration (200 )
528550 self .fade_out_animation .setStartValue (1 )
@@ -539,3 +561,17 @@ def _on_fade_out_finished(self):
539561 import gc
540562
541563 gc .collect ()
564+
565+ def mousePressEvent (self , event ):
566+ """Handle mouse press events on the window itself."""
567+ super ().mousePressEvent (event )
568+ event .accept ()
569+
570+ def changeEvent (self , event ):
571+ """Handle window state changes - close when window becomes inactive."""
572+ if event .type () == QEvent .Type .ActivationChange :
573+ if not self .isActiveWindow () and not self .is_closing :
574+ cursor_pos = QCursor .pos ()
575+ if not self .geometry ().contains (cursor_pos ):
576+ self .fade_out_and_close_gallery ()
577+ super ().changeEvent (event )
0 commit comments