mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-25 08:11:41 +00:00
Graphic Styles panel: add item right-click context menu (Options, Duplicate, Delete, Redefine, Break Link)
This commit is contained in:
@@ -24,7 +24,10 @@ use serde_json::{Value, json};
|
||||
use vectorcraft_color::{BlendMode, Color, Paint};
|
||||
use vectorcraft_doc::style_libs::STYLE_LIBRARIES;
|
||||
use vectorcraft_doc::text::{CharStyle, TextObject};
|
||||
use vectorcraft_doc::{Appearance, AppearanceItem, Document, FillLayer, GraphicStyle, Node, NodeId, NodeKind, PatternDef, StrokeLayer, StyleLibrary};
|
||||
use vectorcraft_doc::{
|
||||
Appearance, AppearanceItem, DEFAULT_GRAPHIC_STYLE, Document, FillLayer, GraphicStyle, Node, NodeId, NodeKind,
|
||||
PatternDef, StrokeLayer, StyleLibrary,
|
||||
};
|
||||
use vectorcraft_engine::cmd::stylelib;
|
||||
use vectorcraft_engine::cmd::swatchlib::LibraryInfo;
|
||||
use vectorcraft_geom::{Affine, Point, shapes};
|
||||
@@ -484,21 +487,88 @@ fn zone_input(ui: &Ui, zone: &Response, d: &Document, ev: &mut Events) {
|
||||
}
|
||||
}
|
||||
|
||||
/// An item's right-click context menu in the Graphic Styles panel.
|
||||
fn item_context_menu(app: &mut VectorcraftApp, ui: &mut Ui, name: &str, sel: &[String]) {
|
||||
let is_default = name == DEFAULT_GRAPHIC_STYLE;
|
||||
let is_multi = sel.len() > 1 && sel.contains(&name.to_string());
|
||||
let targets: Vec<String> = if is_multi {
|
||||
let doc = app.session.active().map(|st| st.doc.clone());
|
||||
if let Some(d) = &doc {
|
||||
d.graphic_styles.iter().filter(|g| sel.contains(&g.name)).map(|g| g.name.clone()).collect()
|
||||
} else {
|
||||
sel.to_vec()
|
||||
}
|
||||
} else {
|
||||
vec![name.to_string()]
|
||||
};
|
||||
let doomed: Vec<String> = targets.iter().filter(|s| *s != DEFAULT_GRAPHIC_STYLE).cloned().collect();
|
||||
|
||||
let can_options = !is_default && !is_multi;
|
||||
if menu_item(ui, tl!("Graphic Style Options…"), can_options, false) {
|
||||
if let Err(e) = app.run("ui.graphicStyleOptions", json!({ "name": name })) {
|
||||
app.status(e);
|
||||
}
|
||||
ui.close();
|
||||
}
|
||||
let duplicate_label = if is_multi { tl!("Duplicate Graphic Styles") } else { tl!("Duplicate Graphic Style") };
|
||||
if menu_item(ui, duplicate_label, true, false) {
|
||||
duplicate(app, ui.ctx(), &targets);
|
||||
ui.close();
|
||||
}
|
||||
let delete_label = if is_multi { tl!("Delete Graphic Styles") } else { tl!("Delete Graphic Style") };
|
||||
let can_delete = !is_default && !doomed.is_empty();
|
||||
if menu_item(ui, delete_label, can_delete, false) {
|
||||
let res = if doomed.len() == 1 {
|
||||
app.run("graphicStyle.delete", json!({ "name": doomed[0] }))
|
||||
} else {
|
||||
app.run("graphicStyle.delete", json!({ "names": doomed }))
|
||||
};
|
||||
if res.is_ok() {
|
||||
set_pstate(ui.ctx(), "gs-sel", Vec::<String>::new());
|
||||
}
|
||||
ui.close();
|
||||
}
|
||||
ui.separator();
|
||||
let has_art_sel = selection_len(app) > 0;
|
||||
if menu_item(ui, tl!("Redefine Graphic Style"), has_art_sel, false) {
|
||||
if let Err(e) = app.run("graphicStyle.redefine", json!({ "name": name })) {
|
||||
app.status(e);
|
||||
}
|
||||
ui.close();
|
||||
}
|
||||
let is_linked = app.session.selection_graphic_style().is_some_and(|(g, _)| g.name == name);
|
||||
if menu_item(ui, tl!("Break Link to Graphic Style"), is_linked, false) {
|
||||
if let Err(e) = app.run("graphicStyle.breakLink", json!({})) {
|
||||
app.status(e);
|
||||
}
|
||||
ui.close();
|
||||
}
|
||||
}
|
||||
|
||||
/// The thumbnail grid (Thumbnail View and the Control bar's picker), `sel` outlined.
|
||||
fn tiles(app: &VectorcraftApp, ui: &mut Ui, s: &Shown, sel: &[String], ev: &mut Events) {
|
||||
fn tiles(app: &mut VectorcraftApp, ui: &mut Ui, s: &Shown, sel: &mut Vec<String>, ev: &mut Events, in_panel: bool) {
|
||||
ui.horizontal_wrapped(|ui| {
|
||||
ui.spacing_mut().item_spacing = vec2(3.0, 3.0);
|
||||
for (i, g) in s.d.graphic_styles.iter().enumerate() {
|
||||
let (r, resp) = ui.allocate_exact_size(vec2(TILE, TILE), Sense::click_and_drag());
|
||||
s.paint(app, ui, r, i);
|
||||
outline(ui, r, sel.contains(&g.name), resp.hovered());
|
||||
cell_input(ui, &resp.on_hover_text(&g.name), s.d, i, false, ev);
|
||||
let resp = resp.on_hover_text(&g.name);
|
||||
cell_input(ui, &resp, s.d, i, false, ev);
|
||||
if in_panel {
|
||||
if resp.secondary_clicked() && !sel.contains(&g.name) {
|
||||
*sel = vec![g.name.clone()];
|
||||
set_pstate(ui.ctx(), "gs-anchor", g.name.clone());
|
||||
set_pstate(ui.ctx(), "gs-sel", sel.clone());
|
||||
}
|
||||
resp.context_menu(|ui| item_context_menu(app, ui, &g.name, sel));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// The list views' rows (`row`: height and thumbnail side), `sel` highlighted.
|
||||
fn rows(app: &VectorcraftApp, ui: &mut Ui, s: &Shown, sel: &[String], row: (f32, f32), ev: &mut Events) {
|
||||
fn rows(app: &mut VectorcraftApp, ui: &mut Ui, s: &Shown, sel: &mut Vec<String>, row: (f32, f32), ev: &mut Events) {
|
||||
let t = Tokens::get(ui.ctx());
|
||||
let (h, side) = row;
|
||||
for (i, g) in s.d.graphic_styles.iter().enumerate() {
|
||||
@@ -512,7 +582,14 @@ fn rows(app: &VectorcraftApp, ui: &mut Ui, s: &Shown, sel: &[String], row: (f32,
|
||||
s.paint(app, ui, th, i);
|
||||
ui.painter().rect_stroke(th, 0.0, Stroke::new(1.0, t.border), StrokeKind::Outside);
|
||||
ui.painter().text(pos2(th.right() + 8.0, r.center().y), egui::Align2::LEFT_CENTER, &g.name, egui::FontId::proportional(12.0), t.text);
|
||||
let resp = resp.on_hover_text(&g.name);
|
||||
cell_input(ui, &resp, s.d, i, true, ev);
|
||||
if resp.secondary_clicked() && !sel.contains(&g.name) {
|
||||
*sel = vec![g.name.clone()];
|
||||
set_pstate(ui.ctx(), "gs-anchor", g.name.clone());
|
||||
set_pstate(ui.ctx(), "gs-sel", sel.clone());
|
||||
}
|
||||
resp.context_menu(|ui| item_context_menu(app, ui, &g.name, sel));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -581,7 +658,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
};
|
||||
let looks = look_hashes(ui.ctx(), app);
|
||||
let shown = Shown { d: &doc, looks: &looks, preview: pstate(ui.ctx(), "gs-preview") };
|
||||
let sel = selected(ui.ctx(), &doc);
|
||||
let mut sel = selected(ui.ctx(), &doc);
|
||||
let view = app.ui.graphic_styles_view;
|
||||
let mut ev = Events::default();
|
||||
// In a sized panel the list fills the space above the footer (#2992), as the Artboards list
|
||||
@@ -602,8 +679,8 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
return;
|
||||
}
|
||||
match view.row() {
|
||||
Some(row) => rows(app, ui, &shown, &sel, row, &mut ev),
|
||||
None => tiles(app, ui, &shown, &sel, &mut ev),
|
||||
Some(row) => rows(app, ui, &shown, &mut sel, row, &mut ev),
|
||||
None => tiles(app, ui, &shown, &mut sel, &mut ev, true),
|
||||
}
|
||||
});
|
||||
// The drop zone is the part of the list on screen: when the list shrinks to its rows,
|
||||
@@ -667,8 +744,9 @@ pub(crate) fn picker(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
let looks = look_hashes(ui.ctx(), app);
|
||||
let shown = Shown { d: &doc, looks: &looks, preview: pstate(ui.ctx(), "gs-preview") };
|
||||
let mut ev = Events::default();
|
||||
let mut sel = Vec::new();
|
||||
ui.set_max_width(6.0 * (TILE + 3.0));
|
||||
tiles(app, ui, &shown, &[], &mut ev);
|
||||
tiles(app, ui, &shown, &mut sel, &mut ev, false);
|
||||
act(app, ui, &shown, ev, true);
|
||||
}
|
||||
|
||||
@@ -1740,4 +1818,174 @@ mod tests {
|
||||
frame_with(&ctx, click(false), |ui| picker(&mut app, ui));
|
||||
assert_eq!(app.session.selection_graphic_style().map(|(g, linked)| (g.name.clone(), linked)), Some((name, true)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn right_click_context_menu_in_graphic_styles_panel() {
|
||||
use egui_kittest::kittest::{NodeT, Queryable};
|
||||
|
||||
let mut app = app();
|
||||
app.run("graphicStyle.new", json!({"name": "Style A"})).unwrap();
|
||||
app.run("graphicStyle.new", json!({"name": "Style B"})).unwrap();
|
||||
|
||||
let mut h = egui_kittest::Harness::builder()
|
||||
.with_size(vec2(260.0, 400.0))
|
||||
.with_accessibility_check(false)
|
||||
.build_ui_state(
|
||||
|ui, app: &mut VectorcraftApp| {
|
||||
show(app, ui);
|
||||
},
|
||||
app,
|
||||
);
|
||||
h.run_steps(2);
|
||||
|
||||
// Initially "Style B" was added last; "Style A" is unselected.
|
||||
let pos_a = h.get_by_label("Style A").rect().center();
|
||||
|
||||
// 1. Right-click unselected "Style A"
|
||||
h.event(egui::Event::PointerMoved(pos_a));
|
||||
h.event(egui::Event::PointerButton { pos: pos_a, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
|
||||
h.event(egui::Event::PointerButton { pos: pos_a, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
|
||||
h.run_steps(2);
|
||||
|
||||
// Right-clicking unselected "Style A" selects it.
|
||||
let doc = h.state().session.active().unwrap().doc.clone();
|
||||
assert_eq!(selected(&h.ctx, &doc), vec!["Style A"]);
|
||||
|
||||
// Context menu items are displayed
|
||||
let options = h.get_by_label(" Graphic Style Options…");
|
||||
let duplicate = h.get_by_label(" Duplicate Graphic Style");
|
||||
let delete = h.get_by_label(" Delete Graphic Style");
|
||||
let redefine = h.get_by_label(" Redefine Graphic Style");
|
||||
let break_link = h.get_by_label(" Break Link to Graphic Style");
|
||||
|
||||
assert!(!options.accesskit_node().is_disabled());
|
||||
assert!(!duplicate.accesskit_node().is_disabled());
|
||||
assert!(!delete.accesskit_node().is_disabled());
|
||||
assert!(!redefine.accesskit_node().is_disabled()); // Art is selected
|
||||
assert!(break_link.accesskit_node().is_disabled()); // Art not linked to Style A
|
||||
|
||||
// 2. Click "Duplicate Graphic Style"
|
||||
duplicate.click();
|
||||
h.run_steps(3);
|
||||
|
||||
let doc = h.state().session.active().unwrap().doc.clone();
|
||||
assert!(doc.graphic_style("Style A copy").is_some());
|
||||
assert_eq!(selected(&h.ctx, &doc), vec!["Style A copy"]);
|
||||
|
||||
// 3. Apply Style A to the active art and verify Break Link
|
||||
h.state_mut().run("graphicStyle.apply", json!({"name": "Style A"})).unwrap();
|
||||
h.run_steps(2);
|
||||
|
||||
let pos_a = h.get_by_label("Style A").rect().center();
|
||||
h.event(egui::Event::PointerMoved(pos_a));
|
||||
h.event(egui::Event::PointerButton { pos: pos_a, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
|
||||
h.event(egui::Event::PointerButton { pos: pos_a, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
|
||||
h.run_steps(2);
|
||||
|
||||
let break_link = h.get_by_label(" Break Link to Graphic Style");
|
||||
assert!(!break_link.accesskit_node().is_disabled());
|
||||
break_link.click();
|
||||
h.run_steps(3);
|
||||
|
||||
assert!(h.state().session.selection_graphic_style().is_none());
|
||||
|
||||
// 4. Right-click Style A and click "Delete Graphic Style"
|
||||
let pos_a = h.get_by_label("Style A").rect().center();
|
||||
h.event(egui::Event::PointerMoved(pos_a));
|
||||
h.event(egui::Event::PointerButton { pos: pos_a, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
|
||||
h.event(egui::Event::PointerButton { pos: pos_a, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
|
||||
h.run_steps(2);
|
||||
|
||||
let delete = h.get_by_label(" Delete Graphic Style");
|
||||
assert!(!delete.accesskit_node().is_disabled());
|
||||
delete.click();
|
||||
h.run_steps(3);
|
||||
|
||||
let doc = h.state().session.active().unwrap().doc.clone();
|
||||
assert!(doc.graphic_style("Style A").is_none());
|
||||
|
||||
// Verify one undo step restores it
|
||||
h.state_mut().run("edit.undo", json!({})).unwrap();
|
||||
let doc = h.state().session.active().unwrap().doc.clone();
|
||||
assert!(doc.graphic_style("Style A").is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn right_click_context_menu_default_style_disables_options_and_delete() {
|
||||
use egui_kittest::kittest::{NodeT, Queryable};
|
||||
|
||||
let app = app();
|
||||
let mut h = egui_kittest::Harness::builder()
|
||||
.with_size(vec2(260.0, 400.0))
|
||||
.with_accessibility_check(false)
|
||||
.build_ui_state(
|
||||
|ui, app: &mut VectorcraftApp| {
|
||||
show(app, ui);
|
||||
},
|
||||
app,
|
||||
);
|
||||
h.run_steps(2);
|
||||
|
||||
let pos_def = h.get_by_label(DEFAULT_GRAPHIC_STYLE).rect().center();
|
||||
h.event(egui::Event::PointerMoved(pos_def));
|
||||
h.event(egui::Event::PointerButton { pos: pos_def, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
|
||||
h.event(egui::Event::PointerButton { pos: pos_def, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
|
||||
h.run_steps(2);
|
||||
|
||||
let options = h.get_by_label(" Graphic Style Options…");
|
||||
let duplicate = h.get_by_label(" Duplicate Graphic Style");
|
||||
let delete = h.get_by_label(" Delete Graphic Style");
|
||||
|
||||
assert!(options.accesskit_node().is_disabled(), "Options disabled on Default Graphic Style");
|
||||
assert!(!duplicate.accesskit_node().is_disabled(), "Duplicate enabled on Default Graphic Style");
|
||||
assert!(delete.accesskit_node().is_disabled(), "Delete disabled on Default Graphic Style");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn right_click_context_menu_preserves_multiselection_and_works_in_list_view() {
|
||||
use egui_kittest::kittest::{NodeT, Queryable};
|
||||
|
||||
let mut app = app();
|
||||
app.ui.graphic_styles_view = View::SmallList;
|
||||
app.run("graphicStyle.new", json!({"name": "Style 1"})).unwrap();
|
||||
app.run("graphicStyle.new", json!({"name": "Style 2"})).unwrap();
|
||||
|
||||
let mut h = egui_kittest::Harness::builder()
|
||||
.with_size(vec2(260.0, 400.0))
|
||||
.with_accessibility_check(false)
|
||||
.build_ui_state(
|
||||
|ui, app: &mut VectorcraftApp| {
|
||||
show(app, ui);
|
||||
},
|
||||
app,
|
||||
);
|
||||
h.run_steps(2);
|
||||
|
||||
set_pstate(&h.ctx, "gs-sel", vec!["Style 1".to_string(), "Style 2".to_string()]);
|
||||
h.run_steps(1);
|
||||
|
||||
let pos_2 = h.get_by_label("Style 2").rect().center();
|
||||
h.event(egui::Event::PointerMoved(pos_2));
|
||||
h.event(egui::Event::PointerButton { pos: pos_2, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
|
||||
h.event(egui::Event::PointerButton { pos: pos_2, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
|
||||
h.run_steps(2);
|
||||
|
||||
let doc = h.state().session.active().unwrap().doc.clone();
|
||||
assert_eq!(selected(&h.ctx, &doc), vec!["Style 1", "Style 2"]);
|
||||
|
||||
let options = h.get_by_label(" Graphic Style Options…");
|
||||
let duplicate = h.get_by_label(" Duplicate Graphic Styles");
|
||||
let delete = h.get_by_label(" Delete Graphic Styles");
|
||||
|
||||
assert!(options.accesskit_node().is_disabled(), "Options disabled on multi-selection");
|
||||
assert!(!duplicate.accesskit_node().is_disabled());
|
||||
assert!(!delete.accesskit_node().is_disabled());
|
||||
|
||||
duplicate.click();
|
||||
h.run_steps(3);
|
||||
|
||||
let doc = h.state().session.active().unwrap().doc.clone();
|
||||
assert!(doc.graphic_style("Style 1 copy").is_some());
|
||||
assert!(doc.graphic_style("Style 2 copy").is_some());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user