# -*- coding: utf-8 -*- # view/widgets/modal_veil.py from __future__ import annotations import tkinter as tk class ModalVeil: """ Universeller Modal-Blocker & Abdunklungs-Schleier: - Legt einen halbtransparenten RGBA-Schleier über das aktive Spielfeld-Canvas, sodass Karten/Chips darunter ausgegraut sichtbar bleiben. - Hebt das übergebene Modal-Fenster über alle UI-Leisten (Topbar, Bottom-Bar, Tisch). - Blockiert sämtliche Klicks, Hover-Events und Tastenkürzel außerhalb des Modals. """ _SEQ_COUNTER = 0 def __init__( self, root: tk.Misc, assets, modal_widget: tk.Widget | None = None, target_canvas: tk.Canvas | None = None, dim_alpha: int = 148, ): ModalVeil._SEQ_COUNTER += 1 self._id = ModalVeil._SEQ_COUNTER self.root = root.winfo_toplevel() self.assets = assets self.modal_widget = modal_widget self.dim_alpha = dim_alpha self._veil_tag = f"modal_dim_veil_{self._id}" self._block_tag = f"ModalBlockTag_{self._id}" self._released = False self._tagged_widgets: list[tk.Misc] = [] self._cfg_bind_id: str | None = None self._resize_job: str | None = None self.canvas = target_canvas or self._find_play_canvas() self._draw_veil() self._install_input_lock() if self.modal_widget is not None: tk.Widget.tkraise(self.modal_widget) self.modal_widget.bind("", self._on_modal_destroy, add="+") try: self._cfg_bind_id = self.root.bind("", self._on_root_configure, add="+") except tk.TclError: self._cfg_bind_id = None def _find_play_canvas(self) -> tk.Canvas | None: best: tk.Canvas | None = None best_area = 0 candidates = list(self.root.winfo_children()) for child in list(candidates): if isinstance(child, tk.Frame): candidates.extend(child.winfo_children()) for w in candidates: if w is self.modal_widget or not isinstance(w, tk.Canvas): continue try: if not w.winfo_ismapped(): continue cw, ch = w.winfo_width(), w.winfo_height() except tk.TclError: continue if ch < 150: continue area = cw * ch if area > best_area: best_area = area best = w return best def _is_inside_modal(self, widget: tk.Misc | None) -> bool: if self.modal_widget is None or widget is None: return False cur: tk.Misc | None = widget while cur is not None: if cur is self.modal_widget: return True cur = getattr(cur, "master", None) return False def _consume_event(self, event): if self._is_inside_modal(getattr(event, "widget", None)): return None return "break" def _install_input_lock(self): blocked_seqs = ( "", "", "", "", "", "", "", "", "", "", "", ) for seq in blocked_seqs: try: self.root.bind_class(self._block_tag, seq, self._consume_event) except tk.TclError: pass self._apply_block_tag_recursive(self.root) def _apply_block_tag_recursive(self, node: tk.Misc): if node is self.modal_widget: return try: tags = list(node.bindtags()) if self._block_tag not in tags: node.bindtags((self._block_tag, *tags)) self._tagged_widgets.append(node) for child in node.winfo_children(): self._apply_block_tag_recursive(child) except tk.TclError: pass def _draw_veil(self): if self._released or self.canvas is None or self.assets is None: return try: if not self.canvas.winfo_exists(): return cw = max(64, self.canvas.winfo_width()) ch = max(64, self.canvas.winfo_height()) self.canvas.delete(self._veil_tag) veil_ph = self.assets.get_dim_veil_photo(cw, ch, alpha=self.dim_alpha) self.canvas.create_image( 0, 0, image=veil_ph, anchor="nw", tags=("modal_dim_veil", self._veil_tag), ) self.canvas.tag_raise(self._veil_tag) except tk.TclError: pass def _on_root_configure(self, _event=None): if self._released: return if self.modal_widget is not None: try: if self.modal_widget.winfo_exists(): tk.Widget.tkraise(self.modal_widget) except tk.TclError: pass if self._resize_job is not None: try: self.root.after_cancel(self._resize_job) except Exception: pass try: self._resize_job = self.root.after(60, self._refresh_after_resize) except Exception: self._resize_job = None def _refresh_after_resize(self): self._resize_job = None if self._released: return self._draw_veil() self._apply_block_tag_recursive(self.root) if self.modal_widget is not None: try: if self.modal_widget.winfo_exists(): tk.Widget.tkraise(self.modal_widget) except tk.TclError: pass def _on_modal_destroy(self, event=None): if event is not None and event.widget is not self.modal_widget: return self.release() def release(self): if self._released: return self._released = True if self._resize_job is not None: try: self.root.after_cancel(self._resize_job) except Exception: pass self._resize_job = None if self._cfg_bind_id is not None: try: self.root.unbind("", self._cfg_bind_id) except Exception: pass self._cfg_bind_id = None if self.canvas is not None: try: if self.canvas.winfo_exists(): self.canvas.delete(self._veil_tag) except Exception: pass for w in self._tagged_widgets: try: if w.winfo_exists(): tags = [t for t in w.bindtags() if t != self._block_tag] w.bindtags(tuple(tags)) except Exception: pass self._tagged_widgets.clear()