MultiSelect: Fixed issue with Ctrl+click on TreeNode + amend demo to test drag and drop.

This commit is contained in:
ocornut
2024-07-18 18:19:12 +02:00
parent b9721c1ed7
commit ad5d3c9bff
2 changed files with 21 additions and 6 deletions
+17 -2
View File
@@ -2817,8 +2817,8 @@ static void ShowDemoWindowMultiSelect()
ImGui::TreePop();
}
IMGUI_DEMO_MARKER("Widgets/Selection State/Multiple Selection (Basic)");
if (ImGui::TreeNode("Multiple Selection (Basic)"))
IMGUI_DEMO_MARKER("Widgets/Selection State/Multiple Selection (Simplified)");
if (ImGui::TreeNode("Multiple Selection (Simplified)"))
{
HelpMarker("Hold CTRL and click to select multiple items.");
static bool selection[5] = { false, false, false, false, false };
@@ -2837,6 +2837,7 @@ static void ShowDemoWindowMultiSelect()
}
IMGUI_DEMO_MARKER("Widgets/Selection State/Multiple Selection (Full)");
//ImGui::SetNextItemOpen(true, ImGuiCond_Once);
if (ImGui::TreeNode("Multiple Selection (Full)"))
{
// Demonstrate holding/updating multi-selection data and using the BeginMultiSelect/EndMultiSelect API to support range-selection and clipping.
@@ -2851,14 +2852,18 @@ static void ShowDemoWindowMultiSelect()
// Test both Selectable() and TreeNode() widgets
enum WidgetType { WidgetType_Selectable, WidgetType_TreeNode };
static bool use_columns = false;
static bool use_drag_drop = true;
static WidgetType widget_type = WidgetType_TreeNode;
if (ImGui::RadioButton("Selectables", widget_type == WidgetType_Selectable)) { widget_type = WidgetType_Selectable; }
ImGui::SameLine();
if (ImGui::RadioButton("Tree nodes", widget_type == WidgetType_TreeNode)) { widget_type = WidgetType_TreeNode; }
ImGui::SameLine();
ImGui::Checkbox("Use 2 columns", &use_columns);
ImGui::SameLine();
ImGui::Checkbox("Use drag & drop", &use_drag_drop);
ImGui::CheckboxFlags("io.ConfigFlags: NavEnableKeyboard", &ImGui::GetIO().ConfigFlags, ImGuiConfigFlags_NavEnableKeyboard);
ImGui::SameLine(); HelpMarker("Hold CTRL and click to select multiple items. Hold SHIFT to select a range. Keyboard is also supported.");
ImGui::Text("Selection size: %d", selection.GetSelectionSize());
// Open a scrolling region
const int ITEMS_COUNT = 1000;
@@ -2904,6 +2909,11 @@ static void ShowDemoWindowMultiSelect()
ImGui::Selectable(label, item_is_selected);
if (ImGui::IsItemToggledSelection())
selection.SetSelected(n, !item_is_selected);
if (use_drag_drop && ImGui::BeginDragDropSource())
{
ImGui::Text("(Dragging %d items)", selection.GetSelectionSize());
ImGui::EndDragDropSource();
}
}
else if (widget_type == WidgetType_TreeNode)
{
@@ -2914,6 +2924,11 @@ static void ShowDemoWindowMultiSelect()
bool open = ImGui::TreeNodeEx(label, tree_node_flags);
if (ImGui::IsItemToggledSelection())
selection.SetSelected(n, !item_is_selected);
if (use_drag_drop && ImGui::BeginDragDropSource())
{
ImGui::Text("(Dragging %d items)", selection.GetSelectionSize());
ImGui::EndDragDropSource();
}
if (open)
ImGui::TreePop();
}
+1 -1
View File
@@ -6477,7 +6477,7 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiID storage_id, ImGuiTreeNodeFlags
// Enabling this test makes actions using CTRL+SHIFT delay their effect on MouseUp which is annoying, but it allows drag and drop of multiple items.
// FIXME-MULTISELECT: Consider opt-in for drag and drop behavior in ImGuiMultiSelectFlags?
if (!selected || (g.ActiveId == id && g.ActiveIdHasBeenPressedBefore))
button_flags |= ImGuiButtonFlags_PressedOnClick;
button_flags = (button_flags | ImGuiButtonFlags_PressedOnClick) & ~ImGuiButtonFlags_PressedOnClickRelease;
else
button_flags |= ImGuiButtonFlags_PressedOnClickRelease;
}