diff --git a/crates/draw/src/ui/slice/dialogs.rs b/crates/draw/src/ui/slice/dialogs.rs index f681946b93..a527821200 100644 --- a/crates/draw/src/ui/slice/dialogs.rs +++ b/crates/draw/src/ui/slice/dialogs.rs @@ -7,7 +7,7 @@ use craft_color_ui::ui::{ChosenColor, ColorChoice, NamedColor}; use craft_theme::Srgba; use craft_ui::choice::{Checkbox, Radio, RadioGroup}; use craft_ui::contract::{Bind, Entry, FieldLabelRole, Props as _, Show as _, ShowContainer as _, SideLabel}; -use craft_ui::dialog::{DialogId, DialogResult, DialogStack, StackDialog, dialog_id}; +use craft_ui::dialog::{DialogId, DialogResult, DialogStack, HeldAnswer, HeldOk, HoldsOk, StackDialog, dialog_id}; use craft_ui::display::{Shade, Text}; use craft_ui::form::{Field, FieldGroup}; use craft_ui::layout_components::Row; @@ -194,6 +194,7 @@ pub struct SliceOptionsDialog { options: SliceOptions, kinds: Vec, requests: HostRequests, + held: Option>, } impl SliceOptionsDialog { @@ -204,6 +205,7 @@ impl SliceOptionsDialog { options, kinds: vec![SliceKind::Image, SliceKind::NoImage], requests: HostRequests::new(), + held: None, } } @@ -220,6 +222,25 @@ impl SliceOptionsDialog { } } +impl HoldsOk for SliceOptionsDialog { + type Answer = SliceOptions; + + fn held_ok(mut self) -> Self { + self.held = Some(HeldAnswer::new()); + self + } + + fn take_ok(&mut self) -> Option { + self.held.as_mut()?.take() + } + + fn answer_ok(&mut self, answer: Result<(), Label>) { + if let Some(held) = &mut self.held { + held.answer(answer); + } + } +} + impl StackDialog for SliceOptionsDialog { type Result = SliceOptions; @@ -333,7 +354,11 @@ impl StackDialog for SliceOptionsDialog { let background_labels = [&words.background_none, &words.background_matte, &words.background_white, &words.background_black]; let measuring = craft_ui::runtime::measure::active(ui.ctx()); let answers = if measuring { Vec::new() } else { self.requests.take_answers() }; - let shown = FormDialog::new(words.options_title.clone(), Bind::bound(&mut self.options)).answers(answers).show(ui, |ui, form, options| { + let mut dialog = FormDialog::new(words.options_title.clone(), Bind::bound(&mut self.options)).answers(answers); + if let Some(held) = &mut self.held { + dialog = dialog.held(held); + } + let shown = dialog.show(ui, |ui, form, options| { for answer in form.take_answers() { if let HostAnswer::Color { key, color, .. } = answer && key == ColorChoice::CUSTOM_KEY @@ -406,7 +431,18 @@ impl StackDialog for SliceOptionsDialog { } fn confirm(&mut self, _: &egui::Context) -> Option { - Some(self.options.clone()) + let Some(held) = &mut self.held else { return Some(self.options.clone()) }; + if let Some(answer) = held.finish() { + return Some(answer); + } + if !held.is_waiting() { + held.file(self.options.clone()); + } + None + } + + fn held_state(&self) -> Option { + self.held.as_ref().and_then(HeldAnswer::state) } fn cancelled(&mut self, _: &egui::Context) { @@ -518,6 +554,7 @@ pub struct DivideSlicesDialog { id: DialogId, axes: Axes, lengths: LengthUnits, + held: Option>, } impl DivideSlicesDialog { @@ -527,6 +564,7 @@ impl DivideSlicesDialog { id: dialog_id("slice.divide"), axes: Axes::of(divide), lengths: LengthUnits::new(UnitKind::Pixels, UnitKind::Pixels, 72.0), + held: None, } } @@ -584,6 +622,25 @@ fn axis_rows(ui: &mut Ui, axis: &mut AxisState, caption: &Label, words: &SliceWo }); } +impl HoldsOk for DivideSlicesDialog { + type Answer = DivideSlices; + + fn held_ok(mut self) -> Self { + self.held = Some(HeldAnswer::new()); + self + } + + fn take_ok(&mut self) -> Option { + self.held.as_mut()?.take()?.answer() + } + + fn answer_ok(&mut self, answer: Result<(), Label>) { + if let Some(held) = &mut self.held { + held.answer(answer); + } + } +} + impl StackDialog for DivideSlicesDialog { type Result = DivideSlices; @@ -633,7 +690,11 @@ impl StackDialog for DivideSlicesDialog { fn show(&mut self, ui: &mut Ui, _: &DialogStack) -> Option> { let words = Env::of(ui).words::(); let lengths = self.lengths; - let shown = FormDialog::new(words.divide_title.clone(), Bind::bound(&mut self.axes)).show(ui, |ui, _, axes| { + let mut dialog = FormDialog::new(words.divide_title.clone(), Bind::bound(&mut self.axes)); + if let Some(held) = &mut self.held { + dialog = dialog.held(held); + } + let shown = dialog.show(ui, |ui, _, axes| { FieldGroup::new(words.divide_title.clone()).show(ui, |ui, _| { axis_rows(ui, &mut axes.rows, &words.horizontally, &words, lengths); axis_rows(ui, &mut axes.columns, &words.vertically, &words, lengths); @@ -648,6 +709,18 @@ impl StackDialog for DivideSlicesDialog { } fn confirm(&mut self, _: &egui::Context) -> Option { - self.axes.answer() + let answer = self.axes.answer(); + let Some(held) = &mut self.held else { return answer }; + if let Some(axes) = held.finish() { + return axes.answer(); + } + if !held.is_waiting() && answer.is_some() { + held.file(self.axes); + } + None + } + + fn held_state(&self) -> Option { + self.held.as_ref().and_then(HeldAnswer::state) } } diff --git a/crates/draw/tests/it/slice.rs b/crates/draw/tests/it/slice.rs index 92241a9cea..196f0ab498 100644 --- a/crates/draw/tests/it/slice.rs +++ b/crates/draw/tests/it/slice.rs @@ -5,11 +5,11 @@ use craft_behavior::geometry::Constraint; use craft_behavior::kurbo::{Point, Rect}; use craft_behavior::{CanvasView, EditEvent}; use craft_draw::ui::slice::{ - DivideBy, DivideSlices, DivideSlicesDialog, SliceCommand, SliceEvent, SliceItem, SliceMode, SliceOrigin, SliceOverlay, SliceRefusal, SliceTool, - SliceToolSettings, + DivideBy, DivideSlices, DivideSlicesDialog, SliceCommand, SliceEvent, SliceItem, SliceMode, SliceOptions, SliceOptionsDialog, SliceOrigin, + SliceOverlay, SliceRefusal, SliceTool, SliceToolSettings, }; use craft_ui::contract::{CommandSet, Role, Show as _}; -use craft_ui::dialog::DialogHost; +use craft_ui::dialog::{DialogHost, DialogResult, HeldOk, HoldsOk}; use craft_ui::runtime::commands::CommandRef; use craft_ui::runtime::frame::take_output; use craft_ui::testing::{Fixture, ToolFixture, access, trial}; @@ -241,3 +241,81 @@ fn divide_slice_names_each_axis_control_by_its_axis() { } } } + +const STALE_SELECTION: &str = "Select the opening slices and try again."; + +#[trial] +fn slice_options_keeps_its_draft_after_the_app_refuses_ok() { + let host = Rc::new(RefCell::new(DialogHost::new())); + let shown = Rc::clone(&host); + let mut fixture = Fixture::new(move |ui| shown.borrow_mut().run(ui)); + fixture.room(Vec2::new(900.0, 700.0)); + let id = host.borrow_mut().open(SliceOptionsDialog::new(SliceOptions::default()).held_ok()); + fixture.idle(10); + host.borrow_mut().set_json_field(fixture.ctx(), id, "name", serde_json::json!("Draft name")).unwrap(); + fixture.idle(4); + + fixture.click(&fixture.by_label("OK").unwrap()).idle(4); + assert_eq!(host.borrow().held_state(id), Some(HeldOk::Waiting)); + let draft = host.borrow_mut().get_mut::().unwrap().take_ok().unwrap(); + assert_eq!(draft.name, "Draft name"); + assert!(host.borrow_mut().get_mut::().unwrap().take_ok().is_none()); + assert!(host.borrow_mut().take::().is_none()); + host.borrow_mut().get_mut::().unwrap().answer_ok(Err(STALE_SELECTION.into())); + fixture.idle(4); + assert_eq!(host.borrow().get::().unwrap().options(), &draft); + assert!(fixture.by_label(STALE_SELECTION).is_some(), "the refusal shows in the dialog"); + + fixture.click(&fixture.by_label("OK").unwrap()).idle(4); + let mut open = host.borrow_mut(); + let dialog = open.get_mut::().unwrap(); + assert_eq!(dialog.take_ok(), Some(draft.clone())); + dialog.answer_ok(Ok(())); + drop(open); + fixture.idle(4); + assert_eq!(host.borrow_mut().take::().and_then(DialogResult::ok), Some(draft)); +} + +#[trial] +fn divide_slices_can_retry_a_held_answer_and_cancel_it_while_waiting() { + let host = Rc::new(RefCell::new(DialogHost::new())); + let shown = Rc::clone(&host); + let mut fixture = Fixture::new(move |ui| shown.borrow_mut().run(ui)); + fixture.room(Vec2::new(900.0, 700.0)); + let id = host.borrow_mut().open(DivideSlicesDialog::new(DivideSlices::new(None, None)).held_ok()); + fixture.idle(10); + assert!(!host.borrow_mut().confirm::(fixture.ctx())); + assert!(host.borrow().held_state(id).is_none(), "two disabled axes produce no answer"); + host.borrow_mut().set_json_field(fixture.ctx(), id, "rows", serde_json::json!(3)).unwrap(); + fixture.idle(4); + + let ok = fixture.by_label("OK").unwrap(); + assert!(!fixture.tree().is_disabled_key(ok.node), "setting one axis enables OK"); + fixture.click(&ok).idle(4); + let draft = DivideSlices::new(Some(DivideBy::Count(3)), None); + let mut open = host.borrow_mut(); + let dialog = open.get_mut::().unwrap(); + assert_eq!(dialog.take_ok(), Some(draft)); + dialog.answer_ok(Err(STALE_SELECTION.into())); + drop(open); + fixture.idle(4); + assert!(fixture.by_label(STALE_SELECTION).is_some()); + assert!(!host.borrow_mut().confirm::(fixture.ctx()), "programmatic OK files before closing"); + let mut open = host.borrow_mut(); + let dialog = open.get_mut::().unwrap(); + assert_eq!(dialog.take_ok(), Some(draft)); + dialog.answer_ok(Ok(())); + assert!(open.confirm::(fixture.ctx())); + assert_eq!(open.take::().and_then(DialogResult::ok), Some(draft)); + open.open(DivideSlicesDialog::new(draft).held_ok()); + assert!(!open.confirm::(fixture.ctx())); + drop(open); + + fixture.idle(4); + fixture.key(Key::Escape, Modifiers::NONE); + fixture.idle(4); + assert!(matches!(host.borrow_mut().take::(), Some(DialogResult::Cancel))); + host.borrow_mut().open(DivideSlicesDialog::new(draft)); + assert!(host.borrow_mut().confirm::(fixture.ctx()), "ordinary callers close immediately"); + assert_eq!(host.borrow_mut().take::().and_then(DialogResult::ok), Some(draft)); +}