2023-10-04 12:00:32 +02:00
|
|
|
#include "window.hpp"
|
|
|
|
|
|
|
|
#if defined(OS_WEB)
|
|
|
|
|
|
|
|
#include <emscripten.h>
|
|
|
|
#include <emscripten/html5.h>
|
|
|
|
|
2023-11-18 14:50:43 +01:00
|
|
|
#include <hex/api/event_manager.hpp>
|
2023-10-04 21:34:50 +02:00
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
// Function used by c++ to get the size of the html canvas
|
|
|
|
EM_JS(int, canvas_get_width, (), {
|
|
|
|
return Module.canvas.width;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Function used by c++ to get the size of the html canvas
|
|
|
|
EM_JS(int, canvas_get_height, (), {
|
|
|
|
return Module.canvas.height;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Function called by javascript
|
|
|
|
EM_JS(void, resizeCanvas, (), {
|
|
|
|
js_resizeCanvas();
|
|
|
|
});
|
|
|
|
|
2023-10-04 21:34:50 +02:00
|
|
|
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() {
|
2023-12-08 16:22:36 +01:00
|
|
|
hex::EventOSThemeChanged::post();
|
2023-10-04 21:34:50 +02:00
|
|
|
}
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
namespace hex {
|
|
|
|
|
|
|
|
void nativeErrorMessage(const std::string &message) {
|
|
|
|
log::fatal(message);
|
|
|
|
EM_ASM({
|
|
|
|
alert(UTF8ToString($0));
|
|
|
|
}, message.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::initNative() {
|
|
|
|
EM_ASM({
|
|
|
|
// Save data directory
|
|
|
|
FS.mkdir("/home/web_user/.local");
|
|
|
|
FS.mount(IDBFS, {}, '/home/web_user/.local');
|
|
|
|
|
|
|
|
FS.syncfs(true, function (err) {
|
|
|
|
if (!err)
|
|
|
|
return;
|
|
|
|
alert("Failed to load permanent file system: "+err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::setupNativeWindow() {
|
|
|
|
resizeCanvas();
|
2023-10-04 21:34:50 +02:00
|
|
|
setupThemeListener();
|
|
|
|
|
|
|
|
bool themeFollowSystem = ImHexApi::System::usesSystemThemeDetection();
|
2023-12-08 10:29:44 +01:00
|
|
|
EventOSThemeChanged::subscribe(this, [themeFollowSystem] {
|
2023-10-04 21:34:50 +02:00
|
|
|
if (!themeFollowSystem) return;
|
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
RequestChangeTheme::post(!isDarkModeEnabled() ? "Light" : "Dark");
|
2023-10-04 21:34:50 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (themeFollowSystem)
|
2023-12-08 10:29:44 +01:00
|
|
|
EventOSThemeChanged::post();
|
2023-10-04 12:00:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Window::beginNativeWindowFrame() {
|
|
|
|
static i32 prevWidth = 0;
|
|
|
|
static i32 prevHeight = 0;
|
|
|
|
|
|
|
|
auto width = canvas_get_width();
|
|
|
|
auto height = canvas_get_height();
|
|
|
|
|
|
|
|
if (prevWidth != width || prevHeight != height) {
|
|
|
|
// Size has changed
|
|
|
|
|
|
|
|
prevWidth = width;
|
|
|
|
prevHeight = height;
|
|
|
|
this->resize(width, height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::endNativeWindowFrame() {
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|