craft-ui-panels: HidePanels derives MemoProps, Titles keys through its content key, ToolRail::hidden is a derived prop

This commit is contained in:
aiongg
2026-11-20 18:17:24 +00:00
parent 8b7fb7143d
commit b250890413
5 changed files with 13 additions and 30 deletions
+2 -15
View File
@@ -1,7 +1,7 @@
//! `DockEvent`, `FloatSpace` and `HidePanels`: what a dock reports to the host and the host tells a dock.
use super::{DockCommands, DockSizes, DragUnit, PanelKey};
use crate::contract::{MemoKey, MemoProps, Tracked};
use crate::contract::{MemoProps, Tracked};
use egui::{Rect, Vec2};
use std::collections::BTreeSet;
@@ -47,7 +47,7 @@ pub enum DockEvent<P> {
///
/// The set is a `Tracked` value, so a dock keys what it shows by the set's revision and keeps the pruned layout until
/// the set, the workspace or the shell's visibility changes.
#[derive(Clone, Copy)]
#[derive(Clone, Copy, MemoProps)]
#[non_exhaustive]
pub enum HidePanels<'a> {
/// Hides none.
@@ -58,19 +58,6 @@ pub enum HidePanels<'a> {
Except(&'a Tracked<BTreeSet<PanelKey>>),
}
impl MemoProps for HidePanels<'_> {
fn key(&self, key: &mut MemoKey) {
match self {
HidePanels::None => key.write_u32(0),
HidePanels::All => key.write_u32(1),
HidePanels::Except(keep) => {
key.write_u32(2);
keep.key(key);
}
}
}
}
/// Where a docking command floats a panel, group or column when it gives no rect. A dock records one each pass;
/// the host reads it to turn a command into placements.
#[derive(Clone, Debug, PartialEq)]
+1
View File
@@ -585,6 +585,7 @@ struct TabRow<'a, P> {
/// The group's anchor panel, which names its drop spots.
#[memo_props(hash = "a dock's panels are plain values it keys by their hash")]
anchor: P,
#[memo_props(hash = "its Hash is the content key taken when the titles were made")]
panels: Titles,
active: PanelKey,
closable: bool,
+4 -3
View File
@@ -136,9 +136,10 @@ impl Titles {
}
}
impl crate::contract::MemoProps for Titles {
fn key(&self, key: &mut crate::contract::MemoKey) {
self.key.key(key);
/// Feeds the content key taken when the titles were made, not the titles.
impl Hash for Titles {
fn hash<H: Hasher>(&self, state: &mut H) {
self.key.hash(state);
}
}
+2 -11
View File
@@ -120,6 +120,7 @@ pub struct ToolRail<'a, T: Value + MemoProps + PlainData + 'static> {
#[prop(flag)]
editing: bool,
/// The tools the user's toolbar settings hide.
#[prop(into)]
hidden: Option<&'a Tracked<Vec<T>>>,
footer: Option<FooterFn<'a>>,
/// The columns the rail lays out in this pass.
@@ -155,13 +156,6 @@ impl<'a, T: Value + MemoProps + PlainData + 'static> ToolRail<'a, T> {
.label(label)
}
/// The tools the user's toolbar settings hide.
#[must_use]
pub fn hidden(mut self, hidden: &'a Tracked<Vec<T>>) -> Self {
self.hidden = Some(hidden);
self
}
/// The cells the rail lays out: its visible slots and the Edit Toolbar button.
fn cells(&self) -> usize {
visible_slots(self.slots, hidden_in(self.hidden)).len() + usize::from(self.edit_toolbar.is_some())
@@ -189,10 +183,7 @@ impl<'a, T: Value + MemoProps + PlainData + 'static> ToolRail<'a, T> {
}
/// The tools a rail hides: the host's, or none.
fn hidden_in<'a, T>(hidden: Option<&'a Tracked<Vec<T>>>) -> &'a [T]
where
T: PlainData,
{
fn hidden_in<T: PlainData>(hidden: Option<&Tracked<Vec<T>>>) -> &[T] {
hidden.map_or(&[], |hidden| hidden.as_slice())
}
+4 -1
View File
@@ -284,6 +284,9 @@ fn an_open_flyout_builds_no_derived_values() {
}
}
/// The room of the key-byte trial.
const KEY_BYTES_ROOM: Vec2 = Vec2::new(120.0, 400.0);
/// The key bytes of an idle pass of a rail of `count` slots, a quarter of whose tools are hidden and half remembered.
fn idle_key_bytes(count: u8) -> usize {
let slots = Tracked::new((0..count).map(|id| ToolSlot::new(u32::from(id), tools(&[id]))).collect::<Vec<_>>());
@@ -296,7 +299,7 @@ fn idle_key_bytes(count: u8) -> usize {
let mut fixture = Fixture::new(move |ui| {
ToolRail::new("Tools", &slots, &0, &last).hidden(&hidden).show_rail(ui, |_, _| {});
});
fixture.room(Vec2::new(120.0, 400.0)).settle().idle(1);
fixture.room(KEY_BYTES_ROOM).settle().idle(1);
assert_eq!(last_frame_derived_builds(fixture.ctx()), Vec::new(), "an idle rail builds no derived value");
last_pass_memo_key_bytes(fixture.ctx())
}