mirror of
https://github.com/storytold/deckcraft.git
synced 2026-12-01 18:23:52 -06:00
format pane: production's gradient stop fields, eight directions, preset headings, dash samples, inch mark
The gradient editor uses StopFields::Transparency (Position, a Transparency slider, Remove stop), shows all eight directions in a wrapping row (the Segmented row was cut after six) and labels its preset grid, as the Pattern grid; the Line section's dashes are one row of drawn line samples; inches show the inch mark (InchSpelling::Mark at the root). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
62ab9ac554
commit
ba30bdead2
@@ -3,11 +3,13 @@
|
||||
//! the pattern editor: a gallery of the preset patterns with foreground and background colors.
|
||||
|
||||
use craft::color_ui::gradient::{ColorStop, GradientStops, StopRows, StopRules};
|
||||
use craft::color_ui::ui::gradient::GradientEditor;
|
||||
use craft::color_ui::ui::gradient::{GradientEditor, StopFields};
|
||||
use craft::i18n::Word;
|
||||
use craft::theme::Srgba;
|
||||
use craft::ui::choice::Segmented;
|
||||
use craft::ui::contract::{Bind, Entry, Props as _, Show as _, ShowContainer as _};
|
||||
use craft::ui::buttons::IconButton;
|
||||
use craft::ui::contract::{Bind, Entry, FieldLayout, Props as _, Show as _, ShowContainer as _, clicked};
|
||||
use craft::ui::layout_components::Row;
|
||||
use craft::ui::runtime::env::Label;
|
||||
use craft::ui::form::Field;
|
||||
use craft::ui::list::ThumbAspect;
|
||||
use craft::ui::pickers::Select;
|
||||
@@ -144,6 +146,25 @@ impl Glyph for DirectionGlyph {
|
||||
}
|
||||
}
|
||||
|
||||
/// The Direction row: a button per direction, the chosen one on, wrapping onto a second line where the pane is narrow;
|
||||
/// returns the index of a direction picked that is not the chosen one.
|
||||
fn directions(ui: &mut Ui, glyphs: &'static [DirectionGlyph], names: &[Label], selected: Option<usize>) -> Option<usize> {
|
||||
let mut field = Field::new(Word::plain("Direction")).show(ui, |ui, _| {
|
||||
let mut row = Row::new().wrap(true).show(ui, |ui, _| {
|
||||
let mut picked = None;
|
||||
for (i, (glyph, name)) in glyphs.iter().zip(names).enumerate() {
|
||||
let button = IconButton::new(IconSpec::glyph(glyph)).label(name.clone()).selected(selected == Some(i)).id_salt(i).show(ui);
|
||||
if clicked(&button) && selected != Some(i) {
|
||||
picked = Some(i);
|
||||
}
|
||||
}
|
||||
picked
|
||||
});
|
||||
row.settle(|_, picked| picked.take()).flatten()
|
||||
});
|
||||
field.settle(|_, picked| picked.take()).flatten()
|
||||
}
|
||||
|
||||
/// The editor for `g`; returns the new gradient when the user changed something.
|
||||
pub(crate) fn gradient_editor(app: &mut crate::SlideApp, ui: &mut Ui, label: &str, g: &Gradient) -> Option<Gradient> {
|
||||
let scheme = app.color_scheme();
|
||||
@@ -190,11 +211,14 @@ pub(crate) fn gradient_editor(app: &mut crate::SlideApp, ui: &mut Ui, label: &st
|
||||
let preset_names =
|
||||
["Color to dark", "Light to color", "Color to shade", "Light through color to shade", "Radial light to shade", "Color to transparent"];
|
||||
let presets: Vec<_> = preset_names.into_iter().enumerate().map(|(i, name)| (i, Word::plain(name).into())).collect();
|
||||
if let Some(i) = preset_grid(ui, "Preset gradients", &presets, None, ThumbAspect::new(17, 11), |cx, rect, i| {
|
||||
let stops: Vec<_> = variants[*i].stops.iter().map(|s| (s.pos as f32, c32(&s.color, sc).into())).collect();
|
||||
cx.checker(rect);
|
||||
cx.ramp(rect, CornerRadius::ZERO, &stops, craft::behavior::Axis::X);
|
||||
}) {
|
||||
let mut grid = Field::new(Word::plain("Preset gradients")).layout(FieldLayout::Above).show(ui, |ui, _| {
|
||||
preset_grid(ui, "Preset gradients", &presets, None, ThumbAspect::new(17, 11), |cx, rect, i| {
|
||||
let stops: Vec<_> = variants[*i].stops.iter().map(|s| (s.pos as f32, c32(&s.color, sc).into())).collect();
|
||||
cx.checker(rect);
|
||||
cx.ramp(rect, CornerRadius::ZERO, &stops, craft::behavior::Axis::X);
|
||||
})
|
||||
});
|
||||
if let Some(i) = grid.settle(|_, picked| picked.take()).flatten() {
|
||||
let mut preset = variants[i].clone();
|
||||
preset.rotate_with_shape = g.rotate_with_shape;
|
||||
out = Some(preset);
|
||||
@@ -224,16 +248,9 @@ pub(crate) fn gradient_editor(app: &mut crate::SlideApp, ui: &mut Ui, label: &st
|
||||
}
|
||||
match &g.shape {
|
||||
GradientShape::Linear { angle, scaled } => {
|
||||
let entries: Vec<_> = DIRECTIONS_LINEAR
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, (_, a))| Entry::new(i, format!("{a}°")).icon(IconSpec::glyph(&LINEAR_ICONS[i])))
|
||||
.collect();
|
||||
let names: Vec<_> = DIRECTIONS_LINEAR.iter().map(|(_, a)| Label::from(format!("{a}°"))).collect();
|
||||
let selected = DIRECTIONS_LINEAR.iter().position(|(_, a)| (angle - a).abs() < 0.5);
|
||||
let mut direction = Field::new(Word::plain("Direction")).show(ui, |ui, _| {
|
||||
Segmented::new(Word::plain("Direction"), Bind::given(selected.unwrap_or(usize::MAX)), &entries).show(ui).events.committed().copied()
|
||||
});
|
||||
if let Some(i) = direction.settle(|_, value| value.take()).flatten() {
|
||||
if let Some(i) = directions(ui, &LINEAR_ICONS, &names, selected) {
|
||||
out = Some(Gradient { shape: GradientShape::Linear { angle: DIRECTIONS_LINEAR[i].1, scaled: *scaled }, ..g.clone() });
|
||||
}
|
||||
let mut field = Field::new(Word::plain("Angle")).show(ui, |ui, _| {
|
||||
@@ -252,16 +269,9 @@ pub(crate) fn gradient_editor(app: &mut crate::SlideApp, ui: &mut Ui, label: &st
|
||||
}
|
||||
}
|
||||
GradientShape::Path { path, focus } => {
|
||||
let entries: Vec<_> = DIRECTIONS_PATH
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, (name, _))| Entry::new(i, Word::plain(name)).icon(IconSpec::glyph(&PATH_ICONS[i])))
|
||||
.collect();
|
||||
let names: Vec<_> = DIRECTIONS_PATH.iter().map(|(name, _)| Label::from(Word::plain(name))).collect();
|
||||
let selected = DIRECTIONS_PATH.iter().position(|(_, f)| focus.iter().zip(f).all(|(a, b)| (a - b).abs() < 1e-3));
|
||||
let mut direction = Field::new(Word::plain("Direction")).show(ui, |ui, _| {
|
||||
Segmented::new(Word::plain("Direction"), Bind::given(selected.unwrap_or(usize::MAX)), &entries).show(ui).events.committed().copied()
|
||||
});
|
||||
if let Some(i) = direction.settle(|_, value| value.take()).flatten() {
|
||||
if let Some(i) = directions(ui, &PATH_ICONS, &names, selected) {
|
||||
out = Some(Gradient { shape: GradientShape::Path { path: path.clone(), focus: DIRECTIONS_PATH[i].1 }, ..g.clone() });
|
||||
}
|
||||
}
|
||||
@@ -272,6 +282,7 @@ pub(crate) fn gradient_editor(app: &mut crate::SlideApp, ui: &mut Ui, label: &st
|
||||
.rules(StopRules::new(StopRows::Combined).stops(2, 10))
|
||||
.midpoints(false)
|
||||
.fields(true)
|
||||
.stop_fields(StopFields::Transparency)
|
||||
.show(ui);
|
||||
if let Some(edited) = editor.edit.events.committed() {
|
||||
out = Some(from_stops(edited, g));
|
||||
@@ -436,11 +447,12 @@ pub(crate) fn pattern_editor(app: &mut crate::SlideApp, ui: &mut Ui, p: &Pattern
|
||||
let mut out: Option<PatternFill> = None;
|
||||
let (fg, bg) = (c32(&pt.fg, sc), c32(&pt.bg, sc));
|
||||
let presets: Vec<_> = pattern_gallery().map(|name| (name.to_owned(), Word::plain(pattern_label(name)).into())).collect();
|
||||
if let Some(preset) = preset_grid(ui, "Pattern", &presets, Some(pt.preset.clone()), ThumbAspect::SQUARE, |cx, rect, name| {
|
||||
paint_pattern(&cx.painter, rect, name, fg, bg);
|
||||
})
|
||||
.filter(|preset| preset != &pt.preset)
|
||||
{
|
||||
let mut grid = Field::new(Word::plain("Pattern")).layout(FieldLayout::Above).show(ui, |ui, _| {
|
||||
preset_grid(ui, "Pattern", &presets, Some(pt.preset.clone()), ThumbAspect::SQUARE, |cx, rect, name| {
|
||||
paint_pattern(&cx.painter, rect, name, fg, bg);
|
||||
})
|
||||
});
|
||||
if let Some(preset) = grid.settle(|_, picked| picked.take()).flatten().filter(|preset| preset != &pt.preset) {
|
||||
out = Some(PatternFill { preset, ..pt.clone() });
|
||||
}
|
||||
for (label, foreground) in [("Foreground", true), ("Background", false)] {
|
||||
|
||||
@@ -5,7 +5,7 @@ mod size;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use craft::ui::buttons::Button;
|
||||
use craft::ui::buttons::{Button, IconButton};
|
||||
use craft::ui::choice::{Checkbox, Radio, RadioGroup, Segmented};
|
||||
use craft::ui::contract::{Bind, Entry, FieldLayout, Props as _, Show as _, ShowContainer as _};
|
||||
use craft::ui::display::{Divider, Shade, Text};
|
||||
@@ -137,6 +137,68 @@ fn choice_buttons(ui: &mut Ui, label: &'static str, current: &str, items: &[(&'s
|
||||
field.settle(|_, picked| picked.take()).flatten().filter(|picked| picked != current)
|
||||
}
|
||||
|
||||
/// The dash presets the Line section offers: name, preset, and the sample's dash and gap lengths as fractions of the
|
||||
/// sample's width, so each reads at icon size (an empty pattern draws solid).
|
||||
const DASHES: [(&str, &str, &[f32]); 5] = [
|
||||
("Solid", "solid", &[]),
|
||||
("Round dot", "sysDot", &[0.08, 0.13]),
|
||||
("Dash", "dash", &[0.25, 0.12]),
|
||||
("Dash dot", "dashDot", &[0.3, 0.12, 0.08, 0.12]),
|
||||
("Long dash", "lgDash", &[0.44, 0.12]),
|
||||
];
|
||||
|
||||
/// A dash preset's sample: a line across the icon box in the preset's dashes.
|
||||
struct DashSample(usize);
|
||||
|
||||
impl craft::ui::contract::MemoProps for DashSample {
|
||||
fn key(&self, key: &mut craft::ui::contract::MemoKey) {
|
||||
key.feed_hash(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
impl craft::ui::primitives::Glyph for DashSample {
|
||||
fn paint(&self, painter: &egui::Painter, tokens: &craft::ui::style::UiTokens, rect: egui::Rect, color: egui::Color32, _: bool) {
|
||||
let width = tokens.stroke.medium;
|
||||
let stroke = egui::Stroke::new(width, color);
|
||||
let (left, right, y) = (rect.left(), rect.right(), rect.center().y);
|
||||
let pattern = DASHES[self.0].2;
|
||||
if pattern.is_empty() {
|
||||
painter.line_segment([egui::pos2(left, y), egui::pos2(right, y)], stroke);
|
||||
return;
|
||||
}
|
||||
let mut x = left;
|
||||
for (i, length) in pattern.iter().cycle().enumerate() {
|
||||
if x >= right {
|
||||
break;
|
||||
}
|
||||
let end = (x + length * rect.width()).min(right);
|
||||
if i % 2 == 0 {
|
||||
painter.line_segment([egui::pos2(x, y), egui::pos2(end, y)], stroke);
|
||||
}
|
||||
x = end;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static DASH_ICONS: [DashSample; 5] = [DashSample(0), DashSample(1), DashSample(2), DashSample(3), DashSample(4)];
|
||||
|
||||
/// The Dash row: one line sample per preset, in one row; returns the picked preset when it differs from `current`.
|
||||
fn dash_samples(ui: &mut Ui, current: &str) -> Option<String> {
|
||||
let mut field = Field::new(Word::plain("Dash")).layout(FieldLayout::Beside).id_salt("Dash").show(ui, |ui, _| {
|
||||
let mut row = Row::new().show(ui, |ui, _| {
|
||||
let mut picked = None;
|
||||
for (sample, (name, preset, _)) in DASH_ICONS.iter().zip(DASHES) {
|
||||
if craft::ui::contract::clicked(&IconButton::new(IconSpec::glyph(sample)).label(Word::plain(name)).id_salt(preset).show(ui)) {
|
||||
picked = Some(preset.to_owned());
|
||||
}
|
||||
}
|
||||
picked
|
||||
});
|
||||
row.settle(|_, picked| picked.take()).flatten()
|
||||
});
|
||||
field.settle(|_, picked| picked.take()).flatten().filter(|picked| picked != current)
|
||||
}
|
||||
|
||||
fn radios(ui: &mut Ui, label: &'static str, current: &str, items: &[(&'static str, &str)]) -> Option<String> {
|
||||
let mut group = RadioGroup::new(Word::plain(label), Bind::given(current.to_owned())).id_salt(label).show(ui, |ui, _| {
|
||||
Stack::new().show(ui, |ui, _| {
|
||||
@@ -283,9 +345,7 @@ fn line_options(app: &mut SlideApp, ui: &mut Ui, sh: &Shape) {
|
||||
Some(deckcraft_model::style::Dash::Custom { .. }) => "custom",
|
||||
None => "solid",
|
||||
};
|
||||
if let Some(value) =
|
||||
choice_buttons(ui, "Dash", dash, &[("—", "solid"), ("···", "sysDot"), ("- -", "dash"), ("-·-", "dashDot"), ("— —", "lgDash")])
|
||||
{
|
||||
if let Some(value) = dash_samples(ui, dash) {
|
||||
run(app, "shape.line", json!({"dash": value}));
|
||||
}
|
||||
line_details(app, ui, sh);
|
||||
|
||||
@@ -145,8 +145,10 @@ fn fill_color_and_transparency_show_only_where_they_change_the_fill() -> TestRes
|
||||
h.run_steps(2);
|
||||
}
|
||||
assert_eq!(color_rows(&h) > line_colors, color, "{kind}: fill colour row");
|
||||
assert_eq!(h.query_by_role_and_label(Role::Slider, "Transparency").is_some(), slider, "{kind}: fill transparency");
|
||||
assert_eq!(h.query_by_role_and_label(Role::SpinButton, "Opacity").is_some(), gradient, "{kind}: stop opacity");
|
||||
// A gradient's selected stop has a Transparency slider of its own, beside its Position.
|
||||
let sliders = h.query_all_by_role_and_label(Role::Slider, "Transparency").count();
|
||||
assert_eq!(sliders, usize::from(slider) + usize::from(gradient), "{kind}: fill and stop transparency");
|
||||
assert_eq!(h.query_by_role_and_label(Role::SpinButton, "Position").is_some(), gradient, "{kind}: stop position");
|
||||
assert_eq!(h.query_by_role_and_label(Role::Group, "Gradient").is_some(), gradient, "{kind}: gradient editor");
|
||||
assert_eq!(h.query_by_role_and_label(Role::Button, "Foreground color").is_some(), pattern, "{kind}: pattern colour row");
|
||||
let tile_name = crate::fillui::pattern_label("zigZag");
|
||||
@@ -255,3 +257,25 @@ fn slide_background_fill_is_a_fill_option() -> TestResult {
|
||||
assert_eq!(color_rows(&h), color_rows(&line_only));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A dash sample sets its preset on the line, and a gradient direction button sets the angle it names; the chosen
|
||||
/// direction reads as pressed.
|
||||
#[test]
|
||||
fn dash_samples_and_direction_buttons_set_what_they_show() -> TestResult {
|
||||
let mut h = fill_pane(Some(json!({"gradient": {"stops": [[0, "accent1"], [1, "accent2"]], "angle": 90}})))?;
|
||||
h.get_by_role_and_label(Role::Button, "Round dot").click();
|
||||
h.run_steps(2);
|
||||
let shape = h.state().session.active().ok_or("no active document")?.selected_shapes().first().map(|s| (*s).clone()).ok_or("no selected shape")?;
|
||||
let dash = shape.line.and_then(|line| line.dash);
|
||||
assert_eq!(dash, Some(deckcraft_model::style::Dash::Preset { name: "sysDot".into() }), "the Round dot sample sets sysDot");
|
||||
assert_eq!(h.get_by_label("90°").accesskit_node().toggled(), Some(Toggled::True), "the chosen direction is pressed");
|
||||
h.get_by_label("180°").click();
|
||||
h.run_steps(2);
|
||||
let Some(Fill::Gradient(gradient)) = selected_fill(&h)? else { return Err("the fill is no longer a gradient".into()) };
|
||||
let angle = match gradient.shape {
|
||||
deckcraft_model::style::GradientShape::Linear { angle, .. } => angle,
|
||||
deckcraft_model::style::GradientShape::Path { .. } => return Err("the gradient is no longer linear".into()),
|
||||
};
|
||||
assert!((angle - 180.0).abs() < 0.5, "the 180° button turns the gradient to 180°: {angle}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -273,10 +273,15 @@ pub fn app_sizes() -> std::sync::Arc<craft::theme::AppSizes> {
|
||||
SIZES.clone()
|
||||
}
|
||||
|
||||
/// The app's root values before the frame's own: [`RootEnv`](craft::ui::runtime::env::RootEnv) with DeckCraft's sizes.
|
||||
/// The app's root values before the frame's own: [`RootEnv`](craft::ui::runtime::env::RootEnv) with DeckCraft's sizes,
|
||||
/// and number fields that write their number and unit as one run at their start, to two decimals ("1.39″", "0.75 pt")
|
||||
/// and whole percents, as the old app's fields did.
|
||||
pub fn root_env() -> craft::ui::runtime::env::RootEnv {
|
||||
let mut root = craft::ui::runtime::env::RootEnv::new();
|
||||
root.sizes = app_sizes();
|
||||
root.values.set(craft::ui::text::NumberLook::Inline);
|
||||
root.values.set(craft::ui::text::NumberPrecision::Hundredths);
|
||||
root.values.set(craft::ui::runtime::env::InchSpelling::Mark);
|
||||
root
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user