mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
Merge pull request #9598 from dotCooCoo/fix/library-panel-add-requires-drawing
Require an open drawing when adding items from a Library panel
This commit is contained in:
2 files changed
+53
-8
No files matched your search
@@ -180,7 +180,6 @@ pub(crate) struct Kind {
|
||||
find: fn(&VectorcraftApp, &str) -> Found,
|
||||
path: fn(&VectorcraftApp, &str) -> Option<String>,
|
||||
neighbour: fn(&VectorcraftApp, &str, isize) -> Option<String>,
|
||||
add: fn(&mut VectorcraftApp, &str, Vec<String>),
|
||||
add_label: &'static str,
|
||||
body: fn(&mut VectorcraftApp, &mut Ui, &str),
|
||||
}
|
||||
@@ -195,7 +194,6 @@ fn kind_of<K: LibraryKind>() -> Kind {
|
||||
find: K::find,
|
||||
path: K::path,
|
||||
neighbour: neighbour::<K>,
|
||||
add: K::add,
|
||||
add_label: K::ADD,
|
||||
body: body::<K>,
|
||||
}
|
||||
@@ -213,6 +211,21 @@ fn kind(kind: &str) -> Option<Kind> {
|
||||
.find(|k| k.kind == kind)
|
||||
}
|
||||
|
||||
/// The document command each library's Add action runs.
|
||||
fn add_command(kind: &str) -> Option<&'static str> {
|
||||
match kind {
|
||||
"swatches" => Some("swatch.library.add"),
|
||||
"brushes" => Some("brush.addFromLibrary"),
|
||||
"symbols" => Some("symbol.addFromLibrary"),
|
||||
"graphicStyles" => Some("graphicStyle.addFromLibrary"),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn can_add(app: &VectorcraftApp, kind: &str) -> bool {
|
||||
add_command(kind).is_some_and(|command| crate::menus::enabled(app, command))
|
||||
}
|
||||
|
||||
/// The id of the panel of library `id` of `kind` (`library:swatches:pastels`).
|
||||
pub fn panel_id(kind: &str, id: &str) -> String {
|
||||
format!("{PREFIX}{kind}:{id}")
|
||||
@@ -662,7 +675,7 @@ fn body<K: LibraryKind>(app: &mut VectorcraftApp, ui: &mut Ui, id: &str) {
|
||||
let title = library_name(id, &name).to_string();
|
||||
// The footer stays under the browser, which scrolls in the room left above it.
|
||||
let footer_out = RefCell::new(None);
|
||||
let any = !selection.items.is_empty();
|
||||
let any = !selection.items.is_empty() && can_add(app, K::KIND);
|
||||
let mut shown = Panel::new(title.clone())
|
||||
.collection_body()
|
||||
.footer(|ui, _| *footer_out.borrow_mut() = Some(footer_bar::<K>(any).show_footer(ui)))
|
||||
@@ -703,8 +716,8 @@ fn body<K: LibraryKind>(app: &mut VectorcraftApp, ui: &mut Ui, id: &str) {
|
||||
}
|
||||
}
|
||||
|
||||
/// The panel's footer: the libraries menu, previous / next library, and Add to … , on while `any` item or folder is
|
||||
/// selected.
|
||||
/// The panel's footer: the libraries menu, previous / next library, and Add to … , on while items or folders are
|
||||
/// selected and the document can receive them.
|
||||
fn footer_bar<K: LibraryKind>(any: bool) -> FooterBar {
|
||||
let icon = |name: &'static str| IconSpec::svg(IconId::new(name));
|
||||
FooterBar::actions([
|
||||
@@ -782,7 +795,7 @@ fn menu_entries(app: &VectorcraftApp, panel: &str) -> Vec<MenuEntry> {
|
||||
params["panel"] = json!(panel);
|
||||
MenuEntry::Row { command: CommandRef::with_params(command, params), label, enabled, checked }
|
||||
};
|
||||
let has_selection = !selected(app, panel).items.is_empty();
|
||||
let has_selection = !selected(app, panel).items.is_empty() && can_add(app, k.kind);
|
||||
let mut out = vec![row("ui.libraryPanel.add", json!({}), k.add_label, has_selection, None), MenuEntry::Separator];
|
||||
out.extend(View::ALL.iter().map(|&(v, label)| row("ui.libraryPanel", json!({ "view": v }), label, true, Some(v == view))));
|
||||
out.extend([
|
||||
@@ -831,7 +844,7 @@ pub(crate) fn menu_states(app: &VectorcraftApp, panel: &str, states: &mut Comman
|
||||
}
|
||||
|
||||
/// Runs a library panel command that the engine does not know: `ui.libraryPanel.add {panel}` adds the
|
||||
/// panel's selection to the document.
|
||||
/// panel's selection to the document. A refused addition returns its command's error.
|
||||
pub(crate) fn run(app: &mut VectorcraftApp, id: &str, params: &Value) -> Result<Value, String> {
|
||||
if id != "ui.libraryPanel.add" {
|
||||
return Err(format!("`{id}` isn't a library panel command"));
|
||||
@@ -839,7 +852,8 @@ pub(crate) fn run(app: &mut VectorcraftApp, id: &str, params: &Value) -> Result<
|
||||
let given = params.get("panel").and_then(Value::as_str).unwrap_or_default();
|
||||
let (k, lib) = parse(given).ok_or_else(|| format!("`{given}` isn't a library panel (see window.swatchLibrary)"))?;
|
||||
let names: Vec<String> = selected(app, given).items.into_iter().map(|key| key.name).collect();
|
||||
(k.add)(app, lib, names);
|
||||
let command = add_command(k.kind).ok_or("this library cannot add items to a drawing")?;
|
||||
app.run(command, json!({ "library": lib, "names": names }))?;
|
||||
Ok(Value::Null)
|
||||
}
|
||||
|
||||
|
||||
@@ -182,6 +182,37 @@ fn modifier_clicks_select_and_add_to_swatches_adds_the_selection() {
|
||||
assert!(selected_names(&app, "library:swatches:brights").is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn adding_selected_library_items_requires_an_open_drawing() {
|
||||
let mut app = app();
|
||||
let ctx = context();
|
||||
app.run("window.swatchLibrary", json!({"library": "earth-tones"})).unwrap();
|
||||
let panel = "library:swatches:earth-tones";
|
||||
click(&mut app, &ctx, "Clay 3", Modifiers::COMMAND, 0.0);
|
||||
assert_eq!(selected_names(&app, panel), ["Clay 3"]);
|
||||
let draw = |app: &mut VectorcraftApp, ui: &mut Ui| show(app, ui, panel);
|
||||
let command = CommandRef::with_params("ui.libraryPanel.add", json!({"panel": panel}));
|
||||
let menu_enabled = |app: &VectorcraftApp| {
|
||||
let mut states = CommandStates::new();
|
||||
menu_states(app, panel, &mut states);
|
||||
states.get(&command).is_some_and(CommandState::is_enabled)
|
||||
};
|
||||
assert!(control(&mut app, &ctx, &draw, "Add to Swatches").0);
|
||||
assert!(menu_enabled(&app));
|
||||
|
||||
app.session.execute("file.close", &json!({"index": 0})).unwrap();
|
||||
assert!(!control(&mut app, &ctx, &draw, "Add to Swatches").0);
|
||||
assert!(!menu_enabled(&app));
|
||||
assert_eq!(selected_names(&app, panel), ["Clay 3"], "the library remains browsable");
|
||||
assert!(app.run("ui.libraryPanel.add", json!({"panel": panel})).is_err());
|
||||
|
||||
app.run("file.new", json!({"width": 100, "height": 100})).unwrap();
|
||||
assert!(control(&mut app, &ctx, &draw, "Add to Swatches").0);
|
||||
assert!(menu_enabled(&app));
|
||||
app.run("ui.libraryPanel.add", json!({"panel": panel})).unwrap();
|
||||
assert!(app.session.active().unwrap().doc.swatch("Clay 3").is_some());
|
||||
}
|
||||
|
||||
const SWATCH_IDS: [&str; 9] = [
|
||||
"web-safe-216",
|
||||
"grays-neutrals",
|
||||
|
||||
Reference in new issue
Block a user