cydonia 0.1.9

Desktop workspace for the ACP agents you run, keeping what they produce as files on your disk
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! The layouts this machine holds: which entries are on screen together, and
//! how they are arranged.
//!
//! A continuation of [`Workspace`]'s one `impl`, the way [`super::boards`] is.
//!
//! A layout is not a project's. It names entries by the project they are in
//! and the id they have there, so one arrangement can hold a session from one
//! repository beside a board from another — which is why the window owns them
//! and why they are kept beside the config rather than in any `.cydonia/`. See
//! [`crate::model::layouts`] for where they are written.
//!
//! There is no `new_layout` a menu calls: a layout is made by dragging a
//! second entry onto the one already open — see [`Workspace::arrange`].
use super::*;
use crate::model::layouts as store;
use artifact::layout::{Kind as MemberKind, Layout, Member, Side};

/// What a pane shows, resolved from the member a layout names.
///
/// An index into the project's own list, not a borrow: the caller needs the
/// workspace back to draw with. Resolved fresh for each frame rather than kept
/// on the pane — an entry made or dropped beside the open one shifts every
/// index past it, and a pane holding a stale one would draw whatever slid into
/// its place.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Showing {
    /// A session, by the id it was minted with — stable for the life of the
    /// process, so this one is not an index.
    Session(u64),
    Board(usize),
    Article(usize),
    Table(usize),
}

impl Workspace {
    /// The layout the window is arranged by, if one is open.
    pub fn active_layout(&self) -> Option<&Layout> {
        self.layouts.get(self.layout?)
    }

    pub fn active_layout_mut(&mut self) -> Option<&mut Layout> {
        self.layouts.get_mut(self.layout?)
    }

    /// Read every layout back off disk. Called at launch, since nothing else
    /// writes them.
    pub fn reload_layouts(&mut self, cx: &mut Context<Self>) {
        let open = self.active_layout().map(|layout| layout.id.clone());
        self.layouts = store::all();
        self.layout = open.and_then(|id| self.layouts.iter().position(|at| at.id == id));
        cx.notify();
    }

    /// Show a layout. The projects its panes are in are opened if they are not
    /// already — an arrangement is a working context, and half of one is not
    /// what was asked for.
    pub fn open_layout(&mut self, ix: usize, cx: &mut Context<Self>) {
        let Some(layout) = self.layouts.get(ix) else {
            return;
        };
        let wanted: Vec<PathBuf> = layout
            .entries()
            .into_iter()
            .map(|member| member.project)
            .filter(|path| !self.projects.iter().any(|open| open.path == *path))
            .filter(|path| path.is_dir())
            .collect();
        for path in wanted {
            self.open_project(path, cx);
        }
        self.layout = Some(ix);
        cx.notify();
    }

    /// Leave the arrangement, which is what opening any single entry does.
    pub fn leave_layout(&mut self) {
        self.layout = None;
    }

    /// Put `arriving` beside `target`, making the layout that holds them if
    /// there is not one yet.
    ///
    /// This is the whole of how a layout is born: the first drag onto a pane
    /// mints one over the entry already there, and every drag after it lands
    /// in the same layout.
    pub fn arrange(
        &mut self,
        target: &Member,
        arriving: &Member,
        side: Side,
        cx: &mut Context<Self>,
    ) -> Option<usize> {
        let at = match self.layout {
            Some(at) if at < self.layouts.len() => at,
            // The first drag. The layout starts as the pane that was there,
            // which is what the arrival is being put beside.
            _ => {
                let layout = store::create("", target.clone())?;
                self.layouts.insert(0, layout);
                0
            }
        };
        // An entry is in one layout at a time, the way a pane is in one tmux
        // window. Dragging it into another moves it: the sidebar lists it
        // under the layout that has it, and it can only be under one.
        for (ix, other) in self.layouts.iter_mut().enumerate() {
            if ix != at && other.remove(arriving) {
                store::save(other);
            }
        }
        let layout = self.layouts.get_mut(at)?;
        if !layout.insert(target, arriving, side) {
            return None;
        }
        store::save(layout);
        self.layout = Some(at);
        cx.notify();
        Some(at)
    }

    /// The pane across the seam on this side of the one given, if there is one
    /// — what says whether a move that way is on offer at all.
    pub fn neighbour_pane(&self, entry: &Member, side: Side) -> Option<Member> {
        self.active_layout()?.tree.neighbour(entry, side)
    }

    /// Exchange a pane with the one across the seam on that side. The
    /// arrangement keeps its shape and its sizes.
    pub fn move_pane(&mut self, entry: &Member, side: Side, cx: &mut Context<Self>) -> bool {
        let Some(across) = self.neighbour_pane(entry, side) else {
            return false;
        };
        let mut moved = false;
        self.edit_layout(cx, |layout| {
            moved = layout.tree.swap(entry, &across);
            moved
        });
        moved
    }

    /// Move a pane already on screen to another pane's edge.
    pub fn relocate_pane(
        &mut self,
        entry: &Member,
        target: &Member,
        side: Side,
        cx: &mut Context<Self>,
    ) {
        self.edit_layout(cx, |layout| layout.relocate(entry, target, side));
    }

    /// Close one pane.
    ///
    /// Closing the last one closes the layout: an arrangement of one pane is
    /// not an arrangement, and leaving the file behind would put a row in the
    /// sidebar for something the window is no longer doing.
    pub fn close_pane(&mut self, entry: &Member, cx: &mut Context<Self>) {
        let last = self
            .active_layout()
            .is_some_and(|layout| layout.leaves() <= 1);
        if last {
            if let Some(at) = self.layout {
                self.delete_layout(at, cx);
            }
            return;
        }
        self.edit_layout(cx, |layout| layout.remove(entry));
    }

    /// Take an entry out of whatever layout holds it, open or not.
    ///
    /// [`Self::close_pane`] is the gesture's version of this and works on the
    /// layout in front; this one is for an entry that is going away from the
    /// list altogether, which can be holding a pane in an arrangement nobody
    /// is looking at. Taking the last pane out takes the layout with it, the
    /// same rule and for the same reason.
    pub fn drop_from_layouts(&mut self, member: &Member, cx: &mut Context<Self>) {
        let Some(ix) = self.layout_holding(member) else {
            return;
        };
        let Some(layout) = self.layouts.get_mut(ix) else {
            return;
        };
        if layout.leaves() <= 1 {
            self.delete_layout(ix, cx);
            return;
        }
        if layout.remove(member) {
            store::save(layout);
            cx.notify();
        }
    }

    /// Stand one pane over the others, or put it back.
    pub fn zoom_pane(&mut self, entry: &Member, cx: &mut Context<Self>) {
        self.edit_layout(cx, |layout| {
            layout.zoom(entry);
            true
        });
    }

    /// Drop the layout: the file goes with it. The entries it arranged are
    /// left alone — a layout holds none of them.
    pub fn delete_layout(&mut self, ix: usize, cx: &mut Context<Self>) {
        if ix >= self.layouts.len() {
            return;
        }
        store::remove(&self.layouts.remove(ix).id);
        self.layout = self
            .layout
            .filter(|open| *open != ix)
            .map(|open| if open > ix { open - 1 } else { open });
        cx.notify();
    }

    /// Put the arrangement away, or bring it back. The members go with it —
    /// see `Cydonia::archive_entry`, which walks them.
    pub fn archive_layout(&mut self, ix: usize, archived: bool, cx: &mut Context<Self>) {
        let Some(layout) = self.layouts.get_mut(ix) else {
            return;
        };
        layout.archived = archived;
        store::save(layout);
        // An arrangement put away is not the one the window is showing.
        if archived && self.layout == Some(ix) {
            self.layout = None;
        }
        cx.notify();
    }

    pub fn rename_layout(&mut self, id: &str, name: String, cx: &mut Context<Self>) {
        if let Some(layout) = self.layouts.iter_mut().find(|layout| layout.id == id) {
            layout.name = name.trim().to_owned();
            store::save(layout);
            cx.notify();
        }
    }

    /// The layout a given entry is a member of, if any — what the sidebar
    /// lists it under. One put away holds nothing: its members are listed
    /// where they would be without it.
    pub fn layout_holding(&self, member: &Member) -> Option<usize> {
        self.layouts
            .iter()
            .position(|layout| !layout.archived && layout.contains(member))
    }

    /// Drop members whose entries have gone. A layout names entries and holds
    /// none of them, so one deleted from the sidebar leaves a pane pointing at
    /// nothing until this runs.
    ///
    /// Only for projects that are open: a member in a project this window has
    /// never opened is not missing, only out of reach.
    pub fn prune_layouts(&mut self, cx: &mut Context<Self>) {
        let mut live: Vec<Member> = Vec::new();
        for open in &self.projects {
            let path = open.path.clone();
            for chat in &open.sessions {
                if let Some(record) = chat.record.clone() {
                    live.push(Member::new(path.clone(), MemberKind::Session, record));
                }
            }
            for board in &open.boards {
                live.push(Member::new(
                    path.clone(),
                    MemberKind::Board,
                    board.id.clone(),
                ));
            }
            for article in &open.articles {
                live.push(Member::new(
                    path.clone(),
                    MemberKind::Article,
                    article.path.to_string_lossy().into_owned(),
                ));
            }
            for table in &open.tables {
                live.push(Member::new(
                    path.clone(),
                    MemberKind::Table,
                    table.key.clone(),
                ));
            }
        }
        let open: Vec<PathBuf> = self.projects.iter().map(|open| open.path.clone()).collect();
        for layout in &mut self.layouts {
            let gone: Vec<Member> = layout
                .entries()
                .into_iter()
                .filter(|member| open.contains(&member.project) && !live.contains(member))
                .collect();
            if !gone.is_empty() && layout.prune(&gone) {
                store::save(layout);
            }
        }
        cx.notify();
    }

    /// What the member names, and which open project it is in. Nothing for one
    /// whose project is shut, or which has gone since the layout named it —
    /// that draws as an empty pane rather than a panic.
    pub fn showing_of(&self, member: &Member) -> Option<(usize, Showing)> {
        let at = self
            .projects
            .iter()
            .position(|open| open.path == member.project)?;
        let open = self.projects.get(at)?;
        let showing = match member.kind {
            MemberKind::Session => open
                .sessions
                .iter()
                .find(|chat| chat.record.as_deref() == Some(member.id.as_str()))
                .map(|chat| Showing::Session(chat.id))?,
            MemberKind::Board => open
                .boards
                .iter()
                .position(|board| board.id == member.id)
                .map(Showing::Board)?,
            MemberKind::Article => open
                .articles
                .iter()
                .position(|article| article.path.to_string_lossy() == member.id)
                .map(Showing::Article)?,
            MemberKind::Table => open
                .tables
                .iter()
                .position(|table| table.key == member.id)
                .map(Showing::Table)?,
        };
        Some((at, showing))
    }

    /// The member that names what a pane is on, so an entry opened from the
    /// sidebar can be put into a layout.
    ///
    /// Nothing for a session with no file yet: a layout names entries on disk,
    /// and one that has had no turn is not there to be named.
    pub fn member_of(&self, project: usize, showing: Showing) -> Option<Member> {
        let open = self.projects.get(project)?;
        let path = open.path.clone();
        Some(match showing {
            Showing::Session(id) => {
                Member::new(path, MemberKind::Session, open.session(id)?.record.clone()?)
            }
            Showing::Board(ix) => {
                Member::new(path, MemberKind::Board, open.boards.get(ix)?.id.clone())
            }
            Showing::Article(ix) => Member::new(
                path,
                MemberKind::Article,
                open.articles.get(ix)?.path.to_string_lossy().into_owned(),
            ),
            Showing::Table(ix) => {
                Member::new(path, MemberKind::Table, open.tables.get(ix)?.key.clone())
            }
        })
    }

    /// The board a pane is on, by its place in its project. Named apart from
    /// [`Self::board_at`], which finds one by file wherever it is open.
    pub fn board_in(&self, project: usize, ix: usize) -> Option<&Board> {
        if !self.settings.features.boards {
            return None;
        }
        self.projects.get(project)?.boards.get(ix)
    }

    pub fn article_in(&self, project: usize, ix: usize) -> Option<&Article> {
        self.projects.get(project)?.articles.get(ix)
    }

    pub fn table_in(&self, project: usize, ix: usize) -> Option<&Table> {
        if !self.settings.features.tables {
            return None;
        }
        self.projects.get(project)?.tables.get(ix)
    }

    /// The rows a table pane is on.
    pub fn page_in(&self, project: usize, ix: usize) -> Option<&Page> {
        if !self.settings.features.tables {
            return None;
        }
        let open = self.projects.get(project)?;
        open.pages.get(&open.tables.get(ix)?.key)
    }

    pub fn session_in(&self, project: usize, id: u64) -> Option<&ChatSession> {
        self.projects.get(project)?.session(id)
    }

    /// Put the project's selection on what a pane is showing.
    ///
    /// The four slots are what every command without a pane of its own reads,
    /// so this is what makes the focused pane the one they act on. A session
    /// is woken, the way landing on one is — see [`Self::wake_session`].
    pub fn select_showing(&mut self, project: usize, showing: Showing, cx: &mut Context<Self>) {
        let Some(open) = self.projects.get_mut(project) else {
            return;
        };
        match showing {
            Showing::Session(id) => open.active = Some(id),
            Showing::Board(ix) => open.board = Some(ix),
            Showing::Article(ix) => open.article = Some(ix),
            Showing::Table(ix) => {
                open.table = Some(ix);
                open.reload_page();
            }
        }
        self.active = Some(project);
        if let Showing::Session(id) = showing {
            self.wake_session(id, cx);
        }
        cx.notify();
    }

    /// Change the open layout and write it back, if the change took.
    fn edit_layout(&mut self, cx: &mut Context<Self>, edit: impl FnOnce(&mut Layout) -> bool) {
        let Some(layout) = self.layout.and_then(|ix| self.layouts.get_mut(ix)) else {
            return;
        };
        if edit(layout) {
            store::save(layout);
            cx.notify();
        }
    }
}

#[cfg(test)]
#[path = "../../../tests/unit/layouts.rs"]
mod layout_tests;