mirror of
https://github.com/storytold/craft-libs.git
synced 2026-12-01 10:48:15 +01:00
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
85 lines
2.8 KiB
Rust
85 lines
2.8 KiB
Rust
//! A dialog the host opens from the Preferences window takes input while the window's page waits for it.
|
|
|
|
use craft_ui::contract::{Bind, ShowContainer as _};
|
|
use craft_ui::dialog::Dialog;
|
|
use craft_ui::patterns::preferences::{
|
|
PageSpec, PrefValue, PreferencesDraft, PreferencesModel, PreferencesState, PreferencesWindow, RowKind, RowSpec,
|
|
};
|
|
use craft_ui::sections::{DialogRole, FooterButton};
|
|
use craft_ui::testing::{Fixture, trial};
|
|
use egui::Ui;
|
|
use std::cell::RefCell;
|
|
use std::rc::Rc;
|
|
|
|
#[derive(Default)]
|
|
struct Model {
|
|
snap: bool,
|
|
saved: bool,
|
|
editing: bool,
|
|
answered: usize,
|
|
}
|
|
|
|
impl PreferencesModel for Model {
|
|
fn pages(&self) -> Vec<PageSpec<'_>> {
|
|
vec![PageSpec::new("general", "General", vec![RowSpec::new("snap", "Snap", RowKind::Bool(self.snap))])]
|
|
}
|
|
fn set(&mut self, _: &str, value: PrefValue) {
|
|
if let PrefValue::Bool(value) = value {
|
|
self.snap = value;
|
|
}
|
|
}
|
|
fn custom_row(&mut self, _: &mut Ui, _: &str) -> bool {
|
|
false
|
|
}
|
|
fn custom_page(&mut self, _: &mut Ui, _: &str) -> bool {
|
|
false
|
|
}
|
|
fn reset_page(&mut self, _: &str) {}
|
|
fn nested(&mut self, ui: &mut Ui) {
|
|
if !self.editing {
|
|
return;
|
|
}
|
|
let shown = Dialog::new("Edit List").buttons([FooterButton::new("Done", DialogRole::Accept)]).show(ui, |_, _| {});
|
|
if shown.output.closed.is_some() {
|
|
self.editing = false;
|
|
self.answered += 1;
|
|
}
|
|
}
|
|
fn nested_open(&self) -> bool {
|
|
self.editing
|
|
}
|
|
}
|
|
|
|
impl PreferencesDraft for Model {
|
|
fn differs(&self) -> bool {
|
|
self.snap != self.saved
|
|
}
|
|
fn apply(&mut self) -> Result<(), String> {
|
|
self.saved = self.snap;
|
|
Ok(())
|
|
}
|
|
fn cancel(&mut self) {
|
|
self.snap = self.saved;
|
|
}
|
|
}
|
|
|
|
/// The nested dialog's own buttons click while it is open: the page the window disables meanwhile must not reach the
|
|
/// dialog drawn after it. Once it closes, the page takes edits again.
|
|
#[trial]
|
|
fn a_nested_dialog_takes_clicks_while_the_page_waits() {
|
|
let model = Rc::new(RefCell::new(Model { editing: true, ..Model::default() }));
|
|
let shown = model.clone();
|
|
let mut state = PreferencesState::default();
|
|
let mut fixture = Fixture::new(move |ui| {
|
|
let _ = PreferencesWindow::new(Bind::bound(&mut state)).embedded().show(ui, &mut *shown.borrow_mut());
|
|
});
|
|
fixture.settle();
|
|
let done = fixture.by_label("Done").expect("the nested dialog's button");
|
|
fixture.click(&done).settle();
|
|
assert_eq!(model.borrow().answered, 1, "the nested dialog's button answers it");
|
|
|
|
let snap = fixture.by_label("Snap").expect("the setting");
|
|
fixture.click(&snap).settle();
|
|
assert!(model.borrow().snap, "the page takes edits again once the nested dialog closed");
|
|
}
|