Files
craft-libs/crates/ui-patterns/tests/it/presets/named.rs
T
aiongg bae11d7916 Preset lists as sources: PresetSource keys a browser's rows, the list runs as a memoized view
PresetBrowser, PresetPicker, PresetManager and BrushPresetPicker take a PresetSource (the host's
MemoProps inputs and capture-free fns that build the rows and answer can_rename, can_delete,
can_move, accepts and thumb), so the browser's grid or list, its drop zone and row menu run in
a memoized view keyed on the inputs. Rows are kept per inputs and scope units (runtime::derived).
The Brush Preset picker's head and its search-row tools are memoized views of their own; the gear
rows are a fn.

Work in progress: the remaining callers (form dialogs, color-ui, draw gradients and patterns,
text style panel, imaging layer styles) are not moved yet and do not build.
2026-11-21 03:20:37 +00:00

75 lines
3.0 KiB
Rust

//! `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, PresetFns, PresetPicker, PresetRow, PresetSource, 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 = PresetSource::new((), PresetFns::new(|_, _| 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");
}