Skip to main content

agentmux_common/
pin.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! A pane pinned to its window: it stays on screen, at the place it has in its own tab, in every window tab
5//! (`docs/specs/SPEC_PIN_TRANSFORM_FACILITY_2026_12_02.md` ยง9). A pin is a flag: the pane's block stays in its tab
6//! and in that tab's layout, so pinning and unpinning in that tab change no layout at all. Where the pane sits is
7//! read from that layout, the one place its size is kept; the workspace holds only the list of pinned blocks.
8
9use serde::{Deserialize, Serialize};
10
11/// Most panes pinned to one window. One: a pinned pane is drawn over the other tabs' panes, and two would have to be
12/// kept from covering each other.
13pub const MAX_PINS: usize = 1;
14
15/// The refusal a pin past `MAX_PINS` gets.
16pub const REFUSE_FULL: &str = "One pane is pinned to a window at a time: unpin the pinned pane first.";
17/// The refusal the only pane of a tab gets: drawn over the other tabs it would cover each of them whole.
18pub const REFUSE_ALONE: &str = "The only pane of a tab cannot be pinned: it would cover every other tab.";
19/// The refusal a maximized pane gets: a pinned pane is drawn at its place in the layout, which a maximized pane is not at.
20pub const REFUSE_MAXIMIZED: &str = "A maximized pane cannot be pinned: restore it first.";
21
22/// One pinned pane.
23#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
24pub struct PinRecord {
25    /// The block the pin is on: one tab of the pane (a pane with several tabs is pinned whole, through this one).
26    #[serde(rename = "blockid")]
27    pub block_id: String,
28    /// The order the pins were made in, first is 0.
29    pub order: u32,
30}
31
32/// Why `block_id` cannot be added to `pins`, or `None` when it can.
33pub fn refuse_pin(pins: &[PinRecord], block_id: &str) -> Option<&'static str> {
34    if pins.iter().any(|p| p.block_id == block_id) {
35        return Some("that pane is already pinned");
36    }
37    if pins.len() >= MAX_PINS {
38        return Some(REFUSE_FULL);
39    }
40    None
41}
42
43/// The next `order`.
44pub fn next_order(pins: &[PinRecord]) -> u32 {
45    pins.iter().map(|p| p.order + 1).max().unwrap_or(0)
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    fn pin(id: &str, order: u32) -> PinRecord {
53        PinRecord { block_id: id.into(), order }
54    }
55
56    #[test]
57    fn one_pin_at_most_and_a_pane_is_pinned_once() {
58        assert!(refuse_pin(&[], "a").is_none());
59        assert_eq!(refuse_pin(&[pin("a", 0)], "b"), Some(REFUSE_FULL));
60        assert_eq!(refuse_pin(&[pin("a", 0)], "a"), Some("that pane is already pinned"));
61    }
62
63    #[test]
64    fn orders_count_up() {
65        assert_eq!(next_order(&[]), 0);
66        assert_eq!(next_order(&[pin("a", 3)]), 4);
67    }
68
69    #[test]
70    fn a_pin_stored_with_an_edge_and_a_size_still_reads() {
71        // The fields of the pin band this replaced are ignored.
72        let old = r#"{"blockid":"a","edge":"left","size":0.3,"order":2}"#;
73        assert_eq!(serde_json::from_str::<PinRecord>(old).unwrap(), pin("a", 2));
74    }
75}