From 5ef4a8567e33e8e0f327472f1453e10126b8a593 Mon Sep 17 00:00:00 2001 From: aiongg Date: Fri, 20 Nov 2026 22:17:20 +0000 Subject: [PATCH] Preset sources: lint fixes - The kept rows key on a tuple of the source and the units; the layer key's MemoProps names it through runtime::egui. - Named fn types for a source's drop test and thumbnail painter; the export dialogs' sources take their names as slices, the named-row stories' name checks arrays. --- crates/ui-contract/src/contract/memo.rs | 2 +- .../patterns/form_dialogs/export/as_dialog.rs | 4 +-- .../src/patterns/form_dialogs/pdf_export.rs | 4 +-- .../ui-patterns/src/patterns/presets/mod.rs | 2 +- .../src/patterns/presets/named/stories.rs | 6 ++-- .../src/patterns/presets/source.rs | 30 ++++++++----------- 6 files changed, 21 insertions(+), 27 deletions(-) diff --git a/crates/ui-contract/src/contract/memo.rs b/crates/ui-contract/src/contract/memo.rs index 61dc0e0c1..2f4bd1e89 100644 --- a/crates/ui-contract/src/contract/memo.rs +++ b/crates/ui-contract/src/contract/memo.rs @@ -1056,7 +1056,7 @@ impl MemoProps for craft_host::target::WindowKind { } /// Derived `Hash` (the layer's order and id). -impl MemoProps for egui::LayerId { +impl MemoProps for crate::runtime::egui::areas::LayerKey { fn key(&self, key: &mut MemoKey) { key.feed_hash(self); } diff --git a/crates/ui-patterns/src/patterns/form_dialogs/export/as_dialog.rs b/crates/ui-patterns/src/patterns/form_dialogs/export/as_dialog.rs index 286f54e73..160b92a3f 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/export/as_dialog.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/export/as_dialog.rs @@ -721,7 +721,7 @@ impl Pass<'_> { let shown = { let presets = library.borrow(); let names: Vec = presets.items().iter().map(|item| item.name.clone()).collect(); - let rows = PresetSource::new(names, PresetFns::new(name_rows)); + let rows = PresetSource::new(names.as_slice(), PresetFns::new(name_rows)); let current = presets.items().iter().find(|item| item.value == *options).map(|item| item.name.clone()); let picker = PresetPicker::new(words.preset.clone(), Bind::given(current), &rows, Bind::bound(&mut held.picker)).field(PickerField::Name); NamedPresetRow::new(words.preset.clone(), picker) @@ -961,6 +961,6 @@ fn choose(ui: &mut Ui, name: &Label, salt: &str, entri } /// One preset row per name, keyed by it. -fn name_rows(names: &Vec, _: &Env) -> Vec> { +fn name_rows(names: &&[String], _: &Env) -> Vec> { names.iter().map(|name| PresetRow::item(name.clone(), name.clone(), 0)).collect() } diff --git a/crates/ui-patterns/src/patterns/form_dialogs/pdf_export.rs b/crates/ui-patterns/src/patterns/form_dialogs/pdf_export.rs index 581b88ca7..1b5a48347 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/pdf_export.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/pdf_export.rs @@ -332,7 +332,7 @@ impl<'a> PdfExportDialog<'a> { // The preset row. let names: Vec<(String, String)> = presets.iter().map(|preset| (preset.name.clone(), words.preset_name(&preset.name, &app).as_str().to_owned())).collect(); - let rows = PresetSource::new(names, PresetFns::new(preset_rows)); + let rows = PresetSource::new(names.as_slice(), PresetFns::new(preset_rows)); let selected = live.preset.as_ref().and_then(|name| presets.iter().find(|preset| preset.name == *name)); let modified = selected.is_some_and(|preset| { let mut shown = work.clone(); @@ -500,6 +500,6 @@ impl<'a> PdfExportDialog<'a> { } /// One preset row per (name, shown name), keyed by the name. -fn preset_rows(names: &Vec<(String, String)>, _: &Env) -> Vec> { +fn preset_rows(names: &&[(String, String)], _: &Env) -> Vec> { names.iter().map(|(name, shown)| PresetRow::item(name.clone(), shown.clone(), 0)).collect() } diff --git a/crates/ui-patterns/src/patterns/presets/mod.rs b/crates/ui-patterns/src/patterns/presets/mod.rs index 6f49750bb..340005861 100644 --- a/crates/ui-patterns/src/patterns/presets/mod.rs +++ b/crates/ui-patterns/src/patterns/presets/mod.rs @@ -32,6 +32,6 @@ pub use model::{ pub use named::{NameCheck, NamedPresetCommands, NamedPresetEvent, NamedPresetOutput, NamedPresetRow, NamedPresetStyle}; pub use panel_menu::{PresetCommands, PresetMenuAction, PresetPanelMenu}; pub use picker::{PickerField, PickerHead, PresetPicker, PresetPickerOutput, PresetPickerStyle}; -pub use source::{PresetFns, PresetSource}; +pub use source::{AcceptsFn, PresetFns, PresetSource, ThumbFn}; pub use state::{DefaultOpen, PresetState, PresetView, default_is_open}; pub use words::PresetWords; diff --git a/crates/ui-patterns/src/patterns/presets/named/stories.rs b/crates/ui-patterns/src/patterns/presets/named/stories.rs index b69d62748..e05625e39 100644 --- a/crates/ui-patterns/src/patterns/presets/named/stories.rs +++ b/crates/ui-patterns/src/patterns/presets/named/stories.rs @@ -10,7 +10,7 @@ use egui::Ui; const TRACE: [&str; 6] = ["Default", "High Fidelity Photo", "Low Fidelity Photo", "3 Colors", "Sketched Art", "Line Art"]; -fn taken(names: &Vec, name: &str) -> Option { +fn taken(names: &[&str; N], name: &str) -> Option { names.iter().any(|label| label.eq_ignore_ascii_case(name)).then_some(PresetError::NameTaken) } @@ -37,7 +37,7 @@ fn contour_buttons(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { NamedPresetRow::new("Contour", picker) .style(NamedPresetStyle::Buttons) .suggest_name("Contour 5") - .check_name(NameCheck::new((1..=4_u32).map(|n| format!("Contour {n}")).collect::>(), taken)) + .check_name(NameCheck::new(["Contour 1", "Contour 2", "Contour 3", "Contour 4"], taken)) .built_in(|_| true) .files(&files) .show(ui) @@ -55,7 +55,7 @@ fn trace_menu(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { NamedPresetRow::new("Trace preset", picker) .style(NamedPresetStyle::Menu) .suggest_name("Preset 7") - .check_name(NameCheck::new(TRACE.iter().map(|name| (*name).to_owned()).collect::>(), taken)) + .check_name(NameCheck::new(TRACE, taken)) .built_in(|_| true) .files(&files) .show(ui) diff --git a/crates/ui-patterns/src/patterns/presets/source.rs b/crates/ui-patterns/src/patterns/presets/source.rs index e3f283834..3b97a9163 100644 --- a/crates/ui-patterns/src/patterns/presets/source.rs +++ b/crates/ui-patterns/src/patterns/presets/source.rs @@ -9,6 +9,13 @@ use crate::runtime::env::Env; use egui::{Context, Id, Rect}; use std::sync::Arc; +/// Whether a drag of presets may drop at a place: the inputs, the drag, the preset under the pointer and the insert +/// position. +pub type AcceptsFn = fn(&I, &ListDrag, Option<&K>, usize) -> bool; + +/// The host's thumbnail painter: the inputs, the painter, the thumbnail's rect and the row. +pub type ThumbFn = fn(&I, &ContentPainterCx, Rect, ThumbInfo<'_, K>); + /// The `fn`s of a [`PresetSource`]: how the rows are built from the inputs `I`, and what the host answers for a row. /// Each is a `fn`, which captures nothing, so the inputs are all a row can depend on. pub struct PresetFns { @@ -16,8 +23,8 @@ pub struct PresetFns { can_rename: Option bool>, can_delete: Option bool>, can_move: Option bool>, - accepts: Option, Option<&K>, usize) -> bool>, - thumb: Option)>, + accepts: Option>, + thumb: Option>, } impl Clone for PresetFns { @@ -58,14 +65,14 @@ impl PresetFns { /// Refuses the reorder landings for which the test is false: it gets the drag, the row under the pointer (none /// over an empty browser) and the position before which the dragged rows would go. #[must_use] - pub fn accepts(mut self, test: fn(&I, &ListDrag, Option<&K>, usize) -> bool) -> Self { + pub fn accepts(mut self, test: AcceptsFn) -> Self { self.accepts = Some(test); self } /// Paints every preset's thumbnail (a swatch, a gradient ramp) in place of its row's own `preview`. #[must_use] - pub fn thumb(mut self, paint: fn(&I, &ContentPainterCx, Rect, ThumbInfo<'_, K>)) -> Self { + pub fn thumb(mut self, paint: ThumbFn) -> Self { self.thumb = Some(paint); self } @@ -104,7 +111,7 @@ impl<'a, K: Clone + PartialEq + Send + Sync + 'static> PresetSource<'a, K> { self.0.key(&mut key); crate::contract::MemoProps::key(&units, &mut key); let id = Id::NULL.with(("preset-rows", key.finish())); - derived(ctx, id, &RowsKey { source: self, units: &units }, || self.0.rows(env)) + derived(ctx, id, &(self, &units), || self.0.rows(env)) } /// Whether the row `key` may be renamed. @@ -145,19 +152,6 @@ impl MemoProps for PresetSource<'_, K> { } } -/// The key the kept rows are built from: the source and the scope's units, which carry its words and number format. -struct RowsKey<'s, 'a, K> { - source: &'s PresetSource<'a, K>, - units: &'s crate::runtime::env::Units, -} - -impl MemoProps for RowsKey<'_, '_, K> { - fn key(&self, key: &mut MemoKey) { - self.source.key(key); - self.units.key(key); - } -} - /// A source: its rows and answers, and the key of its inputs and `fn`s. trait Source { fn key(&self, key: &mut MemoKey);