From 0bb4b3cbc2a9768a4d9705c83cc021eb0ca5ca50 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sun, 22 Nov 2026 06:55:21 +0700 Subject: [PATCH] automation, text: the Actions and Text Styles row menus take production's rows; Redefine names its style --- crates/automation/src/ui/actions/menu.rs | 58 ++++++++++++---- crates/automation/src/ui/actions/panel.rs | 6 +- crates/automation/src/ui/actions/words.rs | 10 +++ crates/automation/tests/it/actions.rs | 34 ++++++++++ crates/text/src/ui/style_panel.rs | 81 +++++++++++++++++++++-- crates/text/tests/it/style_panel.rs | 48 +++++++++++++- 6 files changed, 214 insertions(+), 23 deletions(-) diff --git a/crates/automation/src/ui/actions/menu.rs b/crates/automation/src/ui/actions/menu.rs index 21f6007ba..74add1043 100644 --- a/crates/automation/src/ui/actions/menu.rs +++ b/crates/automation/src/ui/actions/menu.rs @@ -1,4 +1,4 @@ -//! The panel menu's commands and the row context menus (Keyboard Shortcuts, Color; Save Set…). +//! The panel menu's commands and the row context menus. use craft_keymap::Platform; use craft_ui::contract::{CommandSet, ContextMenuRequest, Props as _, Show as _, ShowContainer as _}; @@ -6,6 +6,7 @@ use craft_ui::display::Divider; use craft_ui::list::label_color_label; use craft_ui::menu::{Menu, MenuItem, PanelRow}; use craft_ui::runtime::commands::{CommandRef, CommandState}; +use craft_ui::runtime::env::Label; use egui::{Response, Ui}; use super::body::{Body, delete_event}; @@ -291,7 +292,7 @@ impl PendingShortcut { /// The menu a context request opened, kept until the next request. #[derive(Clone, Debug)] pub(super) enum OpenMenu { - /// A set's menu: Save Set…. + /// A set's menu. Set(String), /// An action's menu, with the shortcut as the menu has made it from the host's. Action { index: usize, shortcut: PendingShortcut }, @@ -318,28 +319,52 @@ impl OpenMenu { } /// Shows the menu the last request chose, hanging from `opener` (the list), and reports what its -/// rows ran: -/// - On an action: Keyboard Shortcuts (a caption), None and F1 to F15 as radio rows, then Shift and -/// Command (Ctrl off macOS) check rows, enabled only with a key; the check rows keep the menu -/// open so both can be ticked, and each tick is reported as it is made with the shortcut as it -/// stands. A separator, then Color: a submenu of None, Red, Orange, Yellow, Green, Blue, Violet -/// and Gray with the current one marked. -/// - On a set: Save Set…. +/// rows ran. Rows that start or change something are off while an action plays or records, as the +/// footer's are; Delete follows the footer's Delete. +/// - On an action: Play and Record; Duplicate and Delete; Action Options… and Batch…. Then +/// Keyboard Shortcuts (a caption), None and F1 to F15 as radio rows, then Shift and Command (Ctrl +/// off macOS) check rows, enabled only with a key; the check rows keep the menu open so both can +/// be ticked, and each tick is reported as it is made with the shortcut as it stands. Last Color: +/// a submenu of None, Red, Orange, Yellow, Green, Blue, Violet and Gray with the current one +/// marked. +/// - On a set: New Action…, New Set…, Duplicate Set and Delete Set; Set Options…; Save Set…. pub(super) fn show_menu(ui: &mut Ui, body: &Body<'_>, opener: &Response, session: &mut Session) -> Vec { - let Body { list, words, list_words, .. } = *body; + let Body { list, host, words, list_words, .. } = *body; + let recording = host.is_some_and(|host| host.recording().is_some()); + let playing = host.is_some_and(|host| host.playing()); + let idle = !recording && !playing; + let hosted = host.is_some(); + let deletable = |row: &ActionSelection| delete_event(row, recording).filter(|_| !playing); let mut events = Vec::new(); match session.menu.as_mut() { None => {} Some(OpenMenu::Set(name)) => { + let set = ActionSelection::Set(name.clone()); Menu::context(words.row_menu.clone(), opener).show(ui, |ui, _| { - if MenuItem::new(words.save_set.clone()).show(ui).ran { - events.push(ActionsEvent::Save { set: Some(name.clone()) }); - } + let events = &mut events; + event_row(ui, events, &words.menu_new_action, (hosted && idle).then_some(ActionsEvent::NewAction)); + event_row(ui, events, &words.menu_new_set, idle.then_some(ActionsEvent::NewSet)); + event_row(ui, events, &words.duplicate_set, idle.then(|| ActionsEvent::Duplicate(set.clone()))); + event_row(ui, events, &words.delete_set, deletable(&set)); + Divider::horizontal().show(ui); + event_row(ui, events, &words.set_options, Some(ActionsEvent::SetOptions(name.clone()))); + Divider::horizontal().show(ui); + event_row(ui, events, &words.save_set, Some(ActionsEvent::save(Some(name.clone())))); }); } Some(OpenMenu::Action { index, shortcut }) => { let (action, current) = (*index, list.action(*index).map(|held| held.color).unwrap_or_default()); + let selection = ActionSelection::Action(action); Menu::context(words.row_menu.clone(), opener).show(ui, |ui, _| { + event_row(ui, &mut events, &words.menu_play, (hosted && idle).then(|| ActionsEvent::play(action, 0))); + event_row(ui, &mut events, &words.menu_record, (hosted && idle).then(|| ActionsEvent::begin_recording(Some(action)))); + Divider::horizontal().show(ui); + event_row(ui, &mut events, &words.duplicate, idle.then(|| ActionsEvent::Duplicate(selection.clone()))); + event_row(ui, &mut events, &words.delete, deletable(&selection)); + Divider::horizontal().show(ui); + event_row(ui, &mut events, &words.action_options, Some(ActionsEvent::ActionOptions(action))); + event_row(ui, &mut events, &words.batch, Some(ActionsEvent::Batch(action))); + Divider::horizontal().show(ui); MenuItem::new(words.keyboard_shortcuts.clone()).enabled(false).show(ui); if MenuItem::new(words.none.clone()).radio_mark(shortcut.key == 0).show(ui).ran { shortcut.key = 0; @@ -375,3 +400,10 @@ pub(super) fn show_menu(ui: &mut Ui, body: &Body<'_>, opener: &Response, session } events } + +/// A row of a row menu, enabled while it has an event, which it reports when it runs. +fn event_row(ui: &mut Ui, events: &mut Vec, label: &Label, event: Option) { + if MenuItem::new(label.clone()).enabled(event.is_some()).show(ui).ran { + events.extend(event); + } +} diff --git a/crates/automation/src/ui/actions/panel.rs b/crates/automation/src/ui/actions/panel.rs index f35bc4c43..08dcb6b67 100644 --- a/crates/automation/src/ui/actions/panel.rs +++ b/crates/automation/src/ui/actions/panel.rs @@ -234,6 +234,8 @@ pub enum ActionsEvent { InsertStop, /// Batch… with this action (the app's dialog). Batch(usize), + /// Action Options… from an action's menu: the app's dialog for this action's name, set, function key and colour. + ActionOptions(usize), /// Load Actions… (the app's file dialog). Load, /// Clear All Actions: the app asks with [`LibraryPrompt::ClearAll`](super::LibraryPrompt) and @@ -378,7 +380,9 @@ impl Default for Session { /// all); a double click on an action's name renames it, elsewhere on an action it plays it. The /// cells sweep down their column as one gesture. A drag moves an action (into or out of a set) /// or a step (within or between actions), or drops it on the footer's Delete. Right-click on an -/// action opens Keyboard Shortcuts and Color; on a set, Save Set…. +/// action or a set selects it and opens its menu: on an action Play, Record, Duplicate, Delete, +/// Action Options…, Batch…, Keyboard Shortcuts and Color; on a set New Action…, New Set…, +/// Duplicate Set, Delete Set, Set Options… and Save Set…. /// - **Keys**: the rows are one Tab stop: arrows move, Left and Right close and open, Enter plays /// an action or toggles a set, Space toggles the include cell, F2 renames, Delete deletes the /// selection (not an action while recording), Cmd/Ctrl+] and [ move the row, Shift to the ends. The footer diff --git a/crates/automation/src/ui/actions/words.rs b/crates/automation/src/ui/actions/words.rs index 745edf374..c2f9ecc2a 100644 --- a/crates/automation/src/ui/actions/words.rs +++ b/crates/automation/src/ui/actions/words.rs @@ -39,6 +39,16 @@ craft_ui::words! { /// The panel menu's submenu-free title of the row menu. row_menu: "actions.rowMenu" = "Action"; save_set: "actions.saveSet" = "Save Set…"; + menu_new_action: "actions.menuNewAction" = "New Action…"; + menu_new_set: "actions.menuNewSet" = "New Set…"; + duplicate_set: "actions.duplicateSet" = "Duplicate Set"; + delete_set: "actions.deleteSet" = "Delete Set"; + set_options: "actions.setOptions" = "Set Options…"; + menu_play: "actions.menuPlay" = "Play"; + menu_record: "actions.menuRecord" = "Record"; + duplicate: "actions.duplicate" = "Duplicate"; + action_options: "actions.actionOptions" = "Action Options…"; + batch: "actions.batch" = "Batch…"; keyboard_shortcuts: "actions.keyboardShortcuts" = "Keyboard Shortcuts"; /// The shortcut menu's "no shortcut" row. none: "actions.none" = "None"; diff --git a/crates/automation/tests/it/actions.rs b/crates/automation/tests/it/actions.rs index cb6834bf7..804613700 100644 --- a/crates/automation/tests/it/actions.rs +++ b/crates/automation/tests/it/actions.rs @@ -313,3 +313,37 @@ fn shift_clicking_a_set_selects_the_range_and_delete_reports_it_once() { assert!(shared.borrow().state.selected_sets.is_empty(), "a click on an action ends the range"); assert_eq!(shared.borrow().state.selected, Some(ActionSelection::Action(1))); } + +/// A right-click at `pos`, with the pointer moved there first. +fn right_click(fixture: &mut Fixture, pos: Pos2) { + fixture.frame(vec![pointer::moved(pos)]); + fixture.frame(vec![pointer::secondary(pos, true)]); + fixture.frame(vec![pointer::secondary(pos, false)]); + fixture.idle(2); +} + +/// What the row `item` of the menu a right-click on `row` opens reports. +fn menu_ran(fixture: &mut Fixture, shared: &Shared, row: &str, item: &str) -> Vec { + let at = node(fixture, row).center(); + right_click(fixture, at); + let at = node(fixture, item).center(); + click(fixture, at); + take(shared) +} + +/// A right-click on an action or a set selects it and opens its menu, whose rows report their event for that row; +/// Play reports nothing while an action plays. +#[trial] +fn a_rows_menu_selects_the_row_and_reports_its_rows_for_it() { + let (mut fixture, shared) = rig(app(two_sets())); + assert_eq!(menu_ran(&mut fixture, &shared, "First", "Action Options…"), [ActionsEvent::ActionOptions(1)]); + assert_eq!(shared.borrow().state.selected, Some(ActionSelection::Action(1)), "the right-click selects the action"); + assert_eq!(menu_ran(&mut fixture, &shared, "Second", "Duplicate"), [ActionsEvent::Duplicate(ActionSelection::Action(2))]); + assert_eq!(menu_ran(&mut fixture, &shared, "Alpha", "Delete Set"), [ActionsEvent::Delete(ActionSelection::Set("Alpha".into()))]); + assert_eq!(shared.borrow().state.selected, Some(ActionSelection::Set("Alpha".into())), "the right-click selects the set"); + + let mut playing = app(two_sets()); + playing.host.playing = true; + let (mut fixture, shared) = rig(playing); + assert_eq!(menu_ran(&mut fixture, &shared, "First", "Play"), [], "Play is off while an action plays"); +} diff --git a/crates/text/src/ui/style_panel.rs b/crates/text/src/ui/style_panel.rs index 0ea5a801e..71ad84b0d 100644 --- a/crates/text/src/ui/style_panel.rs +++ b/crates/text/src/ui/style_panel.rs @@ -7,11 +7,12 @@ use craft_behavior::{IconId, Selection, keyed}; use craft_theme::variant::Tone; use craft_ui::bars::StatusMessage; use craft_ui::contract::{ - ActivateOn, Bind, CollectionEvent, CommandSet, Commands as _, ListDrag, NoValue, Outcome, Pad, Props as _, Remember as _, SelectMode, + ActivateOn, Bind, CollectionEvent, CommandSet, Commands as _, ListDrag, NoValue, Outcome, Pad, Props as _, Remember as _, SelectMode, Show as _, ShowCollection as _, ShowContainer as _, }; +use craft_ui::display::Divider; use craft_ui::list::{List, ListRow, RowInk}; -use craft_ui::menu::PanelRow; +use craft_ui::menu::{Menu, MenuItem, PanelRow}; use craft_ui::runtime::commands::CommandState; use craft_ui::runtime::env::{Env, Label}; use craft_ui::sections::{FooterAction, FooterBar, Panel}; @@ -37,6 +38,11 @@ craft_ui::words! { duplicate: "textStyles.duplicate" = "Duplicate style"; new_style: "textStyles.new" = "Create new style (from the selected type)"; delete: "textStyles.delete" = "Delete style"; + /// The row menu's accessible name. + row_menu: "textStyles.rowMenu" = "Style"; + menu_duplicate: "textStyles.menuDuplicate" = "Duplicate Style"; + menu_delete: "textStyles.menuDelete" = "Delete Style"; + menu_redefine: "textStyles.menuRedefine" = "Redefine Style"; overridden_name: template "textStyles.overriddenName" = "{name}+"; overridden_spoken: template "textStyles.overriddenSpoken" = "{name}, overridden"; } @@ -114,8 +120,9 @@ pub enum TextStyleAction { }, /// Clear Override, with a target whose style is overridden. ClearOverride, - /// Redefine: the current style takes the target's attributes. Not for character style 0. - Redefine, + /// Redefine: the style of this id takes the target's attributes: the current style from the footer and the panel + /// menu, the row's from its menu. Not for character style 0. + Redefine(u64), /// New: a style from the selected type when there is a target (`from_selection`), else a default one. The app /// opens its Style Options for the new id. #[non_exhaustive] @@ -192,9 +199,7 @@ impl Facts { let several = self.selected.len() > 1; match command { TextStyleCommand::ClearOverride => (self.target && self.overridden).then_some(TextStyleAction::ClearOverride), - TextStyleCommand::Redefine => { - (self.target && self.current.is_some_and(|id| id != 0 || self.paragraph)).then_some(TextStyleAction::Redefine) - } + TextStyleCommand::Redefine => self.current.filter(|id| self.target && (*id != 0 || self.paragraph)).map(TextStyleAction::Redefine), TextStyleCommand::New => Some(TextStyleAction::new_style(self.target, false)), TextStyleCommand::Duplicate if several => Some(TextStyleAction::DuplicateHighlighted), TextStyleCommand::Delete if several => Some(TextStyleAction::DeleteHighlighted), @@ -206,6 +211,28 @@ impl Facts { } } +/// The row menu's commands, in its two groups. +const ROW_MENU: [&[TextStyleCommand]; 2] = [ + &[TextStyleCommand::Options, TextStyleCommand::Duplicate, TextStyleCommand::Delete], + &[TextStyleCommand::Redefine, TextStyleCommand::ClearOverride], +]; + +impl Facts { + /// The action of the row menu's `command` on the style `row`, which the right-click made the lead of `selected`; + /// `None` when its condition doesn't hold. Redefine redefines the row's style, and Clear Override applies it without + /// the overrides where a click would apply it (`applies`); the rest act as the footer's. + fn row_action(&self, row: u64, command: TextStyleCommand, applies: bool) -> Option { + match command { + TextStyleCommand::ClearOverride => applies.then_some(TextStyleAction::apply(row, true)), + command => Facts { current: Some(row), picked: Some(row), ..self.clone() }.action(command), + } + } +} + +/// The row whose menu the last context request opened. +#[derive(Clone, Debug, Default)] +struct RowMenu(Option); + /// The footer's buttons: the start group from the start edge, the rest from the end edge. const FOOTER: [TextStyleCommand; 6] = [ TextStyleCommand::ClearOverride, @@ -237,6 +264,9 @@ const START_ACTIONS: usize = 2; /// - With [`highlighting`](Self::highlighting), Shift-click selects a range and Ctrl or Cmd-click adds or removes a /// row without applying. A row dragged onto New or Delete carries every selected style when it is one of them, /// else itself. +/// - Right-click: selects the row (unless it is selected) and opens its menu: Style Options…, Duplicate Style and Delete +/// Style act as the footer's with that row as the lead; Redefine Style redefines the row's style, and Clear Override +/// applies it without the overrides. /// - Footer: Clear Override and Redefine at the start edge; Style Options, Duplicate, New and Delete at the end edge, /// each disabled while its condition doesn't hold. The panel menu ([`TextStylesPanel::panel_rows`]) has the same commands under the same conditions. /// - Padding: none of its own. The panel is a dock body, docked or floating, and the dock group insets it. @@ -430,6 +460,12 @@ impl<'a> TextStylesPanel<'a> { if none { styled.ink(RowInk::Dimmed) } else { styled } }); let (events, second) = (shown_rows.events, shown_rows.activation_clicks >= 2); + if let Some(row) = events.iter().rev().find_map(|event| match event { + CollectionEvent::ContextMenu(request) => Some(request.key), + _ => None, + }) { + cx.memory::().0 = Some(row); + } let mods = craft_ui::runtime::pointer::mods(ui.ctx()); let alt = mods.alt; @@ -451,6 +487,37 @@ impl<'a> TextStylesPanel<'a> { _ => {} } } + + if let Some(row) = cx.memory::().0.filter(|row| rows.iter().any(|shown| shown.id == *row)) { + let selected = selection.reborrow().read(cx).and_then(|held| held.same().map(|held| held.items.clone())).unwrap_or_default(); + let facts = Facts { selected, ..cx.memory::().clone() }; + let applies = target && (paragraph || text_selected); + let label = |command: TextStyleCommand| match command { + TextStyleCommand::Options => &words.options, + TextStyleCommand::Duplicate => &words.menu_duplicate, + TextStyleCommand::Delete => &words.menu_delete, + TextStyleCommand::Redefine => &words.menu_redefine, + _ => &words.clear_override, + }; + let menu = Menu::context(words.row_menu.clone(), &shown_rows.response).show(ui, |ui, _| { + let mut ran = None; + for (at, group) in ROW_MENU.into_iter().enumerate() { + if at > 0 { + Divider::horizontal().show(ui); + } + for &command in group { + let found = facts.row_action(row, command, applies); + if MenuItem::new(label(command).clone()).enabled(found.is_some()).show(ui).ran { + ran = found; + } + } + } + ran + }); + if let Some(found) = menu.inner.flatten() { + action.set(Some(found)); + } + } }); TextStylesPanelOutput { response: shown.output.response, diff --git a/crates/text/tests/it/style_panel.rs b/crates/text/tests/it/style_panel.rs index 3b775270b..161745cf9 100644 --- a/crates/text/tests/it/style_panel.rs +++ b/crates/text/tests/it/style_panel.rs @@ -6,8 +6,8 @@ use std::rc::Rc; use craft_behavior::Selection; use craft_text::ui::style_panel::{TextStyleAction, TextStyleKind, TextStyleRow, TextStylesPanel}; -use craft_ui::contract::Bind; -use craft_ui::testing::{Fixture, trial}; +use craft_ui::contract::{Bind, Role}; +use craft_ui::testing::{Fixture, pointer, trial}; use egui::{Key, Modifiers}; /// The selection the panel shows, the actions it reported and the warnings it raised. @@ -93,3 +93,47 @@ fn every_attempt_to_apply_a_character_style_at_a_bare_caret_warns_and_applies_no assert_eq!(warnings(&state), 1, "Enter on the row warns"); assert!(state.borrow().1.is_empty(), "nothing is applied at a bare caret"); } + +/// What the item `item` of the menu a right-click on `row` opens reports. +fn menu_ran(fixture: &mut Fixture, state: &Rc, Vec)>>, row: &str, item: &str) -> Vec { + let at = fixture.by_label(row).expect("the row is shown").center(); + fixture.frame(vec![pointer::moved(at)]); + fixture.frame(vec![pointer::secondary(at, true)]); + fixture.frame(vec![pointer::secondary(at, false)]); + fixture.idle(2); + let rows = fixture.by_role(Role::MENU_ITEM); + let found = rows.iter().find(|target| fixture.tree().name_of(target.node) == Some(item)).copied(); + fixture.click(&found.unwrap_or_else(|| panic!("the menu has {item:?}"))).idle(4); + std::mem::take(&mut state.borrow_mut().1) +} + +/// A right-click on a row opens its menu: Redefine Style redefines that row's style, not the current one the footer's +/// Redefine takes, Clear Override applies it without the overrides, and on the character style None Redefine Style +/// does nothing. +#[trial] +fn a_rows_menu_redefines_and_clears_the_overrides_of_that_rows_style() { + let state = Rc::new(RefCell::new((Selection::::default(), Vec::::new()))); + let shared = state.clone(); + let mut fixture = Fixture::new(move |ui| { + let rows = [TextStyleRow::new(0, "None"), TextStyleRow::new(1, "Emphasis"), TextStyleRow::new(2, "Strong")]; + let mut guard = shared.borrow_mut(); + let (selection, actions) = &mut *guard; + let out = TextStylesPanel::new("Character Styles", TextStyleKind::Character, &rows, Bind::bound(selection)) + .current(Some(1), true) + .target(true) + .show(ui); + actions.extend(out.action); + }); + fixture.idle(10); + assert_eq!(menu_ran(&mut fixture, &state, "Strong", "Redefine Style"), vec![TextStyleAction::Redefine(2)], "the row's style is redefined"); + assert_eq!( + menu_ran(&mut fixture, &state, "Strong", "Clear Override"), + vec![TextStyleAction::apply(2, true)], + "the row's style applies without overrides" + ); + + let redefine = fixture.by_label("Redefine style by current selection").expect("the footer's Redefine"); + fixture.click(&redefine).idle(4); + assert_eq!(std::mem::take(&mut state.borrow_mut().1), vec![TextStyleAction::Redefine(1)], "the footer redefines the current style"); + assert_eq!(menu_ran(&mut fixture, &state, "None", "Redefine Style"), vec![], "None is never redefined"); +}