mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
Merge pull request #9481 from dotCooCoo/fix/text-styles-row-menu-document
Dismiss Text Styles row menus when documents change
This commit is contained in:
2 files changed
+54
-2
No files matched your search
@@ -66,6 +66,8 @@ impl StylesPanels {
|
||||
pub(crate) struct StylesPanel {
|
||||
/// The uid of the document the highlight was made in.
|
||||
document: Option<u64>,
|
||||
/// Changes when the panel switches documents, so reopening a document cannot restore its old popup.
|
||||
scope_generation: u64,
|
||||
/// The highlighted styles, in the order they were picked.
|
||||
highlight: Vec<String>,
|
||||
/// The selection's lead style.
|
||||
@@ -83,7 +85,7 @@ fn highlighted(app: &mut VectorcraftApp, kind: Kind, listed: &[&str]) -> Vec<Str
|
||||
let document = app.session.active().map(|st| st.uid);
|
||||
let panel = app.ui.text_styles.of(kind);
|
||||
if panel.document.is_some_and(|made| Some(made) != document) {
|
||||
*panel = StylesPanel::default();
|
||||
*panel = StylesPanel { scope_generation: panel.scope_generation.wrapping_add(1), ..StylesPanel::default() };
|
||||
}
|
||||
panel.document = document;
|
||||
panel.highlight.retain(|n| listed.contains(&n.as_str()));
|
||||
@@ -253,6 +255,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui, kind: Kind) {
|
||||
let names = highlighted(app, kind, &listed);
|
||||
let panel = app.ui.text_styles.of(kind);
|
||||
let (lead, anchor, reveal) = (panel.lead.clone(), panel.anchor.clone(), panel.reveal.take());
|
||||
let scope = (panel.document, panel.scope_generation);
|
||||
let mut sel = Selection::new(names.iter().filter_map(|n| id_of(n)).collect(), lead.as_deref().and_then(id_of), anchor.as_deref().and_then(id_of));
|
||||
let rows: Vec<TextStyleRow> =
|
||||
listed.iter().enumerate().map(|(i, name)| TextStyleRow::new(i as u64, super::label_or_name(name, i == 0))).collect();
|
||||
@@ -271,7 +274,8 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui, kind: Kind) {
|
||||
if let Some(id) = reveal.as_deref().and_then(id_of) {
|
||||
list = list.reveal(id);
|
||||
}
|
||||
let out = list.show(ui);
|
||||
// The framework's row menu and footer drag use list indices that are local to this document.
|
||||
let out = ui.push_id(("text-styles-document", kind.panel_kind(), scope), |ui| list.show(ui)).inner;
|
||||
let name_of = |id: &u64| listed.get(*id as usize).map(|n| n.to_string());
|
||||
let panel = app.ui.text_styles.of(kind);
|
||||
panel.highlight = sel.items.iter().filter_map(name_of).collect();
|
||||
|
||||
@@ -4,6 +4,54 @@ use crate::tests::picks::{answer_dialog, cancel_dialog, open_dialogs, poll, scri
|
||||
use crate::tests::synthetic::key_frame;
|
||||
use vectorcraft_doc::{NodeId, NodeKind};
|
||||
|
||||
#[test]
|
||||
fn switching_documents_dismisses_text_styles_row_menus() {
|
||||
use crate::tests::panel_harness::Panel;
|
||||
use vectorcraft_engine::Session;
|
||||
|
||||
for kind in [Kind::Char, Kind::Para] {
|
||||
let mut app = crate::testing::windowed(VectorcraftApp::new(Session::new(), Default::default()));
|
||||
let command = format!("{}.new", kind.prefix());
|
||||
app.run("file.new", json!({"width": 400, "height": 300})).unwrap();
|
||||
app.run(&command, json!({"name": "First Style"})).unwrap();
|
||||
let first = style_id(&mut app, kind, "First Style");
|
||||
let source = app.session.active().unwrap().uid;
|
||||
app.run("file.new", json!({"width": 400, "height": 300})).unwrap();
|
||||
app.run(&command, json!({"name": "Second Style"})).unwrap();
|
||||
let second = style_id(&mut app, kind, "Second Style");
|
||||
let destination = app.session.active().unwrap().uid;
|
||||
assert_eq!(first, second, "the two documents carry styles at the same list index");
|
||||
app.run("window.previousDocument", json!({})).unwrap();
|
||||
|
||||
let draw: fn(&mut VectorcraftApp, &mut Ui) = match kind {
|
||||
Kind::Char => |app, ui| show(app, ui, Kind::Char),
|
||||
Kind::Para => |app, ui| show(app, ui, Kind::Para),
|
||||
};
|
||||
let mut panel = Panel::new(app, draw);
|
||||
let activate = |panel: &mut Panel, index, document| {
|
||||
panel.app().session.execute("document.activate", &json!({"index": index})).unwrap();
|
||||
panel.frame();
|
||||
panel.frame();
|
||||
assert_eq!(panel.app().session.active().unwrap().uid, document);
|
||||
};
|
||||
let has_copy = |panel: &Panel, name: &str| styles(&mut panel.app(), kind).iter().any(|style| style.0 == name);
|
||||
panel.secondary_click("First Style");
|
||||
assert!(panel.menu_item_enabled("Duplicate Style"));
|
||||
activate(&mut panel, 1, destination);
|
||||
assert!(!panel.shows("Duplicate Style"), "the new document does not inherit the row menu");
|
||||
assert!(!has_copy(&panel, "Second Style copy"));
|
||||
|
||||
activate(&mut panel, 0, source);
|
||||
assert!(!panel.shows("Duplicate Style"), "the old row menu does not reappear");
|
||||
assert!(!has_copy(&panel, "First Style copy"));
|
||||
|
||||
activate(&mut panel, 1, destination);
|
||||
panel.secondary_click("Second Style");
|
||||
panel.click_menu_item("Duplicate Style");
|
||||
assert!(has_copy(&panel, "Second Style copy"));
|
||||
}
|
||||
}
|
||||
|
||||
// PLAN: assumed type-w19 items reachable through `super::*`: `run_action(app, kind, action)`,
|
||||
// `StylesPanel { highlight: Vec<String>, .. }` and `StylesPanels { char, para, made }` at
|
||||
// `app.ui.text_styles`, `highlighted(app, kind, listed: &[&str]) -> Vec<String>`, `TextStyleAction`, and a
|
||||
|
||||
Reference in new issue
Block a user