CommandPalette: recents kept for the session when the host binds none

This commit is contained in:
aiongg
2026-11-12 14:32:45 +00:00
parent c8f2a28def
commit 54a549100c
3 changed files with 69 additions and 13 deletions
@@ -69,8 +69,8 @@ impl crate::contract::Outcome for PaletteOutput {
/// - Keys: the field keeps the focus; Up, Down, Page Up, Page Down move the active row (Up and Down wrap at the ends),
/// Enter picks it, Escape clears
/// the query and, on an empty query, closes the box; a press outside closes it. A caption under the list names the keys. The number of results is announced politely.
/// - Picking: the output's `picked` names the command and the box closes. With `recents` bound the command is also
/// put first in the list; [`CommandPalette::remember`] is the rule.
/// - Picking: the output's `picked` names the command, the box closes and the command goes first in `recents`;
/// [`CommandPalette::remember`] is the rule. Without a binding the palette keeps `recents` for the session.
#[derive(Pattern, Props)]
pub struct CommandPalette<'a> {
label: Label,
@@ -78,13 +78,13 @@ pub struct CommandPalette<'a> {
open: Bind<'a, bool>,
/// The commands last run, newest first.
#[prop]
recents: Option<Bind<'a, Vec<CommandRef>>>,
recents: Bind<'a, Vec<CommandRef>>,
}
impl<'a> CommandPalette<'a> {
/// A palette named `label` over `entries`, open while `open` is true.
pub fn new(label: impl Into<Label>, entries: &'a [HelpEntry], open: Bind<'a, bool>) -> Self {
CommandPalette { label: label.into(), entries, open, recents: None }
CommandPalette { label: label.into(), entries, open, recents: Bind::kept() }
}
/// Puts `command` first in `recents`, once, keeping the newest `RECENT`.
@@ -110,8 +110,7 @@ impl<'a> CommandPalette<'a> {
overlay
})
.show(ui, |ui, cx| {
let recent: Vec<CommandRef> =
recents.as_mut().and_then(|bind| bind.reborrow().read(cx)).and_then(|held| held.same().cloned()).unwrap_or_default();
let recent: Vec<CommandRef> = recents.reborrow().read(cx).and_then(|held| held.same().cloned()).unwrap_or_default();
let texts = texts_of(entries, &cx.env);
let folder = cx.env.folder();
// The field is the stack's header and the key hint its footer, so the list takes the room left in
@@ -154,13 +153,11 @@ impl<'a> CommandPalette<'a> {
});
if let Some(command) = &picked {
cx.request_close();
if let Some(bind) = recents.as_mut() {
let before = recent.clone();
let mut after = recent;
Self::remember(&mut after, command.clone());
let name: Arc<str> = Arc::from(label.as_str());
let _ = bind.reborrow().write(cx, &EditEvent::set(Mixed::Same(before), after, name, None));
}
let before = recent.clone();
let mut after = recent;
Self::remember(&mut after, command.clone());
let name: Arc<str> = Arc::from(label.as_str());
let _ = recents.reborrow().write(cx, &EditEvent::set(Mixed::Same(before), after, name, None));
}
});
let closed = shown.output.closed;
@@ -1,6 +1,7 @@
//! The rules of the panels family: an inspector keys its controls by the target and shows the empty notice for no
//! target.
mod palette;
mod tour;
use craft_ui::choice::Checkbox;
@@ -0,0 +1,58 @@
//! A palette without a `recents` binding still remembers what it ran.
use craft_ui::contract::{Bind, CommandSet, Role};
use craft_ui::menu::search::HelpEntry;
use craft_ui::patterns::panels::CommandPalette;
use craft_ui::runtime::commands::CommandRef;
use craft_ui::testing::{Fixture, trial};
use egui::{Key, Modifiers};
use std::cell::{Cell, RefCell};
use std::rc::Rc;
/// Commands that may run while the palette's modal layer is open.
#[derive(Clone, Copy, Debug, PartialEq, Eq, CommandSet)]
#[command(prefix = "paletteTrial")]
#[non_exhaustive]
enum TrialCommand {
#[command(label = "Rotate", modal = "always")]
Rotate,
#[command(label = "Flip", modal = "always")]
Flip,
#[command(label = "Crop", modal = "always")]
Crop,
}
#[trial]
fn a_picked_command_comes_first_when_the_palette_opens_again() {
let entries = vec![
HelpEntry::new(TrialCommand::Rotate, "Rotate").with_path(["Image"]),
HelpEntry::new(TrialCommand::Flip, "Flip").with_path(["Image"]),
HelpEntry::new(TrialCommand::Crop, "Crop").with_path(["Image"]),
];
let open = Rc::new(Cell::new(true));
let picked = Rc::new(RefCell::new(None::<CommandRef>));
let (shown_open, shown_picked) = (open.clone(), picked.clone());
let mut fixture = Fixture::new(move |ui| {
let mut is_open = shown_open.get();
let output = CommandPalette::new("Commands", &entries, Bind::bound(&mut is_open)).show(ui);
shown_open.set(is_open);
if let Some(command) = output.picked {
*shown_picked.borrow_mut() = Some(command);
}
});
fixture.settle();
let first_row = |fixture: &Fixture| {
let rows = fixture.by_role(Role::LIST_BOX_OPTION);
rows.first().and_then(|row| fixture.tree().name_of(row.node)).map(str::to_owned).unwrap_or_default()
};
assert!(first_row(&fixture).starts_with("Rotate"), "with nothing run, the entries come in their order");
fixture.type_text("Flip").settle();
fixture.key(Key::Enter, Modifiers::NONE).settle();
assert_eq!(*picked.borrow(), Some(CommandRef::from(TrialCommand::Flip)), "Enter picked the match");
assert!(!open.get(), "picking closed the palette");
open.set(true);
fixture.settle();
assert!(first_row(&fixture).starts_with("Flip"), "the command just run leads the empty query");
}