diff --git a/README.md b/README.md index 9f53318..fd87944 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,8 @@ ![](https://img.shields.io/pypi/l/win32mica?style=for-the-badge) ![](https://img.shields.io/pypi/pyversions/win32mica?style=for-the-badge) ![](https://img.shields.io/pypi/dm/win32mica?style=for-the-badge) -# Win32mica: A simple module to add the Mica effect on Python UI Windows -The aim of this project is to apply the Mica effect on python applications made with Python, like Tkinter, PyQt/PySide, WxPython, Kivy, etc.
This will work on any windows version, including the new released dev builds where the mica API is public. +# Win32mica: A simple module to add the Mica effect and enable immersive dark mode on Python UI Windows +The aim of this project is to apply the Mica effect and enable immersive dark mode on python applications made with Python, like Tkinter, PyQt/PySide, WxPython, Kivy, etc.
This will work on any windows version, including the new released dev builds where the mica API is public. View this project on [PyPi](https://pypi.org/project/win32mica/) View this project on [GitHub](https://github.com/martinet101/win32mica) @@ -46,6 +46,9 @@ import darkdetect # You can pass the darkdetect return value directly, since the mode = darkdetect.isDark() win32mica.ApplyMica(hwnd, mode) +# Will return 0x32 if the system does not support Mica textures (Windows 10 or less). Immersive dark mode will still be applied (if selected theme is MICAMODE.DARK) +# Will return 0x00 if mica is applied successfully +# If DwmSetWindowAttribute fails, the output code will be returned ``` You can check out the [examples folder](https://github.com/martinet101/win32mica/tree/main/examples) for detailed use in Tk and PySide/PyQt. diff --git a/setup.py b/setup.py index f7b63ee..0b68e85 100644 --- a/setup.py +++ b/setup.py @@ -5,18 +5,18 @@ setuptools.setup( name="win32mica", - version="1.7", + version="1.8", author="Martí Climent", author_email="marticlilop@gmail.com", - description="Apply mica background to Windows 11 Win32 apps made with python, such as Tkinter or PyQt/PySide apps", + description="Apply mica background (if supported) and immersive dark mode to Windows 11 Win32 apps made with python, such as Tkinter or PyQt/PySide apps", long_description=long_description, long_description_content_type="text/markdown", - url="https://github.com/martinet101/win32mica", + url="https://github.com/marticliment/win32mica", project_urls={ - "Bug Tracker": "https://github.com/martinet101/win32mica/issues", + "Bug Tracker": "https://github.com/marticliment/win32mica/issues", }, classifiers=[ - 'Intended Audience :: Developers', + 'Intended Audience :: Developers', "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", "Operating System :: Microsoft :: Windows", diff --git a/src/win32mica/__init__.py b/src/win32mica/__init__.py index d92a78a..529c2e4 100644 --- a/src/win32mica/__init__.py +++ b/src/win32mica/__init__.py @@ -20,43 +20,50 @@ def ApplyMica(HWND: int, ColorMode: bool = MICAMODE.LIGHT) -> int: except ValueError: HWND = int(str(HWND), 16) - if sys.platform == "win32" and sys.getwindowsversion().build >= 22000: - user32 = ctypes.windll.user32 - dwm = ctypes.windll.dwmapi - - class AccentPolicy(ctypes.Structure): - _fields_ = [ - ("AccentState", ctypes.c_uint), - ("AccentFlags", ctypes.c_uint), - ("GradientColor", ctypes.c_uint), - ("AnimationId", ctypes.c_uint) - ] - - class WindowCompositionAttribute(ctypes.Structure): - _fields_ = [ - ("Attribute", ctypes.c_int), - ("Data", ctypes.POINTER(ctypes.c_int)), - ("SizeOfData", ctypes.c_size_t) - ] - - class _MARGINS(ctypes.Structure): - _fields_ = [("cxLeftWidth", ctypes.c_int), - ("cxRightWidth", ctypes.c_int), - ("cyTopHeight", ctypes.c_int), - ("cyBottomHeight", ctypes.c_int) - ] - - DWM_UNDOCUMENTED_MICA_ENTRY = 1029 # Undocumented MICA (Windows 11 22523-) - DWM_UNDOCUMENTED_MICA_VALUE = 0x01 # Undocumented MICA (Windows 11 22523-) - - DWM_DOCUMENTED_MICA_ENTRY = 38 # Documented MICA (Windows 11 22523+) - DWM_DOCUMENTED_MICA_VALUE = 0x02 # Documented MICA (Windows 11 22523+) - DWMW_USE_IMMERSIVE_DARK_MODE = 20 + user32 = ctypes.windll.user32 + dwm = ctypes.windll.dwmapi + + class AccentPolicy(ctypes.Structure): + _fields_ = [ + ("AccentState", ctypes.c_uint), + ("AccentFlags", ctypes.c_uint), + ("GradientColor", ctypes.c_uint), + ("AnimationId", ctypes.c_uint) + ] + + class WindowCompositionAttribute(ctypes.Structure): + _fields_ = [ + ("Attribute", ctypes.c_int), + ("Data", ctypes.POINTER(ctypes.c_int)), + ("SizeOfData", ctypes.c_size_t) + ] + + class _MARGINS(ctypes.Structure): + _fields_ = [("cxLeftWidth", ctypes.c_int), + ("cxRightWidth", ctypes.c_int), + ("cyTopHeight", ctypes.c_int), + ("cyBottomHeight", ctypes.c_int) + ] + + DWM_UNDOCUMENTED_MICA_ENTRY = 1029 # Undocumented MICA (Windows 11 22523-) + DWM_UNDOCUMENTED_MICA_VALUE = 0x01 # Undocumented MICA (Windows 11 22523-) + + DWM_DOCUMENTED_MICA_ENTRY = 38 # Documented MICA (Windows 11 22523+) + DWM_DOCUMENTED_MICA_VALUE = 0x02 # Documented MICA (Windows 11 22523+) + DWMW_USE_IMMERSIVE_DARK_MODE = 20 + + + SetWindowCompositionAttribute = user32.SetWindowCompositionAttribute + DwmSetWindowAttribute = dwm.DwmSetWindowAttribute + DwmExtendFrameIntoClientArea = dwm.DwmExtendFrameIntoClientArea + if ColorMode == MICAMODE.DARK: # Apply dark mode + DwmSetWindowAttribute(HWND, DWMW_USE_IMMERSIVE_DARK_MODE, ctypes.byref(ctypes.c_int(0x01)), ctypes.sizeof(ctypes.c_int)) + else: # Apply light mode + DwmSetWindowAttribute(HWND, DWMW_USE_IMMERSIVE_DARK_MODE, ctypes.byref(ctypes.c_int(0x00)), ctypes.sizeof(ctypes.c_int)) - SetWindowCompositionAttribute = user32.SetWindowCompositionAttribute - DwmSetWindowAttribute = dwm.DwmSetWindowAttribute - DwmExtendFrameIntoClientArea = dwm.DwmExtendFrameIntoClientArea + + if sys.platform == "win32" and sys.getwindowsversion().build >= 22000: Acp = AccentPolicy() Acp.GradientColor = int("00cccccc", base=16) @@ -95,12 +102,6 @@ class _MARGINS(ctypes.Structure): if debugging: print("Win32mica: No SetWindowCompositionAttribute (light mode)") - - if ColorMode == True: # Apply dark mode - DwmSetWindowAttribute(HWND, DWMW_USE_IMMERSIVE_DARK_MODE, ctypes.byref(ctypes.c_int(0x01)), ctypes.sizeof(ctypes.c_int)) - else: # Apply light mode - DwmSetWindowAttribute(HWND, DWMW_USE_IMMERSIVE_DARK_MODE, ctypes.byref(ctypes.c_int(0x00)), ctypes.sizeof(ctypes.c_int)) - if sys.getwindowsversion().build < 22523: # If mica is not a public API return DwmSetWindowAttribute(HWND, DWM_UNDOCUMENTED_MICA_ENTRY, ctypes.byref(ctypes.c_int(DWM_UNDOCUMENTED_MICA_VALUE)), ctypes.sizeof(ctypes.c_int)) else: # If mica is present in the public API @@ -110,4 +111,3 @@ class _MARGINS(ctypes.Structure): return 0x32 -ApplyMica(1181804, MICAMODE.DARK) \ No newline at end of file