"""Pure theme builders and shared Windows title-bar styling.""" from __future__ import annotations import ctypes import os from pathlib import Path from functools import lru_cache from string import Template PALETTES = {'white': {'chrome': '#f7f8fb', 'surface': '#ffffff', 'text': '#111827', 'muted': '#6b7280', 'border': '#dde2eb', 'hover': '#eef4ff', 'selected': '#dbeafe', 'selected_text': '#0f172a', 'splitter': '#e5e7eb', 'focus': '#3b82f6', 'tab_bg': '#edf1f7'}, 'black': {'chrome': '#0c0d10', 'surface': '#17191f', 'text': '#f3f4f6', 'muted': '#a7adb7', 'border': '#2a2e38', 'hover': '#252b36', 'selected': '#243b5a', 'selected_text': '#f8fafc', 'splitter': '#252a33', 'focus': '#60a5fa', 'tab_bg': '#111318'}, 'dark_gray': {'chrome': '#282c33', 'surface': '#343943', 'text': '#f1f5f9', 'muted': '#c1c7d0', 'border': '#48505d', 'hover': '#3f4a59', 'selected': '#365b82', 'selected_text': '#ffffff', 'splitter': '#48505d', 'focus': '#7dd3fc', 'tab_bg': '#303640'}, 'colorful': {'chrome': '#eef5ff', 'surface': '#ffffff', 'text': '#132238', 'muted': '#667085', 'border': '#cbd9ef', 'hover': '#e7f0ff', 'selected': '#cfe2ff', 'selected_text': '#10213d', 'splitter': '#cbd9ef', 'focus': '#2563eb', 'tab_bg': '#dceaff'}} def stylesheet_url(path: Path) -> str: return path.resolve().as_posix() def navigation_theme(theme_color: str, theme: str, theme_background: str, resources_dir: Path, font_height: int) -> tuple[str, str, str]: palette = PALETTES.get(theme_color, PALETTES["white"]) background_accents = { "none": ("", palette["chrome"], ""), "chinese_knot": ("#d92f2f", "#fff1ef", "theme_chinese_knot.svg"), "cloud": ("#8bd5ff", "#effaff", "theme_cloud.svg"), "ruyi": ("#bfe9ca", "#f1fbf3", "theme_ruyi.svg"), "brush": ("#d6d7dc", "#f5f6f8", "theme_brush.svg"), "peony": ("#ffd0df", "#fff3f7", "theme_peony.svg"), "bamboo": ("#a9d878", "#f1faeb", "theme_bamboo.svg"), "meander": ("#ffd94d", "#fff8d8", "theme_meander.svg"), } dark_background_accents = { "none": ("", palette["chrome"], ""), "chinese_knot": ("#6e171a", "#26191c", "theme_chinese_knot.svg"), "cloud": ("#16384d", "#122330", "theme_cloud.svg"), "ruyi": ("#1c4229", "#17251b", "theme_ruyi.svg"), "brush": ("#3a3d45", "#1e2026", "theme_brush.svg"), "peony": ("#552333", "#2a1a22", "theme_peony.svg"), "bamboo": ("#254018", "#1a2616", "theme_bamboo.svg"), "meander": ("#574913", "#282416", "theme_meander.svg"), } accent_source = dark_background_accents if theme == "dark" else background_accents header_background, themed_panel, pattern_file = accent_source.get(theme_background, accent_source["none"]) panel_bg = themed_panel if theme_background != "none" else palette["chrome"] chrome_bg = header_background or panel_bg text = palette["text"] muted = palette["muted"] surface = palette["surface"] border = palette["border"] hover = palette["hover"] selected = palette["selected"] selected_text = palette["selected_text"] splitter = palette["splitter"] input_bg = palette["surface"] focus = palette["focus"] tab_bg = palette["tab_bg"] tab_active = palette["surface"] branch_closed_icon = stylesheet_url(resources_dir / "tree_closed.svg") branch_open_icon = stylesheet_url(resources_dir / "tree_open.svg") search_box_height = max(22, round(font_height * 1.5)) pattern_style = "" chrome_pattern_style = "" if pattern_file: pattern_url = stylesheet_url(resources_dir / pattern_file) pattern_style = f'background-image: url("{pattern_url}"); background-repeat: repeat;' chrome_pattern_style = f'background-image: url("{pattern_url}"); background-repeat: repeat;' stylesheet = theme_stylesheet("ui_navigation.qss", locals()) return stylesheet, chrome_bg, text def style_dialog_theme(theme_color: str) -> tuple[str, str, str]: palette = PALETTES.get(theme_color, PALETTES["white"]) chrome = palette["chrome"] surface = palette["surface"] text = palette["text"] muted = palette["muted"] border = palette["border"] hover = palette["hover"] selected = palette["selected"] focus = palette["focus"] stylesheet = theme_stylesheet("ui_style_dialog.qss", locals()) return stylesheet, chrome, text def apply_title_bar_style(widget, background: str, foreground: str, dark: bool) -> None: if os.name != "nt": return def colorref(value: str) -> int: value = value.lstrip("#") red = int(value[0:2], 16) green = int(value[2:4], 16) blue = int(value[4:6], 16) return red | (green << 8) | (blue << 16) try: hwnd = int(widget.winId()) dwmapi = ctypes.windll.dwmapi dark_mode = ctypes.c_int(1 if dark else 0) for attribute in (20, 19): dwmapi.DwmSetWindowAttribute(hwnd, attribute, ctypes.byref(dark_mode), ctypes.sizeof(dark_mode)) for attribute, value in ((35, background), (36, foreground), (34, background)): color = ctypes.c_int(colorref(value)) dwmapi.DwmSetWindowAttribute(hwnd, attribute, ctypes.byref(color), ctypes.sizeof(color)) except (AttributeError, OSError, ValueError): return @lru_cache(maxsize=4) def _template(name: str) -> Template: return Template((Path(__file__).parent / "resources" / name).read_text(encoding="utf-8")) def theme_stylesheet(name: str, values: dict) -> str: return _template(name).substitute(values)