From 2ef155348f8b22ddc030229a3c7945df95c39293 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Tue, 27 Oct 2026 02:32:14 +0100 Subject: [PATCH] Launcher: keep test runs away from the real trash and clipboard desktop_seams.rs holds the launcher's two hard-to-undo desktop effects. HORIZON_LAUNCHER_TRASH= makes Delete (worlds, screenshots, packs) move into that folder instead of the system's trash, and HORIZON_LAUNCHER_CLIPBOARD= makes every Copy in the window write into that file instead of the clipboard. Test runs set both inside their scratch folders. Co-Authored-By: Claude Opus 5.5 --- launcher/README.md | 5 +- launcher/src/agents_page.rs | 2 +- launcher/src/desktop_seams.rs | 80 ++++++++++++++++++++++++++++++++ launcher/src/internet.rs | 2 +- launcher/src/lib.rs | 1 + launcher/src/main.rs | 25 ++++++++-- launcher/src/packs.rs | 2 +- launcher/src/screenshots.rs | 2 +- launcher/src/screenshots_page.rs | 5 +- launcher/src/servers_page.rs | 2 +- launcher/src/worlds.rs | 17 +------ 11 files changed, 117 insertions(+), 26 deletions(-) create mode 100644 launcher/src/desktop_seams.rs diff --git a/launcher/README.md b/launcher/README.md index e1a9150d0..afe45b2fc 100644 --- a/launcher/README.md +++ b/launcher/README.md @@ -242,7 +242,10 @@ and don't set `%ERRORLEVEL%` / `$LASTEXITCODE`. Scripts that need the exit code `--json` prints JSON where there is something to report (`versions`, `install`, `notes`, `next`, `links`, `game-args`, `update --check`, `logs`, `account`, `benchmark`, `data`, `modpack`). Errors go to stderr with exit code 1. `server` runs in the terminal, so lines you type reach its console. `HORIZON_LAUNCHER_HOME` replaces the data folder (for -scripts and tests). +scripts and tests). Test runs also set `HORIZON_LAUNCHER_TRASH=` and +`HORIZON_LAUNCHER_CLIPBOARD=` inside their scratch folders: what Delete would move to the +system's trash goes into that folder, and what Copy would put on the clipboard is written to that +file, so a test never touches the real trash or clipboard. ## AI agents (MCP) diff --git a/launcher/src/agents_page.rs b/launcher/src/agents_page.rs index e53d6550a..aae76b6f9 100644 --- a/launcher/src/agents_page.rs +++ b/launcher/src/agents_page.rs @@ -77,7 +77,7 @@ impl Launcher { &t, ) .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { - cx.write_to_clipboard(gpui::ClipboardItem::new_string(text.clone())); + copy_to_clipboard(cx, gpui::ClipboardItem::new_string(text.clone())); this.agents.copied = Some(id); cx.notify(); })) diff --git a/launcher/src/desktop_seams.rs b/launcher/src/desktop_seams.rs new file mode 100644 index 000000000..edec7ab8d --- /dev/null +++ b/launcher/src/desktop_seams.rs @@ -0,0 +1,80 @@ +//! The two things the launcher does to the player's desktop that a test run must never do to the +//! real one: moving files to the system's trash, and writing to the clipboard. +//! +//! - `HORIZON_LAUNCHER_TRASH=`: what would go to the trash is moved into that folder +//! instead (under a free name). +//! - `HORIZON_LAUNCHER_CLIPBOARD=`: what would be copied is written to that file instead +//! (text as UTF-8, a picture as its PNG or JPEG bytes). +//! +//! Test runs point both into their scratch folders, beside `HORIZON_HOME` and +//! `HORIZON_LAUNCHER_HOME`. +use std::path::{Path, PathBuf}; + +pub const TRASH_ENV: &str = "HORIZON_LAUNCHER_TRASH"; +pub const CLIPBOARD_ENV: &str = "HORIZON_LAUNCHER_CLIPBOARD"; + +/// Moves a file or folder to the system's trash, where the player can still get it back, or +/// into `HORIZON_LAUNCHER_TRASH` when it is set. +pub fn to_trash(path: &Path) -> Result<(), String> { + match std::env::var_os(TRASH_ENV).filter(|v| !v.is_empty()) { + Some(dir) => to_folder(path, Path::new(&dir)), + None => system_trash(path), + } +} + +fn to_folder(path: &Path, dir: &Path) -> Result<(), String> { + std::fs::create_dir_all(dir).map_err(|e| format!("{}: {e}", dir.display()))?; + let name = path.file_name().ok_or("Nothing to move")?.to_string_lossy(); + let target = (1..) + .map(|n| match n { + 1 => dir.join(&*name), + n => dir.join(format!("{name} {n}")), + }) + .find(|p| !p.exists()) + .unwrap(); + std::fs::rename(path, &target).map_err(|e| e.to_string()) +} + +fn system_trash(path: &Path) -> Result<(), String> { + #[allow(unused_mut)] + let mut trash = trash::TrashContext::default(); + // The crate's default on macOS asks Finder through AppleScript, which needs the player to + // allow the launcher to control Finder first; the file manager call needs nothing. + #[cfg(target_os = "macos")] + trash::macos::TrashContextExtMacos::set_delete_method( + &mut trash, + trash::macos::DeleteMethod::NsFileManager, + ); + trash.delete(path).map_err(|e| e.to_string()) +} + +/// The file copies go to instead of the clipboard, when `HORIZON_LAUNCHER_CLIPBOARD` is set. +pub fn clipboard_file() -> Option { + std::env::var_os(CLIPBOARD_ENV) + .filter(|v| !v.is_empty()) + .map(PathBuf::from) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_trash_folder_takes_what_would_go_to_the_trash() { + let root = std::env::temp_dir().join(format!("horizon-seams-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&root); + let (home, bin) = (root.join("home"), root.join("trash")); + std::fs::create_dir_all(home.join("World/region")).unwrap(); + std::fs::write(home.join("World/level.dat"), b"x").unwrap(); + std::fs::write(home.join("shot.png"), b"1").unwrap(); + to_folder(&home.join("World"), &bin).unwrap(); + to_folder(&home.join("shot.png"), &bin).unwrap(); + std::fs::write(home.join("shot.png"), b"2").unwrap(); + to_folder(&home.join("shot.png"), &bin).unwrap(); + assert!(bin.join("World/level.dat").is_file()); + assert_eq!(std::fs::read(bin.join("shot.png")).unwrap(), b"1"); + assert_eq!(std::fs::read(bin.join("shot.png 2")).unwrap(), b"2"); + assert!(std::fs::read_dir(&home).unwrap().next().is_none()); + std::fs::remove_dir_all(&root).unwrap(); + } +} diff --git a/launcher/src/internet.rs b/launcher/src/internet.rs index 76ab8dab8..d4f70ed18 100644 --- a/launcher/src/internet.rs +++ b/launcher/src/internet.rs @@ -267,7 +267,7 @@ impl Launcher { ) .bg(self.theme.plate) .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { - cx.write_to_clipboard(gpui::ClipboardItem::new_string(text.clone())); + copy_to_clipboard(cx, gpui::ClipboardItem::new_string(text.clone())); this.internet.copied = Some((text.clone(), Instant::now())); cx.notify(); })) diff --git a/launcher/src/lib.rs b/launcher/src/lib.rs index 9fafb80f8..4fb3c38d1 100644 --- a/launcher/src/lib.rs +++ b/launcher/src/lib.rs @@ -4,6 +4,7 @@ pub mod agents; pub mod backend; pub mod browser; pub mod desktop; +pub mod desktop_seams; #[cfg(unix)] pub mod game_app; pub mod heads; diff --git a/launcher/src/main.rs b/launcher/src/main.rs index 0542d7504..e7f606cd8 100644 --- a/launcher/src/main.rs +++ b/launcher/src/main.rs @@ -1192,6 +1192,22 @@ fn button( keyboard(track_hover(base, id), &t).active(|s| s.opacity(theme::PRESSED).top(px(1.))) } +/// Copies to the clipboard, or into `HORIZON_LAUNCHER_CLIPBOARD` when a test run sets it +/// (`horizon_launcher::desktop_seams`). +fn copy_to_clipboard(cx: &mut App, item: gpui::ClipboardItem) { + let Some(file) = horizon_launcher::desktop_seams::clipboard_file() else { + cx.write_to_clipboard(item); + return; + }; + let bytes = match item.entries().first() { + Some(gpui::ClipboardEntry::Image(image)) => image.bytes.clone(), + _ => item.text().unwrap_or_default().into_bytes(), + }; + if let Err(e) = std::fs::write(&file, bytes) { + eprintln!("{}: {e}", file.display()); + } +} + /// An on/off switch: a square knob sliding in a sunk track that turns violet when on. fn switch(id: impl Into, on: bool, t: &Theme) -> Stateful
{ let t = *t; @@ -2251,7 +2267,7 @@ impl Launcher { .on_click(cx.listener( move |this, _: &ClickEvent, _, cx| { if let Ok(text) = std::fs::read_to_string(&report) { - cx.write_to_clipboard(gpui::ClipboardItem::new_string(text)); + copy_to_clipboard(cx, gpui::ClipboardItem::new_string(text)); if let Some(c) = &mut this.crash { c.copied = true; } @@ -2968,9 +2984,10 @@ impl Launcher { .child( button("sign-in-copy", "Copy code", Kind::Secondary, &t).on_click( move |_, _, cx| { - cx.write_to_clipboard(gpui::ClipboardItem::new_string( - user_code.clone(), - )) + copy_to_clipboard( + cx, + gpui::ClipboardItem::new_string(user_code.clone()), + ) }, ), ) diff --git a/launcher/src/packs.rs b/launcher/src/packs.rs index 260d7f21a..368fe3408 100644 --- a/launcher/src/packs.rs +++ b/launcher/src/packs.rs @@ -329,7 +329,7 @@ pub fn remove(home: &Path, pack: &Pack) -> Result<(), String> { if pack.enabled { set_enabled(home, pack, false)?; } - crate::worlds::to_trash(&pack.path) + crate::desktop_seams::to_trash(&pack.path) .map_err(|e| format!("Could not move {} to the trash: {e}", pack.file)) } diff --git a/launcher/src/screenshots.rs b/launcher/src/screenshots.rs index 79e534df6..1d7c79fe1 100644 --- a/launcher/src/screenshots.rs +++ b/launcher/src/screenshots.rs @@ -55,7 +55,7 @@ pub fn find(home: &Path, query: &str) -> Result { /// Moves the screenshot to the system's trash. pub fn delete(shot: &Shot) -> Result<(), String> { - crate::worlds::to_trash(&shot.path) + crate::desktop_seams::to_trash(&shot.path) .map_err(|e| format!("Could not move {} to the trash: {e}", shot.name)) } diff --git a/launcher/src/screenshots_page.rs b/launcher/src/screenshots_page.rs index ce8d75227..d4017ade1 100644 --- a/launcher/src/screenshots_page.rs +++ b/launcher/src/screenshots_page.rs @@ -82,7 +82,10 @@ impl Launcher { }; self.screenshots.note = Some(match std::fs::read(&shot.path) { Ok(bytes) => { - cx.write_to_clipboard(ClipboardItem::new_image(&Image::from_bytes(format, bytes))); + copy_to_clipboard( + cx, + ClipboardItem::new_image(&Image::from_bytes(format, bytes)), + ); Ok("Copied to the clipboard.".into()) } Err(e) => Err(format!("Could not read {}: {e}", shot.name)), diff --git a/launcher/src/servers_page.rs b/launcher/src/servers_page.rs index ce4906a6a..4c52893bc 100644 --- a/launcher/src/servers_page.rs +++ b/launcher/src/servers_page.rs @@ -1643,7 +1643,7 @@ impl Launcher { ) .bg(t.plate) .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { - cx.write_to_clipboard(gpui::ClipboardItem::new_string( + copy_to_clipboard(cx, gpui::ClipboardItem::new_string( copy_text.clone(), )); this.servers.copied = Some(Instant::now()); diff --git a/launcher/src/worlds.rs b/launcher/src/worlds.rs index 68cbebd1a..3317ab1b8 100644 --- a/launcher/src/worlds.rs +++ b/launcher/src/worlds.rs @@ -251,21 +251,8 @@ fn rename_level(world: &Path, name: &str) -> std::io::Result<()> { /// Moves the world to the system's trash, where the player can still get it back. pub fn delete(world: &World) -> Result<(), String> { - to_trash(&world.path).map_err(|e| format!("Could not move {} to the trash: {e}", world.name)) -} - -/// Moves a file or folder to the system's trash. -pub(crate) fn to_trash(path: &Path) -> Result<(), trash::Error> { - #[allow(unused_mut)] - let mut trash = trash::TrashContext::default(); - // The crate's default on macOS asks Finder through AppleScript, which needs the player to - // allow the launcher to control Finder first; the file manager call needs nothing. - #[cfg(target_os = "macos")] - trash::macos::TrashContextExtMacos::set_delete_method( - &mut trash, - trash::macos::DeleteMethod::NsFileManager, - ); - trash.delete(path) + crate::desktop_seams::to_trash(&world.path) + .map_err(|e| format!("Could not move {} to the trash: {e}", world.name)) } #[cfg(test)]