the_goth_cardgame_collection/view/main_window.py

134 lines
4.8 KiB
Python
Raw 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.statistics_view import StatisticsView
from view.widgets.resizing_bg import ResizingBG
class GothCardCollectionView(tk.Tk):
def __init__(self, ctrl, root_dir: Path):
super().__init__()
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.bg_widget = None
self.login_view = LoginView(self)
self.main_menu_view = MainMenuView(self)
self.statistics_view = StatisticsView(self)
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:
self.after_cancel(self._geom_save_job)
self._geom_save_job = self.after(400, self._save_window_state)
def _save_window_state(self):
try:
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):
self._save_window_state()
Database.close()
self.destroy()
def clear_window(self, apply_global_bg=True):
for widget in self.winfo_children():
widget.destroy()
if apply_global_bg and self.assets.raw_bg:
self.bg_widget = ResizingBG(self, self.assets.raw_bg, bg=BG_COLOR)
self.bg_widget.place(relx=0, rely=0, relwidth=1, relheight=1)
def show_login(self, profiles: list[str], preselect_name: str = ""):
self.login_view.show(profiles, preselect_name)
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_statistics(self, profile: dict):
self.statistics_view.show(profile)