Remove more Send bounds and simplify view rendering

This commit is contained in:
Antonio Scandurra 2023-11-02 09:41:49 +01:00
parent 6cab5c2885
commit 64ad8943ba
17 changed files with 720 additions and 534 deletions

View file

@ -5,11 +5,11 @@ use crate::{
Hsla, ImageData, InputEvent, IsZero, KeyListener, KeyMatch, KeyMatcher, Keystroke, LayoutId,
Model, ModelContext, Modifiers, MonochromeSprite, MouseButton, MouseDownEvent, MouseMoveEvent,
MouseUpEvent, Path, Pixels, PlatformAtlas, PlatformWindow, Point, PolychromeSprite, Quad,
Reference, RenderGlyphParams, RenderImageParams, RenderSvgParams, ScaledPixels, SceneBuilder,
Render, RenderGlyphParams, RenderImageParams, RenderSvgParams, ScaledPixels, SceneBuilder,
Shadow, SharedString, Size, Style, Subscription, TaffyLayoutEngine, Task, Underline,
UnderlineStyle, View, VisualContext, WeakModel, WeakView, WindowOptions, SUBPIXEL_VARIANTS,
UnderlineStyle, View, VisualContext, WeakView, WindowOptions, SUBPIXEL_VARIANTS,
};
use anyhow::Result;
use anyhow::{anyhow, Result};
use collections::HashMap;
use derive_more::{Deref, DerefMut};
use parking_lot::RwLock;
@ -20,6 +20,7 @@ use std::{
borrow::{Borrow, BorrowMut, Cow},
fmt::Debug,
future::Future,
hash::{Hash, Hasher},
marker::PhantomData,
mem,
sync::{
@ -156,7 +157,7 @@ impl Drop for FocusHandle {
// Holds the state for a specific window.
pub struct Window {
handle: AnyWindowHandle,
pub(crate) handle: AnyWindowHandle,
platform_window: Box<dyn PlatformWindow>,
display_id: DisplayId,
sprite_atlas: Arc<dyn PlatformAtlas>,
@ -201,23 +202,25 @@ impl Window {
let content_size = platform_window.content_size();
let scale_factor = platform_window.scale_factor();
platform_window.on_resize(Box::new({
let cx = cx.to_async();
let mut cx = cx.to_async();
move |content_size, scale_factor| {
cx.update_window(handle, |cx| {
cx.window.scale_factor = scale_factor;
cx.window.scene_builder = SceneBuilder::new();
cx.window.content_size = content_size;
cx.window.display_id = cx.window.platform_window.display().id();
cx.window.dirty = true;
})
.log_err();
handle
.update(&mut cx, |_, cx| {
cx.window.scale_factor = scale_factor;
cx.window.scene_builder = SceneBuilder::new();
cx.window.content_size = content_size;
cx.window.display_id = cx.window.platform_window.display().id();
cx.window.dirty = true;
})
.log_err();
}
}));
platform_window.on_input({
let cx = cx.to_async();
let mut cx = cx.to_async();
Box::new(move |event| {
cx.update_window(handle, |cx| cx.dispatch_event(event))
handle
.update(&mut cx, |_, cx| cx.dispatch_event(event))
.log_err()
.unwrap_or(true)
})
@ -296,24 +299,14 @@ impl ContentMask<Pixels> {
/// Provides access to application state in the context of a single window. Derefs
/// to an `AppContext`, so you can also pass a `WindowContext` to any method that takes
/// an `AppContext` and call any `AppContext` methods.
pub struct WindowContext<'a, 'w> {
pub(crate) app: Reference<'a, AppContext>,
pub(crate) window: Reference<'w, Window>,
pub struct WindowContext<'a> {
pub(crate) app: &'a mut AppContext,
pub(crate) window: &'a mut Window,
}
impl<'a, 'w> WindowContext<'a, 'w> {
pub(crate) fn immutable(app: &'a AppContext, window: &'w Window) -> Self {
Self {
app: Reference::Immutable(app),
window: Reference::Immutable(window),
}
}
pub(crate) fn mutable(app: &'a mut AppContext, window: &'w mut Window) -> Self {
Self {
app: Reference::Mutable(app),
window: Reference::Mutable(window),
}
impl<'a> WindowContext<'a> {
pub(crate) fn new(app: &'a mut AppContext, window: &'a mut Window) -> Self {
Self { app, window }
}
/// Obtain a handle to the window that belongs to this context.
@ -345,10 +338,9 @@ impl<'a, 'w> WindowContext<'a, 'w> {
self.window.last_blur = Some(self.window.focus);
}
let window_id = self.window.handle.id;
self.window.focus = Some(handle.id);
self.app.push_effect(Effect::FocusChanged {
window_id,
window_handle: self.window.handle,
focused: Some(handle.id),
});
self.notify();
@ -360,15 +352,53 @@ impl<'a, 'w> WindowContext<'a, 'w> {
self.window.last_blur = Some(self.window.focus);
}
let window_id = self.window.handle.id;
self.window.focus = None;
self.app.push_effect(Effect::FocusChanged {
window_id,
window_handle: self.window.handle,
focused: None,
});
self.notify();
}
/// Schedules the given function to be run at the end of the current effect cycle, allowing entities
/// that are currently on the stack to be returned to the app.
pub fn defer(&mut self, f: impl FnOnce(&mut WindowContext) + 'static) {
let handle = self.window.handle;
self.app.defer(move |cx| {
handle.update(cx, |_, cx| f(cx)).ok();
});
}
pub fn subscribe<Emitter, E>(
&mut self,
entity: &E,
mut on_event: impl FnMut(E, &Emitter::Event, &mut WindowContext<'_>) + 'static,
) -> Subscription
where
Emitter: EventEmitter,
E: Entity<Emitter>,
{
let entity_id = entity.entity_id();
let entity = entity.downgrade();
let window_handle = self.window.handle;
self.app.event_listeners.insert(
entity_id,
Box::new(move |event, cx| {
window_handle
.update(cx, |_, cx| {
if let Some(handle) = E::upgrade_from(&entity) {
let event = event.downcast_ref().expect("invalid event type");
on_event(handle, event, cx);
true
} else {
false
}
})
.unwrap_or(false)
}),
)
}
/// Create an `AsyncWindowContext`, which has a static lifetime and can be held across
/// await points in async code.
pub fn to_async(&self) -> AsyncWindowContext {
@ -376,7 +406,7 @@ impl<'a, 'w> WindowContext<'a, 'w> {
}
/// Schedule the given closure to be run directly after the current frame is rendered.
pub fn on_next_frame(&mut self, f: impl FnOnce(&mut WindowContext) + Send + 'static) {
pub fn on_next_frame(&mut self, f: impl FnOnce(&mut WindowContext) + 'static) {
let f = Box::new(f);
let display_id = self.window.display_id;
@ -387,12 +417,12 @@ impl<'a, 'w> WindowContext<'a, 'w> {
return;
}
} else {
let async_cx = self.to_async();
let mut async_cx = self.to_async();
self.next_frame_callbacks.insert(display_id, vec![f]);
self.platform().set_display_link_output_callback(
display_id,
Box::new(move |_current_time, _output_time| {
let _ = async_cx.update(|cx| {
let _ = async_cx.update(|_, cx| {
let callbacks = cx
.next_frame_callbacks
.get_mut(&display_id)
@ -1114,12 +1144,12 @@ impl<'a, 'w> WindowContext<'a, 'w> {
/// is updated.
pub fn observe_global<G: 'static>(
&mut self,
f: impl Fn(&mut WindowContext<'_, '_>) + Send + 'static,
f: impl Fn(&mut WindowContext<'_>) + 'static,
) -> Subscription {
let window_id = self.window.handle.id;
let window_handle = self.window.handle;
self.global_observers.insert(
TypeId::of::<G>(),
Box::new(move |cx| cx.update_window(window_id, |cx| f(cx)).is_ok()),
Box::new(move |cx| window_handle.update(cx, |_, cx| f(cx)).is_ok()),
)
}
@ -1202,43 +1232,52 @@ impl<'a, 'w> WindowContext<'a, 'w> {
}
}
impl Context for WindowContext<'_, '_> {
type ModelContext<'a, T> = ModelContext<'a, T>;
impl Context for WindowContext<'_> {
type Result<T> = T;
fn build_model<T>(
&mut self,
build_model: impl FnOnce(&mut Self::ModelContext<'_, T>) -> T,
build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
) -> Model<T>
where
T: 'static,
{
let slot = self.app.entities.reserve();
let model = build_model(&mut ModelContext::mutable(&mut *self.app, slot.downgrade()));
let model = build_model(&mut ModelContext::new(&mut *self.app, slot.downgrade()));
self.entities.insert(slot, model)
}
fn update_model<T: 'static, R>(
&mut self,
model: &Model<T>,
update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R,
) -> R {
let mut entity = self.entities.lease(model);
let result = update(
&mut *entity,
&mut ModelContext::mutable(&mut *self.app, model.downgrade()),
&mut ModelContext::new(&mut *self.app, model.downgrade()),
);
self.entities.end_lease(entity);
result
}
fn update_window<T, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<T>
where
F: FnOnce(AnyView, &mut WindowContext<'_>) -> T,
{
if window == self.window.handle {
let root_view = self.window.root_view.clone().unwrap();
Ok(update(root_view, self))
} else {
window.update(self.app, update)
}
}
}
impl VisualContext for WindowContext<'_, '_> {
type ViewContext<'a, 'w, V> = ViewContext<'a, 'w, V>;
impl VisualContext for WindowContext<'_> {
fn build_view<V>(
&mut self,
build_view_state: impl FnOnce(&mut Self::ViewContext<'_, '_, V>) -> V,
build_view_state: impl FnOnce(&mut ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static,
@ -1247,7 +1286,7 @@ impl VisualContext for WindowContext<'_, '_> {
let view = View {
model: slot.clone(),
};
let mut cx = ViewContext::mutable(&mut *self.app, &mut *self.window, view.downgrade());
let mut cx = ViewContext::new(&mut *self.app, &mut *self.window, &view);
let entity = build_view_state(&mut cx);
self.entities.insert(slot, entity);
view
@ -1257,17 +1296,35 @@ impl VisualContext for WindowContext<'_, '_> {
fn update_view<T: 'static, R>(
&mut self,
view: &View<T>,
update: impl FnOnce(&mut T, &mut Self::ViewContext<'_, '_, T>) -> R,
update: impl FnOnce(&mut T, &mut ViewContext<'_, T>) -> R,
) -> Self::Result<R> {
let mut lease = self.app.entities.lease(&view.model);
let mut cx = ViewContext::mutable(&mut *self.app, &mut *self.window, view.downgrade());
let mut cx = ViewContext::new(&mut *self.app, &mut *self.window, &view);
let result = update(&mut *lease, &mut cx);
cx.app.entities.end_lease(lease);
result
}
fn replace_root_view<V>(
&mut self,
build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 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, 'w> std::ops::Deref for WindowContext<'a, 'w> {
impl<'a> std::ops::Deref for WindowContext<'a> {
type Target = AppContext;
fn deref(&self) -> &Self::Target {
@ -1275,19 +1332,19 @@ impl<'a, 'w> std::ops::Deref for WindowContext<'a, 'w> {
}
}
impl<'a, 'w> std::ops::DerefMut for WindowContext<'a, 'w> {
impl<'a> std::ops::DerefMut for WindowContext<'a> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.app
}
}
impl<'a, 'w> Borrow<AppContext> for WindowContext<'a, 'w> {
impl<'a> Borrow<AppContext> for WindowContext<'a> {
fn borrow(&self) -> &AppContext {
&self.app
}
}
impl<'a, 'w> BorrowMut<AppContext> for WindowContext<'a, 'w> {
impl<'a> BorrowMut<AppContext> for WindowContext<'a> {
fn borrow_mut(&mut self) -> &mut AppContext {
&mut self.app
}
@ -1455,13 +1512,13 @@ pub trait BorrowWindow: BorrowMut<Window> + BorrowMut<AppContext> {
}
}
impl Borrow<Window> for WindowContext<'_, '_> {
impl Borrow<Window> for WindowContext<'_> {
fn borrow(&self) -> &Window {
&self.window
}
}
impl BorrowMut<Window> for WindowContext<'_, '_> {
impl BorrowMut<Window> for WindowContext<'_> {
fn borrow_mut(&mut self) -> &mut Window {
&mut self.window
}
@ -1469,52 +1526,48 @@ impl BorrowMut<Window> for WindowContext<'_, '_> {
impl<T> BorrowWindow for T where T: BorrowMut<AppContext> + BorrowMut<Window> {}
pub struct ViewContext<'a, 'w, V> {
window_cx: WindowContext<'a, 'w>,
view: WeakView<V>,
pub struct ViewContext<'a, V> {
window_cx: WindowContext<'a>,
view: &'a View<V>,
}
impl<V> Borrow<AppContext> for ViewContext<'_, '_, V> {
impl<V> Borrow<AppContext> for ViewContext<'_, V> {
fn borrow(&self) -> &AppContext {
&*self.window_cx.app
}
}
impl<V> BorrowMut<AppContext> for ViewContext<'_, '_, V> {
impl<V> BorrowMut<AppContext> for ViewContext<'_, V> {
fn borrow_mut(&mut self) -> &mut AppContext {
&mut *self.window_cx.app
}
}
impl<V> Borrow<Window> for ViewContext<'_, '_, V> {
impl<V> Borrow<Window> for ViewContext<'_, V> {
fn borrow(&self) -> &Window {
&*self.window_cx.window
}
}
impl<V> BorrowMut<Window> for ViewContext<'_, '_, V> {
impl<V> BorrowMut<Window> for ViewContext<'_, V> {
fn borrow_mut(&mut self) -> &mut Window {
&mut *self.window_cx.window
}
}
impl<'a, 'w, V: 'static> ViewContext<'a, 'w, V> {
pub(crate) fn mutable(
app: &'a mut AppContext,
window: &'w mut Window,
view: WeakView<V>,
) -> Self {
impl<'a, V: 'static> ViewContext<'a, V> {
pub(crate) fn new(app: &'a mut AppContext, window: &'a mut Window, view: &'a View<V>) -> Self {
Self {
window_cx: WindowContext::mutable(app, window),
window_cx: WindowContext::new(app, window),
view,
}
}
pub fn view(&self) -> WeakView<V> {
pub fn view(&self) -> View<V> {
self.view.clone()
}
pub fn model(&self) -> WeakModel<V> {
pub fn model(&self) -> Model<V> {
self.view.model.clone()
}
@ -1525,40 +1578,50 @@ impl<'a, 'w, V: 'static> ViewContext<'a, 'w, V> {
result
}
pub fn on_next_frame(&mut self, f: impl FnOnce(&mut V, &mut ViewContext<V>) + Send + 'static)
pub fn on_next_frame(&mut self, f: impl FnOnce(&mut V, &mut ViewContext<V>) + 'static)
where
V: Any + Send,
V: 'static,
{
let view = self.view().upgrade().unwrap();
let view = self.view();
self.window_cx.on_next_frame(move |cx| view.update(cx, f));
}
/// Schedules the given function to be run at the end of the current effect cycle, allowing entities
/// that are currently on the stack to be returned to the app.
pub fn defer(&mut self, f: impl FnOnce(&mut V, &mut ViewContext<V>) + 'static) {
let view = self.view().downgrade();
self.window_cx.defer(move |cx| {
view.update(cx, f).ok();
});
}
pub fn observe<V2, E>(
&mut self,
entity: &E,
mut on_notify: impl FnMut(&mut V, E, &mut ViewContext<'_, '_, V>) + Send + 'static,
mut on_notify: impl FnMut(&mut V, E, &mut ViewContext<'_, V>) + 'static,
) -> Subscription
where
V2: 'static,
V: Any + Send,
V: 'static,
E: Entity<V2>,
{
let view = self.view();
let view = self.view().downgrade();
let entity_id = entity.entity_id();
let entity = entity.downgrade();
let window_handle = self.window.handle;
self.app.observers.insert(
entity_id,
Box::new(move |cx| {
cx.update_window(window_handle.id, |cx| {
if let Some(handle) = E::upgrade_from(&entity) {
view.update(cx, |this, cx| on_notify(this, handle, cx))
.is_ok()
} else {
false
}
})
.unwrap_or(false)
window_handle
.update(cx, |_, cx| {
if let Some(handle) = E::upgrade_from(&entity) {
view.update(cx, |this, cx| on_notify(this, handle, cx))
.is_ok()
} else {
false
}
})
.unwrap_or(false)
}),
)
}
@ -1566,44 +1629,44 @@ impl<'a, 'w, V: 'static> ViewContext<'a, 'w, V> {
pub fn subscribe<V2, E>(
&mut self,
entity: &E,
mut on_event: impl FnMut(&mut V, E, &V2::Event, &mut ViewContext<'_, '_, V>) + Send + 'static,
mut on_event: impl FnMut(&mut V, E, &V2::Event, &mut ViewContext<'_, V>) + 'static,
) -> Subscription
where
V2: EventEmitter,
E: Entity<V2>,
{
let view = self.view();
let view = self.view().downgrade();
let entity_id = entity.entity_id();
let handle = entity.downgrade();
let window_handle = self.window.handle;
self.app.event_listeners.insert(
entity_id,
Box::new(move |event, cx| {
cx.update_window(window_handle.id, |cx| {
if let Some(handle) = E::upgrade_from(&handle) {
let event = event.downcast_ref().expect("invalid event type");
view.update(cx, |this, cx| on_event(this, handle, event, cx))
.is_ok()
} else {
false
}
})
.unwrap_or(false)
window_handle
.update(cx, |_, cx| {
if let Some(handle) = E::upgrade_from(&handle) {
let event = event.downcast_ref().expect("invalid event type");
view.update(cx, |this, cx| on_event(this, handle, event, cx))
.is_ok()
} else {
false
}
})
.unwrap_or(false)
}),
)
}
pub fn on_release(
&mut self,
mut on_release: impl FnMut(&mut V, &mut WindowContext) + Send + 'static,
on_release: impl FnOnce(&mut V, &mut WindowContext) + 'static,
) -> Subscription {
let window_handle = self.window.handle;
self.app.release_listeners.insert(
self.view.model.entity_id,
Box::new(move |this, cx| {
let this = this.downcast_mut().expect("invalid entity type");
// todo!("are we okay with silently swallowing the error?")
let _ = cx.update_window(window_handle.id, |cx| on_release(this, cx));
let _ = window_handle.update(cx, |_, cx| on_release(this, cx));
}),
)
}
@ -1611,21 +1674,21 @@ impl<'a, 'w, V: 'static> ViewContext<'a, 'w, V> {
pub fn observe_release<V2, E>(
&mut self,
entity: &E,
mut on_release: impl FnMut(&mut V, &mut V2, &mut ViewContext<'_, '_, V>) + Send + 'static,
mut on_release: impl FnMut(&mut V, &mut V2, &mut ViewContext<'_, V>) + 'static,
) -> Subscription
where
V: Any + Send,
V: 'static,
V2: 'static,
E: Entity<V2>,
{
let view = self.view();
let view = self.view().downgrade();
let entity_id = entity.entity_id();
let window_handle = self.window.handle;
self.app.release_listeners.insert(
entity_id,
Box::new(move |entity, cx| {
let entity = entity.downcast_mut().expect("invalid entity type");
let _ = cx.update_window(window_handle.id, |cx| {
let _ = window_handle.update(cx, |_, cx| {
view.update(cx, |this, cx| on_release(this, entity, cx))
});
}),
@ -1641,9 +1704,9 @@ impl<'a, 'w, V: 'static> ViewContext<'a, 'w, V> {
pub fn on_focus_changed(
&mut self,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + Send + 'static,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + 'static,
) {
let handle = self.view();
let handle = self.view().downgrade();
self.window.focus_listeners.push(Box::new(move |event, cx| {
handle
.update(cx, |view, cx| listener(view, event, cx))
@ -1659,12 +1722,12 @@ impl<'a, 'w, V: 'static> ViewContext<'a, 'w, V> {
let old_stack_len = self.window.key_dispatch_stack.len();
if !self.window.freeze_key_dispatch_stack {
for (event_type, listener) in key_listeners {
let handle = self.view();
let handle = self.view().downgrade();
let listener = Box::new(
move |event: &dyn Any,
context_stack: &[&DispatchContext],
phase: DispatchPhase,
cx: &mut WindowContext<'_, '_>| {
cx: &mut WindowContext<'_>| {
handle
.update(cx, |view, cx| {
listener(view, event, context_stack, phase, cx)
@ -1745,16 +1808,13 @@ impl<'a, 'w, V: 'static> ViewContext<'a, 'w, V> {
R: 'static,
Fut: Future<Output = R> + 'static,
{
let view = self.view();
self.window_cx.spawn(move |_, cx| {
let result = f(view, cx);
async move { result.await }
})
let view = self.view().downgrade();
self.window_cx.spawn(move |_, cx| f(view, cx))
}
pub fn update_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R
where
G: 'static + Send,
G: 'static,
{
let mut global = self.app.lease_global::<G>();
let result = f(&mut global, self);
@ -1764,17 +1824,16 @@ impl<'a, 'w, V: 'static> ViewContext<'a, 'w, V> {
pub fn observe_global<G: 'static>(
&mut self,
f: impl Fn(&mut V, &mut ViewContext<'_, '_, V>) + Send + 'static,
f: impl Fn(&mut V, &mut ViewContext<'_, V>) + 'static,
) -> Subscription {
let window_id = self.window.handle.id;
let handle = self.view();
let window_handle = self.window.handle;
let view = self.view().downgrade();
self.global_observers.insert(
TypeId::of::<G>(),
Box::new(move |cx| {
cx.update_window(window_id, |cx| {
handle.update(cx, |view, cx| f(view, cx)).is_ok()
})
.unwrap_or(false)
window_handle
.update(cx, |_, cx| view.update(cx, |view, cx| f(view, cx)).is_ok())
.unwrap_or(false)
}),
)
}
@ -1783,7 +1842,7 @@ impl<'a, 'w, V: 'static> ViewContext<'a, 'w, V> {
&mut self,
handler: impl Fn(&mut V, &Event, DispatchPhase, &mut ViewContext<V>) + 'static,
) {
let handle = self.view().upgrade().unwrap();
let handle = self.view();
self.window_cx.on_mouse_event(move |event, phase, cx| {
handle.update(cx, |view, cx| {
handler(view, event, phase, cx);
@ -1792,10 +1851,10 @@ impl<'a, 'w, V: 'static> ViewContext<'a, 'w, V> {
}
}
impl<'a, 'w, V> ViewContext<'a, 'w, V>
impl<V> ViewContext<'_, V>
where
V: EventEmitter,
V::Event: Any + Send,
V::Event: 'static,
{
pub fn emit(&mut self, event: V::Event) {
let emitter = self.view.model.entity_id;
@ -1806,13 +1865,12 @@ where
}
}
impl<'a, 'w, V> Context for ViewContext<'a, 'w, V> {
type ModelContext<'b, U> = ModelContext<'b, U>;
impl<V> Context for ViewContext<'_, V> {
type Result<U> = U;
fn build_model<T: 'static>(
&mut self,
build_model: impl FnOnce(&mut Self::ModelContext<'_, T>) -> T,
build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
) -> Model<T> {
self.window_cx.build_model(build_model)
}
@ -1820,18 +1878,23 @@ impl<'a, 'w, V> Context for ViewContext<'a, 'w, V> {
fn update_model<T: 'static, R>(
&mut self,
model: &Model<T>,
update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R,
) -> R {
self.window_cx.update_model(model, update)
}
fn update_window<T, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<T>
where
F: FnOnce(AnyView, &mut WindowContext<'_>) -> T,
{
self.window_cx.update_window(window, update)
}
}
impl<V: 'static> VisualContext for ViewContext<'_, '_, V> {
type ViewContext<'a, 'w, V2> = ViewContext<'a, 'w, V2>;
impl<V: 'static> VisualContext for ViewContext<'_, V> {
fn build_view<W: 'static>(
&mut self,
build_view: impl FnOnce(&mut Self::ViewContext<'_, '_, W>) -> W,
build_view: impl FnOnce(&mut ViewContext<'_, W>) -> W,
) -> Self::Result<View<W>> {
self.window_cx.build_view(build_view)
}
@ -1839,21 +1902,31 @@ impl<V: 'static> VisualContext for ViewContext<'_, '_, V> {
fn update_view<V2: 'static, R>(
&mut self,
view: &View<V2>,
update: impl FnOnce(&mut V2, &mut Self::ViewContext<'_, '_, V2>) -> R,
update: impl FnOnce(&mut V2, &mut ViewContext<'_, V2>) -> R,
) -> Self::Result<R> {
self.window_cx.update_view(view, update)
}
fn replace_root_view<W>(
&mut self,
build_view: impl FnOnce(&mut ViewContext<'_, W>) -> W,
) -> Self::Result<View<W>>
where
W: Render,
{
self.window_cx.replace_root_view(build_view)
}
}
impl<'a, 'w, V> std::ops::Deref for ViewContext<'a, 'w, V> {
type Target = WindowContext<'a, 'w>;
impl<'a, V> std::ops::Deref for ViewContext<'a, V> {
type Target = WindowContext<'a>;
fn deref(&self) -> &Self::Target {
&self.window_cx
}
}
impl<'a, 'w, V> std::ops::DerefMut for ViewContext<'a, 'w, V> {
impl<'a, V> std::ops::DerefMut for ViewContext<'a, V> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.window_cx
}
@ -1868,42 +1941,74 @@ impl WindowId {
}
}
#[derive(PartialEq, Eq)]
#[derive(Deref, DerefMut)]
pub struct WindowHandle<V> {
id: WindowId,
#[deref]
#[deref_mut]
pub(crate) any_handle: AnyWindowHandle,
state_type: PhantomData<V>,
}
impl<S> Copy for WindowHandle<S> {}
impl<S> Clone for WindowHandle<S> {
fn clone(&self) -> Self {
WindowHandle {
id: self.id,
state_type: PhantomData,
}
}
}
impl<S> WindowHandle<S> {
impl<V: 'static + Render> WindowHandle<V> {
pub fn new(id: WindowId) -> Self {
WindowHandle {
id,
any_handle: AnyWindowHandle {
id,
state_type: TypeId::of::<V>(),
},
state_type: PhantomData,
}
}
pub fn update<C, R>(
self,
cx: &mut C,
update: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R,
) -> Result<R>
where
C: Context,
{
cx.update_window(self.any_handle, |root_view, cx| {
let view = root_view
.downcast::<V>()
.map_err(|_| anyhow!("the type of the window's root view has changed"))?;
Ok(cx.update_view(&view, update))
})?
}
}
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<S: 'static> Into<AnyWindowHandle> for WindowHandle<S> {
fn into(self) -> AnyWindowHandle {
AnyWindowHandle {
id: self.id,
state_type: TypeId::of::<S>(),
}
impl<V> PartialEq for WindowHandle<V> {
fn eq(&self, other: &Self) -> bool {
self.any_handle == other.any_handle
}
}
#[derive(Copy, Clone, PartialEq, Eq)]
impl<V> Eq for WindowHandle<V> {}
impl<V> Hash for WindowHandle<V> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.any_handle.hash(state);
}
}
impl<V: 'static> Into<AnyWindowHandle> for WindowHandle<V> {
fn into(self) -> AnyWindowHandle {
self.any_handle
}
}
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct AnyWindowHandle {
pub(crate) id: WindowId,
state_type: TypeId,
@ -1913,6 +2018,28 @@ impl AnyWindowHandle {
pub fn window_id(&self) -> WindowId {
self.id
}
pub fn downcast<T: 'static>(&self) -> Option<WindowHandle<T>> {
if TypeId::of::<T>() == self.state_type {
Some(WindowHandle {
any_handle: *self,
state_type: PhantomData,
})
} else {
None
}
}
pub fn update<C, R>(
self,
cx: &mut C,
update: impl FnOnce(AnyView, &mut WindowContext<'_>) -> R,
) -> Result<R>
where
C: Context,
{
cx.update_window(self, update)
}
}
#[cfg(any(test, feature = "test-support"))]