use crate::model::{GroupId, PanelId};
#[derive(Clone, Debug, serde::Deserialize, PartialEq, serde::Serialize)]
pub struct Group {
pub id: GroupId,
pub tabs: Vec<PanelId>,
pub active: usize,
}
impl Group {
pub fn new(id: GroupId, panel: PanelId) -> Self {
Self { id, tabs: vec![panel], active: 0 }
}
pub fn active_panel(&self) -> &PanelId {
&self.tabs[self.active]
}
pub fn insert_tab(&mut self, panel: PanelId, index: usize) {
assert!(index <= self.tabs.len(), "insert_tab: index out of range");
self.tabs.insert(index, panel);
self.active = index;
}
pub fn remove_tab(&mut self, panel: &PanelId) -> bool {
let pos = self.tabs.iter().position(|p| p == panel).expect("remove_tab: panel not in group");
self.tabs.remove(pos);
if self.tabs.is_empty() {
return true;
}
if self.active > pos {
self.active -= 1;
}
self.active = self.active.min(self.tabs.len() - 1);
false
}
}