1
0
mirror of synced 2025-01-25 15:53:43 +01:00

fix: Shortcuts not working correctly in Web build

This commit is contained in:
WerWolv 2024-12-25 01:34:11 +01:00
parent ab34312089
commit c1ed1baaad
4 changed files with 67 additions and 64 deletions

View File

@ -153,12 +153,7 @@ namespace hex {
constexpr static auto SUPER = Key(static_cast<Keys>(0x0800'0000)); constexpr static auto SUPER = Key(static_cast<Keys>(0x0800'0000));
constexpr static auto CurrentView = Key(static_cast<Keys>(0x1000'0000)); constexpr static auto CurrentView = Key(static_cast<Keys>(0x1000'0000));
constexpr static auto AllowWhileTyping = Key(static_cast<Keys>(0x2000'0000)); constexpr static auto AllowWhileTyping = Key(static_cast<Keys>(0x2000'0000));
constexpr static auto CTRLCMD = Key(static_cast<Keys>(0x4000'0000));
#if defined (OS_MACOS)
constexpr static auto CTRLCMD = SUPER;
#else
constexpr static auto CTRLCMD = CTRL;
#endif
class Shortcut { class Shortcut {
public: public:
@ -255,6 +250,8 @@ namespace hex {
static void resumeShortcuts(); static void resumeShortcuts();
static void pauseShortcuts(); static void pauseShortcuts();
static void enableMacOSMode();
[[nodiscard]] static std::optional<Shortcut> getPreviousShortcut(); [[nodiscard]] static std::optional<Shortcut> getPreviousShortcut();
[[nodiscard]] static std::vector<ShortcutEntry> getGlobalShortcuts(); [[nodiscard]] static std::vector<ShortcutEntry> getGlobalShortcuts();

View File

@ -12,6 +12,7 @@ namespace hex {
AutoReset<std::map<Shortcut, ShortcutManager::ShortcutEntry>> s_globalShortcuts; AutoReset<std::map<Shortcut, ShortcutManager::ShortcutEntry>> s_globalShortcuts;
std::atomic<bool> s_paused; std::atomic<bool> s_paused;
std::optional<Shortcut> s_prevShortcut; std::optional<Shortcut> s_prevShortcut;
bool s_macOSMode = false;
} }
@ -78,22 +79,14 @@ namespace hex {
std::string Shortcut::toString() const { std::string Shortcut::toString() const {
std::string result; std::string result;
#if defined(OS_MACOS) const auto CTRL_NAME = s_macOSMode ? "" : "CTRL";
constexpr static auto CTRL_NAME = ""; const auto ALT_NAME = s_macOSMode ? "" : "ALT";
constexpr static auto ALT_NAME = ""; const auto SHIFT_NAME = s_macOSMode ? "" : "SHIFT";
constexpr static auto SHIFT_NAME = ""; const auto SUPER_NAME = s_macOSMode ? "" : "SUPER";
constexpr static auto SUPER_NAME = ""; const auto Concatination = s_macOSMode ? " " : " + ";
constexpr static auto Concatination = " ";
#else
constexpr static auto CTRL_NAME = "CTRL";
constexpr static auto ALT_NAME = "ALT";
constexpr static auto SHIFT_NAME = "SHIFT";
constexpr static auto SUPER_NAME = "SUPER";
constexpr static auto Concatination = " + ";
#endif
auto keys = m_keys; auto keys = m_keys;
if (keys.erase(CTRL) > 0) { if (keys.erase(CTRL) > 0 || (!s_macOSMode && keys.erase(CTRLCMD) > 0)) {
result += CTRL_NAME; result += CTRL_NAME;
result += Concatination; result += Concatination;
} }
@ -105,7 +98,7 @@ namespace hex {
result += SHIFT_NAME; result += SHIFT_NAME;
result += Concatination; result += Concatination;
} }
if (keys.erase(SUPER) > 0) { if (keys.erase(SUPER) > 0 || (s_macOSMode && keys.erase(CTRLCMD) > 0)) {
result += SUPER_NAME; result += SUPER_NAME;
result += Concatination; result += Concatination;
} }
@ -267,13 +260,13 @@ namespace hex {
Shortcut pressedShortcut; Shortcut pressedShortcut;
if (ctrl) if (ctrl)
pressedShortcut += CTRL; pressedShortcut += s_macOSMode ? CTRL : CTRLCMD;
if (alt) if (alt)
pressedShortcut += ALT; pressedShortcut += ALT;
if (shift) if (shift)
pressedShortcut += SHIFT; pressedShortcut += SHIFT;
if (super) if (super)
pressedShortcut += SUPER; pressedShortcut += s_macOSMode ? CTRLCMD : SUPER;
if (focused) if (focused)
pressedShortcut += CurrentView; pressedShortcut += CurrentView;
if (ImGui::GetIO().WantTextInput) if (ImGui::GetIO().WantTextInput)
@ -387,4 +380,9 @@ namespace hex {
return result; return result;
} }
void ShortcutManager::enableMacOSMode() {
s_macOSMode = true;
}
} }

View File

@ -25,6 +25,10 @@ EM_JS(void, resizeCanvas, (), {
js_resizeCanvas(); js_resizeCanvas();
}); });
EM_JS(bool, isMacOS, (), {
return navigator.userAgent.indexOf('Mac OS X') != -1
});
EM_JS(void, fixCanvasInPlace, (), { EM_JS(void, fixCanvasInPlace, (), {
document.getElementById('canvas').classList.add('canvas-fixed'); document.getElementById('canvas').classList.add('canvas-fixed');
}); });
@ -126,6 +130,9 @@ namespace hex {
if (themeFollowSystem) if (themeFollowSystem)
EventOSThemeChanged::post(); EventOSThemeChanged::post();
if (isMacOS())
ShortcutManager::enableMacOSMode();
} }
void Window::beginNativeWindowFrame() { void Window::beginNativeWindowFrame() {

View File

@ -81,6 +81,10 @@ namespace hex {
EventImHexStartupFinished::post(); EventImHexStartupFinished::post();
TutorialManager::init(); TutorialManager::init();
#if defined(OS_MACOS)
ShortcutManager::enableMacOSMode();
#endif
} }
Window::~Window() { Window::~Window() {
@ -881,13 +885,11 @@ namespace hex {
EventWindowFocused::post(focused == GLFW_TRUE); EventWindowFocused::post(focused == GLFW_TRUE);
}); });
#if !defined(OS_WEB)
// Register key press callback // Register key press callback
glfwSetInputMode(m_window, GLFW_LOCK_KEY_MODS, GLFW_TRUE); glfwSetInputMode(m_window, GLFW_LOCK_KEY_MODS, GLFW_TRUE);
glfwSetKeyCallback(m_window, [](GLFWwindow *window, int key, int scanCode, int action, int mods) { glfwSetKeyCallback(m_window, [](GLFWwindow *window, int key, int scanCode, int action, int mods) {
std::ignore = mods; std::ignore = mods;
// Handle A-Z keys using their ASCII value instead of the keycode // Handle A-Z keys using their ASCII value instead of the keycode
if (key >= GLFW_KEY_A && key <= GLFW_KEY_Z) { if (key >= GLFW_KEY_A && key <= GLFW_KEY_Z) {
std::string_view name = glfwGetKeyName(key, scanCode); std::string_view name = glfwGetKeyName(key, scanCode);
@ -930,7 +932,6 @@ namespace hex {
} }
} }
}); });
#endif
// Register window close callback // Register window close callback
glfwSetWindowCloseCallback(m_window, [](GLFWwindow *window) { glfwSetWindowCloseCallback(m_window, [](GLFWwindow *window) {