1
0
mirror of synced 2025-01-11 05:42:15 +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,56 +885,53 @@ 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
if (key >= GLFW_KEY_A && key <= GLFW_KEY_Z) {
std::string_view name = glfwGetKeyName(key, scanCode);
// Handle A-Z keys using their ASCII value instead of the keycode // If the key name is only one character long, use the ASCII value instead
if (key >= GLFW_KEY_A && key <= GLFW_KEY_Z) { // Otherwise the keyboard was set to a non-English layout and the key name
std::string_view name = glfwGetKeyName(key, scanCode); // is not the same as the ASCII value
if (!name.empty()) {
// If the key name is only one character long, use the ASCII value instead const std::uint8_t byte = name[0];
// Otherwise the keyboard was set to a non-English layout and the key name if (name.length() == 1 && byte <= 0x7F) {
// is not the same as the ASCII value key = std::toupper(byte);
if (!name.empty()) {
const std::uint8_t byte = name[0];
if (name.length() == 1 && byte <= 0x7F) {
key = std::toupper(byte);
}
} }
} }
}
if (key == GLFW_KEY_UNKNOWN) return; if (key == GLFW_KEY_UNKNOWN) return;
if (action == GLFW_PRESS || action == GLFW_REPEAT) { if (action == GLFW_PRESS || action == GLFW_REPEAT) {
if (key != GLFW_KEY_LEFT_CONTROL && key != GLFW_KEY_RIGHT_CONTROL && if (key != GLFW_KEY_LEFT_CONTROL && key != GLFW_KEY_RIGHT_CONTROL &&
key != GLFW_KEY_LEFT_ALT && key != GLFW_KEY_RIGHT_ALT && key != GLFW_KEY_LEFT_ALT && key != GLFW_KEY_RIGHT_ALT &&
key != GLFW_KEY_LEFT_SHIFT && key != GLFW_KEY_RIGHT_SHIFT && key != GLFW_KEY_LEFT_SHIFT && key != GLFW_KEY_RIGHT_SHIFT &&
key != GLFW_KEY_LEFT_SUPER && key != GLFW_KEY_RIGHT_SUPER key != GLFW_KEY_LEFT_SUPER && key != GLFW_KEY_RIGHT_SUPER
) { ) {
auto win = static_cast<Window *>(glfwGetWindowUserPointer(window)); auto win = static_cast<Window *>(glfwGetWindowUserPointer(window));
win->m_unlockFrameRate = true; win->m_unlockFrameRate = true;
if (!(mods & GLFW_MOD_NUM_LOCK)) { if (!(mods & GLFW_MOD_NUM_LOCK)) {
if (key == GLFW_KEY_KP_0) key = GLFW_KEY_INSERT; if (key == GLFW_KEY_KP_0) key = GLFW_KEY_INSERT;
else if (key == GLFW_KEY_KP_1) key = GLFW_KEY_END; else if (key == GLFW_KEY_KP_1) key = GLFW_KEY_END;
else if (key == GLFW_KEY_KP_2) key = GLFW_KEY_DOWN; else if (key == GLFW_KEY_KP_2) key = GLFW_KEY_DOWN;
else if (key == GLFW_KEY_KP_3) key = GLFW_KEY_PAGE_DOWN; else if (key == GLFW_KEY_KP_3) key = GLFW_KEY_PAGE_DOWN;
else if (key == GLFW_KEY_KP_4) key = GLFW_KEY_LEFT; else if (key == GLFW_KEY_KP_4) key = GLFW_KEY_LEFT;
else if (key == GLFW_KEY_KP_6) key = GLFW_KEY_RIGHT; else if (key == GLFW_KEY_KP_6) key = GLFW_KEY_RIGHT;
else if (key == GLFW_KEY_KP_7) key = GLFW_KEY_HOME; else if (key == GLFW_KEY_KP_7) key = GLFW_KEY_HOME;
else if (key == GLFW_KEY_KP_8) key = GLFW_KEY_UP; else if (key == GLFW_KEY_KP_8) key = GLFW_KEY_UP;
else if (key == GLFW_KEY_KP_9) key = GLFW_KEY_PAGE_UP; else if (key == GLFW_KEY_KP_9) key = GLFW_KEY_PAGE_UP;
}
win->m_pressedKeys.push_back(key);
} }
win->m_pressedKeys.push_back(key);
} }
}); }
#endif });
// Register window close callback // Register window close callback
glfwSetWindowCloseCallback(m_window, [](GLFWwindow *window) { glfwSetWindowCloseCallback(m_window, [](GLFWwindow *window) {