mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-17 10:46:46 +00:00
Packaging Tools: each document keeps its own Check Fold Lines result
The Fold Lines panel kept one check for the whole app. Checking a second document replaced the first document's result, so switching back to the first document, unchanged since its check, showed no issues. The panel now keeps the last check of each document by its id, and each result is still shown only while its document has not changed since. Results of documents that changed or closed are dropped when the next check is stored.
This commit is contained in:
@@ -4,8 +4,9 @@
|
||||
//! paths that role (`addon.packaging.setRole`). Its menu's Role Swatches… picks the swatch that
|
||||
//! carries a role (`addon.packaging.setRoleSwatch`). Its Check button, like the Check Fold Lines menu
|
||||
//! item, checks the fold lines (`addon.packaging.check`) and lists the issues found under it, each
|
||||
//! with a Select button; the list is kept until the document changes.
|
||||
//! with a Select button; each document's list is kept until that document changes.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Weak};
|
||||
|
||||
use egui::{Align, Layout, Sense, Ui, vec2};
|
||||
@@ -31,7 +32,7 @@ pub const ROLES: &[(&str, &str)] =
|
||||
|
||||
/// Where the panel keeps the last `addon.packaging.roles` answer (panel state).
|
||||
const STATE: &str = "fold-lines";
|
||||
/// Where the panel keeps the last check (panel state).
|
||||
/// Where the panel keeps the last check of each document (panel state, a [`Checks`]).
|
||||
const CHECKED: &str = "fold-lines-check";
|
||||
|
||||
/// Object › Plug-ins › Packaging Tools.
|
||||
@@ -103,6 +104,10 @@ impl Checked {
|
||||
}
|
||||
}
|
||||
|
||||
/// The last check of each document, by the document's id. The checks are shared, so reading the
|
||||
/// map for a frame copies no issue lists.
|
||||
type Checks = HashMap<u64, Arc<Checked>>;
|
||||
|
||||
/// The active document's [`DocKey`].
|
||||
fn doc_key(app: &VectorcraftApp) -> Option<DocKey> {
|
||||
app.session.active().map(|st| (st.uid, Arc::downgrade(&st.doc)))
|
||||
@@ -110,11 +115,17 @@ fn doc_key(app: &VectorcraftApp) -> Option<DocKey> {
|
||||
|
||||
/// Runs `addon.packaging.check` with `p` and shows its summary in the status bar → the check, kept
|
||||
/// in `ctx` for the panel.
|
||||
fn run_check(app: &mut VectorcraftApp, ctx: &egui::Context, p: Value) -> Option<Checked> {
|
||||
fn run_check(app: &mut VectorcraftApp, ctx: &egui::Context, p: Value) -> Option<Arc<Checked>> {
|
||||
let r = app.run(CHECK, p).map_err(|e| app.status(e)).ok()?;
|
||||
let checked = Checked::of(doc_key(app)?, &r);
|
||||
let key = doc_key(app)?;
|
||||
let uid = key.0;
|
||||
let checked = Arc::new(Checked::of(key, &r));
|
||||
app.status(checked.summary());
|
||||
set_pstate(ctx, CHECKED, checked.clone());
|
||||
let mut checks: Checks = pstate(ctx, CHECKED);
|
||||
// A check of a document that has since changed or closed can never be shown again.
|
||||
checks.retain(|_, c| c.key.as_ref().is_some_and(|(_, doc)| doc.strong_count() > 0));
|
||||
checks.insert(uid, Arc::clone(&checked));
|
||||
set_pstate(ctx, CHECKED, checks);
|
||||
Some(checked)
|
||||
}
|
||||
|
||||
@@ -130,11 +141,13 @@ pub(crate) fn check(app: &mut VectorcraftApp, p: Value) {
|
||||
}
|
||||
|
||||
/// The last check of the active document, while the document has not changed since.
|
||||
fn last_check(app: &VectorcraftApp, ctx: &egui::Context) -> Option<Checked> {
|
||||
let checked: Checked = pstate(ctx, CHECKED);
|
||||
let same = match (&checked.key, doc_key(app)) {
|
||||
(Some((uid, doc)), Some((now, current))) => *uid == now && doc.ptr_eq(¤t),
|
||||
_ => false,
|
||||
fn last_check(app: &VectorcraftApp, ctx: &egui::Context) -> Option<Arc<Checked>> {
|
||||
let (now, current) = doc_key(app)?;
|
||||
let mut checks: Checks = pstate(ctx, CHECKED);
|
||||
let checked = checks.remove(&now)?;
|
||||
let same = match &checked.key {
|
||||
Some((uid, doc)) => *uid == now && doc.ptr_eq(¤t),
|
||||
None => false,
|
||||
};
|
||||
same.then_some(checked)
|
||||
}
|
||||
|
||||
@@ -212,6 +212,28 @@ fn the_check_button_checks_and_clicking_an_issue_selects_its_lines() {
|
||||
assert!(text.contains("No issues found.") && text.contains("Panels: 2, hinges: 1, errors: 0, warnings: 0"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn each_document_keeps_its_own_check() {
|
||||
crate::i18n::set_current(crate::i18n::Lang::EN);
|
||||
let (mut app, _) = blank();
|
||||
let ctx = egui::Context::default();
|
||||
let text = frames(&mut app, &ctx, Some("Check"));
|
||||
assert!(text.contains("A cut line ends without meeting another cut line."), "{text}");
|
||||
// A second document, checked too: it has no cut lines.
|
||||
app.run("file.new", json!({"width": 100, "height": 100})).unwrap();
|
||||
let text = frames(&mut app, &ctx, Some("Check"));
|
||||
assert!(text.contains("No cut lines are in the document.") && !text.contains("A cut line ends"), "{text}");
|
||||
// Back on the first document, unchanged since its check: its issues are still listed.
|
||||
assert!(app.session.set_active(0));
|
||||
let text = frames(&mut app, &ctx, None);
|
||||
assert!(text.contains("A cut line ends without meeting another cut line."), "{text}");
|
||||
assert!(text.contains("Panels: 2, hinges: 1, errors: 1, warnings: 0"), "{text}");
|
||||
// And the second document's check stays its own.
|
||||
assert!(app.session.set_active(1));
|
||||
let text = frames(&mut app, &ctx, None);
|
||||
assert!(text.contains("No cut lines are in the document.") && !text.contains("A cut line ends"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn issues_and_the_summary_show_in_the_interface_language() {
|
||||
let (mut app, _) = blank();
|
||||
|
||||
Reference in New Issue
Block a user