From ef01a6482617abe6a56e7a469767e7f02a9a87e6 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Sat, 30 Sep 2023 09:43:52 -0600 Subject: [PATCH] Fix infinite loop --- crates/gpui3/src/app.rs | 4 +-- crates/gpui3/src/view.rs | 6 ++-- crates/gpui3/src/window.rs | 68 +++++++++++++++++++++----------------- 3 files changed, 40 insertions(+), 38 deletions(-) diff --git a/crates/gpui3/src/app.rs b/crates/gpui3/src/app.rs index 23e05657d4..5981754a28 100644 --- a/crates/gpui3/src/app.rs +++ b/crates/gpui3/src/app.rs @@ -21,7 +21,6 @@ use smallvec::SmallVec; use std::{ any::{type_name, Any, TypeId}, marker::PhantomData, - ops::{Deref, DerefMut}, sync::{Arc, Weak}, }; use util::ResultExt; @@ -220,7 +219,6 @@ impl AppContext { .unwrap(); let result = update(&mut WindowContext::mutable(cx.downcast_mut(), &mut window)); - window.dirty = true; cx.windows .get_mut(id) @@ -266,7 +264,7 @@ impl AppContext { for dirty_window_id in dirty_window_ids { self.update_window(dirty_window_id, |cx| cx.draw()) - .unwrap() // We know we have the window. + .unwrap() .log_err(); } } diff --git a/crates/gpui3/src/view.rs b/crates/gpui3/src/view.rs index 2acdfa570c..218ca1c4f4 100644 --- a/crates/gpui3/src/view.rs +++ b/crates/gpui3/src/view.rs @@ -102,9 +102,7 @@ impl ViewObject for View { fn paint(&mut self, _: Layout, element: &mut dyn Any, cx: &mut WindowContext) -> Result<()> { self.state.update(cx, |state, cx| { - let boxed_element = element.downcast_mut::>().unwrap(); - let element = boxed_element.downcast_mut::>().unwrap(); - + let element = element.downcast_mut::>().unwrap(); element.paint(state, None, cx) }) } @@ -135,7 +133,7 @@ impl Element for AnyView { cx: &mut ViewContext, ) -> Result<()> { dbg!("Element.paint for AnyView"); - self.view.lock().paint(layout, element, cx) + self.view.lock().paint(layout, element.as_mut(), cx) } } diff --git a/crates/gpui3/src/window.rs b/crates/gpui3/src/window.rs index 647217bef1..129f0d0e3f 100644 --- a/crates/gpui3/src/window.rs +++ b/crates/gpui3/src/window.rs @@ -80,33 +80,8 @@ impl<'a, 'w> WindowContext<'a, 'w> { } } - pub(crate) fn draw(&mut self) -> Result<()> { - dbg!("Draw"); - let unit_entity = self.unit_entity.clone(); - self.update_entity(&unit_entity, |_, cx| { - let mut root_view = cx.window.root_view.take().unwrap(); - let (root_layout_id, mut frame_state) = root_view.layout(&mut (), cx)?; - let available_space = cx.window.content_size.map(Into::into); - cx.window - .layout_engine - .compute_layout(root_layout_id, available_space)?; - let layout = cx.window.layout_engine.layout(root_layout_id)?; - dbg!("Paint root view"); - root_view.paint(layout, &mut (), &mut frame_state, cx)?; - cx.window.root_view = Some(root_view); - let scene = cx.window.scene.take(); - dbg!(&scene); - - // todo! - // self.run_on_main(|cx| { - // cx.window - // .platform_window - // .borrow_on_main_thread() - // .draw(scene); - // }); - - Ok(()) - }) + pub fn notify(&mut self) { + self.window.dirty = true; } pub fn request_layout( @@ -151,6 +126,37 @@ impl<'a, 'w> WindowContext<'a, 'w> { pub fn mouse_position(&self) -> Point { self.window.mouse_position } + + pub(crate) fn draw(&mut self) -> Result<()> { + dbg!("Draw"); + let unit_entity = self.unit_entity.clone(); + self.update_entity(&unit_entity, |_, cx| { + let mut root_view = cx.window.root_view.take().unwrap(); + let (root_layout_id, mut frame_state) = root_view.layout(&mut (), cx)?; + let available_space = cx.window.content_size.map(Into::into); + cx.window + .layout_engine + .compute_layout(root_layout_id, available_space)?; + let layout = cx.window.layout_engine.layout(root_layout_id)?; + dbg!("Paint root view"); + root_view.paint(layout, &mut (), &mut frame_state, cx)?; + cx.window.root_view = Some(root_view); + let scene = cx.window.scene.take(); + + dbg!(&scene); + + // todo! + // self.run_on_main(|cx| { + // cx.window + // .platform_window + // .borrow_on_main_thread() + // .draw(scene); + // }); + + cx.window.dirty = false; + Ok(()) + }) + } } impl WindowContext<'_, '_, MainThread> { @@ -268,11 +274,11 @@ impl<'a, 'w, S: Send + Sync + 'static> ViewContext<'a, 'w, S> { } pub fn notify(&mut self) { - let entity_id = self.entity_id; - self.app + self.window_cx.notify(); + self.window_cx + .app .pending_effects - .push_back(Effect::Notify(entity_id)); - self.window.dirty = true; + .push_back(Effect::Notify(self.entity_id)); } pub(crate) fn erase_state(&mut self, f: impl FnOnce(&mut ViewContext<()>) -> R) -> R {