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>
96 lines
2.7 KiB
Rust
96 lines
2.7 KiB
Rust
use refineable::Refineable as _;
|
|
|
|
use crate::{
|
|
App, Bounds, Element, ElementId, GlobalElementId, InspectorElementId, IntoElement, Pixels,
|
|
Style, StyleRefinement, Styled, Window,
|
|
};
|
|
|
|
/// Construct a canvas element with the given paint callback.
|
|
/// Useful for adding short term custom drawing to a view.
|
|
pub fn canvas<T>(
|
|
prepaint: impl 'static + FnOnce(Bounds<Pixels>, &mut Window, &mut App) -> T,
|
|
paint: impl 'static + FnOnce(Bounds<Pixels>, T, &mut Window, &mut App),
|
|
) -> Canvas<T> {
|
|
Canvas {
|
|
prepaint: Some(Box::new(prepaint)),
|
|
paint: Some(Box::new(paint)),
|
|
style: StyleRefinement::default(),
|
|
}
|
|
}
|
|
|
|
/// A canvas element, meant for accessing the low level paint API without defining a whole
|
|
/// custom element
|
|
pub struct Canvas<T> {
|
|
prepaint: Option<Box<dyn FnOnce(Bounds<Pixels>, &mut Window, &mut App) -> T>>,
|
|
paint: Option<Box<dyn FnOnce(Bounds<Pixels>, T, &mut Window, &mut App)>>,
|
|
style: StyleRefinement,
|
|
}
|
|
|
|
impl<T: 'static> IntoElement for Canvas<T> {
|
|
type Element = Self;
|
|
|
|
fn into_element(self) -> Self::Element {
|
|
self
|
|
}
|
|
}
|
|
|
|
impl<T: 'static> Element for Canvas<T> {
|
|
type RequestLayoutState = Style;
|
|
type PrepaintState = Option<T>;
|
|
|
|
fn id(&self) -> Option<ElementId> {
|
|
None
|
|
}
|
|
|
|
fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
|
|
None
|
|
}
|
|
|
|
fn request_layout(
|
|
&mut self,
|
|
_id: Option<&GlobalElementId>,
|
|
_inspector_id: Option<&InspectorElementId>,
|
|
window: &mut Window,
|
|
cx: &mut App,
|
|
) -> (crate::LayoutId, Self::RequestLayoutState) {
|
|
let mut style = Style::default();
|
|
style.refine(&self.style);
|
|
let layout_id = window.request_layout(style.clone(), [], cx);
|
|
(layout_id, style)
|
|
}
|
|
|
|
fn prepaint(
|
|
&mut self,
|
|
_id: Option<&GlobalElementId>,
|
|
_inspector_id: Option<&InspectorElementId>,
|
|
bounds: Bounds<Pixels>,
|
|
_request_layout: &mut Style,
|
|
window: &mut Window,
|
|
cx: &mut App,
|
|
) -> Option<T> {
|
|
Some(self.prepaint.take().unwrap()(bounds, window, cx))
|
|
}
|
|
|
|
fn paint(
|
|
&mut self,
|
|
_id: Option<&GlobalElementId>,
|
|
_inspector_id: Option<&InspectorElementId>,
|
|
bounds: Bounds<Pixels>,
|
|
style: &mut Style,
|
|
prepaint: &mut Self::PrepaintState,
|
|
window: &mut Window,
|
|
cx: &mut App,
|
|
) {
|
|
let prepaint = prepaint.take().unwrap();
|
|
style.paint(bounds, window, cx, |window, cx| {
|
|
(self.paint.take().unwrap())(bounds, prepaint, window, cx)
|
|
});
|
|
}
|
|
}
|
|
|
|
impl<T> Styled for Canvas<T> {
|
|
fn style(&mut self) -> &mut crate::StyleRefinement {
|
|
&mut self.style
|
|
}
|
|
}
|