the_goth_cardgame_collection/view/main_window.py

198 lines
6.7 KiB
Python
Raw Permalink Normal View History

2026-11-14 04:25:51 +00:00
# -*- coding: utf-8 -*-
# view/main_window.py
import sys
import tkinter as tk
from tkinter import ttk
from pathlib import Path
from models.database import Database
from resources.code_res.game_constants import (
ACCENT_GOTH, BG_COLOR, BORDER_COLOR, DEFAULT_WINDOW_GEOM,
FG_COLOR, INPUT_BG, MIN_WINDOW_SIZE, PANEL_BG, SUBTLE_BORDER
)
from resources.code_res.i18n import t
from view.assets_manager import AssetManager
from view.menus.login_view import LoginView
from view.menus.main_menu_view import MainMenuView
from view.menus.settings_view import SettingsView
from view.menus.profile_view import ProfileView
2026-11-14 04:25:51 +00:00
APP_WM_CLASS = "TheGothCardgameCollection"
2026-11-14 04:25:51 +00:00
class GothCardCollectionView(tk.Tk):
def __init__(self, ctrl, root_dir: Path):
super().__init__(baseName=APP_WM_CLASS, className=APP_WM_CLASS)
if sys.platform.startswith("linux"):
self.option_add("*Label.padY", 4)
2026-11-14 04:25:51 +00:00
self.ctrl = ctrl
self.title(t("app_title"))
self.configure(bg=BG_COLOR)
self.minsize(*MIN_WINDOW_SIZE)
self._restore_window_geometry()
self._setup_global_ttk_styles()
self._geom_save_job = None
self.bind("<Configure>", self._on_window_configure)
self.protocol("WM_DELETE_WINDOW", self.close_app)
self.assets = AssetManager(root_dir)
self.update_idletasks()
self.assets.preload_startup_assets((self.winfo_width(), self.winfo_height()))
2026-11-14 04:25:51 +00:00
self.bg_widget = None
self.login_view = LoginView(self)
self.main_menu_view = MainMenuView(self)
self.settings_view = SettingsView(self)
self.profile_view = ProfileView(self)
2026-11-14 04:25:51 +00:00
def _setup_global_ttk_styles(self):
style = ttk.Style()
style.theme_use("clam")
style.configure(
"Goth.TCombobox",
fieldbackground=INPUT_BG,
background=PANEL_BG,
foreground=FG_COLOR,
darkcolor=INPUT_BG,
lightcolor=INPUT_BG,
bordercolor=BORDER_COLOR,
arrowcolor=ACCENT_GOTH,
)
style.map(
"Goth.TCombobox",
fieldbackground=[("readonly", INPUT_BG)],
foreground=[("readonly", FG_COLOR)],
)
style.configure(
"Goth.Horizontal.TProgressbar",
troughcolor=BG_COLOR,
background=ACCENT_GOTH,
bordercolor=SUBTLE_BORDER,
lightcolor=ACCENT_GOTH,
darkcolor=ACCENT_GOTH,
)
def _restore_window_geometry(self):
saved_geom = Database.load_setting("window_geometry", DEFAULT_WINDOW_GEOM)
saved_zoom = Database.load_setting("window_zoomed", "0")
try:
self.geometry(saved_geom)
if saved_zoom == "1":
if sys.platform == "darwin":
self.geometry(f"{self.winfo_screenwidth()}x{self.winfo_screenheight()}+0+0")
else:
try:
self.attributes("-zoomed", True)
except Exception:
self.state("zoomed")
except Exception:
self.geometry(DEFAULT_WINDOW_GEOM)
def _on_window_configure(self, event):
if event.widget == self:
if self._geom_save_job:
try:
self.after_cancel(self._geom_save_job)
except Exception:
pass
2026-11-14 04:25:51 +00:00
self._geom_save_job = self.after(400, self._save_window_state)
def _save_window_state(self):
self._geom_save_job = None
2026-11-14 04:25:51 +00:00
try:
if not self.winfo_exists():
return
2026-11-14 04:25:51 +00:00
is_zoomed = False
if sys.platform == "darwin":
is_zoomed = (
self.winfo_width() >= self.winfo_screenwidth() - 20
and self.winfo_height() >= self.winfo_screenheight() - 80
)
else:
try:
is_zoomed = bool(self.attributes("-zoomed"))
except Exception:
is_zoomed = (self.state() == "zoomed")
Database.save_setting("window_zoomed", "1" if is_zoomed else "0")
min_w, min_h = MIN_WINDOW_SIZE
if not is_zoomed and self.winfo_width() >= min_w and self.winfo_height() >= min_h:
Database.save_setting(
"window_geometry",
f"{self.winfo_width()}x{self.winfo_height()}+{self.winfo_x()}+{self.winfo_y()}"
)
except Exception:
pass
def close_app(self):
if self._geom_save_job:
try:
self.after_cancel(self._geom_save_job)
except Exception:
pass
self._geom_save_job = None
2026-11-14 04:25:51 +00:00
self._save_window_state()
Database.close()
self.destroy()
# Unten in GothCardCollectionView (view/main_window.py):
def clear_window(self, apply_global_bg: bool = True):
bg = getattr(self, "bg_widget", None)
if apply_global_bg and (bg is None or not bg.winfo_exists()):
from view.widgets.resizing_bg import ResizingBG
self.bg_widget = ResizingBG(self, self.assets)
self.bg_widget.place(x=0, y=0, relwidth=1, relheight=1)
bg = self.bg_widget
for child in list(self.winfo_children()):
if child is bg:
continue
try:
child.destroy()
except Exception:
pass
if bg is not None and bg.winfo_exists():
bg.clear_foreground()
if apply_global_bg:
tk.Misc.lower(bg)
2026-11-14 04:25:51 +00:00
def show_login(
self,
profiles: list[str],
preselect_name: str = "",
status_msg: str = "",
is_error: bool = False,
login_mode: str = "local",
):
self.login_view.show(
profiles,
preselect_name=preselect_name,
status_msg=status_msg,
is_error=is_error,
login_mode=login_mode,
)
2026-11-14 04:25:51 +00:00
def show_main_menu(self, profile: dict, available_games: list[dict], devils_resume_only: bool = False):
self.main_menu_view.show(profile, available_games, devils_resume_only=devils_resume_only)
def show_settings(
self,
on_back=None,
in_game_overlay: bool = False,
on_ingame_lang_refresh=None,
feedback_txt: str = "",
feedback_err: bool = False,
):
self.settings_view.show(
on_back=on_back,
in_game_overlay=in_game_overlay,
on_ingame_lang_refresh=on_ingame_lang_refresh,
feedback_txt=feedback_txt,
feedback_err=feedback_err,
)
def show_profile(self, on_back=None):
self.profile_view.show(on_back=on_back)