Skip to content

Commit

Permalink
feat: Properly detect native theme on the web version
Browse files Browse the repository at this point in the history
  • Loading branch information
WerWolv committed Oct 4, 2023
1 parent 13c9b80 commit eb41622
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions main/gui/source/window/web_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <emscripten.h>
#include <emscripten/html5.h>

#include <hex/api/event.hpp>

// Function used by c++ to get the size of the html canvas
EM_JS(int, canvas_get_width, (), {
return Module.canvas.width;
Expand All @@ -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<hex::EventOSThemeChanged>();
}

namespace hex {

void nativeErrorMessage(const std::string &message) {
Expand All @@ -45,6 +62,17 @@ namespace hex {

void Window::setupNativeWindow() {
resizeCanvas();
setupThemeListener();

bool themeFollowSystem = ImHexApi::System::usesSystemThemeDetection();
EventManager::subscribe<EventOSThemeChanged>(this, [themeFollowSystem] {
if (!themeFollowSystem) return;

EventManager::post<RequestChangeTheme>(!isDarkModeEnabled() ? "Light" : "Dark");
});

if (themeFollowSystem)
EventManager::post<EventOSThemeChanged>();
}

void Window::beginNativeWindowFrame() {
Expand Down

0 comments on commit eb41622

Please sign in to comment.