atagen

monolith

public
Star0
Watch0
82c94edb8c503ce7c87c5fbd1a00410265cbad05
1
ssh://brewery@pls.lobotomise.me:33/atagen/monolith.git

Blame: crates/planner/src/plan.rs

b563d2da1c80atagen2026-10-16 08:25 UTC1
use crate::geom::{Change, Rect};
b563d2da1c80atagen2026-10-16 08:25 UTC2
use crate::strategy::plan_group;
b563d2da1c80atagen2026-10-16 08:25 UTC3
b563d2da1c80atagen2026-10-16 08:25 UTC4
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
b563d2da1c80atagen2026-10-16 08:25 UTC5
pub enum Style {
b563d2da1c80atagen2026-10-16 08:25 UTC6
    /// Every window moves or resizes along one axis per phase.
b563d2da1c80atagen2026-10-16 08:25 UTC7
    Orthogonal,
b563d2da1c80atagen2026-10-16 08:25 UTC8
    /// Windows may change both axes in one phase when no pair loses its separation.
b563d2da1c80atagen2026-10-16 08:25 UTC9
    Direct,
b563d2da1c80atagen2026-10-16 08:25 UTC10
}
b563d2da1c80atagen2026-10-16 08:25 UTC11
b563d2da1c80atagen2026-10-16 08:25 UTC12
#[derive(Clone, Copy, Debug, PartialEq)]
b563d2da1c80atagen2026-10-16 08:25 UTC13
pub struct Config {
b563d2da1c80atagen2026-10-16 08:25 UTC14
    pub style: Style,
b563d2da1c80atagen2026-10-16 08:25 UTC15
    /// Gap between tiled windows. Neighbours closer than this are adjacent.
b563d2da1c80atagen2026-10-16 08:25 UTC16
    pub gap: f64,
b563d2da1c80atagen2026-10-16 08:25 UTC17
    /// Tolerance for separation and adjacency tests.
b563d2da1c80atagen2026-10-16 08:25 UTC18
    pub eps: f64,
b563d2da1c80atagen2026-10-16 08:25 UTC19
    /// Lanes thinner than this are not used.
b563d2da1c80atagen2026-10-16 08:25 UTC20
    pub min_lane: f64,
b563d2da1c80atagen2026-10-16 08:25 UTC21
}
b563d2da1c80atagen2026-10-16 08:25 UTC22
b563d2da1c80atagen2026-10-16 08:25 UTC23
impl Default for Config {
b563d2da1c80atagen2026-10-16 08:25 UTC24
    fn default() -> Self {
b563d2da1c80atagen2026-10-16 08:25 UTC25
        Self {
b563d2da1c80atagen2026-10-16 08:25 UTC26
            style: Style::Orthogonal,
b563d2da1c80atagen2026-10-16 08:25 UTC27
            gap: 10.0,
b563d2da1c80atagen2026-10-16 08:25 UTC28
            eps: 0.5,
b563d2da1c80atagen2026-10-16 08:25 UTC29
            min_lane: 6.0,
b563d2da1c80atagen2026-10-16 08:25 UTC30
        }
b563d2da1c80atagen2026-10-16 08:25 UTC31
    }
b563d2da1c80atagen2026-10-16 08:25 UTC32
}
b563d2da1c80atagen2026-10-16 08:25 UTC33
b563d2da1c80atagen2026-10-16 08:25 UTC34
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
b563d2da1c80atagen2026-10-16 08:25 UTC35
pub enum Strategy {
b563d2da1c80atagen2026-10-16 08:25 UTC36
    Unchanged,
b563d2da1c80atagen2026-10-16 08:25 UTC37
    Direct,
b563d2da1c80atagen2026-10-16 08:25 UTC38
    AxisOrder,
b563d2da1c80atagen2026-10-16 08:25 UTC39
    Lanes { lanes: usize },
b563d2da1c80atagen2026-10-16 08:25 UTC40
    Grid { cells: usize },
b563d2da1c80atagen2026-10-16 08:25 UTC41
    Pinch,
b563d2da1c80atagen2026-10-16 08:25 UTC42
}
b563d2da1c80atagen2026-10-16 08:25 UTC43
b563d2da1c80atagen2026-10-16 08:25 UTC44
/// Windows that plan together. Groups never touch each other while moving.
b563d2da1c80atagen2026-10-16 08:25 UTC45
#[derive(Clone, Debug, PartialEq)]
b563d2da1c80atagen2026-10-16 08:25 UTC46
pub struct Group {
b563d2da1c80atagen2026-10-16 08:25 UTC47
    pub members: Vec<usize>,
b563d2da1c80atagen2026-10-16 08:25 UTC48
    pub strategy: Strategy,
b563d2da1c80atagen2026-10-16 08:25 UTC49
    pub phases: usize,
b563d2da1c80atagen2026-10-16 08:25 UTC50
    /// Whether the plan briefly separates two neighbours that touch at both ends.
b563d2da1c80atagen2026-10-16 08:25 UTC51
    pub opens_hole: bool,
b563d2da1c80atagen2026-10-16 08:25 UTC52
}
b563d2da1c80atagen2026-10-16 08:25 UTC53
b563d2da1c80atagen2026-10-16 08:25 UTC54
/// Keyframe layouts indexed like the input. Every phase lasts the same time
b563d2da1c80atagen2026-10-16 08:25 UTC55
/// and every window in a group moves on that group's curve.
b563d2da1c80atagen2026-10-16 08:25 UTC56
#[derive(Clone, Debug, PartialEq)]
b563d2da1c80atagen2026-10-16 08:25 UTC57
pub struct Plan {
b563d2da1c80atagen2026-10-16 08:25 UTC58
    pub keyframes: Vec<Vec<Rect>>,
b563d2da1c80atagen2026-10-16 08:25 UTC59
    pub groups: Vec<Group>,
b563d2da1c80atagen2026-10-16 08:25 UTC60
}
b563d2da1c80atagen2026-10-16 08:25 UTC61
b563d2da1c80atagen2026-10-16 08:25 UTC62
impl Plan {
b563d2da1c80atagen2026-10-16 08:25 UTC63
    pub fn phases(&self) -> usize {
b563d2da1c80atagen2026-10-16 08:25 UTC64
        self.keyframes.len().saturating_sub(1)
b563d2da1c80atagen2026-10-16 08:25 UTC65
    }
b563d2da1c80atagen2026-10-16 08:25 UTC66
b563d2da1c80atagen2026-10-16 08:25 UTC67
    /// Window positions at `phase` with eased progress `s`.
b563d2da1c80atagen2026-10-16 08:25 UTC68
    pub fn sample(&self, phase: usize, s: f64) -> Vec<Rect> {
b563d2da1c80atagen2026-10-16 08:25 UTC69
        match (self.keyframes.get(phase), self.keyframes.get(phase + 1)) {
b563d2da1c80atagen2026-10-16 08:25 UTC70
            (Some(a), Some(b)) => a.iter().zip(b).map(|(a, b)| a.lerp(b, s)).collect(),
b563d2da1c80atagen2026-10-16 08:25 UTC71
            _ => self.keyframes.last().cloned().unwrap_or_default(),
b563d2da1c80atagen2026-10-16 08:25 UTC72
        }
b563d2da1c80atagen2026-10-16 08:25 UTC73
    }
b563d2da1c80atagen2026-10-16 08:25 UTC74
}
b563d2da1c80atagen2026-10-16 08:25 UTC75
b563d2da1c80atagen2026-10-16 08:25 UTC76
pub(crate) struct GroupPlan {
b563d2da1c80atagen2026-10-16 08:25 UTC77
    pub keys: Vec<Vec<Rect>>,
b563d2da1c80atagen2026-10-16 08:25 UTC78
    pub strategy: Strategy,
b563d2da1c80atagen2026-10-16 08:25 UTC79
    pub opens_hole: bool,
b563d2da1c80atagen2026-10-16 08:25 UTC80
}
b563d2da1c80atagen2026-10-16 08:25 UTC81
b563d2da1c80atagen2026-10-16 08:25 UTC82
impl GroupPlan {
b563d2da1c80atagen2026-10-16 08:25 UTC83
    pub fn reversed(mut self) -> Self {
b563d2da1c80atagen2026-10-16 08:25 UTC84
        self.keys.reverse();
b563d2da1c80atagen2026-10-16 08:25 UTC85
        self
b563d2da1c80atagen2026-10-16 08:25 UTC86
    }
b563d2da1c80atagen2026-10-16 08:25 UTC87
}
b563d2da1c80atagen2026-10-16 08:25 UTC88
b563d2da1c80atagen2026-10-16 08:25 UTC89
/// Plans the transition from `from` to `to`. Both slices index the same windows.
b563d2da1c80atagen2026-10-16 08:25 UTC90
pub fn plan(from: &[Rect], to: &[Rect], cfg: &Config) -> Plan {
b563d2da1c80atagen2026-10-16 08:25 UTC91
    assert_eq!(
b563d2da1c80atagen2026-10-16 08:25 UTC92
        from.len(),
b563d2da1c80atagen2026-10-16 08:25 UTC93
        to.len(),
b563d2da1c80atagen2026-10-16 08:25 UTC94
        "layouts must describe the same windows"
b563d2da1c80atagen2026-10-16 08:25 UTC95
    );
b563d2da1c80atagen2026-10-16 08:25 UTC96
    let mut groups = Vec::new();
b563d2da1c80atagen2026-10-16 08:25 UTC97
    let mut plans = Vec::new();
b563d2da1c80atagen2026-10-16 08:25 UTC98
    for members in motion_groups(from, to, cfg) {
b563d2da1c80atagen2026-10-16 08:25 UTC99
        let f: Vec<Rect> = members.iter().map(|&i| from[i]).collect();
b563d2da1c80atagen2026-10-16 08:25 UTC100
        let t: Vec<Rect> = members.iter().map(|&i| to[i]).collect();
b563d2da1c80atagen2026-10-16 08:25 UTC101
        let gp = plan_canonical(&f, &t, cfg);
b563d2da1c80atagen2026-10-16 08:25 UTC102
        groups.push(Group {
b563d2da1c80atagen2026-10-16 08:25 UTC103
            members: members.clone(),
b563d2da1c80atagen2026-10-16 08:25 UTC104
            strategy: gp.strategy,
b563d2da1c80atagen2026-10-16 08:25 UTC105
            phases: gp.keys.len() - 1,
b563d2da1c80atagen2026-10-16 08:25 UTC106
            opens_hole: gp.opens_hole,
b563d2da1c80atagen2026-10-16 08:25 UTC107
        });
b563d2da1c80atagen2026-10-16 08:25 UTC108
        plans.push((members, gp));
b563d2da1c80atagen2026-10-16 08:25 UTC109
    }
b563d2da1c80atagen2026-10-16 08:25 UTC110
    let phases = groups.iter().map(|g| g.phases).max().unwrap_or(0);
b563d2da1c80atagen2026-10-16 08:25 UTC111
    let mut keyframes = vec![to.to_vec(); phases + 1];
b563d2da1c80atagen2026-10-16 08:25 UTC112
    for (members, gp) in &plans {
b563d2da1c80atagen2026-10-16 08:25 UTC113
        for (k, frame) in keyframes.iter_mut().enumerate() {
b563d2da1c80atagen2026-10-16 08:25 UTC114
            let key = &gp.keys[k.min(gp.keys.len() - 1)];
b563d2da1c80atagen2026-10-16 08:25 UTC115
            for (local, &i) in members.iter().enumerate() {
b563d2da1c80atagen2026-10-16 08:25 UTC116
                frame[i] = key[local];
b563d2da1c80atagen2026-10-16 08:25 UTC117
            }
b563d2da1c80atagen2026-10-16 08:25 UTC118
        }
b563d2da1c80atagen2026-10-16 08:25 UTC119
    }
b563d2da1c80atagen2026-10-16 08:25 UTC120
    Plan { keyframes, groups }
b563d2da1c80atagen2026-10-16 08:25 UTC121
}
b563d2da1c80atagen2026-10-16 08:25 UTC122
b563d2da1c80atagen2026-10-16 08:25 UTC123
/// Plans in one fixed direction per pair of layouts, so planning back plays
b563d2da1c80atagen2026-10-16 08:25 UTC124
/// the same plan in reverse.
b563d2da1c80atagen2026-10-16 08:25 UTC125
pub(crate) fn plan_canonical(from: &[Rect], to: &[Rect], cfg: &Config) -> GroupPlan {
b563d2da1c80atagen2026-10-16 08:25 UTC126
    if canonical_order(from, to).is_le() {
b563d2da1c80atagen2026-10-16 08:25 UTC127
        plan_group(from, to, cfg)
b563d2da1c80atagen2026-10-16 08:25 UTC128
    } else {
b563d2da1c80atagen2026-10-16 08:25 UTC129
        plan_group(to, from, cfg).reversed()
b563d2da1c80atagen2026-10-16 08:25 UTC130
    }
b563d2da1c80atagen2026-10-16 08:25 UTC131
}
b563d2da1c80atagen2026-10-16 08:25 UTC132
b563d2da1c80atagen2026-10-16 08:25 UTC133
fn canonical_order(a: &[Rect], b: &[Rect]) -> std::cmp::Ordering {
b563d2da1c80atagen2026-10-16 08:25 UTC134
    let q = |v: f64| (v * 10.0).round() as i64;
b563d2da1c80atagen2026-10-16 08:25 UTC135
    let key = |r: &Rect| [q(r.x), q(r.y), q(r.w), q(r.h)];
b563d2da1c80atagen2026-10-16 08:25 UTC136
    a.iter().map(key).cmp(b.iter().map(key))
b563d2da1c80atagen2026-10-16 08:25 UTC137
}
b563d2da1c80atagen2026-10-16 08:25 UTC138
b563d2da1c80atagen2026-10-16 08:25 UTC139
/// Partitions windows so that windows in different groups can never meet.
b563d2da1c80atagen2026-10-16 08:25 UTC140
/// A moving window joins every window its swept area comes within a gap of.
b563d2da1c80atagen2026-10-16 08:25 UTC141
fn motion_groups(from: &[Rect], to: &[Rect], cfg: &Config) -> Vec<Vec<usize>> {
b563d2da1c80atagen2026-10-16 08:25 UTC142
    let n = from.len();
b563d2da1c80atagen2026-10-16 08:25 UTC143
    let swept: Vec<Rect> = (0..n)
b563d2da1c80atagen2026-10-16 08:25 UTC144
        .map(|i| Rect::bounding([&from[i], &to[i]]).expanded(cfg.gap / 2.0 + cfg.eps))
b563d2da1c80atagen2026-10-16 08:25 UTC145
        .collect();
b563d2da1c80atagen2026-10-16 08:25 UTC146
    let moves: Vec<bool> = (0..n)
b563d2da1c80atagen2026-10-16 08:25 UTC147
        .map(|i| Change::between(&from[i], &to[i]).moves())
b563d2da1c80atagen2026-10-16 08:25 UTC148
        .collect();
b563d2da1c80atagen2026-10-16 08:25 UTC149
    let mut parent: Vec<usize> = (0..n).collect();
b563d2da1c80atagen2026-10-16 08:25 UTC150
    fn find(parent: &mut [usize], mut i: usize) -> usize {
b563d2da1c80atagen2026-10-16 08:25 UTC151
        while parent[i] != i {
b563d2da1c80atagen2026-10-16 08:25 UTC152
            parent[i] = parent[parent[i]];
b563d2da1c80atagen2026-10-16 08:25 UTC153
            i = parent[i];
b563d2da1c80atagen2026-10-16 08:25 UTC154
        }
b563d2da1c80atagen2026-10-16 08:25 UTC155
        i
b563d2da1c80atagen2026-10-16 08:25 UTC156
    }
b563d2da1c80atagen2026-10-16 08:25 UTC157
    for i in 0..n {
b563d2da1c80atagen2026-10-16 08:25 UTC158
        for j in i + 1..n {
b563d2da1c80atagen2026-10-16 08:25 UTC159
            if (moves[i] || moves[j]) && swept[i].intersects(&swept[j]) {
b563d2da1c80atagen2026-10-16 08:25 UTC160
                let (a, b) = (find(&mut parent, i), find(&mut parent, j));
b563d2da1c80atagen2026-10-16 08:25 UTC161
                parent[a] = b;
b563d2da1c80atagen2026-10-16 08:25 UTC162
            }
b563d2da1c80atagen2026-10-16 08:25 UTC163
        }
b563d2da1c80atagen2026-10-16 08:25 UTC164
    }
b563d2da1c80atagen2026-10-16 08:25 UTC165
    let mut groups: Vec<Vec<usize>> = Vec::new();
b563d2da1c80atagen2026-10-16 08:25 UTC166
    let mut slot = vec![usize::MAX; n];
b563d2da1c80atagen2026-10-16 08:25 UTC167
    for i in 0..n {
b563d2da1c80atagen2026-10-16 08:25 UTC168
        let root = find(&mut parent, i);
b563d2da1c80atagen2026-10-16 08:25 UTC169
        if slot[root] == usize::MAX {
b563d2da1c80atagen2026-10-16 08:25 UTC170
            slot[root] = groups.len();
b563d2da1c80atagen2026-10-16 08:25 UTC171
            groups.push(Vec::new());
b563d2da1c80atagen2026-10-16 08:25 UTC172
        }
b563d2da1c80atagen2026-10-16 08:25 UTC173
        groups[slot[root]].push(i);
b563d2da1c80atagen2026-10-16 08:25 UTC174
    }
b563d2da1c80atagen2026-10-16 08:25 UTC175
    groups
b563d2da1c80atagen2026-10-16 08:25 UTC176
}