MultiSelect: Demo: Delete items from menu.
This commit is contained in:
+9
-3
@@ -2778,10 +2778,11 @@ struct ExampleSelection
|
|||||||
// Data
|
// Data
|
||||||
ImGuiStorage Storage; // Selection set
|
ImGuiStorage Storage; // Selection set
|
||||||
int SelectionSize; // Number of selected items (== number of 1 in the Storage, maintained by this class). // FIXME-MULTISELECT: Imply more difficult to track with intrusive selection schemes?
|
int SelectionSize; // Number of selected items (== number of 1 in the Storage, maintained by this class). // FIXME-MULTISELECT: Imply more difficult to track with intrusive selection schemes?
|
||||||
|
bool QueueDeletion; // Request deleting selected items
|
||||||
|
|
||||||
// Functions
|
// Functions
|
||||||
ExampleSelection() { Clear(); }
|
ExampleSelection() { Clear(); }
|
||||||
void Clear() { Storage.Clear(); SelectionSize = 0; }
|
void Clear() { Storage.Clear(); SelectionSize = 0; QueueDeletion = false; }
|
||||||
bool GetSelected(int n) const { return Storage.GetInt((ImGuiID)n, 0) != 0; }
|
bool GetSelected(int n) const { return Storage.GetInt((ImGuiID)n, 0) != 0; }
|
||||||
void SetSelected(int n, bool v) { int* p_int = Storage.GetIntRef((ImGuiID)n, 0); if (*p_int == (int)v) return; if (v) SelectionSize++; else SelectionSize--; *p_int = (bool)v; }
|
void SetSelected(int n, bool v) { int* p_int = Storage.GetIntRef((ImGuiID)n, 0); if (*p_int == (int)v) return; if (v) SelectionSize++; else SelectionSize--; *p_int = (bool)v; }
|
||||||
int GetSize() const { return SelectionSize; }
|
int GetSize() const { return SelectionSize; }
|
||||||
@@ -2822,6 +2823,8 @@ struct ExampleSelection
|
|||||||
template<typename ITEM_TYPE>
|
template<typename ITEM_TYPE>
|
||||||
int ApplyDeletionPreLoop(ImGuiMultiSelectIO* ms_io, ImVector<ITEM_TYPE>& items)
|
int ApplyDeletionPreLoop(ImGuiMultiSelectIO* ms_io, ImVector<ITEM_TYPE>& items)
|
||||||
{
|
{
|
||||||
|
QueueDeletion = false;
|
||||||
|
|
||||||
// If current item is not selected.
|
// If current item is not selected.
|
||||||
if (ms_io->NavIdSelected == false) // Here 'NavIdSelected' should be == to 'GetSelected(ms_io->NavIdData)'
|
if (ms_io->NavIdSelected == false) // Here 'NavIdSelected' should be == to 'GetSelected(ms_io->NavIdData)'
|
||||||
{
|
{
|
||||||
@@ -3130,7 +3133,7 @@ static void ShowDemoWindowMultiSelect()
|
|||||||
// FIXME-MULTISELECT: Shortcut(). Hard to demo this? May be helpful to send a helper/optional "delete" signal.
|
// FIXME-MULTISELECT: Shortcut(). Hard to demo this? May be helpful to send a helper/optional "delete" signal.
|
||||||
// FIXME-MULTISELECT: may turn into 'ms_io->RequestDelete' -> need HasSelection passed.
|
// FIXME-MULTISELECT: may turn into 'ms_io->RequestDelete' -> need HasSelection passed.
|
||||||
// FIXME-MULTISELECT: Test with intermediary modal dialog.
|
// FIXME-MULTISELECT: Test with intermediary modal dialog.
|
||||||
const bool want_delete = (selection.GetSize() > 0) && ImGui::IsWindowFocused() && ImGui::IsKeyPressed(ImGuiKey_Delete);
|
const bool want_delete = selection.QueueDeletion || ((selection.GetSize() > 0) && ImGui::IsWindowFocused() && ImGui::IsKeyPressed(ImGuiKey_Delete));
|
||||||
if (want_delete)
|
if (want_delete)
|
||||||
selection.ApplyDeletionPreLoop(ms_io, items);
|
selection.ApplyDeletionPreLoop(ms_io, items);
|
||||||
const int next_focus_item_idx = (int)(intptr_t)ms_io->RequestFocusItem;
|
const int next_focus_item_idx = (int)(intptr_t)ms_io->RequestFocusItem;
|
||||||
@@ -3222,7 +3225,10 @@ static void ShowDemoWindowMultiSelect()
|
|||||||
// Right-click: context menu
|
// Right-click: context menu
|
||||||
if (ImGui::BeginPopupContextItem())
|
if (ImGui::BeginPopupContextItem())
|
||||||
{
|
{
|
||||||
ImGui::Text("(Testing Selectable inside an embedded popup)");
|
ImGui::BeginDisabled(!use_deletion || selection.GetSize() == 0);
|
||||||
|
sprintf(label, "Delete %d item(s)###DeleteSelected", selection.GetSize());
|
||||||
|
selection.QueueDeletion |= ImGui::Selectable(label);
|
||||||
|
ImGui::EndDisabled();
|
||||||
ImGui::Selectable("Close");
|
ImGui::Selectable("Close");
|
||||||
ImGui::EndPopup();
|
ImGui::EndPopup();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user