Skip to content

Commit

Permalink
Using native dark mode title bar on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ElSaico committed Jun 11, 2024
1 parent e45e8c0 commit e6f8580
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from traceback import print_exc

if sys.platform == 'win32':
from ctypes import windll
from ctypes import windll, byref, c_int
FR_PRIVATE = 0x10
fonts_loaded = windll.gdi32.AddFontResourceExW(str(config.respath_path / 'EUROCAPS.TTF'), FR_PRIVATE, 0)
if fonts_loaded < 1:
Expand Down Expand Up @@ -216,18 +216,26 @@ def apply(self) -> None: # noqa: CCR001, C901
return # Don't need to mess with the window manager
self.active = theme

self.root.withdraw()
self.root.update_idletasks() # Size gets recalculated here
if sys.platform == 'win32':
GWL_STYLE = -16 # noqa: N806 # ctypes
WS_MAXIMIZEBOX = 0x00010000 # noqa: N806 # ctypes
# tk8.5.9/win/tkWinWm.c:342
GWL_EXSTYLE = -20 # noqa: N806 # ctypes
WS_EX_APPWINDOW = 0x00040000 # noqa: N806 # ctypes
WS_EX_LAYERED = 0x00080000 # noqa: N806 # ctypes
DWMWA_USE_IMMERSIVE_DARK_MODE = 20

Check warning on line 228 in theme.py

View workflow job for this annotation

GitHub Actions / push_checks

N806 variable 'DWMWA_USE_IMMERSIVE_DARK_MODE' in function should be lowercase
DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 = 19

Check warning on line 229 in theme.py

View workflow job for this annotation

GitHub Actions / push_checks

N806 variable 'DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1' in function should be lowercase
GetWindowLongW = windll.user32.GetWindowLongW # noqa: N806 # ctypes
SetWindowLongW = windll.user32.SetWindowLongW # noqa: N806 # ctypes
DwmSetWindowAttribute = windll.dwmapi.DwmSetWindowAttribute

Check warning on line 232 in theme.py

View workflow job for this annotation

GitHub Actions / push_checks

N806 variable 'DwmSetWindowAttribute' in function should be lowercase

if theme == self.THEME_DEFAULT:
dark = 0
else:
dark = 1

self.root.withdraw()
self.root.update_idletasks() # Size and windows styles get recalculated here
hwnd = windll.user32.GetParent(self.root.winfo_id())
SetWindowLongW(hwnd, GWL_STYLE, GetWindowLongW(hwnd, GWL_STYLE) & ~WS_MAXIMIZEBOX) # disable maximize

Expand All @@ -236,12 +244,9 @@ def apply(self) -> None: # noqa: CCR001, C901
else:
SetWindowLongW(hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW) # Add to taskbar

self.root.deiconify()
self.root.wait_visibility() # need main window to be displayed before returning

if DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, byref(c_int(dark)), 4) != 0:
DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1, byref(c_int(dark)), 4)
else:
self.root.withdraw()
self.root.update_idletasks() # Size gets recalculated here
if dpy:
xroot = Window()
parent = Window()
Expand All @@ -259,8 +264,8 @@ def apply(self) -> None: # noqa: CCR001, C901

XFlush(dpy)

self.root.deiconify()
self.root.wait_visibility() # need main window to be displayed before returning
self.root.deiconify()
self.root.wait_visibility() # need main window to be displayed before returning

if not self.minwidth:
self.minwidth = self.root.winfo_width() # Minimum width = width on first creation
Expand Down

0 comments on commit e6f8580

Please sign in to comment.