croft-software 0.2.182

VSCode-style TUI written in Rust
//! Sticky scroll: the folders pinned at the top while their contents scroll, and a
//! click on one.

use super::*;

impl FileTree {
    /// Indices into `self.nodes` painted in tree order — every node, in order,
    /// since the Explorer no longer filters its rows. Kept as a method so the
    /// render's scroll machinery can keep mapping a node index to its on-screen
    /// position uniformly.
    pub fn visible_indices(&self) -> Vec<usize> {
        (0..self.nodes.len()).collect()
    }

    /// Test-only view of the band painted this frame.
    #[cfg(test)]
    pub fn sticky_rows_for_test(&self) -> &[(u16, usize)] {
        &self.sticky_rows
    }

    /// Map a click on the sticky band to the pinned directory's node index.
    pub fn sticky_row_at(&self, y: u16) -> Option<usize> {
        self.sticky_rows
            .iter()
            .find(|&&(ry, _)| ry == y)
            .map(|&(_, idx)| idx)
    }
}

/// The ancestor directory chain of `top_idx`, outermost first, capped at
/// `max` keeping the DEEPEST entries (the closest context matters most —
/// VS Code's tree sticky truncates the outer levels the same way). An
/// ancestor is the nearest earlier node with a strictly smaller depth,
/// walked to the root; a top-level node has none.
pub(crate) fn sticky_ancestors(nodes: &[Node], top_idx: usize, max: usize) -> Vec<usize> {
    let Some(top) = nodes.get(top_idx) else {
        return Vec::new();
    };
    let mut chain = Vec::new();
    let mut want_depth = top.depth;
    for i in (0..top_idx).rev() {
        if want_depth == 0 {
            break;
        }
        if nodes[i].depth < want_depth {
            chain.push(i);
            want_depth = nodes[i].depth;
        }
    }
    chain.reverse(); // outermost first
    let skip = chain.len().saturating_sub(max);
    chain.split_off(skip)
}