mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 07:35:02 +00:00
NamedPresetRow: Buttons and Menu styles, commands scoped to the row (F2 renames), prompt opens focused
This commit is contained in:
@@ -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};
|
||||
|
||||
@@ -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<dyn Fn(&str) -> Option<PresetError> + 'a>;
|
||||
|
||||
/// Whether a name may be used: the host's answer for a typed name.
|
||||
@@ -115,9 +131,11 @@ impl<K> Outcome for NamedPresetOutput<K> {
|
||||
struct Draft<K> {
|
||||
target: Option<K>,
|
||||
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<Label>, picker: PresetPicker<'a, K>) -> Self {
|
||||
NamedPresetRow { label: label.into(), picker, suggest_name: None, check_name: None, built_in: None, files: None }
|
||||
NamedPresetRow {
|
||||
label: label.into(),
|
||||
picker,
|
||||
suggest_name: None,
|
||||
check_name: None,
|
||||
built_in: None,
|
||||
files: None,
|
||||
style: NamedPresetStyle::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Shows the row and reports what the host carries out.
|
||||
pub fn show(self, ui: &mut Ui) -> NamedPresetOutput<K> {
|
||||
let NamedPresetRow { label, picker, suggest_name, check_name, built_in, files } = self;
|
||||
let NamedPresetRow { label, picker, suggest_name, check_name, built_in, files, style } = self;
|
||||
let env = Env::of(ui);
|
||||
let words = env.words::<PresetWords>();
|
||||
let common = env.words::<CommonWords>();
|
||||
@@ -173,7 +205,7 @@ impl<'a, K: Value + Eq + Hash + 'static> NamedPresetRow<'a, K> {
|
||||
let mut events: Vec<NamedPresetEvent<K>> = Vec::new();
|
||||
let mut from_picker: Vec<PresetEvent<K>> = Vec::new();
|
||||
|
||||
let shown = Field::new(label.clone()).show(ui, |ui, body| {
|
||||
let shown = Field::new(label.clone()).command_scope(NamedPresetCommands::SCOPE).show(ui, |ui, body| {
|
||||
if let Some(shown) = picker.as_mut() {
|
||||
held = shown.held_key(body);
|
||||
}
|
||||
@@ -185,9 +217,11 @@ impl<'a, K: Value + Eq + Hash + 'static> NamedPresetRow<'a, K> {
|
||||
picker = Some(shown.display_name(words.built_in.fill(&[("name", row.label.as_str())])));
|
||||
}
|
||||
let naming = body.memory::<Option<Draft<K>>>();
|
||||
Row::new().wrap(true).show(ui, |ui, _| {
|
||||
Row::new().wrap(true).show(ui, |ui, row| {
|
||||
let mut field: Option<Response> = None;
|
||||
if let Some(picker) = picker.take() {
|
||||
let out = picker.show(ui);
|
||||
field = Some(out.edit.response.clone());
|
||||
if let Some(key) = out.picked {
|
||||
if naming.as_ref().and_then(|draft| draft.target.as_ref()).is_some_and(|target| *target != key) {
|
||||
*naming = None;
|
||||
@@ -223,12 +257,14 @@ impl<'a, K: Value + Eq + Hash + 'static> NamedPresetRow<'a, K> {
|
||||
.id_salt("name-text")
|
||||
.select_on_focus()
|
||||
.submits()
|
||||
.default_stop()
|
||||
.show(ui)
|
||||
})
|
||||
.inner;
|
||||
let (mut submitted, mut escaped) = (false, false);
|
||||
if let Some(edit) = edit {
|
||||
if draft.focus {
|
||||
draft.focus = !focus::request(ui.ctx(), FocusTarget::Id(edit.response.id));
|
||||
}
|
||||
if let Some(value) = edit.value() {
|
||||
draft.text.clone_from(value);
|
||||
}
|
||||
@@ -253,35 +289,60 @@ impl<'a, K: Value + Eq + Hash + 'static> NamedPresetRow<'a, K> {
|
||||
}
|
||||
|
||||
let idle = naming.is_none();
|
||||
for (command, enabled) in
|
||||
[(NamedPresetCommands::New, idle), (NamedPresetCommands::Rename, user.is_some()), (NamedPresetCommands::Delete, user.is_some())]
|
||||
{
|
||||
row.publish(command, CommandState::new().enabled(enabled));
|
||||
}
|
||||
let mut asked: Vec<Ask> = Vec::new();
|
||||
let more = IconButton::new(IconId::new("more")).label(words.more.clone()).show(ui);
|
||||
let _ = Menu::new(words.more.clone()).opened_by(&more, Trigger::Click).show(ui, |ui, _| {
|
||||
if MenuItem::command(NamedPresetCommands::New).enabled(idle).show(ui).ran {
|
||||
asked.push(Ask::New);
|
||||
}
|
||||
if MenuItem::command(NamedPresetCommands::Rename).enabled(user.is_some()).show(ui).ran {
|
||||
asked.push(Ask::Rename);
|
||||
}
|
||||
if MenuItem::command(NamedPresetCommands::Delete).enabled(user.is_some()).show(ui).ran {
|
||||
asked.push(Ask::Delete);
|
||||
}
|
||||
let Some(files) = files else { return };
|
||||
let commands = files.commands();
|
||||
if !commands.is_empty() {
|
||||
Divider::horizontal().show(ui);
|
||||
}
|
||||
for command in commands {
|
||||
if MenuItem::command(command).enabled(files.enabled(command, held.as_ref())).show(ui).ran {
|
||||
asked.push(Ask::File(command));
|
||||
match style {
|
||||
NamedPresetStyle::Buttons => {
|
||||
for command in files.map(PresetFiles::commands).unwrap_or_default() {
|
||||
let enabled = files.is_some_and(|files| files.enabled(command, held.as_ref()));
|
||||
if clicked(&Button::new(Label::Empty).command(command).enabled(enabled).show(ui)) {
|
||||
asked.push(Ask::File(command));
|
||||
}
|
||||
}
|
||||
let _ = Button::new(Label::Empty).command(NamedPresetCommands::New).show(ui);
|
||||
if let Some(field) = &field {
|
||||
let _ = Menu::new(words.more.clone()).opened_by(field, Trigger::Context).show(ui, |ui, _| {
|
||||
let _ = MenuItem::command(NamedPresetCommands::Rename).show(ui);
|
||||
let _ = MenuItem::command(NamedPresetCommands::Delete).show(ui);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
NamedPresetStyle::Menu => {
|
||||
let more = IconButton::new(IconId::new("more")).label(words.more.clone()).show(ui);
|
||||
let _ = Menu::new(words.more.clone()).opened_by(&more, Trigger::Click).show(ui, |ui, _| {
|
||||
let _ = MenuItem::command(NamedPresetCommands::New).label(words.save_as_new.clone()).show(ui);
|
||||
let _ = MenuItem::command(NamedPresetCommands::Rename).show(ui);
|
||||
let _ = MenuItem::command(NamedPresetCommands::Delete).show(ui);
|
||||
let Some(files) = files else { return };
|
||||
let commands = files.commands();
|
||||
if !commands.is_empty() {
|
||||
Divider::horizontal().show(ui);
|
||||
}
|
||||
for command in commands {
|
||||
if MenuItem::command(command).enabled(files.enabled(command, held.as_ref())).show(ui).ran {
|
||||
asked.push(Ask::File(command));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
while let Some((command, _)) = row.take_command::<NamedPresetCommands>() {
|
||||
asked.push(match command {
|
||||
NamedPresetCommands::New => Ask::New,
|
||||
NamedPresetCommands::Rename => Ask::Rename,
|
||||
NamedPresetCommands::Delete => Ask::Delete,
|
||||
});
|
||||
}
|
||||
for ask in asked {
|
||||
match ask {
|
||||
Ask::New => *naming = Some(Draft { target: None, text: suggestion.to_owned() }),
|
||||
Ask::New => *naming = Some(Draft { target: None, text: suggestion.to_owned(), focus: true }),
|
||||
Ask::Rename => {
|
||||
if let Some(row) = user {
|
||||
*naming = Some(Draft { target: Some(row.key.clone()), text: row.label.clone() });
|
||||
*naming = Some(Draft { target: Some(row.key.clone()), text: row.label.clone(), focus: true });
|
||||
}
|
||||
}
|
||||
Ask::Delete => {
|
||||
|
||||
@@ -1,26 +1,53 @@
|
||||
//! Stories of `NamedPresetRow`.
|
||||
|
||||
use super::{NamedPresetRow, PresetError};
|
||||
use super::{NamedPresetRow, NamedPresetStyle, PresetError};
|
||||
use crate::contract::Bind;
|
||||
use crate::list::ThumbAspect;
|
||||
use crate::patterns::presets::{PickerField, PresetPicker, PresetRow, PresetState};
|
||||
use crate::patterns::presets::{PickerField, PresetExport, PresetFiles, PresetPicker, PresetRow, PresetState};
|
||||
use crate::testing::pictures::paint_contour;
|
||||
use crate::testing::{Drive, StoryData, StoryOutput, story};
|
||||
use egui::Ui;
|
||||
|
||||
const LINEAR: u32 = 1;
|
||||
const MINE: u32 = 2;
|
||||
const TRACE: [&str; 6] = ["Default", "High Fidelity Photo", "Low Fidelity Photo", "3 Colors", "Sketched Art", "Line Art"];
|
||||
|
||||
fn taken(rows: &[PresetRow<u32>], name: &str) -> Option<PresetError> {
|
||||
rows.iter().any(|row| row.label.eq_ignore_ascii_case(name)).then_some(PresetError::NameTaken)
|
||||
}
|
||||
|
||||
/// The contour editor's row: a thumbnail picker, then Load…, Save… and New… as buttons.
|
||||
#[story(NamedPresetRow<'static, u32>, covers = [], drive = Drive::Never)]
|
||||
fn named_preset_row(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let thumb = |row: PresetRow<u32>, n: u32| row.preview(ThumbAspect::SQUARE, move |cx, rect| paint_contour(cx, rect, n));
|
||||
let rows = vec![thumb(PresetRow::item(LINEAR, "Linear", 0), 0), thumb(PresetRow::item(MINE, "Mine", 0), 1)];
|
||||
let (contour, state) = data.value("contour", (Some(MINE), PresetState::<u32>::new()));
|
||||
let picker = PresetPicker::new("Contour", Bind::bound(contour), &rows, Bind::bound(state)).field(PickerField::ThumbnailAndName);
|
||||
NamedPresetRow::new("Contour preset", picker)
|
||||
.suggest_name("Contour 3")
|
||||
.check_name(|name| name.eq_ignore_ascii_case("linear").then_some(PresetError::NameTaken))
|
||||
.built_in(|key| *key == LINEAR)
|
||||
fn contour_buttons(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let rows: Vec<PresetRow<u32>> = (1..=4_u32)
|
||||
.map(|n| PresetRow::item(n, format!("Contour {n}"), 0).preview(ThumbAspect::SQUARE, move |cx, rect| paint_contour(cx, rect, n)))
|
||||
.collect();
|
||||
let files =
|
||||
PresetFiles::new("contours", "Contours", &["shc"]).export(|_| Some(PresetExport { file_name: "Contours.shc".to_owned(), bytes: Vec::new() }));
|
||||
let (contour, state) = data.value("contour", (None::<u32>, PresetState::<u32>::new()));
|
||||
let picker = PresetPicker::new("Contour", Bind::bound(contour), &rows, Bind::bound(state)).field(PickerField::Thumbnail);
|
||||
NamedPresetRow::new("Contour", picker)
|
||||
.style(NamedPresetStyle::Buttons)
|
||||
.suggest_name("Contour 5")
|
||||
.check_name(|name| taken(&rows, name))
|
||||
.built_in(|_| true)
|
||||
.files(&files)
|
||||
.show(ui)
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Image Trace's row: a name picker and one Manage presets menu.
|
||||
#[story(NamedPresetRow<'static, u32>, covers = [], drive = Drive::Never)]
|
||||
fn trace_menu(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let rows: Vec<PresetRow<u32>> = (1..).zip(TRACE).map(|(n, name)| PresetRow::item(n, name, 0)).collect();
|
||||
let files = PresetFiles::new("trace-presets", "Trace presets", &["json"])
|
||||
.export(|_| Some(PresetExport { file_name: "Trace.json".to_owned(), bytes: Vec::new() }));
|
||||
let (preset, state) = data.value("preset", (Some(1_u32), PresetState::<u32>::new()));
|
||||
let picker = PresetPicker::new("Trace preset", Bind::bound(preset), &rows, Bind::bound(state)).field(PickerField::Name);
|
||||
NamedPresetRow::new("Trace preset", picker)
|
||||
.style(NamedPresetStyle::Menu)
|
||||
.suggest_name("Preset 7")
|
||||
.check_name(|name| taken(&rows, name))
|
||||
.built_in(|_| true)
|
||||
.files(&files)
|
||||
.show(ui)
|
||||
.into()
|
||||
}
|
||||
|
||||
@@ -41,5 +41,6 @@ craft_ui::words! {
|
||||
name_taken: "namedPresetRow.nameTaken" = "A preset with this name already exists.";
|
||||
name_invalid: "namedPresetRow.nameInvalid" = "This name can't be used.";
|
||||
more: "namedPresetRow.more" = "Manage presets";
|
||||
save_as_new: "namedPresetRow.saveAsNew" = "Save as New Preset…";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
//! The data and rules of the presets family.
|
||||
|
||||
mod model;
|
||||
mod named;
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
//! `NamedPresetRow`: F2 on the picker renames a user preset and nothing else; New… opens the name prompt.
|
||||
|
||||
use craft_ui::contract::{Bind, Role};
|
||||
use craft_ui::patterns::presets::{NamedPresetEvent, NamedPresetRow, NamedPresetStyle, PresetPicker, PresetRow, PresetState};
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use egui::{Key, Modifiers};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
const LINEAR: u32 = 1;
|
||||
const MINE: u32 = 2;
|
||||
|
||||
struct Rig {
|
||||
fixture: Fixture,
|
||||
events: Rc<RefCell<Vec<NamedPresetEvent<u32>>>>,
|
||||
}
|
||||
|
||||
/// A row in `style` on the preset `current`: Linear is built in, Mine is the user's.
|
||||
fn rig(style: NamedPresetStyle, current: u32) -> Rig {
|
||||
let events = Rc::new(RefCell::new(Vec::new()));
|
||||
let seen = events.clone();
|
||||
let rows = vec![PresetRow::item(LINEAR, "Linear", 0), PresetRow::item(MINE, "Mine", 0)];
|
||||
let mut value = Some(current);
|
||||
let mut state = PresetState::<u32>::new();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let picker = PresetPicker::new("Contour", Bind::bound(&mut value), &rows, Bind::bound(&mut state));
|
||||
let out = NamedPresetRow::new("Contour preset", picker).style(style).suggest_name("Contour 3").built_in(|key| *key == LINEAR).show(ui);
|
||||
seen.borrow_mut().extend(out.events);
|
||||
});
|
||||
fixture.settle();
|
||||
Rig { fixture, events }
|
||||
}
|
||||
|
||||
impl Rig {
|
||||
/// The name prompt's text, when the prompt is open.
|
||||
fn prompt(&self) -> Option<String> {
|
||||
let field = self.fixture.by_role(Role::TEXT_INPUT).into_iter().next()?;
|
||||
Some(self.fixture.tree().value_of(field.node).unwrap_or_default().to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn f2_on_the_picker_renames_a_user_preset_and_enter_reports_the_name() {
|
||||
let mut rig = rig(NamedPresetStyle::Buttons, MINE);
|
||||
assert_eq!(rig.prompt(), None, "the row opens without the prompt");
|
||||
rig.fixture.key(Key::Tab, Modifiers::NONE).settle();
|
||||
rig.fixture.key(Key::F2, Modifiers::NONE).settle();
|
||||
assert_eq!(rig.prompt().as_deref(), Some("Mine"), "F2 opened the prompt with the preset's name");
|
||||
rig.fixture.type_text("Ridge").settle();
|
||||
rig.fixture.key(Key::Enter, Modifiers::NONE).settle();
|
||||
let events = rig.events.borrow();
|
||||
assert!(
|
||||
events.iter().any(|event| matches!(event, NamedPresetEvent::Rename { key: MINE, text, .. } if text == "Ridge")),
|
||||
"Enter renamed the preset: {events:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn f2_on_a_built_in_preset_opens_nothing() {
|
||||
let mut rig = rig(NamedPresetStyle::Menu, LINEAR);
|
||||
rig.fixture.key(Key::Tab, Modifiers::NONE).settle();
|
||||
rig.fixture.key(Key::F2, Modifiers::NONE).settle();
|
||||
assert_eq!(rig.prompt(), None, "a built-in preset has no rename");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn the_new_button_opens_the_prompt_with_the_suggestion() {
|
||||
let mut rig = rig(NamedPresetStyle::Buttons, LINEAR);
|
||||
let new = rig.fixture.by_label("New…").expect("the Buttons style shows New…");
|
||||
rig.fixture.click(&new).settle();
|
||||
assert_eq!(rig.prompt().as_deref(), Some("Contour 3"), "New… opened the prompt with the suggested name");
|
||||
}
|
||||
Reference in New Issue
Block a user