168 lines
5.9 KiB
Python
168 lines
5.9 KiB
Python
# -*- 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
|
|
|
|
APP_WM_CLASS = "TheGothCardgameCollection"
|
|
|
|
|
|
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)
|
|
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()))
|
|
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:
|
|
try:
|
|
self.after_cancel(self._geom_save_job)
|
|
except Exception:
|
|
pass
|
|
self._geom_save_job = self.after(400, self._save_window_state)
|
|
|
|
def _save_window_state(self):
|
|
self._geom_save_job = None
|
|
try:
|
|
if not self.winfo_exists():
|
|
return
|
|
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
|
|
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)
|
|
|
|
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) |