Skip to content

A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.

19.0 KB
Raw Blame
//! The icon grid: icon size steps, the size of a cell at every display scale, cells and their column-major order.
//!
//! WHERE THE NUMBERS COME FROM. Measured on Windows 11 build 26300 at 100 % with the default, medium, size: Explorer's desktop view
//! reports a spacing of 75 across and 99 DOWN for its 48-pixel icons (`IFolderView::GetSpacing`; the icons' own positions step by
//! the same 75 and 99). The system metrics `SM_CXICONSPACING` = `SM_CYICONSPACING` = 75 are NOT the desktop's row height: the view
//! adds room for the label's two lines. The first version of this file took 75 for both, and imported icons stood in rows of 75
//! with a gap after every third.
//!
//! TWO GRIDS THEREFORE. [`metrics`] is the BARE square grid (icon + 27, never under 64: 75 for medium), which a layout file written
//! before the spacing was known is in, and which the pure tests use. [`metrics_spaced`] is the grid of a [`Spacing`]: the one
//! Explorer's view reported when its positions were imported (so changed Windows icon spacing is followed), or
//! [`Spacing::WINDOWS_11`], the measured default, for a layout that starts with no Explorer to ask; the room around the icon is
//! kept for the other icon sizes (Small = 32 and Large = 96, sizes reported from memory: UNVERIFIED, like their spacing). At other
//! scales every length is the 100 % one times the scale, rounded half up.

use std::collections::HashSet;

use serde::{Deserialize, Serialize};

use super::geom::Rect;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub enum IconSize {
    Small,
    Medium,
    Large,
}

impl IconSize {
    pub const ALL: [IconSize; 3] = [IconSize::Small, IconSize::Medium, IconSize::Large];

    /// The icon's edge at 100 %.
    pub const fn px(self) -> u32 {
        match self {
            IconSize::Small => 32,
            IconSize::Medium => 48,
            IconSize::Large => 96,
        }
    }
    /// The step whose size is nearest to `px` (the layout file stores the number; 48 is what Explorer keeps for medium). Halfway
    /// between two steps the larger one.
    pub fn from_px(px: u32) -> IconSize {
        IconSize::ALL.into_iter().min_by_key(|s| ((s.px() as i64 - px as i64).abs(), u32::MAX - s.px())).unwrap_or(IconSize::Medium)
    }
    /// One step bigger / smaller (Ctrl + wheel); the same size at the ends.
    pub fn larger(self) -> IconSize {
        match self {
            IconSize::Small => IconSize::Medium,
            _ => IconSize::Large,
        }
    }
    pub fn smaller(self) -> IconSize {
        match self {
            IconSize::Large => IconSize::Medium,
            _ => IconSize::Small,
        }
    }
    /// The bare cell at 100 %: icon + 27, at least 64, square.
    pub const fn pitch(self) -> u32 {
        let p = self.px() + 27;
        if p < 64 { 64 } else { p }
    }
}

/// The spacing Explorer's desktop view reported when its positions were imported, at 100 %: for icons of `icon` pixels, `x` across
/// and `y` down.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Spacing {
    pub icon: u32,
    pub x: u32,
    pub y: u32,
}

impl Spacing {
    /// What Explorer's desktop view reports on Windows 11 with the default settings (measured, see the head of this file).
    pub const WINDOWS_11: Spacing = Spacing { icon: 48, x: 75, y: 99 };

    /// From the view's numbers, which are in pixels of a display at `scale_percent`. `None` for numbers that cannot be a spacing:
    /// an icon outside 16..=256, a step smaller than the icon or more than 200 past it.
    pub fn from_view(spacing: (i32, i32), icon_px: i32, scale_percent: u32) -> Option<Spacing> {
        let pct = scale_percent.max(50) as i64;
        let at_100 = |v: i32| (v as i64 * 100 + pct / 2).div_euclid(pct);
        let (icon, x, y) = (at_100(icon_px), at_100(spacing.0), at_100(spacing.1));
        let fits = |v: i64| v >= icon && v <= icon + 200;
        ((16..=256).contains(&icon) && fits(x) && fits(y)).then_some(Spacing { icon: icon as u32, x: x as u32, y: y as u32 })
    }
}

/// `v * pct / 100` rounded half up.
pub fn scaled(v: u32, pct: u32) -> u32 {
    ((v as u64 * pct as u64 * 2 + 100) / 200) as u32
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GridMetrics {
    pub icon_px: u32,
    pub pitch_x: u32,
    pub pitch_y: u32,
}

impl GridMetrics {
    /// The gap on each side of the icon inside its cell (the icon is centred horizontally; the label goes below).
    pub fn icon_inset_x(&self) -> u32 {
        self.pitch_x.saturating_sub(self.icon_px) / 2
    }
}

/// The metrics of a size at a display scale in percent (100, 125, 150, 175, 200 ...).
pub fn metrics(size: IconSize, scale_percent: u32) -> GridMetrics {
    let pct = scale_percent.max(50);
    GridMetrics { icon_px: scaled(size.px(), pct), pitch_x: scaled(size.pitch(), pct), pitch_y: scaled(size.pitch(), pct) }
}

/// The metrics of a size at a display scale on the grid of a spacing: the room around the icon is kept for the other icon sizes
/// (never under 64 either way). With `None`, the bare square grid of [`metrics`].
pub fn metrics_spaced(size: IconSize, scale_percent: u32, spacing: Option<Spacing>) -> GridMetrics {
    let Some(sp) = spacing else { return metrics(size, scale_percent) };
    let pct = scale_percent.max(50);
    let across = (size.px() + sp.x.saturating_sub(sp.icon)).max(64);
    let down = (size.px() + sp.y.saturating_sub(sp.icon)).max(64);
    GridMetrics { icon_px: scaled(size.px(), pct), pitch_x: scaled(across, pct), pitch_y: scaled(down, pct) }
}

/// One grid cell. The derived order is column-major (compare the column first), which is the order Windows fills the desktop in: down
/// the first column, then the next column to the right.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Cell {
    pub col: u32,
    pub row: u32,
}

impl Cell {
    pub const fn new(col: u32, row: u32) -> Self {
        Cell { col, row }
    }
}

/// The grid of one monitor.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Grid {
    /// The top-left of cell (0,0), in monitor pixels (the work area's top-left: icons stay clear of a taskbar or dock).
    pub origin: (i32, i32),
    pub metrics: GridMetrics,
    pub cols: u32,
    pub rows: u32,
    work_size: (u32, u32),
}

impl Grid {
    /// `work`: the part of the monitor the icons may use, in the monitor's own coordinates.
    pub fn new(work: Rect, metrics: GridMetrics) -> Grid {
        let (w, h) = (work.width().max(0) as u32, work.height().max(0) as u32);
        let cols = w.checked_div(metrics.pitch_x).unwrap_or(0);
        let mut rows = h.checked_div(metrics.pitch_y).unwrap_or(0);
        // If leftover height below the last full row still fits a single-line icon tile (at least ~80% of pitch_y and icon_px),
        // include that bottom row instead of leaving a large gap above the taskbar or dock.
        if metrics.pitch_y > 0 && cols > 0 {
            let rem = h % metrics.pitch_y;
            let min_row_h = (metrics.pitch_y * 4 / 5).max(metrics.icon_px);
            if rem >= min_row_h {
                rows += 1;
            }
        }
        Grid {
            origin: (work.left, work.top),
            metrics,
            cols,
            rows,
            work_size: (w, h),
        }
    }
    pub fn len(&self) -> usize {
        self.cols as usize * self.rows as usize
    }
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
    pub fn contains(&self, c: Cell) -> bool {
        c.col < self.cols && c.row < self.rows
    }
    /// The cell's rectangle in monitor pixels.
    pub fn cell_rect(&self, c: Cell) -> Rect {
        let (px, py) = (self.metrics.pitch_x as i32, self.metrics.pitch_y as i32);
        Rect::from_size(self.origin.0 + c.col as i32 * px, self.origin.1 + c.row as i32 * py, px as u32, py as u32)
    }
    fn index(&self, c: Cell) -> usize {
        c.col as usize * self.rows as usize + c.row as usize
    }
    fn at_index(&self, i: usize) -> Cell {
        Cell { col: (i / self.rows as usize) as u32, row: (i % self.rows as usize) as u32 }
    }
    /// The first cell in column-major order that is not in `occupied`.
    pub fn first_free(&self, occupied: &HashSet<Cell>) -> Option<Cell> {
        (0..self.len()).map(|i| self.at_index(i)).find(|c| !occupied.contains(c))
    }
    /// The first free cell after `from` in column-major order, wrapping round to the start; `None` when the grid is full.
    pub fn next_free_after(&self, from: Cell, occupied: &HashSet<Cell>) -> Option<Cell> {
        let n = self.len();
        if n == 0 {
            return None;
        }
        let start = if self.contains(from) { self.index(from) } else { n - 1 };
        (1..=n).map(|k| self.at_index((start + k) % n)).find(|c| !occupied.contains(c))
    }
    /// The free cell closest to `target` (distance between cell centres; the earlier in column-major order wins a tie): where a
    /// dropped icon goes when the cell it was dropped on is taken.
    pub fn nearest_free(&self, target: Cell, occupied: &HashSet<Cell>) -> Option<Cell> {
        let d = |c: Cell| {
            let (dc, dr) = (c.col as i64 - target.col as i64, c.row as i64 - target.row as i64);
            (dc * self.metrics.pitch_x as i64).pow(2) + (dr * self.metrics.pitch_y as i64).pow(2)
        };
        (0..self.len()).map(|i| self.at_index(i)).filter(|c| !occupied.contains(c)).min_by_key(|c| (d(*c), *c))
    }
    /// A dragged icon's top-left corner (monitor pixels) to a cell and an offset inside it. Aligned: the nearest cell, no offset.
    /// Free: the cell the corner is in, and how far into it. Outside the grid the nearest edge cell is taken and the offset is 0.
    pub fn snap(&self, x: i32, y: i32, align_to_grid: bool) -> Option<(Cell, i32, i32)> {
        if self.is_empty() {
            return None;
        }
        let (px, py) = (self.metrics.pitch_x as i32, self.metrics.pitch_y as i32);
        let (rx, ry) = (x - self.origin.0, y - self.origin.1);
        let (cx, cy) = if align_to_grid { ((rx + px / 2).div_euclid(px), (ry + py / 2).div_euclid(py)) } else { (rx.div_euclid(px), ry.div_euclid(py)) };
        let col = cx.clamp(0, self.cols as i32 - 1);
        let row = cy.clamp(0, self.rows as i32 - 1);
        let in_work = rx >= 0 && ry >= 0 && rx < self.work_size.0 as i32 && ry < self.work_size.1 as i32;
        let (dx, dy) = if align_to_grid || !in_work { (0, 0) } else { (rx - col * px, ry - row * py) };
        Some((Cell { col: col as u32, row: row as u32 }, dx, dy))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn the_grid_of_a_spacing_keeps_the_room_around_the_icon() {
        // what Explorer reports here: 75 x 99 for 48-pixel icons
        let w = Some(Spacing::WINDOWS_11);
        let m = metrics_spaced(IconSize::Medium, 100, w);
        assert_eq!((m.icon_px, m.pitch_x, m.pitch_y), (48, 75, 99));
        // the other sizes keep the 27 and 51 around the icon; small is held at 64 across
        let s = metrics_spaced(IconSize::Small, 100, w);
        assert_eq!((s.icon_px, s.pitch_x, s.pitch_y), (32, 64, 83));
        let l = metrics_spaced(IconSize::Large, 100, w);
        assert_eq!((l.icon_px, l.pitch_x, l.pitch_y), (96, 123, 147));
        // scaled like everything else
        let m150 = metrics_spaced(IconSize::Medium, 150, w);
        assert_eq!((m150.icon_px, m150.pitch_x, m150.pitch_y), (72, 113, 149));
        // no spacing: the bare square grid
        assert_eq!(metrics_spaced(IconSize::Medium, 100, None), metrics(IconSize::Medium, 100));
        // a user who widened Windows' icon spacing gets that grid
        let wide = Spacing::from_view((100, 120), 48, 100).unwrap();
        let mw = metrics_spaced(IconSize::Medium, 100, Some(wide));
        assert_eq!((mw.pitch_x, mw.pitch_y), (100, 120));
    }

    #[test]
    fn a_views_numbers_become_a_spacing_at_100_percent_or_are_refused() {
        assert_eq!(Spacing::from_view((75, 99), 48, 100), Some(Spacing::WINDOWS_11));
        // a view on a 150 % display reports bigger numbers for the same grid
        assert_eq!(Spacing::from_view((113, 149), 72, 150), Some(Spacing { icon: 48, x: 75, y: 99 }));
        // nonsense is refused: no icon, a step smaller than the icon, a step far past it, negative numbers
        assert_eq!(Spacing::from_view((75, 99), 0, 100), None);
        assert_eq!(Spacing::from_view((40, 99), 48, 100), None);
        assert_eq!(Spacing::from_view((75, 400), 48, 100), None);
        assert_eq!(Spacing::from_view((-75, -99), 48, 100), None);
        assert_eq!(Spacing::from_view((75, 99), 1000, 100), None);
    }

    #[test]
    fn icon_size_steps() {
        assert_eq!(IconSize::ALL.map(|s| s.px()), [32, 48, 96]);
        assert_eq!(IconSize::from_px(48), IconSize::Medium);
        assert_eq!(IconSize::from_px(40), IconSize::Medium);
        assert_eq!(IconSize::from_px(33), IconSize::Small);
        assert_eq!(IconSize::from_px(256), IconSize::Large);
        assert_eq!(IconSize::from_px(0), IconSize::Small);
        assert_eq!(IconSize::Small.larger(), IconSize::Medium);
        assert_eq!(IconSize::Large.larger(), IconSize::Large);
        assert_eq!(IconSize::Large.smaller(), IconSize::Medium);
        assert_eq!(IconSize::Small.smaller(), IconSize::Small);
    }

    #[test]
    fn metrics_at_every_scale() {
        // medium: 48 / 75 at 100 % (the cell size seen on a real machine), then scaled and rounded half up
        let m = |s, p| {
            let g = metrics(s, p);
            (g.icon_px, g.pitch_x, g.pitch_y)
        };
        assert_eq!(m(IconSize::Medium, 100), (48, 75, 75));
        assert_eq!(m(IconSize::Medium, 125), (60, 94, 94)); // 93.75
        assert_eq!(m(IconSize::Medium, 150), (72, 113, 113)); // 112.5 rounds up
        assert_eq!(m(IconSize::Medium, 200), (96, 150, 150));
        assert_eq!(m(IconSize::Small, 100), (32, 64, 64)); // 32 + 27 = 59, raised to the minimum
        assert_eq!(m(IconSize::Small, 125), (40, 80, 80));
        assert_eq!(m(IconSize::Small, 150), (48, 96, 96));
        assert_eq!(m(IconSize::Small, 200), (64, 128, 128));
        assert_eq!(m(IconSize::Large, 100), (96, 123, 123));
        assert_eq!(m(IconSize::Large, 125), (120, 154, 154)); // 153.75
        assert_eq!(m(IconSize::Large, 150), (144, 185, 185)); // 184.5
        assert_eq!(m(IconSize::Large, 200), (192, 246, 246));
        assert_eq!(metrics(IconSize::Medium, 100).icon_inset_x(), 13);
    }

    #[test]
    fn a_grid_fits_whole_cells_into_the_work_area() {
        let work = Rect::new(0, 0, 1920, 1080);
        let dims = |p| {
            let g = Grid::new(work, metrics(IconSize::Medium, p));
            (g.cols, g.rows)
        };
        assert_eq!(dims(100), (25, 14)); // 1920 / 75, 1080 / 75
        assert_eq!(dims(125), (20, 11));
        assert_eq!(dims(150), (16, 9));
        assert_eq!(dims(200), (12, 7));
        // a dock that takes the bottom 100 px: the work area ends at 980
        let g = Grid::new(Rect::new(0, 0, 1920, 980), metrics(IconSize::Medium, 100));
        assert_eq!((g.cols, g.rows), (25, 13));
        // a bar on the left shifts the origin
        let g = Grid::new(Rect::new(60, 0, 1920, 1080), metrics(IconSize::Medium, 100));
        assert_eq!(g.origin, (60, 0));
        assert_eq!(g.cell_rect(Cell::new(1, 2)), Rect::new(135, 150, 210, 225));
        // nothing fits: an empty grid, not a panic
        let g = Grid::new(Rect::new(0, 0, 50, 50), metrics(IconSize::Medium, 100));
        assert!(g.is_empty() && g.first_free(&HashSet::new()).is_none() && g.snap(0, 0, true).is_none());
        assert!(Grid::new(Rect::default(), metrics(IconSize::Medium, 100)).is_empty());
    }

    fn grid() -> Grid {
        // 3 columns x 2 rows of 75
        Grid::new(Rect::new(0, 0, 225, 150), metrics(IconSize::Medium, 100))
    }

    #[test]
    fn the_order_is_down_the_column_then_to_the_right() {
        let g = grid();
        let mut occ = HashSet::new();
        let mut got = Vec::new();
        while let Some(c) = g.first_free(&occ) {
            got.push((c.col, c.row));
            occ.insert(c);
        }
        assert_eq!(got, [(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)]);
        let mut cells = vec![Cell::new(1, 0), Cell::new(0, 1), Cell::new(0, 0)];
        cells.sort();
        assert_eq!(cells, [Cell::new(0, 0), Cell::new(0, 1), Cell::new(1, 0)]);
    }

    #[test]
    fn free_cell_searches() {
        let g = grid();
        let occ: HashSet<Cell> = [Cell::new(0, 0), Cell::new(1, 0)].into_iter().collect();
        assert_eq!(g.first_free(&occ), Some(Cell::new(0, 1)));
        // after (1,0): the next in order is (1,1)
        assert_eq!(g.next_free_after(Cell::new(1, 0), &occ), Some(Cell::new(1, 1)));
        // after the last cell: wraps to the first free one
        assert_eq!(g.next_free_after(Cell::new(2, 1), &occ), Some(Cell::new(0, 1)));
        // a cell outside the grid counts as "after the end"
        assert_eq!(g.next_free_after(Cell::new(9, 9), &occ), Some(Cell::new(0, 1)));
        // a full grid has none
        let full: HashSet<Cell> = (0..3).flat_map(|c| (0..2).map(move |r| Cell::new(c, r))).collect();
        assert_eq!((g.first_free(&full), g.next_free_after(Cell::new(0, 0), &full), g.nearest_free(Cell::new(0, 0), &full)), (None, None, None));
        // nearest: target (1,0) is taken, (1,1) and (0,0)... (0,0) is taken too; (1,1) is one cell away, (2,0) is one cell away: the earlier wins
        let occ2: HashSet<Cell> = [Cell::new(0, 0), Cell::new(1, 0)].into_iter().collect();
        assert_eq!(g.nearest_free(Cell::new(1, 0), &occ2), Some(Cell::new(1, 1)));
        assert_eq!(g.nearest_free(Cell::new(0, 0), &occ2), Some(Cell::new(0, 1)));
    }

    #[test]
    fn snapping_a_dropped_icon() {
        let g = grid();
        // aligned: the corner at (80, 10) is nearest to cell (1, 0)
        assert_eq!(g.snap(80, 10, true), Some((Cell::new(1, 0), 0, 0)));
        assert_eq!(g.snap(36, 36, true), Some((Cell::new(0, 0), 0, 0)));
        assert_eq!(g.snap(38, 38, true), Some((Cell::new(1, 1), 0, 0)));
        // free: the cell the corner is in, and the offset
        assert_eq!(g.snap(80, 10, false), Some((Cell::new(1, 0), 5, 10)));
        // off the grid on any side: the edge cell, no offset
        assert_eq!(g.snap(-500, -500, false), Some((Cell::new(0, 0), 0, 0)));
        assert_eq!(g.snap(5000, 5000, true), Some((Cell::new(2, 1), 0, 0)));
        assert_eq!(g.snap(5000, 10, false), Some((Cell::new(2, 0), 0, 0)));
    }

    #[test]
    fn bottom_row_uses_leftover_work_area_that_fits_a_tile_and_free_drag_reaches_bottom() {
        // 1920x1027 work area (e.g. 53 px dock) with Small icons and {46, 76, 88} spacing -> pitch_y = 74, leftover = 65 px.
        let m = metrics_spaced(IconSize::Small, 100, Some(Spacing { icon: 46, x: 76, y: 88 }));
        assert_eq!(m.pitch_y, 74);
        let g = Grid::new(Rect::new(0, 0, 1920, 1027), m);
        assert_eq!(g.rows, 14, "leftover 65 px fits a 1-line small icon tile above the dock");

        // Free placement in the bottom margin inside the work area preserves the offset rather than snapping back to 0.
        let g_free = Grid::new(Rect::new(0, 0, 225, 175), metrics(IconSize::Medium, 100));
        assert_eq!(g_free.rows, 2);
        assert_eq!(g_free.snap(10, 160, false), Some((Cell::new(0, 1), 10, 85)));
    }
}