* claim: #7192 * Web app: Report a bug saves its screenshot as a download (#7192) Save screenshot… wrote the PNG with image's save, that is std::fs, which the browser doesn't have. The screenshot is now encoded in memory and written through vfs, as every other save is, so the web app hands it out as a download. Natively the same file is written and ui.screenshot replies as before. A failed save says why in plain text instead of a Rust debug value. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Web app: a screenshot without a path no longer panics (#7192) `ui.screenshot` without a `path` saved to `std::env::temp_dir()`, which panics on wasm32-unknown-unknown (std has no file system there), and the web app's control requests come through `window.solidcraft.request`. `vfs::temp_dir()` now returns `vfs::BROWSER_TEMP`, a folder of the in-memory files, in the browser, and `save_screenshot` and `vfs::design_folder_refusal` (reached when a request names `allow_paths` while a design is open) use it. Natively nothing changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Web app: keymap and theme exports download (#7192) Both exports wrote with `std::fs::write`, which always fails in the browser. They now write through `vfs::write` like the other saves, so the web app hands the file out as a download. Natively the file is the same; an agent's folder scope now applies to them as to every other write. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Tests: say what the folder-scope checks of the saves show (#7192) The screenshot test wraps the write in a folder scope by hand: that proves the write goes through vfs, not that ui.screenshot's write runs inside the request's scope. ui.screenshot checks the agent's folders when the request arrives; the PNG is written in a later frame, outside that scope, as on main. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -627,6 +627,8 @@ mod tests {
|
||||
assert_eq!(route("/disk/18f-2a/Bracket.solidcraft"), WriteTo::Disk);
|
||||
assert_eq!(route("/downloads/part.step"), WriteTo::Download);
|
||||
assert_eq!(route("/upload/part.step"), WriteTo::Memory);
|
||||
// The temporary folder (a screenshot without a path, #7192) stays in memory.
|
||||
assert_eq!(route(&format!("{}/solidcraft-shot.png", solidcraft_engine::io::vfs::BROWSER_TEMP)), WriteTo::Memory);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
+10
-2
@@ -177,7 +177,7 @@ fn hidden(name: &std::ffi::OsStr) -> bool {
|
||||
/// in a hidden folder. So a design saved in home itself, in `/`, on a drive root or in a system
|
||||
/// folder never puts `~/.ssh`, `~/.bashrc` or anything else of the user's in reach.
|
||||
pub fn design_folder_refusal(dir: &std::path::Path) -> Option<String> {
|
||||
design_folder_refusal_in(dir, home_dir().as_deref(), Some(&std::env::temp_dir()))
|
||||
design_folder_refusal_in(dir, home_dir().as_deref(), Some(&temp_dir()))
|
||||
}
|
||||
|
||||
/// [`design_folder_refusal`] with the home and temporary folders given (tests).
|
||||
@@ -585,9 +585,17 @@ pub fn long_names(path: &std::path::Path) -> std::path::PathBuf {
|
||||
}
|
||||
}
|
||||
|
||||
/// The temporary folder in the browser. std has none there (`std::env::temp_dir` panics on
|
||||
/// wasm32-unknown-unknown), so it is a folder of the in-memory files: what is written in it stays
|
||||
/// in memory, neither stored nor downloaded (#7192).
|
||||
pub const BROWSER_TEMP: &str = "/tmp";
|
||||
|
||||
/// The temporary folder spelled with long names ([`long_names`]), for the folders SolidCraft
|
||||
/// makes in it.
|
||||
/// makes in it; [`BROWSER_TEMP`] in the browser.
|
||||
pub fn temp_dir() -> std::path::PathBuf {
|
||||
if cfg!(target_arch = "wasm32") {
|
||||
return std::path::PathBuf::from(BROWSER_TEMP);
|
||||
}
|
||||
long_names(&std::env::temp_dir())
|
||||
}
|
||||
|
||||
|
||||
@@ -1152,19 +1152,31 @@ fn occurrence(s: &solidcraft_engine::Session, v: &Value) -> Result<u64, String>
|
||||
.ok_or_else(|| format!("no occurrence `{key}`"))
|
||||
}
|
||||
|
||||
/// Save a screenshot PNG.
|
||||
/// Save a screenshot PNG; without a `path`, in the temporary folder ([`vfs::temp_dir`], which in
|
||||
/// the browser keeps it in memory: std's would panic there).
|
||||
pub fn save_screenshot(image: &egui::ColorImage, path: Option<&str>) -> Value {
|
||||
let [w, h] = image.size;
|
||||
let path = path.map(str::to_string).unwrap_or_else(|| vfs::temp_dir().join("solidcraft-shot.png").to_string_lossy().to_string());
|
||||
match write_screenshot(image, &path) {
|
||||
Ok(()) => json!({"ok": true, "result": {"path": path, "width": w, "height": h}}),
|
||||
Err(e) => json!({"ok": false, "error": e}),
|
||||
}
|
||||
}
|
||||
|
||||
/// Write `image` to `path` in the format its extension names (a PNG for `.png`), through
|
||||
/// [`vfs`] like every other save: the disk natively, a download or browser storage in the web
|
||||
/// app (#7192).
|
||||
pub(crate) fn write_screenshot(image: &egui::ColorImage, path: &str) -> Result<(), String> {
|
||||
let [w, h] = image.size;
|
||||
let mut buf = Vec::with_capacity(w * h * 4);
|
||||
for c in &image.pixels {
|
||||
buf.extend_from_slice(&c.to_array());
|
||||
}
|
||||
let Some(img) = image::RgbaImage::from_raw(w as u32, h as u32, buf) else { return json!({"ok": false, "error": "bad image"}) };
|
||||
let path = path.map(str::to_string).unwrap_or_else(|| std::env::temp_dir().join("solidcraft-shot.png").to_string_lossy().to_string());
|
||||
match img.save(&path) {
|
||||
Ok(()) => json!({"ok": true, "result": {"path": path, "width": w, "height": h}}),
|
||||
Err(e) => json!({"ok": false, "error": e.to_string()}),
|
||||
}
|
||||
let img = image::RgbaImage::from_raw(w as u32, h as u32, buf).ok_or("bad image")?;
|
||||
let format = image::ImageFormat::from_path(path).map_err(|e| e.to_string())?;
|
||||
let mut bytes = std::io::Cursor::new(Vec::new());
|
||||
img.write_to(&mut bytes, format).map_err(|e| e.to_string())?;
|
||||
vfs::write(path, bytes.get_ref()).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -1870,6 +1882,40 @@ mod tests {
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
|
||||
/// A screenshot is written through `vfs`, as other saves are, so the web app can hand it out
|
||||
/// as a download (#7192). On the disk it is the same PNG with the same reply. A folder scope
|
||||
/// active while it is written refuses a path outside it, which shows the write goes through
|
||||
/// `vfs`. (`ui.screenshot` checks an agent's folders when the request arrives; the write comes
|
||||
/// in a later frame, outside the request's scope, as before.)
|
||||
#[test]
|
||||
fn screenshots_are_written_through_vfs_with_the_same_reply() {
|
||||
let base = vfs::temp_dir().join(format!("solidcraft-shot-scope-{}", std::process::id()));
|
||||
let (root, out) = (base.join("root"), base.join("out"));
|
||||
std::fs::create_dir_all(&root).unwrap();
|
||||
std::fs::create_dir_all(&out).unwrap();
|
||||
let image = egui::ColorImage::new([4, 3], vec![egui::Color32::GREEN; 12]);
|
||||
let path = root.join("shot.png").to_string_lossy().to_string();
|
||||
assert_eq!(save_screenshot(&image, Some(&path)), json!({"ok": true, "result": {"path": path, "width": 4, "height": 3}}));
|
||||
let png = image::load_from_memory_with_format(&std::fs::read(&path).unwrap(), image::ImageFormat::Png).unwrap().into_rgba8();
|
||||
assert_eq!((png.dimensions(), png.get_pixel(0, 0).0), ((4, 3), [0, 255, 0, 255]));
|
||||
let outside = out.join("shot.png").to_string_lossy().to_string();
|
||||
let v = vfs::scoped(Some(vfs::PathScope::new([root.clone()])), || save_screenshot(&image, Some(&outside)));
|
||||
assert!(v["ok"] == false && v["error"].as_str().is_some_and(|e| e.contains("outside the folders this agent may use")), "{v}");
|
||||
assert_eq!(std::fs::read_dir(&out).unwrap().count(), 0, "nothing written outside");
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
|
||||
/// Without a `path`, a screenshot goes to `vfs::temp_dir`: in the web app a folder kept in
|
||||
/// memory, where std's temporary folder panics (#7192).
|
||||
#[test]
|
||||
fn a_screenshot_without_a_path_goes_to_the_vfs_temporary_folder() {
|
||||
let image = egui::ColorImage::new([4, 3], vec![egui::Color32::GREEN; 12]);
|
||||
let path = vfs::temp_dir().join("solidcraft-shot.png");
|
||||
let reply = save_screenshot(&image, None);
|
||||
assert_eq!(reply, json!({"ok": true, "result": {"path": path.to_string_lossy(), "width": 4, "height": 3}}));
|
||||
assert!(path.is_file());
|
||||
}
|
||||
|
||||
/// `ui.camera` reads the camera and sets it; its reply, passed back, sets the same view
|
||||
/// again. Fields left out keep their values, `fit` frames occurrences, and a refused field
|
||||
/// changes nothing.
|
||||
|
||||
@@ -1338,6 +1338,32 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// Save screenshot… writes the PNG where the picker says, the way the web app's other saves
|
||||
/// write (#7192), and a failure reads as plain words, not a Rust value.
|
||||
#[test]
|
||||
fn issue_report_save_screenshot_writes_the_png_and_explains_a_failure() {
|
||||
let mut h = crate::scenario::Harness::new();
|
||||
h.app.home.open = false;
|
||||
let mut draft = draft_for(&h.app, None, None);
|
||||
draft.image = Some(Arc::new(ColorImage::new([4, 3], vec![egui::Color32::GREEN; 12])));
|
||||
h.app.issue_report.draft = Some(draft);
|
||||
h.frames(4);
|
||||
for (path, saved) in [(h.dir.join("report.png"), true), (h.dir.join("missing").join("report.png"), false)] {
|
||||
let answer = path.to_string_lossy().to_string();
|
||||
h.app.services.pick_save = Some(Box::new(move |_, _| Box::pin(std::future::ready(Some(answer.clone())))));
|
||||
click_commands(&mut h, "Save screenshot…");
|
||||
let note = h.app.issue_report.draft.as_ref().unwrap().note.clone();
|
||||
if saved {
|
||||
let png = image::load_from_memory_with_format(&std::fs::read(&path).unwrap(), image::ImageFormat::Png).unwrap();
|
||||
assert_eq!(png.into_rgba8().dimensions(), (4, 3));
|
||||
assert_eq!(note, "Screenshot saved. Attach the PNG in the GitHub draft.");
|
||||
} else {
|
||||
let why = std::fs::write(&path, b"").unwrap_err();
|
||||
assert_eq!(note, format!("Could not save screenshot: {why}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn issue_report_editing_enter_and_cad_shortcuts_leave_the_live_boolean_unchanged() {
|
||||
let mut h = crate::scenario::Harness::new();
|
||||
|
||||
@@ -45,12 +45,11 @@ fn screenshot_actions(app: &mut SolveApp, draft: &mut Draft, ui: &mut Ui) -> boo
|
||||
{
|
||||
app.pick_save(&format!("solidcraft-issue-{}.png", app.shot_token), &["png"], move |app, path| {
|
||||
let Some(path) = path else { return };
|
||||
let result = crate::control::save_screenshot(&image, Some(&path));
|
||||
let result = crate::control::write_screenshot(&image, &path);
|
||||
if let Some(draft) = app.issue_report.draft.as_mut() {
|
||||
draft.note = if result.get("ok").and_then(serde_json::Value::as_bool) == Some(true) {
|
||||
"Screenshot saved. Attach the PNG in the GitHub draft.".into()
|
||||
} else {
|
||||
format!("Could not save screenshot: {}", super::debug(&result.get("error"), 1000))
|
||||
draft.note = match result {
|
||||
Ok(()) => "Screenshot saved. Attach the PNG in the GitHub draft.".into(),
|
||||
Err(e) => format!("Could not save screenshot: {}", super::limited(&e, 1000)),
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
@@ -644,7 +644,8 @@ pub fn page(app: &mut SolveApp, ui: &mut egui::Ui) {
|
||||
let text = crate::toolbar_custom::export(app).to_string();
|
||||
app.pick_save("SolidCraft shortcuts.json", &["json"], move |app, path| {
|
||||
if let Some(path) = path {
|
||||
app.keymap.message = std::fs::write(&path, text).err().map(|e| format!("export: {e}"));
|
||||
// Through `vfs`, like every other save: the web app hands it out as a download.
|
||||
app.keymap.message = solidcraft_engine::io::vfs::write(&path, text.as_bytes()).err().map(|e| format!("export: {e}"));
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -1149,6 +1150,38 @@ mod card_tests {
|
||||
assert!(!h.app.prefs_window.open);
|
||||
}
|
||||
|
||||
/// Export… writes the shortcuts file through `vfs`, like the other saves, so the web app hands
|
||||
/// it out as a download (#7192). On the disk it is the same file; a folder scope active while
|
||||
/// it is written refuses a path outside it, which shows the write goes through `vfs`.
|
||||
#[test]
|
||||
fn shortcut_export_writes_through_vfs() {
|
||||
use solidcraft_engine::io::vfs;
|
||||
let mut h = Harness::new();
|
||||
h.app.home.open = false;
|
||||
h.call("ui.prefs", json!({"open":"Keyboard Shortcuts"}));
|
||||
h.frames(2);
|
||||
let (root, out) = (h.dir.join("root"), h.dir.join("out"));
|
||||
std::fs::create_dir_all(&root).unwrap();
|
||||
std::fs::create_dir_all(&out).unwrap();
|
||||
for (path, inside) in [(root.join("keys.json"), true), (out.join("keys.json"), false)] {
|
||||
let answer = path.to_string_lossy().to_string();
|
||||
h.app.services.pick_save = Some(Box::new(move |_, _| Box::pin(std::future::ready(Some(answer.clone())))));
|
||||
vfs::scoped(Some(vfs::PathScope::new([root.clone()])), || {
|
||||
h.step(&json!({"widget":"Export…"})).unwrap();
|
||||
h.frames(4);
|
||||
});
|
||||
let message = h.app.keymap.message.clone();
|
||||
if inside {
|
||||
assert_eq!(message, None);
|
||||
let file: Value = serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
|
||||
assert_eq!(file, crate::toolbar_custom::export(&h.app));
|
||||
} else {
|
||||
assert!(message.as_deref().is_some_and(|m| m.contains("outside the folders this agent may use")), "{message:?}");
|
||||
assert!(!path.exists(), "nothing written outside");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn help_shortcuts_card_keeps_rebinding_read_only() {
|
||||
let mut h = Harness::new();
|
||||
|
||||
@@ -1010,8 +1010,9 @@ fn theme_row(app: &mut SolveApp, ui: &mut egui::Ui) {
|
||||
let name = file.get("name").and_then(serde_json::Value::as_str).unwrap_or("Theme").to_string();
|
||||
let text = serde_json::to_string_pretty(&file).unwrap_or_default();
|
||||
app.pick_save(&format!("{name}.json"), &["json"], move |app, path| {
|
||||
// Through `vfs`, like every other save: the web app hands it out as a download.
|
||||
if let Some(path) = path
|
||||
&& let Err(e) = std::fs::write(&path, text)
|
||||
&& let Err(e) = solidcraft_engine::io::vfs::write(&path, text.as_bytes())
|
||||
{
|
||||
app.set_status(format!("Theme export: {e}"), true);
|
||||
}
|
||||
@@ -3696,6 +3697,43 @@ mod tests {
|
||||
assert_eq!((Tokens::get().control_radius, Tokens::get().radius_xl), (6, 10));
|
||||
}
|
||||
|
||||
/// A theme tile's Export… writes the theme file through `vfs`, like the other saves, so the
|
||||
/// web app hands it out as a download (#7192). On the disk it is the same file; a folder scope
|
||||
/// active while it is written refuses a path outside it, which shows the write goes through
|
||||
/// `vfs`.
|
||||
#[test]
|
||||
fn theme_export_writes_through_vfs() {
|
||||
use serde_json::json;
|
||||
use solidcraft_engine::io::vfs;
|
||||
let mut h = crate::scenario::Harness::new();
|
||||
h.app.home.open = false;
|
||||
h.call("ui.prefs", json!({"theme": "dark", "open": "Interface"}));
|
||||
h.frames(2);
|
||||
super::reveal_preference(&mut h, "prefs:theme_tile:dark");
|
||||
let (root, out) = (h.dir.join("root"), h.dir.join("out"));
|
||||
std::fs::create_dir_all(&root).unwrap();
|
||||
std::fs::create_dir_all(&out).unwrap();
|
||||
for (path, inside) in [(root.join("Dark.json"), true), (out.join("Dark.json"), false)] {
|
||||
let answer = path.to_string_lossy().to_string();
|
||||
h.app.services.pick_save = Some(Box::new(move |_, _| Box::pin(std::future::ready(Some(answer.clone())))));
|
||||
h.app.status = None;
|
||||
vfs::scoped(Some(vfs::PathScope::new([root.clone()])), || {
|
||||
h.step(&json!({"click":{"handle":"prefs:theme_tile:dark"},"button":"right"})).unwrap();
|
||||
h.step(&json!({"click":{"handle":"menu:theme_export"}})).unwrap();
|
||||
h.frames(4);
|
||||
});
|
||||
let status = h.app.status.clone().map(|(text, ..)| text);
|
||||
if inside {
|
||||
assert_eq!(status, None);
|
||||
let file: serde_json::Value = serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
|
||||
assert_eq!(file, super::theme_file(&h.app, "dark", true));
|
||||
} else {
|
||||
assert!(status.as_deref().is_some_and(|s| s.contains("outside the folders this agent may use")), "{status:?}");
|
||||
assert!(!path.exists(), "nothing written outside");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Picking a font closes the font list, not the Preferences window's body: that folded the
|
||||
/// window to its title bar, and egui kept it folded between runs (#1800). A window folded by
|
||||
/// an earlier release unfolds when Preferences opens.
|
||||
|
||||
Reference in New Issue
Block a user