From 9337151a0132c8be7020f36df86a4f6adc6d2c74 Mon Sep 17 00:00:00 2001 From: ocornut Date: Thu, 4 Jan 2024 20:05:05 +0100 Subject: [PATCH] MultiSelect: Box-Select: Fixed initial drag from not claiming hovered id, preventing window behind to move for a frame. --- imgui_demo.cpp | 5 +++-- imgui_widgets.cpp | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/imgui_demo.cpp b/imgui_demo.cpp index 32e6e8f2f..79634d820 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -3086,7 +3086,7 @@ static void ShowDemoWindowMultiSelect() ImGuiListClipper clipper; clipper.Begin(ITEMS_COUNT); - if (ms_io->RangeSrcItem > 0) + if (ms_io->RangeSrcItem != -1) clipper.IncludeItemByIndex((int)ms_io->RangeSrcItem); // Ensure RangeSrc item is not clipped. while (clipper.Step()) { @@ -3210,6 +3210,7 @@ static void ShowDemoWindowMultiSelect() if (ImGui::CheckboxFlags("ImGuiMultiSelectFlags_ScopeRect", &flags, ImGuiMultiSelectFlags_ScopeRect) && (flags & ImGuiMultiSelectFlags_ScopeRect)) flags &= ~ImGuiMultiSelectFlags_ScopeWindow; ImGui::CheckboxFlags("ImGuiMultiSelectFlags_ClearOnClickVoid", &flags, ImGuiMultiSelectFlags_ClearOnClickVoid); + ImGui::CheckboxFlags("ImGuiMultiSelectFlags_BoxSelect", &flags, ImGuiMultiSelectFlags_BoxSelect); for (int selection_scope_n = 0; selection_scope_n < SCOPES_COUNT; selection_scope_n++) { @@ -3336,7 +3337,7 @@ static void ShowDemoWindowMultiSelect() clipper.Begin(items.Size); if (item_curr_idx_to_focus != -1) clipper.IncludeItemByIndex(item_curr_idx_to_focus); // Ensure focused item is not clipped. - if (ms_io->RangeSrcItem > 0) + if (ms_io->RangeSrcItem != -1) clipper.IncludeItemByIndex((int)ms_io->RangeSrcItem); // Ensure RangeSrc item is not clipped. } diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp index f7be14160..646a72852 100644 --- a/imgui_widgets.cpp +++ b/imgui_widgets.cpp @@ -7176,6 +7176,7 @@ bool ImGui::BeginBoxSelect(ImGuiWindow* window, ImGuiID box_select_id, ImGuiMult // IsStarting is set by MultiSelectItemFooter() when considering a possible box-select. We validate it here and lock geometry. if (bs->IsStarting && IsMouseDragPastThreshold(0)) { + IMGUI_DEBUG_LOG_SELECTION("[selection] BeginBoxSelect() 0X%08X: Started.\n", box_select_id); bs->IsActive = true; bs->Window = window; bs->IsStarting = false; @@ -7187,7 +7188,10 @@ bool ImGui::BeginBoxSelect(ImGuiWindow* window, ImGuiID box_select_id, ImGuiMult { bs->IsActive = bs->IsStarting = false; if (g.ActiveId == bs->ID) + { + IMGUI_DEBUG_LOG_SELECTION("[selection] BeginBoxSelect() 0X%08X: Ended.\n", box_select_id); ClearActiveID(); + } bs->ID = 0; } if (!bs->IsActive) @@ -7424,8 +7428,13 @@ ImGuiMultiSelectIO* ImGui::EndMultiSelect() if (scope_hovered && g.HoveredId == 0 && g.ActiveId == 0) { if (ms->Flags & ImGuiMultiSelectFlags_BoxSelect) + { if (!g.BoxSelectState.IsActive && !g.BoxSelectState.IsStarting && g.IO.MouseClickedCount[0] == 1) BoxSelectStartDrag(ms->BoxSelectId, ImGuiSelectionUserData_Invalid); + SetHoveredID(ms->BoxSelectId); + if (ms->Flags & ImGuiMultiSelectFlags_ScopeRect) + SetNavID(0, ImGuiNavLayer_Main, ms->FocusScopeId, ImRect(g.IO.MousePos, g.IO.MousePos)); // Automatically switch FocusScope for initial click from outside to box-select. + } if (ms->Flags & ImGuiMultiSelectFlags_ClearOnClickVoid) if (IsMouseReleased(0) && IsMouseDragPastThreshold(0) == false && g.IO.KeyMods == ImGuiMod_None)