219 lines
7.4 KiB
Python
219 lines
7.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
import sys
|
|
import tempfile
|
|
from pathlib import Path
|
|
from PIL import Image, ImageTk
|
|
|
|
from controller.app_controller import AppController
|
|
from resources.code_res.game_constants import APP_ICON_REL_PATH
|
|
from view.font_loader import FontLoader
|
|
from view.main_window import GothCardCollectionView
|
|
from view.window_state import WindowStatePersistence
|
|
|
|
APP_WM_CLASS = "goth-cardgame-collection"
|
|
|
|
|
|
def _prepare_os_app_identity():
|
|
if sys.platform.startswith("win"):
|
|
try:
|
|
import ctypes
|
|
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(
|
|
"kuro.goth_cardgame_collection.app.1"
|
|
)
|
|
except Exception:
|
|
pass
|
|
elif sys.platform.startswith("linux"):
|
|
try:
|
|
import ctypes
|
|
libc = ctypes.CDLL("libc.so.6")
|
|
libc.prctl(15, APP_WM_CLASS.encode("utf-8")[:15], 0, 0, 0)
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
def _extract_ico_frames(icon_path: Path) -> tuple[Image.Image | None, list[Image.Image]]:
|
|
"""Liest das größte Bild aus game_icon.ico (auch wenn es intern PNG/JPEG/BMP-codiert ist)."""
|
|
try:
|
|
with Image.open(icon_path) as ico:
|
|
best_img = None
|
|
best_area = -1
|
|
|
|
ico_helper = getattr(ico, "ico", None)
|
|
if ico_helper is not None and hasattr(ico_helper, "sizes"):
|
|
for sz in ico_helper.sizes():
|
|
try:
|
|
frame = ico_helper.getimage(sz).convert("RGBA")
|
|
area = frame.width * frame.height
|
|
if area > best_area:
|
|
best_area = area
|
|
best_img = frame
|
|
except Exception:
|
|
continue
|
|
|
|
if best_img is None:
|
|
n_frames = getattr(ico, "n_frames", 1)
|
|
for idx in range(n_frames):
|
|
try:
|
|
ico.seek(idx)
|
|
frame = ico.copy().convert("RGBA")
|
|
area = frame.width * frame.height
|
|
if area > best_area:
|
|
best_area = area
|
|
best_img = frame
|
|
except Exception:
|
|
continue
|
|
|
|
if best_img is None:
|
|
best_img = ico.convert("RGBA")
|
|
|
|
cascade = [
|
|
best_img.resize((px, px), Image.Resampling.LANCZOS)
|
|
for px in (256, 128, 64, 48, 32, 16)
|
|
]
|
|
return best_img, cascade
|
|
except Exception:
|
|
return None, []
|
|
|
|
|
|
def _apply_windows_icon(app, icon_path: Path, best_pil: Image.Image | None):
|
|
"""
|
|
Setzt das Icon unter Windows nativ via Tk iconbitmap + Win32 WM_SETICON,
|
|
ohne dass iconphoto() es wieder mit der Standard-Feder überschreibt.
|
|
"""
|
|
import ctypes
|
|
|
|
target_ico = icon_path
|
|
|
|
# Falls die .ico versehentlich eine umbenannte PNG/JPG ist oder nur einen 256px-PNG-Header hat,
|
|
# den Tcl/Tk 8.6 auf Windows ablehnt -> saubere Multi-Size-BMP-ICO in Temp erzeugen:
|
|
if best_pil is not None:
|
|
try:
|
|
with open(icon_path, "rb") as f:
|
|
magic = f.read(4)
|
|
is_real_ico = (magic == b"\x00\x00\x01\x00")
|
|
except Exception:
|
|
is_real_ico = False
|
|
|
|
try:
|
|
if not is_real_ico:
|
|
raise ValueError("Kein nativer ICO-Header")
|
|
app.wm_iconbitmap(default=str(target_ico))
|
|
app.wm_iconbitmap(str(target_ico))
|
|
except Exception:
|
|
tmp_ico = Path(tempfile.gettempdir()) / "goth_cardgame_collection_win.ico"
|
|
best_pil.save(
|
|
tmp_ico,
|
|
format="ICO",
|
|
sizes=[(16, 16), (32, 32), (48, 48), (64, 64), (128, 128), (256, 256)],
|
|
)
|
|
target_ico = tmp_ico
|
|
try:
|
|
app.wm_iconbitmap(default=str(target_ico))
|
|
app.wm_iconbitmap(str(target_ico))
|
|
except Exception:
|
|
pass
|
|
else:
|
|
try:
|
|
app.wm_iconbitmap(default=str(target_ico))
|
|
app.wm_iconbitmap(str(target_ico))
|
|
except Exception:
|
|
pass
|
|
|
|
# Zusätzlich direkt per Win32 API ans echte Top-Level-HWND schicken (Titelleiste + Taskbar!)
|
|
try:
|
|
app.update_idletasks()
|
|
hwnd = ctypes.windll.user32.GetParent(app.winfo_id()) or app.winfo_id()
|
|
IMAGE_ICON = 1
|
|
LR_LOADFROMFILE = 0x0010
|
|
LR_DEFAULTSIZE = 0x0040
|
|
WM_SETICON = 0x0080
|
|
ICON_SMALL = 0
|
|
ICON_BIG = 1
|
|
|
|
hicon_big = ctypes.windll.user32.LoadImageW(
|
|
None, str(target_ico), IMAGE_ICON, 48, 48, LR_LOADFROMFILE
|
|
) or ctypes.windll.user32.LoadImageW(
|
|
None, str(target_ico), IMAGE_ICON, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE
|
|
)
|
|
hicon_small = ctypes.windll.user32.LoadImageW(
|
|
None, str(target_ico), IMAGE_ICON, 16, 16, LR_LOADFROMFILE
|
|
) or hicon_big
|
|
|
|
if hicon_big:
|
|
ctypes.windll.user32.SendMessageW(hwnd, WM_SETICON, ICON_BIG, hicon_big)
|
|
if hicon_small:
|
|
ctypes.windll.user32.SendMessageW(hwnd, WM_SETICON, ICON_SMALL, hicon_small)
|
|
app._win_hicons = (hicon_big, hicon_small)
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
def _apply_cross_platform_icon(app, root_path: Path):
|
|
"""Setzt game_icon.ico zuverlässig auf Windows, Linux, ChromeOS (Crostini) und macOS."""
|
|
if sys.platform.startswith("linux"):
|
|
try:
|
|
app.tk.call("tk", "appname", APP_WM_CLASS)
|
|
except Exception:
|
|
pass
|
|
|
|
icon_path = (root_path / APP_ICON_REL_PATH).resolve()
|
|
if not icon_path.is_file():
|
|
return
|
|
|
|
best_pil, cascade_pils = _extract_ico_frames(icon_path)
|
|
|
|
# WICHTIG: Unter Windows KEIN iconphoto() nach iconbitmap() aufrufen!
|
|
if sys.platform.startswith("win"):
|
|
_apply_windows_icon(app, icon_path, best_pil)
|
|
return
|
|
|
|
# Linux, ChromeOS (Crostini) & macOS: Multi-Size iconphoto
|
|
if cascade_pils:
|
|
try:
|
|
tk_photos = [ImageTk.PhotoImage(im) for im in cascade_pils]
|
|
app._app_icon_refs = tk_photos
|
|
app.iconphoto(True, *tk_photos)
|
|
except Exception:
|
|
pass
|
|
|
|
# macOS Dock-Icon Bonus
|
|
if sys.platform == "darwin" and best_pil is not None:
|
|
try:
|
|
import io
|
|
from AppKit import NSApplication, NSImage, NSData # type: ignore
|
|
buf = io.BytesIO()
|
|
best_pil.save(buf, format="PNG")
|
|
raw = buf.getvalue()
|
|
ns_data = NSData.dataWithBytes_length_(raw, len(raw))
|
|
ns_img = NSImage.alloc().initWithData_(ns_data)
|
|
if ns_img is not None:
|
|
NSApplication.sharedApplication().setApplicationIconImage_(ns_img)
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
def main():
|
|
_prepare_os_app_identity()
|
|
root_path = Path(__file__).resolve().parent
|
|
|
|
FontLoader.register_fonts(root_path)
|
|
|
|
controller = AppController(root_path)
|
|
app = GothCardCollectionView(controller, root_path)
|
|
|
|
FontLoader.bind_to_tk_root(app)
|
|
_apply_cross_platform_icon(app, root_path)
|
|
WindowStatePersistence(app)
|
|
|
|
controller.bind_view(app)
|
|
controller.start()
|
|
|
|
# Stellt sicher, dass das Windows-HWND-Icon auch nach dem ersten Map-Event sitzt
|
|
if sys.platform.startswith("win"):
|
|
app.after(60, lambda: _apply_cross_platform_icon(app, root_path))
|
|
|
|
app.mainloop()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |