mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 21:42:59 -05:00
Merge pull request #10381 from dotCooCoo/fix/attributes-drafts-follow-selection
Attributes: an unfinished URL or note stays with the objects it was typed for
This commit is contained in:
1 file changed
+122
-2
@@ -116,6 +116,17 @@ fn info(app: &mut VectorcraftApp) -> (AttributesInfo, bool) {
|
||||
v
|
||||
}
|
||||
|
||||
/// What the URL and Note fields edit, as their edit target: the document, how many times File › Revert replaced its
|
||||
/// content, and the selected objects (or the object targeted in the Layers panel). A field drops an unfinished value
|
||||
/// when this changes, even where the new objects hold the same text.
|
||||
type EditTarget = Option<(u64, u64, Vec<NodeId>)>;
|
||||
|
||||
/// The [`EditTarget`] of the active document.
|
||||
fn edit_target(app: &VectorcraftApp) -> EditTarget {
|
||||
let st = app.session.active()?;
|
||||
Some((st.uid, st.reverts, st.selection.subjects().to_vec()))
|
||||
}
|
||||
|
||||
/// A value the targets share, or mixed where they differ (`None`).
|
||||
fn or_mixed<T>(v: Option<T>) -> Mixed<T> {
|
||||
v.map_or(Mixed::Mixed, Mixed::Same)
|
||||
@@ -139,6 +150,7 @@ fn pair<T: Valued>(ui: &mut Ui, name: &'static str, enabled: bool, current: Opti
|
||||
pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
let (info, paths) = info(app);
|
||||
let has = !info.ids.is_empty();
|
||||
let target = edit_target(app);
|
||||
// (command, params) for each edit of this pass; errors show in the status bar through `run`.
|
||||
let mut acts: Vec<(&'static str, Value)> = Vec::new();
|
||||
Row::new().show(ui, |ui, _| {
|
||||
@@ -183,7 +195,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
let recent: Vec<Entry<String>> = menu_urls(app).iter().map(|u| Entry::new(u.clone(), u.clone())).collect();
|
||||
let name = side_name(tl!("URL:"));
|
||||
Field::new(name.clone()).show(ui, |ui, _| {
|
||||
let edit = ComboBox::new(name.clone(), Bind::given(or_mixed(info.url.clone())), &recent).enabled(has).show(ui);
|
||||
let edit = ComboBox::new(name.clone(), Bind::given(or_mixed(info.url.clone())), &recent).enabled(has).edit_target(&target).show(ui);
|
||||
acts.extend(edit.edit.events.committed().map(|u| ("attributes.set", json!({ "url": u }))));
|
||||
});
|
||||
}
|
||||
@@ -191,7 +203,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
Divider::horizontal().show(ui);
|
||||
let name = side_name(tl!("Note:"));
|
||||
Field::new(name.clone()).layout(FieldLayout::Above).show(ui, |ui, _| {
|
||||
let edit = TextField::new(name.clone(), Bind::given(or_mixed(info.note.clone()))).lines(3).enabled(has).show(ui);
|
||||
let edit = TextField::new(name.clone(), Bind::given(or_mixed(info.note.clone()))).lines(3).enabled(has).edit_target(&target).show(ui);
|
||||
acts.extend(edit.events.committed().map(|n| ("attributes.set", json!({ "note": n }))));
|
||||
});
|
||||
}
|
||||
@@ -302,6 +314,114 @@ mod tests {
|
||||
assert!(open_url(&mut panel.app(), &json!({})).is_err());
|
||||
}
|
||||
|
||||
/// Two rectangles with the same URL and note, the first selected: the app and both ids.
|
||||
fn twins() -> (VectorcraftApp, u64, u64) {
|
||||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||||
run(&mut app, "file.new", json!({"width": 100, "height": 100}));
|
||||
let a = run(&mut app, "shape.rectangle", json!({"x": 0, "y": 0, "width": 30, "height": 30}))["id"].as_u64().unwrap();
|
||||
let b = run(&mut app, "shape.rectangle", json!({"x": 50, "y": 0, "width": 30, "height": 30}))["id"].as_u64().unwrap();
|
||||
run(&mut app, "attributes.set", json!({"ids": [a, b], "url": "https://example.com/same", "note": "same note"}));
|
||||
run(&mut app, "select.set", json!({"ids": [a]}));
|
||||
(app, a, b)
|
||||
}
|
||||
|
||||
/// The URL and note of the object `id`.
|
||||
fn url_and_note(panel: &Panel, id: u64) -> (Option<String>, Option<String>) {
|
||||
let info = panel.app().session.execute("attributes.info", &json!({"ids": [id]})).unwrap();
|
||||
(info["url"].as_str().map(str::to_owned), info["note"].as_str().map(str::to_owned))
|
||||
}
|
||||
|
||||
/// Selects `field` and types `text` over its value, leaving it unfinished.
|
||||
fn unfinished(panel: &mut Panel, field: &str, text: &str) {
|
||||
panel.focus(field);
|
||||
panel.key(egui::Key::A, egui::Modifiers::COMMAND);
|
||||
panel.type_text(text);
|
||||
assert_eq!(panel.value(field).as_deref(), Some(text), "the {field} field shows the unfinished text");
|
||||
}
|
||||
|
||||
/// Two frames: the fields' nodes read the values of a new edit target from the second frame after it changes.
|
||||
fn settle(panel: &mut Panel) {
|
||||
panel.frame();
|
||||
panel.frame();
|
||||
}
|
||||
|
||||
/// A point of the panel below its fields, where a click only takes the focus away.
|
||||
fn below(panel: &Panel) -> egui::Pos2 {
|
||||
panel.rect("Note").center_bottom() + egui::vec2(0.0, 40.0)
|
||||
}
|
||||
|
||||
const SAME: (Option<&str>, Option<&str>) = (Some("https://example.com/same"), Some("same note"));
|
||||
|
||||
fn same(v: &(Option<String>, Option<String>)) -> (Option<&str>, Option<&str>) {
|
||||
(v.0.as_deref(), v.1.as_deref())
|
||||
}
|
||||
|
||||
/// An unfinished URL or note belongs to the object it was typed for: after the selection moves to an object with
|
||||
/// the same URL and note, the field shows that object's value, and Enter or a click elsewhere changes neither
|
||||
/// object. Typed for the object still selected, the text applies and the field keeps the focus until it is left.
|
||||
#[test]
|
||||
fn unfinished_url_and_note_stay_with_the_objects_they_were_typed_for() {
|
||||
let (app, a, b) = twins();
|
||||
let mut panel = Panel::new(app, show);
|
||||
panel.room(egui::vec2(320.0, 480.0));
|
||||
panel.frame();
|
||||
|
||||
unfinished(&mut panel, "URL", "https://example.com/typed");
|
||||
run(&mut panel.app(), "select.set", json!({"ids": [b]}));
|
||||
settle(&mut panel);
|
||||
assert_eq!(panel.value("URL").as_deref(), Some("https://example.com/same"), "the URL field shows the new object's URL");
|
||||
panel.key(egui::Key::Enter, egui::Modifiers::NONE);
|
||||
assert_eq!((same(&url_and_note(&panel, a)), same(&url_and_note(&panel, b))), (SAME, SAME), "Enter commits the old URL to neither object");
|
||||
|
||||
unfinished(&mut panel, "Note", "typed note");
|
||||
run(&mut panel.app(), "select.set", json!({"ids": [a]}));
|
||||
settle(&mut panel);
|
||||
assert_eq!(panel.value("Note").as_deref(), Some("same note"), "the Note field shows the new object's note");
|
||||
let at = below(&panel);
|
||||
panel.click_at(at);
|
||||
assert_eq!(
|
||||
(same(&url_and_note(&panel, a)), same(&url_and_note(&panel, b))),
|
||||
(SAME, SAME),
|
||||
"a click elsewhere commits the old note to neither object"
|
||||
);
|
||||
|
||||
// Typed for the object that stays selected, the text applies.
|
||||
unfinished(&mut panel, "Note", "kept note");
|
||||
panel.frame();
|
||||
assert!(panel.focused("Note"), "the field keeps the focus while the selection stays");
|
||||
let at = below(&panel);
|
||||
panel.click_at(at);
|
||||
assert_eq!(url_and_note(&panel, a).1.as_deref(), Some("kept note"));
|
||||
unfinished(&mut panel, "URL", "https://example.com/kept");
|
||||
panel.key(egui::Key::Enter, egui::Modifiers::NONE);
|
||||
assert_eq!(url_and_note(&panel, a).0.as_deref(), Some("https://example.com/kept"));
|
||||
assert_eq!(same(&url_and_note(&panel, b)), SAME);
|
||||
}
|
||||
|
||||
/// File › Revert drops an unfinished note: the restored object keeps its id, URL and note, and a click elsewhere
|
||||
/// leaves the note as the file has it.
|
||||
#[test]
|
||||
fn file_revert_drops_an_unfinished_note() {
|
||||
let (mut app, a, _) = twins();
|
||||
let dir = vectorcraft_testkit::TempDir::new("attributes-revert");
|
||||
app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string())));
|
||||
app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string())));
|
||||
app.run("file.saveAs", json!({"path": dir.join("Art.vectorcraft")})).unwrap();
|
||||
run(&mut app, "shape.ellipse", json!({"x": 0, "y": 50, "width": 30, "height": 30}));
|
||||
run(&mut app, "select.set", json!({"ids": [a]}));
|
||||
let mut panel = Panel::new(app, show);
|
||||
panel.room(egui::vec2(320.0, 480.0));
|
||||
panel.frame();
|
||||
unfinished(&mut panel, "Note", "typed note");
|
||||
panel.app().run("file.revert", json!({"confirmed": true})).unwrap();
|
||||
run(&mut panel.app(), "select.set", json!({"ids": [a]}));
|
||||
settle(&mut panel);
|
||||
assert_eq!(panel.value("Note").as_deref(), Some("same note"), "the Note field shows the restored note");
|
||||
let at = below(&panel);
|
||||
panel.click_at(at);
|
||||
assert_eq!(same(&url_and_note(&panel, a)), SAME, "the reverted object keeps the file's note");
|
||||
}
|
||||
|
||||
/// Only web and mail URLs reach the browser.
|
||||
#[test]
|
||||
fn the_browser_opens_web_and_mail_urls_only() {
|
||||
|
||||
Reference in new issue
Block a user