SyncTk/src/modules/theme_titlebar.py
2023-01-28 17:50:19 +01:00

64 lines
2.3 KiB
Python

import platform
import sys
import sv_ttk
from distutils.version import StrictVersion as Version
from os import system
from tkinter import Misc, Tcl, Toplevel
from typing import Literal, Union
from ttkthemes import ThemedTk
from classes.enums import Theme
if sys.platform.startswith("win"):
from ctypes import byref, c_int, sizeof, windll
def theme_title_bar(window: Union[ThemedTk, Toplevel, Misc], mode: Literal[Theme.DARK, Theme.LIGHT]) -> None:
"""
MORE INFO:
https://learn.microsoft.com/en-us/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute
"""
if mode.value == "dark":
value = 1
#window.configure(background="#1c1c1c")
elif mode.value == "light":
value = 0
#window.configure(background="#ffffff")
else:
raise ValueError()
try:
sv_ttk.set_theme(mode.value)
window.update()
if sys.platform.startswith("win"):
hwnd = windll.user32.GetParent(window.winfo_id())
DWMWA_USE_IMMERSIVE_DARK_MODE = 20
DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 = 19
# try with DWMWA_USE_IMMERSIVE_DARK_MODE
if windll.dwmapi.DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, byref(c_int(value)), sizeof(c_int(value))) != 0:
# try with DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20h1
windll.dwmapi.DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1, byref(c_int(value)), sizeof(c_int(value)))
elif sys.platform == "darwin":
if value == 1:
if Version(platform.python_version()) < Version("3.10"):
if Version(Tcl().call("info", "patchlevel")) >= Version("8.6.9"): # Tcl/Tk >= 8.6.9
system("defaults write -g NSRequiresAquaSystemAppearance -bool No")
# This command allows dark-mode for all programs
else:
if Version(platform.python_version()) < Version("3.10"):
if Version(Tcl().call("info", "patchlevel")) >= Version("8.6.9"): # Tcl/Tk >= 8.6.9
system("defaults delete -g NSRequiresAquaSystemAppearance")
# This command reverts the dark-mode setting for all programs.
except Exception as err:
print(err)