2023-10-04 12:00:32 +02:00
|
|
|
#include "window.hpp"
|
|
|
|
|
|
|
|
#if defined(OS_WEB)
|
|
|
|
|
|
|
|
#include <emscripten.h>
|
|
|
|
#include <emscripten/html5.h>
|
|
|
|
|
impr: Refactor and restructure Event Manager (#2082)
### Problem description
This PR addresses issue #2013 that described a cluttered Event Manager.
This is a DX issue and should not impact the users whatsoever.
### Implementation description
The changes revolve around three main points:
1. the Event Manager (`event_manager.hpp`) was split into four
categories: GUI, Interaction, Lifecycle, and Provider, and two types:
Events, and Requests. This results in the following files:
- `events_gui.hpp`
- `events_interaction.hpp`
- `events_lifecycle.hpp`
- `events_provider.hpp`
- `requests_gui.hpp`
- `requests_interaction.hpp`
- `requests_lifecycle.hpp`
- `requests_provider.hpp`
2. Every event and request now has its own piece of documentation, with
a `@brief`, accompanied by a longer comment if needed, and gets its
`@param`s described.
3. The old `event_manager.hpp` import was removed and replaced by the
correct imports wherever needed, as to reduce spread of those files only
to where they are truly useful.
### Additional things
The commits have been split into (chrono-)logical steps:
- `feat`: split the Event Manager, and replace the imports
- `refactor`, `chore`: make various small changes to match the required
structure
- `docs`: add documentation for events and requests
Hopefully, this will help to review the PR.
*Note: Beware of very long rebuild times in between the commits, use
them sparingly! The Actions will ensure this PR builds anyways*
Closes #2013
---------
Signed-off-by: BioTheWolff <47079795+BioTheWolff@users.noreply.github.com>
Co-authored-by: Nik <werwolv98@gmail.com>
2025-01-25 16:32:07 +01:00
|
|
|
#include <hex/api/events/events_gui.hpp>
|
|
|
|
#include <hex/api/events/events_interaction.hpp>
|
|
|
|
#include <hex/api/events/requests_gui.hpp>
|
2025-01-25 22:27:59 +01:00
|
|
|
#include <hex/api/theme_manager.hpp>
|
2023-10-04 21:34:50 +02:00
|
|
|
|
2024-07-03 22:32:33 +02:00
|
|
|
#include <imgui.h>
|
|
|
|
#include <imgui_internal.h>
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2024-12-25 01:34:11 +01:00
|
|
|
EM_JS(bool, isMacOS, (), {
|
|
|
|
return navigator.userAgent.indexOf('Mac OS X') != -1
|
|
|
|
});
|
|
|
|
|
2024-07-04 21:37:51 +02:00
|
|
|
EM_JS(void, fixCanvasInPlace, (), {
|
|
|
|
document.getElementById('canvas').classList.add('canvas-fixed');
|
|
|
|
});
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-07-03 22:32:33 +02:00
|
|
|
|
|
|
|
EM_JS(void, setupInputModeListener, (), {
|
|
|
|
Module.canvas.addEventListener('mousedown', function() {
|
|
|
|
Module._enterMouseMode();
|
|
|
|
});
|
|
|
|
|
|
|
|
Module.canvas.addEventListener('touchstart', function() {
|
|
|
|
Module._enterTouchMode();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
EMSCRIPTEN_KEEPALIVE
|
|
|
|
extern "C" void enterMouseMode() {
|
|
|
|
ImGui::GetIO().AddMouseSourceEvent(ImGuiMouseSource_Mouse);
|
|
|
|
}
|
|
|
|
|
|
|
|
EMSCRIPTEN_KEEPALIVE
|
|
|
|
extern "C" void enterTouchMode() {
|
|
|
|
ImGui::GetIO().AddMouseSourceEvent(ImGuiMouseSource_TouchScreen);
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2024-05-03 21:39:31 +02:00
|
|
|
void Window::configureGLFW() {
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
|
|
|
|
glfwWindowHint(GLFW_DECORATED, GL_FALSE);
|
|
|
|
glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_FALSE);
|
|
|
|
}
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
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);
|
|
|
|
});
|
2024-07-04 21:37:51 +02:00
|
|
|
|
|
|
|
// Center splash screen
|
|
|
|
document.getElementById('canvas').classList.remove('canvas-fixed');
|
2023-10-04 12:00:32 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::setupNativeWindow() {
|
|
|
|
resizeCanvas();
|
2023-10-04 21:34:50 +02:00
|
|
|
setupThemeListener();
|
2024-07-03 22:32:33 +02:00
|
|
|
setupInputModeListener();
|
2024-07-04 21:37:51 +02:00
|
|
|
fixCanvasInPlace();
|
2023-10-04 21:34:50 +02:00
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2024-01-20 21:03:46 +01:00
|
|
|
// Register file drop callback
|
|
|
|
glfwSetDropCallback(m_window, [](GLFWwindow *, int count, const char **paths) {
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
EventFileDropped::post(reinterpret_cast<const char8_t *>(paths[i]));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-06-28 21:27:35 +02:00
|
|
|
glfwSetWindowRefreshCallback(m_window, [](GLFWwindow *window) {
|
|
|
|
auto win = static_cast<Window *>(glfwGetWindowUserPointer(window));
|
2024-07-01 20:09:16 +02:00
|
|
|
resizeCanvas();
|
2024-06-28 21:27:35 +02:00
|
|
|
win->fullFrame();
|
|
|
|
});
|
|
|
|
|
2023-10-04 21:34:50 +02:00
|
|
|
if (themeFollowSystem)
|
2023-12-08 10:29:44 +01:00
|
|
|
EventOSThemeChanged::post();
|
2024-12-25 01:34:11 +01:00
|
|
|
|
|
|
|
if (isMacOS())
|
|
|
|
ShortcutManager::enableMacOSMode();
|
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() {
|
2025-01-25 22:27:59 +01:00
|
|
|
static float prevScaleFactor = 0;
|
|
|
|
|
|
|
|
const float currScaleFactor = MAIN_THREAD_EM_ASM_DOUBLE({
|
|
|
|
try {
|
|
|
|
// Take square root of scaling to counter scaling applied by Browser
|
|
|
|
return Math.sqrt(window.devicePixelRatio);
|
|
|
|
} catch (e) {
|
|
|
|
return 1.0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (prevScaleFactor != 0 && prevScaleFactor != currScaleFactor) {
|
|
|
|
EventDPIChanged::post(prevScaleFactor, currScaleFactor);
|
|
|
|
resizeCanvas();
|
|
|
|
|
|
|
|
ImHexApi::System::impl::setNativeScale(currScaleFactor);
|
|
|
|
|
|
|
|
ThemeManager::reapplyCurrentTheme();
|
|
|
|
ImGui::GetStyle().ScaleAllSizes(currScaleFactor);
|
|
|
|
}
|
|
|
|
|
|
|
|
prevScaleFactor = currScaleFactor;
|
2023-10-04 12:00:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|