This commit is contained in:
Nathan Sobo
2023-10-31 22:32:18 -06:00
parent 4da8ee1e1d
commit d47ef6470b
9 changed files with 358 additions and 207 deletions
+24 -24
View File
@@ -267,30 +267,6 @@ impl AppContext {
.collect()
}
pub(crate) fn update_window<R>(
&mut self,
handle: AnyWindowHandle,
update: impl FnOnce(&mut WindowContext) -> R,
) -> Result<R> {
self.update(|cx| {
let mut window = cx
.windows
.get_mut(handle.id)
.ok_or_else(|| anyhow!("window not found"))?
.take()
.unwrap();
let result = update(&mut WindowContext::new(cx, &mut window));
cx.windows
.get_mut(handle.id)
.ok_or_else(|| anyhow!("window not found"))?
.replace(window);
Ok(result)
})
}
pub fn update_window_root<V, R>(
&mut self,
handle: &WindowHandle<V>,
@@ -753,6 +729,7 @@ impl AppContext {
}
impl Context for AppContext {
type WindowContext<'a> = WindowContext<'a>;
type ModelContext<'a, T> = ModelContext<'a, T>;
type Result<T> = T;
@@ -784,6 +761,29 @@ impl Context for AppContext {
result
})
}
fn update_window<T, F>(&mut self, handle: AnyWindowHandle, update: F) -> Result<T>
where
F: FnOnce(&mut Self::WindowContext<'_>) -> T,
{
self.update(|cx| {
let mut window = cx
.windows
.get_mut(handle.id)
.ok_or_else(|| anyhow!("window not found"))?
.take()
.unwrap();
let result = update(&mut WindowContext::new(cx, &mut window));
cx.windows
.get_mut(handle.id)
.ok_or_else(|| anyhow!("window not found"))?
.replace(window);
Ok(result)
})
}
}
impl<C> MainThread<C>
+32 -4
View File
@@ -1,6 +1,6 @@
use crate::{
AnyWindowHandle, AppContext, Context, Executor, MainThread, Model, ModelContext, Result, Task,
View, ViewContext, VisualContext, WindowContext, WindowHandle,
AnyWindowHandle, AppContext, Context, Executor, MainThread, Model, ModelContext, Render,
Result, Task, View, ViewContext, VisualContext, WindowContext, WindowHandle,
};
use anyhow::Context as _;
use derive_more::{Deref, DerefMut};
@@ -14,6 +14,7 @@ pub struct AsyncAppContext {
}
impl Context for AsyncAppContext {
type WindowContext<'a> = WindowContext<'a>;
type ModelContext<'a, T> = ModelContext<'a, T>;
type Result<T> = Result<T>;
@@ -38,6 +39,13 @@ impl Context for AsyncAppContext {
let mut lock = app.lock(); // Need this to compile
Ok(lock.update_model(handle, update))
}
fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
where
F: FnOnce(&mut Self::WindowContext<'_>) -> T,
{
todo!()
}
}
impl AsyncAppContext {
@@ -60,12 +68,12 @@ impl AsyncAppContext {
pub fn update_window<R>(
&self,
handle: AnyWindowHandle,
window: AnyWindowHandle,
update: impl FnOnce(&mut WindowContext) -> R,
) -> Result<R> {
let app = self.app.upgrade().context("app was released")?;
let mut app_context = app.lock();
app_context.update_window(handle, update)
app_context.update_window(window, update)
}
pub fn update_window_root<V, R>(
@@ -224,7 +232,9 @@ impl AsyncWindowContext {
}
impl Context for AsyncWindowContext {
type WindowContext<'a> = WindowContext<'a>;
type ModelContext<'a, T> = ModelContext<'a, T>;
type Result<T> = Result<T>;
fn build_model<T>(
@@ -246,6 +256,13 @@ impl Context for AsyncWindowContext {
self.app
.update_window(self.window, |cx| cx.update_model(handle, update))
}
fn update_window<T, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<T>
where
F: FnOnce(&mut Self::WindowContext<'_>) -> T,
{
self.app.update_window(window, update)
}
}
impl VisualContext for AsyncWindowContext {
@@ -270,6 +287,17 @@ impl VisualContext for AsyncWindowContext {
self.app
.update_window(self.window, |cx| cx.update_view(view, update))
}
fn replace_root_view<V>(
&mut self,
build_view: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static + Send + Render,
{
self.app
.update_window(self.window, |cx| cx.replace_root_view(build_view))
}
}
#[cfg(test)]
+1 -1
View File
@@ -1,4 +1,4 @@
use crate::{private::Sealed, AnyBox, AppContext, Context, Entity};
use crate::{private::Sealed, AnyBox, AppContext, AsyncAppContext, Context, Entity, ModelContext};
use anyhow::{anyhow, Result};
use derive_more::{Deref, DerefMut};
use parking_lot::{RwLock, RwLockUpgradableReadGuard};
+11 -2
View File
@@ -1,7 +1,8 @@
use crate::{
AppContext, AsyncAppContext, Context, Effect, Entity, EntityId, EventEmitter, MainThread,
Model, Subscription, Task, WeakModel,
AnyWindowHandle, AppContext, AsyncAppContext, Context, Effect, Entity, EntityId, EventEmitter,
MainThread, Model, Subscription, Task, WeakModel, WindowContext,
};
use anyhow::Result;
use derive_more::{Deref, DerefMut};
use futures::FutureExt;
use std::{
@@ -228,6 +229,7 @@ where
}
impl<'a, T> Context for ModelContext<'a, T> {
type WindowContext<'b> = WindowContext<'b>;
type ModelContext<'b, U> = ModelContext<'b, U>;
type Result<U> = U;
@@ -248,6 +250,13 @@ impl<'a, T> Context for ModelContext<'a, T> {
) -> R {
self.app.update_model(handle, update)
}
fn update_window<R, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<R>
where
F: FnOnce(&mut Self::WindowContext<'_>) -> R,
{
self.app.update_window(window, update)
}
}
impl<T> Borrow<AppContext> for ModelContext<'_, T> {
+9
View File
@@ -12,6 +12,7 @@ pub struct TestAppContext {
}
impl Context for TestAppContext {
type WindowContext<'a> = WindowContext<'a>;
type ModelContext<'a, T> = ModelContext<'a, T>;
type Result<T> = T;
@@ -34,6 +35,14 @@ impl Context for TestAppContext {
let mut lock = self.app.lock();
lock.update_model(handle, update)
}
fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
where
F: FnOnce(&mut Self::WindowContext<'_>) -> T,
{
let mut lock = self.app.lock();
lock.update_window(window, f)
}
}
impl TestAppContext {
+50 -3
View File
@@ -77,6 +77,7 @@ use taffy::TaffyLayoutEngine;
type AnyBox = Box<dyn Any + Send>;
pub trait Context {
type WindowContext<'a>: VisualContext;
type ModelContext<'a, T>;
type Result<T>;
@@ -87,11 +88,17 @@ pub trait Context {
where
T: 'static + Send;
fn update_model<T: 'static, R>(
fn update_model<T, R>(
&mut self,
handle: &Model<T>,
update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
) -> Self::Result<R>;
) -> Self::Result<R>
where
T: 'static;
fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
where
F: FnOnce(&mut Self::WindowContext<'_>) -> T;
}
pub trait VisualContext: Context {
@@ -99,7 +106,7 @@ pub trait VisualContext: Context {
fn build_view<V>(
&mut self,
build_view_state: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V,
build_view: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static + Send;
@@ -109,6 +116,13 @@ pub trait VisualContext: Context {
view: &View<V>,
update: impl FnOnce(&mut V, &mut Self::ViewContext<'_, V>) -> R,
) -> Self::Result<R>;
fn replace_root_view<V>(
&mut self,
build_view: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static + Send + Render;
}
pub trait Entity<T>: Sealed {
@@ -145,6 +159,7 @@ impl<T> DerefMut for MainThread<T> {
}
impl<C: Context> Context for MainThread<C> {
type WindowContext<'a> = MainThread<C::WindowContext<'a>>;
type ModelContext<'a, T> = MainThread<C::ModelContext<'a, T>>;
type Result<T> = C::Result<T>;
@@ -181,6 +196,20 @@ impl<C: Context> Context for MainThread<C> {
update(entity, cx)
})
}
fn update_window<T, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<T>
where
F: FnOnce(&mut Self::WindowContext<'_>) -> T,
{
self.0.update_window(window, |cx| {
let cx = unsafe {
mem::transmute::<&mut C::WindowContext<'_>, &mut MainThread<C::WindowContext<'_>>>(
cx,
)
};
update(cx)
})
}
}
impl<C: VisualContext> VisualContext for MainThread<C> {
@@ -219,6 +248,24 @@ impl<C: VisualContext> VisualContext for MainThread<C> {
update(view_state, cx)
})
}
fn replace_root_view<V>(
&mut self,
build_view: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static + Send + Render,
{
self.0.replace_root_view(|cx| {
let cx = unsafe {
mem::transmute::<
&mut C::ViewContext<'_, V>,
&mut MainThread<C::ViewContext<'_, V>>,
>(cx)
};
build_view(cx)
})
}
}
pub trait BorrowAppContext {
+64 -14
View File
@@ -5,7 +5,7 @@ use crate::{
Hsla, ImageData, InputEvent, IsZero, KeyListener, KeyMatch, KeyMatcher, Keystroke, LayoutId,
MainThread, MainThreadOnly, Model, ModelContext, Modifiers, MonochromeSprite, MouseButton,
MouseDownEvent, MouseMoveEvent, MouseUpEvent, Path, Pixels, PlatformAtlas, PlatformWindow,
Point, PolychromeSprite, Quad, RenderGlyphParams, RenderImageParams, RenderSvgParams,
Point, PolychromeSprite, Quad, Render, RenderGlyphParams, RenderImageParams, RenderSvgParams,
ScaledPixels, SceneBuilder, Shadow, SharedString, Size, Style, Subscription, TaffyLayoutEngine,
Task, Underline, UnderlineStyle, View, VisualContext, WeakView, WindowOptions,
SUBPIXEL_VARIANTS,
@@ -315,6 +315,8 @@ impl<'a> WindowContext<'a> {
Self { app, window }
}
// fn replace_root(&mut )
/// Obtain a handle to the window that belongs to this context.
pub fn window_handle(&self) -> AnyWindowHandle {
self.window.handle
@@ -1264,6 +1266,7 @@ impl<'a> WindowContext<'a> {
}
impl Context for WindowContext<'_> {
type WindowContext<'a> = WindowContext<'a>;
type ModelContext<'a, T> = ModelContext<'a, T>;
type Result<T> = T;
@@ -1292,6 +1295,17 @@ impl Context for WindowContext<'_> {
self.entities.end_lease(entity);
result
}
fn update_window<T, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<T>
where
F: FnOnce(&mut Self::WindowContext<'_>) -> T,
{
if window == self.window.handle {
Ok(update(self))
} else {
self.app.update_window(window, update)
}
}
}
impl VisualContext for WindowContext<'_> {
@@ -1326,6 +1340,24 @@ impl VisualContext for WindowContext<'_> {
cx.app.entities.end_lease(lease);
result
}
fn replace_root_view<V>(
&mut self,
build_view: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static + Send + Render,
{
let slot = self.app.entities.reserve();
let view = View {
model: slot.clone(),
};
let mut cx = ViewContext::new(&mut *self.app, &mut *self.window, &view);
let entity = build_view(&mut cx);
self.entities.insert(slot, entity);
self.window.root_view = Some(view.clone().into());
view
}
}
impl<'a> std::ops::Deref for WindowContext<'a> {
@@ -1900,6 +1932,7 @@ impl<V: 'static> MainThread<ViewContext<'_, V>> {
}
impl<V> Context for ViewContext<'_, V> {
type WindowContext<'a> = WindowContext<'a>;
type ModelContext<'b, U> = ModelContext<'b, U>;
type Result<U> = U;
@@ -1920,6 +1953,13 @@ impl<V> Context for ViewContext<'_, V> {
) -> R {
self.window_cx.update_model(model, update)
}
fn update_window<T, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<T>
where
F: FnOnce(&mut Self::WindowContext<'_>) -> T,
{
self.window_cx.update_window(window, update)
}
}
impl<V: 'static> VisualContext for ViewContext<'_, V> {
@@ -1939,6 +1979,16 @@ impl<V: 'static> VisualContext for ViewContext<'_, V> {
) -> Self::Result<R> {
self.window_cx.update_view(view, update)
}
fn replace_root_view<W>(
&mut self,
build_view: impl FnOnce(&mut Self::ViewContext<'_, W>) -> W,
) -> Self::Result<View<W>>
where
W: 'static + Send + Render,
{
self.window_cx.replace_root_view(build_view)
}
}
impl<'a, V> std::ops::Deref for ViewContext<'a, V> {
@@ -1972,18 +2022,7 @@ pub struct WindowHandle<V> {
state_type: PhantomData<V>,
}
impl<V> Copy for WindowHandle<V> {}
impl<V> Clone for WindowHandle<V> {
fn clone(&self) -> Self {
WindowHandle {
any_handle: self.any_handle,
state_type: PhantomData,
}
}
}
impl<V: 'static> WindowHandle<V> {
impl<V: 'static + Render> WindowHandle<V> {
pub fn new(id: WindowId) -> Self {
WindowHandle {
any_handle: AnyWindowHandle {
@@ -1994,7 +2033,7 @@ impl<V: 'static> WindowHandle<V> {
}
}
pub fn update<R>(
pub fn update_root<R>(
&self,
cx: &mut AppContext,
update: impl FnOnce(&mut V, &mut ViewContext<V>) -> R,
@@ -2012,6 +2051,17 @@ impl<V: 'static> WindowHandle<V> {
}
}
impl<V> Copy for WindowHandle<V> {}
impl<V> Clone for WindowHandle<V> {
fn clone(&self) -> Self {
WindowHandle {
any_handle: self.any_handle,
state_type: PhantomData,
}
}
}
impl<V: 'static> Into<AnyWindowHandle> for WindowHandle<V> {
fn into(self) -> AnyWindowHandle {
self.any_handle