Skip to content

Commit

Permalink
make immersive dark mode be applied on W10 systems also
Browse files Browse the repository at this point in the history
  • Loading branch information
marticliment committed Jan 26, 2023
1 parent ba5ca37 commit 05d620f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 49 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br> 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.<br> 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)
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@

setuptools.setup(
name="win32mica",
version="1.7",
version="1.8",
author="Martí Climent",
author_email="[email protected]",
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",
Expand Down
84 changes: 42 additions & 42 deletions src/win32mica/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -110,4 +111,3 @@ class _MARGINS(ctypes.Structure):
return 0x32


ApplyMica(1181804, MICAMODE.DARK)

0 comments on commit 05d620f

Please sign in to comment.