mod area;
mod fold;
#[cfg(feature = "serde")]
mod saved;
mod solve;
pub use area::{
DefaultLayoutPainter, LayoutArea, LayoutPainter, LayoutStyle, SashPaint, ZoneViewer,
};
pub use fold::{Flyout, FlyoutPaint, StripPaint};
#[cfg(feature = "serde")]
pub use saved::{LAYOUT_VERSION, VersionError};
pub use solve::{Laid, LaidZone, LayoutEvent, Shown, Solve};
use std::collections::BTreeMap;
use std::fmt;
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
serde(transparent)
)]
pub struct ZoneName(pub String);
impl fmt::Display for ZoneName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl From<&str> for ZoneName {
fn from(name: &str) -> Self {
Self(name.to_owned())
}
}
impl From<String> for ZoneName {
fn from(name: String) -> Self {
Self(name)
}
}
impl PartialEq<str> for ZoneName {
fn eq(&self, other: &str) -> bool {
self.0 == other
}
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
serde(try_from = "saved::SavedLayout", into = "saved::SavedLayout")
)]
pub struct Layout {
pub root: Zone,
pub flyouts: BTreeMap<String, f32>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Zone {
pub name: ZoneName,
pub kind: ZoneKind,
pub constraints: Constraints,
pub state: ZoneState,
}
#[derive(Clone, Debug, PartialEq)]
pub enum ZoneKind {
Split {
dir: Dir,
children: Vec<Zone>,
},
Leaf {
content: Content,
},
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
serde(rename_all = "kebab-case")
)]
pub enum Dir {
#[default]
Horizontal,
Vertical,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
serde(rename_all = "kebab-case")
)]
#[non_exhaustive]
pub enum Content {
Dock(String),
App,
#[default]
Empty,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Size {
Points(f32),
Share(f32),
}
impl Size {
pub fn points(self, length: f32) -> f32 {
match self {
Self::Points(points) => points,
Self::Share(share) => share * length,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Priority {
Low,
#[default]
Normal,
High,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Fold {
#[default]
Never,
ToStrip,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Drops {
#[default]
Accept,
Refuse,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[non_exhaustive]
pub struct Constraints {
pub min: Option<Size>,
pub max: Option<Size>,
pub default: Option<Size>,
pub grow: bool,
pub priority: Priority,
pub fit_content: bool,
pub fold: Fold,
pub auto_fold: bool,
pub drops: Drops,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
serde(default)
)]
#[non_exhaustive]
pub struct ZoneState {
pub size: Option<f32>,
pub folded: bool,
pub over: bool,
pub hidden: bool,
}
impl Zone {
pub fn leaf(name: impl Into<ZoneName>, content: Content) -> Self {
Self {
name: name.into(),
kind: ZoneKind::Leaf { content },
constraints: Constraints::default(),
state: ZoneState::default(),
}
}
pub fn split(name: impl Into<ZoneName>, dir: Dir, children: Vec<Self>) -> Self {
Self {
name: name.into(),
kind: ZoneKind::Split { dir, children },
constraints: Constraints::default(),
state: ZoneState::default(),
}
}
pub fn with(mut self, constrain: impl FnOnce(&mut Constraints)) -> Self {
constrain(&mut self.constraints);
self
}
pub fn with_state(mut self, change: impl FnOnce(&mut ZoneState)) -> Self {
change(&mut self.state);
self
}
pub fn children(&self) -> &[Self] {
match &self.kind {
ZoneKind::Split { children, .. } => children,
ZoneKind::Leaf { .. } => &[],
}
}
pub fn find(&self, name: &str) -> Option<&Self> {
if self.name == *name {
return Some(self);
}
self.children().iter().find_map(|child| child.find(name))
}
pub fn visit_mut(&mut self, visit: &mut impl FnMut(&mut Self)) {
visit(self);
if let ZoneKind::Split { children, .. } = &mut self.kind {
for child in children {
child.visit_mut(visit);
}
}
}
pub fn find_mut(&mut self, name: &str) -> Option<&mut Self> {
if self.name == *name {
return Some(self);
}
match &mut self.kind {
ZoneKind::Split { children, .. } => {
children.iter_mut().find_map(|child| child.find_mut(name))
}
ZoneKind::Leaf { .. } => None,
}
}
pub fn visit(&self, visit: &mut impl FnMut(&Self)) {
visit(self);
for child in self.children() {
child.visit(visit);
}
}
}
impl Layout {
pub fn new(root: Zone) -> Self {
Self {
root,
flyouts: BTreeMap::new(),
}
}
pub fn set_folded(&mut self, name: &str, folded: bool) -> bool {
self.zone_mut(name)
.map(|zone| {
zone.state.folded = folded;
zone.state.over = false;
})
.is_some()
}
pub fn set_over(&mut self, name: &str, over: bool) -> bool {
self.zone_mut(name)
.map(|zone| {
zone.state.over = over;
zone.state.folded = !over;
})
.is_some()
}
pub fn single(name: &str) -> Self {
Self::new(Zone::leaf(name, Content::Dock(name.to_owned())))
}
pub fn zone(&self, name: &str) -> Option<&Zone> {
self.root.find(name)
}
pub fn zone_mut(&mut self, name: &str) -> Option<&mut Zone> {
self.root.find_mut(name)
}
pub fn constrain(&mut self, name: &str, constrain: impl FnOnce(&mut Constraints)) -> bool {
self.zone_mut(name)
.map(|zone| constrain(&mut zone.constraints))
.is_some()
}
pub fn names(&self) -> Vec<ZoneName> {
names_under(&self.root)
}
pub fn reconcile(&mut self, code: &Self) -> Repair {
let before = self.names();
if self.root.name != code.root.name {
*self = code.clone();
} else {
let in_code = code.names();
drop_missing(&mut self.root, &in_code);
follow_kinds(&mut self.root, &code.root);
add_missing(&mut self.root, &code.root);
let root = &code.root;
self.root.visit_mut(&mut |zone| {
if let Some(code) = root.find(&zone.name.0) {
zone.constraints = code.constraints;
}
});
}
let after = self.names();
Repair {
added: after
.iter()
.filter(|n| !before.contains(n))
.cloned()
.collect(),
removed: before.into_iter().filter(|n| !after.contains(n)).collect(),
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Repair {
pub added: Vec<ZoneName>,
pub removed: Vec<ZoneName>,
}
impl Repair {
pub fn is_empty(&self) -> bool {
self.added.is_empty() && self.removed.is_empty()
}
}
fn drop_missing(zone: &mut Zone, keep: &[ZoneName]) {
if let ZoneKind::Split { children, .. } = &mut zone.kind {
for mut child in std::mem::take(children) {
drop_missing(&mut child, keep);
if keep.contains(&child.name) {
children.push(child);
} else if let ZoneKind::Split {
children: lifted, ..
} = child.kind
{
children.extend(lifted);
}
}
}
}
fn without_loaded(code: &Zone, loaded: &[ZoneName]) -> Zone {
let mut zone = code.clone();
drop_loaded(&mut zone, loaded);
zone
}
fn drop_loaded(zone: &mut Zone, loaded: &[ZoneName]) {
if let ZoneKind::Split { children, .. } = &mut zone.kind {
children.retain(|child| !loaded.contains(&child.name));
for child in children {
drop_loaded(child, loaded);
}
}
}
fn follow_kinds(root: &mut Zone, code: &Zone) {
let mut mismatched = Vec::new();
root.visit(&mut |zone| {
if let Some(code) = code.find(&zone.name.0) {
let same = matches!(
(&zone.kind, &code.kind),
(ZoneKind::Split { .. }, ZoneKind::Split { .. })
| (ZoneKind::Leaf { .. }, ZoneKind::Leaf { .. })
);
if !same {
mismatched.push(zone.name.clone());
}
}
});
for name in mismatched {
let Some(zone) = root.find_mut(&name.0) else {
continue;
};
zone.kind = ZoneKind::Leaf {
content: Content::Empty,
};
let Some(code) = code.find(&name.0) else {
continue;
};
let loaded = names_under(root);
let kind = without_loaded(code, &loaded).kind;
if let Some(zone) = root.find_mut(&name.0) {
zone.kind = kind;
}
}
}
fn add_missing(root: &mut Zone, code: &Zone) {
for (at, child) in code.children().iter().enumerate() {
let loaded = names_under(root);
if !loaded.contains(&child.name) {
let added = without_loaded(child, &loaded);
if let Some(ZoneKind::Split { children, .. }) =
root.find_mut(&code.name.0).map(|parent| &mut parent.kind)
{
let place = code.children()[..at]
.iter()
.rev()
.find_map(|before| children.iter().position(|c| c.name == before.name))
.map_or(0, |i| i + 1);
children.insert(place, added);
}
}
add_missing(root, child);
}
}
fn names_under(zone: &Zone) -> Vec<ZoneName> {
let mut names = Vec::new();
zone.visit(&mut |under| names.push(under.name.clone()));
names
}
#[cfg(test)]
mod tests;