Skip to content

Commit

Permalink
Shortcuts, Nav: make SetNextItemShortcut() work when another item is …
Browse files Browse the repository at this point in the history
…active. Made NavCalcPreferredRefPos() take account for remote activation. (#456)

Unsure why filter in ItemHandleShortcut(), will probably find out soon enough.
  • Loading branch information
ocornut committed May 23, 2024
1 parent dc5caa4 commit 197f890
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
20 changes: 14 additions & 6 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10020,8 +10020,8 @@ static void ItemHandleShortcut(ImGuiID id)
{
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;
//if (g.ActiveId == 0 || g.ActiveId == id)
g.NavActivateDownId = g.NavActivatePressedId = id;
ImGui::NavHighlightActivated(id);
}
}
Expand Down Expand Up @@ -12026,7 +12026,10 @@ static ImVec2 ImGui::NavCalcPreferredRefPos()
{
ImGuiContext& g = *GImGui;
ImGuiWindow* window = g.NavWindow;
if (g.NavDisableHighlight || !g.NavDisableMouseHover || !window)
const bool activated_shortcut = g.ActiveId != 0 && g.ActiveIdFromShortcut && g.ActiveId == g.LastItemData.ID;

// Testing for !activated_shortcut here could in theory be removed if we decided that activating a remote shortcut altered one of the g.NavDisableXXX flag.
if ((g.NavDisableHighlight || !g.NavDisableMouseHover || !window) && !activated_shortcut)
{
// Mouse (we need a fallback in case the mouse becomes invalid after being used)
// The +1.0f offset when stored by OpenPopupEx() allows reopening this or another popup (same or another mouse button) while not moving the mouse, it is pretty standard.
Expand All @@ -12037,14 +12040,19 @@ static ImVec2 ImGui::NavCalcPreferredRefPos()
else
{
// When navigation is active and mouse is disabled, pick a position around the bottom left of the currently navigated item
ImRect ref_rect;
if (activated_shortcut)
ref_rect = g.LastItemData.NavRect;
else
ref_rect = WindowRectRelToAbs(window, window->NavRectRel[g.NavLayer]);

// Take account of upcoming scrolling (maybe set mouse pos should be done in EndFrame?)
ImRect rect_rel = WindowRectRelToAbs(window, window->NavRectRel[g.NavLayer]);
if (window->LastFrameActive != g.FrameCount && (window->ScrollTarget.x != FLT_MAX || window->ScrollTarget.y != FLT_MAX))
{
ImVec2 next_scroll = CalcNextScrollFromScrollTargetAndClamp(window);
rect_rel.Translate(window->Scroll - next_scroll);
ref_rect.Translate(window->Scroll - next_scroll);
}
ImVec2 pos = ImVec2(rect_rel.Min.x + ImMin(g.Style.FramePadding.x * 4, rect_rel.GetWidth()), rect_rel.Max.y - ImMin(g.Style.FramePadding.y, rect_rel.GetHeight()));
ImVec2 pos = ImVec2(ref_rect.Min.x + ImMin(g.Style.FramePadding.x * 4, ref_rect.GetWidth()), ref_rect.Max.y - ImMin(g.Style.FramePadding.y, ref_rect.GetHeight()));
ImGuiViewport* viewport = GetMainViewport();
return ImTrunc(ImClamp(pos, viewport->Pos, viewport->Pos + viewport->Size)); // ImTrunc() is important because non-integer mouse position application in backend might be lossy and result in undesirable non-zero delta.
}
Expand Down
2 changes: 1 addition & 1 deletion imgui_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7215,7 +7215,7 @@ struct ExampleAppConsole

// Reserve enough left-over height for 1 separator + 1 input text
const float footer_height_to_reserve = ImGui::GetStyle().ItemSpacing.y + ImGui::GetFrameHeightWithSpacing();
if (ImGui::BeginChild("ScrollingRegion", ImVec2(0, -footer_height_to_reserve), ImGuiChildFlags_None, ImGuiWindowFlags_HorizontalScrollbar))
if (ImGui::BeginChild("ScrollingRegion", ImVec2(0, -footer_height_to_reserve), ImGuiChildFlags_None, ImGuiWindowFlags_HorizontalScrollbar | ImGuiWindowFlags_NavFlattened))
{
if (ImGui::BeginPopupContextWindow())
{
Expand Down

0 comments on commit 197f890

Please sign in to comment.