Skip to content

Commit

Permalink
EditableList using List
Browse files Browse the repository at this point in the history
  • Loading branch information
notdanilo committed Oct 25, 2023
1 parent 3a54034 commit ab36d3d
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions tools/editor/src/gui/ui/utils/list/editable_list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ pub use crate::prelude::*;

use egui::containers::CollapsingHeader;

use super::List;

// TODO: Implement this with List.

pub struct EditableList {
list: List,
editable: bool,
name: String,
add_button_name: String,
id_source: String
}

impl EditableList {
pub fn new(name: impl AsRef<str>, add_button_name: impl AsRef<str>) -> Self {
let list = List::new(name);
let editable = false;
let name = name.as_ref().to_string();
let id_source = name.clone();
let add_button_name = add_button_name.as_ref().into();
Self { editable, name, add_button_name, id_source }
Self { list, editable, add_button_name }
}

pub fn editable(&mut self, editable: bool) -> &mut Self {
Expand All @@ -29,28 +29,23 @@ impl EditableList {
where T: Default
{
if !list.is_empty() || self.editable {
CollapsingHeader::new(&self.name)
.default_open(!list.is_empty())
.id_source(&self.id_source)
.show(ui, |ui| {
let mut remove_list = Vec::new();
for (index, item) in list.iter_mut().enumerate() {
ui.horizontal_top(|ui| {
if self.editable && ui.button("x").clicked() {
remove_list.push(index);
}
ui.push_id(index, |ui| {
show_item(ui, item);
});
});
}
for index in remove_list.into_iter().rev() {
list.remove(index);
let mut remove_list = Vec::new();
self.list.show(ui, list.iter_mut().enumerate(), |ui, (index, item)| {
ui.horizontal_top(|ui| {
if self.editable && ui.button("x").clicked() {
remove_list.push(index);
}
if self.editable && ui.button(&self.add_button_name).clicked() {
list.push(T::default());
}
ui.push_id(index, |ui| {
show_item(ui, item);
});
});
});
for index in remove_list.into_iter().rev() {
list.remove(index);
}
if self.editable && ui.button(&self.add_button_name).clicked() {
list.push(T::default());
}
}
}
}

0 comments on commit ab36d3d

Please sign in to comment.