diff --git a/main/gui/source/window/web_window.cpp b/main/gui/source/window/web_window.cpp index 0f05d3167..ba7745ff9 100644 --- a/main/gui/source/window/web_window.cpp +++ b/main/gui/source/window/web_window.cpp @@ -5,6 +5,8 @@ #include #include +#include + // Function used by c++ to get the size of the html canvas EM_JS(int, canvas_get_width, (), { return Module.canvas.width; @@ -20,6 +22,21 @@ EM_JS(void, resizeCanvas, (), { js_resizeCanvas(); }); +EM_JS(void, setupThemeListener, (), { + window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => { + Module._handleThemeChange(); + }); +}); + +EM_JS(bool, isDarkModeEnabled, (), { + return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches +}); + +EMSCRIPTEN_KEEPALIVE +extern "C" void handleThemeChange() { + hex::EventManager::post(); +} + namespace hex { void nativeErrorMessage(const std::string &message) { @@ -45,6 +62,17 @@ namespace hex { void Window::setupNativeWindow() { resizeCanvas(); + setupThemeListener(); + + bool themeFollowSystem = ImHexApi::System::usesSystemThemeDetection(); + EventManager::subscribe(this, [themeFollowSystem] { + if (!themeFollowSystem) return; + + EventManager::post(!isDarkModeEnabled() ? "Light" : "Dark"); + }); + + if (themeFollowSystem) + EventManager::post(); } void Window::beginNativeWindowFrame() {