From a2c1489ffb0d70a13ad3c60032a08da8b6107486 Mon Sep 17 00:00:00 2001 From: Nishi Date: Thu, 16 Apr 2026 09:35:53 +0900 Subject: [PATCH] add MwLLSetDarkTheme --- include/Mw/LowLevel.h | 2 ++ src/backend/call.c | 2 ++ src/backend/cocoa.m | 5 +++++ src/backend/gdi.c | 27 +++++++++++++++++++++++++++ src/backend/wayland.c | 5 +++++ src/backend/x11.c | 7 ++++++- src/core.c | 8 ++++++++ src/lowlevel.c | 2 ++ 8 files changed, 57 insertions(+), 1 deletion(-) diff --git a/include/Mw/LowLevel.h b/include/Mw/LowLevel.h index c958e00..a1a4691 100644 --- a/include/Mw/LowLevel.h +++ b/include/Mw/LowLevel.h @@ -277,6 +277,8 @@ MWDECL void (*MwLLGetClipboard)(MwLL handle); MWDECL void (*MwLLGetCursorCoord)(MwLL handle, MwPoint* point); MWDECL void (*MwLLGetScreenSize)(MwLL handle, MwRect* rect); +MWDECL void (*MwLLSetDarkTheme)(MwLL handle, int toggle); + /* font renderer */ MWDECL void MwFLSetup(void); diff --git a/src/backend/call.c b/src/backend/call.c index e2ce934..07cf1e0 100644 --- a/src/backend/call.c +++ b/src/backend/call.c @@ -54,6 +54,8 @@ \ MwLLGetCursorCoord = MwLLGetCursorCoordImpl; \ MwLLGetScreenSize = MwLLGetScreenSizeImpl; \ +\ + MwLLSetDarkTheme = MwLLSetDarkThemeImpl; \ \ return 0; \ } diff --git a/src/backend/cocoa.m b/src/backend/cocoa.m index 4cdc5b2..95fd56d 100644 --- a/src/backend/cocoa.m +++ b/src/backend/cocoa.m @@ -1082,6 +1082,11 @@ static void MwLLEndStateChangeImpl(MwLL handle) { MwLLShow(handle, 1); } +static void MwLLSetDarkThemeImpl(MwLL handle, int toggle){ + (void)handle; + (void)toggle; +} + static int MwLLCocoaCallInitImpl(void) { #ifndef __APPLE__ printf("Using GNUStep Backend\n"); diff --git a/src/backend/gdi.c b/src/backend/gdi.c index 5d89419..9fe1398 100644 --- a/src/backend/gdi.c +++ b/src/backend/gdi.c @@ -8,6 +8,14 @@ typedef struct userdata { int max_set; } userdata_t; +static struct symtbl { + void* lib_dwmapi; + + HRESULT (WINAPI *DwmSetWindowAttribute)(HWND hwnd, DWORD dwAttribute, LPCVOID pvAttribute, DWORD cbAttribute); +} wsymtbl; + +#define DwmSetWindowAttribute wsymtbl.DwmSetWindowAttribute + static void detect_darktheme(MwLL handle) { DWORD dw; DWORD sz = sizeof(dw); @@ -26,6 +34,12 @@ static void detect_darktheme(MwLL handle) { t = dw ? 0 : 1; + if(t && DwmSetWindowAttribute != NULL){ + LPARAM style = GetWindowLongPtr(handle->gdi.hWnd, GWL_STYLE); + BOOL v = TRUE; + if(!(style & WS_CHILD)) DwmSetWindowAttribute(handle->gdi.hWnd, 20, &v, sizeof(v)); + } + MwLLDispatch(handle, dark_theme, &t); } @@ -804,7 +818,20 @@ static void MwLLEndStateChangeImpl(MwLL handle) { (void)handle; } +static void MwLLSetDarkThemeImpl(MwLL handle, int toggle){ + BOOL v = toggle ? TRUE : FALSE; + + DwmSetWindowAttribute(handle->gdi.hWnd, 20, &v, sizeof(v)); + +} + static int MwLLGDICallInitImpl(void) { + memset(&wsymtbl, 0, sizeof(wsymtbl)); + + wsymtbl.lib_dwmapi = MwDynamicOpen("dwmapi.dll"); + + if(wsymtbl.lib_dwmapi != NULL) DwmSetWindowAttribute = MwDynamicSymbol(wsymtbl.lib_dwmapi, "DwmSetWindowAttribute"); + /* TODO: check properly */ return 0; } diff --git a/src/backend/wayland.c b/src/backend/wayland.c index 1fc68cb..bd46860 100644 --- a/src/backend/wayland.c +++ b/src/backend/wayland.c @@ -2620,6 +2620,11 @@ static void MwLLEndStateChangeImpl(MwLL handle) { } } +static void MwLLSetDarkThemeImpl(MwLL handle, int toggle){ + (void)handle; + (void)toggle; +} + static int MwLLWaylandCallInitImpl(void) { #ifdef __linux__ MwBool loadWayland = MwFALSE; diff --git a/src/backend/x11.c b/src/backend/x11.c index d8f028b..dcc41d7 100644 --- a/src/backend/x11.c +++ b/src/backend/x11.c @@ -4,7 +4,7 @@ #define ClipboardTimeout 100 -static struct { +static struct symtbl { void* lib_xlib; void* lib_xrender; MwBool has_xrender; @@ -1308,6 +1308,11 @@ static void MwLLEndStateChangeImpl(MwLL handle) { MwLLShow(handle, 1); } +static void MwLLSetDarkThemeImpl(MwLL handle, int toggle){ + (void)handle; + (void)toggle; +} + static int MwLLX11CallInitImpl(void) { MwBool loadX11 = MwFALSE; if(getenv("MILSKO_BACKEND")) { diff --git a/src/core.c b/src/core.c index cf45ddd..1b00615 100644 --- a/src/core.c +++ b/src/core.c @@ -532,6 +532,14 @@ void MwSetInteger(MwWidget handle, const char* key, int n) { if(strcmp(key, MwNmodernLook) == 0 || strcmp(key, MwNdarkTheme) == 0 || strcmp(key, MwNbitmapFont) == 0) { force_render_all(handle); } + + if(strcmp(key, MwNdarkTheme) == 0){ + MwWidget h = handle; + + while(h->parent != NULL && h->parent->widget_class != NULL) h = h->parent; + + MwLLSetDarkTheme(h->lowlevel, n); + } } void MwSetText(MwWidget handle, const char* key, const char* value) { diff --git a/src/lowlevel.c b/src/lowlevel.c index 13a7d49..e80b95b 100644 --- a/src/lowlevel.c +++ b/src/lowlevel.c @@ -51,6 +51,8 @@ void (*MwLLGetClipboard)(MwLL handle) = NULL; void (*MwLLGetCursorCoord)(MwLL handle, MwPoint* point) = NULL; void (*MwLLGetScreenSize)(MwLL handle, MwRect* rect) = NULL; +void (*MwLLSetDarkTheme)(MwLL handle, int toggle) = NULL; + void MwLLCreateCommon(MwLL handle) { handle->common.handler = malloc(sizeof(*handle->common.handler)); memset(handle->common.handler, 0, sizeof(*handle->common.handler));