diff --git a/CHANGELOG.md b/CHANGELOG.md index f6a03b2d..2f1f1543 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w * Added `Plot::allow_scroll`, `Plot::allow_zoom` no longer affects scrolling ([#1382](https://github.com/emilk/egui/pull/1382)). * Added `Ui::push_id` to resolve id clashes ([#1374](https://github.com/emilk/egui/pull/1374)). * Added `Frame::outer_margin`. +* Added `Painter::hline` and `Painter::vline`. ### Changed 🔧 * `ClippedMesh` has been replaced with `ClippedPrimitive` ([#1351](https://github.com/emilk/egui/pull/1351)). diff --git a/egui/src/containers/panel.rs b/egui/src/containers/panel.rs index dbadf5a0..1239a900 100644 --- a/egui/src/containers/panel.rs +++ b/egui/src/containers/panel.rs @@ -273,11 +273,9 @@ impl SidePanel { // draw on top of ALL panels so that the resize line won't be covered by subsequent panels let resize_layer = LayerId::new(Order::Foreground, Id::new("panel_resize")); let resize_x = side.opposite().side_x(rect); - let top = pos2(resize_x, rect.top()); - let bottom = pos2(resize_x, rect.bottom()); ui.ctx() .layer_painter(resize_layer) - .line_segment([top, bottom], stroke); + .vline(resize_x, rect.y_range(), stroke); } inner_response @@ -560,11 +558,9 @@ impl TopBottomPanel { // draw on top of ALL panels so that the resize line won't be covered by subsequent panels let resize_layer = LayerId::new(Order::Foreground, Id::new("panel_resize")); let resize_y = side.opposite().side_y(rect); - let left = pos2(rect.left(), resize_y); - let right = pos2(rect.right(), resize_y); ui.ctx() .layer_painter(resize_layer) - .line_segment([left, right], stroke); + .hline(rect.x_range(), resize_y, stroke); } inner_response diff --git a/egui/src/containers/window.rs b/egui/src/containers/window.rs index b059d432..1d419665 100644 --- a/egui/src/containers/window.rs +++ b/egui/src/containers/window.rs @@ -882,14 +882,10 @@ impl TitleBar { if let Some(content_response) = &content_response { // paint separator between title and content: - let left = outer_rect.left(); - let right = outer_rect.right(); let y = content_response.rect.top() + ui.spacing().item_spacing.y * 0.5; // let y = lerp(self.rect.bottom()..=content_response.rect.top(), 0.5); - ui.painter().line_segment( - [pos2(left, y), pos2(right, y)], - ui.visuals().widgets.noninteractive.bg_stroke, - ); + let stroke = ui.visuals().widgets.noninteractive.bg_stroke; + ui.painter().hline(outer_rect.x_range(), y, stroke); } if ui diff --git a/egui/src/painter.rs b/egui/src/painter.rs index 5605ddf2..f4ded9e5 100644 --- a/egui/src/painter.rs +++ b/egui/src/painter.rs @@ -1,5 +1,7 @@ +use std::ops::RangeInclusive; + use crate::{ - emath::{Align2, Pos2, Rect, Vec2}, + emath::{pos2, Align2, Pos2, Rect, Vec2}, layers::{LayerId, PaintList, ShapeIdx}, Color32, Context, FontId, }; @@ -233,8 +235,7 @@ impl Painter { /// # Paint different primitives impl Painter { - /// Paints the line from the first point to the second using the `stroke` - /// for outlining shape. + /// Paints a line from the first point to the second. pub fn line_segment(&self, points: [Pos2; 2], stroke: impl Into) { self.add(Shape::LineSegment { points, @@ -242,6 +243,22 @@ impl Painter { }); } + /// Paints a horizontal line. + pub fn hline(&self, x: RangeInclusive, y: f32, stroke: impl Into) { + self.add(Shape::LineSegment { + points: [pos2(*x.start(), y), pos2(*x.end(), y)], + stroke: stroke.into(), + }); + } + + /// Paints a vertical line. + pub fn vline(&self, x: f32, y: RangeInclusive, stroke: impl Into) { + self.add(Shape::LineSegment { + points: [pos2(x, *y.start()), pos2(x, *y.end())], + stroke: stroke.into(), + }); + } + pub fn circle( &self, center: Pos2, diff --git a/egui/src/widgets/separator.rs b/egui/src/widgets/separator.rs index b3db250e..8a9588fd 100644 --- a/egui/src/widgets/separator.rs +++ b/egui/src/widgets/separator.rs @@ -71,19 +71,12 @@ impl Widget for Separator { let (rect, response) = ui.allocate_at_least(size, Sense::hover()); if ui.is_rect_visible(response.rect) { - let points = if is_horizontal_line { - [ - pos2(rect.left(), rect.center().y), - pos2(rect.right(), rect.center().y), - ] - } else { - [ - pos2(rect.center().x, rect.top()), - pos2(rect.center().x, rect.bottom()), - ] - }; let stroke = ui.visuals().widgets.noninteractive.bg_stroke; - ui.painter().line_segment(points, stroke); + if is_horizontal_line { + ui.painter().hline(rect.x_range(), rect.center().y, stroke); + } else { + ui.painter().vline(rect.center().x, rect.y_range(), stroke); + } } response