mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:24:24 +00:00
Dialogs: StackDialog::story_facts; hosted forms keep their last pass's facts and hosted stories report them
This commit is contained in:
@@ -39,6 +39,8 @@ pub struct HostedAdjustDialog<V, E = ()> {
|
||||
remember: Option<SaveKey>,
|
||||
editor: Box<Editor<V, E>>,
|
||||
asks: Vec<E>,
|
||||
#[cfg(feature = "screen-testing")]
|
||||
facts: craft_ui::testing::StoryOutput,
|
||||
}
|
||||
|
||||
impl<V: Value + Default, E: Send + 'static> HostedAdjustDialog<V, E> {
|
||||
@@ -65,6 +67,8 @@ impl<V: Value + Default, E: Send + 'static> HostedAdjustDialog<V, E> {
|
||||
remember: None,
|
||||
editor: Box::new(editor),
|
||||
asks: Vec::new(),
|
||||
#[cfg(feature = "screen-testing")]
|
||||
facts: craft_ui::testing::StoryOutput::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,6 +151,10 @@ impl<V: Value + Default, E: Send + 'static> StackDialog for HostedAdjustDialog<V
|
||||
true
|
||||
});
|
||||
*comparing = shown.comparing;
|
||||
#[cfg(feature = "screen-testing")]
|
||||
{
|
||||
self.facts = craft_ui::contract::Outcome::story_facts(&shown.response);
|
||||
}
|
||||
let accepted = shown.accepted.map(DialogResult::Ok);
|
||||
accepted.or(shown.closed.map(|_| DialogResult::Cancel))
|
||||
}
|
||||
@@ -154,4 +162,9 @@ impl<V: Value + Default, E: Send + 'static> StackDialog for HostedAdjustDialog<V
|
||||
fn key(&self) -> Option<&str> {
|
||||
Some(&self.key)
|
||||
}
|
||||
|
||||
#[cfg(feature = "screen-testing")]
|
||||
fn story_facts(&self) -> craft_ui::testing::StoryOutput {
|
||||
self.facts.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,6 +85,8 @@ pub struct HostedLayerStyle {
|
||||
styles: Option<(Vec<PresetRow<String>>, Box<Thumb>)>,
|
||||
asks: Vec<LayerStyleAsk>,
|
||||
comparing: bool,
|
||||
#[cfg(feature = "screen-testing")]
|
||||
facts: craft_ui::testing::StoryOutput,
|
||||
}
|
||||
|
||||
/// The key of the hosted Layer Style dialog.
|
||||
@@ -109,6 +111,8 @@ impl HostedLayerStyle {
|
||||
styles: None,
|
||||
asks: Vec::new(),
|
||||
comparing: false,
|
||||
#[cfg(feature = "screen-testing")]
|
||||
facts: craft_ui::testing::StoryOutput::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,6 +227,8 @@ impl StackDialog for HostedLayerStyle {
|
||||
styles,
|
||||
asks,
|
||||
comparing,
|
||||
#[cfg(feature = "screen-testing")]
|
||||
facts,
|
||||
..
|
||||
} = self;
|
||||
// The draft holds Preview: the box edits a copy, written back only when the box changed it, so a Reset that
|
||||
@@ -247,6 +253,10 @@ impl StackDialog for HostedLayerStyle {
|
||||
body = body.style_presets(StylePresets::new(rows, |cx, rect, info| thumb(cx, rect, info)));
|
||||
}
|
||||
let out = body.show(ui);
|
||||
#[cfg(feature = "screen-testing")]
|
||||
{
|
||||
*facts = craft_ui::contract::Outcome::story_facts(&out);
|
||||
}
|
||||
out.apply_view(draft);
|
||||
if let Some(style) = out.edit.value() {
|
||||
draft.style = style.clone();
|
||||
@@ -266,4 +276,9 @@ impl StackDialog for HostedLayerStyle {
|
||||
fn key(&self) -> Option<&str> {
|
||||
Some(KEY)
|
||||
}
|
||||
|
||||
#[cfg(feature = "screen-testing")]
|
||||
fn story_facts(&self) -> craft_ui::testing::StoryOutput {
|
||||
self.facts.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,12 +121,19 @@ impl AlertContent {
|
||||
pub struct HostedAlert {
|
||||
content: AlertContent,
|
||||
open: bool,
|
||||
#[cfg(feature = "testing")]
|
||||
facts: crate::testing::StoryOutput,
|
||||
}
|
||||
|
||||
impl HostedAlert {
|
||||
/// `alert` on the host; its own `opened` binding is not used.
|
||||
pub fn new(alert: Alert<'_>) -> Self {
|
||||
HostedAlert { content: alert.split().0, open: true }
|
||||
HostedAlert {
|
||||
content: alert.split().0,
|
||||
open: true,
|
||||
#[cfg(feature = "testing")]
|
||||
facts: crate::testing::StoryOutput::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,10 +149,19 @@ impl StackDialog for HostedAlert {
|
||||
|
||||
fn show(&mut self, ui: &mut Ui, _: &DialogStack) -> Option<DialogResult<()>> {
|
||||
let shown = self.content.prompt(&Env::of(ui)).opened(Bind::bound(&mut self.open)).show(ui);
|
||||
#[cfg(feature = "testing")]
|
||||
{
|
||||
self.facts = crate::contract::Outcome::story_facts(&shown);
|
||||
}
|
||||
shown.closed.map(|_| DialogResult::Ok(()))
|
||||
}
|
||||
|
||||
fn key(&self) -> Option<&str> {
|
||||
Some(ALERT_KEY)
|
||||
}
|
||||
|
||||
#[cfg(feature = "testing")]
|
||||
fn story_facts(&self) -> crate::testing::StoryOutput {
|
||||
self.facts.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,6 +88,14 @@ pub trait StackDialog: Any + Send {
|
||||
Err(format!("unknown field `{field}`: this dialog has no fields"))
|
||||
}
|
||||
|
||||
/// The facts a story reports about the dialog's last pass, as [`Outcome::story_facts`](crate::contract::Outcome)
|
||||
/// reports a component's: hosted forms keep their component's. Empty by default.
|
||||
#[cfg(feature = "testing")]
|
||||
#[doc(hidden)]
|
||||
fn story_facts(&self) -> crate::testing::StoryOutput {
|
||||
crate::testing::StoryOutput::default()
|
||||
}
|
||||
|
||||
/// Called when the host closes the dialog from outside with [`DialogResult::Cancel`]
|
||||
/// ([`DialogHost::cancel`], a dialog of the same type opened over it, or its parent closing),
|
||||
/// so it can release what it holds: a pick ticket (`Files::forget`), an armed dropper. The
|
||||
|
||||
@@ -21,7 +21,8 @@ pub fn body(ui: &mut Ui, checks: &mut [bool; 2]) {
|
||||
/// A story of a hosted dialog: `open()` on the story's [`DialogHost`] (a story note), which runs every pass. The story's `open` value
|
||||
/// is the dialog's open state, so the card holds the dialog behind its button as it holds any story dialog: the
|
||||
/// dialog opens while the value is on, and the value goes off when the dialog closes. `knobs` holds the story values
|
||||
/// `open` reads; when they change, `open()` replaces the open dialog, so a knob reaches the dialog on screen.
|
||||
/// `open` reads; when they change, `open()` replaces the open dialog, so a knob reaches the dialog on screen. The
|
||||
/// story reports the open dialog's [`story_facts`](StackDialog::story_facts).
|
||||
#[doc(hidden)]
|
||||
pub fn hosted<D: StackDialog, K: PartialEq + 'static>(ui: &mut Ui, data: &mut StoryData, knobs: K, open: impl FnOnce() -> D) -> StoryOutput {
|
||||
hosted_with(ui, data, knobs, open, |_| {})
|
||||
@@ -53,11 +54,12 @@ pub fn hosted_with<D: StackDialog, K: PartialEq + 'static>(
|
||||
pass(host);
|
||||
host.run(ui);
|
||||
let is_open = host.is_open::<D>();
|
||||
let facts = host.get::<D>().map(StackDialog::story_facts).unwrap_or_default();
|
||||
if opened {
|
||||
*data.note::<Option<K>>("knobs", None) = Some(knobs);
|
||||
}
|
||||
*data.value("open", true) = is_open;
|
||||
StoryOutput::default()
|
||||
facts
|
||||
}
|
||||
|
||||
/// The opener of the Grid options dialogs.
|
||||
|
||||
@@ -67,6 +67,7 @@ fn a_story_knob_reaches_its_open_hosted_dialog() {
|
||||
let mut fixture = Fixture::story(&Story::new("titled_alert", titled_alert));
|
||||
fixture.idle(6);
|
||||
assert!(fixture.by_label("Could not open").is_some(), "the story opens its dialog");
|
||||
assert!(fixture.take_outputs().last().is_some_and(|output| output.id.is_some()), "the story reports the open dialog's facts");
|
||||
*fixture.data().value("title", "Could not open") = "Could not save";
|
||||
fixture.idle(6);
|
||||
assert!(fixture.by_label("Could not save").is_some(), "the changed knob shows in the open dialog");
|
||||
|
||||
@@ -469,12 +469,23 @@ pub struct HostedAbout {
|
||||
tab: Option<AboutTab>,
|
||||
link: Option<String>,
|
||||
open: bool,
|
||||
#[cfg(feature = "testing")]
|
||||
facts: crate::testing::StoryOutput,
|
||||
}
|
||||
|
||||
impl HostedAbout {
|
||||
/// The dialog over `info` and `credits`, opening on the About tab.
|
||||
pub fn new(info: AboutInfo, credits: Credits) -> Self {
|
||||
HostedAbout { id: dialog_id(ABOUT_KEY), info, credits, tab: None, link: None, open: true }
|
||||
HostedAbout {
|
||||
id: dialog_id(ABOUT_KEY),
|
||||
info,
|
||||
credits,
|
||||
tab: None,
|
||||
link: None,
|
||||
open: true,
|
||||
#[cfg(feature = "testing")]
|
||||
facts: crate::testing::StoryOutput::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The tab the dialog opens on.
|
||||
@@ -502,6 +513,10 @@ impl StackDialog for HostedAbout {
|
||||
|
||||
fn show(&mut self, ui: &mut Ui, _: &DialogStack) -> Option<DialogResult<()>> {
|
||||
let shown = AboutDialog { info: &self.info, credits: &self.credits, open: Some(Bind::bound(&mut self.open)), tab: self.tab }.show(ui);
|
||||
#[cfg(feature = "testing")]
|
||||
{
|
||||
self.facts = crate::contract::Outcome::story_facts(&shown);
|
||||
}
|
||||
if let Some(link) = shown.link {
|
||||
self.link = Some(link);
|
||||
}
|
||||
@@ -511,4 +526,9 @@ impl StackDialog for HostedAbout {
|
||||
fn key(&self) -> Option<&str> {
|
||||
Some(ABOUT_KEY)
|
||||
}
|
||||
|
||||
#[cfg(feature = "testing")]
|
||||
fn story_facts(&self) -> crate::testing::StoryOutput {
|
||||
self.facts.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -251,6 +251,8 @@ pub struct HostedExportAs {
|
||||
requests: HostRequests,
|
||||
saving: bool,
|
||||
open: bool,
|
||||
#[cfg(feature = "testing")]
|
||||
facts: crate::testing::StoryOutput,
|
||||
}
|
||||
|
||||
impl HostedExportAs {
|
||||
@@ -268,6 +270,8 @@ impl HostedExportAs {
|
||||
requests: HostRequests::new(),
|
||||
saving: false,
|
||||
open: true,
|
||||
#[cfg(feature = "testing")]
|
||||
facts: crate::testing::StoryOutput::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -340,6 +344,10 @@ impl StackDialog for HostedExportAs {
|
||||
..ExportAsDialog::new(self.file_name.clone(), self.source, Bind::bound(&mut self.options))
|
||||
};
|
||||
let mut shown = dialog.show(ui);
|
||||
#[cfg(feature = "testing")]
|
||||
{
|
||||
self.facts = crate::contract::Outcome::story_facts(&shown);
|
||||
}
|
||||
if let Some(wanted) = shown.wanted_preview {
|
||||
self.wanted = Some(wanted);
|
||||
}
|
||||
@@ -361,6 +369,11 @@ impl StackDialog for HostedExportAs {
|
||||
Some(EXPORT_KEY)
|
||||
}
|
||||
|
||||
#[cfg(feature = "testing")]
|
||||
fn story_facts(&self) -> crate::testing::StoryOutput {
|
||||
self.facts.clone()
|
||||
}
|
||||
|
||||
fn cancelled(&mut self, _: &egui::Context) {
|
||||
self.cancel_requests();
|
||||
}
|
||||
|
||||
@@ -373,12 +373,21 @@ pub struct HostedFormatOptions {
|
||||
kind: ExportOptionsKind,
|
||||
initial: Option<FormatOptions>,
|
||||
open: bool,
|
||||
#[cfg(feature = "testing")]
|
||||
facts: crate::testing::StoryOutput,
|
||||
}
|
||||
|
||||
impl HostedFormatOptions {
|
||||
/// The options dialog of `kind`.
|
||||
pub fn new(kind: ExportOptionsKind) -> Self {
|
||||
HostedFormatOptions { id: dialog_id(options_key(kind)), kind, initial: None, open: true }
|
||||
HostedFormatOptions {
|
||||
id: dialog_id(options_key(kind)),
|
||||
kind,
|
||||
initial: None,
|
||||
open: true,
|
||||
#[cfg(feature = "testing")]
|
||||
facts: crate::testing::StoryOutput::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The options the dialog opens with, when they are the options of its format.
|
||||
@@ -403,6 +412,10 @@ impl StackDialog for HostedFormatOptions {
|
||||
|
||||
fn show(&mut self, ui: &mut Ui, _: &DialogStack) -> Option<DialogResult<FormatOptions>> {
|
||||
let shown = FormatOptionsDialog { kind: self.kind, open: Some(Bind::bound(&mut self.open)), initial: self.initial }.show(ui);
|
||||
#[cfg(feature = "testing")]
|
||||
{
|
||||
self.facts = crate::contract::Outcome::story_facts(&shown);
|
||||
}
|
||||
match (shown.result, shown.closed) {
|
||||
(Some(options), _) => Some(DialogResult::Ok(options)),
|
||||
(None, Some(_)) => Some(DialogResult::Cancel),
|
||||
@@ -413,6 +426,11 @@ impl StackDialog for HostedFormatOptions {
|
||||
fn key(&self) -> Option<&str> {
|
||||
Some(options_key(self.kind))
|
||||
}
|
||||
|
||||
#[cfg(feature = "testing")]
|
||||
fn story_facts(&self) -> crate::testing::StoryOutput {
|
||||
self.facts.clone()
|
||||
}
|
||||
}
|
||||
|
||||
/// The parameter list of `start`'s format.
|
||||
|
||||
@@ -216,12 +216,21 @@ pub struct HostedCanvasSize {
|
||||
open: bool,
|
||||
values: CanvasSizeValues,
|
||||
build: Box<Build>,
|
||||
#[cfg(feature = "testing")]
|
||||
facts: crate::testing::StoryOutput,
|
||||
}
|
||||
|
||||
impl HostedCanvasSize {
|
||||
/// A dialog opening with `values`; `build` makes it over the values (`CanvasSize::new` with its settings).
|
||||
pub fn new(values: CanvasSizeValues, build: impl for<'b> Fn(Bind<'b, CanvasSizeValues>) -> CanvasSize<'b> + Send + 'static) -> Self {
|
||||
HostedCanvasSize { id: dialog_id(KEY), open: true, values, build: Box::new(build) }
|
||||
HostedCanvasSize {
|
||||
id: dialog_id(KEY),
|
||||
open: true,
|
||||
values,
|
||||
build: Box::new(build),
|
||||
#[cfg(feature = "testing")]
|
||||
facts: crate::testing::StoryOutput::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The values the dialog shows.
|
||||
@@ -241,10 +250,20 @@ impl StackDialog for HostedCanvasSize {
|
||||
}
|
||||
|
||||
fn show(&mut self, ui: &mut Ui, _: &DialogStack) -> Option<DialogResult<CanvasSizeResult>> {
|
||||
(self.build)(Bind::bound(&mut self.values)).opened(Bind::bound(&mut self.open)).show(ui).hosted()
|
||||
let shown = (self.build)(Bind::bound(&mut self.values)).opened(Bind::bound(&mut self.open)).show(ui);
|
||||
#[cfg(feature = "testing")]
|
||||
{
|
||||
self.facts = crate::contract::Outcome::story_facts(&shown);
|
||||
}
|
||||
shown.hosted()
|
||||
}
|
||||
|
||||
fn key(&self) -> Option<&str> {
|
||||
Some(KEY)
|
||||
}
|
||||
|
||||
#[cfg(feature = "testing")]
|
||||
fn story_facts(&self) -> crate::testing::StoryOutput {
|
||||
self.facts.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,12 +220,21 @@ pub struct HostedImageSize {
|
||||
open: bool,
|
||||
values: ImageSizeValues,
|
||||
build: Box<Build>,
|
||||
#[cfg(feature = "testing")]
|
||||
facts: crate::testing::StoryOutput,
|
||||
}
|
||||
|
||||
impl HostedImageSize {
|
||||
/// A dialog opening with `values`; `build` makes it over the values (`ImageSize::new` with its settings).
|
||||
pub fn new(values: ImageSizeValues, build: impl for<'b> Fn(Bind<'b, ImageSizeValues>) -> ImageSize<'b> + Send + 'static) -> Self {
|
||||
HostedImageSize { id: dialog_id(KEY), open: true, values, build: Box::new(build) }
|
||||
HostedImageSize {
|
||||
id: dialog_id(KEY),
|
||||
open: true,
|
||||
values,
|
||||
build: Box::new(build),
|
||||
#[cfg(feature = "testing")]
|
||||
facts: crate::testing::StoryOutput::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The values the dialog shows.
|
||||
@@ -245,10 +254,20 @@ impl StackDialog for HostedImageSize {
|
||||
}
|
||||
|
||||
fn show(&mut self, ui: &mut Ui, _: &DialogStack) -> Option<DialogResult<ImageSizeResult>> {
|
||||
(self.build)(Bind::bound(&mut self.values)).opened(Bind::bound(&mut self.open)).show(ui).hosted()
|
||||
let shown = (self.build)(Bind::bound(&mut self.values)).opened(Bind::bound(&mut self.open)).show(ui);
|
||||
#[cfg(feature = "testing")]
|
||||
{
|
||||
self.facts = crate::contract::Outcome::story_facts(&shown);
|
||||
}
|
||||
shown.hosted()
|
||||
}
|
||||
|
||||
fn key(&self) -> Option<&str> {
|
||||
Some(KEY)
|
||||
}
|
||||
|
||||
#[cfg(feature = "testing")]
|
||||
fn story_facts(&self) -> crate::testing::StoryOutput {
|
||||
self.facts.clone()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user