"""Выпадающие панели внутри окна приложения. Раньше чипсы движка и модели открывали отдельные Toplevel-окна: они жили поверх приложения, перекрывали топбар и не всегда помещались на экране. Теперь список раскрывается внутри `app.main`, под своей чипсой, и закрывается по клику мимо или по Escape. """ from __future__ import annotations import customtkinter as ctk from ..theme import c class InlineDropdown(ctk.CTkFrame): def __init__(self, app, anchor, width: int = 420, height: int = 400): parent = getattr(app, "main", app) width = int(width) height = int(height) super().__init__( parent, width=width, height=height, fg_color=c("panel"), corner_radius=12, border_width=1, border_color=c("border_strong"), ) self.pack_propagate(False) self.grid_propagate(False) self.app = app self.anchor = anchor self._parent = parent self._panel_width = width self._panel_height = height self._closed = False self._bindings: list[tuple] = [] self._bind_global_events() self.mount() # ---------- позиционирование ---------- def resize(self, height: int): self._panel_height = max(160, int(height)) self.mount() def mount(self): if self._closed: return try: self._parent.update_idletasks() parent_w = int(self._parent.winfo_width()) parent_h = int(self._parent.winfo_height()) if parent_w <= 1 or parent_h <= 1: return ax = self.anchor.winfo_rootx() - self._parent.winfo_rootx() ay = self.anchor.winfo_rooty() - self._parent.winfo_rooty() width = min(self._panel_width, max(240, parent_w - 16)) height = min(self._panel_height, max(180, parent_h - 16)) x = ax + (self.anchor.winfo_width() - width) // 2 x = max(8, min(x, parent_w - width - 8)) y = ay + self.anchor.winfo_height() + 6 if y + height > parent_h - 8: y = max(8, ay - height - 6) self.configure(width=width, height=height) self.place(x=x, y=y) self.lift() except Exception: pass # ---------- закрытие ---------- def _bind_global_events(self): self._bindings = [ (self.app, "", self.app.bind("", self._on_global_click, add="+")), (self.app, "", self.app.bind("", self._on_escape, add="+")), (self.app, "", self.app.bind("", self._on_app_configure, add="+")), ] def _unbind_global_events(self): for widget, sequence, funcid in self._bindings: try: widget.unbind(sequence, funcid) except Exception: pass self._bindings = [] def _on_global_click(self, event=None): widget = getattr(event, "widget", None) while widget is not None: if widget is self or widget is self.anchor: return widget = getattr(widget, "master", None) self.close() def _on_escape(self, _event=None): self.close() return "break" def _on_app_configure(self, event=None): if event is not None and getattr(event, "widget", None) is not self.app: return self.after_idle(self.mount) def close(self): if self._closed: return self._closed = True self._unbind_global_events() for attr in ("_model_picker", "_engine_picker"): try: if getattr(self.app, attr, None) is self: setattr(self.app, attr, None) except Exception: pass try: self.destroy() except Exception: pass def destroy(self): self._unbind_global_events() super().destroy()