Merge pull request #3843 from dotCooCoo/fix/slice-dialog-held-ok

Keep shared Slice dialogs open when an app refuses OK
This commit is contained in:
BFlat authored and GitHub committed 2026-11-27 14:23:04 +07:00
commit 3bb31d85c6
2 files changed
+159 -8

No files matched your search

+78 -5
View File
@@ -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<SliceKind>,
requests: HostRequests,
held: Option<HeldAnswer<SliceOptions>>,
}
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<SliceOptions> {
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<SliceOptions> {
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<HeldOk> {
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<HeldAnswer<Axes>>,
}
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<DivideSlices> {
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<DialogResult<DivideSlices>> {
let words = Env::of(ui).words::<SliceWords>();
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<DivideSlices> {
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<HeldOk> {
self.held.as_ref().and_then(HeldAnswer::state)
}
}
+81 -3
View File
@@ -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::<SliceOptionsDialog>().unwrap().take_ok().unwrap();
assert_eq!(draft.name, "Draft name");
assert!(host.borrow_mut().get_mut::<SliceOptionsDialog>().unwrap().take_ok().is_none());
assert!(host.borrow_mut().take::<SliceOptionsDialog>().is_none());
host.borrow_mut().get_mut::<SliceOptionsDialog>().unwrap().answer_ok(Err(STALE_SELECTION.into()));
fixture.idle(4);
assert_eq!(host.borrow().get::<SliceOptionsDialog>().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::<SliceOptionsDialog>().unwrap();
assert_eq!(dialog.take_ok(), Some(draft.clone()));
dialog.answer_ok(Ok(()));
drop(open);
fixture.idle(4);
assert_eq!(host.borrow_mut().take::<SliceOptionsDialog>().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::<DivideSlicesDialog>(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::<DivideSlicesDialog>().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::<DivideSlicesDialog>(fixture.ctx()), "programmatic OK files before closing");
let mut open = host.borrow_mut();
let dialog = open.get_mut::<DivideSlicesDialog>().unwrap();
assert_eq!(dialog.take_ok(), Some(draft));
dialog.answer_ok(Ok(()));
assert!(open.confirm::<DivideSlicesDialog>(fixture.ctx()));
assert_eq!(open.take::<DivideSlicesDialog>().and_then(DialogResult::ok), Some(draft));
open.open(DivideSlicesDialog::new(draft).held_ok());
assert!(!open.confirm::<DivideSlicesDialog>(fixture.ctx()));
drop(open);
fixture.idle(4);
fixture.key(Key::Escape, Modifiers::NONE);
fixture.idle(4);
assert!(matches!(host.borrow_mut().take::<DivideSlicesDialog>(), Some(DialogResult::Cancel)));
host.borrow_mut().open(DivideSlicesDialog::new(draft));
assert!(host.borrow_mut().confirm::<DivideSlicesDialog>(fixture.ctx()), "ordinary callers close immediately");
assert_eq!(host.borrow_mut().take::<DivideSlicesDialog>().and_then(DialogResult::ok), Some(draft));
}