Remove ReadView and UpdateView traits

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2023-04-24 17:36:14 +02:00
parent a6115d9330
commit c165fb9be5
12 changed files with 160 additions and 183 deletions

View file

@ -126,26 +126,19 @@ pub trait BorrowAppContext {
fn update<T, F: FnOnce(&mut AppContext) -> T>(&mut self, f: F) -> T;
}
pub trait ReadViewWith {
fn read_view_with<V, T>(
pub trait BorrowWindowContext {
type ReturnValue<T>;
fn read_with<T, F: FnOnce(&WindowContext) -> T>(
&self,
handle: &ViewHandle<V>,
read: &mut dyn FnMut(&V, &AppContext) -> T,
) -> T
where
V: View;
}
pub trait UpdateView {
type Output<S>;
fn update_view<T, S>(
window_id: usize,
f: F,
) -> Self::ReturnValue<T>;
fn update<T, F: FnOnce(&mut WindowContext) -> T>(
&mut self,
handle: &ViewHandle<T>,
update: &mut dyn FnMut(&mut T, &mut ViewContext<T>) -> S,
) -> Self::Output<S>
where
T: View;
window_id: usize,
f: F,
) -> Self::ReturnValue<T>;
}
#[derive(Clone)]
@ -388,36 +381,25 @@ impl BorrowAppContext for AsyncAppContext {
}
}
impl UpdateView for AsyncAppContext {
type Output<S> = Result<S>;
impl BorrowWindowContext for AsyncAppContext {
type ReturnValue<T> = Result<T>;
fn update_view<T, S>(
&mut self,
handle: &ViewHandle<T>,
update: &mut dyn FnMut(&mut T, &mut ViewContext<T>) -> S,
) -> Result<S>
where
T: View,
{
fn read_with<T, F: FnOnce(&WindowContext) -> T>(&self, window_id: usize, f: F) -> Result<T> {
self.0
.borrow_mut()
.update_window(handle.window_id, |cx| cx.update_view(handle, update))
.borrow()
.read_window(window_id, f)
.ok_or_else(|| anyhow!("window was closed"))
}
}
impl ReadViewWith for AsyncAppContext {
fn read_view_with<V, T>(
&self,
handle: &ViewHandle<V>,
read: &mut dyn FnMut(&V, &AppContext) -> T,
) -> T
where
V: View,
{
let cx = self.0.borrow();
let cx = &*cx;
read(handle.read(cx), cx)
fn update<T, F: FnOnce(&mut WindowContext) -> T>(
&mut self,
window_id: usize,
f: F,
) -> Result<T> {
self.0
.borrow_mut()
.update_window(window_id, f)
.ok_or_else(|| anyhow!("window was closed"))
}
}
@ -3349,26 +3331,23 @@ impl<'a, 'b, V: View> ViewContext<'a, 'b, V> {
impl<V> BorrowAppContext for ViewContext<'_, '_, V> {
fn read_with<T, F: FnOnce(&AppContext) -> T>(&self, f: F) -> T {
self.window_context.read_with(f)
BorrowAppContext::read_with(&*self.window_context, f)
}
fn update<T, F: FnOnce(&mut AppContext) -> T>(&mut self, f: F) -> T {
self.window_context.update(f)
BorrowAppContext::update(&mut *self.window_context, f)
}
}
impl<V: View> UpdateView for ViewContext<'_, '_, V> {
type Output<S> = S;
impl<V> BorrowWindowContext for ViewContext<'_, '_, V> {
type ReturnValue<T> = T;
fn update_view<T, S>(
&mut self,
handle: &ViewHandle<T>,
update: &mut dyn FnMut(&mut T, &mut ViewContext<T>) -> S,
) -> S
where
T: View,
{
self.window_context.update_view(handle, update)
fn read_with<T, F: FnOnce(&WindowContext) -> T>(&self, window_id: usize, f: F) -> T {
BorrowWindowContext::read_with(&*self.window_context, window_id, f)
}
fn update<T, F: FnOnce(&mut WindowContext) -> T>(&mut self, window_id: usize, f: F) -> T {
BorrowWindowContext::update(&mut *self.window_context, window_id, f)
}
}
@ -3406,26 +3385,23 @@ impl<V: View> DerefMut for EventContext<'_, '_, '_, V> {
impl<V: View> BorrowAppContext for EventContext<'_, '_, '_, V> {
fn read_with<T, F: FnOnce(&AppContext) -> T>(&self, f: F) -> T {
self.view_context.read_with(f)
BorrowAppContext::read_with(&*self.view_context, f)
}
fn update<T, F: FnOnce(&mut AppContext) -> T>(&mut self, f: F) -> T {
self.view_context.update(f)
BorrowAppContext::update(&mut *self.view_context, f)
}
}
impl<V: View> UpdateView for EventContext<'_, '_, '_, V> {
type Output<S> = S;
impl<V: View> BorrowWindowContext for EventContext<'_, '_, '_, V> {
type ReturnValue<T> = T;
fn update_view<T, S>(
&mut self,
handle: &ViewHandle<T>,
update: &mut dyn FnMut(&mut T, &mut ViewContext<T>) -> S,
) -> S
where
T: View,
{
self.view_context.update_view(handle, update)
fn read_with<T, F: FnOnce(&WindowContext) -> T>(&self, window_id: usize, f: F) -> T {
BorrowWindowContext::read_with(&*self.view_context, window_id, f)
}
fn update<T, F: FnOnce(&mut WindowContext) -> T>(&mut self, window_id: usize, f: F) -> T {
BorrowWindowContext::update(&mut *self.view_context, window_id, f)
}
}
@ -3756,27 +3732,29 @@ 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::ReturnValue<S>
where
C: ReadViewWith,
F: FnOnce(&T, &AppContext) -> S,
C: BorrowWindowContext,
F: FnOnce(&T, &ViewContext<T>) -> S,
{
let mut read = Some(read);
cx.read_view_with(self, &mut |view, cx| {
let read = read.take().unwrap();
read(view, cx)
cx.read_with(self.window_id, |cx| {
let cx = ViewContext::immutable(cx, self.view_id);
read(cx.read_view(self), &cx)
})
}
pub fn update<C, F, S>(&self, cx: &mut C, update: F) -> C::Output<S>
pub fn update<C, F, S>(&self, cx: &mut C, update: F) -> C::ReturnValue<S>
where
C: UpdateView,
C: BorrowWindowContext,
F: FnOnce(&mut T, &mut ViewContext<T>) -> S,
{
let mut update = Some(update);
cx.update_view(self, &mut |view, cx| {
let update = update.take().unwrap();
update(view, cx)
cx.update(self.window_id, |cx| {
cx.update_view(self, &mut |view, cx| {
let update = update.take().unwrap();
update(view, cx)
})
})
}

View file

@ -1,3 +1,18 @@
use crate::{
executor,
geometry::vector::Vector2F,
keymap_matcher::Keystroke,
platform,
platform::{Event, InputHandler, KeyDownEvent, Platform},
Action, AnyViewHandle, AppContext, BorrowAppContext, BorrowWindowContext, Entity, FontCache,
Handle, ModelContext, ModelHandle, Subscription, Task, View, ViewContext, ViewHandle,
WeakHandle, WindowContext,
};
use collections::BTreeMap;
use futures::Future;
use itertools::Itertools;
use parking_lot::{Mutex, RwLock};
use smol::stream::StreamExt;
use std::{
any::Any,
cell::RefCell,
@ -11,23 +26,6 @@ use std::{
time::Duration,
};
use futures::Future;
use itertools::Itertools;
use parking_lot::{Mutex, RwLock};
use smol::stream::StreamExt;
use crate::{
executor,
geometry::vector::Vector2F,
keymap_matcher::Keystroke,
platform,
platform::{Event, InputHandler, KeyDownEvent, Platform},
Action, AnyViewHandle, AppContext, BorrowAppContext, Entity, FontCache, Handle, ModelContext,
ModelHandle, ReadViewWith, Subscription, Task, UpdateView, View, ViewContext, ViewHandle,
WeakHandle, WindowContext,
};
use collections::BTreeMap;
use super::{
ref_counts::LeakDetector, window_input_handler::WindowInputHandler, AsyncAppContext, RefCounts,
};
@ -391,36 +389,21 @@ impl BorrowAppContext for TestAppContext {
}
}
impl UpdateView for TestAppContext {
type Output<S> = S;
impl BorrowWindowContext for TestAppContext {
type ReturnValue<T> = T;
fn update_view<T, S>(
&mut self,
handle: &ViewHandle<T>,
update: &mut dyn FnMut(&mut T, &mut ViewContext<T>) -> S,
) -> S
where
T: View,
{
fn read_with<T, F: FnOnce(&WindowContext) -> T>(&self, window_id: usize, f: F) -> T {
self.cx
.borrow()
.read_window(window_id, f)
.expect("window was closed")
}
fn update<T, F: FnOnce(&mut WindowContext) -> T>(&mut self, window_id: usize, f: F) -> T {
self.cx
.borrow_mut()
.update_window(handle.window_id, |cx| cx.update_view(handle, update))
.unwrap()
}
}
impl ReadViewWith for TestAppContext {
fn read_view_with<V, T>(
&self,
handle: &ViewHandle<V>,
read: &mut dyn FnMut(&V, &AppContext) -> T,
) -> T
where
V: View,
{
let cx = self.cx.borrow();
let cx = &*cx;
read(handle.read(cx), cx)
.update_window(window_id, f)
.expect("window was closed")
}
}

View file

@ -13,9 +13,9 @@ use crate::{
},
text_layout::TextLayoutCache,
util::post_inc,
Action, AnyView, AnyViewHandle, AppContext, BorrowAppContext, Effect, Element, Entity, Handle,
MouseRegion, MouseRegionId, ParentId, SceneBuilder, Subscription, UpdateView, View,
ViewContext, ViewHandle, WindowInvalidation,
Action, AnyView, AnyViewHandle, AppContext, BorrowAppContext, BorrowWindowContext, Effect,
Element, Entity, Handle, MouseRegion, MouseRegionId, ParentId, SceneBuilder, Subscription,
View, ViewContext, ViewHandle, WindowInvalidation,
};
use anyhow::{anyhow, bail, Result};
use collections::{HashMap, HashSet};
@ -141,27 +141,23 @@ impl BorrowAppContext for WindowContext<'_> {
}
}
impl UpdateView for WindowContext<'_> {
type Output<S> = S;
impl BorrowWindowContext for WindowContext<'_> {
type ReturnValue<T> = T;
fn update_view<T, S>(
&mut self,
handle: &ViewHandle<T>,
update: &mut dyn FnMut(&mut T, &mut ViewContext<T>) -> S,
) -> S
where
T: View,
{
self.update_any_view(handle.view_id, |view, cx| {
let mut cx = ViewContext::mutable(cx, handle.view_id);
update(
view.as_any_mut()
.downcast_mut()
.expect("downcast is type safe"),
&mut cx,
)
})
.expect("view is already on the stack")
fn read_with<T, F: FnOnce(&WindowContext) -> T>(&self, window_id: usize, f: F) -> T {
if self.window_id == window_id {
f(self)
} else {
panic!("read_with called with id of window that does not belong to this context")
}
}
fn update<T, F: FnOnce(&mut WindowContext) -> T>(&mut self, window_id: usize, f: F) -> T {
if self.window_id == window_id {
f(self)
} else {
panic!("update called with id of window that does not belong to this context")
}
}
}
@ -225,6 +221,26 @@ impl<'a> WindowContext<'a> {
Some(result)
}
pub(crate) fn update_view<T, S>(
&mut self,
handle: &ViewHandle<T>,
update: &mut dyn FnMut(&mut T, &mut ViewContext<T>) -> S,
) -> S
where
T: View,
{
self.update_any_view(handle.view_id, |view, cx| {
let mut cx = ViewContext::mutable(cx, handle.view_id);
update(
view.as_any_mut()
.downcast_mut()
.expect("downcast is type safe"),
&mut cx,
)
})
.expect("view is already on the stack")
}
pub fn defer(&mut self, callback: impl 'static + FnOnce(&mut WindowContext)) {
let window_id = self.window_id;
self.app_context.defer(move |cx| {