checkpoint

This commit is contained in:
Mikayla 2023-11-08 23:16:04 -08:00
parent f569628088
commit 43eb7f28d1
No known key found for this signature in database
22 changed files with 591 additions and 1307 deletions

View file

@ -1008,11 +1008,14 @@ impl Context for AppContext {
read(entity, self)
}
fn read_window<R>(
fn read_window<T, R>(
&self,
window: &AnyWindowHandle,
read: impl FnOnce(AnyView, &AppContext) -> R,
) -> Result<R> {
window: &WindowHandle<T>,
read: impl FnOnce(&T, &AppContext) -> R,
) -> Result<R>
where
T: 'static,
{
let window = self
.windows
.get(window.id)
@ -1021,7 +1024,11 @@ impl Context for AppContext {
.unwrap();
let root_view = window.root_view.clone().unwrap();
Ok(read(root_view, self))
let view = root_view
.downcast::<T>()
.map_err(|_| anyhow!("root view's type has changed"))?;
Ok(read(view.read(self), self))
}
}

View file

@ -67,11 +67,14 @@ impl Context for AsyncAppContext {
lock.update_window(window, f)
}
fn read_window<R>(
fn read_window<T, R>(
&self,
window: &AnyWindowHandle,
read: impl FnOnce(AnyView, &AppContext) -> R,
) -> Result<R> {
window: &WindowHandle<T>,
read: impl FnOnce(&T, &AppContext) -> R,
) -> Result<R>
where
T: 'static,
{
let app = self.app.upgrade().context("app was released")?;
let lock = app.borrow();
lock.read_window(window, read)
@ -261,11 +264,14 @@ impl Context for AsyncWindowContext {
self.app.read_model(handle, read)
}
fn read_window<R>(
fn read_window<T, R>(
&self,
window: &AnyWindowHandle,
read: impl FnOnce(AnyView, &AppContext) -> R,
) -> Result<R> {
window: &WindowHandle<T>,
read: impl FnOnce(&T, &AppContext) -> R,
) -> Result<R>
where
T: 'static,
{
self.app.read_window(window, read)
}
}

View file

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

View file

@ -59,11 +59,14 @@ impl Context for TestAppContext {
app.read_model(handle, read)
}
fn read_window<R>(
fn read_window<T, R>(
&self,
window: &AnyWindowHandle,
read: impl FnOnce(AnyView, &AppContext) -> R,
) -> Result<R> {
window: &WindowHandle<T>,
read: impl FnOnce(&T, &AppContext) -> R,
) -> Result<R>
where
T: 'static,
{
let app = self.app.borrow();
app.read_window(window, read)
}
@ -102,8 +105,8 @@ impl TestAppContext {
Ok(())
}
pub fn executor(&self) -> &BackgroundExecutor {
&self.background_executor
pub fn executor(&self) -> BackgroundExecutor {
self.background_executor.clone()
}
pub fn foreground_executor(&self) -> &ForegroundExecutor {

View file

@ -154,6 +154,30 @@ impl Hsla {
pub fn to_rgb(self) -> Rgba {
self.into()
}
pub fn red() -> Self {
red()
}
pub fn green() -> Self {
green()
}
pub fn blue() -> Self {
blue()
}
pub fn black() -> Self {
black()
}
pub fn white() -> Self {
white()
}
pub fn transparent_black() -> Self {
transparent_black()
}
}
impl Eq for Hsla {}
@ -212,6 +236,15 @@ pub fn blue() -> Hsla {
}
}
pub fn green() -> Hsla {
Hsla {
h: 0.3,
s: 1.,
l: 0.5,
a: 1.,
}
}
impl Hsla {
/// Returns true if the HSLA color is fully transparent, false otherwise.
pub fn is_transparent(&self) -> bool {

View file

@ -105,11 +105,13 @@ pub trait Context {
where
F: FnOnce(AnyView, &mut WindowContext<'_>) -> T;
fn read_window<R>(
fn read_window<T, R>(
&self,
window: &AnyWindowHandle,
read: impl FnOnce(AnyView, &AppContext) -> R,
) -> Result<R>;
window: &WindowHandle<T>,
read: impl FnOnce(&T, &AppContext) -> R,
) -> Result<R>
where
T: 'static;
}
pub trait VisualContext: Context {

View file

@ -10,7 +10,7 @@ use crate::{
SharedString, Size, Style, SubscriberSet, Subscription, TaffyLayoutEngine, Task, Underline,
UnderlineStyle, View, VisualContext, WeakView, WindowBounds, WindowOptions, SUBPIXEL_VARIANTS,
};
use anyhow::{anyhow, Result};
use anyhow::{anyhow, Context as _, Result};
use collections::HashMap;
use derive_more::{Deref, DerefMut};
use futures::{
@ -1429,16 +1429,25 @@ impl Context for WindowContext<'_> {
read(&*entity, &*self.app)
}
fn read_window<R>(
fn read_window<T, R>(
&self,
window: &AnyWindowHandle,
read: impl FnOnce(AnyView, &AppContext) -> R,
) -> Result<R> {
if window == &self.window.handle {
let root_view = self.window.root_view.clone().unwrap();
Ok(read(root_view, self))
window: &WindowHandle<T>,
read: impl FnOnce(&T, &AppContext) -> R,
) -> Result<R>
where
T: 'static,
{
if window.any_handle == self.window.handle {
let root_view = self
.window
.root_view
.clone()
.unwrap()
.downcast::<T>()
.map_err(|_| anyhow!("the type of the window's root view has changed"))?;
Ok(read(root_view.read(self), self))
} else {
window.read(self.app, read)
self.app.read_window(window, read)
}
}
}
@ -2257,11 +2266,14 @@ impl<V> Context for ViewContext<'_, V> {
self.window_cx.read_model(handle, read)
}
fn read_window<R>(
fn read_window<T, R>(
&self,
window: &AnyWindowHandle,
read: impl FnOnce(AnyView, &AppContext) -> R,
) -> Result<R> {
window: &WindowHandle<T>,
read: impl FnOnce(&T, &AppContext) -> R,
) -> Result<R>
where
T: 'static,
{
self.window_cx.read_window(window, read)
}
}
@ -2335,14 +2347,6 @@ impl<V: 'static + Render> WindowHandle<V> {
}
}
pub fn root<C: Context>(&self, cx: &C) -> Result<View<V>> {
cx.read_window(&self.any_handle, |root_view, _| {
root_view
.downcast::<V>()
.map_err(|_| anyhow!("the type of the window's root view has changed"))
})?
}
pub fn update<C, R>(
self,
cx: &mut C,
@ -2358,6 +2362,29 @@ impl<V: 'static + Render> WindowHandle<V> {
Ok(cx.update_view(&view, update))
})?
}
pub fn read<'a>(&self, cx: &'a AppContext) -> Result<&'a V> {
let x = cx
.windows
.get(self.id)
.and_then(|window| {
window
.as_ref()
.and_then(|window| window.root_view.clone())
.map(|root_view| root_view.downcast::<V>())
})
.ok_or_else(|| anyhow!("window not found"))?
.map_err(|_| anyhow!("the type of the window's root view has changed"))?;
Ok(x.read(cx))
}
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))
}
}
impl<V> Copy for WindowHandle<V> {}
@ -2424,11 +2451,16 @@ impl AnyWindowHandle {
cx.update_window(self, update)
}
pub fn read<C, R>(self, cx: &C, read: impl FnOnce(AnyView, &AppContext) -> R) -> Result<R>
pub fn read<T, C, R>(self, cx: &C, read: impl FnOnce(&T, &AppContext) -> R) -> Result<R>
where
C: Context,
T: 'static,
{
cx.read_window(&self, read)
let view = self
.downcast::<T>()
.context("the type of the window's root view has changed")?;
cx.read_window(&view, read)
}
}