fix(gui): set titlebar icon directly on root HWND and remove iconphoto conflict on Windows

This commit is contained in:
tomasalias 2026-10-08 22:06:42 +02:00
parent 872ead4406
commit a8f11dcd53

View file

@ -103,21 +103,45 @@ class BetterCFApp:
ico_path = find_asset("icon.ico") ico_path = find_asset("icon.ico")
if ico_path and os.name == "nt": if ico_path and os.name == "nt":
try: try:
self.root.iconbitmap(str(ico_path.resolve()))
self.root.iconbitmap(default=str(ico_path.resolve())) self.root.iconbitmap(default=str(ico_path.resolve()))
except Exception: except Exception:
pass pass
png_path = find_asset("icon_512.png") or find_asset("icon.png")
if png_path:
try: try:
if HAS_PIL: import ctypes
im = Image.open(png_path) user32 = ctypes.windll.user32
self._icon_photo = ImageTk.PhotoImage(im.resize((64, 64), Image.Resampling.LANCZOS)) GA_ROOT = 2
self.root.iconphoto(True, self._icon_photo) WM_SETICON = 0x0080
else: IMAGE_ICON = 1
self._icon_photo = tk.PhotoImage(file=str(png_path)) LR_LOADFROMFILE = 0x0010
self.root.iconphoto(True, self._icon_photo)
self.root.update_idletasks()
inner_hwnd = self.root.winfo_id()
root_hwnd = user32.GetAncestor(inner_hwnd, GA_ROOT) or inner_hwnd
self._win32_icon_sm = user32.LoadImageW(None, str(ico_path.resolve()), IMAGE_ICON, 16, 16, LR_LOADFROMFILE)
self._win32_icon_bg = user32.LoadImageW(None, str(ico_path.resolve()), IMAGE_ICON, 32, 32, LR_LOADFROMFILE)
for h in (inner_hwnd, root_hwnd):
if self._win32_icon_sm:
user32.SendMessageW(h, WM_SETICON, 0, self._win32_icon_sm)
if self._win32_icon_bg:
user32.SendMessageW(h, WM_SETICON, 1, self._win32_icon_bg)
except Exception: except Exception:
pass pass
elif os.name != "nt":
png_path = find_asset("icon_512.png") or find_asset("icon.png")
if png_path:
try:
if HAS_PIL:
im = Image.open(png_path)
self._icon_photo = ImageTk.PhotoImage(im.resize((64, 64), Image.Resampling.LANCZOS))
self.root.iconphoto(True, self._icon_photo)
else:
self._icon_photo = tk.PhotoImage(file=str(png_path))
self.root.iconphoto(True, self._icon_photo)
except Exception:
pass
def _configure_styles(self): def _configure_styles(self):
style = ttk.Style() style = ttk.Style()