|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +UI Scaling module for FastFlix. |
| 4 | +
|
| 5 | +Provides a singleton UIScaler that manages scale factors for the entire application. |
| 6 | +Scale factors are computed based on the current window size relative to the base |
| 7 | +reference size of 1200x680. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import copy |
| 13 | +from dataclasses import dataclass |
| 14 | +from typing import Callable |
| 15 | + |
| 16 | +from PySide6 import QtCore |
| 17 | + |
| 18 | +BASE_WIDTH = 1200 |
| 19 | +BASE_HEIGHT = 680 |
| 20 | + |
| 21 | + |
| 22 | +@dataclass |
| 23 | +class ScaleFactors: |
| 24 | + """Scale factors for UI elements - immutable, use copy.replace() to modify.""" |
| 25 | + |
| 26 | + width: float = 1.0 |
| 27 | + height: float = 1.0 |
| 28 | + uniform: float = 1.0 |
| 29 | + font: float = 1.0 |
| 30 | + icon: float = 1.0 |
| 31 | + |
| 32 | + |
| 33 | +class UIScaler: |
| 34 | + """Singleton for managing UI scaling throughout the application.""" |
| 35 | + |
| 36 | + _instance: UIScaler | None = None |
| 37 | + |
| 38 | + def __new__(cls) -> UIScaler: |
| 39 | + if cls._instance is None: |
| 40 | + cls._instance = super().__new__(cls) |
| 41 | + cls._instance._initialized = False |
| 42 | + return cls._instance |
| 43 | + |
| 44 | + def __init__(self) -> None: |
| 45 | + if self._initialized: |
| 46 | + return |
| 47 | + self._initialized = True |
| 48 | + self.factors = ScaleFactors() |
| 49 | + self._listeners: list[Callable[[ScaleFactors], None]] = [] |
| 50 | + |
| 51 | + def calculate_factors(self, width: int, height: int) -> None: |
| 52 | + """Calculate and update scale factors based on current window dimensions.""" |
| 53 | + width_factor = width / BASE_WIDTH |
| 54 | + height_factor = height / BASE_HEIGHT |
| 55 | + uniform = min(width_factor, height_factor) |
| 56 | + |
| 57 | + # Use Python 3.13 copy.replace() for immutable update |
| 58 | + self.factors = copy.replace( |
| 59 | + self.factors, |
| 60 | + width=width_factor, |
| 61 | + height=height_factor, |
| 62 | + uniform=uniform, |
| 63 | + font=uniform, |
| 64 | + icon=uniform, |
| 65 | + ) |
| 66 | + self._notify_listeners() |
| 67 | + |
| 68 | + def scale(self, base_value: int) -> int: |
| 69 | + """Scale a base value by the uniform scale factor.""" |
| 70 | + return max(1, int(base_value * self.factors.uniform)) |
| 71 | + |
| 72 | + def scale_font(self, base_size: int) -> int: |
| 73 | + """Scale a font size, with minimum of 8px for readability.""" |
| 74 | + return max(8, int(base_size * self.factors.font)) |
| 75 | + |
| 76 | + def scale_icon(self, base_size: int) -> int: |
| 77 | + """Scale an icon size, with minimum of 10px for visibility.""" |
| 78 | + return max(10, int(base_size * self.factors.icon)) |
| 79 | + |
| 80 | + def scale_size(self, width: int, height: int) -> QtCore.QSize: |
| 81 | + """Scale a width/height pair and return as QSize.""" |
| 82 | + return QtCore.QSize(self.scale(width), self.scale(height)) |
| 83 | + |
| 84 | + def add_listener(self, callback: Callable[[ScaleFactors], None]) -> None: |
| 85 | + """Register a callback to be notified when scale factors change.""" |
| 86 | + self._listeners.append(callback) |
| 87 | + |
| 88 | + def remove_listener(self, callback: Callable[[ScaleFactors], None]) -> None: |
| 89 | + """Unregister a previously registered callback.""" |
| 90 | + if callback in self._listeners: |
| 91 | + self._listeners.remove(callback) |
| 92 | + |
| 93 | + def _notify_listeners(self) -> None: |
| 94 | + """Notify all registered listeners of scale factor changes.""" |
| 95 | + for callback in self._listeners: |
| 96 | + callback(self.factors) |
| 97 | + |
| 98 | + |
| 99 | +# Global singleton instance |
| 100 | +scaler = UIScaler() |
0 commit comments