agentmux_srv_core/backend/wcore/
tab.rs1use uuid::Uuid;
7
8use crate::backend::storage::store::Store;
9use crate::backend::storage::StoreError;
10use crate::backend::obj::*;
11
12pub fn create_tab(store: &Store, ws_id: &str) -> Result<Tab, StoreError> {
16 create_tab_with_opts(store, ws_id, "", false)
17}
18
19pub fn create_tab_with_opts(
21 store: &Store,
22 ws_id: &str,
23 tab_name: &str,
24 pinned: bool,
25) -> Result<Tab, StoreError> {
26 let mut ws = store.must_get::<Workspace>(ws_id)?;
27
28 let name = if tab_name.is_empty() {
34 format!("Tab {}", ws.tabids.len() + ws.pinnedtabids.len() + 1)
35 } else {
36 tab_name.to_string()
37 };
38
39 let mut layout = LayoutState {
41 oid: Uuid::new_v4().to_string(),
42 rootnode: None,
43 magnifiednodeid: String::new(),
44 focusednodeid: String::new(),
45 pendingbackendactions: None,
46 meta: None,
47 ..Default::default()
48 };
49 store.insert(&mut layout)?;
50
51 let mut tab = Tab {
52 oid: Uuid::new_v4().to_string(),
53 name,
54 layoutstate: layout.oid.clone(),
55 blockids: vec![],
56 meta: MetaMapType::new(),
57 ..Default::default()
58 };
59 store.insert(&mut tab)?;
60
61 if pinned {
63 ws.pinnedtabids.push(tab.oid.clone());
64 } else {
65 ws.tabids.push(tab.oid.clone());
66 }
67 if ws.activetabid.is_empty() {
68 ws.activetabid = tab.oid.clone();
69 }
70 store.update(&mut ws)?;
71
72 Ok(tab)
73}
74
75pub fn delete_tab(
77 store: &Store,
78 ws_id: &str,
79 tab_id: &str,
80) -> Result<(), StoreError> {
81 let mut ws = store.must_get::<Workspace>(ws_id)?;
82
83 ws.tabids.retain(|id| id != tab_id);
85 ws.pinnedtabids.retain(|id| id != tab_id);
86
87 if ws.activetabid == tab_id {
89 ws.activetabid = ws.tabids.first().cloned().unwrap_or_default();
90 }
91 store.update(&mut ws)?;
92
93 delete_tab_inner(store, tab_id)?;
94 Ok(())
95}
96
97pub(super) fn delete_tab_inner(store: &Store, tab_id: &str) -> Result<(), StoreError> {
100 if let Ok(tab) = store.must_get::<Tab>(tab_id) {
101 for block_id in &tab.blockids {
103 crate::backend::teardown_hooks::hooks().discard(block_id);
104 }
105 if !tab.layoutstate.is_empty() {
107 let _ = store.delete::<LayoutState>(&tab.layoutstate);
108 }
109 for block_id in &tab.blockids {
111 let _ = store.delete::<Block>(block_id);
112 }
113 }
114 let _ = store.delete::<Tab>(tab_id);
115 Ok(())
116}