SyncTk/modules/theme_titlebar.py

64 lines
2.3 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
2023-01-26 14:29:30 +02:00
from tkinter import Misc, Tcl, Toplevel
2023-01-22 01:35:32 +02:00
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-26 14:29:30 +02:00
from classes.enums import Theme
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
2023-01-26 14:29:30 +02:00
def theme_title_bar(window: Union[ThemedTk, Toplevel, Misc], mode: Literal[Theme.DARK, Theme.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
"""
2023-01-26 14:29:30 +02:00
if mode.value == "dark":
2023-01-22 01:22:08 +02:00
value = 1
2023-01-22 18:53:55 +02:00
#window.configure(background="#1c1c1c")
2023-01-26 14:29:30 +02:00
elif mode.value == "light":
2023-01-22 01:22:08 +02:00
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
2023-01-26 14:29:30 +02:00
sv_ttk.set_theme(mode.value)
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)