One alias for a host's name check (NameRefuses) in the PDF and print dialogs; the swatch thumb painter's boxed type named; a doubled must_use dropped

This commit is contained in:
bflatastic
2026-11-22 02:03:59 +07:00
parent 5f4c1f282d
commit d14207f668
6 changed files with 20 additions and 14 deletions
+4 -1
View File
@@ -575,9 +575,12 @@ impl SwatchLibrary {
pub(super) struct HostThumb<'a> {
key: u128,
#[memo_props(covered_by = "key", reason = "the painter runs the host's `fn` over the host's inputs, and `key` holds both")]
paint: Box<dyn Fn(&ContentPainterCx<'_>, Rect, ThumbInfo<'_, SwatchKey>) + Send + 'a>,
paint: Box<PaintThumb<'a>>,
}
/// The painter a [`HostThumb`] holds: the host's `fn` over its inputs.
type PaintThumb<'a> = dyn Fn(&ContentPainterCx<'_>, Rect, ThumbInfo<'_, SwatchKey>) + Send + 'a;
impl<'a> HostThumb<'a> {
/// `paint` over `inputs`.
pub(super) fn new<I: MemoProps + Send + 'a>(inputs: I, paint: ThumbFn<I, SwatchKey>) -> Self {
-1
View File
@@ -130,7 +130,6 @@ impl<'a> SwatchesPanel<'a> {
/// Paints each swatch's thumbnail with `paint` over the host's `inputs` in place of the panel's own swatch face
/// (the host's gradient and pattern previews), as `PresetFns::thumb` does: the inputs are all the painter reads, so
/// the swatch list keys on them. The painter draws the selected state from `ThumbInfo::selected`.
#[must_use]
pub fn thumb<I: MemoProps + Send + 'a>(mut self, inputs: I, paint: ThumbFn<I, SwatchKey>) -> Self {
self.thumb = Some(HostThumb::new(inputs, paint));
self
+6 -5
View File
@@ -19,7 +19,8 @@ use craft_ui::patterns::form_dialogs::{
DialogFields, FieldValue, FormDialog, HostRequests, Param, ParamRows, SectionList, set_color_field, set_param_field,
};
use craft_ui::patterns::presets::{
NameCheck, NamedPresetEvent, NamedPresetRow, Naming, PickerField, PresetError, PresetPicker, PresetRow, PresetSource, PresetState, PresetWords,
NameCheck, NameRefuses, NamedPresetEvent, NamedPresetRow, Naming, PickerField, PresetError, PresetPicker, PresetRow, PresetSource, PresetState,
PresetWords,
};
use craft_ui::runtime::env::{Env, Label, Message};
use craft_ui::runtime::focus::{ActivateKeys, KeepKeys, KeyUse};
@@ -516,7 +517,7 @@ fn preset_row(
ui: &mut Ui,
words: &PrintDialogWords,
presets: &PrintPresets<'_>,
check: Option<&dyn Fn(&str) -> Option<PresetError>>,
check: Option<&NameRefuses<'_>>,
picker: &mut PresetState<String>,
events: &mut Vec<NamedPresetEvent<String>>,
) {
@@ -533,7 +534,7 @@ fn preset_row(
}
/// The preset editor's name field with the host's refusal under it. Returns whether the name can be saved.
fn name_row(ui: &mut Ui, words: &PrintDialogWords, check: Option<&dyn Fn(&str) -> Option<PresetError>>, name: &mut Bind<'_, String>) -> bool {
fn name_row(ui: &mut Ui, words: &PrintDialogWords, check: Option<&NameRefuses<'_>>, name: &mut Bind<'_, String>) -> bool {
let preset_words = Env::of(ui).words::<PresetWords>();
let current = read(name).unwrap_or_default();
let host = check.map(NameCheck::of_host);
@@ -618,8 +619,8 @@ pub struct PrintDialog<'a> {
#[prop]
preset_name: Option<Bind<'a, String>>,
/// Whether a typed preset name may be used, in the preset editor.
#[prop(boxed)]
check_name: Option<Box<dyn Fn(&str) -> Option<PresetError> + 'a>>,
#[prop(boxed = "Fn(&str) -> Option<PresetError> + 'a")]
check_name: Option<Box<NameRefuses<'a>>>,
/// The driver's Setup… button is shown, with [`PrintLayout::Sections`].
#[prop]
driver: bool,
@@ -25,8 +25,8 @@ use crate::form::{Field, FieldGroup};
use crate::list::ListRow;
use crate::notices::{Notice, NoticeLayout};
use crate::patterns::presets::{
NameCheck, NamedPresetEvent, NamedPresetRow, Naming, PickerField, PresetError, PresetFns, PresetPicker, PresetRow, PresetSource, PresetState,
PresetWords,
NameCheck, NameRefuses, NamedPresetEvent, NamedPresetRow, Naming, PickerField, PresetError, PresetFns, PresetPicker, PresetRow, PresetSource,
PresetState, PresetWords,
};
use crate::pickers::Select;
use crate::runtime::env::{Env, Label, Message, UnitKind};
@@ -342,8 +342,8 @@ pub struct PdfExportDialog<'a> {
#[prop]
preset_draft: Option<Bind<'a, PresetDraft>>,
/// Whether a typed preset name may be used, in the preset editor.
#[prop(boxed)]
check_name: Option<Box<dyn Fn(&str) -> Option<PresetError> + 'a>>,
#[prop(boxed = "Fn(&str) -> Option<PresetError> + 'a")]
check_name: Option<Box<NameRefuses<'a>>>,
/// Binds the shown section.
#[prop]
section: Option<Bind<'a, PdfSection>>,
@@ -30,7 +30,7 @@ pub use model::{
CellShape, GroupPath, PresetChange, PresetError, PresetGroup, PresetId, PresetItem, PresetKey, PresetKind, PresetLibrary, PresetNames, PresetRow,
PresetRowMenu, PresetTiles, Preview, ThumbInfo, children_end, has_captions, nest_allowed,
};
pub use named::{NameCheck, NamedPresetCommands, NamedPresetEvent, NamedPresetOutput, NamedPresetRow, NamedPresetStyle, Naming};
pub use named::{NameCheck, NameRefuses, NamedPresetCommands, NamedPresetEvent, NamedPresetOutput, NamedPresetRow, NamedPresetStyle, Naming};
pub use panel_menu::{PresetCommands, PresetMenuAction, PresetPanelMenu};
pub use picker::{PickerField, PickerHead, PresetPicker, PresetPickerOutput, PresetPickerStyle};
pub use source::{AcceptsFn, PresetFns, PresetSource, ThumbFn};
@@ -61,6 +61,9 @@ pub enum NamedPresetStyle {
Menu,
}
/// A host's check of a typed preset name, as a closure: why it refuses the name, `None` when it accepts it.
pub type NameRefuses<'a> = dyn Fn(&str) -> Option<PresetError> + 'a;
/// Whether a name may be used: the host's answer for a typed name.
pub struct NameCheck<'a>(Box<dyn NameChecker + 'a>);
@@ -74,7 +77,7 @@ impl<'a> NameCheck<'a> {
/// The host's `check`, a closure: a memoized view that holds it runs every pass, as no key can follow what the
/// closure reads.
pub fn of_host(check: &'a dyn Fn(&str) -> Option<PresetError>) -> Self {
pub fn of_host(check: &'a NameRefuses<'a>) -> Self {
NameCheck::new(HostCheck(check), |host, name| (host.0)(name))
}
@@ -95,7 +98,7 @@ impl MemoProps for NameCheck<'_> {
/// The inputs of [`NameCheck::of_host`].
#[derive(MemoProps)]
#[memo_props(every_pass = "the host's name check is a closure, whose answers no key can follow")]
struct HostCheck<'a>(&'a dyn Fn(&str) -> Option<PresetError>);
struct HostCheck<'a>(&'a NameRefuses<'a>);
/// A name check: its answer, and the key of its inputs and `fn`.
trait NameChecker {