Pattern fill: a swatch carries the host's picture of its tile, which the editor paints; the painter fn and both thumbnails methods go

This commit is contained in:
aiongg
2026-11-21 03:20:45 +00:00
parent a5d0fd7a51
commit 3e59ee1208
5 changed files with 49 additions and 68 deletions
+1 -3
View File
@@ -89,9 +89,7 @@ pub use paint_bar::{
EraserMode, MIXER_COMBINATIONS, MIXER_GROUP_STARTS, MixerValues, PaintBar, PaintBarOutput, PaintBarWords, PaintTool, PaintValues, Smoothing,
};
pub use paths::{PathKind, PathRow, PathsCommands, PathsEvent, PathsFooterButton, PathsOutput, PathsPanel, PathsWords};
pub use pattern::{
PatternColors, PatternFillDialog, PatternFillEditor, PatternFillOptions, PatternFillOutput, PatternSwatch, PatternWords, SwatchPainter,
};
pub use pattern::{PatternColors, PatternFillDialog, PatternFillEditor, PatternFillOptions, PatternFillOutput, PatternSwatch, PatternWords};
pub use pen::{AnchorDelete, AnchorTool, PenEnd};
pub use pen::{AnchorsShown, HitMark, PATH_OPTIONS_COLOR_KEY, PathMode, PathOptionsPopover, PathOptionsSettings, PathTool, PenMode, SegmentMark};
pub use pen::{
+3 -16
View File
@@ -1,6 +1,6 @@
//! [`PatternFillDialog`].
use super::editor::{ANGLE_RANGE, SwatchPainter};
use super::editor::ANGLE_RANGE;
use super::{PatternColors, PatternFillEditor, PatternFillOptions, PatternSwatch, PatternWords};
use craft_theme::Srgba;
use craft_ui::contract::{Bind, CanvasGesture, Sampler as _, ShowContainer as _};
@@ -24,7 +24,6 @@ pub struct PatternFillDialog {
initial: PatternFillOptions,
current: PatternFillOptions,
swatches: Vec<PatternSwatch>,
thumbnails: Option<SwatchPainter>,
changed: bool,
canvas: Option<egui::Id>,
/// Whether the dialog is open: bound to the form's open state, so the host's dialog closes with it.
@@ -39,20 +38,12 @@ impl PatternFillDialog {
initial: options.clone(),
current: options,
swatches,
thumbnails: None,
changed: false,
canvas: None,
open: true,
}
}
/// Paints a swatch's tile (see [`PatternFillEditor::thumbnails`]).
#[must_use]
pub fn thumbnails(mut self, paint: SwatchPainter) -> Self {
self.thumbnails = Some(paint);
self
}
/// The canvas whose primary drag moves the pattern while Link with Layer is on: the body passes
/// that gesture to it each pass.
#[must_use]
@@ -147,7 +138,7 @@ impl StackDialog for PatternFillDialog {
fn show(&mut self, ui: &mut Ui, _: &DialogStack) -> Option<DialogResult<PatternFillOptions>> {
let words = Env::of(ui).words::<PatternWords>();
let PatternFillDialog { current, swatches, thumbnails, changed, canvas, open, .. } = self;
let PatternFillDialog { current, swatches, changed, canvas, open, .. } = self;
let before = current.clone();
let form = FormDialog::new(words.title.clone(), Bind::bound(&mut *current)).opened(Bind::bound(open));
let shown = form.show(ui, |ui, _, value| {
@@ -158,11 +149,7 @@ impl StackDialog for PatternFillDialog {
if let (Some(canvas), true) = (*canvas, link) {
cx.pass_canvas_gesture(canvas, CanvasGesture::PRIMARY);
}
let mut editor = PatternFillEditor::new(words.pattern.clone(), Bind::bound(&mut *value), swatches);
if let Some(paint) = *thumbnails {
editor = editor.thumbnails(paint);
}
editor.show(ui);
PatternFillEditor::new(words.pattern.clone(), Bind::bound(&mut *value), swatches).show(ui);
});
true
});
+13 -30
View File
@@ -21,18 +21,6 @@ use egui::{Rect, Response, Ui};
/// The angle of the dial runs over half a turn each way.
pub(super) const ANGLE_RANGE: std::ops::RangeInclusive<f64> = -180.0..=180.0;
/// The host's painter of a swatch's tile. A `fn`, which captures nothing, so the presets that paint it key it by its
/// address.
pub type SwatchPainter = fn(&ContentPainterCx<'_>, Rect, &PatternSwatch);
/// What the editor's presets are built from.
#[derive(craft_ui::contract::MemoProps)]
struct TileInputs<'a> {
swatches: &'a [PatternSwatch],
#[memo_props(hash = "a fn pointer: its address names the code, which captures nothing")]
thumbnails: Option<SwatchPainter>,
}
/// What [`PatternFillEditor`] reports.
#[derive(Debug)]
#[non_exhaustive]
@@ -63,7 +51,7 @@ impl Outcome for PatternFillOutput {
/// Preset mode (`colors` set, PowerPoint's Pattern fill): a grid of the swatches, then Foreground and Background
/// colour chips.
///
/// The tiles come from [`thumbnails`](Self::thumbnails); without it a tile shows the swatch's name. Keyboard: Tab
/// A tile shows its swatch's [`picture`](PatternSwatch::picture); without one, the swatch's name. Keyboard: Tab
/// through the fields; the grid takes arrows. Right to left the rows mirror. The editor is the body a host passes to
/// a paint chip's pattern editor.
#[must_use = "show the editor with `show`"]
@@ -72,29 +60,22 @@ pub struct PatternFillEditor<'a> {
label: Label,
options: Bind<'a, PatternFillOptions>,
swatches: &'a [PatternSwatch],
thumbnails: Option<SwatchPainter>,
}
impl<'a> PatternFillEditor<'a> {
/// The editor named `label` over `options`, offering `swatches`.
pub fn new(label: impl Into<Label>, options: Bind<'a, PatternFillOptions>, swatches: &'a [PatternSwatch]) -> Self {
PatternFillEditor { label: label.into(), options, swatches, thumbnails: None }
}
/// Paints a swatch's tile into a rect (the picker's field and cells, the preset grid).
pub fn thumbnails(mut self, paint: SwatchPainter) -> Self {
self.thumbnails = Some(paint);
self
PatternFillEditor { label: label.into(), options, swatches }
}
/// Draws the editor and reports the edit.
pub fn show(self, ui: &mut Ui) -> PatternFillOutput {
let PatternFillEditor { label, mut options, swatches, thumbnails } = self;
let PatternFillEditor { label, mut options, swatches } = self;
let env = Env::of(ui);
let words = env.words::<PatternWords>();
let units = env.units();
let name: Arc<str> = label.as_str().into();
let rows = PresetSource::new(TileInputs { swatches, thumbnails }, PresetFns::new(tile_rows));
let rows = PresetSource::new(swatches, PresetFns::new(tile_rows));
let mut events = EditEvents::new();
let shown = FieldGroup::new(label).show(ui, |ui, cx| {
events = EditEvents::new();
@@ -196,18 +177,20 @@ impl<'a> PatternFillEditor<'a> {
const SCALE_DECIMALS: usize = 2;
/// The preset row of each swatch: its name, with the tile painter when the host gave one.
fn tile_rows(inputs: &TileInputs<'_>, _: &Env) -> Vec<PresetRow<String>> {
fn tile_rows(swatches: &&[PatternSwatch], _: &Env) -> Vec<PresetRow<String>> {
let tile_row = |swatch: &PatternSwatch| {
let row = PresetRow::item(swatch.id.clone(), swatch.name.clone(), 0);
match inputs.thumbnails {
Some(paint) => {
let swatch = swatch.clone();
row.preview(CellShape::Square.aspect(), move |cx, rect| paint(cx, rect, &swatch))
}
match swatch.picture {
Some(picture) => row.preview(CellShape::Square.aspect(), move |cx, rect| paint_picture(cx, rect, picture)),
None => row,
}
};
inputs.swatches.iter().map(tile_row).collect()
swatches.iter().map(tile_row).collect()
}
/// Paints the whole of a swatch's `picture` into `rect`.
fn paint_picture(cx: &ContentPainterCx<'_>, rect: Rect, picture: egui::TextureId) {
cx.picture(picture, rect, Rect::from_min_max(egui::Pos2::ZERO, egui::pos2(1.0, 1.0)), 0.0);
}
/// The key a browser event picked: a click or Enter on a tile.
+14 -4
View File
@@ -11,12 +11,12 @@ pub(crate) mod stories;
mod words;
pub use dialog::PatternFillDialog;
pub use editor::{PatternFillEditor, PatternFillOutput, SwatchPainter};
pub use editor::{PatternFillEditor, PatternFillOutput};
pub use words::PatternWords;
/// A pattern the editor offers: the app's pattern library (PhotoCraft's Patterns, a bitmap each)
/// or its preset patterns (PowerPoint's "pct25", "dkHorz", …). The app paints each swatch's tile
/// ([`PatternFillEditor::thumbnails`]).
/// or its preset patterns (PowerPoint's "pct25", "dkHorz", …), with the app's picture of its tile
/// ([`picture`](Self::picture)); a swatch without one shows its name.
#[derive(Clone, Debug, PartialEq, Eq, Hash, craft_ui::contract::MemoProps)]
#[non_exhaustive]
pub struct PatternSwatch {
@@ -24,12 +24,22 @@ pub struct PatternSwatch {
pub id: String,
/// The name in the tooltip and for a screen reader ("Bubbles", "25 %").
pub name: String,
/// The tile's picture, a texture the app made: the picker's field and cells and the preset grid show it.
#[memo_props(hash = "a texture handle: the texture store keeps one picture under it")]
pub picture: Option<egui::TextureId>,
}
impl PatternSwatch {
/// A swatch of the app's pattern `id`, named `name`.
pub fn new(id: impl Into<String>, name: impl Into<String>) -> Self {
PatternSwatch { id: id.into(), name: name.into() }
PatternSwatch { id: id.into(), name: name.into(), picture: None }
}
/// This swatch with its tile's `picture`.
#[must_use]
pub fn with_picture(mut self, picture: egui::TextureId) -> Self {
self.picture = Some(picture);
self
}
}
+18 -15
View File
@@ -1,46 +1,49 @@
//! Stories of the pattern fill editor: the bitmap mode, the two-colour preset mode and the editor in Photoshop's
//! dialog. The tiles are painted from the theme's colours by a closure, as a host paints its patterns.
//! dialog. Each tile shows a texture the story makes, as a host hands in its patterns' pictures.
use super::{PatternColors, PatternFillEditor, PatternFillOptions, PatternSwatch};
use craft_theme::Srgba;
use craft_ui::contract::Bind;
use craft_ui::patterns::form_dialogs::FormDialog;
use craft_ui::primitives::ContentPainterCx;
use craft_ui::primitives::textures::TextureCache;
use craft_ui::testing::{Drive, StoryData, StoryOutput, story};
use egui::{Rect, Ui};
use egui::{Color32, ColorImage, Ui};
fn swatches() -> Vec<PatternSwatch> {
["Stripes", "Checks", "Dots", "Grid", "Waves", "Bubbles"].iter().map(|name| PatternSwatch::new(name.to_lowercase(), *name)).collect()
}
/// A tile: a plain fill over the whole rect, the stand-in for the host's pattern bitmap.
fn tile(cx: &ContentPainterCx<'_>, rect: Rect, _: &PatternSwatch) {
cx.checker(rect);
/// The library, each tile a checker texture: the stand-in for the host's pattern bitmaps.
fn swatches(ctx: &egui::Context) -> Vec<PatternSwatch> {
let picture = TextureCache::texture(ctx, "pattern-story-tile", || {
let (light, dark) = (Color32::from_gray(225), Color32::from_gray(150));
ColorImage::new([8, 8], (0..64).map(|at| if (at % 8 < 4) == (at / 8 < 4) { light } else { dark }).collect())
});
["Stripes", "Checks", "Dots", "Grid", "Waves", "Bubbles"]
.iter()
.map(|name| PatternSwatch::new(name.to_lowercase(), *name).with_picture(picture))
.collect()
}
fn editor(ui: &mut Ui, data: &mut StoryData, start: PatternFillOptions, library: &[PatternSwatch]) -> StoryOutput {
let (options, options_at) = data.value_at("options", start);
let out = PatternFillEditor::new("Pattern fill", options_at.bind(options), library).thumbnails(tile).show(ui);
let out = PatternFillEditor::new("Pattern fill", options_at.bind(options), library).show(ui);
StoryOutput::from(out)
}
#[story(PatternFillEditor<'static>, covers = [Default], drive = Drive::Never)]
fn pattern_fill_bitmap(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
editor(ui, data, PatternFillOptions::bitmap("bubbles"), &swatches())
editor(ui, data, PatternFillOptions::bitmap("bubbles"), &swatches(ui.ctx()))
}
#[story(PatternFillEditor<'static>, covers = [Default], drive = Drive::Never)]
fn pattern_fill_preset(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
let colors = PatternColors::new(Srgba::rgb(0.1, 0.1, 0.1), Srgba::rgb(0.95, 0.95, 0.9));
editor(ui, data, PatternFillOptions::preset("checks", colors), &swatches())
editor(ui, data, PatternFillOptions::preset("checks", colors), &swatches(ui.ctx()))
}
#[story(PatternFillEditor<'static>, covers = [Default], drive = Drive::Never)]
fn pattern_fill_dialog(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
let library = swatches();
let library = swatches(ui.ctx());
let ((options, options_at), (open, open_at)) = data.pair_at(("options", PatternFillOptions::bitmap("bubbles")), ("open", true));
FormDialog::new("Pattern Fill", options_at.bind(options)).opened(open_at.bind(open)).show(ui, |ui, _, value| {
PatternFillEditor::new("Pattern fill", Bind::bound(value), &library).thumbnails(tile).show(ui);
PatternFillEditor::new("Pattern fill", Bind::bound(value), &library).show(ui);
true
});
StoryOutput::default()