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=<folder> makes Delete (worlds, screenshots, packs) move into that folder instead of the system's trash, and HORIZON_LAUNCHER_CLIPBOARD=<file> 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 <noreply@anthropic.com>
This commit is contained in:
+4
-1
@@ -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=<folder>` and
|
||||
`HORIZON_LAUNCHER_CLIPBOARD=<file>` 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)
|
||||
|
||||
|
||||
@@ -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();
|
||||
}))
|
||||
|
||||
@@ -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=<folder>`: what would go to the trash is moved into that folder
|
||||
//! instead (under a free name).
|
||||
//! - `HORIZON_LAUNCHER_CLIPBOARD=<file>`: 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<PathBuf> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}))
|
||||
|
||||
@@ -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;
|
||||
|
||||
+21
-4
@@ -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<SharedString>, on: bool, t: &Theme) -> Stateful<Div> {
|
||||
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()),
|
||||
)
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
|
||||
@@ -55,7 +55,7 @@ pub fn find(home: &Path, query: &str) -> Result<Shot, String> {
|
||||
|
||||
/// 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))
|
||||
}
|
||||
|
||||
|
||||
@@ -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)),
|
||||
|
||||
@@ -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());
|
||||
|
||||
+2
-15
@@ -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)]
|
||||
|
||||
Reference in New Issue
Block a user