diff --git a/crates/ui-egui/src/panels/effect_controls/panel.rs b/crates/ui-egui/src/panels/effect_controls/panel.rs index 5ced41673..1bcca9507 100644 --- a/crates/ui-egui/src/panels/effect_controls/panel.rs +++ b/crates/ui-egui/src/panels/effect_controls/panel.rs @@ -3,6 +3,7 @@ use super::editors::{curves_editor, editor_height, histogram_only, levels_editor use super::rows::{FxScope, group_rows, toggle_closed}; use super::{Align2, Color32, EffectcraftApp, EvalCtx, GroupKind, Icon, Layer, PropGroup, Rect, Sense, Stroke, Tokens, json, pos2, vec2, widgets}; use crate::dock::{PanelKind, WorkspaceExt as _}; +use crate::panels::dialogs::{Field, form}; /// Plain arrows in Effect Controls, as in After Effects, before their layer nudge runs: Right / /// Left expand / collapse the selected effects, and Down / Up select the next / previous effect @@ -86,6 +87,16 @@ struct EffectHdr<'a> { layer_id: u64, } +fn paint_header_name(app: &mut EffectcraftApp, ui: &mut egui::Ui, bp: &egui::Painter, h: &EffectHdr, effect: &str, name_area: Rect) { + let painter = bp.with_clip_rect(name_area); + let name = painter.text(pos2(h.tw.max.x + 6.0, h.r.center().y), Align2::LEFT_CENTER, h.shown, Tokens::semibold(12.0), app.tokens.text); + let visible = name.intersect(name_area); + if visible.is_positive() { + app.auto.add(&format!("effectControls.effect.{}.name", h.g.uid), visible, &h.g.name); + } + render_error_mark(app, ui, bp, effect, h.g, pos2(name.max.x + 8.0, h.r.center().y)); +} + /// The effect header's name field while renaming: Enter or blur commits, Escape cancels. fn rename_field(app: &mut EffectcraftApp, ui: &mut egui::Ui, h: &EffectHdr, mut value: String, actions: &mut Actions) { let EffectHdr { g, shown, r, tw, reset, rename_id, layer_id } = *h; @@ -206,6 +217,46 @@ fn render_error_mark(app: &mut EffectcraftApp, ui: &mut egui::Ui, bp: &egui::Pai ui.ctx().request_repaint_after(std::time::Duration::from_millis(500)); } +/// Reserve the Edit button in a Dropdown Menu Control header and open a draft for its own +/// Menu property. Other effects keep the usual space for Reset and the effect name. +fn dropdown_edit_button(app: &mut EffectcraftApp, ui: &mut egui::Ui, cx: &EditorCx, g: &PropGroup, r: Rect, tw: Rect, y: &mut f32) -> (Rect, Rect) { + let mut reset = Rect::from_min_size(pos2(r.max.x - 96.0, r.center().y - 9.0), vec2(40.0, 18.0)); + let mut name_area = Rect::EVERYTHING; + if matches!(&g.kind, GroupKind::Effect { effect } if effect == "ec.control.dropdown") + && let Some(menu) = g.get("menu") + && let super::ParamUi::Popup { options } = &menu.ui + { + // A narrow dock has room for the usual Reset/About pair only. + let edit = if r.width() < 220.0 { + let edit = Rect::from_min_size(pos2(tw.max.x, *y), reset.size()); + *y += ROW; + edit + } else { + let edit = reset; + reset = reset.translate(vec2(-46.0, 0.0)); + edit + }; + name_area = Rect::from_min_max(r.min, pos2(reset.min.x - 4.0, r.max.y)); + let response = ui.interact(edit, egui::Id::unique(("ec-edit-dropdown", g.uid)), Sense::click()); + let label = crate::i18n::tr("Edit…"); + response.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, ui.is_enabled(), label)); + let t = &app.tokens; + cx.bp.text(edit.center(), Align2::CENTER_CENTER, label, Tokens::ui(11.5), if response.hovered() { t.hot_text } else { t.text_dim }); + widgets::focus_outline(ui, &response, t); + app.auto.add_response(&format!("effectControls.effect.{}.edit", g.uid), &response, "Edit Dropdown Menu"); + if response.clicked() { + form( + app, + "Edit Dropdown Menu", + "effect.editDropdown", + json!({"comp": cx.ectx.comp_id.0, "layer": cx.layer.id.0, "prop": menu.uid}), + vec![Field::string_list("items", "Items", options.clone())], + ); + } + } + (reset, name_area) +} + fn about_button(app: &mut EffectcraftApp, ui: &mut egui::Ui, bp: &egui::Painter, t: &Tokens, effect: &str, g: &PropGroup, r: Rect) { // About…: the effect's name and category (AE's About dialog). let about = Rect::from_min_size(pos2(r.max.x - 50.0, r.center().y - 9.0), vec2(44.0, 18.0)); @@ -268,6 +319,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { let mut y = body.min.y + 4.0 - scroll.offset; let mut actions: Actions = vec![]; let bp = p.with_clip_rect(body); + let editor_cx = EditorCx { bp: &bp, layer: &layer, ectx: &ectx }; let fx: Vec = layer.effects().map(|f| f.groups().cloned().collect()).unwrap_or_default(); if fx.is_empty() { bp.text( @@ -322,14 +374,12 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { app.auto.add(&format!("effectControls.effect.{}.twirl", g.uid), tw, &g.name); let rename_id = egui::Id::unique(("ec-rename", g.uid)); let editing: Option = ctx.data(|d| d.get_temp(rename_id)); - let reset = Rect::from_min_size(pos2(r.max.x - 96.0, r.center().y - 9.0), vec2(40.0, 18.0)); + let (reset, name_area) = dropdown_edit_button(app, ui, &editor_cx, g, r, tw, &mut y); let hdr_cx = EffectHdr { g, shown: &shown, r, tw, reset, rename_id, layer_id: layer.id.0 }; if let Some(value) = editing.as_ref().cloned() { rename_field(app, ui, &hdr_cx, value, &mut actions); } else { - let name = bp.text(pos2(tw.max.x + 6.0, r.center().y), Align2::LEFT_CENTER, &shown, Tokens::semibold(12.0), t.text); - app.auto.add(&format!("effectControls.effect.{}.name", g.uid), name, &g.name); - render_error_mark(app, ui, &bp, effect, g, pos2(name.max.x + 8.0, r.center().y)); + paint_header_name(app, ui, &bp, &hdr_cx, effect, name_area); } let rresp = ui.interact(reset, egui::Id::unique(("ec-reset", g.uid)), Sense::click()); bp.text(reset.center(), Align2::CENTER_CENTER, crate::i18n::tr("Reset"), Tokens::ui(11.5), if rresp.hovered() { t.hot_text } else { t.text_dim }); @@ -428,7 +478,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { if ui.input(|i| i.pointer.primary_pressed()) && ui.rect_contains_pointer(er) { actions.push(("prop.select".into(), json!({"layer":layer.id.0,"prop":g.uid}))); } - effect_editor(app, ui, &EditorCx { bp: &bp, layer: &layer, ectx: &ectx }, g, er, &mut actions); + effect_editor(app, ui, &editor_cx, g, er, &mut actions); } } let scope = FxScope { effect: effect.as_str(), root: g, path: String::new() }; diff --git a/crates/ui-egui/src/panels/forms.rs b/crates/ui-egui/src/panels/forms.rs index c5a37cd7a..36e0aeaa6 100644 --- a/crates/ui-egui/src/panels/forms.rs +++ b/crates/ui-egui/src/panels/forms.rs @@ -11,6 +11,19 @@ use serde_json::{Map, Value, json}; use crate::theme::Tokens; use crate::{Dialog, EffectcraftApp}; +/// One draft item. The editor keeps its cursor and undo history when the list is reordered. +#[derive(Clone, Debug)] +pub struct StringListItem { + text: String, + editor: Option, +} + +impl StringListItem { + fn new(text: String) -> Self { + Self { text, editor: None } + } +} + #[derive(Clone, Debug)] pub enum FieldKind { Number { @@ -18,6 +31,11 @@ pub enum FieldKind { speed: f64, }, Text(String), + /// Ordered, nonempty item names. Each item has its own editor so its text remains verbatim. + StringList { + items: Vec, + next_editor: u64, + }, /// A file path with a Browse… button (the host's open dialog, these extensions). Path { value: String, @@ -50,6 +68,10 @@ impl Field { pub fn text(key: &str, label: &str, value: &str) -> Field { Field { key: key.into(), label: label.into(), kind: FieldKind::Text(value.into()) } } + pub fn string_list(key: &str, label: &str, items: Vec) -> Field { + let items = items.into_iter().map(StringListItem::new).collect(); + Field { key: key.into(), label: label.into(), kind: FieldKind::StringList { items, next_editor: 0 } } + } pub fn path(key: &str, label: &str, value: &str, exts: &[&str]) -> Field { Field { key: key.into(), label: label.into(), kind: FieldKind::Path { value: value.into(), exts: exts.iter().map(|e| e.to_string()).collect() } } } @@ -87,6 +109,7 @@ impl Form { let v = match &f.kind { FieldKind::Number { value, .. } => json!(value), FieldKind::Text(s) | FieldKind::Path { value: s, .. } | FieldKind::SavePath(s) => json!(s), + FieldKind::StringList { items, .. } => items.iter().map(|item| Value::String(item.text.clone())).collect(), FieldKind::Note(_) => continue, FieldKind::Bool(b) => json!(b), FieldKind::Choice { options, sel } => options.get(*sel).map(|o| o.1.clone()).unwrap_or(Value::Null), @@ -864,6 +887,67 @@ pub fn open_form(app: &mut EffectcraftApp, id: &str, p: &Value) -> bool { true } +/// Edit a list in the form draft. In particular, a comma or newline within a label is ordinary text. +fn string_list( + ui: &mut egui::Ui, + key: &str, + items: &mut Vec, + next_editor: &mut u64, + regs: &mut Vec<(String, egui::Rect, String)>, +) -> egui::Rect { + let prefix = format!("form.field.{key}"); + let len = items.len(); + let mut remove = None; + let mut swap = None; + ui.vertical(|ui| { + for (i, item) in items.iter_mut().enumerate() { + ui.horizontal(|ui| { + let id = *item.editor.get_or_insert_with(|| { + // Reuse IDs between dialogs, but never their previous text edit state. + let id = egui::Id::unique(("form-string-list", key, *next_editor)); + *next_editor = next_editor.saturating_add(1); + ui.data_mut(|d| d.remove::(id)); + id + }); + let edit = ui.add(egui::TextEdit::multiline(&mut item.text).id(id).desired_rows(1).desired_width(160.0).hint_text(tr("Item name"))); + regs.push((format!("{prefix}.{i}"), edit.rect, item.text.clone())); + let row = i + 1; + crate::a11y::name(&edit, &tr_args("Item {}", &[&row])); + let mut button = |action: &str, symbol: &str, label: &str, enabled: bool| { + let b = ui.add_enabled(enabled, egui::Button::new(symbol)).on_hover_text(tr(label)); + // The glyph stays first (WCAG 2.5.3); the row says which item the action is for. + crate::a11y::button(&b, &tr_args("{} {}, item {}", &[&symbol, &tr(label), &row]), enabled); + regs.push((format!("{prefix}.{action}.{i}"), b.rect, label.into())); + b.clicked() + }; + if button("up", "↑", "Move Up", i > 0) { + swap = Some((i, i.saturating_sub(1))); + } + if button("down", "↓", "Move Down", i + 1 < len) { + swap = Some((i, i + 1)); + } + if button("remove", "−", "Remove", len > 1) { + remove = Some(i); + } + }); + } + if let Some(i) = remove { + if let Some(id) = items.remove(i).editor { + ui.data_mut(|d| d.remove::(id)); + } + } else if let Some((from, to)) = swap { + items.swap(from, to); + } + let add = ui.button(tr("Add Item")); + regs.push((format!("{prefix}.add"), add.rect, "Add Item".into())); + if add.clicked() { + items.push(StringListItem::new(String::new())); + } + }) + .response + .rect +} + /// The label and control rows of a form: the part of [`show_form`] that scrolls. fn form_grid(ui: &mut egui::Ui, f: &mut Form, regs: &mut Vec<(String, egui::Rect, String)>, browse: &mut Option, focused: &mut bool) { egui::Grid::new("form-grid").num_columns(2).spacing([14.0, 10.0]).show(ui, |ui| { @@ -887,6 +971,7 @@ fn form_grid(ui: &mut egui::Ui, f: &mut Form, regs: &mut Vec<(String, egui::Rect } r.rect } + FieldKind::StringList { items, next_editor } => string_list(ui, &fl.key, items, next_editor, regs), FieldKind::Path { value, .. } => { ui.horizontal(|ui| { let r = ui.add(egui::TextEdit::singleline(value).desired_width(150.0)).rect; @@ -948,12 +1033,14 @@ fn form_grid(ui: &mut egui::Ui, f: &mut Form, regs: &mut Vec<(String, egui::Rect pub fn show_form(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) { let mut f = app.dialog_state.form.clone(); let (mut ok, mut close) = (false, false); + let mut valid = true; // Rows are 30 px; a note wraps at about 40 characters a line, so OK and Cancel stay in view. let rows: f32 = f .fields .iter() .map(|fl| match &fl.kind { FieldKind::Note(text) => 10.0 + 15.0 * (text.chars().count() as f32 / 40.0).ceil().max(1.0), + FieldKind::StringList { items, .. } => 30.0 * (items.len() as f32 + 1.0), _ => 30.0, }) .sum(); @@ -969,9 +1056,13 @@ pub fn show_form(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) { egui::ScrollArea::vertical().max_height(max_height).auto_shrink([false, true]).show(ui, |ui| { form_grid(ui, &mut f, &mut regs, &mut browse, &mut focused); }); + valid = f.fields.iter().all(|field| match &field.kind { + FieldKind::StringList { items, .. } => !items.is_empty() && items.iter().all(|item| !item.text.is_empty()), + _ => true, + }); ui.add_space(14.0); ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { - let b = ui.add(egui::Button::new(egui::RichText::new(tr(" OK ")).color(Color32::WHITE)).fill(t.accent)); + let b = ui.add_enabled(valid, egui::Button::new(egui::RichText::new(tr(" OK ")).color(Color32::WHITE)).fill(t.accent)); regs.push(("form.ok".into(), b.rect, "OK".into())); if b.clicked() { ok = true; @@ -1010,7 +1101,7 @@ pub fn show_form(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) { } let enter = !ctx.egui_wants_keyboard_input() && ctx.input(|i| i.key_pressed(egui::Key::Enter)); app.dialog_state.form = f.clone(); - if ok || enter { + if valid && (ok || enter) { app.dialog = None; let params = f.params(); match app.session.execute(&f.command, params) { diff --git a/crates/ui-egui/tests/it/ui_effect_controls.rs b/crates/ui-egui/tests/it/ui_effect_controls.rs index b51490176..26af0d9b6 100644 --- a/crates/ui-egui/tests/it/ui_effect_controls.rs +++ b/crates/ui-egui/tests/it/ui_effect_controls.rs @@ -9,7 +9,7 @@ use effectcraft_ui_egui::EffectcraftApp; use effectcraft_ui_egui::dock::PanelKind; use effectcraft_ui_egui::state::FxPick; use egui_kittest::Harness; -use egui_kittest::kittest::Queryable; +use egui_kittest::kittest::{NodeT, Queryable}; use serde_json::json; struct Ids { @@ -524,6 +524,143 @@ fn effect_controls_widgets_register_automation_ids() { assert!(ids(&h).contains(&pt_id), "effect point control shown for the selected effect"); } +fn click_id(h: &mut Harness<'_, EffectcraftApp>, id: &str) { + let at = rect_of(h, id).center(); + click(h, at); + h.run_steps(2); +} + +fn rename_dropdown_row(h: &mut Harness<'_, EffectcraftApp>, index: usize, text: &str) { + click_id(h, &format!("form.field.items.{index}")); + h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A); + h.event(egui::Event::Text(text.into())); + h.run_steps(2); +} + +#[test] +fn dropdown_edit_targets_its_instance_and_applies_one_undoable_change() { + let mut s = Session::default(); + s.execute("comp.new", json!({"width": 160, "height": 90})).unwrap(); + let lid = s.execute("layer.newSolid", json!({})).unwrap()["layer"].as_u64().unwrap(); + let mut apply = |effect: &str| s.execute("effect.apply", json!({"effect": effect})).unwrap()["effects"][0].as_u64().unwrap(); + let first = apply("Dropdown Menu Control"); + let second = apply("Dropdown Menu Control"); + let slider = apply("Slider Control"); + let original = ["Cool, blue", "Warm", "Discard"]; + s.execute("effect.editDropdown", json!({"layer": lid, "path": "effects/#2/menu", "items": original})).unwrap(); + let mut app = EffectcraftApp::new(s); + app.show_panel(PanelKind::EffectControls); + app.toggle_maximize(PanelKind::EffectControls); + let mut h = + Harness::builder().with_size(egui::vec2(1200.0, 800.0)).with_context_setup(effectcraft_ui_egui::framework::install_headless).build_eframe(|_| app); + h.run_steps(4); + + let options = |h: &Harness<'_, EffectcraftApp>, effect| { + let l = layer(&h.state().session, lid); + let menu = l.props.find_group(effect).unwrap().get("menu").unwrap(); + let effectcraft_engine::project::ParamUi::Popup { options } = &menu.ui else { panic!("expected a dropdown") }; + options.clone() + }; + let edit = |effect| format!("effectControls.effect.{effect}.edit"); + assert!(h.state().auto.find(&edit(first)).is_some(), "each Dropdown Menu Control has an Edit action"); + assert!(h.state().auto.find(&edit(slider)).is_none()); + let undo = h.state().session.history.undo.len(); + click_id(&mut h, &edit(second)); + rename_dropdown_row(&mut h, 0, "Canceled"); + for index in (0..original.len()).rev() { + click_id(&mut h, &format!("form.field.items.remove.{index}")); + } + assert_eq!(h.state().auto.find("form.field.items.0").map(|row| row.label.as_str()), Some("Canceled"), "the last item remains"); + click_id(&mut h, "form.cancel"); + assert_eq!(options(&h, second), original); + assert_eq!(h.state().session.history.undo.len(), undo); + + click_id(&mut h, &edit(second)); + rename_dropdown_row(&mut h, 1, "Amber"); + click_id(&mut h, "form.field.items.remove.2"); + click_id(&mut h, "form.field.items.add"); + h.key_press(egui::Key::Enter); + h.run_steps(2); + assert_eq!(h.state().dialog, Some(effectcraft_ui_egui::Dialog::Form), "an empty item keeps the draft open"); + assert_eq!(h.state().session.history.undo.len(), undo); + rename_dropdown_row(&mut h, 2, "Night"); + click_id(&mut h, "form.field.items.up.2"); + click_id(&mut h, "form.ok"); + assert_eq!(options(&h, second), ["Cool, blue", "Night", "Amber"]); + assert_eq!(options(&h, first), ["Item 1", "Item 2", "Item 3"]); + assert_eq!(h.state().session.history.undo.len(), undo + 1); + assert!(h.state_mut().session.undo()); + assert_eq!(options(&h, second), original); +} + +#[test] +fn dropdown_text_undo_follows_the_item_and_starts_fresh_on_reopen() { + let mut session = Session::default(); + session.execute("comp.new", json!({"width": 160, "height": 90})).unwrap(); + session.execute("layer.newSolid", json!({})).unwrap(); + let effect = session.execute("effect.apply", json!({"effect": "Dropdown Menu Control"})).unwrap()["effects"][0].as_u64().unwrap(); + let mut app = EffectcraftApp::new(session); + app.show_panel(PanelKind::EffectControls); + app.toggle_maximize(PanelKind::EffectControls); + let mut h = Harness::builder() + .with_size(egui::vec2(1200.0, 800.0)) + .with_context_setup(effectcraft_ui_egui::framework::install_headless) + .with_step_dt(1.0 / 60.0) + .build_eframe(|_| app); + h.run_steps(4); + + let edit = format!("effectControls.effect.{effect}.edit"); + let undo_in = |h: &mut Harness<'_, EffectcraftApp>, index| { + click_id(h, &format!("form.field.items.{index}")); + h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::Z); + h.run_steps(2); + }; + let label = |h: &Harness<'_, EffectcraftApp>, index| h.state().auto.find(&format!("form.field.items.{index}")).unwrap().label.clone(); + + click_id(&mut h, &edit); + rename_dropdown_row(&mut h, 0, "Edited"); + click_id(&mut h, "form.field.items.down.0"); + assert_eq!(label(&h, 1), "Edited"); + undo_in(&mut h, 0); + assert_eq!(label(&h, 0), "Item 2", "Undo in the old slot preserves its current item"); + undo_in(&mut h, 1); + assert_eq!(label(&h, 1), "Item 1", "the moved item's own rename can still be undone"); + + rename_dropdown_row(&mut h, 1, "Saved"); + click_id(&mut h, "form.ok"); + click_id(&mut h, &edit); + undo_in(&mut h, 1); + assert_eq!(label(&h, 1), "Saved", "the reopened editor starts with the committed label"); +} + +#[test] +fn dropdown_editor_row_actions_name_their_row() { + let mut session = Session::default(); + session.execute("comp.new", json!({"width": 160, "height": 90})).unwrap(); + session.execute("layer.newSolid", json!({})).unwrap(); + let effect = session.execute("effect.apply", json!({"effect": "Dropdown Menu Control"})).unwrap()["effects"][0].as_u64().unwrap(); + let mut app = EffectcraftApp::new(session); + app.show_panel(PanelKind::EffectControls); + app.toggle_maximize(PanelKind::EffectControls); + let mut h = + Harness::builder().with_size(egui::vec2(1200.0, 800.0)).with_context_setup(effectcraft_ui_egui::framework::install_headless).build_eframe(|_| app); + h.run_steps(4); + click_id(&mut h, &format!("effectControls.effect.{effect}.edit")); + // The default list has three items; keep two so the first and last rows are both edges. + click_id(&mut h, "form.field.items.remove.2"); + + let button = |h: &Harness<'_, EffectcraftApp>, name: &str| h.get_by_role_and_label(egui::accesskit::Role::Button, name).accesskit_node().is_disabled(); + assert!(button(&h, "↑ Move Up, item 1"), "the first row cannot move up"); + assert!(!button(&h, "↑ Move Up, item 2")); + assert!(!button(&h, "↓ Move Down, item 1")); + assert!(button(&h, "↓ Move Down, item 2"), "the last row cannot move down"); + assert!(!button(&h, "− Remove, item 1")); + assert!(!button(&h, "− Remove, item 2")); + for row in ["Item 1", "Item 2"] { + h.get_by_role_and_label(egui::accesskit::Role::MultilineTextInput, row); + } +} + #[test] fn tint_swap_button_targets_its_instance_and_undoes_both_colors() { let mut s = Session::default();