Skip to content

Commit 2d759a9

Browse files
committed
on macos need to fix lightmode -> dark mode
1 parent c3f1f11 commit 2d759a9

File tree

1 file changed

+90
-9
lines changed

1 file changed

+90
-9
lines changed

gui/main.py

Lines changed: 90 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -314,25 +314,106 @@ def _apply_native_theme(self) -> None:
314314
style.theme_use(style.theme_use())
315315

316316
def _apply_theme(self, dark: bool) -> None:
317-
# Always use native macOS theme for consistency
318-
self._apply_native_theme()
319-
320317
style = ttk.Style()
321318

319+
# Update appearance based on dark mode preference
320+
if sys.platform == "darwin":
321+
# On macOS, set the appearance mode
322+
try:
323+
# This tells macOS to use dark or light appearance
324+
appearance = "darkAqua" if dark else "aqua"
325+
self.root.tk.call("::tk::unsupported::MacWindowStyle", "appearance", self.root._w, appearance)
326+
327+
# Force the window to redraw with new appearance
328+
self.root.update()
329+
except Exception:
330+
pass
331+
332+
# Apply aqua theme (it will adapt to the appearance we just set)
333+
if "aqua" in style.theme_names():
334+
style.theme_use("aqua")
335+
elif sys.platform == "win32":
336+
# Apply Windows native theme first
337+
for name in ("vista", "xpnative", "winnative"):
338+
if name in style.theme_names():
339+
style.theme_use(name)
340+
break
341+
322342
# Minimal styling - let native theme handle colors
323343
style.configure("Primary.TButton", padding=(PADDING_MEDIUM, PADDING_SMALL))
324344
style.configure("TLabelframe.Label", font=("TkDefaultFont", 10, "bold"))
325345
style.configure("Header.TLabel", font=("TkDefaultFont", 16, "bold"))
326346

327-
# Update appearance based on dark mode preference
328-
if sys.platform == "darwin":
329-
# On macOS, set the appearance mode
347+
# Windows-specific theming
348+
if sys.platform == "win32":
349+
# On Windows, manually apply dark/light theme colors
330350
try:
331-
# This tells macOS to use dark or light appearance
332351
if dark:
333-
self.root.tk.call("::tk::unsupported::MacWindowStyle", "appearance", self.root._w, "darkAqua")
352+
# Dark theme colors
353+
bg_color = "#1e1e1e"
354+
fg_color = "#ffffff"
355+
select_bg = "#094771"
356+
select_fg = "#ffffff"
357+
358+
self.root.configure(bg=bg_color)
359+
style.configure(".", background=bg_color, foreground=fg_color,
360+
fieldbackground=bg_color, selectbackground=select_bg,
361+
selectforeground=select_fg)
362+
style.configure("TFrame", background=bg_color)
363+
style.configure("TLabel", background=bg_color, foreground=fg_color)
364+
style.configure("TLabelframe", background=bg_color, foreground=fg_color)
365+
style.configure("TLabelframe.Label", background=bg_color, foreground=fg_color)
366+
style.configure("Header.TLabel", background=bg_color, foreground=fg_color)
367+
style.configure("TButton", background=bg_color, foreground=fg_color)
368+
style.configure("TCheckbutton", background=bg_color, foreground=fg_color)
369+
style.configure("TRadiobutton", background=bg_color, foreground=fg_color)
370+
style.configure("TNotebook", background=bg_color, foreground=fg_color)
371+
style.configure("TNotebook.Tab", background=bg_color, foreground=fg_color)
372+
373+
# Try to set title bar to dark mode (Windows 10 build 17763+)
374+
try:
375+
import ctypes
376+
hwnd = ctypes.windll.user32.GetParent(self.root.winfo_id())
377+
DWMWA_USE_IMMERSIVE_DARK_MODE = 20
378+
ctypes.windll.dwmapi.DwmSetWindowAttribute(
379+
hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE,
380+
ctypes.byref(ctypes.c_int(1)), ctypes.sizeof(ctypes.c_int)
381+
)
382+
except Exception:
383+
pass
334384
else:
335-
self.root.tk.call("::tk::unsupported::MacWindowStyle", "appearance", self.root._w, "aqua")
385+
# Light theme colors (system defaults)
386+
bg_color = "#f0f0f0"
387+
fg_color = "#000000"
388+
select_bg = "#0078d7"
389+
select_fg = "#ffffff"
390+
391+
self.root.configure(bg=bg_color)
392+
style.configure(".", background=bg_color, foreground=fg_color,
393+
fieldbackground="#ffffff", selectbackground=select_bg,
394+
selectforeground=select_fg)
395+
style.configure("TFrame", background=bg_color)
396+
style.configure("TLabel", background=bg_color, foreground=fg_color)
397+
style.configure("TLabelframe", background=bg_color, foreground=fg_color)
398+
style.configure("TLabelframe.Label", background=bg_color, foreground=fg_color)
399+
style.configure("Header.TLabel", background=bg_color, foreground=fg_color)
400+
style.configure("TButton", background=bg_color, foreground=fg_color)
401+
style.configure("TCheckbutton", background=bg_color, foreground=fg_color)
402+
style.configure("TRadiobutton", background=bg_color, foreground=fg_color)
403+
style.configure("TNotebook", background=bg_color, foreground=fg_color)
404+
style.configure("TNotebook.Tab", background=bg_color, foreground=fg_color)
405+
406+
# Try to set title bar to light mode
407+
try:
408+
import ctypes
409+
hwnd = ctypes.windll.user32.GetParent(self.root.winfo_id())
410+
DWMWA_USE_IMMERSIVE_DARK_MODE = 20
411+
ctypes.windll.dwmapi.DwmSetWindowAttribute(
412+
hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE,
413+
ctypes.byref(ctypes.c_int(0)), ctypes.sizeof(ctypes.c_int)
414+
)
415+
except Exception:
416+
pass
336417
except Exception:
337418
pass
338419

0 commit comments

Comments
 (0)