use std::fmt;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct EditorGroupId(pub u64);
impl fmt::Display for EditorGroupId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "{}", self.0)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum SplitDirection {
#[default]
Horizontal,
Vertical,
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct LayoutTab {
pub id: String,
pub title: String,
pub pinned: bool,
pub preview: bool,
}
impl LayoutTab {
pub fn new(id: impl Into<String>, title: impl Into<String>) -> Self {
Self {
id: id.into(),
title: title.into(),
pinned: false,
preview: false,
}
}
pub fn pinned(mut self) -> Self {
self.pinned = true;
self.preview = false;
self
}
pub fn preview(mut self) -> Self {
self.preview = true;
self.pinned = false;
self
}
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct EditorGroup {
pub id: EditorGroupId,
pub tabs: Vec<LayoutTab>,
pub active: usize,
}
impl EditorGroup {
pub fn new(id: EditorGroupId) -> Self {
Self {
id,
tabs: Vec::new(),
active: 0,
}
}
pub fn with_tabs(id: EditorGroupId, tabs: Vec<LayoutTab>) -> Self {
Self { id, tabs, active: 0 }
}
pub fn active_tab(&self) -> Option<&LayoutTab> {
self.tabs.get(self.active)
}
pub fn normalize(&mut self) {
if self.tabs.is_empty() {
self.active = 0;
} else {
self.active = self.active.min(self.tabs.len() - 1);
}
}
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "kind", rename_all = "snake_case"))]
#[non_exhaustive]
pub enum LayoutNode {
Group(EditorGroup),
Split {
direction: SplitDirection,
ratio: f32,
first: Box<LayoutNode>,
second: Box<LayoutNode>,
},
}
impl LayoutNode {
pub fn group(&self, id: EditorGroupId) -> Option<&EditorGroup> {
match self {
Self::Group(group) => (group.id == id).then_some(group),
Self::Split { first, second, .. } => first.group(id).or_else(|| second.group(id)),
}
}
pub fn group_mut(&mut self, id: EditorGroupId) -> Option<&mut EditorGroup> {
match self {
Self::Group(group) => (group.id == id).then_some(group),
Self::Split { first, second, .. } => first.group_mut(id).or_else(|| second.group_mut(id)),
}
}
pub fn ids(&self) -> Vec<EditorGroupId> {
let mut ids = Vec::new();
self.collect_ids(&mut ids);
ids
}
fn collect_ids(&self, ids: &mut Vec<EditorGroupId>) {
match self {
Self::Group(group) => ids.push(group.id),
Self::Split { first, second, .. } => {
first.collect_ids(ids);
second.collect_ids(ids);
}
}
}
pub fn first_id(&self) -> Option<EditorGroupId> {
self.ids().first().copied()
}
pub(super) fn remove_group(&mut self, id: EditorGroupId) -> bool {
match self {
Self::Group(_) => false,
Self::Split { first, second, .. } => {
if matches!(&**first, Self::Group(group) if group.id == id) {
let replacement = std::mem::replace(&mut **second, Self::Group(EditorGroup::new(id)));
*self = replacement;
return true;
}
if matches!(&**second, Self::Group(group) if group.id == id) {
let replacement = std::mem::replace(&mut **first, Self::Group(EditorGroup::new(id)));
*self = replacement;
return true;
}
first.remove_group(id) || second.remove_group(id)
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn split() -> LayoutNode {
LayoutNode::Split {
direction: SplitDirection::Horizontal,
ratio: 0.5,
first: Box::new(LayoutNode::Group(EditorGroup::new(EditorGroupId(1)))),
second: Box::new(LayoutNode::Group(EditorGroup::new(EditorGroupId(2)))),
}
}
#[test]
fn groups_are_found_in_both_children_and_ids_follow_document_order() {
let mut node = split();
assert!(node.group(EditorGroupId(1)).is_some());
assert!(node.group(EditorGroupId(3)).is_none());
assert!(node.group_mut(EditorGroupId(2)).is_some());
assert!(node.group_mut(EditorGroupId(3)).is_none());
assert_eq!(node.ids(), [EditorGroupId(1), EditorGroupId(2)]);
assert_eq!(node.first_id(), Some(EditorGroupId(1)));
}
#[test]
fn removing_each_child_promotes_the_sibling() {
let mut first = split();
assert!(first.remove_group(EditorGroupId(1)));
assert_eq!(first.ids(), [EditorGroupId(2)]);
let mut second = split();
assert!(second.remove_group(EditorGroupId(2)));
assert_eq!(second.ids(), [EditorGroupId(1)]);
assert!(!LayoutNode::Group(EditorGroup::new(EditorGroupId(4))).remove_group(EditorGroupId(4)));
assert!(!second.remove_group(EditorGroupId(9)));
}
#[test]
fn groups_normalize_empty_and_active_tabs() {
let mut group = EditorGroup::with_tabs(
EditorGroupId(1),
vec![LayoutTab::new("a", "A"), LayoutTab::new("b", "B").pinned()],
);
group.active = 9;
group.normalize();
assert_eq!(group.active, 1);
assert_eq!(group.active_tab().map(|tab| tab.title.as_str()), Some("B"));
group.tabs.clear();
group.normalize();
assert_eq!(group.active, 0);
assert!(group.active_tab().is_none());
}
}