# -*- coding: utf-8 -*- # view/window_state.py import re from models.database import Database GEOM_RE = re.compile(r"^(\d+)x(\d+)\+(-?\d+)\+(-?\d+)$") class WindowStatePersistence: """Merkt sich Fenstergröße, Position (x/y) sowie Maximiert-/Fullscreen-Zustand über Neustarts hinweg.""" def __init__(self, root): self.root = root self._save_job = None self._last_normal_geom: str = Database.load_setting("window_geometry", "") self._restore() self.root.bind("", self._on_configure, add="+") self.root.protocol("WM_DELETE_WINDOW", self._on_close) def _restore(self): saved_geom = self._last_normal_geom was_zoomed = Database.load_setting("window_zoomed", "0") == "1" was_fullscreen = Database.load_setting("window_fullscreen", "0") == "1" if saved_geom and self._is_visible_on_screen(saved_geom): try: self.root.geometry(saved_geom) except Exception: pass if was_fullscreen: try: self.root.attributes("-fullscreen", True) return except Exception: pass if was_zoomed: try: self.root.state("zoomed") except Exception: try: self.root.attributes("-zoomed", True) except Exception: pass def _is_visible_on_screen(self, geom: str) -> bool: m = GEOM_RE.match(geom) if not m: return False w, h, x, y = map(int, m.groups()) if w < 600 or h < 450: return False sw = self.root.winfo_screenwidth() sh = self.root.winfo_screenheight() return (-w + 120 < x < sw - 80) and (-20 <= y < sh - 80) def _is_currently_zoomed(self) -> bool: try: if self.root.state() == "zoomed": return True except Exception: pass try: return bool(self.root.attributes("-zoomed")) except Exception: return False def _is_currently_fullscreen(self) -> bool: try: return bool(self.root.attributes("-fullscreen")) except Exception: return False def _on_configure(self, event): if event.widget is not self.root: return if self._save_job is not None: try: self.root.after_cancel(self._save_job) except Exception: pass self._save_job = self.root.after(350, self.persist_now) def persist_now(self): self._save_job = None if not self.root.winfo_exists(): return try: st = self.root.state() except Exception: st = "normal" if st == "iconic": return is_fs = self._is_currently_fullscreen() is_zm = self._is_currently_zoomed() if not is_fs and not is_zm and st == "normal": cur_geom = self.root.geometry() if GEOM_RE.match(cur_geom): self._last_normal_geom = cur_geom Database.save_setting("window_geometry", cur_geom) Database.save_setting("window_zoomed", "1" if is_zm else "0") Database.save_setting("window_fullscreen", "1" if is_fs else "0") def _on_close(self): self.persist_now() self.root.destroy()