ratatui-spatial-splits 0.1.1

Pure geometry engine for spatial split management in ratatui applications
Documentation
//! Core types for the spatial split system.

/// Unique identifier for a leaf area in the split tree.
///
/// Opaque newtype wrapping a `u64`. Created automatically by [`SplitManager`](super::SplitManager).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct AreaId(pub u64);

impl std::fmt::Display for AreaId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "AreaId({})", self.0)
    }
}

/// Split ratio: proportion (0.0–1.0) assigned to the first child.
pub type SplitRatio = f64;

/// A node in the binary split tree.
#[derive(Debug, Clone, PartialEq)]
pub enum SplitNode {
    /// A leaf node representing a content area.
    Leaf {
        /// The unique identifier for this area.
        id: AreaId,
    },
    /// A horizontal split (top/bottom children).
    Horizontal {
        /// The top child node.
        top: Box<SplitNode>,
        /// The bottom child node.
        bottom: Box<SplitNode>,
        /// Proportion of height for the top child (0.0–1.0).
        ratio: SplitRatio,
    },
    /// A vertical split (left/right children).
    Vertical {
        /// The left child node.
        left: Box<SplitNode>,
        /// The right child node.
        right: Box<SplitNode>,
        /// Proportion of width for the left child (0.0–1.0).
        ratio: SplitRatio,
    },
}

/// Result of splitting an area into two.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SplitResult {
    /// The original area that was split.
    pub original: AreaId,
    /// The new area created by the split.
    pub new: AreaId,
}

/// Result of closing (removing) an area.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CloseResult {
    /// The area that was removed.
    pub removed: AreaId,
    /// The surviving sibling area.
    pub surviving: AreaId,
}

impl SplitNode {
    /// Returns `true` if this node is a leaf.
    pub fn is_leaf(&self) -> bool {
        matches!(self, Self::Leaf { .. })
    }

    /// Returns the `AreaId` if this is a leaf, otherwise `None`.
    pub fn leaf_id(&self) -> Option<AreaId> {
        match self {
            Self::Leaf { id } => Some(*id),
            _ => None,
        }
    }
}