the_goth_cardgame_collection/view/widgets/bars/goth_bottombar.py

201 lines
7.2 KiB
Python

# -*- coding: utf-8 -*-
# view/widgets/bars/goth_bottombar.py
from __future__ import annotations
import tkinter as tk
from resources.code_res.game_constants import BG_COLOR
from view.layout_mode import LayoutMode
from view.widgets.bars.goth_desktop_bottombar import (
BG_SAFE_BOTTOM_RATIO,
DesktopGothBottombar,
)
from view.widgets.bars.goth_more_bottombar import (
COMPACT_BOTTOMBAR_TOTAL_H,
CompactMoreBottombar,
)
from view.widgets.buttons.image_button import ImageButton
class GothBottombar(tk.Frame):
"""
Vorgeschalteter Größen-Regler für die universelle Fußleiste aller Spiele:
- Normales Fenster: zeigt `DesktopGothBottombar` (HUD links + Buttons rechts)
- Kompaktes Fenster: übergibt HUD/Status an die `CompactBurgerTopbar` oben rechts
und nutzt unten die volle Breite für Schnellzugriffs-Buttons (`CompactMoreBottombar`).
"""
def __init__(self, parent, ctx, hud_var: tk.StringVar, status_var: tk.StringVar):
self.ctx = ctx
self.assets = ctx.assets
self.hud_var = hud_var
self.status_var = status_var
self._root_win = ctx.root
# HUD-Variablen am Kontext registrieren, damit die Burger-Topbar sie oben rechts anzeigen kann
setattr(self.ctx, "_active_hud_var", self.hud_var)
setattr(self.ctx, "_active_status_var", self.status_var)
rw = max(480, self._root_win.winfo_width())
rh = max(620, self._root_win.winfo_height())
self._is_compact = LayoutMode.is_compact(rw, rh)
init_h = (
COMPACT_BOTTOMBAR_TOTAL_H
if self._is_compact
else (48 + max(56, int(rh * BG_SAFE_BOTTOM_RATIO)))
)
super().__init__(parent, height=init_h, bg=BG_COLOR, bd=0, highlightthickness=0)
self.pack_propagate(False)
self._buttons: list[ImageButton] = []
self._desktop_bar: DesktopGothBottombar | None = None
self._compact_bar: CompactMoreBottombar | None = None
self._sync_job: str | None = None
self._root_cfg_bind_id: str | None = None
self.hud_var.trace_add("write", lambda *_: self.refresh_texts())
self.status_var.trace_add("write", lambda *_: self.refresh_texts())
self._apply_mode_widget(force=True)
self._notify_topbar_hud()
self.bind("<Destroy>", self._on_destroy)
try:
self._root_cfg_bind_id = self._root_win.bind(
"<Configure>", self._on_root_configure, add="+"
)
except tk.TclError:
self._root_cfg_bind_id = None
@property
def total_height(self) -> int:
return COMPACT_BOTTOMBAR_TOTAL_H if self._is_compact else int(self.cget("height"))
def _notify_topbar_hud(self):
cb = getattr(self.ctx, "_compact_topbar_hud_cb", None)
if callable(cb):
cb()
def set_buttons(
self,
buttons: list[ImageButton],
overflow_buttons: list[ImageButton] | None = None,
):
primary_list = list(buttons)
extra_list = list(overflow_buttons) if overflow_buttons else []
for b in primary_list:
setattr(b, "can_overflow", False)
for b in extra_list:
setattr(b, "can_overflow", True)
combined = primary_list + extra_list
keep_ids = {id(b) for b in combined}
for old_b in self._buttons:
if id(old_b) not in keep_ids:
try:
if old_b.winfo_exists():
old_b.place_forget()
old_b.destroy()
except Exception:
pass
self._buttons = combined
if self._desktop_bar is not None:
self._desktop_bar._last_btn_split = None
if self._compact_bar is not None:
self._compact_bar.close_drawer()
self._compact_bar._last_layout_sig = None
self.relayout()
self.after(20, self.relayout)
def refresh_texts(self):
if not self.winfo_exists():
return
if self._is_compact:
self._notify_topbar_hud()
elif self._desktop_bar is not None:
self._desktop_bar.refresh_texts()
def relayout(self):
if not self.winfo_exists():
return
self._apply_mode_widget(force=False)
if self._is_compact and self._compact_bar is not None:
self._compact_bar.relayout(force=True)
self._notify_topbar_hud()
elif not self._is_compact and self._desktop_bar is not None:
target_h = self._desktop_bar.desired_height
if int(self.cget("height")) != target_h:
self.configure(height=target_h)
self._desktop_bar.relayout()
def _apply_mode_widget(self, force: bool = False):
rw = max(480, self._root_win.winfo_width())
rh = max(620, self._root_win.winfo_height())
want_compact = LayoutMode.is_compact(rw, rh)
if not force and want_compact == self._is_compact:
return
self._is_compact = want_compact
if want_compact:
if self._desktop_bar is not None:
self._desktop_bar.pack_forget()
if self._compact_bar is None:
self._compact_bar = CompactMoreBottombar(self, self)
self.configure(height=COMPACT_BOTTOMBAR_TOTAL_H)
self._compact_bar.pack(fill="both", expand=True)
tk.Misc.lower(self._compact_bar)
self._compact_bar._last_layout_sig = None
self._compact_bar.relayout(force=True)
self._notify_topbar_hud()
else:
if self._compact_bar is not None:
self._compact_bar.close_drawer()
self._compact_bar.pack_forget()
if self._desktop_bar is None:
self._desktop_bar = DesktopGothBottombar(self, self)
target_h = self._desktop_bar.desired_height
self.configure(height=target_h)
self. _desktop_bar.pack(fill="both", expand=True)
tk.Misc.lower(self._desktop_bar)
self._desktop_bar._last_btn_split = None
self._desktop_bar.relayout()
def _on_destroy(self, event=None):
if event is not None and event.widget is not self:
return
if getattr(self.ctx, "_active_hud_var", None) is self.hud_var:
setattr(self.ctx, "_active_hud_var", None)
setattr(self.ctx, "_active_status_var", None)
self._notify_topbar_hud()
if self._sync_job is not None:
try:
self.after_cancel(self._sync_job)
except Exception:
pass
self._sync_job = None
if self._root_cfg_bind_id is not None:
try:
self._root_win.unbind("<Configure>", self._root_cfg_bind_id)
except Exception:
pass
self._root_cfg_bind_id = None
def _on_root_configure(self, event=None):
if event is not None and event.widget is not self._root_win:
return
if not self.winfo_exists():
return
if self._sync_job is not None:
try:
self.after_cancel(self._sync_job)
except Exception:
pass
self._sync_job = self.after(18, self.relayout)
SolitaireBottomBar = GothBottombar