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)
}
}
}
}