Files
oak-gpui/crates/ui/src/utils/corner_solver.rs
T
Nate Butler 8c03934b26 welcome: Theme preview tile (#29689)
![CleanShot 2025-04-30 at 13 26
44@2x](https://github.com/user-attachments/assets/f68fefe2-84a1-48b7-b9a2-47c2547cd06b)


- Adds the ThemePreviewTile component, used for upcoming onboarding UI
- Adds the CornerSolver utility for resolving correct nested corner
radii

Release Notes:

- N/A
2025-04-30 17:46:11 +00:00

62 lines
2.0 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
use gpui::Pixels;
/// Calculates the childs content-corner radius for a single nested level.
///
/// child_content_radius = max(0, parent_radius - parent_border - parent_padding + self_border)
///
/// - parent_radius: outer corner radius of the parent element
/// - parent_border: border width of the parent element
/// - parent_padding: padding of the parent element
/// - self_border: border width of this child element (for content inset)
pub fn inner_corner_radius(
parent_radius: Pixels,
parent_border: Pixels,
parent_padding: Pixels,
self_border: Pixels,
) -> Pixels {
(parent_radius - parent_border - parent_padding + self_border).max(Pixels::ZERO)
}
/// Solver for arbitrarily deep nested corner radii.
///
/// Each nested levels outer border-box radius is:
/// R₀ = max(0, root_radius - root_border - root_padding)
/// Rᵢ = max(0, Rᵢ₋₁ - childᵢ₋₁_border - childᵢ₋₁_padding) for i > 0
pub struct CornerSolver {
root_radius: Pixels,
root_border: Pixels,
root_padding: Pixels,
children: Vec<(Pixels, Pixels)>, // (border, padding)
}
impl CornerSolver {
pub fn new(root_radius: Pixels, root_border: Pixels, root_padding: Pixels) -> Self {
Self {
root_radius,
root_border,
root_padding,
children: Vec::new(),
}
}
pub fn add_child(mut self, border: Pixels, padding: Pixels) -> Self {
self.children.push((border, padding));
self
}
pub fn corner_radius(&self, level: usize) -> Pixels {
if level == 0 {
return (self.root_radius - self.root_border - self.root_padding).max(Pixels::ZERO);
}
if level >= self.children.len() {
return Pixels::ZERO;
}
let mut r = (self.root_radius - self.root_border - self.root_padding).max(Pixels::ZERO);
for i in 0..level {
let (b, p) = self.children[i];
r = (r - b - p).max(Pixels::ZERO);
}
r
}
}