This commit is contained in:
Mikayla 2023-11-09 00:18:00 -08:00
parent 43eb7f28d1
commit 656eb9d455
No known key found for this signature in database
9 changed files with 451 additions and 249 deletions

File diff suppressed because it is too large Load diff

View file

@ -5,6 +5,7 @@ use std::{
};
use anyhow::Result;
use serde_json::json;
use crate::{Editor, ToPoint};
use collections::HashSet;

View file

@ -1011,7 +1011,7 @@ impl Context for AppContext {
fn read_window<T, R>(
&self,
window: &WindowHandle<T>,
read: impl FnOnce(&T, &AppContext) -> R,
read: impl FnOnce(View<T>, &AppContext) -> R,
) -> Result<R>
where
T: 'static,
@ -1028,7 +1028,7 @@ impl Context for AppContext {
.downcast::<T>()
.map_err(|_| anyhow!("root view's type has changed"))?;
Ok(read(view.read(self), self))
Ok(read(view, self))
}
}

View file

@ -70,7 +70,7 @@ impl Context for AsyncAppContext {
fn read_window<T, R>(
&self,
window: &WindowHandle<T>,
read: impl FnOnce(&T, &AppContext) -> R,
read: impl FnOnce(View<T>, &AppContext) -> R,
) -> Result<R>
where
T: 'static,
@ -267,7 +267,7 @@ impl Context for AsyncWindowContext {
fn read_window<T, R>(
&self,
window: &WindowHandle<T>,
read: impl FnOnce(&T, &AppContext) -> R,
read: impl FnOnce(View<T>, &AppContext) -> R,
) -> Result<R>
where
T: 'static,

View file

@ -1,6 +1,6 @@
use crate::{
AnyView, AnyWindowHandle, AppContext, AsyncAppContext, Context, Effect, Entity, EntityId,
EventEmitter, Model, Subscription, Task, WeakModel, WindowContext, WindowHandle,
EventEmitter, Model, Subscription, Task, View, WeakModel, WindowContext, WindowHandle,
};
use anyhow::Result;
use derive_more::{Deref, DerefMut};
@ -243,7 +243,7 @@ impl<'a, T> Context for ModelContext<'a, T> {
fn read_window<U, R>(
&self,
window: &WindowHandle<U>,
read: impl FnOnce(&U, &AppContext) -> R,
read: impl FnOnce(View<U>, &AppContext) -> R,
) -> Result<R>
where
U: 'static,

View file

@ -1,8 +1,8 @@
use crate::{
AnyView, AnyWindowHandle, AppCell, AppContext, AsyncAppContext, BackgroundExecutor, Context,
EventEmitter, ForegroundExecutor, InputEvent, KeyDownEvent, Keystroke, Model, ModelContext,
Render, Result, Task, TestDispatcher, TestPlatform, ViewContext, VisualContext, WindowContext,
WindowHandle, WindowOptions,
Render, Result, Task, TestDispatcher, TestPlatform, View, ViewContext, VisualContext,
WindowContext, WindowHandle, WindowOptions,
};
use anyhow::{anyhow, bail};
use futures::{Stream, StreamExt};
@ -62,7 +62,7 @@ impl Context for TestAppContext {
fn read_window<T, R>(
&self,
window: &WindowHandle<T>,
read: impl FnOnce(&T, &AppContext) -> R,
read: impl FnOnce(View<T>, &AppContext) -> R,
) -> Result<R>
where
T: 'static,
@ -276,3 +276,105 @@ impl<T: Send> Model<T> {
.expect("model was dropped")
}
}
use derive_more::{Deref, DerefMut};
#[derive(Deref, DerefMut)]
pub struct VisualTestContext<'a> {
#[deref]
#[deref_mut]
cx: &'a mut TestAppContext,
window: AnyWindowHandle,
}
impl<'a> VisualTestContext<'a> {
pub fn from_window(window: AnyWindowHandle, cx: &'a mut TestAppContext) -> Self {
Self { cx, window }
}
}
impl<'a> Context for VisualTestContext<'a> {
type Result<T> = <TestAppContext as Context>::Result<T>;
fn build_model<T: 'static>(
&mut self,
build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
) -> Self::Result<Model<T>> {
self.cx.build_model(build_model)
}
fn update_model<T, R>(
&mut self,
handle: &Model<T>,
update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R,
) -> Self::Result<R>
where
T: 'static,
{
self.cx.update_model(handle, update)
}
fn read_model<T, R>(
&self,
handle: &Model<T>,
read: impl FnOnce(&T, &AppContext) -> R,
) -> Self::Result<R>
where
T: 'static,
{
self.cx.read_model(handle, read)
}
fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
where
F: FnOnce(AnyView, &mut WindowContext<'_>) -> T,
{
self.cx.update_window(window, f)
}
fn read_window<T, R>(
&self,
window: &WindowHandle<T>,
read: impl FnOnce(View<T>, &AppContext) -> R,
) -> Result<R>
where
T: 'static,
{
self.cx.read_window(window, read)
}
}
impl<'a> VisualContext for VisualTestContext<'a> {
fn build_view<V>(
&mut self,
build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static + Render,
{
self.window
.update(self.cx, |_, cx| cx.build_view(build_view))
.unwrap()
}
fn update_view<V: 'static, R>(
&mut self,
view: &View<V>,
update: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R,
) -> Self::Result<R> {
self.window
.update(self.cx, |_, cx| cx.update_view(view, update))
.unwrap()
}
fn replace_root_view<V>(
&mut self,
build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: Render,
{
self.window
.update(self.cx, |_, cx| cx.replace_root_view(build_view))
.unwrap()
}
}

View file

@ -141,7 +141,7 @@ impl TryFrom<&'_ str> for Rgba {
}
}
#[derive(Default, Copy, Clone, Debug, PartialEq)]
#[derive(Default, Copy, Clone, Debug, PartialEq, PartialOrd)]
#[repr(C)]
pub struct Hsla {
pub h: f32,

View file

@ -108,7 +108,7 @@ pub trait Context {
fn read_window<T, R>(
&self,
window: &WindowHandle<T>,
read: impl FnOnce(&T, &AppContext) -> R,
read: impl FnOnce(View<T>, &AppContext) -> R,
) -> Result<R>
where
T: 'static;

View file

@ -1432,7 +1432,7 @@ impl Context for WindowContext<'_> {
fn read_window<T, R>(
&self,
window: &WindowHandle<T>,
read: impl FnOnce(&T, &AppContext) -> R,
read: impl FnOnce(View<T>, &AppContext) -> R,
) -> Result<R>
where
T: 'static,
@ -1445,7 +1445,7 @@ impl Context for WindowContext<'_> {
.unwrap()
.downcast::<T>()
.map_err(|_| anyhow!("the type of the window's root view has changed"))?;
Ok(read(root_view.read(self), self))
Ok(read(root_view, self))
} else {
self.app.read_window(window, read)
}
@ -1769,6 +1769,10 @@ impl<'a, V: 'static> ViewContext<'a, V> {
}
}
pub fn entity_id(&self) -> EntityId {
self.view.entity_id()
}
pub fn view(&self) -> &View<V> {
self.view
}
@ -2269,7 +2273,7 @@ impl<V> Context for ViewContext<'_, V> {
fn read_window<T, R>(
&self,
window: &WindowHandle<T>,
read: impl FnOnce(&T, &AppContext) -> R,
read: impl FnOnce(View<T>, &AppContext) -> R,
) -> Result<R>
where
T: 'static,
@ -2348,7 +2352,7 @@ impl<V: 'static + Render> WindowHandle<V> {
}
pub fn update<C, R>(
self,
&self,
cx: &mut C,
update: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R,
) -> Result<R>
@ -2379,11 +2383,18 @@ impl<V: 'static + Render> WindowHandle<V> {
Ok(x.read(cx))
}
pub fn read_with<C, R>(self, cx: &C, read_with: impl FnOnce(&V, &AppContext) -> R) -> Result<R>
pub fn read_with<C, R>(&self, cx: &C, read_with: impl FnOnce(&V, &AppContext) -> R) -> Result<R>
where
C: Context,
{
cx.read_window(&self, |root_view: &V, cx| read_with(root_view, cx))
cx.read_window(self, |root_view, cx| read_with(root_view.read(cx), cx))
}
pub fn root_view<C>(&self, cx: &C) -> Result<View<V>>
where
C: Context,
{
cx.read_window(self, |root_view, _cx| root_view.clone())
}
}
@ -2451,7 +2462,7 @@ impl AnyWindowHandle {
cx.update_window(self, update)
}
pub fn read<T, C, R>(self, cx: &C, read: impl FnOnce(&T, &AppContext) -> R) -> Result<R>
pub fn read<T, C, R>(self, cx: &C, read: impl FnOnce(View<T>, &AppContext) -> R) -> Result<R>
where
C: Context,
T: 'static,