From 77e41718940635a9977636c2fcba4f1237c54224 Mon Sep 17 00:00:00 2001 From: ocornut Date: Thu, 23 May 2024 16:12:58 +0200 Subject: [PATCH] Shortcuts: added flags to SetNextItemShortcut(). (#456) --- imgui.cpp | 27 ++++++++++++++++----------- imgui.h | 2 +- imgui_internal.h | 1 + 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index d2707f007..20a36d6e0 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -1112,6 +1112,9 @@ static void SetPlatformImeDataFn_DefaultImpl(ImGuiViewport* viewport namespace ImGui { +// Item +static void ItemHandleShortcut(ImGuiID id); + // Navigation static void NavUpdate(); static void NavUpdateWindowing(); @@ -9674,11 +9677,12 @@ bool ImGui::IsKeyChordPressed(ImGuiKeyChord key_chord, ImGuiInputFlags flags, Im return true; } -void ImGui::SetNextItemShortcut(ImGuiKeyChord key_chord) +void ImGui::SetNextItemShortcut(ImGuiKeyChord key_chord, ImGuiInputFlags flags) { ImGuiContext& g = *GImGui; g.NextItemData.Flags |= ImGuiNextItemDataFlags_HasShortcut; g.NextItemData.Shortcut = key_chord; + g.NextItemData.ShortcutFlags = flags; } bool ImGui::Shortcut(ImGuiKeyChord key_chord, ImGuiInputFlags flags) @@ -10012,18 +10016,19 @@ void ImGui::KeepAliveID(ImGuiID id) g.ActiveIdPreviousFrameIsAlive = true; } -static void ItemHandleShortcut(ImGuiID id) +void ImGui::ItemHandleShortcut(ImGuiID id) { - // FIXME: Generalize Activation queue? ImGuiContext& g = *GImGui; - if (ImGui::Shortcut(g.NextItemData.Shortcut, ImGuiInputFlags_None, id) && g.NavActivateId == 0) - { - g.NavActivateId = id; // Will effectively disable clipping. - g.NavActivateFlags = ImGuiActivateFlags_PreferInput | ImGuiActivateFlags_FromShortcut; - //if (g.ActiveId == 0 || g.ActiveId == id) - g.NavActivateDownId = g.NavActivatePressedId = id; - ImGui::NavHighlightActivated(id); - } + ImGuiInputFlags flags = g.NextItemData.ShortcutFlags; + if (!Shortcut(g.NextItemData.Shortcut, flags, id) || g.NavActivateId != 0) + return; + + // FIXME: Generalize Activation queue? + g.NavActivateId = id; // Will effectively disable clipping. + g.NavActivateFlags = ImGuiActivateFlags_PreferInput | ImGuiActivateFlags_FromShortcut; + //if (g.ActiveId == 0 || g.ActiveId == id) + g.NavActivateDownId = g.NavActivatePressedId = id; + NavHighlightActivated(id); } // Declare item bounding box for clipping and interaction. diff --git a/imgui.h b/imgui.h index 7f96f8afe..c0152ff30 100644 --- a/imgui.h +++ b/imgui.h @@ -955,7 +955,7 @@ namespace ImGui // - Shortcut() submits a route, routes are resolved, if it currently can be routed it calls IsKeyChordPressed() -> function has (desirable) side-effects as it can prevents another call from getting the route. // - Visualize registered routes in 'Metrics/Debugger->Inputs'. IMGUI_API bool Shortcut(ImGuiKeyChord key_chord, ImGuiInputFlags flags = 0); - IMGUI_API void SetNextItemShortcut(ImGuiKeyChord key_chord); + IMGUI_API void SetNextItemShortcut(ImGuiKeyChord key_chord, ImGuiInputFlags flags = 0); // Inputs Utilities: Mouse specific // - To refer to a mouse button, you may use named enums in your code e.g. ImGuiMouseButton_Left, ImGuiMouseButton_Right. diff --git a/imgui_internal.h b/imgui_internal.h index c895f7321..fa65dae87 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -1186,6 +1186,7 @@ struct ImGuiNextItemData ImGuiSelectionUserData SelectionUserData; // Set by SetNextItemSelectionUserData() (note that NULL/0 is a valid value, we use -1 == ImGuiSelectionUserData_Invalid to mark invalid values) float Width; // Set by SetNextItemWidth() ImGuiKeyChord Shortcut; // Set by SetNextItemShortcut() + ImGuiInputFlags ShortcutFlags; // Set by SetNextItemShortcut() bool OpenVal; // Set by SetNextItemOpen() ImGuiCond OpenCond : 8;