ui-patterns: a custom row's CustomContext::request_action reaches HostedParamDialog::take_actions

This commit is contained in:
bflatastic
2026-11-22 04:20:30 +07:00
parent 547deb28b6
commit d0e5b48039
5 changed files with 63 additions and 8 deletions
@@ -1055,8 +1055,7 @@ impl HostedParamDialog {
/// The ids of the actions the custom rows requested since the last call (`CustomContext::request_action`).
pub fn take_actions(&mut self) -> Vec<String> {
let _ = self;
todo!("ex.FA1")
self.requests.take_actions()
}
/// Makes OK file its answer for the host to take instead of closing: the host checks it and answers with
@@ -43,6 +43,8 @@ pub struct HostRequests {
/// The previewed colour of each colour row a child picker edits, by row key; one entry per key.
previews: Vec<(String, Srgba)>,
answers: Vec<HostAnswer>,
/// The ids of the actions custom rows asked the host to run, in the order asked.
actions: Vec<String>,
}
impl HostRequests {
@@ -69,6 +71,12 @@ impl HostRequests {
self.help_link = Some(link);
}
self.sampling_ended |= std::mem::take(&mut rows.sampling_ended);
self.actions.append(&mut rows.actions);
}
/// The ids of the actions custom rows asked the host to run since the last call, once, in the order asked.
pub fn take_actions(&mut self) -> Vec<String> {
std::mem::take(&mut self.actions)
}
/// The help link a row's button reported, once.
@@ -145,7 +153,9 @@ impl HostRequests {
/// Moves the requests and answers of `other` in; its requests replace the ones not taken yet.
pub fn extend(&mut self, other: HostRequests) {
let HostRequests { color, path, eyedropper, listing, preview_click, footer_action, help_link, sampling_ended, previews, answers } = other;
let HostRequests { color, path, eyedropper, listing, preview_click, footer_action, help_link, sampling_ended, previews, answers, actions } =
other;
self.actions.extend(actions);
self.sampling_ended |= sampling_ended;
self.help_link = help_link.or(self.help_link.take());
self.color = color.or(self.color.take());
@@ -420,6 +420,8 @@ pub struct CustomContext {
sampling_ended: bool,
/// The gesture steps the row's controls reported ([`CustomContext::gesture`]).
gestures: Vec<Lifted>,
/// The ids of the actions the row asked the host to run ([`CustomContext::request_action`]).
actions: Vec<String>,
}
/// One edit event of a custom row's control with its value erased.
@@ -436,7 +438,7 @@ pub(crate) struct Lifted {
impl CustomContext {
/// The context of a row labelled `title`; `sampling_ended` says an Escape ended sampling in the previous pass.
pub(crate) fn new(title: Label, canvas: Option<egui::Id>, sampling_ended: bool) -> Self {
CustomContext { title, request: None, canvas, eyedropper: None, sampling: false, sampling_ended, gestures: Vec::new() }
CustomContext { title, request: None, canvas, eyedropper: None, sampling: false, sampling_ended, gestures: Vec::new(), actions: Vec::new() }
}
/// The gesture steps the row reported, once.
@@ -447,8 +449,12 @@ impl CustomContext {
/// Asks the host to run the action `id`; the host takes this pass's requests once with
/// `HostedParamDialog::take_actions`.
pub fn request_action(&mut self, id: impl Into<String>) {
let _ = (self, id);
todo!("ex.FA1")
self.actions.push(id.into());
}
/// The ids of the actions the row asked for, once.
pub(crate) fn take_actions(&mut self) -> Vec<String> {
std::mem::take(&mut self.actions)
}
/// Arms the host's eyedropper for the colour under `request.key`, as a colour row's dropper toggle does.
@@ -89,6 +89,9 @@ pub struct ParamRowsOutput {
pub help_link: Option<String>,
/// An Escape ended a row's sampling in this pass: the dialog stays open, and the next Escape cancels as usual.
pub sampling_ended: bool,
/// The ids of the actions custom rows asked the host to run in this pass (`CustomContext::request_action`), in
/// the order asked.
pub actions: Vec<String>,
}
impl Outcome for ParamRowsOutput {
@@ -245,8 +248,11 @@ impl<'a> ParamRows<'a> {
})
};
let shown = if read_only { with_env(ui, |scope| scope.read_only = Some(true), group) } else { group(ui) };
let (pick_color, path, eyedropper, listing, out_of_range, help_link) =
if read_only { Default::default() } else { (pass.color, pass.path, pass.eyedropper, pass.listing, pass.out_of_range, pass.help_link) };
let (pick_color, path, eyedropper, listing, out_of_range, help_link, actions) = if read_only {
Default::default()
} else {
(pass.color, pass.path, pass.eyedropper, pass.listing, pass.out_of_range, pass.help_link, pass.actions)
};
ParamRowsOutput {
response: shown.output,
changed,
@@ -260,6 +266,7 @@ impl<'a> ParamRows<'a> {
unparsed: pass.unparsed,
help_link,
sampling_ended: pass.sampling_ended,
actions,
}
}
}
@@ -314,6 +321,8 @@ struct Pass<'p> {
unparsed: bool,
/// The help link a row's button reported.
help_link: Option<String>,
/// The actions custom rows asked the host to run.
actions: Vec<String>,
/// An Escape ended sampling in this pass.
sampling_ended: bool,
/// An Escape ended sampling in the previous pass.
@@ -419,6 +428,7 @@ impl<'p> Pass<'p> {
events: EditEvents::new(),
unparsed: false,
help_link: None,
actions: Vec::new(),
sampling_ended: false,
ended_prev: false,
pending: EditEvents::new(),
@@ -436,6 +446,7 @@ impl<'p> Pass<'p> {
self.events = EditEvents::new();
self.unparsed = false;
self.help_link = None;
self.actions.clear();
self.sampling_ended = false;
self.pending = EditEvents::new();
self.locked = false;
@@ -1267,6 +1278,7 @@ impl<'p> Pass<'p> {
self.eyedropper = Some(request);
}
self.sampling |= context.is_sampling();
self.actions.extend(context.take_actions());
}
}
@@ -436,6 +436,34 @@ fn a_hosted_dialogs_new_requests_come_once_and_a_cancel_drops_them() {
assert_eq!(on_dialog(&host, HostedParamDialog::take_listing).flatten(), None, "a cancel drops the listing request");
}
/// A dialog with a custom row whose button asks the host to run the action `pen`.
fn pen_dialog(fields: &mut DialogFields) -> ParamDialog<'_> {
let pen = Param::custom("pen", "Pen", |ui, _, cx| {
let pressed = Button::new("Pen…").show(ui);
if clicked(&pressed) {
cx.request_action("pen");
}
pressed.response().clone()
});
ParamDialog::new("pens", "Pens", vec![pen]).values(Bind::bound(fields))
}
// A custom row's action request reaches the host once, in a frame that runs twice too.
#[trial]
fn a_custom_rows_action_request_reaches_the_host_once() {
let (host, twice) = (Rc::new(RefCell::new(DialogHost::new())), Rc::new(Cell::new(false)));
let mut fixture = host_fixture(&host, &twice);
host.borrow_mut().open(HostedParamDialog::new(fixture.ctx(), "pens", DialogFields::new(), pen_dialog));
fixture.idle(10);
let pen = fixture.by_label("Pen").expect("the row's button, named by the row");
twice.set(true);
fixture.click(&pen);
twice.set(false);
fixture.idle(4);
assert_eq!(on_dialog(&host, HostedParamDialog::take_actions), Some(vec!["pen".to_owned()]), "the press asks for the action");
assert_eq!(on_dialog(&host, HostedParamDialog::take_actions), Some(Vec::new()), "the request is taken once");
}
/// A dialog with a radio row whose second option is disabled.
fn brush_tip(fields: &mut DialogFields) -> ParamDialog<'_> {
let options = vec![("round".to_owned(), Label::from("Round")), ("square".to_owned(), Label::from("Square"))];