Get collab2 green

This commit is contained in:
Mikayla 2023-11-03 18:01:06 -07:00
parent c529343ba1
commit e1525e2b47
No known key found for this signature in database
265 changed files with 64477 additions and 40 deletions

View file

@ -16,8 +16,8 @@ pub use test_context::*;
use crate::{
current_platform, image_cache::ImageCache, Action, AnyBox, AnyView, AnyWindowHandle,
AppMetadata, AssetSource, BackgroundExecutor, ClipboardItem, Context, DispatchPhase, DisplayId,
Entity, FocusEvent, FocusHandle, FocusId, ForegroundExecutor, KeyBinding, Keymap, LayoutId,
PathPromptOptions, Pixels, Platform, PlatformDisplay, Point, Render, SharedString,
Entity, EventEmitter, FocusEvent, FocusHandle, FocusId, ForegroundExecutor, KeyBinding, Keymap,
LayoutId, PathPromptOptions, Pixels, Platform, PlatformDisplay, Point, Render, SharedString,
SubscriberSet, Subscription, SvgRenderer, Task, TextStyle, TextStyleRefinement, TextSystem,
View, Window, WindowContext, WindowHandle, WindowId,
};
@ -48,15 +48,19 @@ pub struct AppCell {
impl AppCell {
#[track_caller]
pub fn borrow(&self) -> AppRef {
let thread_id = std::thread::current().id();
eprintln!("borrowed {thread_id:?}");
if let Some(_) = option_env!("TRACK_THREAD_BORROWS") {
let thread_id = std::thread::current().id();
eprintln!("borrowed {thread_id:?}");
}
AppRef(self.app.borrow())
}
#[track_caller]
pub fn borrow_mut(&self) -> AppRefMut {
let thread_id = std::thread::current().id();
eprintln!("borrowed {thread_id:?}");
if let Some(_) = option_env!("TRACK_THREAD_BORROWS") {
let thread_id = std::thread::current().id();
eprintln!("borrowed {thread_id:?}");
}
AppRefMut(self.app.borrow_mut())
}
}
@ -292,6 +296,83 @@ impl AppContext {
result
}
pub fn observe<W, E>(
&mut self,
entity: &E,
mut on_notify: impl FnMut(E, &mut AppContext) + 'static,
) -> Subscription
where
W: 'static,
E: Entity<W>,
{
self.observe_internal(entity, move |e, cx| {
on_notify(e, cx);
true
})
}
pub fn observe_internal<W, E>(
&mut self,
entity: &E,
mut on_notify: impl FnMut(E, &mut AppContext) -> bool + 'static,
) -> Subscription
where
W: 'static,
E: Entity<W>,
{
let entity_id = entity.entity_id();
let handle = entity.downgrade();
self.observers.insert(
entity_id,
Box::new(move |cx| {
if let Some(handle) = E::upgrade_from(&handle) {
on_notify(handle, cx)
} else {
false
}
}),
)
}
pub fn subscribe<T, E>(
&mut self,
entity: &E,
mut on_event: impl FnMut(E, &T::Event, &mut AppContext) + 'static,
) -> Subscription
where
T: 'static + EventEmitter,
E: Entity<T>,
{
self.subscribe_internal(entity, move |entity, event, cx| {
on_event(entity, event, cx);
true
})
}
pub(crate) fn subscribe_internal<T, E>(
&mut self,
entity: &E,
mut on_event: impl FnMut(E, &T::Event, &mut AppContext) -> bool + 'static,
) -> Subscription
where
T: 'static + EventEmitter,
E: Entity<T>,
{
let entity_id = entity.entity_id();
let entity = entity.downgrade();
self.event_listeners.insert(
entity_id,
Box::new(move |event, cx| {
let event: &T::Event = event.downcast_ref().expect("invalid event type");
if let Some(handle) = E::upgrade_from(&entity) {
on_event(handle, event, cx)
} else {
false
}
}),
)
}
pub fn windows(&self) -> Vec<AnyWindowHandle> {
self.windows
.values()
@ -670,6 +751,23 @@ impl AppContext {
self.globals_by_type.insert(global_type, Box::new(global));
}
pub fn clear_globals(&mut self) {
//todo!(notify globals?)
self.globals_by_type.drain();
}
/// Set the value of the global of the given type.
pub fn remove_global<G: Any>(&mut self) -> G {
let global_type = TypeId::of::<G>();
//todo!(notify globals?)
*self
.globals_by_type
.remove(&global_type)
.unwrap_or_else(|| panic!("no global added for {}", std::any::type_name::<G>()))
.downcast()
.unwrap()
}
/// Update the global of the given type with a closure. Unlike `global_mut`, this method provides
/// your closure with mutable access to the `AppContext` and the global simultaneously.
pub fn update_global<G: 'static, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R {
@ -841,6 +939,18 @@ impl Context for AppContext {
Ok(result)
})
}
fn read_model<T, R>(
&self,
handle: &Model<T>,
read: impl FnOnce(&T, &AppContext) -> R,
) -> Self::Result<R>
where
T: 'static,
{
let entity = self.entities.read(handle);
read(entity, self)
}
}
/// These effects are processed at the end of each application update cycle.