use crate::workspace::Workspace;
use ratatui::widgets::TableState;
#[derive(Debug, Clone)]
pub struct WorkspaceState {
pub workspace: Workspace,
pub selected: bool,
}
#[derive(Debug, Clone)]
pub struct WorkspacesModel {
pub workspaces: Vec<WorkspaceState>,
pub table_state: TableState,
}
impl WorkspacesModel {
pub fn new() -> Self {
Self {
workspaces: Vec::new(),
table_state: TableState::default(),
}
}
pub fn load_workspaces(&mut self, workspaces: Vec<Workspace>) {
self.workspaces = workspaces
.into_iter()
.map(|w| WorkspaceState {
workspace: w,
selected: true, })
.collect();
if !self.workspaces.is_empty() {
self.table_state.select(Some(0));
}
}
pub fn get_selected_workspaces(&self) -> Vec<Workspace> {
self.workspaces
.iter()
.filter(|ws| ws.selected)
.map(|ws| ws.workspace.clone())
.collect()
}
pub fn toggle_selection(&mut self, index: usize) {
if let Some(ws) = self.workspaces.get_mut(index) {
ws.selected = !ws.selected;
}
}
pub fn select_all(&mut self) {
for ws in &mut self.workspaces {
ws.selected = true;
}
}
pub fn select_none(&mut self) {
for ws in &mut self.workspaces {
ws.selected = false;
}
}
pub fn selected_count(&self) -> usize {
self.workspaces.iter().filter(|w| w.selected).count()
}
}
impl Default for WorkspacesModel {
fn default() -> Self {
Self::new()
}
}