From 5bbc780217230d99ed5cee674b6dbb4f4ce6fa87 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sat, 21 Nov 2026 21:25:51 +0700 Subject: [PATCH] forms-w1: net change of 1 commits - forms-w1: written from its sheet --- crates/ui-contract/src/runtime/memory.rs | 11 ++-- crates/ui-panels/src/dialog/held.rs | 26 ++++++++-- crates/ui-panels/src/dialog/host.rs | 52 +++++++++++++------ .../src/patterns/form_dialogs/fields.rs | 39 +++++++++----- .../patterns/form_dialogs/host_requests.rs | 12 ++++- 5 files changed, 101 insertions(+), 39 deletions(-) diff --git a/crates/ui-contract/src/runtime/memory.rs b/crates/ui-contract/src/runtime/memory.rs index 5255376f8..835628c7c 100644 --- a/crates/ui-contract/src/runtime/memory.rs +++ b/crates/ui-contract/src/runtime/memory.rs @@ -301,14 +301,17 @@ pub enum Lifetime { /// The run-only value under `key`, in the session tier. It never reaches the host. It gives the default until a value /// is set, and it is readable outside a pass, as [`saved_value`] is. pub fn run_value(ctx: &Context, key: &SaveKey) -> T { - let _ = (ctx, key); - todo!("od.OD2") + MemoryStage::peek_session(ctx, global("craft_ui.run", key)) } /// Sets the run-only value under `key`; see [`run_value`]. pub fn set_run_value(ctx: &Context, key: &SaveKey, value: T) { - let _ = (ctx, key, value); - todo!("od.OD2") + let id = global("craft_ui.run", key); + let mut stage = MemoryStage::new(ctx, id, id, false); + // No component shows under the key's id, so the stage stamps no `Seen` for it. + stage.touched.get_mut().clear(); + *stage.session::(id) = value; + stage.write_back(); } /// The saved value under `key`, outside a dialog: an app reads one SAVED value through the same store the dialogs' diff --git a/crates/ui-panels/src/dialog/held.rs b/crates/ui-panels/src/dialog/held.rs index bd06124d3..ccf75b854 100644 --- a/crates/ui-panels/src/dialog/held.rs +++ b/crates/ui-panels/src/dialog/held.rs @@ -12,7 +12,8 @@ pub enum HeldOk { Waiting, /// The app accepted the held OK: the dialog finishes with it at its next pass or [`DialogHost::confirm`](super::DialogHost::confirm). Accepted, - /// The app refused the last held OK for this reason; OK is enabled again. + /// The app refused the last held OK for this reason; OK is enabled again, unless the refusal blocks + /// ([`HeldAnswer::refuse`]). Refused(Label), } @@ -27,7 +28,8 @@ pub struct HeldAnswer { /// `take` has not handed out `filed` yet. untaken: bool, accepted: bool, - refused: Option<(Label, T)>, + /// The reason, the refused answer and whether the refusal blocks OK until it is withdrawn. + refused: Option<(Label, T, bool)>, } impl Default for HeldAnswer { @@ -71,7 +73,7 @@ impl HeldAnswer { } Err(reason) => { self.untaken = false; - self.refused = Some((reason, filed)); + self.refused = Some((reason, filed, false)); } } true @@ -89,7 +91,7 @@ impl HeldAnswer { /// Why the app refused the last held answer, and that answer; `None` once OK filed again or the refusal ended. pub fn refusal(&self) -> Option<(&Label, &T)> { - self.refused.as_ref().map(|(reason, answer)| (reason, answer)) + self.refused.as_ref().map(|(reason, answer, _)| (reason, answer)) } /// Ends the refusal shown so far (the values it refused changed). @@ -97,12 +99,26 @@ impl HeldAnswer { self.refused = None; } + /// The host refuses `answer` for `reason` without a filed answer: OK stays off while + /// [`refusal_blocks`](HeldAnswer::refusal_blocks), until [`clear_refusal`](HeldAnswer::clear_refusal). + pub fn refuse(&mut self, reason: Label, answer: T) { + self.filed = None; + self.untaken = false; + self.accepted = false; + self.refused = Some((reason, answer, true)); + } + + /// Whether the refusal keeps OK off: a host refusal does, an `answer(Err)` does not. + pub fn refusal_blocks(&self) -> bool { + self.refused.as_ref().is_some_and(|(_, _, blocks)| *blocks) + } + /// Where the held OK stands, for [`StackDialog::held_state`](super::StackDialog::held_state). pub fn state(&self) -> Option { match (&self.filed, &self.refused) { (Some(_), _) if self.accepted => Some(HeldOk::Accepted), (Some(_), _) => Some(HeldOk::Waiting), - (None, Some((reason, _))) => Some(HeldOk::Refused(reason.clone())), + (None, Some((reason, _, _))) => Some(HeldOk::Refused(reason.clone())), (None, None) => None, } } diff --git a/crates/ui-panels/src/dialog/host.rs b/crates/ui-panels/src/dialog/host.rs index 35d02b2b2..e7d2d1b3c 100644 --- a/crates/ui-panels/src/dialog/host.rs +++ b/crates/ui-panels/src/dialog/host.rs @@ -138,8 +138,18 @@ pub trait StackDialog: Any + Send { /// default snapshots [`json_fields`](StackDialog::json_fields), sets each field in turn and, on the first /// refusal, restores the snapshot and returns the refusal. fn set_json_fields(&mut self, ctx: &Context, fields: &[(String, Value)]) -> Result<(), String> { - let _ = (ctx, fields); - todo!("sh.K23") + let snapshot = self.json_fields(); + for (field, value) in fields { + if let Err(refusal) = self.set_json_field(ctx, field, value.clone()) { + if let Some(Value::Object(before)) = &snapshot { + for (name, old) in before { + let _ = self.set_json_field(ctx, name, old.clone()); + } + } + return Err(refusal); + } + } + Ok(()) } /// Whether the dialog has a Reset now. `false` by default. @@ -229,15 +239,10 @@ trait AnyStackDialog: Send { /// [`StackDialog::cancelled`]. fn release(&mut self, ctx: &Context); fn preview_color(&mut self, key: &str, color: Option); - #[allow(dead_code)] // P3(forms-w1): co.K13 fn take_color_request(&mut self) -> Option; - #[allow(dead_code)] // P3(forms-w1): co.K13 fn answer_color(&mut self, key: &str, color: craft_theme::Srgba); - #[allow(dead_code)] // P3(forms-w1): sh.K23 fn set_json_fields(&mut self, ctx: &Context, fields: &[(String, Value)]) -> Result<(), String>; - #[allow(dead_code)] // P3(forms-w1): wg.W18 fn can_reset(&self) -> bool; - #[allow(dead_code)] // P3(forms-w1): wg.W18 fn reset(&mut self, ctx: &Context) -> bool; fn as_any(&self) -> &dyn Any; fn as_any_mut(&mut self) -> &mut dyn Any; @@ -634,31 +639,46 @@ impl DialogHost { /// The top-most open dialog's pending [`ColorRequest`] with its id; the app opens a colour picker over it and /// answers through [`answer_color`](DialogHost::answer_color). pub fn take_color_request(&mut self) -> Option<(DialogId, ColorRequest)> { - todo!("co.K14") + self.open.iter_mut().rev().find_map(|dialog| { + let id = dialog.id(); + dialog.take_color_request().map(|request| (id, request)) + }) } /// Writes the picked `color` into colour row `key` of the open dialog `id`. pub fn answer_color(&mut self, id: DialogId, key: &str, color: craft_theme::Srgba) { - let _ = (id, key, color); - todo!("co.K14") + if let Some(dialog) = self.open.iter_mut().find(|dialog| dialog.id() == id) { + dialog.answer_color(key, color); + } } /// Sets every field of `fields` on the open dialog `id` or none ([`StackDialog::set_json_fields`]). pub fn set_json_fields(&mut self, ctx: &Context, id: DialogId, fields: &[(String, Value)]) -> Result<(), String> { - let _ = (ctx, id, fields); - todo!("sh.K24") + match self.open.iter_mut().find(|dialog| dialog.id() == id) { + Some(dialog) => { + let answer = dialog.set_json_fields(ctx, fields); + if answer.is_ok() { + ctx.request_repaint(); + } + answer + } + None => Err("no such dialog".to_owned()), + } } /// Runs the reset of the open dialog `id`; `false` for an unknown id or a dialog without a Reset. pub fn reset_by_id(&mut self, ctx: &Context, id: DialogId) -> bool { - let _ = (ctx, id); - todo!("wg.W19") + let Some(dialog) = self.open.iter_mut().find(|dialog| dialog.id() == id) else { return false }; + let done = dialog.reset(ctx); + if done { + ctx.request_repaint(); + } + done } /// Whether the open dialog `id` has a Reset now; `false` for an unknown id. pub fn can_reset_by_id(&self, id: DialogId) -> bool { - let _ = id; - todo!("wg.W19") + self.open.iter().find(|dialog| dialog.id() == id).is_some_and(|dialog| dialog.can_reset()) } /// The open dialog `id` ([`dialog_id`](super::dialog_id) of its key) as `Any`, for an app with diff --git a/crates/ui-patterns/src/patterns/form_dialogs/fields.rs b/crates/ui-patterns/src/patterns/form_dialogs/fields.rs index 707faf139..1609c2085 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/fields.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/fields.rs @@ -86,11 +86,12 @@ pub struct DialogFields { opening: BTreeMap, /// The keys marked mixed ([`DialogFields::set_mixed`]). #[serde(skip)] - #[allow(dead_code)] // P3(forms-w1): ty.X64 mixed: BTreeSet, + /// The marks the opening snapshot kept, which Reset restores. + #[serde(skip)] + opening_mixed: BTreeSet, /// The keys of the tabs a reset button reset ([`DialogFields::mark_reset`]). #[serde(skip)] - #[allow(dead_code)] // P3(forms-w1): ty.FD1 reset: BTreeSet, } @@ -113,26 +114,29 @@ impl DialogFields { values: pairs.into_iter().map(|(k, v)| (k.into(), v.into())).collect(), opening: BTreeMap::new(), mixed: BTreeSet::new(), + opening_mixed: BTreeSet::new(), reset: BTreeSet::new(), } } - /// Sets `key` to `value`, replacing any earlier value. + /// Sets `key` to `value`, replacing any earlier value. An edit answers a mixed mark, so it clears it. pub fn set(&mut self, key: impl Into, value: impl Into) { - self.values.insert(key.into(), value.into()); + let key = key.into(); + self.mixed.remove(&key); + self.values.insert(key, value.into()); } /// Marks `key` as differing over what the dialog or panel edits: its value is removed and its row shows blank. The /// marks are not serialized, so a mixed key is absent from `json_fields` and every answer. pub fn set_mixed(&mut self, key: impl Into) { - let _ = (self, key); - todo!("ty.X64") + let key = key.into(); + self.values.remove(&key); + self.mixed.insert(key); } /// Whether `key` is marked mixed. pub fn is_mixed(&self, key: &str) -> bool { - let _ = (self, key); - todo!("ty.X65") + self.mixed.contains(key) } /// The value under `key`, if any. @@ -145,8 +149,9 @@ impl DialogFields { self.values.get_mut(key) } - /// Removes `key` and returns its value. + /// Removes `key` and returns its value; the key is no longer marked mixed. pub fn remove(&mut self, key: &str) -> Option { + self.mixed.remove(key); self.values.remove(key) } @@ -255,20 +260,20 @@ impl DialogFields { /// Marks the tab `key` as reset by a reset button. pub fn mark_reset(&mut self, key: impl Into) { - let _ = (self, key); - todo!("ty.FD1") + self.reset.insert(key.into()); } /// Whether a reset button reset the tab `key`; `snapshot` and `reset` clear the marks. pub fn was_reset(&self, key: &str) -> bool { - let _ = (self, key); - todo!("ty.FD1") + self.reset.contains(key) } /// Records the current values as the opening ones. Dialogs call it on open; a host that /// changes a value from outside the dialog (a script) does not move the snapshot. pub fn snapshot(&mut self) { self.opening = self.values.clone(); + self.opening_mixed = self.mixed.clone(); + self.reset.clear(); } /// Restores the snapshot. Keys added since the snapshot are kept; keys removed since come @@ -278,9 +283,17 @@ impl DialogFields { for (key, value) in &self.opening { if self.values.get(key) != Some(value) { self.values.insert(key.clone(), value.clone()); + self.mixed.remove(key); changed = true; } } + for key in &self.opening_mixed { + if self.mixed.insert(key.clone()) { + self.values.remove(key); + changed = true; + } + } + self.reset.clear(); changed } diff --git a/crates/ui-patterns/src/patterns/form_dialogs/host_requests.rs b/crates/ui-patterns/src/patterns/form_dialogs/host_requests.rs index 85f38f7b3..e6450dca1 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/host_requests.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/host_requests.rs @@ -38,6 +38,7 @@ pub struct HostRequests { listing: Option, preview_click: Option, footer_action: Option, + help_link: Option, /// The previewed colour of each colour row a child picker edits, by row key; one entry per key. previews: Vec<(String, Srgba)>, answers: Vec, @@ -63,6 +64,14 @@ impl HostRequests { if let Some(request) = rows.listing.take() { self.request_listing(request); } + if let Some(link) = rows.help_link.take() { + self.help_link = Some(link); + } + } + + /// The help link a row's button reported, once. + pub fn take_help_link(&mut self) -> Option { + self.help_link.take() } /// Files a colour request; it replaces one not taken yet. @@ -129,7 +138,8 @@ impl HostRequests { /// Moves the requests and answers of `other` in; its requests replace the ones not taken yet. pub fn extend(&mut self, other: HostRequests) { - let HostRequests { color, path, eyedropper, listing, preview_click, footer_action, previews, answers } = other; + let HostRequests { color, path, eyedropper, listing, preview_click, footer_action, help_link, previews, answers } = other; + self.help_link = help_link.or(self.help_link.take()); self.color = color.or(self.color.take()); self.path = path.or(self.path.take()); self.eyedropper = eyedropper.or(self.eyedropper.take());