Skip to content

Commit

Permalink
Shortcuts: added flags to SetNextItemShortcut(). (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocornut committed May 23, 2024
1 parent 197f890 commit 77e4171
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
27 changes: 16 additions & 11 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions imgui_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 77e4171

Please sign in to comment.