diff --git a/egui/src/containers/collapsing_header.rs b/egui/src/containers/collapsing_header.rs index 978d1706..b1fe66be 100644 --- a/egui/src/containers/collapsing_header.rs +++ b/egui/src/containers/collapsing_header.rs @@ -122,12 +122,7 @@ pub fn paint_icon(ui: &mut Ui, openness: f32, response: &Response) { *p = rect.center() + v; } - ui.painter().add(PaintCmd::Path { - points, - closed: true, - fill: Default::default(), - stroke, - }); + ui.painter().add(PaintCmd::closed_line(points, stroke)); } /// A header which can be collapsed/expanded, revealing a contained `Ui` region. diff --git a/egui/src/containers/combo_box.rs b/egui/src/containers/combo_box.rs index 3be25f0a..b9f608a8 100644 --- a/egui/src/containers/combo_box.rs +++ b/egui/src/containers/combo_box.rs @@ -116,10 +116,8 @@ fn paint_icon(painter: &Painter, rect: Rect, visuals: &WidgetVisuals) { rect.center(), vec2(rect.width() * 0.7, rect.height() * 0.45), ); - painter.add(PaintCmd::Path { - points: vec![rect.left_top(), rect.right_top(), rect.center_bottom()], - closed: true, - fill: Default::default(), - stroke: visuals.fg_stroke, - }); + painter.add(PaintCmd::closed_line( + vec![rect.left_top(), rect.right_top(), rect.center_bottom()], + visuals.fg_stroke, + )); } diff --git a/egui/src/containers/window.rs b/egui/src/containers/window.rs index 67876709..cccc3ce9 100644 --- a/egui/src/containers/window.rs +++ b/egui/src/containers/window.rs @@ -583,12 +583,7 @@ fn paint_frame_interaction( points.push(pos2(max.x, min.y + cr)); points.push(pos2(max.x, max.y - cr)); } - ui.painter().add(PaintCmd::Path { - points, - closed: false, - fill: Default::default(), - stroke: visuals.bg_stroke, - }); + ui.painter().add(PaintCmd::line(points, visuals.bg_stroke)); } // ---------------------------------------------------------------------------- diff --git a/egui/src/demos/demo_window.rs b/egui/src/demos/demo_window.rs index 917bd772..c167dab4 100644 --- a/egui/src/demos/demo_window.rs +++ b/egui/src/demos/demo_window.rs @@ -318,12 +318,7 @@ impl Painting { for line in &self.lines { if line.len() >= 2 { let points: Vec = line.iter().map(|p| rect.min + *p).collect(); - painter.add(paint::PaintCmd::Path { - points, - closed: false, - stroke: self.stroke, - fill: Default::default(), - }); + painter.add(paint::PaintCmd::line(points, self.stroke)); } } } diff --git a/egui/src/paint/command.rs b/egui/src/paint/command.rs index 4c3956b1..859b60e9 100644 --- a/egui/src/paint/command.rs +++ b/egui/src/paint/command.rs @@ -46,6 +46,7 @@ pub enum PaintCmd { Triangles(Triangles), } +/// ## Constructors impl PaintCmd { pub fn line_segment(points: [Pos2; 2], stroke: impl Into) -> Self { Self::LineSegment { @@ -54,6 +55,33 @@ impl PaintCmd { } } + pub fn line(points: Vec, stroke: impl Into) -> Self { + Self::Path { + points, + closed: false, + fill: Default::default(), + stroke: stroke.into(), + } + } + + pub fn closed_line(points: Vec, stroke: impl Into) -> Self { + Self::Path { + points, + closed: true, + fill: Default::default(), + stroke: stroke.into(), + } + } + + pub fn polygon(points: Vec, fill: impl Into, stroke: impl Into) -> Self { + Self::Path { + points, + closed: true, + fill: fill.into(), + stroke: stroke.into(), + } + } + pub fn circle_filled(center: Pos2, radius: f32, fill_color: impl Into) -> Self { Self::Circle { center, @@ -108,7 +136,10 @@ impl PaintCmd { color, } } +} +/// ## Operations +impl PaintCmd { pub fn triangles(triangles: Triangles) -> Self { debug_assert!(triangles.is_valid()); Self::Triangles(triangles) diff --git a/egui/src/widgets/color_picker.rs b/egui/src/widgets/color_picker.rs index 234428d4..698ce17f 100644 --- a/egui/src/widgets/color_picker.rs +++ b/egui/src/widgets/color_picker.rs @@ -115,16 +115,15 @@ fn color_slider_1d(ui: &mut Ui, value: &mut f32, color_at: impl Fn(f32) -> Srgba let x = lerp(rect.left()..=rect.right(), *value); let r = rect.height() / 4.0; let picked_color = color_at(*value); - ui.painter().add(PaintCmd::Path { - points: vec![ + ui.painter().add(PaintCmd::polygon( + vec![ pos2(x - r, rect.bottom()), pos2(x + r, rect.bottom()), pos2(x, rect.center().y), ], - closed: true, - fill: picked_color, - stroke: Stroke::new(visuals.fg_stroke.width, contrast_color(picked_color)), - }); + picked_color, + Stroke::new(visuals.fg_stroke.width, contrast_color(picked_color)), + )); } response diff --git a/egui/src/widgets/mod.rs b/egui/src/widgets/mod.rs index d83adba0..a592766e 100644 --- a/egui/src/widgets/mod.rs +++ b/egui/src/widgets/mod.rs @@ -411,16 +411,15 @@ impl<'a> Widget for Checkbox<'a> { }); if *checked { - ui.painter().add(PaintCmd::Path { - points: vec![ + // Check mark: + ui.painter().add(PaintCmd::line( + vec![ pos2(small_icon_rect.left(), small_icon_rect.center().y), pos2(small_icon_rect.center().x, small_icon_rect.bottom()), pos2(small_icon_rect.right(), small_icon_rect.top()), ], - closed: false, - fill: Default::default(), - stroke: visuals.fg_stroke, - }); + visuals.fg_stroke, + )); } let text_color = text_color