From 9acaa83178a3b76db964f6c1d838acb2293ea85c Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 02:03:55 +0000 Subject: [PATCH] NamedPresetRow: Buttons and Menu styles, commands scoped to the row (F2 renames), prompt opens focused --- .../ui-patterns/src/patterns/presets/mod.rs | 2 +- .../ui-patterns/src/patterns/presets/named.rs | 129 +++++++++++++----- .../src/patterns/presets/named/stories.rs | 53 +++++-- .../ui-patterns/src/patterns/presets/words.rs | 1 + crates/ui-patterns/tests/it/presets/mod.rs | 1 + crates/ui-patterns/tests/it/presets/named.rs | 72 ++++++++++ 6 files changed, 210 insertions(+), 48 deletions(-) create mode 100644 crates/ui-patterns/tests/it/presets/named.rs diff --git a/crates/ui-patterns/src/patterns/presets/mod.rs b/crates/ui-patterns/src/patterns/presets/mod.rs index a135be754..3a7cede9a 100644 --- a/crates/ui-patterns/src/patterns/presets/mod.rs +++ b/crates/ui-patterns/src/patterns/presets/mod.rs @@ -25,7 +25,7 @@ pub use model::{ CellShape, GroupPath, PresetChange, PresetError, PresetGroup, PresetId, PresetItem, PresetKey, PresetKind, PresetLibrary, PresetRow, Preview, PreviewPaint, ThumbInfo, children_end, has_captions, nest_allowed, }; -pub use named::{NameCheck, NamedPresetCommands, NamedPresetEvent, NamedPresetOutput, NamedPresetRow}; +pub use named::{NameCheck, NamedPresetCommands, NamedPresetEvent, NamedPresetOutput, NamedPresetRow, NamedPresetStyle}; pub use panel_menu::{PresetCommands, PresetMenuAction, PresetPanelMenu}; pub use picker::{PickerField, PickerHead, PresetPicker, PresetPickerOutput, PresetPickerStyle}; pub use state::{DefaultOpen, PresetState, PresetView, default_is_open}; diff --git a/crates/ui-patterns/src/patterns/presets/named.rs b/crates/ui-patterns/src/patterns/presets/named.rs index 5789e2ad7..ef92e4272 100644 --- a/crates/ui-patterns/src/patterns/presets/named.rs +++ b/crates/ui-patterns/src/patterns/presets/named.rs @@ -1,4 +1,5 @@ -//! `NamedPresetRow`: a preset picker with a name prompt and a menu of New, Rename, Delete and the file entries. +//! `NamedPresetRow`: a preset picker with a name prompt and the commands New, Rename, Delete and the file entries, as +//! buttons or one menu. //! //! The host keeps the library and performs every event the row reports. New and Rename open the prompt: Enter or OK //! commits the name, Escape or Cancel closes it, and a name the host refuses shows its message under the field and @@ -20,7 +21,9 @@ use crate::form::Field; use crate::layout_components::Row; use crate::list::RowTest; use crate::menu::{Menu, MenuItem}; +use crate::runtime::commands::{CommandSet as _, CommandState, Commands as _}; use crate::runtime::env::{Env, Label, Message}; +use crate::runtime::focus::{self, FocusTarget}; use crate::runtime::words::CommonWords; use crate::text::TextField; use craft_behavior::{IconId, Value}; @@ -28,10 +31,10 @@ use craft_theme::Variant; use egui::{Response, Ui}; use std::hash::Hash; -/// The commands of the row's menu. They are app commands: the row reports a run row as the event of the same name, -/// and a host that binds a key to one (F2 renames) handles it as it handles the event. +/// The commands of a row, run on the row that holds the focus: its menu rows, its buttons and the chords the keymap +/// binds (F2 renames). The row reports each as the event of the same name. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, craft_ui_macros::CommandSet)] -#[command(prefix = "namedPreset")] +#[command(prefix = "namedPreset", scope = "namedPreset")] #[non_exhaustive] pub enum NamedPresetCommands { /// Ask for a name and make a preset of the current settings. @@ -45,6 +48,19 @@ pub enum NamedPresetCommands { Delete, } +/// How a [`NamedPresetRow`] lays out its commands. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +#[non_exhaustive] +pub enum NamedPresetStyle { + /// Buttons after the picker: the file entries, then New…. Rename… and Delete are the picker's context menu + /// (Photoshop's Contour Editor). + #[default] + Buttons, + /// One "Manage presets" button with a menu: Save as New Preset…, Rename…, Delete, then the file entries + /// (Illustrator's Image Trace). + Menu, +} + type NameCheckFn<'a> = Box Option + 'a>; /// Whether a name may be used: the host's answer for a typed name. @@ -115,9 +131,11 @@ impl Outcome for NamedPresetOutput { struct Draft { target: Option, text: String, + /// The prompt has just opened and asks for the focus. + focus: bool, } -/// What a menu row asked for in this pass. +/// What a command of the row asked for in this pass. enum Ask { New, Rename, @@ -128,11 +146,14 @@ enum Ask { /// A preset picker with the commands that make, rename and delete presets. /// /// - **Picker.** The host builds the `PresetPicker` and its bindings; a pick comes back as `Apply`. -/// - **Prompt.** New opens the name field with the suggestion; Rename opens it with the current name. The host's +/// - **Prompt.** New opens the name field, focused, with the suggestion; Rename opens it with the current name. The host's /// `check_name` refuses a name, and the message shows through the field. A name equal to the current name, ignoring /// case, is no clash with itself, and the same name changes nothing. /// - **Built-in presets.** `built_in` presets show "(built-in)" after their name and cannot be renamed or deleted. -/// - **Files.** `files` adds its Load, Replace and Save entries to the menu. +/// - **Files.** `files` adds its Load, Replace and Save entries. +/// - **Style.** `Buttons` (the default) puts the file entries and New… as buttons after the picker, with Rename… and +/// Delete as the picker's context menu; `Menu` puts them all in one "Manage presets" menu. +/// - **Keys.** F2 on the picker renames a user preset ([`NamedPresetCommands`] has the keys). #[must_use = "show the row with `show`"] #[derive(crate::contract::Pattern, crate::contract::Props)] #[pattern(probe(NamedPresetRow<'static, u32>))] @@ -151,17 +172,28 @@ pub struct NamedPresetRow<'a, K: Value + Eq + Hash + 'static> { /// The file entries of the menu. #[prop] files: Option<&'a PresetFiles<'a, K>>, + /// How the commands are laid out. + #[prop] + style: NamedPresetStyle, } impl<'a, K: Value + Eq + Hash + 'static> NamedPresetRow<'a, K> { /// A row named `label` around `picker`. pub fn new(label: impl Into