Files
oak-gpui/crates/ui/src/utils/with_rem_size.rs
T
ab59982bf7 Add initial element inspector for Zed development (#31315)
Open inspector with `dev: toggle inspector` from command palette or
`cmd-alt-i` on mac or `ctrl-alt-i` on linux.

https://github.com/user-attachments/assets/54c43034-d40b-414e-ba9b-190bed2e6d2f

* Picking of elements via the mouse, with scroll wheel to inspect
occluded elements.

* Temporary manipulation of the selected element.

* Layout info and JSON-based style manipulation for `Div`.

* Navigation to code that constructed the element.

Big thanks to @as-cii and @maxdeviant for sorting out how to implement
the core of an inspector.

Release Notes:

- N/A

---------

Co-authored-by: Antonio Scandurra <me@as-cii.com>
Co-authored-by: Marshall Bowers <git@maxdeviant.com>
Co-authored-by: Federico Dionisi <code@fdionisi.me>
2025-05-23 23:08:59 +00:00

115 lines
3.1 KiB
Rust

use gpui::{
AnyElement, App, Bounds, Div, DivFrameState, Element, ElementId, GlobalElementId, Hitbox,
InteractiveElement as _, IntoElement, LayoutId, ParentElement, Pixels, StyleRefinement, Styled,
Window, div,
};
/// An element that sets a particular rem size for its children.
pub struct WithRemSize {
div: Div,
rem_size: Pixels,
}
impl WithRemSize {
/// Create a new [WithRemSize] element, which sets a
/// particular rem size for its children.
pub fn new(rem_size: impl Into<Pixels>) -> Self {
Self {
div: div(),
rem_size: rem_size.into(),
}
}
/// Block the mouse from interacting with this element or any of its children
/// The fluent API equivalent to [`Interactivity::occlude_mouse`]
///
/// [`Interactivity::occlude_mouse`]: gpui::Interactivity::occlude_mouse
pub fn occlude(mut self) -> Self {
self.div = self.div.occlude();
self
}
}
impl Styled for WithRemSize {
fn style(&mut self) -> &mut StyleRefinement {
self.div.style()
}
}
impl ParentElement for WithRemSize {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.div.extend(elements)
}
}
impl Element for WithRemSize {
type RequestLayoutState = DivFrameState;
type PrepaintState = Option<Hitbox>;
fn id(&self) -> Option<ElementId> {
Element::id(&self.div)
}
fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
Element::source_location(&self.div)
}
fn request_layout(
&mut self,
id: Option<&GlobalElementId>,
inspector_id: Option<&gpui::InspectorElementId>,
window: &mut Window,
cx: &mut App,
) -> (LayoutId, Self::RequestLayoutState) {
window.with_rem_size(Some(self.rem_size), |window| {
self.div.request_layout(id, inspector_id, window, cx)
})
}
fn prepaint(
&mut self,
id: Option<&GlobalElementId>,
inspector_id: Option<&gpui::InspectorElementId>,
bounds: Bounds<Pixels>,
request_layout: &mut Self::RequestLayoutState,
window: &mut Window,
cx: &mut App,
) -> Self::PrepaintState {
window.with_rem_size(Some(self.rem_size), |window| {
self.div
.prepaint(id, inspector_id, bounds, request_layout, window, cx)
})
}
fn paint(
&mut self,
id: Option<&GlobalElementId>,
inspector_id: Option<&gpui::InspectorElementId>,
bounds: Bounds<Pixels>,
request_layout: &mut Self::RequestLayoutState,
prepaint: &mut Self::PrepaintState,
window: &mut Window,
cx: &mut App,
) {
window.with_rem_size(Some(self.rem_size), |window| {
self.div.paint(
id,
inspector_id,
bounds,
request_layout,
prepaint,
window,
cx,
)
})
}
}
impl IntoElement for WithRemSize {
type Element = Self;
fn into_element(self) -> Self::Element {
self
}
}