libbhoo 0.3.1

Minimal UI foundations for SpectreOS
Documentation
//! Keyed placement motion for reorderable grids and lists.

use crate::{Frame, Rect};
use std::collections::HashMap;

#[derive(Clone, Copy)]
struct Placement {
    from: [f32; 2],
    to: [f32; 2],
    start: f64,
    epoch: u64,
}

impl Placement {
    fn sample(self, now: f64, duration: f64) -> [f32; 2] {
        let t = ((now - self.start) / duration).clamp(0.0, 1.0) as f32;
        let eased = (1.0 - (std::f32::consts::PI * t).cos()) * 0.5;
        std::array::from_fn(|i| self.from[i] + (self.to[i] - self.from[i]) * eased)
    }
}

/// Keeps the presented position of each item while the caller changes slot order.
#[derive(Default)]
pub struct ReorderMotion {
    placements: HashMap<u64, Placement>,
    epoch: u64,
}

impl ReorderMotion {
    pub fn clear(&mut self) {
        self.placements.clear();
    }

    /// Return presented bounds in destination order. Keys must be stable and
    /// unique. Supply bounds and viewport in unscrolled content coordinates.
    /// The caller owns drag tracking, slot choice, clipping and item rendering.
    pub fn layout(
        &mut self,
        frame: &mut Frame<'_>,
        viewport: Rect,
        duration: f64,
        items: impl IntoIterator<Item = (u64, Rect)>,
    ) -> Vec<Rect> {
        let reduced = frame.reduced_motion() || !duration.is_finite() || duration <= 0.0;
        let now = frame.time();
        self.epoch = self.epoch.wrapping_add(1);
        let epoch = self.epoch;
        let mut visible_motion = false;
        let bounds = items
            .into_iter()
            .map(|(id, target)| {
                let to = [target.x, target.y];
                let placement = self.placements.entry(id).or_insert(Placement {
                    from: to,
                    to,
                    start: now,
                    epoch,
                });
                let current = if reduced {
                    to
                } else {
                    placement.sample(now, duration)
                };
                if reduced || placement.to != to {
                    *placement = Placement {
                        from: current,
                        to,
                        start: now,
                        epoch,
                    };
                }
                placement.epoch = epoch;
                let presented = Rect {
                    x: current[0],
                    y: current[1],
                    ..target
                };
                let on_screen = |rect: Rect| {
                    let area = rect.intersect(viewport);
                    area.width > 0.0 && area.height > 0.0
                };
                visible_motion |=
                    !reduced && current != to && (on_screen(presented) || on_screen(target));
                presented
            })
            .collect();
        self.placements
            .retain(|_, placement| placement.epoch == epoch);
        if visible_motion {
            // Visibility was checked in content coordinates above.
            frame.continuous(frame.clip);
        }
        bounds
    }
}

/// Move an item to its destination while preserving the order of its neighbors.
pub fn move_reorder_item<T>(items: &mut [T], from: usize, to: usize) -> bool {
    if from == to || from >= items.len() || to >= items.len() {
        return false;
    }
    if from < to {
        items[from..=to].rotate_left(1);
    } else {
        items[to..=from].rotate_right(1);
    }
    true
}

#[cfg(test)]
#[path = "reorder.spec.rs"]
mod tests;