mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-30 18:27:00 +01:00
dialogs-w17: net change of 2 commits
- dialogs-w17: written by an agent - dialogs-w17: review fixes (review-1)
This commit is contained in:
1 parent
7632e28b85
commit
0dc0a30a96
4 files changed
+115
-17
No files matched your search
@@ -129,6 +129,13 @@ pub trait StackDialog: Any + Send {
|
||||
/// dialog's own Cancel button does this itself before it returns. Does nothing by default.
|
||||
fn cancelled(&mut self, _ctx: &Context) {}
|
||||
|
||||
/// The requests the dialog raised for its app since the last call (a list edit that takes effect at once, say),
|
||||
/// each taken once. The host keeps what is undelivered when the dialog closes, however it closes
|
||||
/// ([`DialogHost::take_actions`]). Empty by default.
|
||||
fn take_actions(&mut self) -> Vec<Value> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
/// Shows `color` live in the dialog's colour row `key` while a child picker edits it (`None` ends the preview).
|
||||
/// Hosted dialogs with colour rows override it; does nothing by default.
|
||||
fn preview_color(&mut self, key: &str, color: Option<craft_theme::Srgba>) {
|
||||
@@ -269,6 +276,8 @@ trait AnyStackDialog: Send {
|
||||
fn cancelled(&self) -> Box<dyn Any + Send>;
|
||||
/// [`StackDialog::cancelled`].
|
||||
fn release(&mut self, ctx: &Context);
|
||||
/// [`StackDialog::take_actions`].
|
||||
fn take_actions(&mut self) -> Vec<Value>;
|
||||
fn preview_color(&mut self, key: &str, color: Option<craft_theme::Srgba>);
|
||||
fn take_color_request(&mut self) -> Option<ColorRequest>;
|
||||
fn answer_color(&mut self, key: &str, color: craft_theme::Srgba);
|
||||
@@ -332,6 +341,10 @@ impl<D: StackDialog> AnyStackDialog for D {
|
||||
StackDialog::cancelled(self, ctx);
|
||||
}
|
||||
|
||||
fn take_actions(&mut self) -> Vec<Value> {
|
||||
StackDialog::take_actions(self)
|
||||
}
|
||||
|
||||
fn cancelled(&self) -> Box<dyn Any + Send> {
|
||||
Box::new(DialogResult::<D::Result>::Cancel)
|
||||
}
|
||||
@@ -422,6 +435,8 @@ pub struct DialogHost {
|
||||
/// Dialogs [`close_by_id`](DialogHost::close_by_id) asked to close, closed by the next `run`
|
||||
/// (which has the `Context` a close needs).
|
||||
closing: Vec<DialogId>,
|
||||
/// Actions of dialogs that closed before the app took them, by dialog id, in the order raised.
|
||||
kept_actions: Vec<(DialogId, Vec<Value>)>,
|
||||
}
|
||||
|
||||
/// One open dialog as automation sees it ([`DialogHost::open_dialogs`]).
|
||||
@@ -541,9 +556,9 @@ impl DialogHost {
|
||||
continue;
|
||||
};
|
||||
self.remove_tree(id);
|
||||
let dialog = self.open.remove(at);
|
||||
let mut dialog = self.open.remove(at);
|
||||
crate::runtime::overlay::end_stack_dialog(&ctx, id);
|
||||
self.store_result(dialog.as_ref(), result);
|
||||
self.store_result(&mut *dialog, result);
|
||||
self.cancel_orphans(Some(&ctx));
|
||||
}
|
||||
if self.open_next_queued() {
|
||||
@@ -632,9 +647,9 @@ impl DialogHost {
|
||||
let Some(result) = self.open[at].confirm_erased(ctx) else { return false };
|
||||
let id = self.open[at].id();
|
||||
self.remove_tree(id);
|
||||
let dialog = self.open.remove(at);
|
||||
let mut dialog = self.open.remove(at);
|
||||
crate::runtime::overlay::end_stack_dialog(ctx, id);
|
||||
self.store_result(dialog.as_ref(), result);
|
||||
self.store_result(&mut *dialog, result);
|
||||
self.cancel_orphans(Some(ctx));
|
||||
true
|
||||
}
|
||||
@@ -680,7 +695,8 @@ impl DialogHost {
|
||||
let mut dialog = self.open.remove(at);
|
||||
crate::runtime::overlay::end_stack_dialog(ctx, id);
|
||||
dialog.release(ctx);
|
||||
self.store_result(dialog.as_ref(), dialog.cancelled());
|
||||
let result = dialog.cancelled();
|
||||
self.store_result(&mut *dialog, result);
|
||||
self.cancel_orphans(Some(ctx));
|
||||
true
|
||||
}
|
||||
@@ -793,7 +809,33 @@ impl DialogHost {
|
||||
self.open.iter().position(|dialog| dialog.id() == id)
|
||||
}
|
||||
|
||||
fn store_result(&mut self, dialog: &dyn AnyStackDialog, result: Box<dyn Any + Send>) {
|
||||
/// The actions the app has not taken yet, of the open dialog `id` names and of earlier dialogs of that id that
|
||||
/// closed, oldest first, each once. A dialog's actions survive its close (finish, confirm, cancel, a replacing open
|
||||
/// or its parent closing), so the app drains them before it settles the dialog's result.
|
||||
pub fn take_actions(&mut self, id: DialogId) -> Vec<Value> {
|
||||
let mut actions = Vec::new();
|
||||
self.kept_actions.retain_mut(|(kept, list)| {
|
||||
if *kept == id {
|
||||
actions.append(list);
|
||||
}
|
||||
*kept != id
|
||||
});
|
||||
if let Some(dialog) = self.open.iter_mut().find(|dialog| dialog.id() == id) {
|
||||
actions.extend(dialog.take_actions());
|
||||
}
|
||||
actions
|
||||
}
|
||||
|
||||
/// Moves what a dialog leaving the host still holds for the app into `kept_actions`.
|
||||
fn keep_actions(&mut self, dialog: &mut dyn AnyStackDialog) {
|
||||
let actions = dialog.take_actions();
|
||||
if !actions.is_empty() {
|
||||
self.kept_actions.push((dialog.id(), actions));
|
||||
}
|
||||
}
|
||||
|
||||
fn store_result(&mut self, dialog: &mut dyn AnyStackDialog, result: Box<dyn Any + Send>) {
|
||||
self.keep_actions(dialog);
|
||||
let (type_id, instance) = (dialog.result_type(), dialog.instance());
|
||||
self.results.retain(|stored| stored.type_id != type_id || stored.instance != instance);
|
||||
self.results.push(StoredResult { type_id, id: dialog.id(), instance, key: dialog.key(), result });
|
||||
@@ -812,8 +854,9 @@ impl DialogHost {
|
||||
self.results.retain(|stored| stored.type_id != type_id || stored.instance != instance);
|
||||
if let Some(at) = self.open.iter().position(|open| open.result_type() == type_id && open.instance() == instance) {
|
||||
self.remove_tree(self.open[at].id());
|
||||
let old = self.open.remove(at);
|
||||
self.store_result(old.as_ref(), old.cancelled());
|
||||
let mut old = self.open.remove(at);
|
||||
let result = old.cancelled();
|
||||
self.store_result(&mut *old, result);
|
||||
self.replaced.push(old);
|
||||
self.cancel_orphans(None);
|
||||
}
|
||||
@@ -833,11 +876,13 @@ impl DialogHost {
|
||||
let (kept, orphans): (Vec<_>, Vec<_>) = std::mem::take(&mut self.open).into_iter().partition(|dialog| self.stack.contains(dialog.id()));
|
||||
self.open = kept;
|
||||
for mut dialog in orphans {
|
||||
self.store_result(dialog.as_ref(), dialog.cancelled());
|
||||
let result = dialog.cancelled();
|
||||
self.store_result(&mut *dialog, result);
|
||||
match ctx {
|
||||
Some(ctx) => {
|
||||
crate::runtime::overlay::end_stack_dialog(ctx, dialog.id());
|
||||
dialog.release(ctx);
|
||||
self.keep_actions(&mut *dialog);
|
||||
}
|
||||
None => self.replaced.push(dialog),
|
||||
}
|
||||
@@ -849,6 +894,7 @@ impl DialogHost {
|
||||
for mut dialog in std::mem::take(&mut self.replaced) {
|
||||
crate::runtime::overlay::end_stack_dialog(ctx, dialog.id());
|
||||
dialog.release(ctx);
|
||||
self.keep_actions(&mut *dialog);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -105,6 +105,55 @@ fn a_parent_with_an_open_child_refuses_confirm() {
|
||||
assert_eq!(host.take::<Probe<1>>(), Some(DialogResult::Ok(1)));
|
||||
}
|
||||
|
||||
/// A dialog holding requests for its app, which never finishes by itself.
|
||||
struct Emitter {
|
||||
actions: Vec<serde_json::Value>,
|
||||
}
|
||||
|
||||
impl StackDialog for Emitter {
|
||||
type Result = ();
|
||||
|
||||
fn id(&self) -> DialogId {
|
||||
dialog_id("emitter")
|
||||
}
|
||||
|
||||
fn show(&mut self, _: &mut Ui, _: &DialogStack) -> Option<DialogResult<()>> {
|
||||
None
|
||||
}
|
||||
|
||||
fn confirm(&mut self, _: &egui::Context) -> Option<()> {
|
||||
Some(())
|
||||
}
|
||||
|
||||
fn take_actions(&mut self) -> Vec<serde_json::Value> {
|
||||
std::mem::take(&mut self.actions)
|
||||
}
|
||||
}
|
||||
|
||||
/// A request a dialog raised and the app has not taken reaches the app once, whether the dialog confirmed, was
|
||||
/// cancelled or was replaced by another of its type.
|
||||
#[trial]
|
||||
fn a_dialogs_undrained_actions_survive_every_way_it_closes() {
|
||||
let mut frames = Frames::new();
|
||||
let mut host = DialogHost::new();
|
||||
let id = dialog_id("emitter");
|
||||
for way in 0..3 {
|
||||
let raised = vec![json!({ "add": way })];
|
||||
host.open(Emitter { actions: raised.clone() });
|
||||
run(&mut host, &mut frames);
|
||||
match way {
|
||||
0 => assert!(host.confirm::<Emitter>(&frames.ctx)),
|
||||
1 => assert!(host.cancel::<Emitter>(&frames.ctx)),
|
||||
_ => {
|
||||
host.open(Emitter { actions: Vec::new() });
|
||||
assert!(host.cancel::<Emitter>(&frames.ctx));
|
||||
}
|
||||
}
|
||||
assert_eq!(host.take_actions(id), raised, "way {way}");
|
||||
assert!(host.take_actions(id).is_empty(), "each action is delivered once");
|
||||
}
|
||||
}
|
||||
|
||||
/// A dialog that asks for a `Probe<3>` child in the show after `ask` is set, and finishes with `finish` once set.
|
||||
struct Asker {
|
||||
ask: bool,
|
||||
|
||||
@@ -43,26 +43,27 @@ pub struct PageSpec<'a> {
|
||||
pub keywords: &'a str,
|
||||
/// The host draws the whole page ([`PreferencesModel::custom_page`]).
|
||||
pub custom: bool,
|
||||
/// Whether the page offers Reset Section.
|
||||
pub resettable: bool,
|
||||
}
|
||||
|
||||
impl<'a> PageSpec<'a> {
|
||||
/// Whether the page offers Reset Section (default true).
|
||||
// P3(dialogs-w17): owed by dialogs-w17 (q222). Until it lands every page stays resettable, the behaviour before
|
||||
// the option existed.
|
||||
pub fn resettable(self, resettable: bool) -> Self {
|
||||
let _ = resettable;
|
||||
#[must_use]
|
||||
pub fn resettable(mut self, resettable: bool) -> Self {
|
||||
self.resettable = resettable;
|
||||
self
|
||||
}
|
||||
|
||||
/// A plain page of one untitled group.
|
||||
pub fn new(id: &'a str, title: &'a str, rows: Vec<RowSpec<'a>>) -> Self {
|
||||
Self { id, title, groups: vec![GroupSpec::new(None, rows)], footnote: None, keywords: "", custom: false }
|
||||
Self { id, title, groups: vec![GroupSpec::new(None, rows)], footnote: None, keywords: "", custom: false, resettable: true }
|
||||
}
|
||||
|
||||
/// A page the host draws ([`PreferencesModel::custom_page`]). Its [`group`](PageSpec::group)s follow the host's
|
||||
/// part, drawn and searched as a plain page's groups are.
|
||||
pub fn custom(id: &'a str, title: &'a str) -> Self {
|
||||
Self { id, title, groups: Vec::new(), footnote: None, keywords: "", custom: true }
|
||||
Self { id, title, groups: Vec::new(), footnote: None, keywords: "", custom: true, resettable: true }
|
||||
}
|
||||
|
||||
/// Adds `group` under a custom page's own part, where the search sees its rows.
|
||||
|
||||
@@ -184,6 +184,7 @@ struct ShownPage {
|
||||
id: String,
|
||||
title: Label,
|
||||
custom: bool,
|
||||
resettable: bool,
|
||||
footnote: Option<Label>,
|
||||
groups: Vec<ShownGroup>,
|
||||
}
|
||||
@@ -250,6 +251,7 @@ fn shown_page(matcher: &Matcher, listed: &Listed, page: &PageSpec<'_>) -> ShownP
|
||||
id: page.id.to_owned(),
|
||||
title: Label::from(page.title.to_owned()),
|
||||
custom: page.custom,
|
||||
resettable: page.resettable,
|
||||
footnote: page.footnote.map(|note| Label::from(note.to_owned())),
|
||||
groups,
|
||||
}
|
||||
@@ -347,7 +349,7 @@ fn show_page<H: PreferencesModel + ?Sized>(
|
||||
if let Some(note) = &page.footnote {
|
||||
Text::new(note.clone()).role(TypeRole::Caption).shade(Shade::Muted).show(ui);
|
||||
}
|
||||
if !searching && (page.custom || !page.groups.is_empty()) && reset_button(ui, words.reset_section.clone()) {
|
||||
if page.resettable && !searching && (page.custom || !page.groups.is_empty()) && reset_button(ui, words.reset_section.clone()) {
|
||||
out.reset = Some(page.id.clone());
|
||||
}
|
||||
}
|
||||
@@ -429,7 +431,7 @@ fn show_card<H: PreferencesModel + ?Sized>(
|
||||
if let Some(note) = &page.footnote {
|
||||
Text::new(note.clone()).role(TypeRole::Caption).shade(Shade::Muted).show(ui);
|
||||
}
|
||||
if !searching && (page.custom || !page.groups.is_empty()) && reset_button(ui, words.reset_section.clone()) {
|
||||
if page.resettable && !searching && (page.custom || !page.groups.is_empty()) && reset_button(ui, words.reset_section.clone()) {
|
||||
out.reset = Some(page.id.clone());
|
||||
}
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user