@jazzycamel Although your post is almost 7 years old at this point, it still works flawlessly. I created an account just to thank you for this haha :D (PS - For others stumbling upon this, here's a simple implementation for PyQt6)
import collections
from PyQt6.QtCore import QObject
class Singleton(type(QObject)):
def __init__(cls, name, bases, dict):
super().__init__(name, bases, dict)
cls.instance = None
def __call__(cls, *args, **kw):
if cls.instance is None:
cls.instance = super().__call__(*args, **kw)
return cls.instance
class WindowManager(QObject, metaclass=Singleton):
def __init__(self, parent=None, **kwargs) -> None:
# Always call the parent class's __init__ first
super().__init__(parent, **kwargs)
# Window configuration
self.window_width = 550 # Fixed width for equation windows
self.window_height = 250 # Fixed height for equation windows
self.window_margin = 20 # Margin from screen edge
self.vertical_gap = 20 # Gap between windows
# Calculate max windows that fit on screen
self.max_windows = 3
# Use deque with dynamic maxlen to auto-remove old windows
self.active_windows = collections.deque(maxlen=self.max_windows)
self.window_positions = [] # Track positions of windows
window_manager = WindowManager()