Apply preferences as they change

This commit is contained in:
Dylan M. Taylor
2026-11-20 13:24:31 -05:00
parent c91b117fb1
commit c5210ed0a1
2 changed files with 19 additions and 18 deletions
+6 -6
View File
@@ -1,5 +1,4 @@
//! Preferences: the draft the window edits, the model that maps its rows to the app, and the live
//! effects an OK applies.
//! Preferences: the draft the window edits and the model that applies each change to the app.
use std::sync::{Arc, Mutex};
@@ -12,7 +11,9 @@ use craft::theme_store::ui::AppearancePage;
use craft::ui::contract::{Bind, Entry, Props as _, SaveKey, Show as _};
use craft::ui::dialog::DialogHost;
use craft::ui::notices::{Notice, NoticeLayout};
use craft::ui::patterns::preferences::{GroupSpec, PageSpec, PrefValue, PreferencesModel, PreferencesState, PreferencesWindow, RowKind, RowSpec};
use craft::ui::patterns::preferences::{
GroupSpec, PageSpec, PrefValue, PreferencesMode, PreferencesModel, PreferencesState, PreferencesWindow, RowKind, RowSpec,
};
use craft::ui::runtime::env::Label;
use craft::ui::text::TextField;
use craft::window::policy::TitleBarPref;
@@ -54,8 +55,7 @@ impl PreferencesSession {
}
}
/// Every setting the window edits, copied from the app when the window opens. Nothing reaches the
/// app before OK or Apply.
/// Every setting the window edits, copied from the app when the window opens and applied as it changes.
#[derive(Clone, PartialEq)]
struct PrefsDraft {
language: String,
@@ -189,7 +189,7 @@ impl PdfCraftApp {
let ctx = ui.ctx().clone();
let output = {
let mut prefs = Prefs::new(self, draft, selection, host, answers, &ctx);
PreferencesWindow::new(Bind::bound(state)).remember(SaveKey::new("pdfcraft.preferences")).show(ui, &mut prefs)
PreferencesWindow::new(Bind::bound(state)).mode(PreferencesMode::Live).remember(SaveKey::new("pdfcraft.preferences")).show(ui, &mut prefs)
};
if let Some(request) = output.pick_color {
let answers = Arc::clone(answers);
+13 -12
View File
@@ -1,4 +1,4 @@
//! Preferences rows apply on OK and change nothing on Cancel.
//! Preferences rows apply as they change and persist when the window closes.
use egui_kittest::Harness;
use egui_kittest::kittest::Queryable;
@@ -11,7 +11,7 @@ const PDF: &[u8] = include_bytes!("../data/form.pdf");
type H = Harness<'static, PdfCraftApp>;
/// One row edit and the app state it must produce after OK; the same state must be absent after Cancel.
/// One row edit and the app state it must produce immediately.
struct Case {
name: &'static str,
page: &'static str,
@@ -45,17 +45,18 @@ fn preferences_shown(h: &H) -> bool {
}
fn click(h: &mut H, label: &str) {
h.get_by_label(label).click();
h.get_by(|node| !node.is_hidden() && node.role() != egui::accesskit::Role::Label && node.label().as_deref() == Some(label)).click();
h.run_steps(3);
}
/// Replaces the text of the field named `label` through the keyboard, as a user would.
fn type_into(h: &mut H, label: &str, text: &str) {
h.get_by_label(label).click();
let role = if label == "Recent files to keep" { egui::accesskit::Role::SpinButton } else { egui::accesskit::Role::TextInput };
h.get_by_role_and_label(role, label).click();
h.run_steps(2);
h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A);
h.run_steps(1);
h.get_by_label(label).type_text(text);
h.get_by_role_and_label(role, label).type_text(text);
h.run_steps(3);
}
@@ -145,26 +146,26 @@ fn edited(case: &Case) -> H {
h.state_mut().open_preferences();
h.run_steps(4);
// The page list's row, not the heading of the page it shows.
h.get_by_role_and_label(egui::accesskit::Role::ListItem, case.page).click();
h.get_by(|node| !node.is_hidden() && node.role() == egui::accesskit::Role::ListItem && node.label().as_deref() == Some(case.page)).click();
h.run_steps(3);
(case.edit)(&mut h);
h
}
#[test]
fn preference_rows_apply_on_ok_and_nothing_changes_on_cancel() {
fn preference_rows_apply_live_and_survive_closing() {
for case in cases() {
let mut h = edited(&case);
assert!(!(case.applied)(h.state()), "{}: a row edits the draft, not the app", case.name);
assert!((case.applied)(h.state()), "{}: a row applies immediately", case.name);
click(&mut h, "OK");
h.run_steps(3);
assert!(!preferences_shown(&h), "{}: OK closes the window", case.name);
assert!((case.applied)(h.state()), "{}: OK applies the draft", case.name);
assert!((case.applied)(h.state()), "{}: OK keeps the change", case.name);
let mut h = edited(&case);
click(&mut h, "Cancel");
h.key_press(egui::Key::Escape);
h.run_steps(3);
assert!(!preferences_shown(&h), "{}: Cancel closes the window", case.name);
assert!(!(case.applied)(h.state()), "{}: Cancel leaves the app as it was", case.name);
assert!(!preferences_shown(&h), "{}: Escape closes the window", case.name);
assert!((case.applied)(h.state()), "{}: Escape keeps the change", case.name);
}
}