Grid view with virtualized lines of cells laid out by the folder model, a segmented details/grid switch with a cross-fade, list motion for inserts, removals and reorders driven by a transition clock, and a slide in the direction of travel when a folder opens. Reduced motion makes all of it instant. Co-Authored-By: Claude Opus 5.5 <[email protected]>
340 lines
10 KiB
Rust
340 lines
10 KiB
Rust
//! List motion. When the rows of the open folder change in place (sort, filter, hidden files,
|
|
//! watch events), rows that stay slide from where they were drawn, new rows grow in, and
|
|
//! removed rows stay as leaving copies until the change settles. The view draws it from the
|
|
//! per-row fields of `DetailRow` and one clock (`MotionClock` in `motion.slint`).
|
|
//!
|
|
//! Positions are in view units: rows for the details view; cells, cell lines and header
|
|
//! lines for the grid.
|
|
|
|
use std::collections::{HashMap, HashSet};
|
|
use std::ops::{Add, Mul, Sub};
|
|
use std::time::{Duration, Instant};
|
|
|
|
use crate::{DetailRow, GridRow, ViewMode};
|
|
|
|
/// Above this many rows (before or after) a change is instant.
|
|
pub const LIMIT: usize = 2000;
|
|
|
|
/// Identity of a row across rebuilds.
|
|
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
|
|
pub enum Key {
|
|
Header(&'static str),
|
|
Item(Vec<u8>),
|
|
}
|
|
|
|
#[derive(Clone, Copy, Default, PartialEq, Debug)]
|
|
pub struct Pos {
|
|
pub x: f32,
|
|
/// Rows (details) or cell lines (grid).
|
|
pub y: f32,
|
|
/// Header lines (grid only; details headers are rows).
|
|
pub h: f32,
|
|
}
|
|
|
|
impl Add for Pos {
|
|
type Output = Pos;
|
|
fn add(self, o: Pos) -> Pos {
|
|
Pos {
|
|
x: self.x + o.x,
|
|
y: self.y + o.y,
|
|
h: self.h + o.h,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Sub for Pos {
|
|
type Output = Pos;
|
|
fn sub(self, o: Pos) -> Pos {
|
|
Pos {
|
|
x: self.x - o.x,
|
|
y: self.y - o.y,
|
|
h: self.h - o.h,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Mul<f32> for Pos {
|
|
type Output = Pos;
|
|
fn mul(self, k: f32) -> Pos {
|
|
Pos {
|
|
x: self.x * k,
|
|
y: self.y * k,
|
|
h: self.h * k,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// How one row moves: from `from` to `to`, both relative to its slot.
|
|
#[derive(Clone, Copy, Default, Debug)]
|
|
pub struct Anim {
|
|
pub from: Pos,
|
|
pub to: Pos,
|
|
pub enter: bool,
|
|
pub leave: bool,
|
|
}
|
|
|
|
impl Anim {
|
|
pub fn fill(self, row: &mut DetailRow) {
|
|
row.from_x = self.from.x;
|
|
row.from_y = self.from.y;
|
|
row.from_h = self.from.h;
|
|
row.to_x = self.to.x;
|
|
row.to_y = self.to.y;
|
|
row.to_h = self.to.h;
|
|
row.enter = self.enter;
|
|
row.leave = self.leave;
|
|
}
|
|
}
|
|
|
|
/// Same curve as `Motion.settle` in `motion.slint`.
|
|
pub fn settle(t: f32) -> f32 {
|
|
1.0 - (1.0 - t.clamp(0.0, 1.0)).powi(3)
|
|
}
|
|
|
|
/// A running change.
|
|
#[derive(Clone, Copy)]
|
|
pub struct Clock {
|
|
pub start: Instant,
|
|
pub duration: Duration,
|
|
}
|
|
|
|
impl Clock {
|
|
pub fn settled(&self) -> f32 {
|
|
settle(self.start.elapsed().as_secs_f32() / self.duration.as_secs_f32())
|
|
}
|
|
}
|
|
|
|
/// The grid's lines for rows whose header flags are `headers`, and each row's slot. With
|
|
/// `skip`, rows marked there take no slot: they sit at the slot of the row after them (the
|
|
/// layout once they are gone).
|
|
pub fn grid(headers: &[bool], skip: &[bool], columns: usize) -> (Vec<GridRow>, Vec<Pos>) {
|
|
let columns = columns.max(1);
|
|
let mut lines: Vec<GridRow> = Vec::new();
|
|
let mut slots = vec![Pos::default(); headers.len()];
|
|
let (mut y, mut h, mut x) = (0.0, 0.0, 0usize);
|
|
// Rows waiting for the next slot (skipped ones).
|
|
let mut pending: Vec<usize> = Vec::new();
|
|
let mut open = false;
|
|
for (r, &header) in headers.iter().enumerate() {
|
|
if skip.get(r).copied().unwrap_or(false) {
|
|
pending.push(r);
|
|
continue;
|
|
}
|
|
if header {
|
|
if open {
|
|
y += 1.0;
|
|
open = false;
|
|
}
|
|
let at = Pos { x: 0.0, y, h };
|
|
for p in pending.drain(..) {
|
|
slots[p] = at;
|
|
}
|
|
slots[r] = at;
|
|
h += 1.0;
|
|
x = 0;
|
|
continue;
|
|
}
|
|
if x == columns {
|
|
y += 1.0;
|
|
x = 0;
|
|
}
|
|
let at = Pos { x: x as f32, y, h };
|
|
for p in pending.drain(..) {
|
|
slots[p] = at;
|
|
}
|
|
slots[r] = at;
|
|
x += 1;
|
|
open = true;
|
|
}
|
|
let end = Pos { x: x as f32, y, h };
|
|
for p in pending {
|
|
slots[p] = end;
|
|
}
|
|
// Lines cover every row, skipped or not, in order, so the view can index them.
|
|
let mut x = 0usize;
|
|
let mut headers_before = 0;
|
|
for (r, &header) in headers.iter().enumerate() {
|
|
if header {
|
|
lines.push(GridRow {
|
|
header: true,
|
|
start: r as i32,
|
|
count: 1,
|
|
headers_before,
|
|
});
|
|
headers_before += 1;
|
|
x = 0;
|
|
continue;
|
|
}
|
|
match lines.last_mut() {
|
|
Some(line) if !line.header && x < columns => line.count += 1,
|
|
_ => {
|
|
lines.push(GridRow {
|
|
header: false,
|
|
start: r as i32,
|
|
count: 1,
|
|
headers_before,
|
|
});
|
|
x = 0;
|
|
}
|
|
}
|
|
x += 1;
|
|
}
|
|
(lines, slots)
|
|
}
|
|
|
|
/// Where each row is drawn at progress `s` (settled 0..1) of `anims`.
|
|
pub fn drawn(mode: ViewMode, headers: &[bool], anims: &[Anim], columns: usize, s: f32) -> Vec<Pos> {
|
|
let anim = |r: usize| anims.get(r).copied().unwrap_or_default();
|
|
match mode {
|
|
ViewMode::Details => {
|
|
let mut y = 0.0;
|
|
(0..headers.len())
|
|
.map(|r| {
|
|
let a = anim(r);
|
|
let pos = Pos {
|
|
y: y + a.from.y * (1.0 - s),
|
|
..Pos::default()
|
|
};
|
|
y += if a.enter {
|
|
s
|
|
} else if a.leave {
|
|
1.0 - s
|
|
} else {
|
|
1.0
|
|
};
|
|
pos
|
|
})
|
|
.collect()
|
|
}
|
|
ViewMode::Grid => {
|
|
let (_, slots) = grid(headers, &[], columns);
|
|
slots
|
|
.iter()
|
|
.enumerate()
|
|
.map(|(r, &at)| {
|
|
let a = anim(r);
|
|
at + a.to + (a.from - a.to) * (1.0 - s)
|
|
})
|
|
.collect()
|
|
}
|
|
}
|
|
}
|
|
|
|
/// One row before the change: identity, look (for a leaving copy) and where it was drawn.
|
|
pub struct Old {
|
|
pub key: Key,
|
|
pub row: DetailRow,
|
|
pub drawn: Pos,
|
|
/// Already collapsing out of an interrupted change: it goes now instead of collapsing
|
|
/// again, and if it comes back it grows in like a new row.
|
|
pub leaving: bool,
|
|
}
|
|
|
|
/// Rows of the new list in order: `Ok(new index)` or `Err(old index)` for a leaving copy.
|
|
pub type Merged = Vec<Result<usize, usize>>;
|
|
|
|
/// Puts each removed old row back right after the old row before it that stays (or first).
|
|
pub fn merge(old: &[Old], new: &[Key]) -> Merged {
|
|
let at: HashMap<&Key, usize> = new.iter().enumerate().map(|(i, k)| (k, i)).collect();
|
|
// Leaving rows grouped by the new row they follow (None: before the first).
|
|
let mut after: HashMap<Option<usize>, Vec<usize>> = HashMap::new();
|
|
let mut anchor = None;
|
|
for (i, o) in old.iter().enumerate() {
|
|
match at.get(&o.key) {
|
|
Some(&n) => anchor = Some(n),
|
|
None if !o.leaving => after.entry(anchor).or_default().push(i),
|
|
None => {}
|
|
}
|
|
}
|
|
let mut merged = Vec::with_capacity(new.len() + old.len());
|
|
merged.extend(after.remove(&None).unwrap_or_default().into_iter().map(Err));
|
|
for n in 0..new.len() {
|
|
merged.push(Ok(n));
|
|
if let Some(leaving) = after.remove(&Some(n)) {
|
|
merged.extend(leaving.into_iter().map(Err));
|
|
}
|
|
}
|
|
merged
|
|
}
|
|
|
|
/// The animation of every merged row, given the new rows' keys and header flags. `None` when
|
|
/// nothing would move.
|
|
pub fn plan(
|
|
mode: ViewMode,
|
|
old: &[Old],
|
|
new: &[Key],
|
|
merged: &Merged,
|
|
headers: &[bool],
|
|
columns: usize,
|
|
) -> Option<Vec<Anim>> {
|
|
let was: HashMap<&Key, Pos> = old
|
|
.iter()
|
|
.filter(|o| !o.leaving)
|
|
.map(|o| (&o.key, o.drawn))
|
|
.collect();
|
|
let fresh: HashSet<usize> = (0..new.len())
|
|
.filter(|&n| !was.contains_key(&new[n]))
|
|
.collect();
|
|
let leave: Vec<bool> = merged.iter().map(Result::is_err).collect();
|
|
let mut anims = vec![Anim::default(); merged.len()];
|
|
match mode {
|
|
ViewMode::Details => {
|
|
// At the start entering rows have no height and leaving rows full height.
|
|
let mut y = 0.0;
|
|
for (r, m) in merged.iter().enumerate() {
|
|
let (enter, from) = match *m {
|
|
Ok(n) if fresh.contains(&n) => (true, 0.0),
|
|
Ok(n) => (false, was[&new[n]].y - y),
|
|
Err(o) => (false, old[o].drawn.y - y),
|
|
};
|
|
anims[r] = Anim {
|
|
from: Pos {
|
|
y: from,
|
|
..Pos::default()
|
|
},
|
|
enter,
|
|
leave: leave[r],
|
|
..Anim::default()
|
|
};
|
|
if !enter {
|
|
y += 1.0;
|
|
}
|
|
}
|
|
}
|
|
ViewMode::Grid => {
|
|
let (_, slots) = grid(headers, &[], columns);
|
|
let (_, target) = grid(headers, &leave, columns);
|
|
for (r, m) in merged.iter().enumerate() {
|
|
let to = target[r] - slots[r];
|
|
anims[r] = match *m {
|
|
// New cells appear where they end up.
|
|
Ok(n) if fresh.contains(&n) => Anim {
|
|
from: to,
|
|
to,
|
|
enter: true,
|
|
..Anim::default()
|
|
},
|
|
Ok(n) => Anim {
|
|
from: was[&new[n]] - slots[r],
|
|
to,
|
|
..Anim::default()
|
|
},
|
|
// Leaving cells fade where they were.
|
|
Err(o) => {
|
|
let from = old[o].drawn - slots[r];
|
|
Anim {
|
|
from,
|
|
to: from,
|
|
leave: true,
|
|
..Anim::default()
|
|
}
|
|
}
|
|
};
|
|
}
|
|
}
|
|
}
|
|
let still = anims
|
|
.iter()
|
|
.all(|a| !a.enter && !a.leave && a.from == Pos::default() && a.to == Pos::default());
|
|
(!still).then_some(anims)
|
|
}
|