Skip to content

Commit

Permalink
Dark mode will remain even if windows switches to light mode (fix mar…
Browse files Browse the repository at this point in the history
  • Loading branch information
marticliment committed Jun 13, 2023
1 parent d4f2e79 commit 9887ea5
Showing 1 changed file with 47 additions and 6 deletions.
53 changes: 47 additions & 6 deletions src/win32mica/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ctypes, sys
import ctypes, sys, threading, time, winreg

class MICAMODE():
DARK = True
Expand All @@ -7,8 +7,30 @@ class MICAMODE():

debugging = False


def ApplyMica(HWND: int, ColorMode: bool = MICAMODE.LIGHT) -> int:
def readRegedit(aKey, sKey, default, storage=winreg.HKEY_CURRENT_USER):
registry = winreg.ConnectRegistry(None, storage)
reg_keypath = aKey
try:
reg_key = winreg.OpenKey(registry, reg_keypath)
except FileNotFoundError as e:
return default
except Exception as e:
print(e)
return default

for i in range(1024):
try:
value_name, value, _ = winreg.EnumValue(reg_key, i)
if value_name == sKey:
return value
except OSError as e:
return default
except Exception as e:
print(e)
return default


def ApplyMica(HWND: int, ColorMode: bool = MICAMODE.LIGHT, darkModeMode: int = 20) -> int:
"""Apply the new mica effect on a window making use of the hidden win32api and return an integer depending on the result of the operation
Keyword arguments:
Expand Down Expand Up @@ -58,7 +80,26 @@ class _MARGINS(ctypes.Structure):
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))
def setMode():
oldMode = -1
while True:
mode = readRegedit(r"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize", "AppsUseLightTheme", 0)
if oldMode != mode:
oldMode = mode
DwmSetWindowAttribute(HWND, DWMW_USE_IMMERSIVE_DARK_MODE, ctypes.byref(ctypes.c_int(0x01)), ctypes.sizeof(ctypes.c_int))
time.sleep(0.5)
DwmSetWindowAttribute(HWND, DWMW_USE_IMMERSIVE_DARK_MODE, ctypes.byref(ctypes.c_int(0x01)), ctypes.sizeof(ctypes.c_int))
time.sleep(0.5)
DwmSetWindowAttribute(HWND, DWMW_USE_IMMERSIVE_DARK_MODE, ctypes.byref(ctypes.c_int(0x01)), ctypes.sizeof(ctypes.c_int))
time.sleep(0.5)
DwmSetWindowAttribute(HWND, DWMW_USE_IMMERSIVE_DARK_MODE, ctypes.byref(ctypes.c_int(0x01)), ctypes.sizeof(ctypes.c_int))
time.sleep(0.5)
time.sleep(0.1)



threading.Thread(target=setMode, daemon=True, name="win32mica: ensure dark mode").start()

else: # Apply light mode
DwmSetWindowAttribute(HWND, DWMW_USE_IMMERSIVE_DARK_MODE, ctypes.byref(ctypes.c_int(0x00)), ctypes.sizeof(ctypes.c_int))

Expand All @@ -71,7 +112,7 @@ class _MARGINS(ctypes.Structure):
Acp.AccentPolicy = 19

Wca = WindowCompositionAttribute()
Wca.Attribute = 19
Wca.Attribute = 20
Wca.SizeOfData = ctypes.sizeof(Acp)
Wca.Data = ctypes.cast(ctypes.pointer(Acp), ctypes.POINTER(ctypes.c_int))

Expand All @@ -91,7 +132,7 @@ class _MARGINS(ctypes.Structure):
print("Win32mica: SetWindowCompositionAttribute Ok")

if ColorMode == MICAMODE.DARK:
Wca.Attribute = 26
Wca.Attribute = 1
o = SetWindowCompositionAttribute(HWND, Wca)
if debugging:
if o != 0:
Expand Down

0 comments on commit 9887ea5

Please sign in to comment.