SyncTk/modules/theme_titlebar.py

62 lines
2.2 KiB
Python
Raw Normal View History

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