mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 19:47:53 +00:00
draw: StrokeEditor::caps_on_closed keeps the Caps row enabled on a closed path without dashes, and with the default of false a solid closed path still disables Caps
This commit is contained in:
@@ -71,7 +71,7 @@ impl Outcome for StrokeEditorOutput {
|
||||
/// 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); Corners (Miter, Round, Bevel; enabled only with
|
||||
/// 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".
|
||||
@@ -96,6 +96,7 @@ pub struct StrokeEditor<'a> {
|
||||
value: Bind<'a, StrokeStyle>,
|
||||
sections: StrokeSections,
|
||||
closed: bool,
|
||||
caps_on_closed: bool,
|
||||
corners: bool,
|
||||
mixed: StrokeMixed,
|
||||
rows: &'a [StrokeChoiceRow<'a>],
|
||||
@@ -109,6 +110,7 @@ impl<'a> StrokeEditor<'a> {
|
||||
value,
|
||||
sections: StrokeSections::default(),
|
||||
closed: true,
|
||||
caps_on_closed: false,
|
||||
corners: true,
|
||||
mixed: StrokeMixed::default(),
|
||||
rows: &[],
|
||||
@@ -121,13 +123,20 @@ impl<'a> StrokeEditor<'a> {
|
||||
self
|
||||
}
|
||||
|
||||
/// Whether the stroked path is closed: alignment applies and caps need dashes.
|
||||
/// True by default.
|
||||
/// 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;
|
||||
@@ -148,7 +157,7 @@ impl<'a> StrokeEditor<'a> {
|
||||
|
||||
/// Shows the editor.
|
||||
pub fn show(self, ui: &mut Ui) -> StrokeEditorOutput {
|
||||
let StrokeEditor { label, mut value, sections, closed, corners, mixed, rows } = self;
|
||||
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();
|
||||
@@ -164,7 +173,7 @@ impl<'a> StrokeEditor<'a> {
|
||||
Some((base, draft)) if base == held => draft,
|
||||
_ => held.clone(),
|
||||
};
|
||||
let body = Body { words: &words, style: &style, sections, closed, corners, mixed, rows, name: name.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 {
|
||||
@@ -251,6 +260,7 @@ struct Body<'a> {
|
||||
style: &'a StrokeStyle,
|
||||
sections: StrokeSections,
|
||||
closed: bool,
|
||||
caps_on_closed: bool,
|
||||
corners: bool,
|
||||
mixed: StrokeMixed,
|
||||
rows: &'a [StrokeChoiceRow<'a>],
|
||||
@@ -325,7 +335,7 @@ impl Body<'_> {
|
||||
|
||||
fn grid(&self, ui: &mut Ui, found: &mut Found) {
|
||||
let (style, mixed, words) = (self.style, self.mixed, self.words);
|
||||
let solid_closed = self.closed && style.dashes.is_empty() && !mixed.dashes;
|
||||
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 = [
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
//! The stroke editor's dash fields: typing in the blank after the pattern adds an entry, clearing a later field ends
|
||||
//! the pattern there, and the first field never clears.
|
||||
//! the pattern there, and the first field never clears. Its Caps row on a solid closed path: disabled by default,
|
||||
//! enabled with `caps_on_closed`.
|
||||
|
||||
use craft_draw::ui::stroke::{StrokeEditor, StrokeSections, StrokeStyle};
|
||||
use craft_draw::ui::stroke::{LineCap, StrokeEditor, StrokeSections, StrokeStyle};
|
||||
use craft_ui::contract::Bind;
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use egui::{Key, Modifiers};
|
||||
@@ -34,6 +35,39 @@ fn type_into(fixture: &mut Fixture, label: &str, text: &str) {
|
||||
fixture.idle(3);
|
||||
}
|
||||
|
||||
/// The panel editor over a solid closed path, with `caps_on_closed` as given, and the style it writes through to.
|
||||
fn closed_editor(caps_on_closed: bool) -> (Fixture, Rc<RefCell<StrokeStyle>>) {
|
||||
let style = Rc::new(RefCell::new(StrokeStyle::default()));
|
||||
let shown = Rc::clone(&style);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut style = shown.borrow_mut();
|
||||
let _ = StrokeEditor::new("Stroke", Bind::bound(&mut *style))
|
||||
.sections(StrokeSections::PANEL)
|
||||
.closed(true)
|
||||
.caps_on_closed(caps_on_closed)
|
||||
.show(ui);
|
||||
});
|
||||
fixture.settle();
|
||||
(fixture, style)
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn caps_on_closed_keeps_the_cap_buttons_enabled_on_a_solid_closed_path() {
|
||||
let (mut fixture, style) = closed_editor(false);
|
||||
let square = fixture.by_label("Square").expect("the editor shows Square");
|
||||
assert!(fixture.tree().is_disabled_key(square.node), "by default Caps is disabled for a solid closed path");
|
||||
fixture.click(&square);
|
||||
fixture.idle(3);
|
||||
assert_ne!(style.borrow().cap, LineCap::Square, "a disabled cap button does not change the cap");
|
||||
|
||||
let (mut fixture, style) = closed_editor(true);
|
||||
let square = fixture.by_label("Square").expect("the editor shows Square");
|
||||
assert!(!fixture.tree().is_disabled_key(square.node), "with caps_on_closed Caps is enabled");
|
||||
fixture.click(&square);
|
||||
fixture.idle(3);
|
||||
assert_eq!(style.borrow().cap, LineCap::Square, "the cap button sets the cap");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn dash_fields_add_after_the_pattern_and_a_cleared_field_ends_it() {
|
||||
let (mut fixture, style) = editor(vec![4.0, 2.0]);
|
||||
|
||||
Reference in New Issue
Block a user