EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust
Project: folder twirls and label swatches take clicks (#152)
bflatastic472521954d41
diff --git a/crates/ui-egui/src/panels/project.rs b/crates/ui-egui/src/panels/project.rsindex d59ba5d9d..1a9c21631 100644--- a/crates/ui-egui/src/panels/project.rs+++ b/crates/ui-egui/src/panels/project.rs@@ -424,6 +424,10 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {// Flat rows like After Effects' Project panel (no stripes).if selected { t.row_selected } else { t.row },);+ // The row's click / drag area goes first, so the twirl, proxy, label and comment widgets+ // on top of it take their own clicks.+ let mut resp = ui.interact(r.intersect(list), egui::Id::new(("pitem", id.0)), Sense::click_and_drag());+ app.auto.add(&format!("project.item.{}", id.0), r, &it.name);let _ = i;let x0 = r.min.x + 8.0 + 14.0 * *depth as f32;if it.is_folder() {@@ -533,6 +537,9 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {app.auto.add(&format!("project.item.{}.comment", id.0), cell, &it.comment);if cresp.double_clicked() {ctx.data_mut(|d| d.insert_temp::<Editing>(edit_id(), (id.0, "comment".into(), it.comment.clone())));+ } else {+ // Other clicks select the row and open its context menu.+ resp = resp.union(cresp);}it.comment.clone()}@@ -545,8 +552,6 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {if dragging.is_some_and(|d| d != id.0) && drop_row == Some(*id) && it.is_folder() {lp.rect_stroke(r.shrink(1.0), 2.0, Stroke::new(1.5, t.accent), egui::StrokeKind::Inside);}- let resp = ui.interact(r.intersect(list), egui::Id::new(("pitem", id.0)), Sense::click_and_drag());- app.auto.add(&format!("project.item.{}", id.0), r, &it.name);let nresp = ui.interact(name_clip, egui::Id::new(("pname", id.0)), Sense::click_and_drag());app.auto.add(&format!("project.item.{}.name", id.0), name_clip, &it.name);let resp = resp.union(nresp);diff --git a/crates/ui-egui/tests/ui_project_rows.rs b/crates/ui-egui/tests/ui_project_rows.rsnew file mode 100644index 000000000..cd15d46ed--- /dev/null+++ b/crates/ui-egui/tests/ui_project_rows.rs@@ -0,0 +1,97 @@+//! Project panel rows: a folder's twirl and an item's label swatch take their own clicks, over+//! the row's click / drag area (#152).++use effectcraft_engine::Session;+use effectcraft_engine::color::Label;+use effectcraft_engine::project::{ItemId, ItemKind};+use effectcraft_ui_egui::EffectcraftApp;+use egui::{Event, Modifiers, Pos2, pos2};+use egui_kittest::Harness;+use egui_kittest::kittest::Queryable;+use serde_json::json;++/// A closed folder holding a comp, and a solid at the project root: (harness, folder, comp, solid).+fn harness() -> (Harness<'static, EffectcraftApp>, ItemId, ItemId, ItemId) {+ let mut s = Session::default();+ let comp = ItemId(s.execute("comp.new", json!({"name": "Main", "width": 320, "height": 180, "duration": 4})).unwrap()["comp"].as_u64().unwrap());+ s.execute("layer.newSolid", json!({"name": "Solid", "color": "#406080"})).unwrap();+ let solid = s.project.items.values().find(|i| matches!(i.kind, ItemKind::Solid(_))).unwrap().id;+ let folder = ItemId(s.execute("project.newFolder", json!({"name": "Folder"})).unwrap()["item"].as_u64().unwrap());+ s.execute("project.move", json!({"items": [comp.0], "folder": folder.0})).unwrap();+ s.execute("project.move", json!({"items": [solid.0], "folder": null})).unwrap();+ s.execute("project.select", json!({"items": []})).unwrap();+ let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(|_| EffectcraftApp::new(s));+ h.run_steps(3);+ (h, folder, comp, solid)+}++fn center(h: &Harness<'_, EffectcraftApp>, id: &str) -> Pos2 {+ let e = h.state().auto.find(id).unwrap_or_else(|| panic!("no {id}")).clone();+ pos2(e.rect[0] + e.rect[2] / 2.0, e.rect[1] + e.rect[3] / 2.0)+}++fn button(h: &mut Harness<'_, EffectcraftApp>, p: Pos2, pressed: bool) {+ h.input_mut().events.push(Event::PointerButton { pos: p, button: egui::PointerButton::Primary, pressed, modifiers: Modifiers::NONE });+}++fn click_at(h: &mut Harness<'_, EffectcraftApp>, p: Pos2) {+ h.input_mut().events.push(Event::PointerMoved(p));+ h.step();+ button(h, p, true);+ h.step();+ button(h, p, false);+ h.run_steps(3);+}++/// Both clicks in one frame: the harness' frame time is longer than a double-click's.+fn double_click_at(h: &mut Harness<'_, EffectcraftApp>, p: Pos2) {+ h.input_mut().events.push(Event::PointerMoved(p));+ h.step();+ for _ in 0..2 {+ button(h, p, true);+ button(h, p, false);+ }+ h.run_steps(3);+}++fn click(h: &mut Harness<'_, EffectcraftApp>, id: &str) {+ let p = center(h, id);+ click_at(h, p);+}++#[test]+fn folder_twirl_opens_and_closes_the_folder_on_one_click() {+ let (mut h, folder, comp, _) = harness();+ let twirl = format!("project.item.{}.twirl", folder.0);+ let comp_row = format!("project.item.{}", comp.0);+ assert!(!h.state().ui.project_open_folders.contains(&folder.0));+ assert!(h.state().auto.find(&comp_row).is_none(), "the closed folder hides its comp");+ click(&mut h, &twirl);+ assert!(h.state().ui.project_open_folders.contains(&folder.0), "one click on the twirl opens the folder");+ assert!(h.state().auto.find(&comp_row).is_some(), "the open folder shows its comp");+ assert!(h.state().session.state.project_selection.is_empty(), "the twirl does not select the folder");+ click(&mut h, &twirl);+ assert!(!h.state().ui.project_open_folders.contains(&folder.0), "a second click closes it");+ // The row under the twirl still selects on a click and twirls on a double-click.+ let name = center(&h, &format!("project.item.{}.name", folder.0));+ click_at(&mut h, name);+ assert_eq!(h.state().session.state.project_selection, vec![folder]);+ double_click_at(&mut h, name);+ assert!(h.state().ui.project_open_folders.contains(&folder.0), "a double-click on the row opens the folder");+}++#[test]+fn label_swatch_opens_the_label_menu() {+ let (mut h, _, _, solid) = harness();+ assert_ne!(h.state().session.project.item(solid).unwrap().label, Label::Green);+ let green = h.state().session.prefs.label_name(Label::Green);+ assert!(h.query_by_label(&green).is_none());+ click(&mut h, &format!("project.item.{}.label", solid.0));+ assert!(h.state().session.state.project_selection.is_empty(), "the swatch does not select the row");+ let entry = h.query_by_label(&green).expect("the label menu is open").rect();+ let undo = h.state().session.history.undo.len();+ click_at(&mut h, entry.center());+ assert_eq!(h.state().session.project.item(solid).unwrap().label, Label::Green);+ assert_eq!(h.state().session.history.undo.len(), undo + 1);+ assert!(h.query_by_label(&green).is_none(), "the menu closed after the choice");+}