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.

22.2 KB
Raw Blame
//! The windows a taskbar shows, found with the same rules as Explorer's taskbar, grouped by
//! application (first step of our own taskbar, see notes/design: görev çubuğu). Read-only:
//! nothing here changes any window.
//!
//! A window is a task when it is visible, not cloaked by DWM (other virtual desktops, suspended
//! UWP frames), and either has no owner and isn't a tool window, or asks for a taskbar button
//! with `WS_EX_APPWINDOW`. Windows are grouped by their AppUserModelID, or by their program
//! when they have none — as Explorer groups them.

use std::path::PathBuf;

use windows::Win32::Foundation::{CloseHandle, HWND, LPARAM, RECT};
use windows::Win32::Graphics::Dwm::{DWMWA_CLOAKED, DwmGetWindowAttribute};
use windows::Win32::System::Threading::{OpenProcess, PROCESS_NAME_WIN32, PROCESS_QUERY_LIMITED_INFORMATION, QueryFullProcessImageNameW};
use windows::Win32::UI::WindowsAndMessaging::{
    EnumWindows, GW_OWNER, GWL_EXSTYLE, GetWindow, GetWindowLongPtrW, GetWindowTextLengthW, GetWindowTextW, GetWindowThreadProcessId, IsIconic,
    IsWindowVisible, WS_EX_APPWINDOW, WS_EX_NOACTIVATE, WS_EX_TOOLWINDOW,
};
use windows::core::{BOOL, PWSTR};

/// One taskbar button's window.
#[derive(Clone, Debug)]
pub struct Task {
    pub hwnd: isize,
    pub title: String,
    pub pid: u32,
    /// The program, when it can be read (not for elevated processes of other users).
    pub exe: Option<PathBuf>,
    /// The window's AppUserModelID, when it sets one.
    pub app_id: Option<String>,
    pub minimized: bool,
    /// How the window asks to be started again, when it says so (see [`Relaunch`]).
    pub relaunch: Option<Relaunch>,
}

/// What a window tells the taskbar to start for it (`System.AppUserModel.RelaunchCommand` in its property store) when the program
/// that owns the window is not the one to start: Steam's window belongs to `steamwebhelper.exe`, which does nothing started alone,
/// and names `steam.exe` here. Windows' taskbar pins and starts this; the dock took the window's own program, so a Steam pinned from
/// its running window never started.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Relaunch {
    pub program: PathBuf,
    pub args: String,
    /// `System.AppUserModel.RelaunchDisplayNameResource` when it is a plain name (not a `@file,-id` reference).
    pub name: Option<String>,
    /// `System.AppUserModel.RelaunchIconResource`: the icon the window asks to be shown with (file and index), when the file is there.
    /// A browser's installed web app sets it to the app's own icon, while its program is the browser (issue 689: Gmail installed from
    /// Chrome showed Chrome's icon on the dock).
    pub icon: Option<(PathBuf, i32)>,
}

/// An icon resource as the shell writes it, `C:\x\app.ico` or `C:\x\app.dll,-101`: the file and the index (0 when none is given).
pub fn icon_resource(text: &str) -> Option<(PathBuf, i32)> {
    let t = text.trim().trim_start_matches('@');
    let (file, index) = match t.rsplit_once(',') {
        Some((f, i)) if i.trim().parse::<i32>().is_ok() => (f, i.trim().parse::<i32>().ok()?),
        _ => (t, 0),
    };
    let file = file.trim().trim_matches('"');
    (!file.is_empty()).then(|| (PathBuf::from(file), index))
}

/// A relaunch command line's program and arguments: the program ends with its `.exe` and may stand in quotes. `None` for a command
/// that names no program.
pub fn relaunch_of(command: &str, name: Option<String>) -> Option<Relaunch> {
    let line = command.trim();
    let end = line.to_ascii_lowercase().find(".exe")? + 4;
    let program = line[..end].trim().trim_matches('"');
    if program.is_empty() {
        return None;
    }
    let args = line[end..].trim_start_matches('"').trim().to_string();
    let name = name.map(|n| n.trim().to_string()).filter(|n| !n.is_empty() && !n.starts_with('@'));
    Some(Relaunch { program: PathBuf::from(program), args, name, icon: None })
}

impl Task {
    /// What Explorer groups by: the app id, else the program path (case-insensitive).
    pub fn group(&self) -> String {
        match (&self.app_id, &self.exe) {
            (Some(id), _) => id.to_lowercase(),
            (None, Some(exe)) => exe.to_string_lossy().to_lowercase(),
            (None, None) => format!("pid:{}", self.pid),
        }
    }
}

/// The style rules alone (testable without windows): owner, extended style.
pub fn style_is_task(has_owner: bool, ex_style: u32) -> bool {
    let app = ex_style & WS_EX_APPWINDOW.0 != 0;
    if app {
        return true;
    }
    !has_owner && ex_style & WS_EX_TOOLWINDOW.0 == 0 && ex_style & WS_EX_NOACTIVATE.0 == 0
}

/// An untitled window of no size is a helper, not a button (Explorer skips them too). A MINIMIZED one is not: a window without a
/// caption reports no size while it is minimized, and an untitled game window that minimizes itself when it loses focus (fullscreen
/// games do) left the dock's running apps although Windows' taskbar kept it (issue 60: "the app's icon and its running indicator
/// disappear from the Dock" as soon as focus moves elsewhere, "janela sem título", an untitled window).
pub fn is_helper(title: &str, width: i32, height: i32, minimized: bool) -> bool {
    !minimized && title.is_empty() && (width <= 0 || height <= 0)
}

fn cloaked(h: HWND) -> bool {
    let mut v: u32 = 0;
    unsafe { DwmGetWindowAttribute(h, DWMWA_CLOAKED, &mut v as *mut u32 as *mut _, 4) }.is_ok() && v != 0
}

fn title(h: HWND) -> String {
    let n = unsafe { GetWindowTextLengthW(h) };
    if n <= 0 {
        return String::new();
    }
    let mut buf = vec![0u16; n as usize + 1];
    let got = unsafe { GetWindowTextW(h, &mut buf) };
    String::from_utf16_lossy(&buf[..got.max(0) as usize])
}

pub(crate) fn exe_of(pid: u32) -> Option<PathBuf> {
    unsafe {
        let p = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, pid).ok()?;
        let mut buf = [0u16; 1024];
        let mut len = buf.len() as u32;
        let r = QueryFullProcessImageNameW(p, PROCESS_NAME_WIN32, PWSTR(buf.as_mut_ptr()), &mut len);
        let _ = CloseHandle(p);
        r.ok()?;
        Some(PathBuf::from(String::from_utf16_lossy(&buf[..len as usize])))
    }
}

/// The full path of the program a process runs (`None` when it cannot be opened with the limited query right, or has gone). Read-only;
/// it opens a handle, so it belongs on a worker thread. The tray feed uses it to give a notification icon the identity the order file
/// remembers it by.
pub fn process_exe_path(pid: u32) -> Option<PathBuf> {
    exe_of(pid)
}

/// The window's AppUserModelID (`System.AppUserModel.ID` in its property store) and how it asks to be started again (a command
/// whose program is on the disk; else none).
fn identity_of(h: HWND) -> (Option<String>, Option<Relaunch>) {
    use windows::Win32::Foundation::PROPERTYKEY;
    use windows::Win32::Storage::EnhancedStorage::{
        PKEY_AppUserModel_ID, PKEY_AppUserModel_RelaunchCommand, PKEY_AppUserModel_RelaunchDisplayNameResource, PKEY_AppUserModel_RelaunchIconResource,
    };
    use windows::Win32::System::Com::StructuredStorage::PropVariantToStringAlloc;
    use windows::Win32::UI::Shell::PropertiesSystem::{IPropertyStore, SHGetPropertyStoreForWindow};
    unsafe {
        let Ok(store) = SHGetPropertyStoreForWindow::<IPropertyStore>(h) else { return (None, None) };
        let text = |key: &PROPERTYKEY| -> Option<String> {
            let v = store.GetValue(key).ok()?;
            let p = PropVariantToStringAlloc(&v).ok()?;
            let s = p.to_string().ok();
            windows::Win32::System::Com::CoTaskMemFree(Some(p.0 as *const _));
            s.filter(|s| !s.is_empty())
        };
        let id = text(&PKEY_AppUserModel_ID);
        let relaunch = text(&PKEY_AppUserModel_RelaunchCommand)
            .and_then(|c| relaunch_of(&c, text(&PKEY_AppUserModel_RelaunchDisplayNameResource)))
            .filter(|r| r.program.is_file())
            .map(|mut r| {
                r.icon = text(&PKEY_AppUserModel_RelaunchIconResource).and_then(|t| icon_resource(&t)).filter(|(f, _)| f.is_file());
                r
            });
        (id, relaunch)
    }
}

fn task_of(h: HWND) -> Option<Task> {
    unsafe {
        if !IsWindowVisible(h).as_bool() {
            return None;
        }
        let ex = GetWindowLongPtrW(h, GWL_EXSTYLE) as u32;
        let has_owner = GetWindow(h, GW_OWNER).is_ok_and(|o| !o.is_invalid());
        if !style_is_task(has_owner, ex) || cloaked(h) {
            return None;
        }
        let mut r = RECT::default();
        let _ = windows::Win32::UI::WindowsAndMessaging::GetWindowRect(h, &mut r);
        let mut pid = 0u32;
        GetWindowThreadProcessId(h, Some(&mut pid));
        let title = title(h);
        let minimized = IsIconic(h).as_bool();
        if is_helper(&title, r.right - r.left, r.bottom - r.top, minimized) {
            return None;
        }
        let (app_id, relaunch) = identity_of(h);
        Some(Task { hwnd: h.0 as isize, title, pid, exe: exe_of(pid), app_id, minimized, relaunch })
    }
}

/// The current task windows, in Z order (the active one first).
pub fn tasks() -> Vec<Task> {
    unsafe extern "system" fn each(h: HWND, l: LPARAM) -> BOOL {
        let out = unsafe { &mut *(l.0 as *mut Vec<Task>) };
        if let Some(t) = task_of(h) {
            out.push(t);
        }
        BOOL(1)
    }
    let mut out: Vec<Task> = Vec::new();
    unsafe {
        let _ = EnumWindows(Some(each), LPARAM(&mut out as *mut Vec<Task> as isize));
    }
    // The windows seen before, and the one in front, that the enumeration did not give: still tasks while they pass the same rules.
    // WHY (issue 60): a game behind Easy Anti-Cheat showed in the dock only while it had the focus, and left it about a minute after;
    // its reporter found that EnumWindows from another program does not return the game's window at all, though it is on screen and
    // Windows' taskbar keeps it (the taskbar keeps the windows it was told about). Now a window met once (in front) is kept for as
    // long as it exists and is a task.
    let mut known = KNOWN.lock().unwrap_or_else(|e| e.into_inner());
    let front = unsafe { windows::Win32::UI::WindowsAndMessaging::GetForegroundWindow() }.0 as isize;
    let mut asked: Vec<isize> = known.clone();
    if front != 0 {
        asked.push(front);
    }
    let out = with_known(out, &asked, |h| task_of(HWND(h as *mut _)));
    *known = out.iter().map(|t| t.hwnd).collect();
    out
}

/// The windows the last listing gave (with those kept by [`with_known`]).
static KNOWN: std::sync::Mutex<Vec<isize>> = std::sync::Mutex::new(Vec::new());

/// `listed`, then each of `known` it lacks that `check` still finds a task (in `known`'s order, no window twice).
fn with_known(mut listed: Vec<Task>, known: &[isize], check: impl Fn(isize) -> Option<Task>) -> Vec<Task> {
    for &h in known {
        if listed.iter().any(|t| t.hwnd == h) {
            continue;
        }
        if let Some(t) = check(h) {
            listed.push(t);
        }
    }
    listed
}

/// Tasks grouped like taskbar buttons: (group key, its windows), in order of first appearance.
pub fn grouped(tasks: Vec<Task>) -> Vec<(String, Vec<Task>)> {
    let mut groups: Vec<(String, Vec<Task>)> = Vec::new();
    for t in tasks {
        let k = t.group();
        match groups.iter_mut().find(|g| g.0 == k) {
            Some(g) => g.1.push(t),
            None => groups.push((k, vec![t])),
        }
    }
    groups
}

// ---------- change tracking ----------

use std::cell::RefCell;
use std::sync::mpsc::{Receiver, Sender};
use std::time::Duration;

/// What changed among the task windows (coalesced over a frame).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Changes {
    /// Windows appeared, went away, or were shown / hidden / cloaked: the list changed.
    pub list: bool,
    /// A title changed.
    pub titles: bool,
    /// Another window came to the front, or one was minimized / restored.
    pub active: bool,
}

thread_local! {
    static EVENTS: RefCell<Option<Sender<u32>>> = const { RefCell::new(None) };
}

unsafe extern "system" fn on_event(
    _hook: windows::Win32::UI::Accessibility::HWINEVENTHOOK,
    event: u32,
    hwnd: HWND,
    id_object: i32,
    id_child: i32,
    _thread: u32,
    _time: u32,
) {
    // only whole windows, not their parts (carets, scroll bars, list items)
    if id_object != 0 || id_child != 0 || hwnd.is_invalid() {
        return;
    }
    EVENTS.with(|e| {
        if let Some(tx) = e.borrow().as_ref() {
            let _ = tx.send(event);
        }
    });
}

/// Watches the task windows; `on_change` runs on a background thread, at most once a frame
/// (~16 ms) with what changed. Drop to stop.
pub struct TaskWatch {
    thread: u32,
}

impl Drop for TaskWatch {
    fn drop(&mut self) {
        use windows::Win32::UI::WindowsAndMessaging::{PostThreadMessageW, WM_QUIT};
        unsafe {
            let _ = PostThreadMessageW(self.thread, WM_QUIT, windows::Win32::Foundation::WPARAM(0), LPARAM(0));
        }
    }
}

fn classify(event: u32, c: &mut Changes) {
    use windows::Win32::UI::WindowsAndMessaging::{
        EVENT_OBJECT_CLOAKED, EVENT_OBJECT_CREATE, EVENT_OBJECT_DESTROY, EVENT_OBJECT_HIDE, EVENT_OBJECT_NAMECHANGE, EVENT_OBJECT_SHOW, EVENT_OBJECT_UNCLOAKED,
        EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_MINIMIZEEND, EVENT_SYSTEM_MINIMIZESTART,
    };
    match event {
        EVENT_OBJECT_CREATE | EVENT_OBJECT_DESTROY | EVENT_OBJECT_SHOW | EVENT_OBJECT_HIDE | EVENT_OBJECT_CLOAKED | EVENT_OBJECT_UNCLOAKED => c.list = true,
        EVENT_OBJECT_NAMECHANGE => c.titles = true,
        EVENT_SYSTEM_FOREGROUND | EVENT_SYSTEM_MINIMIZESTART | EVENT_SYSTEM_MINIMIZEEND => c.active = true,
        _ => {}
    }
}

/// Starts watching. The hooks live on their own thread with a message loop (out-of-context
/// WinEvents are delivered there); a second thread coalesces them.
pub fn watch(on_change: impl Fn(Changes) + Send + 'static) -> anyhow::Result<TaskWatch> {
    use windows::Win32::UI::Accessibility::{SetWinEventHook, UnhookWinEvent};
    use windows::Win32::UI::WindowsAndMessaging::{
        DispatchMessageW, EVENT_OBJECT_CLOAKED, EVENT_OBJECT_CREATE, EVENT_OBJECT_HIDE, EVENT_OBJECT_NAMECHANGE, EVENT_OBJECT_UNCLOAKED, EVENT_SYSTEM_FOREGROUND,
        EVENT_SYSTEM_MINIMIZEEND, EVENT_SYSTEM_MINIMIZESTART, GetMessageW, MSG, TranslateMessage, WINEVENT_OUTOFCONTEXT,
    };
    let (tx, rx): (Sender<u32>, Receiver<u32>) = std::sync::mpsc::channel();
    let (ready_tx, ready_rx) = std::sync::mpsc::channel::<u32>();
    std::thread::Builder::new().name("rf-taskwatch".into()).spawn(move || {
        EVENTS.with(|e| *e.borrow_mut() = Some(tx));
        let ranges = [
            (EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND),
            (EVENT_SYSTEM_MINIMIZESTART, EVENT_SYSTEM_MINIMIZEEND),
            (EVENT_OBJECT_CREATE, EVENT_OBJECT_HIDE),
            (EVENT_OBJECT_NAMECHANGE, EVENT_OBJECT_NAMECHANGE),
            (EVENT_OBJECT_CLOAKED, EVENT_OBJECT_UNCLOAKED),
        ];
        let hooks: Vec<_> = ranges.iter().map(|&(a, b)| unsafe { SetWinEventHook(a, b, None, Some(on_event), 0, 0, WINEVENT_OUTOFCONTEXT) }).collect();
        let _ = ready_tx.send(unsafe { windows::Win32::System::Threading::GetCurrentThreadId() });
        let mut msg = MSG::default();
        unsafe {
            while GetMessageW(&mut msg, None, 0, 0).as_bool() {
                let _ = TranslateMessage(&msg);
                DispatchMessageW(&msg);
            }
            for h in hooks {
                let _ = UnhookWinEvent(h);
            }
        }
        EVENTS.with(|e| *e.borrow_mut() = None);
    })?;
    let thread = ready_rx.recv_timeout(Duration::from_secs(5))?;
    std::thread::Builder::new().name("rf-taskwatch-coalesce".into()).spawn(move || {
        while let Ok(first) = rx.recv() {
            let mut c = Changes::default();
            classify(first, &mut c);
            let until = std::time::Instant::now() + Duration::from_millis(16);
            loop {
                let left = until.saturating_duration_since(std::time::Instant::now());
                match rx.recv_timeout(left) {
                    Ok(e) => classify(e, &mut c),
                    Err(std::sync::mpsc::RecvTimeoutError::Timeout) => break,
                    Err(_) => return,
                }
            }
            if c != Changes::default() {
                on_change(c);
            }
        }
    })?;
    Ok(TaskWatch { thread })
}

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

    #[test]
    fn style_rules_match_explorer() {
        let tool = WS_EX_TOOLWINDOW.0;
        let app = WS_EX_APPWINDOW.0;
        assert!(style_is_task(false, 0), "a plain top-level window");
        assert!(!style_is_task(true, 0), "an owned dialog has no button");
        assert!(!style_is_task(false, tool), "tool windows have none");
        assert!(style_is_task(true, app), "APPWINDOW forces a button, even owned");
        assert!(style_is_task(false, tool | app), "APPWINDOW wins over TOOLWINDOW");
        assert!(!style_is_task(false, WS_EX_NOACTIVATE.0));
    }

    #[test]
    fn groups_by_app_id_then_program() {
        let t = |pid, exe: &str, id: Option<&str>| Task { hwnd: pid as isize, title: String::new(), pid, exe: Some(PathBuf::from(exe)), app_id: id.map(String::from), minimized: false, relaunch: None };
        let g = grouped(vec![
            t(1, r"C:\a\Chrome.exe", None),
            t(2, r"C:\b\code.exe", Some("Microsoft.VisualStudioCode")),
            t(3, r"C:\A\chrome.exe", None),
            t(4, r"C:\c\other.exe", Some("microsoft.visualstudiocode")),
        ]);
        assert_eq!(g.len(), 2);
        assert_eq!(g[0].1.iter().map(|t| t.pid).collect::<Vec<_>>(), vec![1, 3]);
        assert_eq!(g[1].1.iter().map(|t| t.pid).collect::<Vec<_>>(), vec![2, 4]);
    }

    #[test]
    fn notices_a_new_window_quickly() {
        use std::sync::{Arc, Mutex};
        use windows::Win32::UI::WindowsAndMessaging::{
            CreateWindowExW, DestroyWindow, SW_SHOWNOACTIVATE, ShowWindow, WINDOW_EX_STYLE, WS_OVERLAPPEDWINDOW,
        };
        let seen: Arc<Mutex<Vec<(std::time::Instant, Changes)>>> = Default::default();
        let s2 = seen.clone();
        let _w = watch(move |c| s2.lock().unwrap().push((std::time::Instant::now(), c))).unwrap();
        std::thread::sleep(Duration::from_millis(200));
        seen.lock().unwrap().clear();
        // an off-screen, never-activated window, from this thread
        let class = crate::wide::wide("STATIC");
        let title = crate::wide::wide("rf tasklist test");
        let shown_at;
        let h = unsafe {
            let h = CreateWindowExW(
                WINDOW_EX_STYLE(0),
                windows::core::PCWSTR(class.as_ptr()),
                windows::core::PCWSTR(title.as_ptr()),
                WS_OVERLAPPEDWINDOW,
                -4000,
                -4000,
                200,
                100,
                None,
                None,
                None,
                None,
            )
            .unwrap();
            shown_at = std::time::Instant::now();
            let _ = ShowWindow(h, SW_SHOWNOACTIVATE);
            h
        };
        let deadline = std::time::Instant::now() + Duration::from_secs(2);
        let mut latency = None;
        while std::time::Instant::now() < deadline {
            if let Some((t, _)) = seen.lock().unwrap().iter().find(|(_, c)| c.list) {
                latency = Some(t.duration_since(shown_at));
                break;
            }
            std::thread::sleep(Duration::from_millis(5));
        }
        let listed = tasks().iter().any(|t| t.hwnd == h.0 as isize && t.title == "rf tasklist test");
        unsafe {
            let _ = DestroyWindow(h);
        }
        let latency = latency.expect("the new window was reported");
        assert!(listed, "and it is a task");
        assert!(latency < Duration::from_millis(500), "reported after {latency:?}");
        eprintln!("new window reported after {latency:?}");
    }

    #[test]
    fn lists_without_failing() {
        // whatever is open on the test machine: each listed task has a pid and is visible
        for t in tasks() {
            assert!(t.pid != 0);
        }
    }
}

#[cfg(test)]
mod helper_tests {
    use super::is_helper;

    #[test]
    fn a_minimized_untitled_window_stays_a_task() {
        assert!(is_helper("", 0, 0, false), "an untitled window of no size: a helper");
        assert!(!is_helper("", 0, 0, true), "the same window minimized (an untitled game that lost focus): a task");
        assert!(!is_helper("", 800, 600, false) && !is_helper("Notes", 0, 0, false));
    }
}

#[cfg(test)]
mod known_tests {
    use std::path::PathBuf;

    use super::{Task, relaunch_of, with_known};

    #[test]
    fn a_relaunch_command_is_a_program_and_its_arguments() {
        let r = relaunch_of(r#""C:/Program Files (x86)/Steam/steam.exe" -foreground"#, Some("Steam".into())).unwrap();
        assert_eq!(r.program, PathBuf::from("C:/Program Files (x86)/Steam/steam.exe"));
        assert_eq!((r.args.as_str(), r.name.as_deref()), ("-foreground", Some("Steam")));
        let r = relaunch_of("C:/a/b.EXE", Some("@C:/a/b.exe,-101".into())).unwrap();
        assert_eq!((r.program, r.args.as_str(), r.name), (PathBuf::from("C:/a/b.EXE"), "", None), "a resource reference is no name");
        assert_eq!(relaunch_of("  ", None), None);
        assert_eq!(super::icon_resource(r"C:\Users\u\AppData\Local\Google\Chrome\User Data\Default\Web Applications\x\Gmail.ico"), Some((PathBuf::from(r"C:\Users\u\AppData\Local\Google\Chrome\User Data\Default\Web Applications\x\Gmail.ico"), 0)));
        assert_eq!(super::icon_resource(r"@C:\a\app.dll,-101"), Some((PathBuf::from(r"C:\a\app.dll"), -101)));
        assert_eq!(super::icon_resource(r"C:\a, b\x.ico"), Some((PathBuf::from(r"C:\a, b\x.ico"), 0)), "a comma in a folder name is no index");
        assert_eq!(super::icon_resource("  "), None);
        assert_eq!(relaunch_of("shell:AppsFolder/x", None), None, "no program named");
    }

    fn task(h: isize) -> Task {
        Task { hwnd: h, title: format!("w{h}"), pid: 1, exe: None, app_id: None, minimized: false, relaunch: None }
    }

    #[test]
    fn a_window_the_enumeration_misses_is_kept_while_it_is_still_a_task() {
        let listed = vec![task(1), task(2)];
        // 3 was seen before and is still a task; 4 is gone; 2 is listed already
        let out = with_known(listed, &[2, 3, 4], |h| (h != 4).then(|| task(h)));
        assert_eq!(out.iter().map(|t| t.hwnd).collect::<Vec<_>>(), vec![1, 2, 3]);
    }
}