mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 21:52:32 +00:00
Merge branch 'area/fwb2-f3' into area/fwb2
This commit is contained in:
@@ -34,7 +34,9 @@ fn dialog_methods_read_set_and_answer_typed_dialogs() {
|
||||
);
|
||||
assert_eq!(inspect(&host)[0]["fields"]["radius"], 4.5);
|
||||
assert_eq!(call(&mut host, &ctx, "ui.dialog.confirm", json!({"dialog": blur})), Ok(Value::Null));
|
||||
let Some(DialogResult::Ok(result)) = host.take::<HostedParamDialog>() else { panic!("the blur dialog answers OK") };
|
||||
let Some(DialogResult::Ok(result)) = host.take_keyed::<HostedParamDialog>().map(|(result, _)| result) else {
|
||||
panic!("the blur dialog answers OK")
|
||||
};
|
||||
assert_eq!((result.fields.number("radius"), result.fields.text("method")), (Some(4.5), Some("spin")));
|
||||
assert_eq!(inspect(&host), json!([]));
|
||||
|
||||
@@ -90,7 +92,7 @@ fn param_dialog_automation_follows_its_rows() {
|
||||
host.open(blur(false));
|
||||
let id = inspect(&host)[0]["id"].clone();
|
||||
assert!(call(&mut host, &ctx, "ui.dialog.confirm", json!({"dialog": id})).is_err());
|
||||
assert!(host.take::<HostedParamDialog>().is_none());
|
||||
assert!(host.take_keyed::<HostedParamDialog>().map(|(result, _)| result).is_none());
|
||||
assert!(host.cancel::<HostedParamDialog>(&ctx));
|
||||
|
||||
host.open(blur(true));
|
||||
@@ -104,7 +106,9 @@ fn param_dialog_automation_follows_its_rows() {
|
||||
assert_eq!(set(&mut host, "radius", json!(7)), Ok(Value::Null));
|
||||
assert_eq!(inspect(&host)[0]["fields"]["cells"], json!([1, 2, 3]));
|
||||
assert_eq!(call(&mut host, &ctx, "ui.dialog.confirm", json!({"dialog": id})), Ok(Value::Null));
|
||||
let Some(DialogResult::Ok(result)) = host.take::<HostedParamDialog>() else { panic!("the blur dialog answers OK") };
|
||||
let Some(DialogResult::Ok(result)) = host.take_keyed::<HostedParamDialog>().map(|(result, _)| result) else {
|
||||
panic!("the blur dialog answers OK")
|
||||
};
|
||||
assert_eq!(result.fields.grid("cells"), Some(&[1, 2, 3][..]));
|
||||
result.remember(&ctx);
|
||||
|
||||
@@ -114,7 +118,9 @@ fn param_dialog_automation_follows_its_rows() {
|
||||
let fields = &listed[0]["fields"];
|
||||
assert_eq!((fields["radius"].as_f64(), &fields["method"], &fields["cells"]), (Some(7.0), &json!("zoom"), &json!([0, 1, 0])), "{fields}");
|
||||
assert_eq!(call(&mut host, &ctx, "ui.dialog.confirm", json!({"dialog": id})), Ok(Value::Null));
|
||||
let Some(DialogResult::Ok(reopened)) = host.take::<HostedParamDialog>() else { panic!("the reopened dialog answers OK") };
|
||||
let Some(DialogResult::Ok(reopened)) = host.take_keyed::<HostedParamDialog>().map(|(result, _)| result) else {
|
||||
panic!("the reopened dialog answers OK")
|
||||
};
|
||||
assert_eq!((reopened.fields.number("radius"), reopened.fields.text("method")), (Some(7.0), Some("zoom")));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
//! [`HostedAdjustDialog`]: an Image › Adjustments dialog on the dialog host, around any adjustment editor.
|
||||
|
||||
use craft_behavior::{GestureId, Value};
|
||||
use craft_ui::contract::{Bind, Sampler as _};
|
||||
use craft_ui::dialog::{DialogId, DialogResult, DialogStack, FieldRefusal, StackDialog, dialog_id, serde_fields, set_serde_field};
|
||||
use craft_theme::variant::Tone;
|
||||
use craft_ui::contract::{Bind, Sampler as _, Show as _};
|
||||
use craft_ui::dialog::{
|
||||
DialogId, DialogResult, DialogStack, FieldRefusal, HeldAnswer, HeldOk, StackDialog, dialog_id, serde_fields, set_serde_field,
|
||||
};
|
||||
use craft_ui::notices::{Notice, NoticeLayout};
|
||||
use craft_ui::patterns::form_dialogs::FormDialog;
|
||||
use craft_ui::runtime::env::Label;
|
||||
use craft_ui::runtime::memory::SaveKey;
|
||||
use craft_ui::runtime::overlay::CloseReason;
|
||||
use egui::{Context, Ui};
|
||||
use serde::Serialize;
|
||||
use serde::de::DeserializeOwned;
|
||||
@@ -99,6 +104,10 @@ pub struct HostedAdjustDialog<V, E = ()> {
|
||||
select_point: Option<usize>,
|
||||
editor: Box<Editor<V, E>>,
|
||||
asks: Vec<E>,
|
||||
/// Whether OK files its value for the app instead of closing ([`held_ok`](HostedAdjustDialog::held_ok)).
|
||||
hold: bool,
|
||||
/// The OK the app holds back until it answers `answer_ok`, and its refusal, shown until the value changes.
|
||||
held: HeldAnswer<V>,
|
||||
#[cfg(feature = "screen-testing")]
|
||||
facts: craft_ui::testing::StoryOutput,
|
||||
}
|
||||
@@ -133,11 +142,33 @@ impl<V: Value + Default + Serialize + DeserializeOwned, E: Send + 'static> Hoste
|
||||
select_point: None,
|
||||
editor: Box::new(editor),
|
||||
asks: Vec::new(),
|
||||
hold: false,
|
||||
held: HeldAnswer::new(),
|
||||
#[cfg(feature = "screen-testing")]
|
||||
facts: craft_ui::testing::StoryOutput::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Makes OK file its value for the app instead of closing: the app runs the adjustment and answers with
|
||||
/// [`answer_ok`](HostedAdjustDialog::answer_ok); a refusal keeps the dialog open and shows why. OK stays disabled
|
||||
/// until the answer.
|
||||
#[must_use]
|
||||
pub fn held_ok(mut self) -> Self {
|
||||
self.hold = true;
|
||||
self
|
||||
}
|
||||
|
||||
/// The value a held OK filed, once.
|
||||
pub fn take_ok(&mut self) -> Option<V> {
|
||||
self.held.take()
|
||||
}
|
||||
|
||||
/// The app's answer to the held OK: `Ok` closes the dialog with the held value in the next pass; `Err` enables OK
|
||||
/// again and shows the reason under the editor until the value changes. Without a held OK it does nothing.
|
||||
pub fn answer_ok(&mut self, answer: Result<(), Label>) {
|
||||
self.held.answer(answer);
|
||||
}
|
||||
|
||||
/// The editor's context: Dynamic Color Sliders, a grey document. Always a dialog context.
|
||||
#[must_use]
|
||||
pub fn context(mut self, dynamic_color_sliders: bool, gray: bool) -> Self {
|
||||
@@ -261,20 +292,28 @@ impl<V: Value + Default + Serialize + DeserializeOwned, E: Send + 'static> Stack
|
||||
clipping,
|
||||
select_point,
|
||||
reset_with,
|
||||
hold,
|
||||
held,
|
||||
..
|
||||
} = self;
|
||||
// The app accepted the held OK: this pass closes the dialog with it. A measuring run closes nothing.
|
||||
let finish = held.is_accepted() && !craft_ui::runtime::measure::active(ui.ctx());
|
||||
let refusal = held.refusal().map(|(reason, refused)| (reason.clone(), refused.clone()));
|
||||
// The request to select a point is this pass's alone: a rerun of the pass in the frame finds none.
|
||||
let (canvas, select_point) = (*canvas, select_point.take());
|
||||
let (context, resizable, remember) = (*context, *resizable, remember.clone());
|
||||
let form = FormDialog::new(title.clone(), Bind::bound(value)).opened(Bind::bound(open)).preview(Some(Bind::bound(&mut *preview))).shaped(
|
||||
move |dialog| {
|
||||
let form = FormDialog::new(title.clone(), Bind::bound(value))
|
||||
.opened(Bind::bound(open))
|
||||
.preview(Some(Bind::bound(&mut *preview)))
|
||||
.accept_stays_open(*hold)
|
||||
.ok_enabled(!held.is_waiting())
|
||||
.shaped(move |dialog| {
|
||||
let dialog = dialog.resizable(resizable);
|
||||
match remember {
|
||||
Some(key) => dialog.remember(key),
|
||||
None => dialog,
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
let form = match reset_with.as_deref() {
|
||||
Some(target) => form.reset_with(target),
|
||||
None => form,
|
||||
@@ -286,14 +325,30 @@ impl<V: Value + Default + Serialize + DeserializeOwned, E: Send + 'static> Stack
|
||||
let mut cx =
|
||||
AdjustDialogCx { context, canvas, picker: &mut *picker, channel: &mut *channel, select_point, clipping: &mut *clipping, edit: None };
|
||||
asks.extend(editor(ui, value, &mut cx));
|
||||
if let Some((reason, _)) = refusal.as_ref().filter(|(_, refused)| refused == value) {
|
||||
Notice::new(Tone::Bad, reason.clone()).layout(NoticeLayout::Inline).show(ui);
|
||||
}
|
||||
if finish {
|
||||
form.request_close_as(CloseReason::Activated);
|
||||
}
|
||||
true
|
||||
});
|
||||
*comparing = shown.comparing;
|
||||
if held.refusal().is_some_and(|(_, refused)| refused != value) {
|
||||
held.clear_refusal();
|
||||
}
|
||||
// The pass reports a press only in the pass that took the input, so a second pass of the frame files nothing.
|
||||
if *hold && shown.picked == Some(0) && !held.is_waiting() {
|
||||
held.file(value.clone());
|
||||
}
|
||||
if finish && shown.closed == Some(CloseReason::Activated) {
|
||||
return Some(held.finish().map_or(DialogResult::Cancel, DialogResult::Ok));
|
||||
}
|
||||
#[cfg(feature = "screen-testing")]
|
||||
{
|
||||
self.facts = craft_ui::contract::Outcome::story_facts(&shown.response);
|
||||
}
|
||||
let accepted = shown.accepted.map(DialogResult::Ok);
|
||||
let accepted = shown.accepted.filter(|_| !*hold).map(DialogResult::Ok);
|
||||
accepted.or(shown.closed.map(|_| DialogResult::Cancel))
|
||||
}
|
||||
|
||||
@@ -301,10 +356,26 @@ impl<V: Value + Default + Serialize + DeserializeOwned, E: Send + 'static> Stack
|
||||
Some(&self.key)
|
||||
}
|
||||
|
||||
/// OK as the button: a held dialog files the value and answers none, and answers the held value once the app
|
||||
/// accepted it.
|
||||
fn confirm(&mut self, _: &Context) -> Option<V> {
|
||||
if self.held.is_accepted() {
|
||||
return self.held.finish();
|
||||
}
|
||||
if self.held.is_waiting() {
|
||||
return None;
|
||||
}
|
||||
if self.hold {
|
||||
self.held.file(self.value.clone());
|
||||
return None;
|
||||
}
|
||||
Some(self.value.clone())
|
||||
}
|
||||
|
||||
fn held_state(&self) -> Option<HeldOk> {
|
||||
self.held.state()
|
||||
}
|
||||
|
||||
fn json_fields(&self) -> Option<serde_json::Value> {
|
||||
let mut fields = serde_fields(&self.value)?;
|
||||
let object = fields.as_object_mut()?;
|
||||
|
||||
@@ -7,7 +7,8 @@ use std::rc::Rc;
|
||||
use craft_imaging::layer_style::{EffectId, EffectInstance, EffectKind, LayerStyle, LayerStyleDraft, LayerStylePage, StyleParams};
|
||||
use craft_imaging::ui::adjust::{BrightnessContrast, BrightnessContrastEditor, HostedAdjustDialog};
|
||||
use craft_imaging::ui::layer_style::{HostedLayerStyle, LayerStyleAsk};
|
||||
use craft_ui::dialog::{DialogHost, DialogResult, StackDialog, dialog_id};
|
||||
use craft_ui::dialog::{DialogHost, DialogResult, HeldOk, StackDialog, dialog_id};
|
||||
use craft_ui::runtime::env::Label;
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use egui::{Key, Modifiers, Vec2};
|
||||
|
||||
@@ -98,3 +99,46 @@ fn a_hosted_adjustment_dialog_previews_and_answers_with_its_value() {
|
||||
fixture.idle(10).key(Key::Escape, Modifiers::NONE).idle(4);
|
||||
assert_eq!(host.borrow_mut().take::<Adjust>(), Some(DialogResult::Cancel), "Escape cancels a reopened dialog");
|
||||
}
|
||||
|
||||
/// Why the app refuses the held OK.
|
||||
const REFUSED: &str = "The layer is locked.";
|
||||
|
||||
// OK of a held adjustment dialog files its value and waits with OK disabled; a refusal keeps the dialog open with OK
|
||||
// enabled and the reason shown until the value changes; an accepted OK closes it with the value.
|
||||
#[trial]
|
||||
fn a_held_adjustment_ok_refused_stays_open_and_an_accepted_one_closes() {
|
||||
let host = Rc::new(RefCell::new(DialogHost::new()));
|
||||
let mut fixture = fixture(&host);
|
||||
let id = host.borrow_mut().open(brightness_contrast().held_ok());
|
||||
fixture.idle(10);
|
||||
let on_dialog = |act: &mut dyn FnMut(&mut Adjust)| host.borrow_mut().get_mut::<Adjust>().map(act);
|
||||
|
||||
let ok = fixture.by_label("OK").expect("the OK button");
|
||||
fixture.click(&ok).idle(4);
|
||||
let mut filed = None;
|
||||
on_dialog(&mut |dialog| filed = dialog.take_ok());
|
||||
assert_eq!(filed.map(|value| value.brightness), Some(20.0), "OK files the value");
|
||||
assert_eq!(host.borrow().held_state(id), Some(HeldOk::Waiting), "the filed OK waits for the app");
|
||||
let ok = fixture.by_label("OK").expect("the OK button");
|
||||
assert!(fixture.tree().is_disabled_key(ok.node), "OK waits disabled for the app's answer");
|
||||
|
||||
on_dialog(&mut |dialog| dialog.answer_ok(Err(Label::from(REFUSED))));
|
||||
fixture.idle(4);
|
||||
assert!(host.borrow().is_open::<Adjust>(), "a refused OK keeps the dialog open");
|
||||
assert_eq!(host.borrow().held_state(id), Some(HeldOk::Refused(Label::from(REFUSED))));
|
||||
assert!(fixture.by_label(REFUSED).is_some(), "the refusal shows in the dialog");
|
||||
let ok = fixture.by_label("OK").expect("the OK button");
|
||||
assert!(!fixture.tree().is_disabled_key(ok.node), "a refusal enables OK again");
|
||||
on_dialog(&mut |dialog| dialog.value_mut().brightness = 5.0);
|
||||
fixture.idle(4);
|
||||
assert!(fixture.by_label(REFUSED).is_none(), "a changed value clears the refusal");
|
||||
|
||||
let ctx = fixture.ctx().clone();
|
||||
assert!(!host.borrow_mut().confirm_by_id(&ctx, id), "confirm on a held dialog files the value and answers nothing yet");
|
||||
on_dialog(&mut |dialog| filed = dialog.take_ok());
|
||||
assert_eq!(filed.map(|value| value.brightness), Some(5.0), "confirm files the value as the button does");
|
||||
on_dialog(&mut |dialog| dialog.answer_ok(Ok(())));
|
||||
fixture.idle(4);
|
||||
let answered = host.borrow_mut().take_keyed::<Adjust>().and_then(|(result, _)| result.ok()).map(|value| value.brightness);
|
||||
assert_eq!(answered, Some(5.0), "the accepted OK closes the dialog with the held value");
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ use std::sync::Arc;
|
||||
use egui::{Context, Response, Ui};
|
||||
|
||||
use crate::contract::Bind;
|
||||
use crate::dialog::{DialogId, DialogResult, DialogStack, FieldRefusal, HeldOk, StackDialog, dialog_id};
|
||||
use crate::dialog::{DialogId, DialogResult, DialogStack, FieldRefusal, HeldAnswer, HeldOk, StackDialog, dialog_id};
|
||||
use crate::runtime::env::Label;
|
||||
|
||||
/// A full-window workspace (Liquify, Filter Gallery, Select and Mask) that a [`HostedWorkspace`] shows on the app's
|
||||
@@ -159,16 +159,9 @@ pub struct HostedWorkspace<W: FullWindowWorkspace> {
|
||||
open: bool,
|
||||
revision: u64,
|
||||
hold_ok: bool,
|
||||
/// From the filed OK until the app's answer: the workspace disables its OK.
|
||||
ok_waits: bool,
|
||||
/// The held OK's answer, kept until the app answers it.
|
||||
filed: Option<W::Result>,
|
||||
/// `take_ok` has not handed out `filed` yet.
|
||||
untaken: bool,
|
||||
/// The app accepted the held OK: the next pass finishes with `filed`.
|
||||
finish: bool,
|
||||
/// Why the app refused the last held OK, until OK files again.
|
||||
refused: Option<Label>,
|
||||
/// The held OK's answer until the app answers it (the workspace disables its OK meanwhile), and why the app
|
||||
/// refused the last one, until OK files again.
|
||||
held: HeldAnswer<W::Result>,
|
||||
#[cfg(feature = "testing")]
|
||||
facts: crate::testing::StoryOutput,
|
||||
}
|
||||
@@ -184,11 +177,7 @@ impl<W: FullWindowWorkspace> HostedWorkspace<W> {
|
||||
open: true,
|
||||
revision: 0,
|
||||
hold_ok: false,
|
||||
ok_waits: false,
|
||||
filed: None,
|
||||
untaken: false,
|
||||
finish: false,
|
||||
refused: None,
|
||||
held: HeldAnswer::new(),
|
||||
#[cfg(feature = "testing")]
|
||||
facts: crate::testing::StoryOutput::default(),
|
||||
}
|
||||
@@ -222,28 +211,18 @@ impl<W: FullWindowWorkspace> HostedWorkspace<W> {
|
||||
where
|
||||
W::Result: Clone,
|
||||
{
|
||||
if !std::mem::take(&mut self.untaken) {
|
||||
return None;
|
||||
}
|
||||
self.filed.clone()
|
||||
self.held.take()
|
||||
}
|
||||
|
||||
/// The app's answer to a held OK: `Ok` closes the workspace with it in the next pass, `Err(reason)` keeps it
|
||||
/// open with OK enabled again and hands `reason` to [`FullWindowWorkspace::ok_failed`]. Does nothing while no OK
|
||||
/// is held.
|
||||
pub fn answer_ok(&mut self, answer: Result<(), Label>) {
|
||||
if self.filed.is_none() {
|
||||
return;
|
||||
}
|
||||
match answer {
|
||||
Ok(()) => self.finish = true,
|
||||
Err(reason) => {
|
||||
self.filed = None;
|
||||
self.untaken = false;
|
||||
self.ok_waits = false;
|
||||
self.refused = Some(reason.clone());
|
||||
self.workspace.ok_failed(reason);
|
||||
}
|
||||
let refused = answer.as_ref().err().cloned();
|
||||
if self.held.answer(answer)
|
||||
&& let Some(reason) = refused
|
||||
{
|
||||
self.workspace.ok_failed(reason);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -259,10 +238,7 @@ impl<W: FullWindowWorkspace> HostedWorkspace<W> {
|
||||
|
||||
/// Files a held OK's `result` for the app; OK waits for the answer.
|
||||
fn file(&mut self, result: W::Result) {
|
||||
self.filed = Some(result);
|
||||
self.untaken = true;
|
||||
self.ok_waits = true;
|
||||
self.refused = None;
|
||||
self.held.file(result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -275,9 +251,7 @@ impl<W: FullWindowWorkspace> StackDialog for HostedWorkspace<W> {
|
||||
|
||||
fn show(&mut self, ui: &mut Ui, _: &DialogStack) -> Option<DialogResult<W::Result>> {
|
||||
// The app accepted the held OK: the first pass after the answer finishes, whichever pass of a frame it is.
|
||||
if std::mem::take(&mut self.finish)
|
||||
&& let Some(result) = self.filed.take()
|
||||
{
|
||||
if let Some(result) = self.held.finish() {
|
||||
return Some(DialogResult::Ok(result));
|
||||
}
|
||||
let cx = WorkspaceCx {
|
||||
@@ -285,7 +259,7 @@ impl<W: FullWindowWorkspace> StackDialog for HostedWorkspace<W> {
|
||||
revision: self.revision,
|
||||
open: Bind::bound(&mut self.open),
|
||||
hold_ok: self.hold_ok,
|
||||
ok_waits: self.ok_waits,
|
||||
ok_waits: self.held.is_waiting(),
|
||||
};
|
||||
let pass = self.workspace.show(ui, cx);
|
||||
#[cfg(feature = "testing")]
|
||||
@@ -295,7 +269,7 @@ impl<W: FullWindowWorkspace> StackDialog for HostedWorkspace<W> {
|
||||
self.events.extend(pass.events);
|
||||
// A pick reaches only the pass whose input held it; a later pass of the frame finds OK waiting and files
|
||||
// nothing more.
|
||||
if let Some(result) = pass.held.filter(|_| self.hold_ok && !self.ok_waits) {
|
||||
if let Some(result) = pass.held.filter(|_| self.hold_ok && !self.held.is_waiting()) {
|
||||
self.file(result);
|
||||
}
|
||||
pass.result
|
||||
@@ -308,11 +282,10 @@ impl<W: FullWindowWorkspace> StackDialog for HostedWorkspace<W> {
|
||||
/// OK as the Accept button: a held workspace files the result and answers none, and answers the held result once
|
||||
/// the app accepted it.
|
||||
fn confirm(&mut self, ctx: &Context) -> Option<W::Result> {
|
||||
if self.finish {
|
||||
self.finish = false;
|
||||
return self.filed.take();
|
||||
if self.held.is_accepted() {
|
||||
return self.held.finish();
|
||||
}
|
||||
if self.ok_waits {
|
||||
if self.held.is_waiting() {
|
||||
return None;
|
||||
}
|
||||
let result = self.workspace.confirm(ctx, &*self.source)?;
|
||||
@@ -324,12 +297,7 @@ impl<W: FullWindowWorkspace> StackDialog for HostedWorkspace<W> {
|
||||
}
|
||||
|
||||
fn held_state(&self) -> Option<HeldOk> {
|
||||
match (&self.filed, &self.refused) {
|
||||
(Some(_), _) if self.finish => Some(HeldOk::Accepted),
|
||||
(Some(_), _) => Some(HeldOk::Waiting),
|
||||
(None, Some(reason)) => Some(HeldOk::Refused(reason.clone())),
|
||||
(None, None) => None,
|
||||
}
|
||||
self.held.state()
|
||||
}
|
||||
|
||||
fn json_fields(&self) -> Option<serde_json::Value> {
|
||||
|
||||
@@ -163,7 +163,7 @@ fn body(ui: &mut Ui, cx: &mut BodyCx<'_>, state: &mut MockState) {
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if let Some(result) = host.take::<HostedParamDialog>().and_then(DialogResult::<ParamResult>::ok) {
|
||||
if let Some(result) = host.take_keyed::<HostedParamDialog>().map(|(result, _)| result).and_then(DialogResult::<ParamResult>::ok) {
|
||||
result.remember(ui);
|
||||
state.document.dirty = true;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
//! The held OK every hosted dialog family shares: OK files its answer for the app, which accepts or refuses it before
|
||||
//! the dialog closes (a check the app runs, a command that may fail, a save path it picks).
|
||||
|
||||
use crate::runtime::env::Label;
|
||||
|
||||
/// Where a dialog's held OK stands ([`StackDialog::held_state`](super::StackDialog::held_state)): an OK that files its
|
||||
/// answer for the app, which answers it before the dialog closes (a check the app runs, a save path it picks).
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[non_exhaustive]
|
||||
pub enum HeldOk {
|
||||
/// OK filed its answer; the app has not answered it yet.
|
||||
Waiting,
|
||||
/// The app accepted the held OK: the dialog finishes with it at its next pass or [`DialogHost::confirm`](super::DialogHost::confirm).
|
||||
Accepted,
|
||||
/// The app refused the last held OK for this reason; OK is enabled again.
|
||||
Refused(Label),
|
||||
}
|
||||
|
||||
/// The state of a dialog's held OK, from the filed answer to the app's answer. A hosted dialog keeps one, files the
|
||||
/// answer its OK gives ([`file`](HeldAnswer::file)), keeps OK disabled while [`is_waiting`](HeldAnswer::is_waiting),
|
||||
/// hands the answer to the app once ([`take`](HeldAnswer::take)), and closes with [`finish`](HeldAnswer::finish) once
|
||||
/// the app accepted it; a refusal keeps the refused answer beside its reason, so the dialog can show the reason until
|
||||
/// its values change.
|
||||
#[derive(Debug)]
|
||||
pub struct HeldAnswer<T> {
|
||||
filed: Option<T>,
|
||||
/// `take` has not handed out `filed` yet.
|
||||
untaken: bool,
|
||||
accepted: bool,
|
||||
refused: Option<(Label, T)>,
|
||||
}
|
||||
|
||||
impl<T> Default for HeldAnswer<T> {
|
||||
fn default() -> Self {
|
||||
HeldAnswer { filed: None, untaken: false, accepted: false, refused: None }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> HeldAnswer<T> {
|
||||
/// Nothing held.
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Holds `answer` for the app; a refusal shown so far ends.
|
||||
pub fn file(&mut self, answer: T) {
|
||||
self.filed = Some(answer);
|
||||
self.untaken = true;
|
||||
self.accepted = false;
|
||||
self.refused = None;
|
||||
}
|
||||
|
||||
/// An answer is filed and not refused: OK waits disabled, and a second OK files nothing.
|
||||
pub fn is_waiting(&self) -> bool {
|
||||
self.filed.is_some()
|
||||
}
|
||||
|
||||
/// The app accepted the filed answer: the dialog closes with it.
|
||||
pub fn is_accepted(&self) -> bool {
|
||||
self.accepted && self.filed.is_some()
|
||||
}
|
||||
|
||||
/// The app's answer to the filed answer: `Ok` accepts it, `Err` refuses it for `reason`, which the dialog shows
|
||||
/// with OK enabled again. Returns whether an answer was filed; without one it does nothing.
|
||||
pub fn answer(&mut self, answer: Result<(), Label>) -> bool {
|
||||
let Some(filed) = self.filed.take() else { return false };
|
||||
match answer {
|
||||
Ok(()) => {
|
||||
self.filed = Some(filed);
|
||||
self.accepted = true;
|
||||
}
|
||||
Err(reason) => {
|
||||
self.untaken = false;
|
||||
self.refused = Some((reason, filed));
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// The accepted answer, for the pass or the confirm that closes the dialog; `None` until the app accepted one.
|
||||
pub fn finish(&mut self) -> Option<T> {
|
||||
if !self.is_accepted() {
|
||||
return None;
|
||||
}
|
||||
self.accepted = false;
|
||||
self.untaken = false;
|
||||
self.filed.take()
|
||||
}
|
||||
|
||||
/// Why the app refused the last held answer, and that answer; `None` once OK filed again or the refusal ended.
|
||||
pub fn refusal(&self) -> Option<(&Label, &T)> {
|
||||
self.refused.as_ref().map(|(reason, answer)| (reason, answer))
|
||||
}
|
||||
|
||||
/// Ends the refusal shown so far (the values it refused changed).
|
||||
pub fn clear_refusal(&mut self) {
|
||||
self.refused = None;
|
||||
}
|
||||
|
||||
/// Where the held OK stands, for [`StackDialog::held_state`](super::StackDialog::held_state).
|
||||
pub fn state(&self) -> Option<HeldOk> {
|
||||
match (&self.filed, &self.refused) {
|
||||
(Some(_), _) if self.accepted => Some(HeldOk::Accepted),
|
||||
(Some(_), _) => Some(HeldOk::Waiting),
|
||||
(None, Some((reason, _))) => Some(HeldOk::Refused(reason.clone())),
|
||||
(None, None) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Clone> HeldAnswer<T> {
|
||||
/// The filed answer for the app to judge, once; `None` when none is filed or it was handed out already.
|
||||
pub fn take(&mut self) -> Option<T> {
|
||||
if !std::mem::take(&mut self.untaken) {
|
||||
return None;
|
||||
}
|
||||
self.filed.clone()
|
||||
}
|
||||
}
|
||||
@@ -10,9 +10,9 @@ use std::fmt;
|
||||
use egui::{Context, Ui};
|
||||
use serde_json::Value;
|
||||
|
||||
use super::held::HeldOk;
|
||||
use super::stack::{DialogId, DialogStack};
|
||||
use crate::contract::{Pattern, PatternKind, PatternProbe};
|
||||
use crate::runtime::env::Label;
|
||||
|
||||
/// How a dialog on the [`DialogHost`] finished. `T` is the dialog's own answer type: a struct of
|
||||
/// values for a form, an enum for a prompt with three buttons.
|
||||
@@ -25,19 +25,6 @@ pub enum DialogResult<T> {
|
||||
Cancel,
|
||||
}
|
||||
|
||||
/// Where a dialog's held OK stands ([`StackDialog::held_state`]): an OK that files its answer for the app, which answers
|
||||
/// it before the dialog closes (a check the app runs, a save path it picks).
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[non_exhaustive]
|
||||
pub enum HeldOk {
|
||||
/// OK filed its answer; the app has not answered it yet.
|
||||
Waiting,
|
||||
/// The app accepted the held OK: the dialog finishes with it at its next pass or [`DialogHost::confirm`].
|
||||
Accepted,
|
||||
/// The app refused the last held OK for this reason; OK is enabled again.
|
||||
Refused(Label),
|
||||
}
|
||||
|
||||
impl<T> DialogResult<T> {
|
||||
/// The answer, or `None` for Cancel.
|
||||
pub fn ok(self) -> Option<T> {
|
||||
@@ -312,6 +299,10 @@ pub struct DialogHost {
|
||||
/// Dialogs another of their type replaced, waiting for the pass's `run` to run their
|
||||
/// [`StackDialog::cancelled`] (`open` has no `Context`).
|
||||
replaced: Vec<Box<dyn AnyStackDialog>>,
|
||||
/// The number the last open got ([`open_serial`](DialogHost::open_serial)).
|
||||
opens: u64,
|
||||
/// The number of each dialog's last open, by id; read only while the dialog is open.
|
||||
serials: Vec<(DialogId, u64)>,
|
||||
}
|
||||
|
||||
/// One open dialog as automation sees it ([`DialogHost::open_dialogs`]).
|
||||
@@ -421,16 +412,18 @@ impl DialogHost {
|
||||
/// of the type; if that `open` replaced a `D` that was still open, the replaced one's
|
||||
/// `Cancel` waits instead. Results are per type, so the app tells two `D` apart by what the
|
||||
/// result carries (a key, a tag). A `D` opened with an instance ([`StackDialog::instance`])
|
||||
/// keeps its result apart, for [`take_by_id`](DialogHost::take_by_id) only.
|
||||
/// keeps its result apart, for [`take_keyed`](DialogHost::take_keyed) and
|
||||
/// [`take_by_id`](DialogHost::take_by_id).
|
||||
pub fn take<D: StackDialog>(&mut self) -> Option<DialogResult<D::Result>> {
|
||||
self.take_keyed::<D>().map(|(result, _)| result)
|
||||
let at = self.results.iter().rposition(|stored| stored.type_id == TypeId::of::<D>() && stored.instance.is_none())?;
|
||||
self.take_at::<D>(at).map(|(result, _)| result)
|
||||
}
|
||||
|
||||
/// [`take`](DialogHost::take) with the key the dialog had ([`StackDialog::key`]): what a
|
||||
/// `Cancel` after a replace-by-open needs to tell which command it was. `None` key for a
|
||||
/// dialog without one.
|
||||
/// The result of the last `D` that finished or was cancelled, whatever its instance, with the
|
||||
/// key the dialog had ([`StackDialog::key`]), once: the key tells which command it was, a
|
||||
/// `Cancel` after a replace-by-open included. `None` key for a dialog without one.
|
||||
pub fn take_keyed<D: StackDialog>(&mut self) -> Option<(DialogResult<D::Result>, Option<String>)> {
|
||||
let at = self.results.iter().rposition(|stored| stored.type_id == TypeId::of::<D>() && stored.instance.is_none())?;
|
||||
let at = self.results.iter().rposition(|stored| stored.type_id == TypeId::of::<D>())?;
|
||||
self.take_at::<D>(at)
|
||||
}
|
||||
|
||||
@@ -595,6 +588,14 @@ impl DialogHost {
|
||||
self.results.push(StoredResult { type_id, id: dialog.id(), instance, key: dialog.key(), result });
|
||||
}
|
||||
|
||||
/// Which open of the dialog `id` this is: a number distinct for every open on this host, the same while the
|
||||
/// dialog stays open. A dialog's id is the same at every open of its key; this tells one open from the next (a
|
||||
/// preview cached for a cancelled open is not the reopened dialog's). `None` when `id` is not open.
|
||||
pub fn open_serial(&self, id: DialogId) -> Option<u64> {
|
||||
self.position_by_id(id)?;
|
||||
self.serials.iter().find(|(open, _)| *open == id).map(|(_, serial)| *serial)
|
||||
}
|
||||
|
||||
fn open_with_parent(&mut self, dialog: Box<dyn AnyStackDialog>, parent: Option<DialogId>) -> DialogId {
|
||||
let (type_id, instance) = (dialog.result_type(), dialog.instance());
|
||||
self.results.retain(|stored| stored.type_id != type_id || stored.instance != instance);
|
||||
@@ -606,6 +607,10 @@ impl DialogHost {
|
||||
self.cancel_orphans(None);
|
||||
}
|
||||
let id = dialog.id();
|
||||
self.opens += 1;
|
||||
let open = &self.open;
|
||||
self.serials.retain(|(serial_of, _)| *serial_of != id && open.iter().any(|dialog| dialog.id() == *serial_of));
|
||||
self.serials.push((id, self.opens));
|
||||
self.stack.push(id, parent);
|
||||
self.open.push(dialog);
|
||||
id
|
||||
|
||||
@@ -6,6 +6,7 @@ mod alert;
|
||||
mod blocking;
|
||||
#[doc(hidden)]
|
||||
pub mod frame;
|
||||
mod held;
|
||||
mod host;
|
||||
mod modal;
|
||||
mod prompt;
|
||||
@@ -26,7 +27,8 @@ pub use blocking::{BlockingOverlay, BlockingOverlayStyle};
|
||||
pub use frame::{DialogFrame, DialogFrameStyle, DialogOutput, FooterCx, FooterHooks};
|
||||
#[doc(hidden)]
|
||||
pub use host::dialog_probe;
|
||||
pub use host::{DialogHost, DialogResult, FieldRefusal, HeldOk, OpenDialog, StackDialog, StartupStage};
|
||||
pub use held::{HeldAnswer, HeldOk};
|
||||
pub use host::{DialogHost, DialogResult, FieldRefusal, OpenDialog, StackDialog, StartupStage};
|
||||
pub use modal::{Dialog, DialogStyle};
|
||||
pub use prompt::{Prompt, PromptOutput};
|
||||
pub use prompts::{PromptAnswer, PromptChoice, PromptKind, StandardPrompt, StandardPromptOutput};
|
||||
|
||||
@@ -269,3 +269,20 @@ fn cmd_d_answers_dont_save_on_macos() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Each open of a dialog has its own serial, kept while it stays open, though its id is the same at every open: a
|
||||
/// reopened dialog is told apart from the one cancelled before it.
|
||||
#[trial]
|
||||
fn each_open_of_a_dialog_has_its_own_serial() {
|
||||
let mut frames = Frames::new();
|
||||
let mut host = DialogHost::new();
|
||||
let id = host.open(Probe::<1>::new("picker"));
|
||||
let first = host.open_serial(id).expect("an open dialog has a serial");
|
||||
run(&mut host, &mut frames);
|
||||
assert_eq!(host.open_serial(id), Some(first), "the serial stays while the dialog is open");
|
||||
assert!(host.cancel_by_id(&frames.ctx, id));
|
||||
assert_eq!(host.open_serial(id), None, "a closed dialog has none");
|
||||
let again = host.open(Probe::<1>::new("picker"));
|
||||
assert_eq!(again, id, "the reopened dialog has the same id");
|
||||
assert_ne!(host.open_serial(id), Some(first), "the reopened dialog has a serial of its own");
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ use super::rows::{ParamRows, ParamRowsOutput};
|
||||
|
||||
use crate::contract::{Bind, BodyCx, NoValue, Outcome, Remember as _, Sampler as _, SaveKey, Show as _, ShowContainer as _};
|
||||
|
||||
use crate::dialog::{Dialog, DialogId, DialogResult, DialogStack, FieldRefusal, FooterHooks, HeldOk, StackDialog, dialog_id};
|
||||
use crate::dialog::{Dialog, DialogId, DialogResult, DialogStack, FieldRefusal, FooterHooks, HeldAnswer, HeldOk, StackDialog, dialog_id};
|
||||
|
||||
use crate::display::{Shade, Text};
|
||||
use crate::layout_components::{Row, Stack};
|
||||
@@ -764,10 +764,8 @@ pub struct HostedParamDialog {
|
||||
shown: ShownState,
|
||||
/// Whether OK files its answer for the host instead of closing ([`held_ok`](HostedParamDialog::held_ok)).
|
||||
hold: bool,
|
||||
/// An OK the host holds back until it answers `answer_ok`.
|
||||
held: Option<Held>,
|
||||
/// The host's refusal of the last held OK, shown until a value changes.
|
||||
refused: Option<Refused>,
|
||||
/// The OK the host holds back until it answers `answer_ok`, and its refusal, shown until a value changes.
|
||||
held: HeldAnswer<ParamResult>,
|
||||
/// The picture point the next pass centres the preview on ([`centre_preview`](HostedParamDialog::centre_preview)).
|
||||
centre: Option<Pos2>,
|
||||
}
|
||||
@@ -781,21 +779,6 @@ struct ShownState {
|
||||
centre: Option<Pos2>,
|
||||
}
|
||||
|
||||
/// An OK a [`HostedParamDialog`] holds back until the host answers it.
|
||||
struct Held {
|
||||
result: ParamResult,
|
||||
/// `take_ok` has handed the result to the host.
|
||||
taken: bool,
|
||||
/// The host accepted it: the next pass closes the dialog with it.
|
||||
accepted: bool,
|
||||
}
|
||||
|
||||
/// Why the host refused a held OK, and the values it refused.
|
||||
struct Refused {
|
||||
reason: Label,
|
||||
fields: DialogFields,
|
||||
}
|
||||
|
||||
impl HostedParamDialog {
|
||||
/// A hosted dialog for the command `key`, opening with `fields`; `build` makes the dialog over the values. The
|
||||
/// values open now, as a `ParamDialog`'s first pass opens its own: the remembered values `ctx` keeps over
|
||||
@@ -811,10 +794,11 @@ impl HostedParamDialog {
|
||||
let (params, patterns) = (params.into_owned(), patterns.into_owned());
|
||||
let saved = remember.and_then(|key| MemoryStage::peek_saved::<Option<DialogFields>>(ctx, &key));
|
||||
ParamDialog::open_values(¶ms, &patterns, saved.as_ref(), &mut fields);
|
||||
let id = dialog_id(key.as_str());
|
||||
HostedParamDialog {
|
||||
id: dialog_id(key.as_str()),
|
||||
id,
|
||||
key,
|
||||
instance: None,
|
||||
instance: Some(id),
|
||||
opening: fields.clone(),
|
||||
fields,
|
||||
build: Box::new(build),
|
||||
@@ -823,8 +807,7 @@ impl HostedParamDialog {
|
||||
preview: None,
|
||||
shown: ShownState::default(),
|
||||
hold: false,
|
||||
held: None,
|
||||
refused: None,
|
||||
held: HeldAnswer::new(),
|
||||
centre: None,
|
||||
}
|
||||
}
|
||||
@@ -841,6 +824,9 @@ impl HostedParamDialog {
|
||||
}
|
||||
|
||||
/// Names this dialog among others made with the same key (one per document, say), so they keep separate state.
|
||||
/// Without it the dialog is the one of its key: dialogs of different keys stay open together, and the same key
|
||||
/// opened again replaces it. Its result is taken with [`DialogHost::take_keyed`](crate::dialog::DialogHost::take_keyed)
|
||||
/// or by id.
|
||||
#[must_use]
|
||||
pub fn instance(mut self, instance: impl std::hash::Hash + std::fmt::Debug) -> Self {
|
||||
self.id = dialog_id((self.key.as_str(), instance));
|
||||
@@ -913,33 +899,19 @@ impl HostedParamDialog {
|
||||
|
||||
/// The OK filed by a held dialog, once.
|
||||
pub fn take_ok(&mut self) -> Option<ParamResult> {
|
||||
let held = self.held.as_mut().filter(|held| !held.taken)?;
|
||||
held.taken = true;
|
||||
Some(held.result.clone())
|
||||
self.held.take()
|
||||
}
|
||||
|
||||
/// The host's answer to the held OK: `Ok` closes the dialog with the held answer in the next pass; `Err` enables
|
||||
/// OK again and shows the label under the rows until a value changes. Without a held OK it does nothing.
|
||||
pub fn answer_ok(&mut self, answer: Result<(), Label>) {
|
||||
match answer {
|
||||
Ok(()) => {
|
||||
if let Some(held) = self.held.as_mut() {
|
||||
held.accepted = true;
|
||||
}
|
||||
}
|
||||
Err(reason) => {
|
||||
if let Some(held) = self.held.take() {
|
||||
self.refused = Some(Refused { reason, fields: held.result.fields });
|
||||
}
|
||||
}
|
||||
}
|
||||
self.held.answer(answer);
|
||||
}
|
||||
|
||||
/// Holds `result` for the host: the OK of a held dialog, from the button or from `confirm`.
|
||||
fn file_held(&mut self, mut result: ParamResult) {
|
||||
result.id = self.id;
|
||||
self.held = Some(Held { result, taken: false, accepted: false });
|
||||
self.refused = None;
|
||||
self.held.file(result);
|
||||
}
|
||||
|
||||
/// The footer button id that was pressed, once (`ParamDialog::extra_buttons`).
|
||||
@@ -1077,7 +1049,7 @@ impl StackDialog for HostedParamDialog {
|
||||
fn show(&mut self, ui: &mut Ui, _: &DialogStack) -> Option<DialogResult<ParamResult>> {
|
||||
let measuring = crate::runtime::measure::active(ui.ctx());
|
||||
let answers = if measuring { Vec::new() } else { self.requests.take_answers() };
|
||||
let finish = self.held.as_ref().is_some_and(|held| held.accepted);
|
||||
let finish = self.held.is_accepted();
|
||||
// A measuring run keeps the centre: the real run of the pass takes it.
|
||||
let centre = if measuring { self.centre } else { self.centre.take() };
|
||||
let mut dialog = (self.build)(&mut self.fields)
|
||||
@@ -1086,11 +1058,11 @@ impl StackDialog for HostedParamDialog {
|
||||
.opened(Bind::bound(&mut self.open))
|
||||
.hold_ok(self.hold)
|
||||
.finish_held(finish)
|
||||
.refusal(self.refused.as_ref().map(|refused| (refused.reason.clone(), refused.fields.clone())))
|
||||
.refusal(self.held.refusal().map(|(reason, refused)| (reason.clone(), refused.fields.clone())))
|
||||
.preview_colors(self.requests.previewed_colors().to_vec())
|
||||
.centre_on(centre);
|
||||
// OK waits for the host's answer: a second OK cannot file over the held one.
|
||||
if self.held.is_some() {
|
||||
if self.held.is_waiting() {
|
||||
dialog = dialog.ok_enabled(false);
|
||||
}
|
||||
if let Some(picture) = self.preview {
|
||||
@@ -1108,8 +1080,8 @@ impl StackDialog for HostedParamDialog {
|
||||
self.requests.request_footer_action(action);
|
||||
}
|
||||
self.shown = ShownState { previewing: shown.preview_on, comparing: shown.comparing, centre: shown.preview_centre };
|
||||
if self.refused.as_ref().is_some_and(|refused| refused.fields != self.fields) {
|
||||
self.refused = None;
|
||||
if self.held.refusal().is_some_and(|(_, refused)| refused.fields != self.fields) {
|
||||
self.held.clear_refusal();
|
||||
}
|
||||
if let Some(result) = shown.held {
|
||||
self.file_held(result);
|
||||
@@ -1120,7 +1092,7 @@ impl StackDialog for HostedParamDialog {
|
||||
Some(DialogResult::Ok(result))
|
||||
}
|
||||
(None, Some(CloseReason::Activated)) if finish => {
|
||||
Some(self.held.take().map_or(DialogResult::Cancel, |held| DialogResult::Ok(held.result)))
|
||||
Some(self.held.finish().map_or(DialogResult::Cancel, DialogResult::Ok))
|
||||
}
|
||||
(None, Some(_)) => Some(DialogResult::Cancel),
|
||||
(None, None) => None,
|
||||
@@ -1136,22 +1108,17 @@ impl StackDialog for HostedParamDialog {
|
||||
}
|
||||
|
||||
fn held_state(&self) -> Option<HeldOk> {
|
||||
match (&self.held, &self.refused) {
|
||||
(Some(held), _) if held.accepted => Some(HeldOk::Accepted),
|
||||
(Some(_), _) => Some(HeldOk::Waiting),
|
||||
(None, Some(refused)) => Some(HeldOk::Refused(refused.reason.clone())),
|
||||
(None, None) => None,
|
||||
}
|
||||
self.held.state()
|
||||
}
|
||||
|
||||
/// Answers OK as the button does: none while OK is disabled. The result keeps what a remembering dialog saves. A
|
||||
/// held dialog files the answer for the host, as its button does, and answers none; once the host accepted the
|
||||
/// held answer, it answers that.
|
||||
fn confirm(&mut self, _: &Context) -> Option<ParamResult> {
|
||||
if self.held.as_ref().is_some_and(|held| held.accepted) {
|
||||
return self.held.take().map(|held| held.result);
|
||||
if self.held.is_accepted() {
|
||||
return self.held.finish();
|
||||
}
|
||||
if self.held.is_some() {
|
||||
if self.held.is_waiting() {
|
||||
return None;
|
||||
}
|
||||
let ParamDialog { key, params, remember, ok_enabled, .. } = (self.build)(&mut self.fields);
|
||||
|
||||
@@ -174,7 +174,7 @@ fn a_held_ok_refused_keeps_the_dialog_open_and_an_accepted_one_closes() {
|
||||
on_dialog(&host, |dialog| dialog.answer_ok(Ok(())));
|
||||
assert_eq!(host.borrow().held_state(dialog_id("blur")), Some(HeldOk::Accepted), "the accepted OK closes at the next pass");
|
||||
fixture.idle(4);
|
||||
let answered = host.borrow_mut().take::<HostedParamDialog>().and_then(DialogResult::ok);
|
||||
let answered = host.borrow_mut().take_keyed::<HostedParamDialog>().map(|(result, _)| result).and_then(DialogResult::ok);
|
||||
assert_eq!(answered.map(|result| result.fields), Some(filed.fields), "the accepted OK closes the dialog with the held values");
|
||||
}
|
||||
|
||||
@@ -188,7 +188,7 @@ fn a_hosted_dialog_reopened_under_the_same_key_opens() {
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut host = shown.borrow_mut();
|
||||
host.run(ui);
|
||||
if let Some(result) = host.take::<HostedParamDialog>() {
|
||||
if let Some(result) = host.take_keyed::<HostedParamDialog>().map(|(result, _)| result) {
|
||||
if again.replace(false) {
|
||||
host.open(HostedParamDialog::new(ui.ctx(), "blur", DialogFields::new(), blur));
|
||||
} else {
|
||||
@@ -323,7 +323,7 @@ fn a_remembered_value_its_row_refuses_opens_as_the_default() {
|
||||
let mut host = shown.borrow_mut();
|
||||
host.run(ui);
|
||||
// The app remembers an answer where it takes it, after its command ran.
|
||||
if let Some(DialogResult::Ok(result)) = host.take::<HostedParamDialog>() {
|
||||
if let Some(DialogResult::Ok(result)) = host.take_keyed::<HostedParamDialog>().map(|(result, _)| result) {
|
||||
result.remember(ui.ctx());
|
||||
}
|
||||
});
|
||||
@@ -460,3 +460,36 @@ fn a_field_set_before_the_first_show_resets_to_the_opening_value() {
|
||||
fixture.hold(Modifiers::NONE).settle();
|
||||
assert_eq!(on_dialog(&host, |dialog| dialog.fields().number("radius")).flatten(), Some(10.0), "Reset restores the opening value");
|
||||
}
|
||||
|
||||
/// A Sharpen dialog over the host's values.
|
||||
fn sharpen(fields: &mut DialogFields) -> ParamDialog<'_> {
|
||||
ParamDialog::new("sharpen", "Sharpen", Param::parse_list(SPEC)).values(Bind::bound(fields))
|
||||
}
|
||||
|
||||
// Hosted dialogs of two commands stay open together and each answers by its key; the same command opened again
|
||||
// replaces only its own dialog.
|
||||
#[trial]
|
||||
fn hosted_dialogs_of_two_commands_stay_open_together() {
|
||||
let (host, twice) = (Rc::new(RefCell::new(DialogHost::new())), Rc::new(Cell::new(false)));
|
||||
let mut fixture = host_fixture(&host, &twice);
|
||||
let ctx = fixture.ctx().clone();
|
||||
host.borrow_mut().open(HostedParamDialog::new(&ctx, "blur", DialogFields::new(), blur));
|
||||
host.borrow_mut().open(HostedParamDialog::new(&ctx, "sharpen", DialogFields::new(), sharpen));
|
||||
fixture.idle(4);
|
||||
assert_eq!(host.borrow().stack().ids().count(), 2, "the second command's dialog leaves the first open");
|
||||
|
||||
host.borrow_mut().open(HostedParamDialog::new(&ctx, "blur", DialogFields::new(), blur));
|
||||
fixture.idle(4);
|
||||
let replaced = host.borrow_mut().take_keyed::<HostedParamDialog>();
|
||||
assert!(matches!(replaced, Some((DialogResult::Cancel, Some(ref key))) if key == "blur"), "the same command replaces its own dialog");
|
||||
assert!(host.borrow().get_by_id::<HostedParamDialog>(dialog_id("sharpen")).is_some(), "the other command's dialog stays open");
|
||||
|
||||
for key in ["blur", "sharpen"] {
|
||||
assert!(host.borrow_mut().confirm_by_id(&ctx, dialog_id(key)), "{key}: OK answers");
|
||||
let answered = host.borrow_mut().take_keyed::<HostedParamDialog>();
|
||||
assert!(
|
||||
matches!(answered, Some((DialogResult::Ok(ref result), Some(ref named))) if named == key && result.key == key),
|
||||
"{key}: its OK comes back by its key"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,9 @@
|
||||
// The catalog is given through `Frames::commands`, which keeps it for every later frame.
|
||||
|
||||
use craft_ui::contract::{Bind, Props as _, Show as _};
|
||||
use craft_ui::runtime::commands::{AppCatalog, Binding, CommandDef, CommandInput, CommandKind, FirePhase, Scope, keymap_builds};
|
||||
use craft_ui::runtime::commands::{
|
||||
AppCatalog, Binding, CommandDef, CommandInput, CommandKind, CommandRef, CommandState, EditCommands, FirePhase, Scope, keymap_builds,
|
||||
};
|
||||
use craft_ui::runtime::env::WindowKey;
|
||||
use craft_ui::runtime::frame::take_output;
|
||||
use craft_ui::shell::AppShell;
|
||||
@@ -112,3 +114,61 @@ fn an_unchanged_catalog_builds_no_keymap() {
|
||||
frames.frame(vec![key_release(Key::P, chord)], &mut show);
|
||||
assert_eq!(fired(&frames), [hit(RENAME, FirePhase::Press)], "the new generation's command fires by its chord");
|
||||
}
|
||||
|
||||
/// A key event with no physical key, as egui_kittest and some platforms send it.
|
||||
fn logical(key: Key, pressed: bool, modifiers: Modifiers) -> egui::Event {
|
||||
egui::Event::Key { key, physical_key: None, pressed, repeat: false, modifiers }
|
||||
}
|
||||
|
||||
/// A press with no physical key runs its command by the logical key, once, whether its release comes in a later pass or
|
||||
/// in the same one (a quick tap during a slow frame), for a plain letter and a Cmd chord alike.
|
||||
#[trial]
|
||||
fn a_press_without_a_physical_key_fires_its_command_once() {
|
||||
let mut show = |ui: &mut Ui| {
|
||||
AppShell::new(WindowKey::main()).show(ui, &mut ());
|
||||
};
|
||||
let mut frames = Frames::new();
|
||||
frames.commands(catalog(1));
|
||||
frames.idle(3, &mut show);
|
||||
let chord = Modifiers::COMMAND | Modifiers::ALT;
|
||||
for (key, mods, id) in [(Key::Q, Modifiers::NONE, QUICK), (Key::U, chord, EXPORT)] {
|
||||
frames.frame(vec![logical(key, true, mods)], &mut show);
|
||||
frames.frame(vec![logical(key, false, mods)], &mut show);
|
||||
assert_eq!(fired(&frames), [hit(id, FirePhase::Press)], "{id}: press and release on separate passes");
|
||||
let one_pass = vec![
|
||||
egui::Event::ModifiersChanged(mods),
|
||||
logical(key, true, mods),
|
||||
logical(key, false, mods),
|
||||
egui::Event::ModifiersChanged(Modifiers::NONE),
|
||||
];
|
||||
frames.frame(one_pass, &mut show);
|
||||
frames.idle(2, &mut show);
|
||||
assert_eq!(fired(&frames), [hit(id, FirePhase::Press)], "{id}: press and release in one pass");
|
||||
}
|
||||
}
|
||||
|
||||
/// An app catalog's def of a registry command adds its keys beside the registry's: F2 does what Cmd+X does (Photoshop's
|
||||
/// second keys for Cut, Copy and Paste).
|
||||
#[trial]
|
||||
fn an_app_catalogs_alternate_key_for_a_registry_command_runs_it() {
|
||||
let cut = CommandRef::from(EditCommands::Cut);
|
||||
let alternate = CommandDef::new(cut.clone(), "Cut", Scope::app()).with_default(Binding::parse("F2").expect("the trial's chord parses"));
|
||||
let input = || {
|
||||
CommandInput::new()
|
||||
.catalog(AppCatalog::new(vec![alternate.clone()], 1).expect("the trial's catalog is valid"))
|
||||
.state(cut.clone(), CommandState::new())
|
||||
};
|
||||
let mut show = |ui: &mut Ui| {
|
||||
AppShell::new(WindowKey::main()).show(ui, &mut ());
|
||||
};
|
||||
let mut frames = Frames::new();
|
||||
frames.commands(input());
|
||||
frames.idle(3, &mut show);
|
||||
frames.frame(vec![key_press(Key::X, Modifiers::COMMAND)], &mut show);
|
||||
frames.frame(vec![key_release(Key::X, Modifiers::COMMAND)], &mut show);
|
||||
let by_registry_key = fired(&frames);
|
||||
assert_eq!(by_registry_key, [(cut.id.clone(), None, FirePhase::Press)], "the registry's key runs Cut");
|
||||
frames.frame(vec![key_press(Key::F2, Modifiers::NONE)], &mut show);
|
||||
frames.frame(vec![key_release(Key::F2, Modifiers::NONE)], &mut show);
|
||||
assert_eq!(fired(&frames), by_registry_key, "the catalog's alternate key does what the registry's key does");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user