Skip to content

Commit c7edb44

Browse files
committed
Shortcut(): always test ownership.
- It doesn't sense to test route without ownership (which may be overrided by code not using routing) - It also wouldn't be possible to call Shortcut() with _None anyway, since successful routing sets ownership. Tangential to experiments for ocornut#7237
1 parent 1844f90 commit c7edb44

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

imgui.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8406,15 +8406,15 @@ static bool IsKeyChordPotentiallyCharInput(ImGuiKeyChord key_chord)
84068406
// - Routes and key ownership are attributed at the beginning of next frame based on best score and mod state.
84078407
// (Conceptually this does a "Submit for next frame" + "Test for current frame".
84088408
// As such, it could be called TrySetXXX or SubmitXXX, or the Submit and Test operations should be separate.)
8409-
// - Using 'owner_id == ImGuiKeyOwner_Any/0': auto-assign an owner based on current focus scope (each window has its focus scope by default)
8410-
// - Using 'owner_id == ImGuiKeyOwner_None': allows disabling/locking a shortcut.
84118409
bool ImGui::SetShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags flags)
84128410
{
84138411
ImGuiContext& g = *GImGui;
84148412
if ((flags & ImGuiInputFlags_RouteMask_) == 0)
84158413
flags |= ImGuiInputFlags_RouteGlobalHigh; // IMPORTANT: This is the default for SetShortcutRouting() but NOT Shortcut()
84168414
else
84178415
IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiInputFlags_RouteMask_)); // Check that only 1 routing flag is used
8416+
IM_ASSERT(owner_id != ImGuiKeyOwner_Any && owner_id != ImGuiKeyOwner_None);
8417+
84188418
if (key_chord & ImGuiMod_Shortcut)
84198419
key_chord = ConvertShortcutMod(key_chord);
84208420

@@ -8454,18 +8454,17 @@ bool ImGui::SetShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiI
84548454
// Submit routing for NEXT frame (assuming score is sufficient)
84558455
// FIXME: Could expose a way to use a "serve last" policy for same score resolution (using <= instead of <).
84568456
ImGuiKeyRoutingData* routing_data = GetShortcutRoutingData(key_chord);
8457-
const ImGuiID routing_id = GetRoutingIdFromOwnerId(owner_id);
84588457
//const bool set_route = (flags & ImGuiInputFlags_ServeLast) ? (score <= routing_data->RoutingNextScore) : (score < routing_data->RoutingNextScore);
84598458
if (score < routing_data->RoutingNextScore)
84608459
{
8461-
routing_data->RoutingNext = routing_id;
8460+
routing_data->RoutingNext = owner_id;
84628461
routing_data->RoutingNextScore = (ImU8)score;
84638462
}
84648463

84658464
// Return routing state for CURRENT frame
8466-
if (routing_data->RoutingCurr == routing_id)
8465+
if (routing_data->RoutingCurr == owner_id)
84678466
IMGUI_DEBUG_LOG_INPUTROUTING("--> granting current route\n");
8468-
return routing_data->RoutingCurr == routing_id;
8467+
return routing_data->RoutingCurr == owner_id;
84698468
}
84708469

84718470
// Currently unused by core (but used by tests)
@@ -9428,6 +9427,13 @@ bool ImGui::Shortcut(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags
94289427
// When using (owner_id == 0/Any): SetShortcutRouting() will use CurrentFocusScopeId and filter with this, so IsKeyPressed() is fine with he 0/Any.
94299428
if ((flags & ImGuiInputFlags_RouteMask_) == 0)
94309429
flags |= ImGuiInputFlags_RouteFocused;
9430+
9431+
// Using 'owner_id == ImGuiKeyOwner_Any/0': auto-assign an owner based on current focus scope (each window has its focus scope by default)
9432+
// Effectively makes Shortcut() always input-owner aware.
9433+
if (owner_id == ImGuiKeyOwner_Any || owner_id == ImGuiKeyOwner_None)
9434+
owner_id = GetRoutingIdFromOwnerId(owner_id);
9435+
9436+
// Submit route
94319437
if (!SetShortcutRouting(key_chord, owner_id, flags))
94329438
return false;
94339439

imgui_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3210,7 +3210,7 @@ namespace ImGui
32103210
// - Shortcut() submits a route then if currently can be routed calls IsKeyChordPressed() -> function has (desirable) side-effects.
32113211
IMGUI_API bool IsKeyChordPressed(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags flags = 0);
32123212
IMGUI_API bool Shortcut(ImGuiKeyChord key_chord, ImGuiID owner_id = 0, ImGuiInputFlags flags = 0);
3213-
IMGUI_API bool SetShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id = 0, ImGuiInputFlags flags = 0);
3213+
IMGUI_API bool SetShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags flags = 0); // owner_id needs to be explicit and cannot be 0
32143214
IMGUI_API bool TestShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id);
32153215
IMGUI_API ImGuiKeyRoutingData* GetShortcutRoutingData(ImGuiKeyChord key_chord);
32163216

0 commit comments

Comments
 (0)