mirror of
https://github.com/zed-industries/zed.git
synced 2025-01-24 19:10:24 +00:00
Merge pull request #2429 from zed-industries/fix-debug-elements-panic
Move `debug_elements` to `AsyncAppContext`
This commit is contained in:
parent
1c7fb0f0cb
commit
c3212e559f
4 changed files with 23 additions and 78 deletions
|
@ -43,6 +43,7 @@ use window_input_handler::WindowInputHandler;
|
||||||
use crate::{
|
use crate::{
|
||||||
elements::{AnyElement, AnyRootElement, RootElement},
|
elements::{AnyElement, AnyRootElement, RootElement},
|
||||||
executor::{self, Task},
|
executor::{self, Task},
|
||||||
|
json,
|
||||||
keymap_matcher::{self, Binding, KeymapContext, KeymapMatcher, Keystroke, MatchResult},
|
keymap_matcher::{self, Binding, KeymapContext, KeymapMatcher, Keystroke, MatchResult},
|
||||||
platform::{
|
platform::{
|
||||||
self, FontSystem, KeyDownEvent, KeyUpEvent, ModifiersChangedEvent, MouseButton,
|
self, FontSystem, KeyDownEvent, KeyUpEvent, ModifiersChangedEvent, MouseButton,
|
||||||
|
@ -309,6 +310,14 @@ impl AsyncAppContext {
|
||||||
self.0.borrow_mut().update_window(window_id, callback)
|
self.0.borrow_mut().update_window(window_id, callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn debug_elements(&self, window_id: usize) -> Option<json::Value> {
|
||||||
|
self.0.borrow().read_window(window_id, |cx| {
|
||||||
|
let root_view = cx.window.root_view();
|
||||||
|
let root_element = cx.window.rendered_views.get(&root_view.id())?;
|
||||||
|
root_element.debug(cx).log_err()
|
||||||
|
})?
|
||||||
|
}
|
||||||
|
|
||||||
pub fn add_model<T, F>(&mut self, build_model: F) -> ModelHandle<T>
|
pub fn add_model<T, F>(&mut self, build_model: F) -> ModelHandle<T>
|
||||||
where
|
where
|
||||||
T: Entity,
|
T: Entity,
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
elements::AnyRootElement,
|
elements::AnyRootElement,
|
||||||
geometry::rect::RectF,
|
geometry::rect::RectF,
|
||||||
json::{self, ToJson},
|
json::ToJson,
|
||||||
keymap_matcher::{Binding, Keystroke, MatchResult},
|
keymap_matcher::{Binding, Keystroke, MatchResult},
|
||||||
platform::{
|
platform::{
|
||||||
self, Appearance, CursorStyle, Event, KeyDownEvent, KeyUpEvent, ModifiersChangedEvent,
|
self, Appearance, CursorStyle, Event, KeyDownEvent, KeyUpEvent, ModifiersChangedEvent,
|
||||||
|
@ -975,17 +975,6 @@ impl<'a> WindowContext<'a> {
|
||||||
.flatten()
|
.flatten()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn debug_elements(&self) -> Option<json::Value> {
|
|
||||||
let view = self.window.root_view();
|
|
||||||
Some(json!({
|
|
||||||
"root_view": view.debug_json(self),
|
|
||||||
"root_element": self.window.rendered_views.get(&view.id())
|
|
||||||
.and_then(|root_element| {
|
|
||||||
root_element.debug(self).log_err()
|
|
||||||
})
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_window_title(&mut self, title: &str) {
|
pub fn set_window_title(&mut self, title: &str) {
|
||||||
self.window.platform_window.set_title(title);
|
self.window.platform_window.set_title(title);
|
||||||
}
|
}
|
||||||
|
@ -1454,13 +1443,7 @@ impl<V: View> Element<V> for ChildView {
|
||||||
) -> serde_json::Value {
|
) -> serde_json::Value {
|
||||||
json!({
|
json!({
|
||||||
"type": "ChildView",
|
"type": "ChildView",
|
||||||
"view_id": self.view_id,
|
|
||||||
"bounds": bounds.to_json(),
|
"bounds": bounds.to_json(),
|
||||||
"view": if let Some(view) = cx.views.get(&(cx.window_id, self.view_id)) {
|
|
||||||
view.debug_json(cx)
|
|
||||||
} else {
|
|
||||||
json!(null)
|
|
||||||
},
|
|
||||||
"child": if let Some(element) = cx.window.rendered_views.get(&self.view_id) {
|
"child": if let Some(element) = cx.window.rendered_views.get(&self.view_id) {
|
||||||
element.debug(&cx.window_context).log_err().unwrap_or_else(|| json!(null))
|
element.debug(&cx.window_context).log_err().unwrap_or_else(|| json!(null))
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -45,7 +45,6 @@ use std::{
|
||||||
mem,
|
mem,
|
||||||
ops::{Deref, DerefMut, Range},
|
ops::{Deref, DerefMut, Range},
|
||||||
};
|
};
|
||||||
use util::ResultExt;
|
|
||||||
|
|
||||||
pub trait Element<V: View>: 'static {
|
pub trait Element<V: View>: 'static {
|
||||||
type LayoutState;
|
type LayoutState;
|
||||||
|
@ -709,7 +708,12 @@ impl<V: View> AnyRootElement for RootElement<V> {
|
||||||
.ok_or_else(|| anyhow!("debug called on a root element for a dropped view"))?;
|
.ok_or_else(|| anyhow!("debug called on a root element for a dropped view"))?;
|
||||||
let view = view.read(cx);
|
let view = view.read(cx);
|
||||||
let view_context = ViewContext::immutable(cx, self.view.id());
|
let view_context = ViewContext::immutable(cx, self.view.id());
|
||||||
Ok(self.element.debug(view, &view_context))
|
Ok(serde_json::json!({
|
||||||
|
"view_id": self.view.id(),
|
||||||
|
"view_name": V::ui_name(),
|
||||||
|
"view": view.debug_json(cx),
|
||||||
|
"element": self.element.debug(view, &view_context)
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn name(&self) -> Option<&str> {
|
fn name(&self) -> Option<&str> {
|
||||||
|
@ -717,63 +721,6 @@ impl<V: View> AnyRootElement for RootElement<V> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<V: View, R: View> Element<V> for RootElement<R> {
|
|
||||||
type LayoutState = ();
|
|
||||||
type PaintState = ();
|
|
||||||
|
|
||||||
fn layout(
|
|
||||||
&mut self,
|
|
||||||
constraint: SizeConstraint,
|
|
||||||
_view: &mut V,
|
|
||||||
cx: &mut ViewContext<V>,
|
|
||||||
) -> (Vector2F, ()) {
|
|
||||||
let size = AnyRootElement::layout(self, constraint, cx)
|
|
||||||
.log_err()
|
|
||||||
.unwrap_or_else(|| Vector2F::zero());
|
|
||||||
(size, ())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn paint(
|
|
||||||
&mut self,
|
|
||||||
scene: &mut SceneBuilder,
|
|
||||||
bounds: RectF,
|
|
||||||
visible_bounds: RectF,
|
|
||||||
_layout: &mut Self::LayoutState,
|
|
||||||
_view: &mut V,
|
|
||||||
cx: &mut ViewContext<V>,
|
|
||||||
) {
|
|
||||||
AnyRootElement::paint(self, scene, bounds.origin(), visible_bounds, cx).log_err();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn rect_for_text_range(
|
|
||||||
&self,
|
|
||||||
range_utf16: Range<usize>,
|
|
||||||
_bounds: RectF,
|
|
||||||
_visible_bounds: RectF,
|
|
||||||
_layout: &Self::LayoutState,
|
|
||||||
_paint: &Self::PaintState,
|
|
||||||
_view: &V,
|
|
||||||
cx: &ViewContext<V>,
|
|
||||||
) -> Option<RectF> {
|
|
||||||
AnyRootElement::rect_for_text_range(self, range_utf16, cx)
|
|
||||||
.log_err()
|
|
||||||
.flatten()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn debug(
|
|
||||||
&self,
|
|
||||||
_bounds: RectF,
|
|
||||||
_layout: &Self::LayoutState,
|
|
||||||
_paint: &Self::PaintState,
|
|
||||||
_view: &V,
|
|
||||||
cx: &ViewContext<V>,
|
|
||||||
) -> serde_json::Value {
|
|
||||||
AnyRootElement::debug(self, cx)
|
|
||||||
.log_err()
|
|
||||||
.unwrap_or_default()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait ParentElement<'a, V: View>: Extend<AnyElement<V>> + Sized {
|
pub trait ParentElement<'a, V: View>: Extend<AnyElement<V>> + Sized {
|
||||||
fn add_children<E: Element<V>>(&mut self, children: impl IntoIterator<Item = E>) {
|
fn add_children<E: Element<V>>(&mut self, children: impl IntoIterator<Item = E>) {
|
||||||
self.extend(children.into_iter().map(|child| child.into_any()));
|
self.extend(children.into_iter().map(|child| child.into_any()));
|
||||||
|
|
|
@ -11,6 +11,7 @@ use collections::VecDeque;
|
||||||
pub use editor;
|
pub use editor;
|
||||||
use editor::{Editor, MultiBuffer};
|
use editor::{Editor, MultiBuffer};
|
||||||
|
|
||||||
|
use anyhow::anyhow;
|
||||||
use feedback::{
|
use feedback::{
|
||||||
feedback_info_text::FeedbackInfoText, submit_feedback_button::SubmitFeedbackButton,
|
feedback_info_text::FeedbackInfoText, submit_feedback_button::SubmitFeedbackButton,
|
||||||
};
|
};
|
||||||
|
@ -223,9 +224,14 @@ pub fn init(app_state: &Arc<AppState>, cx: &mut gpui::AppContext) {
|
||||||
move |_: &mut Workspace, _: &DebugElements, cx: &mut ViewContext<Workspace>| {
|
move |_: &mut Workspace, _: &DebugElements, cx: &mut ViewContext<Workspace>| {
|
||||||
let app_state = app_state.clone();
|
let app_state = app_state.clone();
|
||||||
let markdown = app_state.languages.language_for_name("JSON");
|
let markdown = app_state.languages.language_for_name("JSON");
|
||||||
let content = to_string_pretty(&cx.debug_elements()).unwrap();
|
let window_id = cx.window_id();
|
||||||
cx.spawn(|workspace, mut cx| async move {
|
cx.spawn(|workspace, mut cx| async move {
|
||||||
let markdown = markdown.await.log_err();
|
let markdown = markdown.await.log_err();
|
||||||
|
let content = to_string_pretty(
|
||||||
|
&cx.debug_elements(window_id)
|
||||||
|
.ok_or_else(|| anyhow!("could not debug elements for {window_id}"))?,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
workspace
|
workspace
|
||||||
.update(&mut cx, |workspace, cx| {
|
.update(&mut cx, |workspace, cx| {
|
||||||
workspace.with_local_workspace(&app_state, cx, move |workspace, cx| {
|
workspace.with_local_workspace(&app_state, cx, move |workspace, cx| {
|
||||||
|
|
Loading…
Reference in a new issue