use gpui_kit::{
AbsoluteLength, Bounds, DefiniteLength, DivInspectorState, Edges, ElementId,
InspectorElementId, Length, Pixels, Refineable as _, Size, Style, size,
};
const SHARED_CRATE: &str = "subspace_dashboard::";
const SUBSPACE_PREFIX: &str = "subspace_";
const SHARED_SOURCES: &str = "board-kit/dashboard/src/";
const REGISTRY_SOURCES: &str = "/registry/src/";
#[derive(Clone, Debug, PartialEq)]
pub(super) struct Component {
pub(super) path: String,
}
impl Component {
pub(super) fn name(&self) -> String {
let path = self.path.strip_prefix(SHARED_CRATE).unwrap_or(&self.path);
collapse_repeats(path)
}
fn module(&self) -> &str {
self.path.rsplit_once("::").map_or("", |(module, _)| module)
}
fn is_subspace(&self) -> bool {
self.path.starts_with(SUBSPACE_PREFIX)
}
fn is_shared(&self) -> bool {
self.path.starts_with(SHARED_CRATE)
}
fn is_part_of(&self, outer: &Self) -> bool {
let module = outer.module();
let inner = self.module();
module.contains("::")
&& (inner == module
|| inner
.strip_prefix(module)
.is_some_and(|rest| rest.starts_with("::")))
}
}
fn collapse_repeats(path: &str) -> String {
let mut segments: Vec<&str> = Vec::new();
for segment in path.split("::") {
if segments.last() != Some(&segment) {
segments.push(segment);
}
}
segments.join("::")
}
fn type_path(id: &ElementId) -> Option<String> {
let ElementId::Name(name) = id else {
return None;
};
let path = name.split('<').next().unwrap_or_default();
let segments: Vec<&str> = path.split("::").collect();
let identifiers = segments.iter().all(|segment| {
!segment.is_empty()
&& segment
.chars()
.all(|character| character.is_ascii_alphanumeric() || character == '_')
});
let crate_case = segments
.first()
.and_then(|segment| segment.chars().next())
.is_some_and(|first| first.is_ascii_lowercase());
let type_case = segments
.last()
.and_then(|segment| segment.chars().next())
.is_some_and(|first| first.is_ascii_uppercase());
(segments.len() > 1 && identifiers && crate_case && type_case).then(|| path.to_owned())
}
pub(super) fn components(path: &[ElementId]) -> Vec<Component> {
path.iter()
.filter_map(type_path)
.map(|path| Component { path })
.collect()
}
pub(super) fn named(components: &[Component]) -> Option<&Component> {
innermost_whole(components.iter().filter(|c| c.is_shared()))
.or_else(|| innermost_whole(components.iter().filter(|c| c.is_subspace())))
.or_else(|| components.last())
}
fn innermost_whole<'a>(list: impl Iterator<Item = &'a Component>) -> Option<&'a Component> {
let mut named: Option<&Component> = None;
for component in list {
if named.is_none_or(|outer| !component.is_part_of(outer)) {
named = Some(component);
}
}
named
}
pub(super) fn element_id(path: &[ElementId]) -> Option<String> {
path.iter()
.rev()
.find(|id| type_path(id).is_none() && !matches!(id, ElementId::View(_)))
.map(ToString::to_string)
}
pub(super) fn compact_source(file: &str) -> String {
match file.split_once(REGISTRY_SOURCES) {
Some((_, rest)) => rest
.split_once('/')
.map_or(rest, |(_, path)| path)
.to_owned(),
None => file.to_owned(),
}
}
pub(super) fn shared_module(file: &str) -> Option<String> {
let relative = file.split_once(SHARED_SOURCES)?.1.strip_suffix(".rs")?;
let module = relative.strip_suffix("/mod").unwrap_or(relative);
(module != "lib").then(|| module.replace('/', "::"))
}
pub(super) fn size_label(size: Size<Pixels>) -> String {
format!(
"{} × {}",
number(f32::from(size.width)),
number(f32::from(size.height))
)
}
const TENTHS: f32 = 10.;
fn number(value: f32) -> String {
let rounded = (value * TENTHS).round() / TENTHS;
if rounded.fract().abs() < f32::EPSILON {
format!("{rounded:.0}")
} else {
format!("{rounded:.1}")
}
}
pub(super) fn edges_label(edges: &Edges<Pixels>) -> String {
let [top, right, bottom, left] =
[edges.top, edges.right, edges.bottom, edges.left].map(|edge| number(f32::from(edge)));
if top == right && right == bottom && bottom == left {
top
} else if top == bottom && right == left {
format!("{top} {right}")
} else if right == left {
format!("{top} {right} {bottom}")
} else {
format!("{top} {right} {bottom} {left}")
}
}
#[derive(Clone, Debug, PartialEq)]
pub(super) struct BoxModel {
pub(super) bounds: Bounds<Pixels>,
pub(super) content: Size<Pixels>,
pub(super) padding: Edges<Pixels>,
pub(super) border: Edges<Pixels>,
pub(super) margin: Edges<Pixels>,
}
impl BoxModel {
pub(super) fn new(state: &DivInspectorState, rem_size: Pixels) -> Self {
let style = Style::default().refined((*state.base_style).clone());
let basis = size(
AbsoluteLength::Pixels(state.bounds.size.width),
AbsoluteLength::Pixels(state.bounds.size.height),
);
Self {
bounds: state.bounds,
content: state.content_size,
padding: style.padding.to_pixels(basis, rem_size),
border: style.border_widths.to_pixels(rem_size),
margin: margin(&style.margin, state.bounds.size, rem_size),
}
}
}
fn margin(edges: &Edges<Length>, basis: Size<Pixels>, rem_size: Pixels) -> Edges<Pixels> {
let resolve = |length: Length, base: Pixels| match length {
Length::Definite(DefiniteLength::Absolute(length)) => length.to_pixels(rem_size),
Length::Definite(DefiniteLength::Fraction(fraction)) => base * fraction,
Length::Auto => Pixels::ZERO,
};
Edges {
top: resolve(edges.top, basis.height),
right: resolve(edges.right, basis.width),
bottom: resolve(edges.bottom, basis.height),
left: resolve(edges.left, basis.width),
}
}
#[derive(Clone, Debug, PartialEq)]
pub(super) struct ElementReport {
pub(super) components: Vec<Component>,
pub(super) element_id: Option<String>,
pub(super) id_path: String,
pub(super) source: String,
pub(super) module: Option<String>,
pub(super) layout: BoxModel,
}
impl ElementReport {
pub(super) fn new(id: &InspectorElementId, state: &DivInspectorState, rem: Pixels) -> Self {
let path: &[ElementId] = &id.path.global_id;
let location = id.path.source_location;
Self {
components: components(path),
element_id: element_id(path),
id_path: id.path.global_id.to_string(),
source: format!(
"{}:{}:{}",
compact_source(location.file()),
location.line(),
location.column()
),
module: shared_module(location.file()),
layout: BoxModel::new(state, rem),
}
}
pub(super) fn name(&self) -> Option<String> {
named(&self.components)
.map(Component::name)
.or_else(|| self.module.clone())
}
pub(super) fn chain(&self) -> String {
self.components
.iter()
.map(Component::name)
.collect::<Vec<_>>()
.join(" › ")
}
pub(super) fn tag(&self) -> String {
let size = size_label(self.layout.bounds.size);
match self.name() {
Some(name) => format!("{name} {size}"),
None => size,
}
}
pub(super) fn summary(&self) -> String {
let layout = &self.layout;
[
("Component", self.name().unwrap_or_default()),
("Inside", self.chain()),
("ID", self.element_id.clone().unwrap_or_default()),
("ID path", self.id_path.clone()),
("Source", self.source.clone()),
("Size", size_label(layout.bounds.size)),
(
"Origin",
format!(
"{}, {}",
number(f32::from(layout.bounds.origin.x)),
number(f32::from(layout.bounds.origin.y))
),
),
("Content", size_label(layout.content)),
("Padding", edges_label(&layout.padding)),
("Border", edges_label(&layout.border)),
("Margin", edges_label(&layout.margin)),
]
.into_iter()
.filter(|(_, value)| !value.is_empty())
.map(|(label, value)| format!("{label}: {value}"))
.collect::<Vec<_>>()
.join("\n")
}
}