Inputs, Shortcut: fixes some edge cases for GetKeyChordName(), clarify that it is aimed at display. (#456)

Rename IsNamedKeyOrModKey() to IsNamedKeyOrMod() for consistency.
Fixed GetKeyName(ImGuiKey_None) from returning "N/A" or "None" depending on IMGUI_DISABLE_OBSOLETE_KEYIO.
See "inputs_keychord_name" in imgui_test_suite.
This commit is contained in:
ocornut 2024-05-17 17:03:00 +02:00
parent 25e279ee73
commit ae8218a3ea
3 changed files with 43 additions and 27 deletions

View File

@ -60,6 +60,8 @@ Other changes:
- e.g. Drags/Sliders now use Cmd+Click to input a value. (#4084) - e.g. Drags/Sliders now use Cmd+Click to input a value. (#4084)
- Some shortcuts still uses Ctrl on Mac: e.g. Ctrl+Tab to switch windows. (#4828) - Some shortcuts still uses Ctrl on Mac: e.g. Ctrl+Tab to switch windows. (#4828)
- Inputs: (OSX) Ctrl+Left Click alias as a Right click. (#2343) [@haldean, @ocornut] - Inputs: (OSX) Ctrl+Left Click alias as a Right click. (#2343) [@haldean, @ocornut]
- Inputs: Fixed ImGui::GetKeyName(ImGuiKey_None) from returning "N/A" or "None" depending
on value of IMGUI_DISABLE_OBSOLETE_KEYIO. It always returns "None".
- Nav: fixed holding Ctrl or gamepad L1 from not slowing down keyboard/gamepad tweak speed. - Nav: fixed holding Ctrl or gamepad L1 from not slowing down keyboard/gamepad tweak speed.
Broken during a refactor refactor for 1.89. Holding Shift/R1 to speed up wasn't broken. Broken during a refactor refactor for 1.89. Holding Shift/R1 to speed up wasn't broken.
- Tables: fixed cell background of fully clipped row overlapping with header. (#7575, #7041) [@prabuinet] - Tables: fixed cell background of fully clipped row overlapping with header. (#7575, #7041) [@prabuinet]

View File

@ -1491,7 +1491,7 @@ void ImGuiIO::AddKeyAnalogEvent(ImGuiKey key, bool down, float analog_value)
if (key == ImGuiKey_None || !AppAcceptingEvents) if (key == ImGuiKey_None || !AppAcceptingEvents)
return; return;
ImGuiContext& g = *Ctx; ImGuiContext& g = *Ctx;
IM_ASSERT(ImGui::IsNamedKeyOrModKey(key)); // Backend needs to pass a valid ImGuiKey_ constant. 0..511 values are legacy native key codes which are not accepted by this API. IM_ASSERT(ImGui::IsNamedKeyOrMod(key)); // Backend needs to pass a valid ImGuiKey_ constant. 0..511 values are legacy native key codes which are not accepted by this API.
IM_ASSERT(ImGui::IsAliasKey(key) == false); // Backend cannot submit ImGuiKey_MouseXXX values they are automatically inferred from AddMouseXXX() events. IM_ASSERT(ImGui::IsAliasKey(key) == false); // Backend cannot submit ImGuiKey_MouseXXX values they are automatically inferred from AddMouseXXX() events.
// MacOS: swap Cmd(Super) and Ctrl // MacOS: swap Cmd(Super) and Ctrl
@ -8250,6 +8250,8 @@ ImGuiID ImGui::GetID(const void* ptr_id)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// [SECTION] INPUTS // [SECTION] INPUTS
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// - GetModForModKey() [Internal]
// - FixupKeyChord() [Internal]
// - GetKeyData() [Internal] // - GetKeyData() [Internal]
// - GetKeyIndex() [Internal] // - GetKeyIndex() [Internal]
// - GetKeyName() // - GetKeyName()
@ -8311,21 +8313,25 @@ ImGuiID ImGui::GetID(const void* ptr_id)
// - Shortcut() [Internal] // - Shortcut() [Internal]
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static ImGuiKeyChord GetModForModKey(ImGuiKey key)
{
if (key == ImGuiKey_LeftCtrl || key == ImGuiKey_RightCtrl)
return ImGuiMod_Ctrl;
if (key == ImGuiKey_LeftShift || key == ImGuiKey_RightShift)
return ImGuiMod_Shift;
if (key == ImGuiKey_LeftAlt || key == ImGuiKey_RightAlt)
return ImGuiMod_Alt;
if (key == ImGuiKey_LeftSuper || key == ImGuiKey_RightSuper)
return ImGuiMod_Super;
return ImGuiMod_None;
}
ImGuiKeyChord ImGui::FixupKeyChord(ImGuiKeyChord key_chord) ImGuiKeyChord ImGui::FixupKeyChord(ImGuiKeyChord key_chord)
{ {
// Add ImGuiMod_XXXX when a corresponding ImGuiKey_LeftXXX/ImGuiKey_RightXXX is specified. // Add ImGuiMod_XXXX when a corresponding ImGuiKey_LeftXXX/ImGuiKey_RightXXX is specified.
ImGuiKey key = (ImGuiKey)(key_chord & ~ImGuiMod_Mask_); ImGuiKey key = (ImGuiKey)(key_chord & ~ImGuiMod_Mask_);
if (IsModKey(key)) if (IsModKey(key))
{ key_chord |= GetModForModKey(key);
if (key == ImGuiKey_LeftCtrl || key == ImGuiKey_RightCtrl)
key_chord |= ImGuiMod_Ctrl;
if (key == ImGuiKey_LeftShift || key == ImGuiKey_RightShift)
key_chord |= ImGuiMod_Shift;
if (key == ImGuiKey_LeftAlt || key == ImGuiKey_RightAlt)
key_chord |= ImGuiMod_Alt;
if (key == ImGuiKey_LeftSuper || key == ImGuiKey_RightSuper)
key_chord |= ImGuiMod_Super;
}
return key_chord; return key_chord;
} }
@ -8386,8 +8392,10 @@ IM_STATIC_ASSERT(ImGuiKey_NamedKey_COUNT == IM_ARRAYSIZE(GKeyNames));
const char* ImGui::GetKeyName(ImGuiKey key) const char* ImGui::GetKeyName(ImGuiKey key)
{ {
if (key == ImGuiKey_None)
return "None";
#ifdef IMGUI_DISABLE_OBSOLETE_KEYIO #ifdef IMGUI_DISABLE_OBSOLETE_KEYIO
IM_ASSERT((IsNamedKeyOrModKey(key) || key == ImGuiKey_None) && "Support for user key indices was dropped in favor of ImGuiKey. Please update backend and user code."); IM_ASSERT(IsNamedKeyOrMod(key) && "Support for user key indices was dropped in favor of ImGuiKey. Please update backend and user code.");
#else #else
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
if (IsLegacyKey(key)) if (IsLegacyKey(key))
@ -8398,8 +8406,6 @@ const char* ImGui::GetKeyName(ImGuiKey key)
key = (ImGuiKey)g.IO.KeyMap[key]; key = (ImGuiKey)g.IO.KeyMap[key];
} }
#endif #endif
if (key == ImGuiKey_None)
return "None";
if (key & ImGuiMod_Mask_) if (key & ImGuiMod_Mask_)
key = ConvertSingleModFlagToKey(key); key = ConvertSingleModFlagToKey(key);
if (!IsNamedKey(key)) if (!IsNamedKey(key))
@ -8408,16 +8414,24 @@ const char* ImGui::GetKeyName(ImGuiKey key)
return GKeyNames[key - ImGuiKey_NamedKey_BEGIN]; return GKeyNames[key - ImGuiKey_NamedKey_BEGIN];
} }
// Return untranslated names: on macOS, Cmd key will show as Ctrl, Ctrl key will show as super.
// Lifetime of return value: valid until next call to same function.
const char* ImGui::GetKeyChordName(ImGuiKeyChord key_chord) const char* ImGui::GetKeyChordName(ImGuiKeyChord key_chord)
{ {
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
key_chord = FixupKeyChord(key_chord);
// Return "Ctrl+LeftShift" instead of "Ctrl+Shift+LeftShift"
const ImGuiKey key = (ImGuiKey)(key_chord & ~ImGuiMod_Mask_);
if (IsModKey(key))
key_chord &= ~GetModForModKey(key);
ImFormatString(g.TempKeychordName, IM_ARRAYSIZE(g.TempKeychordName), "%s%s%s%s%s", ImFormatString(g.TempKeychordName, IM_ARRAYSIZE(g.TempKeychordName), "%s%s%s%s%s",
(key_chord & ImGuiMod_Ctrl) ? "Ctrl+" : "", (key_chord & ImGuiMod_Ctrl) ? "Ctrl+" : "",
(key_chord & ImGuiMod_Shift) ? "Shift+" : "", (key_chord & ImGuiMod_Shift) ? "Shift+" : "",
(key_chord & ImGuiMod_Alt) ? "Alt+" : "", (key_chord & ImGuiMod_Alt) ? "Alt+" : "",
(key_chord & ImGuiMod_Super) ? "Super+" : "", (key_chord & ImGuiMod_Super) ? "Super+" : "",
GetKeyName((ImGuiKey)(key_chord & ~ImGuiMod_Mask_))); (key != ImGuiKey_None || key_chord == ImGuiKey_None) ? GetKeyName(key) : "");
if (key == ImGuiKey_None && key_chord != 0 && g.TempKeychordName[0]) // Remove trailing '+'
g.TempKeychordName[strlen(g.TempKeychordName) - 1] = 0;
return g.TempKeychordName; return g.TempKeychordName;
} }
@ -9524,7 +9538,7 @@ void ImGui::UpdateInputEvents(bool trickle_fast_inputs)
ImGuiID ImGui::GetKeyOwner(ImGuiKey key) ImGuiID ImGui::GetKeyOwner(ImGuiKey key)
{ {
if (!IsNamedKeyOrModKey(key)) if (!IsNamedKeyOrMod(key))
return ImGuiKeyOwner_None; return ImGuiKeyOwner_None;
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
@ -9544,7 +9558,7 @@ ImGuiID ImGui::GetKeyOwner(ImGuiKey key)
// All paths are also testing for key not being locked, for the rare cases that key have been locked with using ImGuiInputFlags_LockXXX flags. // All paths are also testing for key not being locked, for the rare cases that key have been locked with using ImGuiInputFlags_LockXXX flags.
bool ImGui::TestKeyOwner(ImGuiKey key, ImGuiID owner_id) bool ImGui::TestKeyOwner(ImGuiKey key, ImGuiID owner_id)
{ {
if (!IsNamedKeyOrModKey(key)) if (!IsNamedKeyOrMod(key))
return true; return true;
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
@ -9578,7 +9592,7 @@ bool ImGui::TestKeyOwner(ImGuiKey key, ImGuiID owner_id)
void ImGui::SetKeyOwner(ImGuiKey key, ImGuiID owner_id, ImGuiInputFlags flags) void ImGui::SetKeyOwner(ImGuiKey key, ImGuiID owner_id, ImGuiInputFlags flags)
{ {
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
IM_ASSERT(IsNamedKeyOrModKey(key) && (owner_id != ImGuiKeyOwner_Any || (flags & (ImGuiInputFlags_LockThisFrame | ImGuiInputFlags_LockUntilRelease)))); // Can only use _Any with _LockXXX flags (to eat a key away without an ID to retrieve it) IM_ASSERT(IsNamedKeyOrMod(key) && (owner_id != ImGuiKeyOwner_Any || (flags & (ImGuiInputFlags_LockThisFrame | ImGuiInputFlags_LockUntilRelease)))); // Can only use _Any with _LockXXX flags (to eat a key away without an ID to retrieve it)
IM_ASSERT((flags & ~ImGuiInputFlags_SupportedBySetKeyOwner) == 0); // Passing flags not supported by this function! IM_ASSERT((flags & ~ImGuiInputFlags_SupportedBySetKeyOwner) == 0); // Passing flags not supported by this function!
//IMGUI_DEBUG_LOG("SetKeyOwner(%s, owner_id=0x%08X, flags=%08X)\n", GetKeyName(key), owner_id, flags); //IMGUI_DEBUG_LOG("SetKeyOwner(%s, owner_id=0x%08X, flags=%08X)\n", GetKeyName(key), owner_id, flags);

View File

@ -3210,7 +3210,7 @@ namespace ImGui
// Inputs // Inputs
// FIXME: Eventually we should aim to move e.g. IsActiveIdUsingKey() into IsKeyXXX functions. // FIXME: Eventually we should aim to move e.g. IsActiveIdUsingKey() into IsKeyXXX functions.
inline bool IsNamedKey(ImGuiKey key) { return key >= ImGuiKey_NamedKey_BEGIN && key < ImGuiKey_NamedKey_END; } inline bool IsNamedKey(ImGuiKey key) { return key >= ImGuiKey_NamedKey_BEGIN && key < ImGuiKey_NamedKey_END; }
inline bool IsNamedKeyOrModKey(ImGuiKey key) { return (key >= ImGuiKey_NamedKey_BEGIN && key < ImGuiKey_NamedKey_END) || key == ImGuiMod_Ctrl || key == ImGuiMod_Shift || key == ImGuiMod_Alt || key == ImGuiMod_Super; } inline bool IsNamedKeyOrMod(ImGuiKey key) { return (key >= ImGuiKey_NamedKey_BEGIN && key < ImGuiKey_NamedKey_END) || key == ImGuiMod_Ctrl || key == ImGuiMod_Shift || key == ImGuiMod_Alt || key == ImGuiMod_Super; }
inline bool IsLegacyKey(ImGuiKey key) { return key >= ImGuiKey_LegacyNativeKey_BEGIN && key < ImGuiKey_LegacyNativeKey_END; } inline bool IsLegacyKey(ImGuiKey key) { return key >= ImGuiKey_LegacyNativeKey_BEGIN && key < ImGuiKey_LegacyNativeKey_END; }
inline bool IsKeyboardKey(ImGuiKey key) { return key >= ImGuiKey_Keyboard_BEGIN && key < ImGuiKey_Keyboard_END; } inline bool IsKeyboardKey(ImGuiKey key) { return key >= ImGuiKey_Keyboard_BEGIN && key < ImGuiKey_Keyboard_END; }
inline bool IsGamepadKey(ImGuiKey key) { return key >= ImGuiKey_Gamepad_BEGIN && key < ImGuiKey_Gamepad_END; } inline bool IsGamepadKey(ImGuiKey key) { return key >= ImGuiKey_Gamepad_BEGIN && key < ImGuiKey_Gamepad_END; }