mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 02:09:00 +00:00
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.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -721,7 +721,7 @@ impl Pass<'_> {
|
||||
let shown = {
|
||||
let presets = library.borrow();
|
||||
let names: Vec<String> = 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<T: craft_behavior::Value>(ui: &mut Ui, name: &Label, salt: &str, entri
|
||||
}
|
||||
|
||||
/// One preset row per name, keyed by it.
|
||||
fn name_rows(names: &Vec<String>, _: &Env) -> Vec<PresetRow<String>> {
|
||||
fn name_rows(names: &&[String], _: &Env) -> Vec<PresetRow<String>> {
|
||||
names.iter().map(|name| PresetRow::item(name.clone(), name.clone(), 0)).collect()
|
||||
}
|
||||
|
||||
@@ -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<PresetRow<String>> {
|
||||
fn preset_rows(names: &&[(String, String)], _: &Env) -> Vec<PresetRow<String>> {
|
||||
names.iter().map(|(name, shown)| PresetRow::item(name.clone(), shown.clone(), 0)).collect()
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<String>, name: &str) -> Option<PresetError> {
|
||||
fn taken<const N: usize>(names: &[&str; N], name: &str) -> Option<PresetError> {
|
||||
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::<Vec<_>>(), 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::<Vec<_>>(), taken))
|
||||
.check_name(NameCheck::new(TRACE, taken))
|
||||
.built_in(|_| true)
|
||||
.files(&files)
|
||||
.show(ui)
|
||||
|
||||
@@ -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<I, K> = fn(&I, &ListDrag<K>, Option<&K>, usize) -> bool;
|
||||
|
||||
/// The host's thumbnail painter: the inputs, the painter, the thumbnail's rect and the row.
|
||||
pub type ThumbFn<I, K> = 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<I, K> {
|
||||
@@ -16,8 +23,8 @@ pub struct PresetFns<I, K> {
|
||||
can_rename: Option<fn(&I, &K) -> bool>,
|
||||
can_delete: Option<fn(&I, &K) -> bool>,
|
||||
can_move: Option<fn(&I, &K) -> bool>,
|
||||
accepts: Option<fn(&I, &ListDrag<K>, Option<&K>, usize) -> bool>,
|
||||
thumb: Option<fn(&I, &ContentPainterCx, Rect, ThumbInfo<'_, K>)>,
|
||||
accepts: Option<AcceptsFn<I, K>>,
|
||||
thumb: Option<ThumbFn<I, K>>,
|
||||
}
|
||||
|
||||
impl<I, K> Clone for PresetFns<I, K> {
|
||||
@@ -58,14 +65,14 @@ impl<I, K> PresetFns<I, K> {
|
||||
/// 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<K>, Option<&K>, usize) -> bool) -> Self {
|
||||
pub fn accepts(mut self, test: AcceptsFn<I, K>) -> 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<I, K>) -> 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<K> 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<K> 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<K> {
|
||||
fn key(&self, key: &mut MemoKey);
|
||||
|
||||
Reference in New Issue
Block a user