1
0
mirror of https://github.com/ocornut/imgui.git synced 2024-11-24 07:40:22 +01:00

RangeSelect/MultiSelect: (Breaking) Added current_selection_size to BeginMultiSelect().

Required for shortcut routing so we can e.g. have Escape be used to clear selection THEN to exit child window.
This commit is contained in:
ocornut 2023-12-19 14:06:58 +01:00
parent 955210ae5b
commit 9435a3185a
3 changed files with 14 additions and 12 deletions

View File

@ -675,7 +675,7 @@ namespace ImGui
// - This enables standard multi-selection/range-selection idioms (CTRL+Mouse/Keyboard, SHIFT+Mouse/Keyboard, etc.) in a way that also allow a clipper to be used. // - This enables standard multi-selection/range-selection idioms (CTRL+Mouse/Keyboard, SHIFT+Mouse/Keyboard, etc.) in a way that also allow a clipper to be used.
// - ImGuiSelectionUserData is often used to store your item index. // - ImGuiSelectionUserData is often used to store your item index.
// - Read comments near ImGuiMultiSelectIO for instructions/details and see 'Demo->Widgets->Selection State & Multi-Select' for demo. // - Read comments near ImGuiMultiSelectIO for instructions/details and see 'Demo->Widgets->Selection State & Multi-Select' for demo.
IMGUI_API ImGuiMultiSelectIO* BeginMultiSelect(ImGuiMultiSelectFlags flags); IMGUI_API ImGuiMultiSelectIO* BeginMultiSelect(ImGuiMultiSelectFlags flags, int current_selection_size = -1);
IMGUI_API ImGuiMultiSelectIO* EndMultiSelect(); IMGUI_API ImGuiMultiSelectIO* EndMultiSelect();
IMGUI_API void SetNextItemSelectionUserData(ImGuiSelectionUserData selection_user_data); IMGUI_API void SetNextItemSelectionUserData(ImGuiSelectionUserData selection_user_data);
IMGUI_API bool IsItemToggledSelection(); // Was the last item selection state toggled? Useful if you need the per-item information _before_ reaching EndMultiSelect(). We only returns toggle _event_ in order to handle clipping correctly. IMGUI_API bool IsItemToggledSelection(); // Was the last item selection state toggled? Useful if you need the per-item information _before_ reaching EndMultiSelect(). We only returns toggle _event_ in order to handle clipping correctly.

View File

@ -2929,7 +2929,7 @@ struct ExampleDualListBox
if (child_visible) if (child_visible)
{ {
ImGuiMultiSelectFlags flags = ImGuiMultiSelectFlags_None; ImGuiMultiSelectFlags flags = ImGuiMultiSelectFlags_None;
ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(flags); ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(flags, selection.Size);
ApplySelectionRequests(ms_io, side); ApplySelectionRequests(ms_io, side);
for (int item_n = 0; item_n < items.Size; item_n++) for (int item_n = 0; item_n < items.Size; item_n++)
@ -3060,7 +3060,7 @@ static void ShowDemoWindowMultiSelect()
if (ImGui::BeginChild("##Basket", ImVec2(-FLT_MIN, ImGui::GetFontSize() * 20), ImGuiChildFlags_FrameStyle | ImGuiChildFlags_ResizeY)) if (ImGui::BeginChild("##Basket", ImVec2(-FLT_MIN, ImGui::GetFontSize() * 20), ImGuiChildFlags_FrameStyle | ImGuiChildFlags_ResizeY))
{ {
ImGuiMultiSelectFlags flags = ImGuiMultiSelectFlags_ClearOnEscape | ImGuiMultiSelectFlags_BoxSelect; ImGuiMultiSelectFlags flags = ImGuiMultiSelectFlags_ClearOnEscape | ImGuiMultiSelectFlags_BoxSelect;
ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(flags); ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(flags, selection.Size);
selection.ApplyRequests(ms_io, ITEMS_COUNT); selection.ApplyRequests(ms_io, ITEMS_COUNT);
for (int n = 0; n < ITEMS_COUNT; n++) for (int n = 0; n < ITEMS_COUNT; n++)
@ -3094,7 +3094,7 @@ static void ShowDemoWindowMultiSelect()
if (ImGui::BeginChild("##Basket", ImVec2(-FLT_MIN, ImGui::GetFontSize() * 20), ImGuiChildFlags_FrameStyle | ImGuiChildFlags_ResizeY)) if (ImGui::BeginChild("##Basket", ImVec2(-FLT_MIN, ImGui::GetFontSize() * 20), ImGuiChildFlags_FrameStyle | ImGuiChildFlags_ResizeY))
{ {
ImGuiMultiSelectFlags flags = ImGuiMultiSelectFlags_ClearOnEscape | ImGuiMultiSelectFlags_BoxSelect; ImGuiMultiSelectFlags flags = ImGuiMultiSelectFlags_ClearOnEscape | ImGuiMultiSelectFlags_BoxSelect;
ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(flags); ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(flags, selection.Size);
selection.ApplyRequests(ms_io, ITEMS_COUNT); selection.ApplyRequests(ms_io, ITEMS_COUNT);
ImGuiListClipper clipper; ImGuiListClipper clipper;
@ -3158,7 +3158,7 @@ static void ShowDemoWindowMultiSelect()
if (ImGui::BeginChild("##Basket", ImVec2(-FLT_MIN, ImGui::GetFontSize() * 20), ImGuiChildFlags_FrameStyle | ImGuiChildFlags_ResizeY)) if (ImGui::BeginChild("##Basket", ImVec2(-FLT_MIN, ImGui::GetFontSize() * 20), ImGuiChildFlags_FrameStyle | ImGuiChildFlags_ResizeY))
{ {
ImGuiMultiSelectFlags flags = ImGuiMultiSelectFlags_ClearOnEscape | ImGuiMultiSelectFlags_BoxSelect; ImGuiMultiSelectFlags flags = ImGuiMultiSelectFlags_ClearOnEscape | ImGuiMultiSelectFlags_BoxSelect;
ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(flags); ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(flags, selection.Size);
selection.ApplyRequests(ms_io, items.Size); selection.ApplyRequests(ms_io, items.Size);
const bool want_delete = ImGui::Shortcut(ImGuiKey_Delete, ImGuiInputFlags_Repeat) && (selection.Size > 0); const bool want_delete = ImGui::Shortcut(ImGuiKey_Delete, ImGuiInputFlags_Repeat) && (selection.Size > 0);
@ -3274,7 +3274,7 @@ static void ShowDemoWindowMultiSelect()
{ {
ImGui::PushID(selection_scope_n); ImGui::PushID(selection_scope_n);
ImGuiSelectionBasicStorage* selection = &selections_data[selection_scope_n]; ImGuiSelectionBasicStorage* selection = &selections_data[selection_scope_n];
ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(flags); ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(flags, selection->Size);
selection->ApplyRequests(ms_io, ITEMS_COUNT); selection->ApplyRequests(ms_io, ITEMS_COUNT);
ImGui::SeparatorText("Selection scope"); ImGui::SeparatorText("Selection scope");
@ -3373,7 +3373,7 @@ static void ShowDemoWindowMultiSelect()
if (widget_type == WidgetType_TreeNode) if (widget_type == WidgetType_TreeNode)
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(ImGui::GetStyle().ItemSpacing.x, 0.0f)); ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(ImGui::GetStyle().ItemSpacing.x, 0.0f));
ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(flags); ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(flags, selection.Size);
selection.ApplyRequests(ms_io, items.Size); selection.ApplyRequests(ms_io, items.Size);
const bool want_delete = (ImGui::Shortcut(ImGuiKey_Delete, ImGuiInputFlags_Repeat) && (selection.Size > 0)) || request_deletion_from_menu; const bool want_delete = (ImGui::Shortcut(ImGuiKey_Delete, ImGuiInputFlags_Repeat) && (selection.Size > 0)) || request_deletion_from_menu;
@ -9844,7 +9844,7 @@ struct ExampleAssetsBrowser
ms_flags |= ImGuiMultiSelectFlags_SelectOnClickRelease; // To allow dragging an unselected item without altering selection. ms_flags |= ImGuiMultiSelectFlags_SelectOnClickRelease; // To allow dragging an unselected item without altering selection.
if (AllowBoxSelect) if (AllowBoxSelect)
ms_flags |= ImGuiMultiSelectFlags_BoxSelect2d; // Enable box-select in 2D mode. ms_flags |= ImGuiMultiSelectFlags_BoxSelect2d; // Enable box-select in 2D mode.
ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(ms_flags); ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(ms_flags, Selection.Size);
// Use custom selection adapter: store ID in selection (recommended) // Use custom selection adapter: store ID in selection (recommended)
Selection.AdapterData = this; Selection.AdapterData = this;

View File

@ -7293,7 +7293,10 @@ static void DebugLogMultiSelectRequests(const char* function, const ImGuiMultiSe
// Return ImGuiMultiSelectIO structure. // Return ImGuiMultiSelectIO structure.
// Lifetime: don't hold on ImGuiMultiSelectIO* pointers over multiple frames or past any subsequent call to BeginMultiSelect() or EndMultiSelect(). // Lifetime: don't hold on ImGuiMultiSelectIO* pointers over multiple frames or past any subsequent call to BeginMultiSelect() or EndMultiSelect().
ImGuiMultiSelectIO* ImGui::BeginMultiSelect(ImGuiMultiSelectFlags flags) // Passing 'current_selection_size' is currently optional:
// - it is useful for shortcut routing with ImGuiMultiSelectFlags_ClearOnEscape: so we can have Escape be used to clear selection THEN to exit child window.
// - if it is costly for you to compute, but can easily tell if your selection is empty or not, you may alter the ImGuiMultiSelectFlags_ClearOnEscape flag based on that.
ImGuiMultiSelectIO* ImGui::BeginMultiSelect(ImGuiMultiSelectFlags flags, int current_selection_size)
{ {
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
ImGuiWindow* window = g.CurrentWindow; ImGuiWindow* window = g.CurrentWindow;
@ -7360,9 +7363,8 @@ ImGuiMultiSelectIO* ImGui::BeginMultiSelect(ImGuiMultiSelectFlags flags)
if (ms->IsFocused) if (ms->IsFocused)
{ {
// Shortcut: Clear selection (Escape) // Shortcut: Clear selection (Escape)
// FIXME-MULTISELECT: Only hog shortcut if selection is not null, meaning we need "has selection or "selection size" data here. // Only claim shortcut if selection is not empty, allowing further presses on Escape to e.g. leave current child window.
// Otherwise may be done by caller but it means Shortcut() needs to be exposed. if ((flags & ImGuiMultiSelectFlags_ClearOnEscape) && (current_selection_size != 0))
if (flags & ImGuiMultiSelectFlags_ClearOnEscape)
if (Shortcut(ImGuiKey_Escape)) if (Shortcut(ImGuiKey_Escape))
request_clear = true; request_clear = true;