Use smaller quads to draw borders

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2023-12-21 17:54:13 +01:00
parent 67f76468f1
commit c03c0d50d5
3 changed files with 78 additions and 4 deletions

View file

@ -1590,6 +1590,15 @@ impl Edges<Pixels> {
left: self.left.scale(factor),
}
}
/// Returns the maximum value of any edge.
///
/// # Returns
///
/// The maximum `Pixels` value among all four edges.
pub fn max(&self) -> Pixels {
self.top.max(self.right).max(self.bottom).max(self.left)
}
}
impl Into<Edges<Pixels>> for f32 {
@ -1740,6 +1749,18 @@ impl Corners<Pixels> {
bottom_left: self.bottom_left.scale(factor),
}
}
/// Returns the maximum value of any corner.
///
/// # Returns
///
/// The maximum `Pixels` value among all four corners.
pub fn max(&self) -> Pixels {
self.top_left
.max(self.top_right)
.max(self.bottom_right)
.max(self.bottom_left)
}
}
impl<T: Clone + Default + Debug> Corners<T> {

View file

@ -402,13 +402,65 @@ impl Style {
if self.is_border_visible() {
cx.with_z_index(3, |cx| {
cx.paint_quad(quad(
let corner_radii = self.corner_radii.to_pixels(bounds.size, rem_size);
let border_widths = self.border_widths.to_pixels(rem_size);
let max_border_width = border_widths.max();
let max_corner_radius = corner_radii.max();
let top_bounds = Bounds::from_corners(
bounds.origin,
bounds.upper_right()
+ point(Pixels::ZERO, max_border_width.max(max_corner_radius)),
);
let bottom_bounds = Bounds::from_corners(
bounds.lower_left()
- point(Pixels::ZERO, max_border_width.max(max_corner_radius)),
bounds.lower_right(),
);
let left_bounds = Bounds::from_corners(
top_bounds.lower_left(),
bottom_bounds.origin + point(max_border_width, Pixels::ZERO),
);
let right_bounds = Bounds::from_corners(
top_bounds.lower_right() - point(max_border_width, Pixels::ZERO),
bottom_bounds.upper_right(),
);
let quad = quad(
bounds,
self.corner_radii.to_pixels(bounds.size, rem_size),
corner_radii,
Hsla::transparent_black(),
self.border_widths.to_pixels(rem_size),
border_widths,
self.border_color.unwrap_or_default(),
));
);
cx.with_content_mask(Some(ContentMask { bounds: top_bounds }), |cx| {
cx.paint_quad(quad.clone());
});
cx.with_content_mask(
Some(ContentMask {
bounds: right_bounds,
}),
|cx| {
cx.paint_quad(quad.clone());
},
);
cx.with_content_mask(
Some(ContentMask {
bounds: bottom_bounds,
}),
|cx| {
cx.paint_quad(quad.clone());
},
);
cx.with_content_mask(
Some(ContentMask {
bounds: left_bounds,
}),
|cx| {
cx.paint_quad(quad);
},
);
});
}

View file

@ -3074,6 +3074,7 @@ impl From<(&'static str, u64)> for ElementId {
}
/// A rectangle, to be rendered on the screen by GPUI at the given position and size.
#[derive(Clone)]
pub struct PaintQuad {
bounds: Bounds<Pixels>,
corner_radii: Corners<Pixels>,