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.
Rust archive-managerdesktop-appdual-paneexplorer-alternativefile-browserfile-explorerfile-managerfluent-designgpu-acceleratedgpuimacosmicaproductivityquick-lookrustrust-guitabswindowswindows-11windows-explorer
Dock previews: steady order, x at the top right, no accidental swap
gelenmedya5b9a4149f58c
diff --git a/crates/rf-app/src/dock/hover_preview.rs b/crates/rf-app/src/dock/hover_preview.rsindex 1efef91a..fc99bd56 100644--- a/crates/rf-app/src/dock/hover_preview.rs+++ b/crates/rf-app/src/dock/hover_preview.rs@@ -5,7 +5,7 @@//!//! THE TIMING is `rf_win::dock::preview::Machine` (pure, tested): this file feeds it the icons' hover events and the preview's own, carries//! out what it answers, and sleeps until its next deadline (no polling for the timing). Moving from the icon into the preview keeps it-//! open, moving to a neighbouring running icon swaps it at once, leaving both closes it after 300 ms, a click on an icon closes it, a+//! open, resting on a neighbouring running icon swaps it (crossing one on the way to a card does not), leaving both closes it after 300 ms, a click on an icon closes it, a//! right click replaces it with the menu (the menu blocks the machine while it is open), and it closes when the dock hides.//!//! IT IS NEVER ACTIVATED AND NEVER TAKES THE KEYBOARD. The dock's panels are activated (they need the keys); this is not a panel. Its@@ -92,6 +92,8 @@ struct PreviewHost {/// Bumped at every show and hide: a task of an earlier state sees that and does nothing.generation: u64,basis: Option<(f32, Rect, i32)>,+ /// Each app's windows in the order they were first seen (`preview::keep_order`): the cards keep their places.+ orders: std::collections::HashMap<String, Vec<isize>>,}impl Global for PreviewHost {}@@ -110,6 +112,7 @@ fn host_g(cx: &mut App) -> &mut PreviewHost {shown: false,generation: 0,basis: None,+ orders: std::collections::HashMap::new(),});}cx.global_mut::<PreviewHost>()@@ -127,8 +130,15 @@ pub fn icon_hover(info: Option<IconInfo>, cx: &mut App) {let input = match info {Some(i) => {let key = i.app.key.clone();- let refs = winops::refs(&i.app.windows);+ let mut refs = winops::refs(&i.app.windows);let host = host_g(cx);+ // (the app's list follows which of its windows was in front last; the cards do not)+ let now: Vec<isize> = refs.iter().map(|r| r.hwnd).collect();+ let order = preview::keep_order(host.orders.entry(key.clone()).or_default(), &now);+ refs.sort_by_key(|r| order.iter().position(|h| *h == r.hwnd).unwrap_or(usize::MAX));+ if refs.is_empty() {+ host.orders.remove(&key);+ }host.refs = refs;host.info = Some(i);Input::Icon(Some(key))@@ -337,8 +347,10 @@ fn show_now(key: String, started: Instant, cx: &mut App) {cx.spawn(async move |cx| {rf_win::window::set_dark_mode(hwnd, dark);geo::show_at(hwnd, rect);+ // (the pointer inside the window, logical px: it may be resting on a card already, and then no hover event comes)+ let pointer = geo::cursor().map(|(x, y)| ((x - rect.left) as f32 / scale, (y - rect.top) as f32 / scale));cx.update(|cx| {- let live = handle.update(cx, |v, _, cx| v.on_shown(dark, cx)).unwrap_or(0);+ let live = handle.update(cx, |v, _, cx| v.on_shown(dark, pointer, cx)).unwrap_or(0);log::info!("dock: hover preview shown in {:?} at x={anchor} ({count} cards, {live} live pictures, shapes asked in {probe_took:?}, {unknown} of unknown shape{}{})",started.elapsed(),@@ -516,7 +528,13 @@ impl PreviewView {/// The window is on the screen: the glass goes on, then the live pictures (after the glass, so that nothing of the engine can be/// above them). Returns how many cards have a live picture.- fn on_shown(&mut self, dark: bool, cx: &mut Context<Self>) -> usize {+ fn on_shown(&mut self, dark: bool, pointer: Option<(f32, f32)>, cx: &mut Context<Self>) -> usize {+ // The card under a pointer that did not move: after a window was closed with the x the cards are laid out anew under it, and+ // the toolkit reports a card's hover only when the pointer comes onto it (the next card stayed unmarked until the pointer+ // left and came back).+ if self.shown {+ self.hover = pointer.and_then(|p| self.layout.card_at(p)).filter(|i| *i < self.cards.len());+ }if self.popup.is_none() && self.shown {self.popup = Some(PopupGlass::start_dock_panel_thin_rim(self.hwnd, dark, rim_cap()));self._watch = Some(glass_watch::watch(cx, |this: &mut Self| this.popup.as_mut()));@@ -641,8 +659,8 @@ impl Render for PreviewView {.items_center().justify_center().when(!live, |d| d.bg(icon_tile).when_some(self.icon.clone(), |d, img| d.child(gpui_kit::img(img).size(px(48.)))));- // the title row: the small icon, the title, and the x on the card under the pointer (not over the picture: DWM's picture is- // drawn over everything of ours that lies on it)+ // the title row, above the picture: the small icon, the title, and the x at the card's top right corner, on every card and+ // all the time (not over the picture: DWM's picture is drawn over everything of ours that lies on it)let row = div().absolute().left(px(b.title_in_card[0]))@@ -655,27 +673,25 @@ impl Render for PreviewView {.gap(px(6.)).child(div().size(px(16.)).flex_shrink_0().when_some(self.icon_small.clone(), |d, img| d.child(gpui_kit::img(img).size(px(16.))))).child(div().flex_1().min_w_0().truncate().child(title))- .when(hot, |d| {- d.child(- div()- .id(("pv-close", i))- .size(px(preview::CLOSE_SIZE))- .flex_shrink_0()- .flex()- .items_center()- .justify_center()- .rounded_full()- .cursor_pointer()- .hover(move |s| s.bg(card_hot))- .child(Icon::new(L::X).size(px(12.)).text_color(secondary))- // the press is the x's: the card under it took the click as its own, brought the window forward and closed- // the preview, and the x never got it; the window closed only at a second try (issue 132)- .on_mouse_down(MouseButton::Left, cx.listener(move |_, _, _, cx| {- cx.stop_propagation();- close_window(win);- })),- )- });+ .child(+ div()+ .id(("pv-close", i))+ .size(px(preview::CLOSE_SIZE))+ .flex_shrink_0()+ .flex()+ .items_center()+ .justify_center()+ .rounded_full()+ .cursor_pointer()+ .hover(move |s| s.bg(card_hot))+ .child(Icon::new(L::X).size(px(12.)).text_color(if hot { text } else { secondary }))+ // the press is the x's: the card under it took the click as its own, brought the window forward and closed+ // the preview, and the x never got it; the window closed only at a second try (issue 132)+ .on_mouse_down(MouseButton::Left, cx.listener(move |_, _, _, cx| {+ cx.stop_propagation();+ close_window(win);+ })),+ );root = root.child(div().id(("pv-card", i))diff --git a/crates/rf-win/src/dock/preview.rs b/crates/rf-win/src/dock/preview.rsindex 407ea146..32d505ed 100644--- a/crates/rf-win/src/dock/preview.rs+++ b/crates/rf-win/src/dock/preview.rs@@ -5,7 +5,9 @@//!//! THE RULES (what a user sees)://! * the pointer rests on a RUNNING app's icon for [`DWELL_MS`] (80 ms: at once to the eye, so that two windows of an app are seen the moment the pointer is on it; it was 400 ms until a user asked; a pointer that only crosses the icon on its way still opens nothing): the preview opens;-//! * while it is open, moving the pointer onto another running app's icon swaps the preview at once (no waiting);+//! * while it is open, RESTING the pointer on another running app's icon for [`SWAP_MS`] swaps the preview. It was at once, and a+//! preview of several windows is wider than its icon: on the way from the icon to a card at the side the pointer crosses the+//! neighbouring icons, and the preview turned into a neighbour's under it. A pointer that only crosses an icon swaps nothing;//! * moving from the icon into the preview keeps it open; leaving both (onto nothing, onto a gap, onto an icon that is not running)//! closes it after [`GRACE_MS`], and coming back to the icon or the preview inside that time cancels the closing;//! * a click on an icon closes it and it does not come back until the pointer has left that icon (clicking is an answer, not a@@ -20,6 +22,9 @@ use super::layout::{Rect, px};pub const DWELL_MS: u64 = 80;/// How long the preview stays after the pointer has left the icon and the preview.pub const GRACE_MS: u64 = 300;+/// How long the pointer must rest on ANOTHER running app's icon before the open preview becomes that app's. Longer than a pointer+/// needs to cross an icon on its way up to a card, shorter than the grace (the open preview must not close first).+pub const SWAP_MS: u64 = 200;/// What the pointer is doing, as the caller sees it. `K` names an app (its dock key).#[derive(Clone, Debug, PartialEq, Eq)]@@ -41,7 +46,7 @@ pub enum Output<K> {Nothing,/// Open the preview of this app.Open(K),- /// The preview is open: show this app's instead, without any delay.+ /// The preview is open: show this app's instead.Swap(K),Close,}@@ -58,11 +63,13 @@ pub struct Machine<K> {blocked: bool,/// After a click: no preview for this app until the pointer has left its icon.suppressed: Option<K>,+ /// While a preview is open: another running app's icon the pointer is on, and since when (ms).+ swap: Option<(K, u64)>,}impl<K> Default for Machine<K> {fn default() -> Self {- Machine { shown: None, dwell: None, on_icon: None, in_preview: false, away_since: None, blocked: false, suppressed: None }+ Machine { shown: None, dwell: None, on_icon: None, in_preview: false, away_since: None, blocked: false, suppressed: None, swap: None }}}@@ -84,6 +91,7 @@ impl<K: Clone + PartialEq> Machine<K> {self.dwell = None;self.away_since = None;self.in_preview = false;+ self.swap = None;if self.shown.take().is_some() { Output::Close } else { Output::Nothing }}@@ -110,6 +118,8 @@ impl<K: Clone + PartialEq> Machine<K> {if self.shown.is_some() {if inside {self.away_since = None;+ // (the pointer reached the preview: the icon it crossed on the way was not asked for)+ self.swap = None;} else if self.on_icon != self.shown && self.away_since.is_none() {self.away_since = Some(now_ms);}@@ -130,16 +140,18 @@ impl<K: Clone + PartialEq> Machine<K> {self.away_since = None;self.dwell = None;if k == shown {- Output::Nothing+ self.swap = None;} else if self.suppressed.as_ref() == Some(&k) {// (a click on this very icon a moment ago: leave the other preview as it is, it closes with its own grace)- Output::Nothing- } else {- self.shown = Some(k.clone());- Output::Swap(k)+ self.swap = None;+ } else if self.swap.as_ref().map(|(s, _)| s) != Some(&k) {+ // (the open preview stays until the pointer has rested here: see `SWAP_MS`)+ self.swap = Some((k, now_ms));}+ Output::Nothing}(Some(_), None) => {+ self.swap = None;if !self.in_preview && self.away_since.is_none() {self.away_since = Some(now_ms);}@@ -173,6 +185,16 @@ impl<K: Clone + PartialEq> Machine<K> {self.shown = Some(k.clone());return Output::Open(k);}+ if let Some((k, since)) = self.swap.clone()+ && !self.blocked+ && self.shown.is_some()+ && now_ms.saturating_sub(since) >= SWAP_MS+ {+ self.swap = None;+ self.away_since = None;+ self.shown = Some(k.clone());+ return Output::Swap(k);+ }if let Some(since) = self.away_since&& now_ms.saturating_sub(since) >= GRACE_MS{@@ -185,10 +207,8 @@ impl<K: Clone + PartialEq> Machine<K> {pub fn deadline(&self) -> Option<u64> {let dwell = self.dwell.as_ref().filter(|_| !self.blocked).map(|(_, since)| since + DWELL_MS);let away = self.away_since.map(|since| since + GRACE_MS);- match (dwell, away) {- (Some(a), Some(b)) => Some(a.min(b)),- (a, b) => a.or(b),- }+ let swap = self.swap.as_ref().filter(|_| !self.blocked).map(|(_, since)| since + SWAP_MS);+ [dwell, away, swap].into_iter().flatten().min()}}@@ -257,7 +277,8 @@ pub fn pointer_away(cursor: (i32, i32), preview: Rect, glass: Rect, scale: f32,// (`rf-app/src/dock/hover_preview.rs`) and DWM is told where to draw the live picture from the very same box (`picture`, through// [`to_device`]): there is no second formula that could drift from the first.//-// A card is: CARD_PAD, the picture box (card_w() - 2 CARD_PAD wide, as tall as the row's tallest window needs), the title row, CARD_PAD.+// A card is: CARD_PAD, the title row, the picture box (card_w() - 2 CARD_PAD wide, as tall as the row's tallest window needs), CARD_PAD.+// The title row is on top, as in Windows' own previews, so that the close button is at the card's top right corner.// The box is DYNAMIC IN HEIGHT: it follows the shape of the window it shows ([`pic_height`]), the cards of one row share the row's// tallest box, and the window follows the rows.@@ -295,8 +316,8 @@ pub const UNKNOWN_PIC_RATIO: f32 = 0.625;/// Most cards in a row, and most rows (more windows than that are not shown).pub const MAX_COLS: usize = 6;pub const MAX_ROWS: usize = 3;-/// The close button on a card (it sits at the right end of the title row, under the picture: DWM's picture is drawn over anything of-/// ours that lies on it): its side.+/// The close button on a card (it sits at the right end of the title row, above the picture, and not on the picture: DWM's picture+/// is drawn over anything of ours that lies on it): its side.pub const CLOSE_SIZE: f32 = 22.0;/// The picture box's height, logical px, for a window of this size (`None`: not known yet, 16:10): the box's width times the window's@@ -342,7 +363,7 @@ pub struct CardBox {pub card: [f32; 4],/// The picture box: the toolkit paints its backdrop here and DWM draws the live window in it.pub picture: [f32; 4],- /// The title row under the box (the small icon, the title, the close button).+ /// The title row above the box (the small icon, the title, the close button).pub title: [f32; 4],/// `picture` and `title` again, relative to the card's own top left corner: the toolkit places them as children of the card, so its/// drawing takes these and subtracts nothing of its own (the numbers cannot drift from `picture`, which DWM is given).@@ -429,12 +450,35 @@ impl Layout {let pic_h = self.row_pic_height(row);CardBox {card: [x, y, card_w(), card_height(pic_h)],- picture: [x + CARD_PAD, y + CARD_PAD, pic_w(), pic_h],- title: [x + CARD_PAD, y + CARD_PAD + pic_h, pic_w(), TITLE_H],- picture_in_card: [CARD_PAD, CARD_PAD, pic_w(), pic_h],- title_in_card: [CARD_PAD, CARD_PAD + pic_h, pic_w(), TITLE_H],+ picture: [x + CARD_PAD, y + CARD_PAD + TITLE_H, pic_w(), pic_h],+ title: [x + CARD_PAD, y + CARD_PAD, pic_w(), TITLE_H],+ picture_in_card: [CARD_PAD, CARD_PAD + TITLE_H, pic_w(), pic_h],+ title_in_card: [CARD_PAD, CARD_PAD, pic_w(), TITLE_H],}}++ /// The card that holds the point (logical px in the preview window), if any. The toolkit says which card the pointer is on only+ /// when it comes onto one; after the cards were laid out anew under a pointer that did not move (a window was closed with the x),+ /// this says it.+ pub fn card_at(&self, point: (f32, f32)) -> Option<usize> {+ (0..self.grid.shown).find(|&i| {+ let [x, y, w, h] = self.card(i).card;+ point.0 >= x && point.0 < x + w && point.1 >= y && point.1 < y + h+ })+ }+}++/// An app's windows in the order they were first seen, whatever order they are listed in now (the list follows which window was in+/// front last, so the cards changed places every time another window of the app was used). `remembered` is the app's order so far:+/// windows that are gone leave it, new ones join at its end in the order given. Returns the windows of `now` in that order.+pub fn keep_order(remembered: &mut Vec<isize>, now: &[isize]) -> Vec<isize> {+ remembered.retain(|h| now.contains(h));+ for h in now {+ if !remembered.contains(h) {+ remembered.push(*h);+ }+ }+ remembered.clone()}/// A logical rectangle in device px (for DWM, which takes the destination in pixels).@@ -492,15 +536,63 @@ mod tests {}#[test]- fn a_neighbouring_running_icon_swaps_the_open_preview_at_once() {+ fn resting_on_a_neighbouring_running_icon_swaps_the_open_preview() {let mut m = M::new();on(&mut m, 0, "a");m.tick(400);- assert_eq!(on(&mut m, 450, "b"), Output::Swap("b"), "no delay");+ assert_eq!(on(&mut m, 450, "b"), Output::Nothing, "not at once");+ assert_eq!(m.shown(), Some(&"a"), "a's preview stays meanwhile");+ assert_eq!(m.deadline(), Some(450 + SWAP_MS));+ assert_eq!(m.tick(450 + SWAP_MS - 1), Output::Nothing);+ assert_eq!(m.tick(450 + SWAP_MS), Output::Swap("b"));assert_eq!(m.shown(), Some(&"b"));assert_eq!(m.deadline(), None);- // and back- assert_eq!(on(&mut m, 460, "a"), Output::Swap("a"));+ // and back, the same way+ assert_eq!(on(&mut m, 1_000, "a"), Output::Nothing);+ assert_eq!(m.tick(1_000 + SWAP_MS), Output::Swap("a"));+ // a jitter on the same icon does not start the wait again+ on(&mut m, 2_000, "b");+ on(&mut m, 2_000 + SWAP_MS - 10, "b");+ assert_eq!(m.tick(2_000 + SWAP_MS), Output::Swap("b"));+ }++ #[test]+ fn crossing_neighbouring_icons_on_the_way_to_a_card_at_the_side_keeps_the_preview() {+ // a's preview has three cards and is wider than a's icon: the pointer goes up and to the right, over b's and c's icons+ let mut m = M::new();+ on(&mut m, 0, "a");+ m.tick(400);+ assert_eq!(on(&mut m, 500, "b"), Output::Nothing);+ assert_eq!(on(&mut m, 560, "c"), Output::Nothing);+ assert_eq!(off(&mut m, 620), Output::Nothing, "above the icons, not yet in the preview");+ assert_eq!(m.input(660, Input::Preview(true)), Output::Nothing);+ assert_eq!(m.shown(), Some(&"a"));+ assert_eq!(m.deadline(), None, "nothing waits: no swap, no closing");+ assert_eq!(m.tick(5_000), Output::Nothing);+ assert_eq!(m.shown(), Some(&"a"));+ // straight from b's icon into the preview, with no gap between: the same+ let mut m = M::new();+ on(&mut m, 0, "a");+ m.tick(400);+ on(&mut m, 500, "b");+ m.input(550, Input::Preview(true));+ assert_eq!(m.tick(500 + SWAP_MS), Output::Nothing);+ assert_eq!(m.shown(), Some(&"a"));+ }++ #[test]+ fn going_back_to_the_previews_own_icon_or_clicking_ends_a_waiting_swap() {+ let mut m = M::new();+ on(&mut m, 0, "a");+ m.tick(400);+ on(&mut m, 500, "b");+ on(&mut m, 550, "a");+ assert_eq!(m.tick(500 + SWAP_MS), Output::Nothing);+ assert_eq!(m.shown(), Some(&"a"));+ on(&mut m, 1_000, "b");+ assert_eq!(m.input(1_050, Input::Click), Output::Close);+ assert_eq!(m.tick(1_000 + SWAP_MS), Output::Nothing, "a click is an answer: nothing opens under it");+ assert_eq!(m.shown(), None);}#[test]@@ -511,8 +603,9 @@ mod tests {// the pointer is on nothing for 40 ms between the icons, then on bassert_eq!(off(&mut m, 500), Output::Nothing);assert_eq!(m.deadline(), Some(500 + GRACE_MS));- assert_eq!(on(&mut m, 540, "b"), Output::Swap("b"));- assert_eq!(m.deadline(), None, "the grace was cancelled by the swap");+ assert_eq!(on(&mut m, 540, "b"), Output::Nothing);+ assert_eq!(m.deadline(), Some(540 + SWAP_MS), "the grace was cancelled by the icon; the swap waits for the pointer to rest");+ assert_eq!(m.tick(540 + SWAP_MS), Output::Swap("b"));assert_eq!(m.tick(5_000), Output::Nothing);}@@ -886,7 +979,8 @@ mod tests {let c = layout(&[SRC_169], 1_900.0).card(0);assert_eq!(c.card[0], OUTER_PAD);assert_eq!(c.picture[0] - c.card[0], CARD_PAD);- assert_eq!(c.picture[1] - c.card[1], CARD_PAD);+ assert_eq!(c.title[1] - c.card[1], CARD_PAD, "the title row is the card's top");+ assert_eq!(c.picture[1] - c.card[1], CARD_PAD + TITLE_H);assert_eq!(c.card[0] + c.card[2] - (c.picture[0] + c.picture[2]), CARD_PAD, "the same on the right");}@@ -907,7 +1001,7 @@ mod tests {let c = l.card(i);let inside = |p: [f32; 4]| p[0] >= c.card[0] && p[1] >= c.card[1] && p[0] + p[2] <= c.card[0] + c.card[2] && p[1] + p[3] <= c.card[1] + c.card[3];assert!(inside(c.picture) && inside(c.title), "card {i}");- assert!(c.title[1] >= c.picture[1] + c.picture[3], "the title is under the box");+ assert!(c.title[1] + c.title[3] <= c.picture[1], "the title row is above the box");}for i in 0..9 {for j in (i + 1)..9 {@@ -999,4 +1093,32 @@ mod tests {// (both edges are rounded, not the height: 7 * 1.5 = 10.5 -> 11 and 126 * 1.5 = 189)assert_eq!((r.width(), r.height()), (285, 178));}++ #[test]+ fn the_windows_of_an_app_keep_the_order_they_were_first_seen_in() {+ let mut order = Vec::new();+ assert_eq!(keep_order(&mut order, &[30, 10, 20]), [30, 10, 20], "the first look gives the order");+ // another window came to the front: the list is told in another order, the cards stay where they are+ assert_eq!(keep_order(&mut order, &[10, 20, 30]), [30, 10, 20]);+ assert_eq!(keep_order(&mut order, &[20, 30, 10]), [30, 10, 20]);+ // a new window joins at the end, a closed one leaves its place+ assert_eq!(keep_order(&mut order, &[40, 20, 30, 10]), [30, 10, 20, 40]);+ assert_eq!(keep_order(&mut order, &[40, 20, 30]), [30, 20, 40]);+ assert_eq!(keep_order(&mut order, &[]), Vec::<isize>::new());+ assert!(order.is_empty());+ }++ #[test]+ fn the_card_under_a_point_is_found_and_the_gaps_belong_to_no_card() {+ let l = layout(&[SRC_169, SRC_169, SRC_169], 1_900.0);+ for i in 0..3 {+ let [x, y, w, h] = l.card(i).card;+ assert_eq!(l.card_at((x + w / 2.0, y + h / 2.0)), Some(i));+ assert_eq!(l.card_at((x, y)), Some(i), "its own corner");+ }+ let a = l.card(0).card;+ assert_eq!(l.card_at((a[0] + a[2] + CARD_GAP / 2.0, a[1] + 5.0)), None, "between two cards");+ assert_eq!(l.card_at((1.0, 1.0)), None, "the window's own padding");+ assert_eq!(l.card_at((-5.0, 10.0)), None);+ }}