Files
craft-libs/crates/draw/src/ui/stroke/editor.rs
T

542 lines
24 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! [`StrokeEditor`]: the stroke editor's body, the one a popover or a docked panel hosts.
use std::borrow::Cow;
use std::ops::RangeInclusive;
use craft_behavior::{CancelReason, ColumnWidth, DisplayOrder, EditEvent, EditEvents, IconId, ItemSource, Mixed, Selection, Value};
use craft_theme::variant::Tone;
use craft_ui::choice::{Checkbox, Segmented};
use craft_ui::contract::{
Bind, Entry, NoValue, Outcome, Props as _, Remember as _, SelectMode, Show as _, ShowCollection as _, ShowContainer as _, Sizing,
};
use craft_ui::display::{Divider, Shade, Text};
use craft_ui::form::{Field, FieldGroup};
use craft_ui::layout_components::{Grid, Stack};
use craft_ui::list::{List, ListRow, ThumbAspect, ThumbSize};
use craft_ui::pickers::Select;
use craft_ui::primitives::{ContentPainterCx, IconSpec};
use craft_ui::runtime::env::{Env, Label, UnitKind};
use craft_ui::style::TypeRole;
use craft_ui::text::NumberField;
use egui::{Rect, Response, Ui};
use std::sync::Arc;
use super::{
DashAlign, DashUnit, LineCap, LineJoin, LineStyle, StrokeAlign, StrokeChoiceRow, StrokeField, StrokeMixed, StrokePreview, StrokeSections,
StrokeStyle, StrokeWords,
};
/// The width of a preset row's line sample as a multiple of its height.
const PRESET_ASPECT: (u16, u16) = (8, 1);
/// The dash and gap fields in a row: Illustrator's six in two rows, so each field keeps the width of its widest value.
const DASH_FIELDS: usize = 3;
/// The dash and gap fields the editor always shows (Illustrator's Stroke panel).
const DASH_SHOWN: usize = 6;
/// The columns of a row of dash and gap fields.
const DASH_COLUMNS: &[ColumnWidth] = &[ColumnWidth::Content; DASH_FIELDS];
/// The preset rows, in list order.
const PRESETS: [LineStyle; 3] = [LineStyle::Solid, LineStyle::Dashed, LineStyle::Dotted];
/// What the stroke editor reports.
#[derive(Debug)]
#[non_exhaustive]
#[must_use = "the host applies the editor's event"]
pub struct StrokeEditorOutput {
/// The editor's whole area.
pub response: Response,
/// The valid changes, in order, each the whole new style: a preset row, a select, a segmented button and the check
/// are `Set`s; a number field's typed value is a `Set` and its scrub a gesture (Escape cancels). A style that fails
/// [`StrokeStyle::validate`] is never reported. A binding that writes through has them already.
pub events: EditEvents<StrokeStyle>,
/// The control the events came from, so the host coalesces one field's edits into one undo step.
pub field: Option<StrokeField>,
/// A pick in a [`StrokeChoiceRow`]: the row's id and the choice's id.
pub choice: Option<(String, String)>,
}
impl Outcome for StrokeEditorOutput {
type Value = NoValue;
fn response(&self) -> &Response {
&self.response
}
}
/// The stroke editor's body (PhotoCraft's Stroke Options popover body; VectorCraft's and DesignCraft's stroke panels
/// with [`StrokeSections::PANEL`]). It edits the host's [`StrokeStyle`] and reports every valid change at once; there
/// is no OK button (lead call 21).
///
/// - Presets ([`StrokeSections::PRESETS`]): Solid, Dashed (`[4, 2]`), Dotted (`[0, 2]`, round caps) in a list, each
/// with a line sample drawn at opacity 1; the row matching [`StrokeStyle::line_style`] is selected, none for Custom.
/// Picking one applies [`StrokeStyle::with_line_style`].
/// - Rows: Alignment (Inside, Center, Outside; enabled only when [`closed`](Self::closed)); Caps (Butt / Flat, Round,
/// Square; disabled for a solid closed path unless [`caps_on_closed`](Self::caps_on_closed)); Corners (Miter, Round, Bevel; enabled only with
/// [`corners`](Self::corners)); the app's [`rows`](Self::rows); Miter limit (1–500, `×`; enabled only for Miter
/// corners); Opacity ([`StrokeSections::OPACITY`], 0–100 %, stored 0..=1). Choices are selects, or segmented icon
/// buttons with [`StrokeSections::ICONS`]. Under the rows of an open path: "Open paths use centered strokes".
/// - Dashes ([`StrokeSections::DASHES`]): a "Dashed line" check (setting it applies the Dashed preset, clearing it
/// Solid); while dashed, a hint by [`StrokeStyle::dash_unit`], then Illustrator's six fields in two rows of
/// three, dash, gap, dash / gap, dash, gap (0–10000, `×`, or `pt` for document units), each with its word under
/// it. A field shows its entry of [`StrokeStyle::dashes`] or is blank; typing in the blank after the pattern adds
/// an entry, the blanks after it are off, and clearing a field after the first ends the pattern there. A pattern
/// longer than six continues in further rows of three. Then "Dash offset" (±1,000,000) and, with
/// [`StrokeSections::DASH_ALIGN`], "Align dashes" (Exact, Corners).
/// - While the style fails [`StrokeStyle::validate`], the invalid word shows at the end and nothing is reported.
/// - [`mixed`](Self::mixed): a mixed field shows the mixed state and lights no button or preset; the first edit sets it
/// for all.
///
/// ```ignore
/// let out = StrokeEditor::new("Stroke", Bind::bound(&mut style)).sections(StrokeSections::PANEL).show(ui);
/// ```
#[must_use = "show the editor with `show`"]
#[derive(craft_ui::contract::Pattern)]
pub struct StrokeEditor<'a> {
label: Label,
value: Bind<'a, StrokeStyle>,
sections: StrokeSections,
closed: bool,
caps_on_closed: bool,
corners: bool,
mixed: StrokeMixed,
rows: &'a [StrokeChoiceRow<'a>],
}
impl<'a> StrokeEditor<'a> {
/// The editor named `label` over `value`.
pub fn new(label: impl Into<Label>, value: Bind<'a, StrokeStyle>) -> Self {
Self {
label: label.into(),
value,
sections: StrokeSections::default(),
closed: true,
caps_on_closed: false,
corners: true,
mixed: StrokeMixed::default(),
rows: &[],
}
}
/// Which parts show; [`StrokeSections::POPOVER`] by default.
pub fn sections(mut self, sections: StrokeSections) -> Self {
self.sections = sections;
self
}
/// Whether the stroked path is closed: alignment applies and caps need dashes, unless
/// [`caps_on_closed`](Self::caps_on_closed) is set. True by default.
pub fn closed(mut self, closed: bool) -> Self {
self.closed = closed;
self
}
/// Whether the Caps row stays enabled on a closed path without dashes. False by default: the row is then disabled
/// for a solid closed path and enabled for an open or dashed one.
pub fn caps_on_closed(mut self, enabled: bool) -> Self {
self.caps_on_closed = enabled;
self
}
/// Whether the path has corners for the Corners and Miter limit rows (an ellipse has none). True by default.
pub fn corners(mut self, corners: bool) -> Self {
self.corners = corners;
self
}
/// The fields that differ across the host's selection.
pub fn mixed(mut self, mixed: &StrokeMixed) -> Self {
self.mixed = *mixed;
self
}
/// The app's choice rows, after the Corners row.
pub fn rows(mut self, rows: &'a [StrokeChoiceRow<'a>]) -> Self {
self.rows = rows;
self
}
/// Shows the editor.
pub fn show(self, ui: &mut Ui) -> StrokeEditorOutput {
let StrokeEditor { label, mut value, sections, closed, caps_on_closed, corners, mixed, rows } = self;
let env = Env::of(ui);
let words = env.words::<StrokeWords>();
let name: Arc<str> = label.as_str().into();
let mut found = Found::default();
let shown = Stack::new().label(label).show(ui, |ui, cx| {
found = Found::default();
let held = match value.reborrow().read(cx) {
Some(Mixed::Same(held)) => held,
_ => StrokeStyle::default(),
};
let kept = cx.memory::<Pending>().clone();
let style = match kept.0 {
Some((base, draft)) if base == held => draft,
_ => held.clone(),
};
let body = Body { words: &words, style: &style, sections, closed, caps_on_closed, corners, mixed, rows, name: name.clone() };
body.show(ui, &mut found);
if !found.events.is_empty() {
for event in &found.events {
let _ = value.reborrow().write(cx, event);
}
*cx.memory::<Pending>() = Pending(None);
} else if let Some(draft) = found.invalid.take() {
*cx.memory::<Pending>() = Pending(Some((held, draft)));
}
});
StrokeEditorOutput { response: shown.output, events: found.events, field: found.field, choice: found.choice }
}
}
/// The invalid edit the user last made on `base`, kept until the host's value or a valid edit replaces it: the fields
/// show it and the message names it.
#[derive(Clone, Debug, Default, PartialEq)]
struct Pending(Option<(StrokeStyle, StrokeStyle)>);
/// What one pass found: the events of the first control that reported, the field they came from, a choice pick and the
/// last invalid edit.
#[derive(Default)]
struct Found {
events: EditEvents<StrokeStyle>,
field: Option<StrokeField>,
choice: Option<(String, String)>,
invalid: Option<StrokeStyle>,
}
impl Found {
/// Takes the events of the first control that reports in the pass; a field's gesture is the only thing that
/// reports more than once.
fn push(&mut self, field: StrokeField, events: impl Into<EditEvents<StrokeStyle>>) {
for event in events.into() {
if self.field.is_some_and(|first| first != field) {
return;
}
if let Some(style) = event.value().filter(|style| style.validate().is_err()) {
self.invalid = Some(style.clone());
}
if let Some(event) = checked(event) {
self.events.push(event);
self.field = Some(field);
}
}
}
}
/// The event as the editor reports it: a style that fails [`StrokeStyle::validate`] is never reported, so a gesture
/// whose value turns invalid ends in a cancel.
fn checked(event: EditEvent<StrokeStyle>) -> Option<EditEvent<StrokeStyle>> {
let valid = |style: &StrokeStyle| style.validate().is_ok();
match event {
EditEvent::Update { ref value, .. } if !valid(value) => None,
EditEvent::Set { ref after, .. } if !valid(after) => None,
EditEvent::Commit { id, before, ref after, .. } if !valid(after) => Some(EditEvent::cancel(id, CancelReason::Refused, Some(before))),
event => Some(event),
}
}
/// The preset rows as the list's item source: the position is the key.
struct PresetRows;
impl DisplayOrder<usize> for PresetRows {
fn len(&self) -> usize {
PRESETS.len()
}
fn key(&self, at: usize) -> usize {
at
}
fn text(&self, _at: usize) -> Option<Cow<'_, str>> {
None
}
}
impl ItemSource<usize> for PresetRows {}
/// One pass of the editor: what it draws from.
struct Body<'a> {
words: &'a StrokeWords,
/// The host's value, or the pending invalid edit made on it.
style: &'a StrokeStyle,
sections: StrokeSections,
closed: bool,
caps_on_closed: bool,
corners: bool,
mixed: StrokeMixed,
rows: &'a [StrokeChoiceRow<'a>],
/// The name of the edits, for the host's undo.
name: Arc<str>,
}
impl Body<'_> {
/// Reports the one-step change to `next`.
fn set(&self, found: &mut Found, field: StrokeField, next: StrokeStyle) {
let event = EditEvent::set(Mixed::Same(self.style.clone()), next, self.name.clone(), None);
found.push(field, event);
}
fn show(&self, ui: &mut Ui, found: &mut Found) {
if self.sections.contains(StrokeSections::PRESETS) {
self.presets(ui, found);
Divider::horizontal().show(ui);
}
self.grid(ui, found);
if !self.closed {
Text::new(self.words.open_paths.clone()).role(TypeRole::Caption).shade(Shade::Muted).show(ui);
}
if self.sections.contains(StrokeSections::DASHES) {
Divider::horizontal().show(ui);
self.dashes(ui, found);
}
if found.invalid.is_some() || self.style.validate().is_err() {
Text::new(self.words.invalid.clone()).role(TypeRole::Caption).tone(Tone::Bad).show(ui);
}
}
fn preset_name(&self, preset: LineStyle) -> Label {
match preset {
LineStyle::Solid => self.words.solid.clone(),
LineStyle::Dashed => self.words.dashed.clone(),
LineStyle::Dotted => self.words.dotted.clone(),
LineStyle::Custom => self.words.custom.clone(),
}
}
fn presets(&self, ui: &mut Ui, found: &mut Found) {
let current = if self.mixed.dashes { None } else { PRESETS.iter().position(|preset| *preset == self.style.line_style()) };
// The rows show their pattern whatever the opacity is.
let samples: Vec<StrokeStyle> = PRESETS
.iter()
.map(|preset| {
let mut sample = self.style.with_line_style(*preset);
sample.opacity = 1.0;
sample
})
.collect();
let mut selection = current.map_or_else(Selection::default, Selection::single);
List::new(self.words.title.clone())
.id_salt("presets")
.select(SelectMode::Single)
.natural_height(true)
.selection(Bind::bound(&mut selection))
.show_items(ui, &PresetRows, &mut |at, _| {
let sample = &samples[*at];
ListRow::new(self.preset_name(PRESETS[*at])).thumb_aspect(
ThumbSize::Small,
ThumbAspect::new(PRESET_ASPECT.0, PRESET_ASPECT.1),
move |cx, rect| StrokePreview::sized(sample, rect).paint(cx, rect),
)
});
let picked = selection.items.first().copied().filter(|at| Some(*at) != current);
if let Some(at) = picked {
self.set(found, StrokeField::Preset, self.style.with_line_style(PRESETS[at]));
}
}
fn grid(&self, ui: &mut Ui, found: &mut Found) {
let (style, mixed, words) = (self.style, self.mixed, self.words);
let solid_closed = self.closed && !self.caps_on_closed && style.dashes.is_empty() && !mixed.dashes;
let icons = self.sections.contains(StrokeSections::ICONS);
FieldGroup::new(words.title.clone()).show(ui, |ui, _| {
let align = [
(StrokeAlign::Inside, "stroke_align_inside", &words.inside),
(StrokeAlign::Center, "stroke_align_center", &words.center),
(StrokeAlign::Outside, "stroke_align_outside", &words.outside),
];
let cap = [
(LineCap::Butt, "stroke_cap_butt", &words.butt),
(LineCap::Round, "stroke_cap_round", &words.round_cap),
(LineCap::Square, "stroke_cap_square", &words.square),
];
let join = [
(LineJoin::Miter, "stroke_join_miter", &words.miter),
(LineJoin::Round, "stroke_join_round", &words.round_join),
(LineJoin::Bevel, "stroke_join_bevel", &words.bevel),
];
let event = choice(ui, &words.alignment, "align", held(mixed.align, style.align), &align, self.closed, icons);
found.push(StrokeField::Align, event.map(|align| StrokeStyle { align, ..style.clone() }));
let event = choice(ui, &words.caps, "cap", held(mixed.cap, style.cap), &cap, !solid_closed, icons);
found.push(StrokeField::Cap, event.map(|cap| StrokeStyle { cap, ..style.clone() }));
let event = choice(ui, &words.corners, "join", held(mixed.join, style.join), &join, self.corners, icons);
found.push(StrokeField::Join, event.map(|join| StrokeStyle { join, ..style.clone() }));
self.app_rows(ui, found);
let miter = self.corners && (mixed.join || style.join == LineJoin::Miter);
let event = number(
ui,
&words.miter_limit,
"miter-limit",
f64::from(style.miter_limit),
mixed.miter_limit,
wide(StrokeStyle::MITER_LIMIT_RANGE),
UnitKind::Times,
miter,
);
found.push(StrokeField::MiterLimit, event.map(|v| StrokeStyle { miter_limit: v as f32, ..style.clone() }));
if self.sections.contains(StrokeSections::OPACITY) {
let event =
number(ui, &words.opacity, "opacity", f64::from(style.opacity) * 100.0, mixed.opacity, 0.0..=100.0, UnitKind::Percent, true);
found.push(StrokeField::Opacity, event.map(|v| StrokeStyle { opacity: v as f32 / 100.0, ..style.clone() }));
}
});
}
/// The app's rows: each a select of the row's choices with painted previews.
fn app_rows(&self, ui: &mut Ui, found: &mut Found) {
for row in self.rows {
let current = match row.selected {
Some(Mixed::Same(id)) => Mixed::Same(id.to_owned()),
Some(Mixed::Mixed) => Mixed::Mixed,
None => Mixed::Same(String::new()),
};
let entries: Vec<Entry<String>> = row.choices.iter().map(|choice| Entry::new(choice.id.clone(), choice.label.clone())).collect();
let mut picked = None;
Field::new(row.label.clone()).id_salt(("row", row.id.as_str())).show(ui, |ui, _| {
let mut select = Select::new(row.label.clone(), Bind::given(current.clone()), &entries);
if let Some(painter) = &row.paint {
select = select.picture(move |id: &String, cx: &ContentPainterCx, rect: Rect| {
if let Some(choice) = row.choices.iter().find(|choice| &choice.id == id) {
painter.paint(cx, rect, choice);
}
});
}
picked = select.show(ui).value().cloned();
});
if let Some(id) = picked
&& found.choice.is_none()
{
found.choice = Some((row.id.clone(), id));
}
}
}
fn dash_unit(&self) -> UnitKind {
match self.style.dash_unit {
DashUnit::Widths => UnitKind::Times,
DashUnit::Document => UnitKind::Points,
}
}
fn dashes(&self, ui: &mut Ui, found: &mut Found) {
let (style, words) = (self.style, self.words);
let state = if self.mixed.dashes { Mixed::Mixed } else { Mixed::Same(!style.dashes.is_empty()) };
if let Some(on) = Checkbox::new(Bind::given(state)).label(words.dashed_line.clone()).id_salt("dashed").show(ui).value().cloned() {
let preset = if on { LineStyle::Dashed } else { LineStyle::Solid };
self.set(found, StrokeField::Dashed, style.with_line_style(preset));
}
if self.mixed.dashes || style.dashes.is_empty() {
return;
}
let hint = match style.dash_unit {
DashUnit::Widths => &words.dash_widths,
DashUnit::Document => &words.dash_document,
};
Text::new(hint.clone()).role(TypeRole::Caption).shade(Shade::Muted).show(ui);
let count = style.dashes.len().max(DASH_SHOWN).div_ceil(DASH_FIELDS) * DASH_FIELDS;
for (line, first) in (0..count).step_by(DASH_FIELDS).enumerate() {
Grid::new(DASH_COLUMNS).id_salt(("dashes", line)).show(ui, |ui, _| {
for index in first..first + DASH_FIELDS {
let (caption, name) = if index % 2 == 0 { (&words.dash, &words.dash_number) } else { (&words.gap, &words.gap_number) };
let name = Label::from(name.fill(&[("n", &(index / 2 + 1).to_string())]));
Stack::new().id_salt(("dash", index)).show(ui, |ui, _| {
let shown = style.dashes.get(index).map(|length| f64::from(*length));
let out = NumberField::new(name.clone(), Bind::<Option<f64>>::given(shown))
.id_salt("field")
.range(wide(StrokeStyle::DASH_RANGE))
.unit_kind(self.dash_unit())
.enabled(index <= style.dashes.len() && index < StrokeStyle::MAX_DASHES)
.sizing(Sizing::Content)
.show(ui);
let with = |v: Option<f64>| StrokeStyle { dashes: dashes_with(&style.dashes, index, v), ..style.clone() };
found.push(StrokeField::Dash(index), out.edit.events.map(with));
Text::new(caption.clone()).role(TypeRole::Caption).shade(Shade::Muted).show(ui);
});
}
});
}
FieldGroup::new(words.dash_offset.clone()).id_salt("offset").show(ui, |ui, _| {
let range = -f64::from(StrokeStyle::DASH_OFFSET_MAX)..=f64::from(StrokeStyle::DASH_OFFSET_MAX);
let event =
number(ui, &words.dash_offset, "dash-offset", f64::from(style.dash_offset), self.mixed.dash_offset, range, self.dash_unit(), true);
found.push(StrokeField::DashOffset, event.map(|v| StrokeStyle { dash_offset: v as f32, ..style.clone() }));
});
if self.sections.contains(StrokeSections::DASH_ALIGN) {
let align = [(DashAlign::Exact, "dash_align_exact", &words.dash_exact), (DashAlign::Corners, "dash_align_corners", &words.dash_corners)];
let icons = self.sections.contains(StrokeSections::ICONS);
let event = choice(ui, &words.dash_align, "dash-align", held(self.mixed.dash_align, style.dash_align), &align, true, icons);
found.push(StrokeField::DashAlign, event.map(|dash_align| StrokeStyle { dash_align, ..style.clone() }));
}
}
}
/// `dashes` with dash field `index` set to `value`: a number sets that entry, or adds it after the last; a cleared field
/// after the first ends the pattern there; the first field never clears (the "Dashed line" check turns dashes off).
pub(super) fn dashes_with(dashes: &[f32], index: usize, value: Option<f64>) -> Vec<f32> {
let mut next = dashes.to_vec();
match value {
Some(length) if index < next.len() => next[index] = length as f32,
Some(length) if index == next.len() => next.push(length as f32),
None if index > 0 => next.truncate(index),
_ => {}
}
next
}
/// `value` as a field's binding value: mixed while the field is.
fn held<T: Value>(mixed: bool, value: T) -> Mixed<T> {
if mixed { Mixed::Mixed } else { Mixed::Same(value) }
}
fn wide(range: RangeInclusive<f32>) -> RangeInclusive<f64> {
f64::from(*range.start())..=f64::from(*range.end())
}
/// A row of choices as a select, or as a segmented set with icons; the event of a pick. A mixed field lights nothing.
fn choice<T: Value + 'static>(
ui: &mut Ui,
label: &Label,
salt: &'static str,
current: Mixed<T>,
items: &[(T, &'static str, &Label)],
enabled: bool,
icons: bool,
) -> EditEvents<T> {
let entries: Vec<Entry<T>> = items
.iter()
.map(|(item, icon, name)| {
let entry = Entry::new(item.clone(), (*name).clone());
if icons { entry.icon(IconSpec::svg(IconId::new(icon))) } else { entry }
})
.collect();
let mut event = EditEvents::new();
Field::new(label.clone()).id_salt(salt).enabled(enabled).show(ui, |ui, _| {
let bind = Bind::given(current.clone());
event = if icons {
Segmented::new(label.clone(), bind, &entries).show(ui).events
} else {
Select::new(label.clone(), bind, &entries).show(ui).events
};
});
event
}
/// A number row; the event of an edit, in the field's own units. A mixed field shows the mixed state.
#[expect(clippy::too_many_arguments, reason = "one call per field")]
fn number(
ui: &mut Ui,
label: &Label,
salt: &'static str,
value: f64,
mixed: bool,
range: RangeInclusive<f64>,
kind: UnitKind,
enabled: bool,
) -> EditEvents<f64> {
let mut event = EditEvents::new();
Field::new(label.clone()).id_salt(salt).enabled(enabled).show(ui, |ui, _| {
let bind = if mixed { Bind::<Option<f64>>::given(Mixed::Mixed) } else { Bind::<Option<f64>>::given(Some(value)) };
let out = NumberField::new(label.clone(), bind).id_salt("field").range(range.clone()).unit_kind(kind).show(ui);
event = out.edit.events.map(|v| v.unwrap_or(value));
});
event
}