fwb2-fix: SettingsDraft::remove

(cherry picked from commit 91d42261e2b702d8e31ec8d379e50b96aa22a562)
This commit is contained in:
aiongg
2026-11-15 12:06:38 +00:00
parent e9b59d3197
commit d2c962e392
2 changed files with 42 additions and 1 deletions
@@ -25,6 +25,12 @@ impl SettingsDraft {
}
}
/// Removes the value at `key`, for a row whose off state stores no value (an `Option` that is `None`); Apply then
/// reports the key with no value. A missing key removes nothing.
pub fn remove(&mut self, key: &str) {
remove(&mut self.table, key);
}
/// Writes the colour row `key` (the `#rrggbb` text a [`SettingRule::color`] row holds), for the colour a picker
/// opened from [`CustomSettings::nested`] returned.
pub fn set_color(&mut self, key: &str, color: Srgba) {
@@ -1,7 +1,9 @@
//! The settings document without a window: rows, applied edits, rebase and row conditions.
use craft_theme::Rgba;
use craft_ui::patterns::preferences::{PageInfo, PrefValue, PreferencesModel, RowKind, SettingRule, SettingsDocument, StandardPage};
use craft_ui::patterns::preferences::{
CustomSettings, PageInfo, PrefValue, PreferencesModel, RowKind, SettingRule, SettingsDocument, SettingsDraft, StandardPage,
};
use craft_ui::testing::trial;
use serde::{Deserialize, Serialize};
use toml::Value;
@@ -241,3 +243,36 @@ fn a_row_with_several_conditions_shows_only_while_all_hold() {
assert!(shown(&doc, "export.other"), "an absent key passes shown_unless");
assert!(!shown(&doc, "export.never"), "an absent key fails shown_when");
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
struct Memory {
percent: Option<u32>,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
struct MemoryPrefs {
memory: Memory,
}
/// A custom row that turns its value off.
struct TurnOff;
impl CustomSettings<MemoryPrefs> for TurnOff {
fn nested(&mut self, _: &egui::Context, draft: &mut SettingsDraft) {
draft.remove("memory.percent");
}
}
/// A custom row's `SettingsDraft::remove` stores no value: Apply reports the key with none and the struct reads `None`.
#[trial]
fn a_removed_draft_value_applies_as_no_value() {
let saved = MemoryPrefs { memory: Memory { percent: Some(70) } };
let mut doc = SettingsDocument::new(saved).expect("serializes").custom(TurnOff);
doc.nested(&egui::Context::default());
doc.apply().expect("the draft fits the struct");
let edits = doc.take_applied_edits().expect("applied");
assert_eq!(edits, vec![("memory.percent".to_owned(), None)]);
assert_eq!(doc.saved().memory.percent, None);
}