use crate::{
pane_grid::{Axis, Configuration, Direction, Node, Pane, Split},
Hasher, Point, Rectangle, Size,
};
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct State<T> {
pub(super) panes: HashMap<Pane, T>,
pub(super) internal: Internal,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Focus {
Idle,
Dragging,
}
impl<T> State<T> {
pub fn new(first_pane_state: T) -> (Self, Pane) {
(
Self::with_configuration(Configuration::Pane(first_pane_state)),
Pane(0),
)
}
pub fn with_configuration(config: impl Into<Configuration<T>>) -> Self {
let mut panes = HashMap::new();
let (layout, last_id) =
Self::distribute_content(&mut panes, config.into(), 0);
State {
panes,
internal: Internal {
layout,
last_id,
action: Action::Idle { focus: None },
},
}
}
pub fn len(&self) -> usize {
self.panes.len()
}
pub fn get(&self, pane: &Pane) -> Option<&T> {
self.panes.get(pane)
}
pub fn get_mut(&mut self, pane: &Pane) -> Option<&mut T> {
self.panes.get_mut(pane)
}
pub fn iter(&self) -> impl Iterator<Item = (&Pane, &T)> {
self.panes.iter()
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&Pane, &mut T)> {
self.panes.iter_mut()
}
pub fn layout(&self) -> &Node {
&self.internal.layout
}
pub fn focused(&self) -> Option<Pane> {
self.internal.focused_pane()
}
pub fn active(&self) -> Option<Pane> {
self.internal.active_pane()
}
pub fn adjacent(&self, pane: &Pane, direction: Direction) -> Option<Pane> {
let regions = self
.internal
.layout
.pane_regions(0.0, Size::new(4096.0, 4096.0));
let current_region = regions.get(pane)?;
let target = match direction {
Direction::Left => {
Point::new(current_region.x - 1.0, current_region.y + 1.0)
}
Direction::Right => Point::new(
current_region.x + current_region.width + 1.0,
current_region.y + 1.0,
),
Direction::Up => {
Point::new(current_region.x + 1.0, current_region.y - 1.0)
}
Direction::Down => Point::new(
current_region.x + 1.0,
current_region.y + current_region.height + 1.0,
),
};
let mut colliding_regions =
regions.iter().filter(|(_, region)| region.contains(target));
let (pane, _) = colliding_regions.next()?;
Some(*pane)
}
pub fn focus(&mut self, pane: &Pane) {
self.internal.focus(pane);
}
pub fn unfocus(&mut self) {
self.internal.unfocus();
}
pub fn split(
&mut self,
axis: Axis,
pane: &Pane,
state: T,
) -> Option<(Pane, Split)> {
let node = self.internal.layout.find(pane)?;
let new_pane = {
self.internal.last_id = self.internal.last_id.checked_add(1)?;
Pane(self.internal.last_id)
};
let new_split = {
self.internal.last_id = self.internal.last_id.checked_add(1)?;
Split(self.internal.last_id)
};
node.split(new_split, axis, new_pane);
let _ = self.panes.insert(new_pane, state);
self.focus(&new_pane);
Some((new_pane, new_split))
}
pub fn swap(&mut self, a: &Pane, b: &Pane) {
self.internal.layout.update(&|node| match node {
Node::Split { .. } => {}
Node::Pane(pane) => {
if pane == a {
*node = Node::Pane(*b);
} else if pane == b {
*node = Node::Pane(*a);
}
}
});
}
pub fn resize(&mut self, split: &Split, ratio: f32) {
let _ = self.internal.layout.resize(split, ratio);
}
pub fn close(&mut self, pane: &Pane) -> Option<T> {
if let Some(sibling) = self.internal.layout.remove(pane) {
self.focus(&sibling);
self.panes.remove(pane)
} else {
None
}
}
fn distribute_content(
panes: &mut HashMap<Pane, T>,
content: Configuration<T>,
next_id: usize,
) -> (Node, usize) {
match content {
Configuration::Split { axis, ratio, a, b } => {
let (a, next_id) = Self::distribute_content(panes, *a, next_id);
let (b, next_id) = Self::distribute_content(panes, *b, next_id);
(
Node::Split {
id: Split(next_id),
axis,
ratio,
a: Box::new(a),
b: Box::new(b),
},
next_id + 1,
)
}
Configuration::Pane(state) => {
let id = Pane(next_id);
let _ = panes.insert(id, state);
(Node::Pane(id), next_id + 1)
}
}
}
}
#[derive(Debug, Clone)]
pub struct Internal {
layout: Node,
last_id: usize,
action: Action,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Action {
Idle {
focus: Option<Pane>,
},
Dragging {
pane: Pane,
origin: Point,
focus: Option<Pane>,
},
Resizing {
split: Split,
axis: Axis,
focus: Option<Pane>,
},
}
impl Action {
pub fn focus(&self) -> Option<(Pane, Focus)> {
match self {
Action::Idle { focus } | Action::Resizing { focus, .. } => {
focus.map(|pane| (pane, Focus::Idle))
}
Action::Dragging { pane, .. } => Some((*pane, Focus::Dragging)),
}
}
}
impl Internal {
pub fn action(&self) -> Action {
self.action
}
pub fn focused_pane(&self) -> Option<Pane> {
match self.action {
Action::Idle { focus } => focus,
Action::Dragging { focus, .. } => focus,
Action::Resizing { focus, .. } => focus,
}
}
pub fn active_pane(&self) -> Option<Pane> {
match self.action {
Action::Idle { focus } => focus,
_ => None,
}
}
pub fn picked_pane(&self) -> Option<(Pane, Point)> {
match self.action {
Action::Dragging { pane, origin, .. } => Some((pane, origin)),
_ => None,
}
}
pub fn picked_split(&self) -> Option<(Split, Axis)> {
match self.action {
Action::Resizing { split, axis, .. } => Some((split, axis)),
_ => None,
}
}
pub fn pane_regions(
&self,
spacing: f32,
size: Size,
) -> HashMap<Pane, Rectangle> {
self.layout.pane_regions(spacing, size)
}
pub fn split_regions(
&self,
spacing: f32,
size: Size,
) -> HashMap<Split, (Axis, Rectangle, f32)> {
self.layout.split_regions(spacing, size)
}
pub fn focus(&mut self, pane: &Pane) {
self.action = Action::Idle { focus: Some(*pane) };
}
pub fn pick_pane(&mut self, pane: &Pane, origin: Point) {
let focus = self.focused_pane();
self.action = Action::Dragging {
pane: *pane,
origin,
focus,
};
}
pub fn pick_split(&mut self, split: &Split, axis: Axis) {
if self.picked_pane().is_some() {
return;
}
let focus = self.action.focus().map(|(pane, _)| pane);
self.action = Action::Resizing {
split: *split,
axis,
focus,
};
}
pub fn drop_split(&mut self) {
match self.action {
Action::Resizing { focus, .. } => {
self.action = Action::Idle { focus };
}
_ => {}
}
}
pub fn unfocus(&mut self) {
self.action = Action::Idle { focus: None };
}
pub fn hash_layout(&self, hasher: &mut Hasher) {
use std::hash::Hash;
self.layout.hash(hasher);
}
}