Get tests compiling returning WindowHandle<V: View> from add_window

This commit is contained in:
Nathan Sobo 2023-08-02 14:05:03 -06:00
parent 60e190e500
commit 884cee6dfd
24 changed files with 726 additions and 578 deletions

View file

@ -1424,7 +1424,7 @@ impl AppContext {
&mut self,
window_id: usize,
build_root_view: F,
) -> Option<ViewHandle<V>>
) -> Option<WindowHandle<V>>
where
V: View,
F: FnOnce(&mut ViewContext<V>) -> V,
@ -3826,6 +3826,15 @@ impl<V: View> WindowHandle<V> {
self.read_with(cx, |cx| cx.root_view().clone().downcast().unwrap())
}
/// Keep this window open until it's explicitly closed.
//
// TODO: Implement window dropping behavior when we don't call this.
pub fn detach(mut self, cx: &impl BorrowAppContext) -> ViewHandle<V> {
let root = self.root(cx);
self.any_handle.ref_counts.take();
root
}
pub fn read_with<C, F, R>(&self, cx: &C, read: F) -> R
where
C: BorrowAppContext,
@ -3893,7 +3902,7 @@ impl<V: View> WindowHandle<V> {
pub struct AnyWindowHandle {
window_id: usize,
root_view_type: TypeId,
ref_counts: Arc<Mutex<RefCounts>>,
ref_counts: Option<Arc<Mutex<RefCounts>>>,
#[cfg(any(test, feature = "test-support"))]
handle_id: usize,
@ -3913,7 +3922,7 @@ impl AnyWindowHandle {
Self {
window_id,
root_view_type: TypeId::of::<V>(),
ref_counts,
ref_counts: Some(ref_counts),
#[cfg(any(test, feature = "test-support"))]
handle_id,
}
@ -3937,7 +3946,16 @@ impl AnyWindowHandle {
impl Drop for AnyWindowHandle {
fn drop(&mut self) {
self.ref_counts.lock().dec_window(self.window_id)
if let Some(ref_counts) = self.ref_counts.as_ref() {
ref_counts.lock().dec_window(self.window_id);
#[cfg(any(test, feature = "test-support"))]
ref_counts
.lock()
.leak_detector
.lock()
.handle_dropped(self.window_id, self.handle_id);
}
}
}

View file

@ -15,7 +15,7 @@ use crate::{
util::post_inc,
Action, AnyView, AnyViewHandle, AppContext, BorrowAppContext, BorrowWindowContext, Effect,
Element, Entity, Handle, LayoutContext, MouseRegion, MouseRegionId, SceneBuilder, Subscription,
View, ViewContext, ViewHandle, WindowInvalidation,
View, ViewContext, ViewHandle, WindowHandle, WindowInvalidation,
};
use anyhow::{anyhow, bail, Result};
use collections::{HashMap, HashSet};
@ -1151,15 +1151,15 @@ impl<'a> WindowContext<'a> {
self.window.platform_window.prompt(level, msg, answers)
}
pub fn replace_root_view<V, F>(&mut self, build_root_view: F) -> ViewHandle<V>
pub fn replace_root_view<V, F>(&mut self, build_root_view: F) -> WindowHandle<V>
where
V: View,
F: FnOnce(&mut ViewContext<V>) -> V,
{
let root_view = self.add_view(|cx| build_root_view(cx));
self.window.root_view = Some(root_view.clone().into_any());
self.window.focused_view_id = Some(root_view.id());
root_view
self.window.root_view = Some(root_view.into_any());
WindowHandle::new(self.window_id, self.ref_counts.clone())
}
pub fn add_view<T, F>(&mut self, build_view: F) -> ViewHandle<T>