use iced::Color;
use iced_nodegraph_sdf::Pattern;
use super::ColorQuad;
use super::EdgeCurve;
#[derive(Debug, Clone, PartialEq)]
pub struct EdgeStyle {
pub stroke_color: ColorQuad,
pub pattern: Pattern,
pub stroke_outline_width: f32,
pub stroke_outline_color: ColorQuad,
pub border_color: ColorQuad,
pub border_width: f32,
pub border_gap: f32,
pub border_outline_width: f32,
pub border_outline_color: ColorQuad,
pub border_background: ColorQuad,
pub shadow_color: ColorQuad,
pub shadow_expand: f32,
pub shadow_blur: f32,
pub shadow_offset: (f32, f32),
pub curve: EdgeCurve,
}
impl EdgeStyle {
fn stroke(color: ColorQuad, pattern: Pattern) -> Self {
let none = ColorQuad::solid(Color::TRANSPARENT);
Self {
stroke_color: color,
pattern,
stroke_outline_width: 0.0,
stroke_outline_color: none,
border_color: none,
border_width: 0.0,
border_gap: 0.5,
border_outline_width: 0.0,
border_outline_color: none,
border_background: none,
shadow_color: none,
shadow_expand: 0.0,
shadow_blur: 0.0,
shadow_offset: (0.0, 0.0),
curve: EdgeCurve::BezierCubic,
}
}
pub fn data_flow() -> Self {
Self::stroke(
ColorQuad::solid(Color::from_rgb(0.3, 0.6, 1.0)),
Pattern::solid(2.5),
)
}
pub fn error() -> Self {
let red = Color::from_rgb(0.9, 0.2, 0.2);
let mut s = Self::stroke(
ColorQuad::solid(red),
Pattern::dashed(2.0, 6.0, 4.0).flow(30.0),
);
s.border_color = ColorQuad::solid(red);
s.border_width = 1.0;
s.border_gap = 0.5;
s
}
pub fn disabled() -> Self {
Self::stroke(
ColorQuad::solid(Color::from_rgb(0.5, 0.5, 0.5)),
Pattern::dashed(1.5, 12.0, 6.0),
)
}
pub fn highlighted() -> Self {
let yellow = Color::from_rgb(1.0, 0.8, 0.2);
let mut s = Self::stroke(ColorQuad::solid(yellow), Pattern::solid(3.0));
s.border_color = ColorQuad::solid(Color::from_rgba(1.0, 1.0, 1.0, 0.3));
s.border_width = 2.0;
s.border_gap = 1.0;
s
}
pub fn debug() -> Self {
let mut s = Self::stroke(
ColorQuad::solid(Color::from_rgb(0.0, 1.0, 1.0)),
Pattern::dotted(8.0, 2.0),
);
s.curve = EdgeCurve::Line;
s
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn struct_update_overrides_over_default() {
use crate::style::{EdgeStatus, default_edge_style};
let base = default_edge_style(&iced::Theme::Dark, EdgeStatus::Idle);
let style = EdgeStyle {
border_width: 2.0,
curve: EdgeCurve::Line,
..base
};
assert_eq!(style.border_width, 2.0); assert_eq!(style.curve, EdgeCurve::Line); assert_eq!(style.pattern, Pattern::solid(2.0)); }
#[test]
fn sdf_layers_preserves_stroke_pattern() {
let mut s = EdgeStyle::data_flow();
s.pattern = Pattern::dashed(2.0, 12.0, 6.0);
let layers = s.sdf_layers();
let stroke = &layers[0]; let pat = stroke.style.pattern.expect("stroke lost its pattern");
assert!(
matches!(
pat.pattern_type,
iced_nodegraph_sdf::pattern::PatternType::Dashed { .. }
),
"stroke pattern is not Dashed: {:?}",
pat.pattern_type
);
}
}