Skip to content

Commit 6c1c12c

Browse files
committed
Remove empty parent menus when menu item is deleted.
1 parent 8cde0df commit 6c1c12c

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/ui/model_data/src/model_data_actor.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,24 @@ utility::JsonTree *find_node_matching_string_field(
6969
}
7070

7171
bool find_and_delete(
72-
utility::JsonTree *data, const std::string &field, const std::string &field_value) {
72+
utility::JsonTree *data, const std::string &field, const std::string &field_value, const bool cleanup_empty_nodes=false) {
7373
if (data->data().contains(field) && data->data()[field].get<std::string>() == field_value) {
7474

7575
data->parent()->erase(std::next(data->parent()->begin(), data->index()));
7676
return true;
7777

7878
} else {
7979
for (auto p = data->begin(); p != data->end(); ++p) {
80-
if (find_and_delete(&(*p), field, field_value)) {
80+
if (find_and_delete(&(*p), field, field_value, cleanup_empty_nodes)) {
81+
if (cleanup_empty_nodes &&
82+
data->empty() && data->parent()) {
83+
// after we've done a deletion, does that leave the node's
84+
// parent empty? For menu models, this will leave an empty
85+
// submenu which will generally not be desirable.
86+
// Therefore, remove the empty parent node here.
87+
data->parent()->erase(std::next(data->parent()->begin(), data->index()));
88+
}
89+
8190
return true;
8291
}
8392
}
@@ -1404,7 +1413,7 @@ void GlobalUIModelData::remove_node(
14041413

14051414
std::string uuid_string = to_string(model_item_id);
14061415
std::string field("uuid");
1407-
if (find_and_delete(menu_model_data, field, uuid_string)) {
1416+
if (find_and_delete(menu_model_data, field, uuid_string, true)) {
14081417
broadcast_whole_model_data(mmn->name_);
14091418
}
14101419
}

0 commit comments

Comments
 (0)