From d125e4e094946967e879018fc47f792231a4b792 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 19 Nov 2026 15:38:39 +0000 Subject: [PATCH] craft-ui: bind_tracked! binds a whole Tracked value; Selection, OpenSet and Level are plain data --- Cargo.lock | 2 ++ crates/behavior/Cargo.toml | 3 +++ crates/behavior/src/selection.rs | 2 +- crates/ui-contract/src/contract/level.rs | 2 +- crates/ui-contract/src/contract/values.rs | 16 +++++++++++++--- crates/ui-widgets/src/list/tree.rs | 2 +- 6 files changed, 21 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 51829b161..5dbab676d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2077,6 +2077,8 @@ dependencies = [ "craft-host", "craft-keymap", "craft-theme", + "craft-tracked", + "craft-ui-macros", "emath", "kurbo", "serde", diff --git a/crates/behavior/Cargo.toml b/crates/behavior/Cargo.toml index bb6332a39..571f3a7ad 100644 --- a/crates/behavior/Cargo.toml +++ b/crates/behavior/Cargo.toml @@ -21,6 +21,9 @@ bitflags = "2" craft-host = { workspace = true } craft-keymap = { workspace = true } craft-theme = { workspace = true } +# Value types readers hold in `Tracked` derive its `PlainData` marker. +craft-tracked = { workspace = true } +craft-ui-macros = { workspace = true } # The dock layout is a persisted shape. serde = { workspace = true } diff --git a/crates/behavior/src/selection.rs b/crates/behavior/src/selection.rs index 13acb9fb5..371cebb9d 100644 --- a/crates/behavior/src/selection.rs +++ b/crates/behavior/src/selection.rs @@ -193,7 +193,7 @@ pub trait SelectionModel: Value + Default { } /// A set of keys with a lead and an anchor. -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq, craft_ui_macros::PlainData)] #[non_exhaustive] pub struct Selection { /// The selected keys. diff --git a/crates/ui-contract/src/contract/level.rs b/crates/ui-contract/src/contract/level.rs index a92d43e8f..1706d88f7 100644 --- a/crates/ui-contract/src/contract/level.rs +++ b/crates/ui-contract/src/contract/level.rs @@ -17,7 +17,7 @@ use super::{Probe, ShapeKind}; use std::collections::BTreeMap; /// Where a type sits in the design system, in the order a viewer lists them. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, craft_ui_macros::PlainData)] #[non_exhaustive] pub enum Level { /// Colours, the edit lifecycle, stock egui. diff --git a/crates/ui-contract/src/contract/values.rs b/crates/ui-contract/src/contract/values.rs index 6446817d4..ec48191d3 100644 --- a/crates/ui-contract/src/contract/values.rs +++ b/crates/ui-contract/src/contract/values.rs @@ -743,9 +743,9 @@ pub(crate) enum Checked { /// Write-through bindings to fields of a [`Tracked`](crate::contract::Tracked) value: `bind_tracked!(doc => size)` /// gives one [`Bind`], `bind_tracked!(doc => title, style.size)` a tuple with one per field path (field names and -/// tuple indices only). Making and reading the bindings leaves the revision; an edit that changes a field moves it at -/// the write, so bindings made every pass don't invalidate the value's readers. Each binding borrows its own field, -/// so the paths must not overlap. +/// tuple indices only), and `bind_tracked!(name)` one binding to the whole value. Making and reading the bindings +/// leaves the revision; an edit that changes the bound value moves it at the write, so bindings made every pass don't +/// invalidate the value's readers. Each binding borrows its own field, so the paths must not overlap. /// /// ``` /// # extern crate craft_ui_contract as craft_ui; @@ -760,6 +760,12 @@ pub(crate) enum Checked { /// let mut doc = Tracked::new(Doc { title: "a".into(), style: (12.0, false) }); /// let (title, size) = bind_tracked!(doc => title, style.0); /// # let _ = (title, size); +/// +/// let mut name = Tracked::new(String::from("a")); +/// let before = name.revision(); +/// let whole = bind_tracked!(name); +/// # let _ = whole; +/// assert_eq!(name.revision(), before, "binding leaves the revision"); /// ``` /// /// Overlapping paths don't compile: @@ -791,4 +797,8 @@ macro_rules! bind_tracked { let (model, count) = $tracked.__split_for_bind(); ($($crate::contract::Bind::__tracked(&mut model.$first $(.$rest)*, count),)+) }}; + ($tracked:expr) => {{ + let (model, count) = $tracked.__split_for_bind(); + $crate::contract::Bind::__tracked(model, count) + }}; } diff --git a/crates/ui-widgets/src/list/tree.rs b/crates/ui-widgets/src/list/tree.rs index 2716271ff..6d41c138b 100644 --- a/crates/ui-widgets/src/list/tree.rs +++ b/crates/ui-widgets/src/list/tree.rs @@ -5,7 +5,7 @@ use std::hash::Hash; /// The groups of a tree that are open, for an app whose source reports `ItemSource::open` from it. Every group is /// closed to begin with. -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, crate::contract::PlainData)] pub struct OpenSet { open: HashSet, }