Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/rustgis-mobile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ fn mark(purpose: &str) -> String {
}
match purpose {
"shortcuts" => rustgis_ui_egui::SHORTCUT_PICK,
"script" => rustgis_ui_egui::SCRIPT_PICK,
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
"text" => rustgis_ui_egui::DELIMITED_PICK,
"shapefile" => rustgis_ui_egui::SHAPEFILE_PICK,
p if p.starts_with(rustgis_ui_egui::TOOL_FILE_PURPOSE) => rustgis_ui_egui::TOOL_FILE_PICK,
Expand Down Expand Up @@ -240,6 +241,7 @@ mod tests {
fn a_dxf_request_keeps_its_id_in_its_mark() {
assert_eq!(super::mark("dxf:7"), format!("{}7:", rustgis_ui_egui::DXF_PICK));
assert_eq!(super::mark("shapefile"), rustgis_ui_egui::SHAPEFILE_PICK);
assert_eq!(super::mark("script"), rustgis_ui_egui::SCRIPT_PICK);
assert_eq!(super::mark("data"), "");
}

Expand Down
26 changes: 26 additions & 0 deletions apps/rustgis-mobile/src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ struct App<P: Phone> {
enum Problem {
/// The shortcut file `name` is over `SHORTCUT_FILE_MAX_BYTES`.
ShortcutTooLarge { name: String },
/// The script file `name`, picked for the Script pane's Load…, is over `SCRIPT_FILE_MAX_BYTES`.
ScriptTooLarge { name: String },
/// The file `name` is over the size read into memory (`rustgis_data::io::MAX_BYTES`), by
/// itself or `together` with the files picked with it and already read.
TooLarge { name: String, together: bool },
Expand Down Expand Up @@ -213,6 +215,7 @@ impl<P: Phone> App<P> {
let limit = rustgis_ui_egui::SHORTCUT_FILE_MAX_BYTES.to_string();
i18n::fmt("{name} wasn't opened: a shortcut file is at most {limit} bytes.", &[("name", &name), ("limit", &limit)])
}
Problem::ScriptTooLarge { name } => i18n::fmt("{file} is larger than 1 MB", &[("file", &name)]),
Problem::TooLarge { name, together } => {
let limit = (rustgis_engine::data::io::MAX_BYTES >> 20).to_string();
let template = if together {
Expand Down Expand Up @@ -352,6 +355,9 @@ fn read_batch(purpose: &str, files: Vec<(String, PathBuf)>, inbox: &Inbox, probl
let size = std::fs::metadata(&path).map(|m| m.len()).unwrap_or(0);
if shortcut && size > rustgis_ui_egui::SHORTCUT_FILE_MAX_BYTES as u64 {
say(Problem::ShortcutTooLarge { name });
} else if mark == rustgis_ui_egui::SCRIPT_PICK && size > rustgis_ui_egui::SCRIPT_FILE_MAX_BYTES as u64 {
// Refused before it is read, as the pane would refuse it after.
say(Problem::ScriptTooLarge { name });
} else if held.saturating_add(size) > rustgis_engine::data::io::MAX_BYTES {
// Refused before it is read: no more than the cap is held in memory at once, in
// one file or in the files picked together.
Expand Down Expand Up @@ -764,4 +770,24 @@ mod tests {
assert_eq!(reading.load(Ordering::Relaxed), 0);
assert_eq!(*inbox.lock().unwrap(), vec![(crate::mark("dxf:7"), Vec::<u8>::new())]);
}

/// A script picked for Load… over the pane's limit is refused before it is read, naming it; one
/// within it reaches the inbox marked for the pane.
#[test]
fn a_script_over_the_limit_is_refused_before_it_is_read() {
let dir = std::env::temp_dir().join(format!("rustgis-mobile-script-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let (big, small) = (dir.join("big.txt"), dir.join("small.txt"));
std::fs::write(&big, vec![b'#'; rustgis_ui_egui::SCRIPT_FILE_MAX_BYTES + 1]).unwrap();
std::fs::write(&small, "map.zoomFull").unwrap();
let (inbox, problems) = (Inbox::default(), Problems::default());
let files = vec![("big.txt".to_string(), big), ("small.txt".to_string(), small)];
read_batch("script", files, &inbox, &problems, &mut 0);
let said = problems.lock().unwrap();
assert!(matches!(said.as_slice(), [Problem::ScriptTooLarge { name }] if name == "big.txt"));
let names: Vec<String> = inbox.lock().unwrap().iter().map(|(n, _)| n.clone()).collect();
assert_eq!(names, [format!("{}small.txt", rustgis_ui_egui::SCRIPT_PICK)]);
// The copies are gone, read or not, and their folder with them (`remove_copy`).
assert!(!dir.exists());
}
}
8 changes: 8 additions & 0 deletions apps/rustgis-web/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,11 @@ async fn read_together(files: Vec<web_sys::File>, mark: &str, inbox: &Inbox, not
.push((rustgis_ui_egui::tl!("Shortcut file exceeds the size limit.").into(), true));
continue;
}
if mark == rustgis_ui_egui::SCRIPT_PICK && file.size() > rustgis_ui_egui::SCRIPT_FILE_MAX_BYTES as f64 {
let text = rustgis_ui_egui::i18n::fmt("{file} is larger than 1 MB", &[("file", &name)]);
notes.lock().unwrap_or_else(|e| e.into_inner()).push((text, true));
continue;
}
if let Some(bytes) = crate::parts::read_file(file, notes.clone()).await {
read.push((name, bytes));
} else if mark.starts_with(rustgis_ui_egui::DXF_PICK) {
Expand Down Expand Up @@ -1250,6 +1255,7 @@ fn services(inbox: Inbox, font_inbox: Inbox, notes: Notes, ctx: egui::Context, e
} else {
match (purpose, tool_exts) {
("shortcuts", _) => rustgis_ui_egui::SHORTCUT_PICK,
("script", _) => rustgis_ui_egui::SCRIPT_PICK,
("text", _) => rustgis_ui_egui::DELIMITED_PICK,
("shapefile", _) => rustgis_ui_egui::SHAPEFILE_PICK,
(_, Some(_)) => rustgis_ui_egui::TOOL_FILE_PICK,
Expand All @@ -1262,6 +1268,8 @@ fn services(inbox: Inbox, font_inbox: Inbox, notes: Notes, ctx: egui::Context, e
if exts.is_empty() { rfd::AsyncFileDialog::new() } else { rfd::AsyncFileDialog::new().add_filter(exts.join(", "), &exts) }
} else if purpose == "shortcuts" {
rfd::AsyncFileDialog::new().add_filter(rustgis_ui_egui::tl!("Keyboard shortcuts"), &["json", "rgisshortcuts"])
} else if purpose == "script" {
rfd::AsyncFileDialog::new().add_filter(rustgis_ui_egui::tl!("Script"), &["txt"])
} else if purpose == "shapefile" {
rfd::AsyncFileDialog::new().add_filter("Shapefile", &["shp", "dbf", "shx", "prj", "cpg", "zip"])
} else if purpose == "project" {
Expand Down
170 changes: 167 additions & 3 deletions crates/ui-egui/src/analysispane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ pub struct Workflow {
pub script_last: Option<ScriptRun>,
/// The last run's Details window is open.
pub script_details: bool,
/// A script file picked with Load… in the browser or on a phone ([`script_picked`]), for
/// [`RustGisApp::logic`] to open in the pane once the inbox is read ([`open_script`]): what
/// keeps its undo needs the frame's context, which `open_picked` hasn't.
pub(crate) script_picked: Option<String>,
/// When each Script pane run under way or waiting started (seconds since 1970, and the
/// instant), and the project it was asked in ([`rustgis_engine::Session::opened`]), in the
/// order asked, for [`script_ran`] to tell as each lands.
Expand Down Expand Up @@ -1851,6 +1855,51 @@ pub(crate) fn open_script(app: &mut RustGisApp, ctx: &egui::Context, script: Str
ctx.memory_mut(|m| m.request_focus(box_id));
}

/// Load…: a script file read into the Script pane in place of its script ([`open_script`]). In
/// the browser and on a phone the shell's picker answers later, through [`script_picked`].
fn load_script(app: &mut RustGisApp, ctx: &egui::Context) {
if let Some(open) = &app.services.open_async {
open("script");
return;
}
let Some(path) = crate::dialogs::pick_open(&["txt"], tl!("Script")) else { return };
// One byte past the limit is enough to refuse it, whatever the file's size.
let read = std::fs::File::open(&path).and_then(|file| {
use std::io::Read;
let mut bytes = Vec::new();
file.take(crate::SCRIPT_FILE_MAX_BYTES as u64 + 1).read_to_end(&mut bytes).map(|_| bytes)
});
let name = std::path::Path::new(&path).file_name().map_or_else(|| path.clone(), |n| n.to_string_lossy().into_owned());
let unread = |e: std::io::Error| crate::i18n::fmt("{path}: {error}", &[("path", &path), ("error", &e.to_string())]);
match read.map_err(unread).and_then(|bytes| script_text(app, &name, &bytes)) {
Ok(script) => open_script(app, ctx, script),
Err(e) => app.session.notify(e, true),
}
}

/// A script file the shell's picker read for Load…, kept for [`RustGisApp::logic`] to open once
/// the inbox is read. A file [`script_text`] refuses is said, and the pane's script stays.
pub(crate) fn script_picked(app: &mut RustGisApp, name: &str, bytes: &[u8]) -> Result<Value, String> {
let script = script_text(app, name, bytes).inspect_err(|e| app.session.notify(e.clone(), true))?;
app.workflow.script_picked = Some(script);
Ok(Value::Null)
}

/// Script file `name`'s text for the Script pane, without the byte-order mark some editors
/// start a UTF-8 file with (it would make the first line's command unknown): refused when it is
/// over [`crate::SCRIPT_FILE_MAX_BYTES`] or isn't UTF-8 text, or while a run is under way (its
/// results would land in the replaced script).
fn script_text(app: &RustGisApp, name: &str, bytes: &[u8]) -> Result<String, String> {
if script_running(app) {
return Err(tl!("Wait for the run to finish").to_string());
}
if bytes.len() > crate::SCRIPT_FILE_MAX_BYTES {
return Err(crate::i18n::fmt("{file} is larger than 1 MB", &[("file", name)]));
}
let bytes = bytes.strip_prefix(b"\xEF\xBB\xBF").unwrap_or(bytes);
std::str::from_utf8(bytes).map(str::to_owned).map_err(|_| crate::i18n::fmt("{file} isn't UTF-8 text", &[("file", name)]))
}

/// The Script pane emptied: its script, results and count, so the next run is `Run #1`, and the
/// Details window with the run it showed. Clear does it, and another project opening.
fn clear_script(app: &mut RustGisApp) {
Expand Down Expand Up @@ -2027,11 +2076,20 @@ fn script(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens, fit: bool) {
if ui.button(tl!("Commands…")).on_hover_text(tl!("Search every command and its parameters")).clicked() {
let _ = app.run("ui.commandSearch", Value::Null);
}
// Not while a run is under way: its results would land in the emptied pane.
// Not while a run is under way: its results would land in the replaced or emptied pane.
let running = script_running(app);
let undo = if mac { "⌘+Z" } else { "Ctrl+Z" };
let mut load = ui
.add_enabled(!running, egui::Button::new(tl!("Load…")))
.on_hover_text(crate::i18n::fmt("Read a script from a file in place of this one ({keys} brings it back)", &[("keys", undo)]));
if running {
load = load.on_disabled_hover_text(tl!("Wait for the run to finish"));
}
if load.clicked() {
load_script(app, ui.ctx());
}
let w = &app.workflow;
let running = !w.script_started.is_empty();
let anything = !w.script.is_empty() || !w.script_out.is_empty() || w.script_last.is_some();
let undo = if mac { "⌘+Z" } else { "Ctrl+Z" };
let mut clear = ui
.add_enabled(anything && !running, egui::Button::new(tl!("Clear")))
.on_hover_text(crate::i18n::fmt("Empty the script and its results ({keys} brings the script back)", &[("keys", undo)]));
Expand Down Expand Up @@ -2498,6 +2556,112 @@ mod tests {
assert!(!on(&h));
}

/// Load… reads a script file in place of the pane's script, emptying its results as Clear
/// does, and Ctrl+Z brings the old script back; a cancelled picker and a file that isn't UTF-8
/// text leave it (the latter said), and it is off while a run is under way.
#[test]
fn load_reads_a_script_file_in_place_of_the_panes() {
let dir = std::env::temp_dir().join(format!("rustgis-script-load-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let (good, bad) = (dir.join("zoom.txt"), dir.join("latin1.txt"));
std::fs::write(&good, "map.zoomFull\n").unwrap();
std::fs::write(&bad, b"map.new {\"name\": \"Espa\xf1a\"}").unwrap();
let mut app = RustGisApp::with_sample();
app.workflow.script = "kept".into();
script_ran(&mut app, crate::Landed::Ran(Ok(json!({"ran": [{"line": 1, "command": "map.zoomFull", "result": null}]}))));
let mut fonts = false;
let mut h = egui_kittest::Harness::builder().with_size(vec2(900.0, 800.0)).build_ui_state(
move |ui, app: &mut RustGisApp| {
if !fonts {
crate::theme::install_fonts(ui.ctx(), &[], None);
fonts = true;
return;
}
script_pane(app, ui, &Tokens::light());
},
app,
);
h.run();
let load = |h: &mut egui_kittest::Harness<'_, RustGisApp>, path: Option<&std::path::Path>| {
crate::dialogs::PICKED.with(|c| *c.borrow_mut() = path.map(|p| p.display().to_string()));
h.get_by_label("Load…").click();
h.run();
};
load(&mut h, None);
assert_eq!(h.state().workflow.script, "kept", "a cancelled picker changes nothing");
assert!(h.state().workflow.script_last.is_some());
load(&mut h, Some(&bad));
assert_eq!(h.state().workflow.script, "kept", "a file that isn't UTF-8 leaves the script");
assert!(h.state().workflow.script_last.is_some());
assert!(h.state().session.notices.iter().any(|n| n.text == "latin1.txt isn't UTF-8 text"), "{:?}", h.state().session.notices);
load(&mut h, Some(&good));
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
let w = &h.state().workflow;
assert_eq!(w.script, "map.zoomFull\n");
assert!(w.script_out.is_empty() && w.script_last.is_none(), "the results go, as with Clear");
let id = egui::Id::unique("script_text");
let state = egui::widgets::text_edit::TextEditState::load(&h.ctx, id).expect("the box's state");
let at = state.cursor.char_range().expect("a cursor");
assert_eq!(
state.undoer().undo(&(at, "map.zoomFull\n".to_string())).map(|(_, t)| t.as_str()),
Some("kept"),
"Ctrl+Z brings it back"
);
// A run under way: off, as its results would land in the replaced script.
h.state_mut().workflow.script_started.push_back((0, web_time::Instant::now(), 0));
h.run();
assert!(egui_kittest::kittest::NodeT::accesskit_node(&h.get_by_label("Load…")).is_disabled());
std::fs::remove_dir_all(dir).unwrap();
}

/// In the browser and on a phone, Load… asks the shell's `"script"` picker; its file opens in
/// the pane on the next frame, a cancelled one (an empty answer) changes nothing, and one
/// landing while a run is under way is refused and said.
#[test]
fn a_script_from_the_shells_picker_opens_in_the_pane() {
use std::sync::{Arc, Mutex};
let mut app = RustGisApp::with_sample();
app.workflow.script = "kept".into();
let asked = Arc::new(Mutex::new(Vec::<String>::new()));
let ask = asked.clone();
app.services.open_async = Some(Box::new(move |purpose| ask.lock().unwrap().push(purpose.to_string())));
let mut fonts = false;
let mut h = egui_kittest::Harness::builder().with_size(vec2(900.0, 800.0)).build_ui_state(
move |ui, app: &mut RustGisApp| {
if !fonts {
crate::theme::install_fonts(ui.ctx(), &[], None);
fonts = true;
return;
}
app.logic(ui.ctx());
script_pane(app, ui, &Tokens::light());
},
app,
);
h.run();
h.get_by_label("Load…").click();
h.run();
assert_eq!(*asked.lock().unwrap(), ["script"]);
h.state_mut().open_picked(crate::SCRIPT_PICK, &[]).unwrap();
h.run();
assert_eq!(h.state().workflow.script, "kept", "a cancelled picker changes nothing");
assert!(h.state_mut().open_picked(&format!("{}bad.txt", crate::SCRIPT_PICK), b"\xff").is_err());
let big = vec![b'#'; crate::SCRIPT_FILE_MAX_BYTES + 1];
assert!(h.state_mut().open_picked(&format!("{}big.txt", crate::SCRIPT_PICK), &big).is_err());
assert!(h.state().session.notices.iter().any(|n| n.text == "big.txt is larger than 1 MB"));
h.run();
assert_eq!(h.state().workflow.script, "kept");
// A byte-order mark (Notepad's UTF-8) isn't kept: the first line's command stays known.
h.state_mut().open_picked(&format!("{}zoom.txt", crate::SCRIPT_PICK), b"\xEF\xBB\xBFmap.zoomFull").unwrap();
h.run();
assert_eq!(h.state().workflow.script, "map.zoomFull");
assert_eq!(h.state().ui.right, Some(crate::RightPane::Script));
h.state_mut().workflow.script_started.push_back((0, web_time::Instant::now(), 0));
assert!(h.state_mut().open_picked(&format!("{}other.txt", crate::SCRIPT_PICK), b"map.new").is_err());
h.run();
assert_eq!(h.state().workflow.script, "map.zoomFull", "not while a run is under way");
assert!(h.state().session.notices.iter().any(|n| n.text == "Wait for the run to finish"), "{:?}", h.state().session.notices);
}

/// Details… opens the last run in a window of its own, each line folded: a result the pane cuts
/// shows whole there, laid out to read, Copy copies it and Copy All the run; a run that stopped
/// shows why.
Expand Down
19 changes: 18 additions & 1 deletion crates/ui-egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ pub const TOOL_FILE_PURPOSE: &str = "toolfile:";
/// Marks a returned shortcut picker file; cancellation leaves no pending routing state.
pub const SHORTCUT_PICK: &str = "\u{1}shortcuts:";

/// Starts the name of a script file the shell's `"script"` picker put in the inbox, for the
/// Script pane's Load….
pub const SCRIPT_PICK: &str = "\u{1}script:";

/// The largest script file the Script pane's Load… reads, as the assistant's command file; the
/// browser's shell refuses a larger one before reading it.
pub const SCRIPT_FILE_MAX_BYTES: usize = 1024 * 1024;

/// Maximum portable shortcut document size: saved overrides plus room for version metadata.
pub const SHORTCUT_FILE_MAX_BYTES: usize = keymap::MAX_PREF_BYTES + 100;

Expand Down Expand Up @@ -2028,12 +2036,17 @@ impl RustGisApp {
/// Opens a file from the inbox: one the `"text"` picker marked ([`DELIMITED_PICK`]) opens Add
/// Delimited Text for it, or is read with a script's options; one the tool file picker marked
/// ([`TOOL_FILE_PICK`]) goes to the Tools pane. Source-CRS picks carry [`SHAPEFILE_PICK`];
/// shortcut picks carry [`SHORTCUT_PICK`] and open the validated merge preview; DXF picks
/// shortcut picks carry [`SHORTCUT_PICK`] and open the validated merge preview; script picks
/// carry [`SCRIPT_PICK`] and open in the Script pane; DXF picks
/// carry [`DXF_PICK`] and open Import DXF.
pub fn open_picked(&mut self, name: &str, bytes: &[u8]) -> Result<Value, String> {
if name.starts_with(SHORTCUT_PICK) {
return shortcutfile::picked(self, bytes);
}
if let Some(name) = name.strip_prefix(SCRIPT_PICK) {
// An answer with no name is a cancelled picker.
return if name.is_empty() { Ok(Value::Null) } else { analysispane::script_picked(self, name, bytes) };
}
if let Some(name) = name.strip_prefix(TOOL_FILE_PICK) {
panes::received_file(self, name, bytes);
return Ok(Value::Null);
Expand Down Expand Up @@ -6528,6 +6541,10 @@ impl RustGisApp {
for (name, bytes) in arrived {
let _ = self.open_picked(&name, &bytes);
}
// A script file for the Script pane's Load…, opened with the context its undo needs.
if let Some(script) = self.workflow.script_picked.take() {
analysispane::open_script(self, ctx, script);
}
// Files dropped on the desktop window arrive as paths. (The web shell reads browser drops
// asynchronously and passes them through the inbox instead.)
if !cfg!(target_arch = "wasm32") {
Expand Down
Loading
Loading