Add Context::read_window, WindowHandle::root, and change ViewContext.view() to return a reference

This commit is contained in:
Mikayla
2023-11-08 22:11:19 -08:00
parent ad3b0bd227
commit 7a7ef1025d
7 changed files with 103 additions and 6 deletions
+18
View File
@@ -66,6 +66,16 @@ impl Context for AsyncAppContext {
let mut lock = app.borrow_mut();
lock.update_window(window, f)
}
fn read_window<R>(
&self,
window: &AnyWindowHandle,
read: impl FnOnce(AnyView, &AppContext) -> R,
) -> Result<R> {
let app = self.app.upgrade().context("app was released")?;
let lock = app.borrow();
lock.read_window(window, read)
}
}
impl AsyncAppContext {
@@ -250,6 +260,14 @@ impl Context for AsyncWindowContext {
{
self.app.read_model(handle, read)
}
fn read_window<R>(
&self,
window: &AnyWindowHandle,
read: impl FnOnce(AnyView, &AppContext) -> R,
) -> Result<R> {
self.app.read_window(window, read)
}
}
impl VisualContext for AsyncWindowContext {
+8
View File
@@ -239,6 +239,14 @@ impl<'a, T> Context for ModelContext<'a, T> {
{
self.app.read_model(handle, read)
}
fn read_window<R>(
&self,
window: &AnyWindowHandle,
read: impl FnOnce(AnyView, &AppContext) -> R,
) -> Result<R> {
self.app.read_window(window, read)
}
}
impl<T> Borrow<AppContext> for ModelContext<'_, T> {
+14
View File
@@ -58,6 +58,15 @@ impl Context for TestAppContext {
let app = self.app.borrow();
app.read_model(handle, read)
}
fn read_window<R>(
&self,
window: &AnyWindowHandle,
read: impl FnOnce(AnyView, &AppContext) -> R,
) -> Result<R> {
let app = self.app.borrow();
app.read_window(window, read)
}
}
impl TestAppContext {
@@ -146,6 +155,11 @@ impl TestAppContext {
Some(read(lock.try_global()?, &lock))
}
pub fn set_global<G: 'static, R>(&mut self, global: G) {
let mut lock = self.app.borrow_mut();
lock.set_global(global);
}
pub fn update_global<G: 'static, R>(
&mut self,
update: impl FnOnce(&mut G, &mut AppContext) -> R,