This commit is contained in:
Nathan Sobo 2023-08-03 08:10:16 -06:00
parent 8e36da1382
commit 3c938a7377
3 changed files with 16 additions and 4 deletions

View file

@ -130,10 +130,12 @@ pub trait BorrowAppContext {
}
pub trait BorrowWindowContext {
fn read_with<T, F>(&self, window_id: usize, f: F) -> T
type Return<T>;
fn read_with<T, F>(&self, window_id: usize, f: F) -> Self::Return<T>
where
F: FnOnce(&WindowContext) -> T;
fn update<T, F>(&mut self, window_id: usize, f: F) -> T
fn update<T, F>(&mut self, window_id: usize, f: F) -> Self::Return<T>
where
F: FnOnce(&mut WindowContext) -> T;
}
@ -3358,6 +3360,8 @@ impl<V> BorrowAppContext for ViewContext<'_, '_, V> {
}
impl<V> BorrowWindowContext for ViewContext<'_, '_, V> {
type Return<T> = T;
fn read_with<T, F: FnOnce(&WindowContext) -> T>(&self, window_id: usize, f: F) -> T {
BorrowWindowContext::read_with(&*self.window_context, window_id, f)
}
@ -3463,6 +3467,8 @@ impl<V: View> BorrowAppContext for LayoutContext<'_, '_, '_, V> {
}
impl<V: View> BorrowWindowContext for LayoutContext<'_, '_, '_, V> {
type Return<T> = T;
fn read_with<T, F: FnOnce(&WindowContext) -> T>(&self, window_id: usize, f: F) -> T {
BorrowWindowContext::read_with(&*self.view_context, window_id, f)
}
@ -3515,6 +3521,8 @@ impl<V: View> BorrowAppContext for EventContext<'_, '_, '_, V> {
}
impl<V: View> BorrowWindowContext for EventContext<'_, '_, '_, V> {
type Return<T> = T;
fn read_with<T, F: FnOnce(&WindowContext) -> T>(&self, window_id: usize, f: F) -> T {
BorrowWindowContext::read_with(&*self.view_context, window_id, f)
}
@ -4013,7 +4021,7 @@ impl<T: View> ViewHandle<T> {
cx.read_view(self)
}
pub fn read_with<C, F, S>(&self, cx: &C, read: F) -> S
pub fn read_with<C, F, S>(&self, cx: &C, read: F) -> C::Return<S>
where
C: BorrowWindowContext,
F: FnOnce(&T, &ViewContext<T>) -> S,
@ -4024,7 +4032,7 @@ impl<T: View> ViewHandle<T> {
})
}
pub fn update<C, F, S>(&self, cx: &mut C, update: F) -> S
pub fn update<C, F, S>(&self, cx: &mut C, update: F) -> C::Return<S>
where
C: BorrowWindowContext,
F: FnOnce(&mut T, &mut ViewContext<T>) -> S,

View file

@ -406,6 +406,8 @@ impl BorrowAppContext for TestAppContext {
}
impl BorrowWindowContext for TestAppContext {
type Return<T> = T;
fn read_with<T, F: FnOnce(&WindowContext) -> T>(&self, window_id: usize, f: F) -> T {
self.cx
.borrow()

View file

@ -142,6 +142,8 @@ impl BorrowAppContext for WindowContext<'_> {
}
impl BorrowWindowContext for WindowContext<'_> {
type Return<T> = T;
fn read_with<T, F: FnOnce(&WindowContext) -> T>(&self, window_id: usize, f: F) -> T {
if self.window_id == window_id {
f(self)