Merge branch 'main' into zed2-project-test

Co-authored-by: Marshall <marshall@zed.dev>
This commit is contained in:
Max Brunsfeld 2023-10-31 11:50:56 -07:00
commit 291d35f337
607 changed files with 52667 additions and 14124 deletions

View file

@ -4,7 +4,7 @@ use collections::{HashMap, HashSet};
use serde::Deserialize;
use std::any::{type_name, Any};
pub trait Action: Any + Send + Sync {
pub trait Action: Any + Send {
fn qualified_name() -> SharedString
where
Self: Sized;
@ -19,7 +19,7 @@ pub trait Action: Any + Send + Sync {
impl<A> Action for A
where
A: for<'a> Deserialize<'a> + PartialEq + Any + Send + Sync + Clone + Default,
A: for<'a> Deserialize<'a> + PartialEq + Any + Send + Clone + Default,
{
fn qualified_name() -> SharedString {
type_name::<A>().into()

View file

@ -15,14 +15,14 @@ pub use test_context::*;
use crate::{
current_platform, image_cache::ImageCache, Action, AnyBox, AnyView, AppMetadata, AssetSource,
ClipboardItem, Context, DispatchPhase, DisplayId, Executor, FocusEvent, FocusHandle, FocusId,
KeyBinding, Keymap, LayoutId, MainThread, MainThreadOnly, Pixels, Platform, Point,
KeyBinding, Keymap, LayoutId, MainThread, MainThreadOnly, Pixels, Platform, Point, Render,
SharedString, SubscriberSet, Subscription, SvgRenderer, Task, TextStyle, TextStyleRefinement,
TextSystem, View, Window, WindowContext, WindowHandle, WindowId,
};
use anyhow::{anyhow, Result};
use collections::{HashMap, HashSet, VecDeque};
use futures::{future::BoxFuture, Future};
use parking_lot::{Mutex, RwLock};
use parking_lot::Mutex;
use slotmap::SlotMap;
use std::{
any::{type_name, Any, TypeId},
@ -38,7 +38,10 @@ use util::http::{self, HttpClient};
pub struct App(Arc<Mutex<AppContext>>);
/// Represents an application before it is fully launched. Once your app is
/// configured, you'll start the app with `App::run`.
impl App {
/// Builds an app with the given asset source.
pub fn production(asset_source: Arc<dyn AssetSource>) -> Self {
Self(AppContext::new(
current_platform(),
@ -47,6 +50,8 @@ impl App {
))
}
/// Start the application. The provided callback will be called once the
/// app is fully launched.
pub fn run<F>(self, on_finish_launching: F)
where
F: 'static + FnOnce(&mut MainThread<AppContext>),
@ -60,6 +65,8 @@ impl App {
}));
}
/// Register a handler to be invoked when the platform instructs the application
/// to open one or more URLs.
pub fn on_open_urls<F>(&self, mut callback: F) -> &Self
where
F: 'static + FnMut(Vec<String>, &mut AppContext),
@ -109,11 +116,10 @@ impl App {
type ActionBuilder = fn(json: Option<serde_json::Value>) -> anyhow::Result<Box<dyn Action>>;
type FrameCallback = Box<dyn FnOnce(&mut WindowContext) + Send>;
type Handler = Box<dyn FnMut(&mut AppContext) -> bool + Send + Sync + 'static>;
type Listener = Box<dyn FnMut(&dyn Any, &mut AppContext) -> bool + Send + Sync + 'static>;
type QuitHandler =
Box<dyn FnMut(&mut AppContext) -> BoxFuture<'static, ()> + Send + Sync + 'static>;
type ReleaseListener = Box<dyn FnMut(&mut dyn Any, &mut AppContext) + Send + Sync + 'static>;
type Handler = Box<dyn FnMut(&mut AppContext) -> bool + Send + 'static>;
type Listener = Box<dyn FnMut(&dyn Any, &mut AppContext) -> bool + Send + 'static>;
type QuitHandler = Box<dyn FnMut(&mut AppContext) -> BoxFuture<'static, ()> + Send + 'static>;
type ReleaseListener = Box<dyn FnMut(&mut dyn Any, &mut AppContext) + Send + 'static>;
pub struct AppContext {
this: Weak<Mutex<AppContext>>,
@ -130,12 +136,11 @@ pub struct AppContext {
pub(crate) image_cache: ImageCache,
pub(crate) text_style_stack: Vec<TextStyleRefinement>,
pub(crate) globals_by_type: HashMap<TypeId, AnyBox>,
pub(crate) unit_entity: Handle<()>,
pub(crate) entities: EntityMap,
pub(crate) windows: SlotMap<WindowId, Option<Window>>,
pub(crate) keymap: Arc<RwLock<Keymap>>,
pub(crate) keymap: Arc<Mutex<Keymap>>,
pub(crate) global_action_listeners:
HashMap<TypeId, Vec<Box<dyn Fn(&dyn Action, DispatchPhase, &mut Self) + Send + Sync>>>,
HashMap<TypeId, Vec<Box<dyn Fn(&dyn Action, DispatchPhase, &mut Self) + Send>>>,
action_builders: HashMap<SharedString, ActionBuilder>,
pending_effects: VecDeque<Effect>,
pub(crate) pending_notifications: HashSet<EntityId>,
@ -162,8 +167,8 @@ impl AppContext {
);
let text_system = Arc::new(TextSystem::new(platform.text_system()));
let mut entities = EntityMap::new();
let unit_entity = entities.insert(entities.reserve(), ());
let entities = EntityMap::new();
let app_metadata = AppMetadata {
os_name: platform.os_name(),
os_version: platform.os_version().ok(),
@ -185,10 +190,9 @@ impl AppContext {
image_cache: ImageCache::new(http_client),
text_style_stack: Vec::new(),
globals_by_type: HashMap::default(),
unit_entity,
entities,
windows: SlotMap::with_key(),
keymap: Arc::new(RwLock::new(Keymap::default())),
keymap: Arc::new(Mutex::new(Keymap::default())),
global_action_listeners: HashMap::default(),
action_builders: HashMap::default(),
pending_effects: VecDeque::new(),
@ -206,6 +210,8 @@ impl AppContext {
})
}
/// Quit the application gracefully. Handlers registered with `ModelContext::on_app_quit`
/// will be given 100ms to complete before exiting.
pub fn quit(&mut self) {
let mut futures = Vec::new();
@ -233,6 +239,8 @@ impl AppContext {
self.app_metadata.clone()
}
/// Schedules all windows in the application to be redrawn. This can be called
/// multiple times in an update cycle and still result in a single redraw.
pub fn refresh(&mut self) {
self.pending_effects.push_back(Effect::Refresh);
}
@ -305,6 +313,9 @@ impl AppContext {
self.pending_effects.push_back(effect);
}
/// Called at the end of AppContext::update to complete any side effects
/// such as notifying observers, emitting events, etc. Effects can themselves
/// cause effects, so we continue looping until all effects are processed.
fn flush_effects(&mut self) {
loop {
self.release_dropped_entities();
@ -351,6 +362,9 @@ impl AppContext {
}
}
/// Repeatedly called during `flush_effects` to release any entities whose
/// reference count has become zero. We invoke any release observers before dropping
/// each entity.
fn release_dropped_entities(&mut self) {
loop {
let dropped = self.entities.take_dropped();
@ -368,6 +382,9 @@ impl AppContext {
}
}
/// Repeatedly called during `flush_effects` to handle a focused handle being dropped.
/// For now, we simply blur the window if this happens, but we may want to support invoking
/// a window blur handler to restore focus to some logical element.
fn release_dropped_focus_handles(&mut self) {
let window_ids = self.windows.keys().collect::<SmallVec<[_; 8]>>();
for window_id in window_ids {
@ -447,10 +464,12 @@ impl AppContext {
.retain(&type_id, |observer| observer(self));
}
fn apply_defer_effect(&mut self, callback: Box<dyn FnOnce(&mut Self) + Send + Sync + 'static>) {
fn apply_defer_effect(&mut self, callback: Box<dyn FnOnce(&mut Self) + Send + 'static>) {
callback(self);
}
/// Creates an `AsyncAppContext`, which can be cloned and has a static lifetime
/// so it can be held across `await` points.
pub fn to_async(&self) -> AsyncAppContext {
AsyncAppContext {
app: unsafe { mem::transmute(self.this.clone()) },
@ -458,10 +477,14 @@ impl AppContext {
}
}
/// Obtains a reference to the executor, which can be used to spawn futures.
pub fn executor(&self) -> &Executor {
&self.executor
}
/// Runs the given closure on the main thread, where interaction with the platform
/// is possible. The given closure will be invoked with a `MainThread<AppContext>`, which
/// has platform-specific methods that aren't present on `AppContext`.
pub fn run_on_main<R>(
&mut self,
f: impl FnOnce(&mut MainThread<AppContext>) -> R + Send + 'static,
@ -482,6 +505,11 @@ impl AppContext {
}
}
/// Spawns the future returned by the given function on the main thread, where interaction with
/// the platform is possible. The given closure will be invoked with a `MainThread<AsyncAppContext>`,
/// which has platform-specific methods that aren't present on `AsyncAppContext`. The future will be
/// polled exclusively on the main thread.
// todo!("I think we need somehow to prevent the MainThread<AsyncAppContext> from implementing Send")
pub fn spawn_on_main<F, R>(
&self,
f: impl FnOnce(MainThread<AsyncAppContext>) -> F + Send + 'static,
@ -494,6 +522,8 @@ impl AppContext {
self.executor.spawn_on_main(move || f(MainThread(cx)))
}
/// Spawns the future returned by the given function on the thread pool. The closure will be invoked
/// with AsyncAppContext, which allows the application state to be accessed across await points.
pub fn spawn<Fut, R>(&self, f: impl FnOnce(AsyncAppContext) -> Fut + Send + 'static) -> Task<R>
where
Fut: Future<Output = R> + Send + 'static,
@ -506,20 +536,25 @@ impl AppContext {
})
}
pub fn defer(&mut self, f: impl FnOnce(&mut AppContext) + 'static + Send + Sync) {
/// 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 AppContext) + 'static + Send) {
self.push_effect(Effect::Defer {
callback: Box::new(f),
});
}
/// Accessor for the application's asset source, which is provided when constructing the `App`.
pub fn asset_source(&self) -> &Arc<dyn AssetSource> {
&self.asset_source
}
/// Accessor for the text system.
pub fn text_system(&self) -> &Arc<TextSystem> {
&self.text_system
}
/// The current text style. Which is composed of all the style refinements provided to `with_text_style`.
pub fn text_style(&self) -> TextStyle {
let mut style = TextStyle::default();
for refinement in &self.text_style_stack {
@ -528,10 +563,12 @@ impl AppContext {
style
}
/// Check whether a global of the given type has been assigned.
pub fn has_global<G: 'static>(&self) -> bool {
self.globals_by_type.contains_key(&TypeId::of::<G>())
}
/// Access the global of the given type. Panics if a global for that type has not been assigned.
pub fn global<G: 'static>(&self) -> &G {
self.globals_by_type
.get(&TypeId::of::<G>())
@ -540,12 +577,14 @@ impl AppContext {
.unwrap()
}
/// Access the global of the given type if a value has been assigned.
pub fn try_global<G: 'static>(&self) -> Option<&G> {
self.globals_by_type
.get(&TypeId::of::<G>())
.map(|any_state| any_state.downcast_ref::<G>().unwrap())
}
/// Access the global of the given type mutably. Panics if a global for that type has not been assigned.
pub fn global_mut<G: 'static>(&mut self) -> &mut G {
let global_type = TypeId::of::<G>();
self.push_effect(Effect::NotifyGlobalObservers { global_type });
@ -556,7 +595,9 @@ impl AppContext {
.unwrap()
}
pub fn default_global<G: 'static + Default + Sync + Send>(&mut self) -> &mut G {
/// Access the global of the given type mutably. A default value is assigned if a global of this type has not
/// yet been assigned.
pub fn default_global<G: 'static + Default + Send>(&mut self) -> &mut G {
let global_type = TypeId::of::<G>();
self.push_effect(Effect::NotifyGlobalObservers { global_type });
self.globals_by_type
@ -566,12 +607,15 @@ impl AppContext {
.unwrap()
}
pub fn set_global<G: Any + Send + Sync>(&mut self, global: G) {
/// Set the value of the global of the given type.
pub fn set_global<G: Any + Send>(&mut self, global: G) {
let global_type = TypeId::of::<G>();
self.push_effect(Effect::NotifyGlobalObservers { global_type });
self.globals_by_type.insert(global_type, Box::new(global));
}
/// 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 {
let mut global = self.lease_global::<G>();
let result = f(&mut global, self);
@ -579,9 +623,10 @@ impl AppContext {
result
}
/// Register a callback to be invoked when a global of the given type is updated.
pub fn observe_global<G: 'static>(
&mut self,
mut f: impl FnMut(&mut Self) + Send + Sync + 'static,
mut f: impl FnMut(&mut Self) + Send + 'static,
) -> Subscription {
self.global_observers.insert(
TypeId::of::<G>(),
@ -592,6 +637,11 @@ impl AppContext {
)
}
pub fn all_action_names<'a>(&'a self) -> impl Iterator<Item = SharedString> + 'a {
self.action_builders.keys().cloned()
}
/// Move the global of the given type to the stack.
pub(crate) fn lease_global<G: 'static>(&mut self) -> GlobalLease<G> {
GlobalLease::new(
self.globals_by_type
@ -601,6 +651,7 @@ impl AppContext {
)
}
/// Restore the global of the given type after it is moved to the stack.
pub(crate) fn end_global_lease<G: 'static>(&mut self, lease: GlobalLease<G>) {
let global_type = TypeId::of::<G>();
self.push_effect(Effect::NotifyGlobalObservers { global_type });
@ -615,15 +666,14 @@ impl AppContext {
self.text_style_stack.pop();
}
/// Register key bindings.
pub fn bind_keys(&mut self, bindings: impl IntoIterator<Item = KeyBinding>) {
self.keymap.write().add_bindings(bindings);
self.keymap.lock().add_bindings(bindings);
self.pending_effects.push_back(Effect::Refresh);
}
pub fn on_action<A: Action>(
&mut self,
listener: impl Fn(&A, &mut Self) + Send + Sync + 'static,
) {
/// Register a global listener for actions invoked via the keyboard.
pub fn on_action<A: Action>(&mut self, listener: impl Fn(&A, &mut Self) + Send + 'static) {
self.global_action_listeners
.entry(TypeId::of::<A>())
.or_default()
@ -635,10 +685,12 @@ impl AppContext {
}));
}
/// Register an action type to allow it to be referenced in keymaps.
pub fn register_action_type<A: Action>(&mut self) {
self.action_builders.insert(A::qualified_name(), A::build);
}
/// Construct an action based on its name and parameters.
pub fn build_action(
&mut self,
name: &str,
@ -651,36 +703,43 @@ impl AppContext {
(build)(params)
}
/// Halt propagation of a mouse event, keyboard event, or action. This prevents listeners
/// that have not yet been invoked from receiving the event.
pub fn stop_propagation(&mut self) {
self.propagate_event = false;
}
}
impl Context for AppContext {
type EntityContext<'a, 'w, T> = ModelContext<'a, T>;
type ModelContext<'a, T> = ModelContext<'a, T>;
type Result<T> = T;
fn entity<T: Any + Send + Sync>(
/// Build an entity that is owned by the application. The given function will be invoked with
/// a `ModelContext` and must return an object representing the entity. A `Model` will be returned
/// which can be used to access the entity in a context.
fn build_model<T: 'static + Send>(
&mut self,
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
) -> Handle<T> {
build_model: impl FnOnce(&mut Self::ModelContext<'_, T>) -> T,
) -> Model<T> {
self.update(|cx| {
let slot = cx.entities.reserve();
let entity = build_entity(&mut ModelContext::mutable(cx, slot.downgrade()));
let entity = build_model(&mut ModelContext::mutable(cx, slot.downgrade()));
cx.entities.insert(slot, entity)
})
}
fn update_entity<T: 'static, R>(
/// Update the entity referenced by the given model. The function is passed a mutable reference to the
/// entity along with a `ModelContext` for the entity.
fn update_model<T: 'static, R>(
&mut self,
handle: &Handle<T>,
update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R,
model: &Model<T>,
update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
) -> R {
self.update(|cx| {
let mut entity = cx.entities.lease(handle);
let mut entity = cx.entities.lease(model);
let result = update(
&mut entity,
&mut ModelContext::mutable(cx, handle.downgrade()),
&mut ModelContext::mutable(cx, model.downgrade()),
);
cx.entities.end_lease(entity);
result
@ -696,30 +755,37 @@ where
self.0.borrow().platform.borrow_on_main_thread()
}
/// Instructs the platform to activate the application by bringing it to the foreground.
pub fn activate(&self, ignoring_other_apps: bool) {
self.platform().activate(ignoring_other_apps);
}
/// Writes data to the platform clipboard.
pub fn write_to_clipboard(&self, item: ClipboardItem) {
self.platform().write_to_clipboard(item)
}
/// Reads data from the platform clipboard.
pub fn read_from_clipboard(&self) -> Option<ClipboardItem> {
self.platform().read_from_clipboard()
}
/// Writes credentials to the platform keychain.
pub fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Result<()> {
self.platform().write_credentials(url, username, password)
}
/// Reads credentials from the platform keychain.
pub fn read_credentials(&self, url: &str) -> Result<Option<(String, Vec<u8>)>> {
self.platform().read_credentials(url)
}
/// Deletes credentials from the platform keychain.
pub fn delete_credentials(&self, url: &str) -> Result<()> {
self.platform().delete_credentials(url)
}
/// Directs the platform's default browser to open the given URL.
pub fn open_url(&self, url: &str) {
self.platform().open_url(url);
}
@ -750,7 +816,10 @@ impl MainThread<AppContext> {
})
}
pub fn open_window<V: 'static>(
/// Opens a new window with the given option and the root view returned by the given function.
/// The function is invoked with a `WindowContext`, which can be used to interact with window-specific
/// functionality.
pub fn open_window<V: Render>(
&mut self,
options: crate::WindowOptions,
build_root_view: impl FnOnce(&mut WindowContext) -> View<V> + Send + 'static,
@ -760,13 +829,15 @@ impl MainThread<AppContext> {
let handle = WindowHandle::new(id);
let mut window = Window::new(handle.into(), options, cx);
let root_view = build_root_view(&mut WindowContext::mutable(cx, &mut window));
window.root_view.replace(root_view.into_any());
window.root_view.replace(root_view.into());
cx.windows.get_mut(id).unwrap().replace(window);
handle
})
}
pub fn update_global<G: 'static + Send + Sync, R>(
/// Update the global of the given type with a closure. Unlike `global_mut`, this method provides
/// your closure with mutable access to the `MainThread<AppContext>` and the global simultaneously.
pub fn update_global<G: 'static + Send, R>(
&mut self,
update: impl FnOnce(&mut G, &mut MainThread<AppContext>) -> R,
) -> R {
@ -777,13 +848,14 @@ impl MainThread<AppContext> {
}
}
/// These effects are processed at the end of each application update cycle.
pub(crate) enum Effect {
Notify {
emitter: EntityId,
},
Emit {
emitter: EntityId,
event: Box<dyn Any + Send + Sync + 'static>,
event: Box<dyn Any + Send + 'static>,
},
FocusChanged {
window_id: WindowId,
@ -794,10 +866,11 @@ pub(crate) enum Effect {
global_type: TypeId,
},
Defer {
callback: Box<dyn FnOnce(&mut AppContext) + Send + Sync + 'static>,
callback: Box<dyn FnOnce(&mut AppContext) + Send + 'static>,
},
}
/// Wraps a global variable value during `update_global` while the value has been moved to the stack.
pub(crate) struct GlobalLease<G: 'static> {
global: AnyBox,
global_type: PhantomData<G>,
@ -826,11 +899,11 @@ impl<G: 'static> DerefMut for GlobalLease<G> {
}
}
/// Contains state associated with an active drag operation, started by dragging an element
/// within the window or by dragging into the app from the underlying platform.
pub(crate) struct AnyDrag {
pub drag_handle_view: Option<AnyView>,
pub view: AnyView,
pub cursor_offset: Point<Pixels>,
pub state: AnyBox,
pub state_type: TypeId,
}
#[cfg(test)]

View file

@ -1,11 +1,11 @@
use crate::{
AnyWindowHandle, AppContext, Context, Executor, Handle, MainThread, ModelContext, Result, Task,
ViewContext, WindowContext,
AnyWindowHandle, AppContext, Context, Executor, MainThread, Model, ModelContext, Result, Task,
WindowContext,
};
use anyhow::anyhow;
use derive_more::{Deref, DerefMut};
use parking_lot::Mutex;
use std::{any::Any, future::Future, sync::Weak};
use std::{future::Future, sync::Weak};
#[derive(Clone)]
pub struct AsyncAppContext {
@ -14,35 +14,35 @@ pub struct AsyncAppContext {
}
impl Context for AsyncAppContext {
type EntityContext<'a, 'w, T> = ModelContext<'a, T>;
type ModelContext<'a, T> = ModelContext<'a, T>;
type Result<T> = Result<T>;
fn entity<T: 'static>(
fn build_model<T: 'static>(
&mut self,
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
) -> Self::Result<Handle<T>>
build_model: impl FnOnce(&mut Self::ModelContext<'_, T>) -> T,
) -> Self::Result<Model<T>>
where
T: Any + Send + Sync,
T: 'static + Send,
{
let app = self
.app
.upgrade()
.ok_or_else(|| anyhow!("app was released"))?;
let mut lock = app.lock(); // Need this to compile
Ok(lock.entity(build_entity))
Ok(lock.build_model(build_model))
}
fn update_entity<T: 'static, R>(
fn update_model<T: 'static, R>(
&mut self,
handle: &Handle<T>,
update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R,
handle: &Model<T>,
update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
) -> Self::Result<R> {
let app = self
.app
.upgrade()
.ok_or_else(|| anyhow!("app was released"))?;
let mut lock = app.lock(); // Need this to compile
Ok(lock.update_entity(handle, update))
Ok(lock.update_model(handle, update))
}
}
@ -216,27 +216,27 @@ impl AsyncWindowContext {
}
impl Context for AsyncWindowContext {
type EntityContext<'a, 'w, T> = ViewContext<'a, 'w, T>;
type ModelContext<'a, T> = ModelContext<'a, T>;
type Result<T> = Result<T>;
fn entity<T>(
fn build_model<T>(
&mut self,
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
) -> Result<Handle<T>>
build_model: impl FnOnce(&mut Self::ModelContext<'_, T>) -> T,
) -> Result<Model<T>>
where
T: Any + Send + Sync,
T: 'static + Send,
{
self.app
.update_window(self.window, |cx| cx.entity(build_entity))
.update_window(self.window, |cx| cx.build_model(build_model))
}
fn update_entity<T: 'static, R>(
fn update_model<T: 'static, R>(
&mut self,
handle: &Handle<T>,
update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R,
handle: &Model<T>,
update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
) -> Result<R> {
self.app
.update_window(self.window, |cx| cx.update_entity(handle, update))
.update_window(self.window, |cx| cx.update_model(handle, update))
}
}

View file

@ -1,10 +1,10 @@
use crate::{AnyBox, AppContext, Context};
use crate::{private::Sealed, AnyBox, AppContext, Context, Entity};
use anyhow::{anyhow, Result};
use derive_more::{Deref, DerefMut};
use parking_lot::{RwLock, RwLockUpgradableReadGuard};
use slotmap::{SecondaryMap, SlotMap};
use std::{
any::{type_name, Any, TypeId},
any::{type_name, TypeId},
fmt::{self, Display},
hash::{Hash, Hasher},
marker::PhantomData,
@ -53,29 +53,29 @@ impl EntityMap {
/// Reserve a slot for an entity, which you can subsequently use with `insert`.
pub fn reserve<T: 'static>(&self) -> Slot<T> {
let id = self.ref_counts.write().counts.insert(1.into());
Slot(Handle::new(id, Arc::downgrade(&self.ref_counts)))
Slot(Model::new(id, Arc::downgrade(&self.ref_counts)))
}
/// Insert an entity into a slot obtained by calling `reserve`.
pub fn insert<T>(&mut self, slot: Slot<T>, entity: T) -> Handle<T>
pub fn insert<T>(&mut self, slot: Slot<T>, entity: T) -> Model<T>
where
T: Any + Send + Sync,
T: 'static + Send,
{
let handle = slot.0;
self.entities.insert(handle.entity_id, Box::new(entity));
handle
let model = slot.0;
self.entities.insert(model.entity_id, Box::new(entity));
model
}
/// Move an entity to the stack.
pub fn lease<'a, T>(&mut self, handle: &'a Handle<T>) -> Lease<'a, T> {
self.assert_valid_context(handle);
pub fn lease<'a, T>(&mut self, model: &'a Model<T>) -> Lease<'a, T> {
self.assert_valid_context(model);
let entity = Some(
self.entities
.remove(handle.entity_id)
.remove(model.entity_id)
.expect("Circular entity lease. Is the entity already being updated?"),
);
Lease {
handle,
model,
entity,
entity_type: PhantomData,
}
@ -84,18 +84,18 @@ impl EntityMap {
/// Return an entity after moving it to the stack.
pub fn end_lease<T>(&mut self, mut lease: Lease<T>) {
self.entities
.insert(lease.handle.entity_id, lease.entity.take().unwrap());
.insert(lease.model.entity_id, lease.entity.take().unwrap());
}
pub fn read<T: 'static>(&self, handle: &Handle<T>) -> &T {
self.assert_valid_context(handle);
self.entities[handle.entity_id].downcast_ref().unwrap()
pub fn read<T: 'static>(&self, model: &Model<T>) -> &T {
self.assert_valid_context(model);
self.entities[model.entity_id].downcast_ref().unwrap()
}
fn assert_valid_context(&self, handle: &AnyHandle) {
fn assert_valid_context(&self, model: &AnyModel) {
debug_assert!(
Weak::ptr_eq(&handle.entity_map, &Arc::downgrade(&self.ref_counts)),
"used a handle with the wrong context"
Weak::ptr_eq(&model.entity_map, &Arc::downgrade(&self.ref_counts)),
"used a model with the wrong context"
);
}
@ -120,7 +120,7 @@ impl EntityMap {
pub struct Lease<'a, T> {
entity: Option<AnyBox>,
pub handle: &'a Handle<T>,
pub model: &'a Model<T>,
entity_type: PhantomData<T>,
}
@ -148,15 +148,15 @@ impl<'a, T> Drop for Lease<'a, T> {
}
#[derive(Deref, DerefMut)]
pub struct Slot<T>(Handle<T>);
pub struct Slot<T>(Model<T>);
pub struct AnyHandle {
pub struct AnyModel {
pub(crate) entity_id: EntityId,
entity_type: TypeId,
pub(crate) entity_type: TypeId,
entity_map: Weak<RwLock<EntityRefCounts>>,
}
impl AnyHandle {
impl AnyModel {
fn new(id: EntityId, entity_type: TypeId, entity_map: Weak<RwLock<EntityRefCounts>>) -> Self {
Self {
entity_id: id,
@ -169,36 +169,36 @@ impl AnyHandle {
self.entity_id
}
pub fn downgrade(&self) -> AnyWeakHandle {
AnyWeakHandle {
pub fn downgrade(&self) -> AnyWeakModel {
AnyWeakModel {
entity_id: self.entity_id,
entity_type: self.entity_type,
entity_ref_counts: self.entity_map.clone(),
}
}
pub fn downcast<T: 'static>(&self) -> Option<Handle<T>> {
pub fn downcast<T: 'static>(self) -> Result<Model<T>, AnyModel> {
if TypeId::of::<T>() == self.entity_type {
Some(Handle {
any_handle: self.clone(),
Ok(Model {
any_model: self,
entity_type: PhantomData,
})
} else {
None
Err(self)
}
}
}
impl Clone for AnyHandle {
impl Clone for AnyModel {
fn clone(&self) -> Self {
if let Some(entity_map) = self.entity_map.upgrade() {
let entity_map = entity_map.read();
let count = entity_map
.counts
.get(self.entity_id)
.expect("detected over-release of a handle");
.expect("detected over-release of a model");
let prev_count = count.fetch_add(1, SeqCst);
assert_ne!(prev_count, 0, "Detected over-release of a handle.");
assert_ne!(prev_count, 0, "Detected over-release of a model.");
}
Self {
@ -209,7 +209,7 @@ impl Clone for AnyHandle {
}
}
impl Drop for AnyHandle {
impl Drop for AnyModel {
fn drop(&mut self) {
if let Some(entity_map) = self.entity_map.upgrade() {
let entity_map = entity_map.upgradable_read();
@ -218,7 +218,7 @@ impl Drop for AnyHandle {
.get(self.entity_id)
.expect("detected over-release of a handle.");
let prev_count = count.fetch_sub(1, SeqCst);
assert_ne!(prev_count, 0, "Detected over-release of a handle.");
assert_ne!(prev_count, 0, "Detected over-release of a model.");
if prev_count == 1 {
// We were the last reference to this entity, so we can remove it.
let mut entity_map = RwLockUpgradableReadGuard::upgrade(entity_map);
@ -228,60 +228,100 @@ impl Drop for AnyHandle {
}
}
impl<T> From<Handle<T>> for AnyHandle {
fn from(handle: Handle<T>) -> Self {
handle.any_handle
impl<T> From<Model<T>> for AnyModel {
fn from(model: Model<T>) -> Self {
model.any_model
}
}
impl Hash for AnyHandle {
impl Hash for AnyModel {
fn hash<H: Hasher>(&self, state: &mut H) {
self.entity_id.hash(state);
}
}
impl PartialEq for AnyHandle {
impl PartialEq for AnyModel {
fn eq(&self, other: &Self) -> bool {
self.entity_id == other.entity_id
}
}
impl Eq for AnyHandle {}
impl Eq for AnyModel {}
#[derive(Deref, DerefMut)]
pub struct Handle<T> {
#[deref]
#[deref_mut]
any_handle: AnyHandle,
entity_type: PhantomData<T>,
impl std::fmt::Debug for AnyModel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AnyModel")
.field("entity_id", &self.entity_id.as_u64())
.finish()
}
}
unsafe impl<T> Send for Handle<T> {}
unsafe impl<T> Sync for Handle<T> {}
#[derive(Deref, DerefMut)]
pub struct Model<T> {
#[deref]
#[deref_mut]
pub(crate) any_model: AnyModel,
pub(crate) entity_type: PhantomData<T>,
}
impl<T: 'static> Handle<T> {
unsafe impl<T> Send for Model<T> {}
unsafe impl<T> Sync for Model<T> {}
impl<T> Sealed for Model<T> {}
impl<T: 'static> Entity<T> for Model<T> {
type Weak = WeakModel<T>;
fn entity_id(&self) -> EntityId {
self.any_model.entity_id
}
fn downgrade(&self) -> Self::Weak {
WeakModel {
any_model: self.any_model.downgrade(),
entity_type: self.entity_type,
}
}
fn upgrade_from(weak: &Self::Weak) -> Option<Self>
where
Self: Sized,
{
Some(Model {
any_model: weak.any_model.upgrade()?,
entity_type: weak.entity_type,
})
}
}
impl<T: 'static> Model<T> {
fn new(id: EntityId, entity_map: Weak<RwLock<EntityRefCounts>>) -> Self
where
T: 'static,
{
Self {
any_handle: AnyHandle::new(id, TypeId::of::<T>(), entity_map),
any_model: AnyModel::new(id, TypeId::of::<T>(), entity_map),
entity_type: PhantomData,
}
}
pub fn downgrade(&self) -> WeakHandle<T> {
WeakHandle {
any_handle: self.any_handle.downgrade(),
entity_type: self.entity_type,
}
/// Downgrade the this to a weak model reference
pub fn downgrade(&self) -> WeakModel<T> {
// Delegate to the trait implementation to keep behavior in one place.
// This method was included to improve method resolution in the presence of
// the Model's deref
Entity::downgrade(self)
}
/// Convert this into a dynamically typed model.
pub fn into_any(self) -> AnyModel {
self.any_model
}
pub fn read<'a>(&self, cx: &'a AppContext) -> &'a T {
cx.entities.read(self)
}
/// Update the entity referenced by this handle with the given function.
/// Update the entity referenced by this model with the given function.
///
/// The update function receives a context appropriate for its environment.
/// When updating in an `AppContext`, it receives a `ModelContext`.
@ -289,63 +329,63 @@ impl<T: 'static> Handle<T> {
pub fn update<C, R>(
&self,
cx: &mut C,
update: impl FnOnce(&mut T, &mut C::EntityContext<'_, '_, T>) -> R,
update: impl FnOnce(&mut T, &mut C::ModelContext<'_, T>) -> R,
) -> C::Result<R>
where
C: Context,
{
cx.update_entity(self, update)
cx.update_model(self, update)
}
}
impl<T> Clone for Handle<T> {
impl<T> Clone for Model<T> {
fn clone(&self) -> Self {
Self {
any_handle: self.any_handle.clone(),
any_model: self.any_model.clone(),
entity_type: self.entity_type,
}
}
}
impl<T> std::fmt::Debug for Handle<T> {
impl<T> std::fmt::Debug for Model<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Handle {{ entity_id: {:?}, entity_type: {:?} }}",
self.any_handle.entity_id,
"Model {{ entity_id: {:?}, entity_type: {:?} }}",
self.any_model.entity_id,
type_name::<T>()
)
}
}
impl<T> Hash for Handle<T> {
impl<T> Hash for Model<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.any_handle.hash(state);
self.any_model.hash(state);
}
}
impl<T> PartialEq for Handle<T> {
impl<T> PartialEq for Model<T> {
fn eq(&self, other: &Self) -> bool {
self.any_handle == other.any_handle
self.any_model == other.any_model
}
}
impl<T> Eq for Handle<T> {}
impl<T> Eq for Model<T> {}
impl<T> PartialEq<WeakHandle<T>> for Handle<T> {
fn eq(&self, other: &WeakHandle<T>) -> bool {
self.entity_id() == other.entity_id()
impl<T> PartialEq<WeakModel<T>> for Model<T> {
fn eq(&self, other: &WeakModel<T>) -> bool {
self.any_model.entity_id() == other.entity_id()
}
}
#[derive(Clone)]
pub struct AnyWeakHandle {
pub struct AnyWeakModel {
pub(crate) entity_id: EntityId,
entity_type: TypeId,
entity_ref_counts: Weak<RwLock<EntityRefCounts>>,
}
impl AnyWeakHandle {
impl AnyWeakModel {
pub fn entity_id(&self) -> EntityId {
self.entity_id
}
@ -359,10 +399,10 @@ impl AnyWeakHandle {
ref_count > 0
}
pub fn upgrade(&self) -> Option<AnyHandle> {
let entity_map = self.entity_ref_counts.upgrade()?;
let entity_map = entity_map.read();
let ref_count = entity_map.counts.get(self.entity_id)?;
pub fn upgrade(&self) -> Option<AnyModel> {
let ref_counts = &self.entity_ref_counts.upgrade()?;
let ref_counts = ref_counts.read();
let ref_count = ref_counts.counts.get(self.entity_id)?;
// entity_id is in dropped_entity_ids
if ref_count.load(SeqCst) == 0 {
@ -370,7 +410,7 @@ impl AnyWeakHandle {
}
ref_count.fetch_add(1, SeqCst);
Some(AnyHandle {
Some(AnyModel {
entity_id: self.entity_id,
entity_type: self.entity_type,
entity_map: self.entity_ref_counts.clone(),
@ -378,55 +418,54 @@ impl AnyWeakHandle {
}
}
impl<T> From<WeakHandle<T>> for AnyWeakHandle {
fn from(handle: WeakHandle<T>) -> Self {
handle.any_handle
impl<T> From<WeakModel<T>> for AnyWeakModel {
fn from(model: WeakModel<T>) -> Self {
model.any_model
}
}
impl Hash for AnyWeakHandle {
impl Hash for AnyWeakModel {
fn hash<H: Hasher>(&self, state: &mut H) {
self.entity_id.hash(state);
}
}
impl PartialEq for AnyWeakHandle {
impl PartialEq for AnyWeakModel {
fn eq(&self, other: &Self) -> bool {
self.entity_id == other.entity_id
}
}
impl Eq for AnyWeakHandle {}
impl Eq for AnyWeakModel {}
#[derive(Deref, DerefMut)]
pub struct WeakHandle<T> {
pub struct WeakModel<T> {
#[deref]
#[deref_mut]
any_handle: AnyWeakHandle,
any_model: AnyWeakModel,
entity_type: PhantomData<T>,
}
unsafe impl<T> Send for WeakHandle<T> {}
unsafe impl<T> Sync for WeakHandle<T> {}
unsafe impl<T> Send for WeakModel<T> {}
unsafe impl<T> Sync for WeakModel<T> {}
impl<T> Clone for WeakHandle<T> {
impl<T> Clone for WeakModel<T> {
fn clone(&self) -> Self {
Self {
any_handle: self.any_handle.clone(),
any_model: self.any_model.clone(),
entity_type: self.entity_type,
}
}
}
impl<T: 'static> WeakHandle<T> {
pub fn upgrade(&self) -> Option<Handle<T>> {
Some(Handle {
any_handle: self.any_handle.upgrade()?,
entity_type: self.entity_type,
})
impl<T: 'static> WeakModel<T> {
/// Upgrade this weak model reference into a strong model reference
pub fn upgrade(&self) -> Option<Model<T>> {
// Delegate to the trait implementation to keep behavior in one place.
Model::upgrade_from(self)
}
/// Update the entity referenced by this handle with the given function if
/// Update the entity referenced by this model with the given function if
/// the referenced entity still exists. Returns an error if the entity has
/// been released.
///
@ -436,7 +475,7 @@ impl<T: 'static> WeakHandle<T> {
pub fn update<C, R>(
&self,
cx: &mut C,
update: impl FnOnce(&mut T, &mut C::EntityContext<'_, '_, T>) -> R,
update: impl FnOnce(&mut T, &mut C::ModelContext<'_, T>) -> R,
) -> Result<R>
where
C: Context,
@ -445,28 +484,28 @@ impl<T: 'static> WeakHandle<T> {
crate::Flatten::flatten(
self.upgrade()
.ok_or_else(|| anyhow!("entity release"))
.map(|this| cx.update_entity(&this, update)),
.map(|this| cx.update_model(&this, update)),
)
}
}
impl<T> Hash for WeakHandle<T> {
impl<T> Hash for WeakModel<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.any_handle.hash(state);
self.any_model.hash(state);
}
}
impl<T> PartialEq for WeakHandle<T> {
impl<T> PartialEq for WeakModel<T> {
fn eq(&self, other: &Self) -> bool {
self.any_handle == other.any_handle
self.any_model == other.any_model
}
}
impl<T> Eq for WeakHandle<T> {}
impl<T> Eq for WeakModel<T> {}
impl<T> PartialEq<Handle<T>> for WeakHandle<T> {
fn eq(&self, other: &Handle<T>) -> bool {
self.entity_id() == other.entity_id()
impl<T> PartialEq<Model<T>> for WeakModel<T> {
fn eq(&self, other: &Model<T>) -> bool {
self.entity_id() == other.any_model.entity_id()
}
}

View file

@ -1,6 +1,6 @@
use crate::{
AppContext, AsyncAppContext, Context, Effect, EntityId, EventEmitter, Handle, MainThread,
Reference, Subscription, Task, WeakHandle,
AppContext, AsyncAppContext, Context, Effect, Entity, EntityId, EventEmitter, MainThread,
Model, Reference, Subscription, Task, WeakModel,
};
use derive_more::{Deref, DerefMut};
use futures::FutureExt;
@ -15,11 +15,11 @@ pub struct ModelContext<'a, T> {
#[deref]
#[deref_mut]
app: Reference<'a, AppContext>,
model_state: WeakHandle<T>,
model_state: WeakModel<T>,
}
impl<'a, T: 'static> ModelContext<'a, T> {
pub(crate) fn mutable(app: &'a mut AppContext, model_state: WeakHandle<T>) -> Self {
pub(crate) fn mutable(app: &'a mut AppContext, model_state: WeakModel<T>) -> Self {
Self {
app: Reference::Mutable(app),
model_state,
@ -30,30 +30,33 @@ impl<'a, T: 'static> ModelContext<'a, T> {
self.model_state.entity_id
}
pub fn handle(&self) -> Handle<T> {
self.weak_handle()
pub fn handle(&self) -> Model<T> {
self.weak_model()
.upgrade()
.expect("The entity must be alive if we have a model context")
}
pub fn weak_handle(&self) -> WeakHandle<T> {
pub fn weak_model(&self) -> WeakModel<T> {
self.model_state.clone()
}
pub fn observe<T2: 'static>(
pub fn observe<T2, E>(
&mut self,
handle: &Handle<T2>,
mut on_notify: impl FnMut(&mut T, Handle<T2>, &mut ModelContext<'_, T>) + Send + Sync + 'static,
entity: &E,
mut on_notify: impl FnMut(&mut T, E, &mut ModelContext<'_, T>) + Send + 'static,
) -> Subscription
where
T: Any + Send + Sync,
T: 'static + Send,
T2: 'static,
E: Entity<T2>,
{
let this = self.weak_handle();
let handle = handle.downgrade();
let this = self.weak_model();
let entity_id = entity.entity_id();
let handle = entity.downgrade();
self.app.observers.insert(
handle.entity_id,
entity_id,
Box::new(move |cx| {
if let Some((this, handle)) = this.upgrade().zip(handle.upgrade()) {
if let Some((this, handle)) = this.upgrade().zip(E::upgrade_from(&handle)) {
this.update(cx, |this, cx| on_notify(this, handle, cx));
true
} else {
@ -63,24 +66,24 @@ impl<'a, T: 'static> ModelContext<'a, T> {
)
}
pub fn subscribe<E: 'static + EventEmitter>(
pub fn subscribe<T2, E>(
&mut self,
handle: &Handle<E>,
mut on_event: impl FnMut(&mut T, Handle<E>, &E::Event, &mut ModelContext<'_, T>)
+ Send
+ Sync
+ 'static,
entity: &E,
mut on_event: impl FnMut(&mut T, E, &T2::Event, &mut ModelContext<'_, T>) + Send + 'static,
) -> Subscription
where
T: Any + Send + Sync,
T: 'static + Send,
T2: 'static + EventEmitter,
E: Entity<T2>,
{
let this = self.weak_handle();
let handle = handle.downgrade();
let this = self.weak_model();
let entity_id = entity.entity_id();
let entity = entity.downgrade();
self.app.event_listeners.insert(
handle.entity_id,
entity_id,
Box::new(move |event, cx| {
let event: &E::Event = event.downcast_ref().expect("invalid event type");
if let Some((this, handle)) = this.upgrade().zip(handle.upgrade()) {
let event: &T2::Event = event.downcast_ref().expect("invalid event type");
if let Some((this, handle)) = this.upgrade().zip(E::upgrade_from(&entity)) {
this.update(cx, |this, cx| on_event(this, handle, event, cx));
true
} else {
@ -92,7 +95,7 @@ impl<'a, T: 'static> ModelContext<'a, T> {
pub fn on_release(
&mut self,
mut on_release: impl FnMut(&mut T, &mut AppContext) + Send + Sync + 'static,
mut on_release: impl FnMut(&mut T, &mut AppContext) + Send + 'static,
) -> Subscription
where
T: 'static,
@ -106,17 +109,20 @@ impl<'a, T: 'static> ModelContext<'a, T> {
)
}
pub fn observe_release<E: 'static>(
pub fn observe_release<T2, E>(
&mut self,
handle: &Handle<E>,
mut on_release: impl FnMut(&mut T, &mut E, &mut ModelContext<'_, T>) + Send + Sync + 'static,
entity: &E,
mut on_release: impl FnMut(&mut T, &mut T2, &mut ModelContext<'_, T>) + Send + 'static,
) -> Subscription
where
T: Any + Send + Sync,
T: Any + Send,
T2: 'static,
E: Entity<T2>,
{
let this = self.weak_handle();
let entity_id = entity.entity_id();
let this = self.weak_model();
self.app.release_listeners.insert(
handle.entity_id,
entity_id,
Box::new(move |entity, cx| {
let entity = entity.downcast_mut().expect("invalid entity type");
if let Some(this) = this.upgrade() {
@ -128,12 +134,12 @@ impl<'a, T: 'static> ModelContext<'a, T> {
pub fn observe_global<G: 'static>(
&mut self,
mut f: impl FnMut(&mut T, &mut ModelContext<'_, T>) + Send + Sync + 'static,
mut f: impl FnMut(&mut T, &mut ModelContext<'_, T>) + Send + 'static,
) -> Subscription
where
T: Any + Send + Sync,
T: 'static + Send,
{
let handle = self.weak_handle();
let handle = self.weak_model();
self.global_observers.insert(
TypeId::of::<G>(),
Box::new(move |cx| handle.update(cx, |view, cx| f(view, cx)).is_ok()),
@ -142,13 +148,13 @@ impl<'a, T: 'static> ModelContext<'a, T> {
pub fn on_app_quit<Fut>(
&mut self,
mut on_quit: impl FnMut(&mut T, &mut ModelContext<T>) -> Fut + Send + Sync + 'static,
mut on_quit: impl FnMut(&mut T, &mut ModelContext<T>) -> Fut + Send + 'static,
) -> Subscription
where
Fut: 'static + Future<Output = ()> + Send,
T: Any + Send + Sync,
T: 'static + Send,
{
let handle = self.weak_handle();
let handle = self.weak_model();
self.app.quit_observers.insert(
(),
Box::new(move |cx| {
@ -177,7 +183,7 @@ impl<'a, T: 'static> ModelContext<'a, T> {
pub fn update_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R
where
G: 'static + Send + Sync,
G: 'static + Send,
{
let mut global = self.app.lease_global::<G>();
let result = f(&mut global, self);
@ -187,26 +193,26 @@ impl<'a, T: 'static> ModelContext<'a, T> {
pub fn spawn<Fut, R>(
&self,
f: impl FnOnce(WeakHandle<T>, AsyncAppContext) -> Fut + Send + 'static,
f: impl FnOnce(WeakModel<T>, AsyncAppContext) -> Fut + Send + 'static,
) -> Task<R>
where
T: 'static,
Fut: Future<Output = R> + Send + 'static,
R: Send + 'static,
{
let this = self.weak_handle();
let this = self.weak_model();
self.app.spawn(|cx| f(this, cx))
}
pub fn spawn_on_main<Fut, R>(
&self,
f: impl FnOnce(WeakHandle<T>, MainThread<AsyncAppContext>) -> Fut + Send + 'static,
f: impl FnOnce(WeakModel<T>, MainThread<AsyncAppContext>) -> Fut + Send + 'static,
) -> Task<R>
where
Fut: Future<Output = R> + 'static,
R: Send + 'static,
{
let this = self.weak_handle();
let this = self.weak_model();
self.app.spawn_on_main(|cx| f(this, cx))
}
}
@ -214,7 +220,7 @@ impl<'a, T: 'static> ModelContext<'a, T> {
impl<'a, T> ModelContext<'a, T>
where
T: EventEmitter,
T::Event: Send + Sync,
T::Event: Send,
{
pub fn emit(&mut self, event: T::Event) {
self.app.pending_effects.push_back(Effect::Emit {
@ -225,25 +231,25 @@ where
}
impl<'a, T> Context for ModelContext<'a, T> {
type EntityContext<'b, 'c, U> = ModelContext<'b, U>;
type ModelContext<'b, U> = ModelContext<'b, U>;
type Result<U> = U;
fn entity<U>(
fn build_model<U>(
&mut self,
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, U>) -> U,
) -> Handle<U>
build_model: impl FnOnce(&mut Self::ModelContext<'_, U>) -> U,
) -> Model<U>
where
U: 'static + Send + Sync,
U: 'static + Send,
{
self.app.entity(build_entity)
self.app.build_model(build_model)
}
fn update_entity<U: 'static, R>(
fn update_model<U: 'static, R>(
&mut self,
handle: &Handle<U>,
update: impl FnOnce(&mut U, &mut Self::EntityContext<'_, '_, U>) -> R,
handle: &Model<U>,
update: impl FnOnce(&mut U, &mut Self::ModelContext<'_, U>) -> R,
) -> R {
self.app.update_entity(handle, update)
self.app.update_model(handle, update)
}
}

View file

@ -1,10 +1,10 @@
use crate::{
AnyWindowHandle, AppContext, AsyncAppContext, Context, EventEmitter, Executor, Handle,
MainThread, ModelContext, Result, Task, TestDispatcher, TestPlatform, WindowContext,
AnyWindowHandle, AppContext, AsyncAppContext, Context, EventEmitter, Executor, MainThread,
Model, ModelContext, Result, Task, TestDispatcher, TestPlatform, WindowContext,
};
use futures::SinkExt;
use parking_lot::Mutex;
use std::{any::Any, future::Future, sync::Arc};
use std::{future::Future, sync::Arc};
#[derive(Clone)]
pub struct TestAppContext {
@ -13,27 +13,27 @@ pub struct TestAppContext {
}
impl Context for TestAppContext {
type EntityContext<'a, 'w, T> = ModelContext<'a, T>;
type ModelContext<'a, T> = ModelContext<'a, T>;
type Result<T> = T;
fn entity<T: 'static>(
fn build_model<T: 'static>(
&mut self,
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
) -> Self::Result<Handle<T>>
build_model: impl FnOnce(&mut Self::ModelContext<'_, T>) -> T,
) -> Self::Result<Model<T>>
where
T: Any + Send + Sync,
T: 'static + Send,
{
let mut lock = self.app.lock();
lock.entity(build_entity)
lock.build_model(build_model)
}
fn update_entity<T: 'static, R>(
fn update_model<T: 'static, R>(
&mut self,
handle: &Handle<T>,
update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R,
handle: &Model<T>,
update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
) -> Self::Result<R> {
let mut lock = self.app.lock();
lock.update_entity(handle, update)
lock.update_model(handle, update)
}
}
@ -151,9 +151,9 @@ impl TestAppContext {
}
}
pub fn subscribe<T: 'static + EventEmitter + Send + Sync>(
pub fn subscribe<T: 'static + EventEmitter + Send>(
&mut self,
entity: &Handle<T>,
entity: &Model<T>,
) -> futures::channel::mpsc::UnboundedReceiver<T::Event>
where
T::Event: 'static + Send + Clone,
@ -161,7 +161,7 @@ impl TestAppContext {
let (mut tx, rx) = futures::channel::mpsc::unbounded();
entity
.update(self, |_, cx: &mut ModelContext<T>| {
cx.subscribe(&entity, move |_, _, event, cx| {
cx.subscribe(entity, move |_, _, event, cx| {
cx.executor().block(tx.send(event.clone())).unwrap();
})
})
@ -169,20 +169,3 @@ impl TestAppContext {
rx
}
}
// pub fn subscribe<T: Entity>(
// entity: &impl Handle<T>,
// cx: &mut TestAppContext,
// ) -> Observation<T::Event>
// where
// T::Event: Clone,
// {
// let (tx, rx) = smol::channel::unbounded();
// let _subscription = cx.update(|cx| {
// cx.subscribe(entity, move |_, event, _| {
// let _ = smol::block_on(tx.send(event.clone()));
// })
// });
// Observation { rx, _subscription }
// }

View file

@ -4,7 +4,7 @@ pub(crate) use smallvec::SmallVec;
use std::{any::Any, mem};
pub trait Element<V: 'static> {
type ElementState: 'static;
type ElementState: 'static + Send;
fn id(&self) -> Option<ElementId>;
@ -16,8 +16,6 @@ pub trait Element<V: 'static> {
element_state: Option<Self::ElementState>,
cx: &mut ViewContext<V>,
) -> Self::ElementState;
// where
// V: Any + Send + Sync;
fn layout(
&mut self,
@ -25,8 +23,6 @@ pub trait Element<V: 'static> {
element_state: &mut Self::ElementState,
cx: &mut ViewContext<V>,
) -> LayoutId;
// where
// V: Any + Send + Sync;
fn paint(
&mut self,
@ -35,9 +31,6 @@ pub trait Element<V: 'static> {
element_state: &mut Self::ElementState,
cx: &mut ViewContext<V>,
);
// where
// Self::ViewState: Any + Send + Sync;
}
#[derive(Deref, DerefMut, Default, Clone, Debug, Eq, PartialEq, Hash)]
@ -104,8 +97,7 @@ impl<V, E: Element<V>> RenderedElement<V, E> {
impl<V, E> ElementObject<V> for RenderedElement<V, E>
where
E: Element<V>,
// E::ViewState: Any + Send + Sync,
E::ElementState: Any + Send + Sync,
E::ElementState: 'static + Send,
{
fn initialize(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) {
let frame_state = if let Some(id) = self.element.id() {
@ -178,18 +170,16 @@ where
}
}
pub struct AnyElement<V>(Box<dyn ElementObject<V> + Send + Sync>);
pub struct AnyElement<V>(Box<dyn ElementObject<V> + Send>);
unsafe impl<V> Send for AnyElement<V> {}
unsafe impl<V> Sync for AnyElement<V> {}
impl<V> AnyElement<V> {
pub fn new<E>(element: E) -> Self
where
V: 'static,
E: 'static + Send + Sync,
E: Element<V>,
E::ElementState: Any + Send + Sync,
E: 'static + Element<V> + Send,
E::ElementState: Any + Send,
{
AnyElement(Box::new(RenderedElement::new(element)))
}
@ -230,8 +220,8 @@ impl<V> Component<V> for AnyElement<V> {
impl<V, E, F> Element<V> for Option<F>
where
V: 'static,
E: 'static + Component<V> + Send + Sync,
F: FnOnce(&mut V, &mut ViewContext<'_, '_, V>) -> E + Send + Sync + 'static,
E: 'static + Component<V> + Send,
F: FnOnce(&mut V, &mut ViewContext<'_, '_, V>) -> E + Send + 'static,
{
type ElementState = AnyElement<V>;
@ -274,8 +264,8 @@ where
impl<V, E, F> Component<V> for Option<F>
where
V: 'static,
E: 'static + Component<V> + Send + Sync,
F: FnOnce(&mut V, &mut ViewContext<'_, '_, V>) -> E + Send + Sync + 'static,
E: 'static + Component<V> + Send,
F: FnOnce(&mut V, &mut ViewContext<'_, '_, V>) -> E + Send + 'static,
{
fn render(self) -> AnyElement<V> {
AnyElement::new(self)
@ -285,8 +275,8 @@ where
impl<V, E, F> Component<V> for F
where
V: 'static,
E: 'static + Component<V> + Send + Sync,
F: FnOnce(&mut V, &mut ViewContext<'_, '_, V>) -> E + Send + Sync + 'static,
E: 'static + Component<V> + Send,
F: FnOnce(&mut V, &mut ViewContext<'_, '_, V>) -> E + Send + 'static,
{
fn render(self) -> AnyElement<V> {
AnyElement::new(Some(self))

View file

@ -1,8 +1,7 @@
use crate::{AppContext, PlatformDispatcher};
use futures::{channel::mpsc, pin_mut};
use futures::{channel::mpsc, pin_mut, FutureExt};
use smol::prelude::*;
use std::{
borrow::BorrowMut,
fmt::Debug,
marker::PhantomData,
mem,
@ -181,16 +180,16 @@ impl Executor {
duration: Duration,
future: impl Future<Output = R>,
) -> Result<R, impl Future<Output = R>> {
let mut future = Box::pin(future);
let timeout = {
let future = &mut future;
async {
let timer = async {
self.timer(duration).await;
Err(())
};
let future = async move { Ok(future.await) };
timer.race(future).await
let mut future = Box::pin(future.fuse());
if duration.is_zero() {
return Err(future);
}
let mut timer = self.timer(duration).fuse();
let timeout = async {
futures::select_biased! {
value = future => Ok(value),
_ = timer => Err(()),
}
};
match self.block(timeout) {

View file

@ -4,12 +4,11 @@ use crate::{
};
use refineable::Refineable;
use smallvec::SmallVec;
use std::sync::Arc;
pub type FocusListeners<V> = SmallVec<[FocusListener<V>; 2]>;
pub type FocusListener<V> =
Arc<dyn Fn(&mut V, &FocusHandle, &FocusEvent, &mut ViewContext<V>) + Send + Sync + 'static>;
Box<dyn Fn(&mut V, &FocusHandle, &FocusEvent, &mut ViewContext<V>) + Send + 'static>;
pub trait Focusable<V: 'static>: Element<V> {
fn focus_listeners(&mut self) -> &mut FocusListeners<V>;
@ -43,13 +42,13 @@ pub trait Focusable<V: 'static>: Element<V> {
fn on_focus(
mut self,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + Send + Sync + 'static,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + Send + 'static,
) -> Self
where
Self: Sized,
{
self.focus_listeners()
.push(Arc::new(move |view, focus_handle, event, cx| {
.push(Box::new(move |view, focus_handle, event, cx| {
if event.focused.as_ref() == Some(focus_handle) {
listener(view, event, cx)
}
@ -59,13 +58,13 @@ pub trait Focusable<V: 'static>: Element<V> {
fn on_blur(
mut self,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + Send + Sync + 'static,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + Send + 'static,
) -> Self
where
Self: Sized,
{
self.focus_listeners()
.push(Arc::new(move |view, focus_handle, event, cx| {
.push(Box::new(move |view, focus_handle, event, cx| {
if event.blurred.as_ref() == Some(focus_handle) {
listener(view, event, cx)
}
@ -75,13 +74,13 @@ pub trait Focusable<V: 'static>: Element<V> {
fn on_focus_in(
mut self,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + Send + Sync + 'static,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + Send + 'static,
) -> Self
where
Self: Sized,
{
self.focus_listeners()
.push(Arc::new(move |view, focus_handle, event, cx| {
.push(Box::new(move |view, focus_handle, event, cx| {
let descendant_blurred = event
.blurred
.as_ref()
@ -100,13 +99,13 @@ pub trait Focusable<V: 'static>: Element<V> {
fn on_focus_out(
mut self,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + Send + Sync + 'static,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + Send + 'static,
) -> Self
where
Self: Sized,
{
self.focus_listeners()
.push(Arc::new(move |view, focus_handle, event, cx| {
.push(Box::new(move |view, focus_handle, event, cx| {
let descendant_blurred = event
.blurred
.as_ref()
@ -123,7 +122,7 @@ pub trait Focusable<V: 'static>: Element<V> {
}
}
pub trait ElementFocus<V: 'static>: 'static + Send + Sync {
pub trait ElementFocus<V: 'static>: 'static + Send {
fn as_focusable(&self) -> Option<&FocusEnabled<V>>;
fn as_focusable_mut(&mut self) -> Option<&mut FocusEnabled<V>>;
@ -138,7 +137,7 @@ pub trait ElementFocus<V: 'static>: 'static + Send + Sync {
.focus_handle
.get_or_insert_with(|| focus_handle.unwrap_or_else(|| cx.focus_handle()))
.clone();
for listener in focusable.focus_listeners.iter().cloned() {
for listener in focusable.focus_listeners.drain(..) {
let focus_handle = focus_handle.clone();
cx.on_focus_changed(move |view, event, cx| {
listener(view, &focus_handle, event, cx)

View file

@ -24,6 +24,12 @@ mod util;
mod view;
mod window;
mod private {
/// A mechanism for restricting implementations of a trait to only those in GPUI.
/// See: https://predr.ag/blog/definitive-guide-to-sealed-traits-in-rust/
pub trait Sealed {}
}
pub use action::*;
pub use anyhow::Result;
pub use app::*;
@ -39,6 +45,7 @@ pub use image_cache::*;
pub use interactive::*;
pub use keymap::*;
pub use platform::*;
use private::Sealed;
pub use refineable::*;
pub use scene::*;
pub use serde;
@ -67,26 +74,53 @@ use std::{
};
use taffy::TaffyLayoutEngine;
type AnyBox = Box<dyn Any + Send + Sync>;
type AnyBox = Box<dyn Any + Send>;
pub trait Context {
type EntityContext<'a, 'w, T>;
type ModelContext<'a, T>;
type Result<T>;
fn entity<T>(
fn build_model<T>(
&mut self,
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
) -> Self::Result<Handle<T>>
build_model: impl FnOnce(&mut Self::ModelContext<'_, T>) -> T,
) -> Self::Result<Model<T>>
where
T: 'static + Send + Sync;
T: 'static + Send;
fn update_entity<T: 'static, R>(
fn update_model<T: 'static, R>(
&mut self,
handle: &Handle<T>,
update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R,
handle: &Model<T>,
update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
) -> Self::Result<R>;
}
pub trait VisualContext: Context {
type ViewContext<'a, 'w, V>;
fn build_view<V>(
&mut self,
build_view_state: impl FnOnce(&mut Self::ViewContext<'_, '_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static + Send;
fn update_view<V: 'static, R>(
&mut self,
view: &View<V>,
update: impl FnOnce(&mut V, &mut Self::ViewContext<'_, '_, V>) -> R,
) -> Self::Result<R>;
}
pub trait Entity<T>: Sealed {
type Weak: 'static + Send;
fn entity_id(&self) -> EntityId;
fn downgrade(&self) -> Self::Weak;
fn upgrade_from(weak: &Self::Weak) -> Option<Self>
where
Self: Sized;
}
pub enum GlobalKey {
Numeric(usize),
View(EntityId),
@ -111,37 +145,37 @@ impl<T> DerefMut for MainThread<T> {
}
impl<C: Context> Context for MainThread<C> {
type EntityContext<'a, 'w, T> = MainThread<C::EntityContext<'a, 'w, T>>;
type ModelContext<'a, T> = MainThread<C::ModelContext<'a, T>>;
type Result<T> = C::Result<T>;
fn entity<T>(
fn build_model<T>(
&mut self,
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
) -> Self::Result<Handle<T>>
build_model: impl FnOnce(&mut Self::ModelContext<'_, T>) -> T,
) -> Self::Result<Model<T>>
where
T: Any + Send + Sync,
T: 'static + Send,
{
self.0.entity(|cx| {
self.0.build_model(|cx| {
let cx = unsafe {
mem::transmute::<
&mut C::EntityContext<'_, '_, T>,
&mut MainThread<C::EntityContext<'_, '_, T>>,
&mut C::ModelContext<'_, T>,
&mut MainThread<C::ModelContext<'_, T>>,
>(cx)
};
build_entity(cx)
build_model(cx)
})
}
fn update_entity<T: 'static, R>(
fn update_model<T: 'static, R>(
&mut self,
handle: &Handle<T>,
update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R,
handle: &Model<T>,
update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
) -> Self::Result<R> {
self.0.update_entity(handle, |entity, cx| {
self.0.update_model(handle, |entity, cx| {
let cx = unsafe {
mem::transmute::<
&mut C::EntityContext<'_, '_, T>,
&mut MainThread<C::EntityContext<'_, '_, T>>,
&mut C::ModelContext<'_, T>,
&mut MainThread<C::ModelContext<'_, T>>,
>(cx)
};
update(entity, cx)
@ -149,12 +183,50 @@ impl<C: Context> Context for MainThread<C> {
}
}
impl<C: VisualContext> VisualContext for MainThread<C> {
type ViewContext<'a, 'w, V> = MainThread<C::ViewContext<'a, 'w, V>>;
fn build_view<V>(
&mut self,
build_view_state: impl FnOnce(&mut Self::ViewContext<'_, '_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static + Send,
{
self.0.build_view(|cx| {
let cx = unsafe {
mem::transmute::<
&mut C::ViewContext<'_, '_, V>,
&mut MainThread<C::ViewContext<'_, '_, V>>,
>(cx)
};
build_view_state(cx)
})
}
fn update_view<V: 'static, R>(
&mut self,
view: &View<V>,
update: impl FnOnce(&mut V, &mut Self::ViewContext<'_, '_, V>) -> R,
) -> Self::Result<R> {
self.0.update_view(view, |view_state, cx| {
let cx = unsafe {
mem::transmute::<
&mut C::ViewContext<'_, '_, V>,
&mut MainThread<C::ViewContext<'_, '_, V>>,
>(cx)
};
update(view_state, cx)
})
}
}
pub trait BorrowAppContext {
fn with_text_style<F, R>(&mut self, style: TextStyleRefinement, f: F) -> R
where
F: FnOnce(&mut Self) -> R;
fn set_global<T: Send + Sync + 'static>(&mut self, global: T);
fn set_global<T: Send + 'static>(&mut self, global: T);
}
impl<C> BorrowAppContext for C
@ -171,7 +243,7 @@ where
result
}
fn set_global<G: 'static + Send + Sync>(&mut self, global: G) {
fn set_global<G: 'static + Send>(&mut self, global: G) {
self.borrow_mut().set_global(global)
}
}

View file

@ -1,7 +1,8 @@
use crate::{
point, px, view, Action, AnyBox, AnyDrag, AppContext, BorrowWindow, Bounds, Component,
DispatchContext, DispatchPhase, Element, ElementId, FocusHandle, KeyMatch, Keystroke,
Modifiers, Overflow, Pixels, Point, SharedString, Size, Style, StyleRefinement, ViewContext,
div, point, px, Action, AnyDrag, AnyView, AppContext, BorrowWindow, Bounds, Component,
DispatchContext, DispatchPhase, Div, Element, ElementId, FocusHandle, KeyMatch, Keystroke,
Modifiers, Overflow, Pixels, Point, Render, SharedString, Size, Style, StyleRefinement, View,
ViewContext,
};
use collections::HashMap;
use derive_more::{Deref, DerefMut};
@ -12,6 +13,7 @@ use std::{
any::{Any, TypeId},
fmt::Debug,
marker::PhantomData,
mem,
ops::Deref,
path::PathBuf,
sync::Arc,
@ -48,14 +50,14 @@ pub trait StatelessInteractive<V: 'static>: Element<V> {
fn on_mouse_down(
mut self,
button: MouseButton,
handler: impl Fn(&mut V, &MouseDownEvent, &mut ViewContext<V>) + Send + Sync + 'static,
handler: impl Fn(&mut V, &MouseDownEvent, &mut ViewContext<V>) + Send + 'static,
) -> Self
where
Self: Sized,
{
self.stateless_interaction()
.mouse_down_listeners
.push(Arc::new(move |view, event, bounds, phase, cx| {
.push(Box::new(move |view, event, bounds, phase, cx| {
if phase == DispatchPhase::Bubble
&& event.button == button
&& bounds.contains_point(&event.position)
@ -69,14 +71,14 @@ pub trait StatelessInteractive<V: 'static>: Element<V> {
fn on_mouse_up(
mut self,
button: MouseButton,
handler: impl Fn(&mut V, &MouseUpEvent, &mut ViewContext<V>) + Send + Sync + 'static,
handler: impl Fn(&mut V, &MouseUpEvent, &mut ViewContext<V>) + Send + 'static,
) -> Self
where
Self: Sized,
{
self.stateless_interaction()
.mouse_up_listeners
.push(Arc::new(move |view, event, bounds, phase, cx| {
.push(Box::new(move |view, event, bounds, phase, cx| {
if phase == DispatchPhase::Bubble
&& event.button == button
&& bounds.contains_point(&event.position)
@ -90,14 +92,14 @@ pub trait StatelessInteractive<V: 'static>: Element<V> {
fn on_mouse_down_out(
mut self,
button: MouseButton,
handler: impl Fn(&mut V, &MouseDownEvent, &mut ViewContext<V>) + Send + Sync + 'static,
handler: impl Fn(&mut V, &MouseDownEvent, &mut ViewContext<V>) + Send + 'static,
) -> Self
where
Self: Sized,
{
self.stateless_interaction()
.mouse_down_listeners
.push(Arc::new(move |view, event, bounds, phase, cx| {
.push(Box::new(move |view, event, bounds, phase, cx| {
if phase == DispatchPhase::Capture
&& event.button == button
&& !bounds.contains_point(&event.position)
@ -111,14 +113,14 @@ pub trait StatelessInteractive<V: 'static>: Element<V> {
fn on_mouse_up_out(
mut self,
button: MouseButton,
handler: impl Fn(&mut V, &MouseUpEvent, &mut ViewContext<V>) + Send + Sync + 'static,
handler: impl Fn(&mut V, &MouseUpEvent, &mut ViewContext<V>) + Send + 'static,
) -> Self
where
Self: Sized,
{
self.stateless_interaction()
.mouse_up_listeners
.push(Arc::new(move |view, event, bounds, phase, cx| {
.push(Box::new(move |view, event, bounds, phase, cx| {
if phase == DispatchPhase::Capture
&& event.button == button
&& !bounds.contains_point(&event.position)
@ -131,14 +133,14 @@ pub trait StatelessInteractive<V: 'static>: Element<V> {
fn on_mouse_move(
mut self,
handler: impl Fn(&mut V, &MouseMoveEvent, &mut ViewContext<V>) + Send + Sync + 'static,
handler: impl Fn(&mut V, &MouseMoveEvent, &mut ViewContext<V>) + Send + 'static,
) -> Self
where
Self: Sized,
{
self.stateless_interaction()
.mouse_move_listeners
.push(Arc::new(move |view, event, bounds, phase, cx| {
.push(Box::new(move |view, event, bounds, phase, cx| {
if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
handler(view, event, cx);
}
@ -148,14 +150,14 @@ pub trait StatelessInteractive<V: 'static>: Element<V> {
fn on_scroll_wheel(
mut self,
handler: impl Fn(&mut V, &ScrollWheelEvent, &mut ViewContext<V>) + Send + Sync + 'static,
handler: impl Fn(&mut V, &ScrollWheelEvent, &mut ViewContext<V>) + Send + 'static,
) -> Self
where
Self: Sized,
{
self.stateless_interaction()
.scroll_wheel_listeners
.push(Arc::new(move |view, event, bounds, phase, cx| {
.push(Box::new(move |view, event, bounds, phase, cx| {
if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
handler(view, event, cx);
}
@ -176,14 +178,14 @@ pub trait StatelessInteractive<V: 'static>: Element<V> {
fn on_action<A: 'static>(
mut self,
listener: impl Fn(&mut V, &A, DispatchPhase, &mut ViewContext<V>) + Send + Sync + 'static,
listener: impl Fn(&mut V, &A, DispatchPhase, &mut ViewContext<V>) + Send + 'static,
) -> Self
where
Self: Sized,
{
self.stateless_interaction().key_listeners.push((
TypeId::of::<A>(),
Arc::new(move |view, event, _, phase, cx| {
Box::new(move |view, event, _, phase, cx| {
let event = event.downcast_ref().unwrap();
listener(view, event, phase, cx);
None
@ -194,17 +196,14 @@ pub trait StatelessInteractive<V: 'static>: Element<V> {
fn on_key_down(
mut self,
listener: impl Fn(&mut V, &KeyDownEvent, DispatchPhase, &mut ViewContext<V>)
+ Send
+ Sync
+ 'static,
listener: impl Fn(&mut V, &KeyDownEvent, DispatchPhase, &mut ViewContext<V>) + Send + 'static,
) -> Self
where
Self: Sized,
{
self.stateless_interaction().key_listeners.push((
TypeId::of::<KeyDownEvent>(),
Arc::new(move |view, event, _, phase, cx| {
Box::new(move |view, event, _, phase, cx| {
let event = event.downcast_ref().unwrap();
listener(view, event, phase, cx);
None
@ -215,17 +214,14 @@ pub trait StatelessInteractive<V: 'static>: Element<V> {
fn on_key_up(
mut self,
listener: impl Fn(&mut V, &KeyUpEvent, DispatchPhase, &mut ViewContext<V>)
+ Send
+ Sync
+ 'static,
listener: impl Fn(&mut V, &KeyUpEvent, DispatchPhase, &mut ViewContext<V>) + Send + 'static,
) -> Self
where
Self: Sized,
{
self.stateless_interaction().key_listeners.push((
TypeId::of::<KeyUpEvent>(),
Arc::new(move |view, event, _, phase, cx| {
Box::new(move |view, event, _, phase, cx| {
let event = event.downcast_ref().unwrap();
listener(view, event, phase, cx);
None
@ -262,17 +258,17 @@ pub trait StatelessInteractive<V: 'static>: Element<V> {
self
}
fn on_drop<S: 'static>(
fn on_drop<W: 'static + Send>(
mut self,
listener: impl Fn(&mut V, S, &mut ViewContext<V>) + Send + Sync + 'static,
listener: impl Fn(&mut V, View<W>, &mut ViewContext<V>) + Send + 'static,
) -> Self
where
Self: Sized,
{
self.stateless_interaction().drop_listeners.push((
TypeId::of::<S>(),
Arc::new(move |view, drag_state, cx| {
listener(view, *drag_state.downcast().unwrap(), cx);
TypeId::of::<W>(),
Box::new(move |view, dragged_view, cx| {
listener(view, dragged_view.downcast().unwrap(), cx);
}),
));
self
@ -307,54 +303,39 @@ pub trait StatefulInteractive<V: 'static>: StatelessInteractive<V> {
fn on_click(
mut self,
listener: impl Fn(&mut V, &ClickEvent, &mut ViewContext<V>) + Send + Sync + 'static,
listener: impl Fn(&mut V, &ClickEvent, &mut ViewContext<V>) + Send + 'static,
) -> Self
where
Self: Sized,
{
self.stateful_interaction()
.click_listeners
.push(Arc::new(move |view, event, cx| listener(view, event, cx)));
.push(Box::new(move |view, event, cx| listener(view, event, cx)));
self
}
fn on_drag<S, R, E>(
fn on_drag<W>(
mut self,
listener: impl Fn(&mut V, &mut ViewContext<V>) -> Drag<S, R, V, E> + Send + Sync + 'static,
listener: impl Fn(&mut V, &mut ViewContext<V>) -> View<W> + Send + 'static,
) -> Self
where
Self: Sized,
S: Any + Send + Sync,
R: Fn(&mut V, &mut ViewContext<V>) -> E,
R: 'static + Send + Sync,
E: Component<V>,
W: 'static + Send + Render,
{
debug_assert!(
self.stateful_interaction().drag_listener.is_none(),
"calling on_drag more than once on the same element is not supported"
);
self.stateful_interaction().drag_listener =
Some(Arc::new(move |view_state, cursor_offset, cx| {
let drag = listener(view_state, cx);
let view_handle = cx.handle().upgrade().unwrap();
let drag_handle_view = Some(
view(view_handle, move |view_state, cx| {
(drag.render_drag_handle)(view_state, cx)
})
.into_any(),
);
AnyDrag {
drag_handle_view,
cursor_offset,
state: Box::new(drag.state),
state_type: TypeId::of::<S>(),
}
Some(Box::new(move |view_state, cursor_offset, cx| AnyDrag {
view: listener(view_state, cx).into(),
cursor_offset,
}));
self
}
}
pub trait ElementInteraction<V: 'static>: 'static + Send + Sync {
pub trait ElementInteraction<V: 'static>: 'static + Send {
fn as_stateless(&self) -> &StatelessInteraction<V>;
fn as_stateless_mut(&mut self) -> &mut StatelessInteraction<V>;
fn as_stateful(&self) -> Option<&StatefulInteraction<V>>;
@ -369,7 +350,7 @@ pub trait ElementInteraction<V: 'static>: 'static + Send + Sync {
cx.with_element_id(stateful.id.clone(), |global_id, cx| {
stateful.key_listeners.push((
TypeId::of::<KeyDownEvent>(),
Arc::new(move |_, key_down, context, phase, cx| {
Box::new(move |_, key_down, context, phase, cx| {
if phase == DispatchPhase::Bubble {
let key_down = key_down.downcast_ref::<KeyDownEvent>().unwrap();
if let KeyMatch::Some(action) =
@ -387,9 +368,9 @@ pub trait ElementInteraction<V: 'static>: 'static + Send + Sync {
result
})
} else {
let stateless = self.as_stateless();
let stateless = self.as_stateless_mut();
cx.with_key_dispatch_context(stateless.dispatch_context.clone(), |cx| {
cx.with_key_listeners(&stateless.key_listeners, f)
cx.with_key_listeners(mem::take(&mut stateless.key_listeners), f)
})
}
}
@ -417,7 +398,7 @@ pub trait ElementInteraction<V: 'static>: 'static + Send + Sync {
if let Some(drag) = cx.active_drag.take() {
for (state_type, group_drag_style) in &self.as_stateless().group_drag_over_styles {
if let Some(group_bounds) = GroupBounds::get(&group_drag_style.group, cx) {
if *state_type == drag.state_type
if *state_type == drag.view.entity_type()
&& group_bounds.contains_point(&mouse_position)
{
style.refine(&group_drag_style.style);
@ -426,7 +407,8 @@ pub trait ElementInteraction<V: 'static>: 'static + Send + Sync {
}
for (state_type, drag_over_style) in &self.as_stateless().drag_over_styles {
if *state_type == drag.state_type && bounds.contains_point(&mouse_position) {
if *state_type == drag.view.entity_type() && bounds.contains_point(&mouse_position)
{
style.refine(drag_over_style);
}
}
@ -455,26 +437,26 @@ pub trait ElementInteraction<V: 'static>: 'static + Send + Sync {
element_state: &mut InteractiveElementState,
cx: &mut ViewContext<V>,
) {
let stateless = self.as_stateless();
for listener in stateless.mouse_down_listeners.iter().cloned() {
let stateless = self.as_stateless_mut();
for listener in stateless.mouse_down_listeners.drain(..) {
cx.on_mouse_event(move |state, event: &MouseDownEvent, phase, cx| {
listener(state, event, &bounds, phase, cx);
})
}
for listener in stateless.mouse_up_listeners.iter().cloned() {
for listener in stateless.mouse_up_listeners.drain(..) {
cx.on_mouse_event(move |state, event: &MouseUpEvent, phase, cx| {
listener(state, event, &bounds, phase, cx);
})
}
for listener in stateless.mouse_move_listeners.iter().cloned() {
for listener in stateless.mouse_move_listeners.drain(..) {
cx.on_mouse_event(move |state, event: &MouseMoveEvent, phase, cx| {
listener(state, event, &bounds, phase, cx);
})
}
for listener in stateless.scroll_wheel_listeners.iter().cloned() {
for listener in stateless.scroll_wheel_listeners.drain(..) {
cx.on_mouse_event(move |state, event: &ScrollWheelEvent, phase, cx| {
listener(state, event, &bounds, phase, cx);
})
@ -510,11 +492,11 @@ pub trait ElementInteraction<V: 'static>: 'static + Send + Sync {
}
if cx.active_drag.is_some() {
let drop_listeners = stateless.drop_listeners.clone();
let drop_listeners = mem::take(&mut stateless.drop_listeners);
cx.on_mouse_event(move |view, event: &MouseUpEvent, phase, cx| {
if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
if let Some(drag_state_type) =
cx.active_drag.as_ref().map(|drag| drag.state_type)
cx.active_drag.as_ref().map(|drag| drag.view.entity_type())
{
for (drop_state_type, listener) in &drop_listeners {
if *drop_state_type == drag_state_type {
@ -522,7 +504,7 @@ pub trait ElementInteraction<V: 'static>: 'static + Send + Sync {
.active_drag
.take()
.expect("checked for type drag state type above");
listener(view, drag.state, cx);
listener(view, drag.view.clone(), cx);
cx.notify();
cx.stop_propagation();
}
@ -532,9 +514,9 @@ pub trait ElementInteraction<V: 'static>: 'static + Send + Sync {
});
}
if let Some(stateful) = self.as_stateful() {
let click_listeners = stateful.click_listeners.clone();
let drag_listener = stateful.drag_listener.clone();
if let Some(stateful) = self.as_stateful_mut() {
let click_listeners = mem::take(&mut stateful.click_listeners);
let drag_listener = mem::take(&mut stateful.drag_listener);
if !click_listeners.is_empty() || drag_listener.is_some() {
let pending_mouse_down = element_state.pending_mouse_down.clone();
@ -690,7 +672,7 @@ impl<V> From<ElementId> for StatefulInteraction<V> {
}
}
type DropListener<V> = dyn Fn(&mut V, AnyBox, &mut ViewContext<V>) + 'static + Send + Sync;
type DropListener<V> = dyn Fn(&mut V, AnyView, &mut ViewContext<V>) + 'static + Send;
pub struct StatelessInteraction<V> {
pub dispatch_context: DispatchContext,
@ -703,7 +685,7 @@ pub struct StatelessInteraction<V> {
pub group_hover_style: Option<GroupStyle>,
drag_over_styles: SmallVec<[(TypeId, StyleRefinement); 2]>,
group_drag_over_styles: SmallVec<[(TypeId, GroupStyle); 2]>,
drop_listeners: SmallVec<[(TypeId, Arc<DropListener<V>>); 2]>,
drop_listeners: SmallVec<[(TypeId, Box<DropListener<V>>); 2]>,
}
impl<V> StatelessInteraction<V> {
@ -871,7 +853,7 @@ pub struct Drag<S, R, V, E>
where
R: Fn(&mut V, &mut ViewContext<V>) -> E,
V: 'static,
E: Component<V>,
E: Component<()>,
{
pub state: S,
pub render_drag_handle: R,
@ -882,7 +864,7 @@ impl<S, R, V, E> Drag<S, R, V, E>
where
R: Fn(&mut V, &mut ViewContext<V>) -> E,
V: 'static,
E: Component<V>,
E: Component<()>,
{
pub fn new(state: S, render_drag_handle: R) -> Self {
Drag {
@ -893,6 +875,10 @@ where
}
}
// impl<S, R, V, E> Render for Drag<S, R, V, E> {
// // fn render(&mut self, cx: ViewContext<Self>) ->
// }
#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug)]
pub enum MouseButton {
Left,
@ -1000,6 +986,14 @@ impl Deref for MouseExitEvent {
#[derive(Debug, Clone, Default)]
pub struct ExternalPaths(pub(crate) SmallVec<[PathBuf; 2]>);
impl Render for ExternalPaths {
type Element = Div<Self>;
fn render(&mut self, _: &mut ViewContext<Self>) -> Self::Element {
div() // Intentionally left empty because the platform will render icons for the dragged files
}
}
#[derive(Debug, Clone)]
pub enum FileDropEvent {
Entered {
@ -1082,40 +1076,35 @@ pub struct FocusEvent {
pub focused: Option<FocusHandle>,
}
pub type MouseDownListener<V> = Arc<
pub type MouseDownListener<V> = Box<
dyn Fn(&mut V, &MouseDownEvent, &Bounds<Pixels>, DispatchPhase, &mut ViewContext<V>)
+ Send
+ Sync
+ 'static,
>;
pub type MouseUpListener<V> = Arc<
pub type MouseUpListener<V> = Box<
dyn Fn(&mut V, &MouseUpEvent, &Bounds<Pixels>, DispatchPhase, &mut ViewContext<V>)
+ Send
+ Sync
+ 'static,
>;
pub type MouseMoveListener<V> = Arc<
pub type MouseMoveListener<V> = Box<
dyn Fn(&mut V, &MouseMoveEvent, &Bounds<Pixels>, DispatchPhase, &mut ViewContext<V>)
+ Send
+ Sync
+ 'static,
>;
pub type ScrollWheelListener<V> = Arc<
pub type ScrollWheelListener<V> = Box<
dyn Fn(&mut V, &ScrollWheelEvent, &Bounds<Pixels>, DispatchPhase, &mut ViewContext<V>)
+ Send
+ Sync
+ 'static,
>;
pub type ClickListener<V> =
Arc<dyn Fn(&mut V, &ClickEvent, &mut ViewContext<V>) + Send + Sync + 'static>;
pub type ClickListener<V> = Box<dyn Fn(&mut V, &ClickEvent, &mut ViewContext<V>) + Send + 'static>;
pub(crate) type DragListener<V> =
Arc<dyn Fn(&mut V, Point<Pixels>, &mut ViewContext<V>) -> AnyDrag + Send + Sync + 'static>;
Box<dyn Fn(&mut V, Point<Pixels>, &mut ViewContext<V>) -> AnyDrag + Send + 'static>;
pub type KeyListener<V> = Arc<
pub type KeyListener<V> = Box<
dyn Fn(
&mut V,
&dyn Any,
@ -1124,6 +1113,5 @@ pub type KeyListener<V> = Arc<
&mut ViewContext<V>,
) -> Option<Box<dyn Action>>
+ Send
+ Sync
+ 'static,
>;

View file

@ -1,17 +1,17 @@
use crate::{Action, DispatchContext, Keymap, KeymapVersion, Keystroke};
use parking_lot::RwLock;
use parking_lot::Mutex;
use smallvec::SmallVec;
use std::sync::Arc;
pub struct KeyMatcher {
pending_keystrokes: Vec<Keystroke>,
keymap: Arc<RwLock<Keymap>>,
keymap: Arc<Mutex<Keymap>>,
keymap_version: KeymapVersion,
}
impl KeyMatcher {
pub fn new(keymap: Arc<RwLock<Keymap>>) -> Self {
let keymap_version = keymap.read().version();
pub fn new(keymap: Arc<Mutex<Keymap>>) -> Self {
let keymap_version = keymap.lock().version();
Self {
pending_keystrokes: Vec::new(),
keymap_version,
@ -21,7 +21,7 @@ impl KeyMatcher {
// todo!("replace with a function that calls an FnMut for every binding matching the action")
// pub fn bindings_for_action(&self, action_id: TypeId) -> impl Iterator<Item = &Binding> {
// self.keymap.read().bindings_for_action(action_id)
// self.keymap.lock().bindings_for_action(action_id)
// }
pub fn clear_pending(&mut self) {
@ -46,7 +46,7 @@ impl KeyMatcher {
keystroke: &Keystroke,
context_stack: &[&DispatchContext],
) -> KeyMatch {
let keymap = self.keymap.read();
let keymap = self.keymap.lock();
// Clear pending keystrokes if the keymap has changed since the last matched keystroke.
if keymap.version() != self.keymap_version {
self.keymap_version = keymap.version();
@ -89,7 +89,7 @@ impl KeyMatcher {
contexts: &[&DispatchContext],
) -> Option<SmallVec<[Keystroke; 2]>> {
self.keymap
.read()
.lock()
.bindings()
.iter()
.rev()

View file

@ -185,8 +185,7 @@ impl MacPlatform {
}))
}
unsafe fn read_from_pasteboard(&self, kind: id) -> Option<&[u8]> {
let pasteboard = self.0.lock().pasteboard;
unsafe fn read_from_pasteboard(&self, pasteboard: *mut Object, kind: id) -> Option<&[u8]> {
let data = pasteboard.dataForType(kind);
if data == nil {
None
@ -787,14 +786,16 @@ impl Platform for MacPlatform {
fn read_from_clipboard(&self) -> Option<ClipboardItem> {
let state = self.0.lock();
unsafe {
if let Some(text_bytes) = self.read_from_pasteboard(NSPasteboardTypeString) {
if let Some(text_bytes) =
self.read_from_pasteboard(state.pasteboard, NSPasteboardTypeString)
{
let text = String::from_utf8_lossy(text_bytes).to_string();
let hash_bytes = self
.read_from_pasteboard(state.text_hash_pasteboard_type)
.read_from_pasteboard(state.pasteboard, state.text_hash_pasteboard_type)
.and_then(|bytes| bytes.try_into().ok())
.map(u64::from_be_bytes);
let metadata_bytes = self
.read_from_pasteboard(state.metadata_pasteboard_type)
.read_from_pasteboard(state.pasteboard, state.metadata_pasteboard_type)
.and_then(|bytes| String::from_utf8(bytes.to_vec()).ok());
if let Some((hash, metadata)) = hash_bytes.zip(metadata_bytes) {

View file

@ -775,55 +775,55 @@ impl Bounds<ScaledPixels> {
}
}
#[cfg(test)]
mod tests {
use crate::{point, size};
// #[cfg(test)]
// mod tests {
// use crate::{point, size};
use super::*;
use smallvec::smallvec;
// use super::*;
// use smallvec::smallvec;
#[test]
fn test_scene() {
let mut scene = SceneBuilder::new();
assert_eq!(scene.layers_by_order.len(), 0);
// #[test]
// fn test_scene() {
// let mut scene = SceneBuilder::new();
// assert_eq!(scene.layers_by_order.len(), 0);
scene.insert(&smallvec![1].into(), quad());
scene.insert(&smallvec![2].into(), shadow());
scene.insert(&smallvec![3].into(), quad());
// scene.insert(&smallvec![1].into(), quad());
// scene.insert(&smallvec![2].into(), shadow());
// scene.insert(&smallvec![3].into(), quad());
let mut batches_count = 0;
for _ in scene.build().batches() {
batches_count += 1;
}
assert_eq!(batches_count, 3);
}
// let mut batches_count = 0;
// for _ in scene.build().batches() {
// batches_count += 1;
// }
// assert_eq!(batches_count, 3);
// }
fn quad() -> Quad {
Quad {
order: 0,
bounds: Bounds {
origin: point(ScaledPixels(0.), ScaledPixels(0.)),
size: size(ScaledPixels(100.), ScaledPixels(100.)),
},
content_mask: Default::default(),
background: Default::default(),
border_color: Default::default(),
corner_radii: Default::default(),
border_widths: Default::default(),
}
}
// fn quad() -> Quad {
// Quad {
// order: 0,
// bounds: Bounds {
// origin: point(ScaledPixels(0.), ScaledPixels(0.)),
// size: size(ScaledPixels(100.), ScaledPixels(100.)),
// },
// content_mask: Default::default(),
// background: Default::default(),
// border_color: Default::default(),
// corner_radii: Default::default(),
// border_widths: Default::default(),
// }
// }
fn shadow() -> Shadow {
Shadow {
order: Default::default(),
bounds: Bounds {
origin: point(ScaledPixels(0.), ScaledPixels(0.)),
size: size(ScaledPixels(100.), ScaledPixels(100.)),
},
corner_radii: Default::default(),
content_mask: Default::default(),
color: Default::default(),
blur_radius: Default::default(),
}
}
}
// fn shadow() -> Shadow {
// Shadow {
// order: Default::default(),
// bounds: Bounds {
// origin: point(ScaledPixels(0.), ScaledPixels(0.)),
// size: size(ScaledPixels(100.), ScaledPixels(100.)),
// },
// corner_radii: Default::default(),
// content_mask: Default::default(),
// color: Default::default(),
// blur_radius: Default::default(),
// }
// }
// }

View file

@ -1,7 +1,7 @@
use crate::{
black, phi, point, rems, AbsoluteLength, BorrowAppContext, BorrowWindow, Bounds, ContentMask,
Corners, CornersRefinement, DefiniteLength, Edges, EdgesRefinement, Font, FontFeatures,
FontStyle, FontWeight, Hsla, Length, Pixels, Point, PointRefinement, Rems, Result,
FontStyle, FontWeight, Hsla, Length, Pixels, Point, PointRefinement, Rems, Result, Rgba,
SharedString, Size, SizeRefinement, Styled, TextRun, ViewContext, WindowContext,
};
use refineable::{Cascade, Refineable};
@ -417,3 +417,12 @@ impl From<Hsla> for HighlightStyle {
}
}
}
impl From<Rgba> for HighlightStyle {
fn from(color: Rgba) -> Self {
Self {
color: Some(color.into()),
..Default::default()
}
}
}

View file

@ -21,8 +21,8 @@ struct SubscriberSetState<EmitterKey, Callback> {
impl<EmitterKey, Callback> SubscriberSet<EmitterKey, Callback>
where
EmitterKey: 'static + Send + Sync + Ord + Clone + Debug,
Callback: 'static + Send + Sync,
EmitterKey: 'static + Send + Ord + Clone + Debug,
Callback: 'static + Send,
{
pub fn new() -> Self {
Self(Arc::new(Mutex::new(SubscriberSetState {
@ -96,7 +96,7 @@ where
#[must_use]
pub struct Subscription {
unsubscribe: Option<Box<dyn FnOnce() + Send + Sync + 'static>>,
unsubscribe: Option<Box<dyn FnOnce() + Send + 'static>>,
}
impl Subscription {

View file

@ -179,7 +179,7 @@ struct Measureable<F>(F);
impl<F> taffy::tree::Measurable for Measureable<F>
where
F: Send + Sync + Fn(Size<Option<Pixels>>, Size<AvailableSpace>) -> Size<Pixels>,
F: Fn(Size<Option<Pixels>>, Size<AvailableSpace>) -> Size<Pixels> + Send + Sync,
{
fn measure(
&self,

View file

@ -1,47 +1,76 @@
use parking_lot::Mutex;
use crate::{
AnyBox, AnyElement, BorrowWindow, Bounds, Component, Element, ElementId, EntityId, Handle,
LayoutId, Pixels, ViewContext, WindowContext,
private::Sealed, AnyBox, AnyElement, AnyModel, AnyWeakModel, AppContext, AvailableSpace,
BorrowWindow, Bounds, Component, Element, ElementId, Entity, EntityId, LayoutId, Model, Pixels,
Size, ViewContext, VisualContext, WeakModel, WindowContext,
};
use std::{marker::PhantomData, sync::Arc};
use anyhow::{Context, Result};
use std::{any::TypeId, marker::PhantomData};
pub trait Render: 'static + Sized {
type Element: Element<Self> + 'static + Send;
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element;
}
pub struct View<V> {
state: Handle<V>,
render: Arc<dyn Fn(&mut V, &mut ViewContext<V>) -> AnyElement<V> + Send + Sync + 'static>,
pub(crate) model: Model<V>,
}
impl<V> Sealed for View<V> {}
impl<V: 'static> Entity<V> for View<V> {
type Weak = WeakView<V>;
fn entity_id(&self) -> EntityId {
self.model.entity_id
}
fn downgrade(&self) -> Self::Weak {
WeakView {
model: self.model.downgrade(),
}
}
fn upgrade_from(weak: &Self::Weak) -> Option<Self>
where
Self: Sized,
{
let model = weak.model.upgrade()?;
Some(View { model })
}
}
impl<V: 'static> View<V> {
pub fn into_any(self) -> AnyView {
AnyView {
view: Arc::new(Mutex::new(self)),
}
/// Convert this strong view reference into a weak view reference.
pub fn downgrade(&self) -> WeakView<V> {
Entity::downgrade(self)
}
pub fn update<C, R>(
&self,
cx: &mut C,
f: impl FnOnce(&mut V, &mut C::ViewContext<'_, '_, V>) -> R,
) -> C::Result<R>
where
C: VisualContext,
{
cx.update_view(self, f)
}
pub fn read<'a>(&self, cx: &'a AppContext) -> &'a V {
self.model.read(cx)
}
}
impl<V> Clone for View<V> {
fn clone(&self) -> Self {
Self {
state: self.state.clone(),
render: self.render.clone(),
model: self.model.clone(),
}
}
}
pub fn view<V, E>(
state: Handle<V>,
render: impl Fn(&mut V, &mut ViewContext<V>) -> E + Send + Sync + 'static,
) -> View<V>
where
E: Component<V>,
{
View {
state,
render: Arc::new(move |state, cx| render(state, cx).render()),
}
}
impl<V: 'static, ParentViewState: 'static> Component<ParentViewState> for View<V> {
impl<V: Render, ParentViewState: 'static> Component<ParentViewState> for View<V> {
fn render(self) -> AnyElement<ParentViewState> {
AnyElement::new(EraseViewState {
view: self,
@ -50,11 +79,14 @@ impl<V: 'static, ParentViewState: 'static> Component<ParentViewState> for View<V
}
}
impl<V: 'static> Element<()> for View<V> {
impl<V> Element<()> for View<V>
where
V: Render,
{
type ElementState = AnyElement<V>;
fn id(&self) -> Option<crate::ElementId> {
Some(ElementId::View(self.state.entity_id))
Some(ElementId::View(self.model.entity_id))
}
fn initialize(
@ -63,8 +95,8 @@ impl<V: 'static> Element<()> for View<V> {
_: Option<Self::ElementState>,
cx: &mut ViewContext<()>,
) -> Self::ElementState {
self.state.update(cx, |state, cx| {
let mut any_element = (self.render)(state, cx);
self.update(cx, |state, cx| {
let mut any_element = AnyElement::new(state.render(cx));
any_element.initialize(state, cx);
any_element
})
@ -76,7 +108,7 @@ impl<V: 'static> Element<()> for View<V> {
element: &mut Self::ElementState,
cx: &mut ViewContext<()>,
) -> LayoutId {
self.state.update(cx, |state, cx| element.layout(state, cx))
self.update(cx, |state, cx| element.layout(state, cx))
}
fn paint(
@ -86,7 +118,34 @@ impl<V: 'static> Element<()> for View<V> {
element: &mut Self::ElementState,
cx: &mut ViewContext<()>,
) {
self.state.update(cx, |state, cx| element.paint(state, cx))
self.update(cx, |state, cx| element.paint(state, cx))
}
}
pub struct WeakView<V> {
pub(crate) model: WeakModel<V>,
}
impl<V: 'static> WeakView<V> {
pub fn upgrade(&self) -> Option<View<V>> {
Entity::upgrade_from(self)
}
pub fn update<R>(
&self,
cx: &mut WindowContext,
f: impl FnOnce(&mut V, &mut ViewContext<V>) -> R,
) -> Result<R> {
let view = self.upgrade().context("error upgrading view")?;
Ok(view.update(cx, f))
}
}
impl<V> Clone for WeakView<V> {
fn clone(&self) -> Self {
Self {
model: self.model.clone(),
}
}
}
@ -96,15 +155,14 @@ struct EraseViewState<V, ParentV> {
}
unsafe impl<V, ParentV> Send for EraseViewState<V, ParentV> {}
unsafe impl<V, ParentV> Sync for EraseViewState<V, ParentV> {}
impl<V: 'static, ParentV: 'static> Component<ParentV> for EraseViewState<V, ParentV> {
impl<V: Render, ParentV: 'static> Component<ParentV> for EraseViewState<V, ParentV> {
fn render(self) -> AnyElement<ParentV> {
AnyElement::new(self)
}
}
impl<V: 'static, ParentV: 'static> Element<ParentV> for EraseViewState<V, ParentV> {
impl<V: Render, ParentV: 'static> Element<ParentV> for EraseViewState<V, ParentV> {
type ElementState = AnyBox;
fn id(&self) -> Option<crate::ElementId> {
@ -141,149 +199,212 @@ impl<V: 'static, ParentV: 'static> Element<ParentV> for EraseViewState<V, Parent
}
trait ViewObject: Send + Sync {
fn entity_type(&self) -> TypeId;
fn entity_id(&self) -> EntityId;
fn initialize(&mut self, cx: &mut WindowContext) -> AnyBox;
fn layout(&mut self, element: &mut AnyBox, cx: &mut WindowContext) -> LayoutId;
fn paint(&mut self, bounds: Bounds<Pixels>, element: &mut AnyBox, cx: &mut WindowContext);
fn model(&self) -> AnyModel;
fn initialize(&self, cx: &mut WindowContext) -> AnyBox;
fn layout(&self, element: &mut AnyBox, cx: &mut WindowContext) -> LayoutId;
fn paint(&self, bounds: Bounds<Pixels>, element: &mut AnyBox, cx: &mut WindowContext);
fn debug(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result;
}
impl<V: 'static> ViewObject for View<V> {
fn entity_id(&self) -> EntityId {
self.state.entity_id
impl<V> ViewObject for View<V>
where
V: Render,
{
fn entity_type(&self) -> TypeId {
TypeId::of::<V>()
}
fn initialize(&mut self, cx: &mut WindowContext) -> AnyBox {
cx.with_element_id(self.entity_id(), |_global_id, cx| {
self.state.update(cx, |state, cx| {
let mut any_element = Box::new((self.render)(state, cx));
fn entity_id(&self) -> EntityId {
Entity::entity_id(self)
}
fn model(&self) -> AnyModel {
self.model.clone().into_any()
}
fn initialize(&self, cx: &mut WindowContext) -> AnyBox {
cx.with_element_id(ViewObject::entity_id(self), |_global_id, cx| {
self.update(cx, |state, cx| {
let mut any_element = Box::new(AnyElement::new(state.render(cx)));
any_element.initialize(state, cx);
any_element as AnyBox
any_element
})
})
}
fn layout(&mut self, element: &mut AnyBox, cx: &mut WindowContext) -> LayoutId {
cx.with_element_id(self.entity_id(), |_global_id, cx| {
self.state.update(cx, |state, cx| {
fn layout(&self, element: &mut AnyBox, cx: &mut WindowContext) -> LayoutId {
cx.with_element_id(ViewObject::entity_id(self), |_global_id, cx| {
self.update(cx, |state, cx| {
let element = element.downcast_mut::<AnyElement<V>>().unwrap();
element.layout(state, cx)
})
})
}
fn paint(&mut self, _: Bounds<Pixels>, element: &mut AnyBox, cx: &mut WindowContext) {
cx.with_element_id(self.entity_id(), |_global_id, cx| {
self.state.update(cx, |state, cx| {
fn paint(&self, _: Bounds<Pixels>, element: &mut AnyBox, cx: &mut WindowContext) {
cx.with_element_id(ViewObject::entity_id(self), |_global_id, cx| {
self.update(cx, |state, cx| {
let element = element.downcast_mut::<AnyElement<V>>().unwrap();
element.paint(state, cx);
});
});
}
fn debug(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct(&format!("AnyView<{}>", std::any::type_name::<V>()))
.field("entity_id", &ViewObject::entity_id(self).as_u64())
.finish()
}
}
#[derive(Clone, Debug)]
pub struct AnyView {
view: Arc<Mutex<dyn ViewObject>>,
model: AnyModel,
initialize: fn(&AnyView, &mut WindowContext) -> AnyBox,
layout: fn(&AnyView, &mut AnyBox, &mut WindowContext) -> LayoutId,
paint: fn(&AnyView, &mut AnyBox, &mut WindowContext),
}
impl<ParentV: 'static> Component<ParentV> for AnyView {
fn render(self) -> AnyElement<ParentV> {
AnyElement::new(EraseAnyViewState {
view: self,
parent_view_state_type: PhantomData,
})
impl AnyView {
pub fn downgrade(&self) -> AnyWeakView {
AnyWeakView {
model: self.model.downgrade(),
initialize: self.initialize,
layout: self.layout,
paint: self.paint,
}
}
pub fn downcast<T: 'static>(self) -> Result<View<T>, Self> {
match self.model.downcast() {
Ok(model) => Ok(View { model }),
Err(model) => Err(Self {
model,
initialize: self.initialize,
layout: self.layout,
paint: self.paint,
}),
}
}
pub(crate) fn entity_type(&self) -> TypeId {
self.model.entity_type
}
pub(crate) fn draw(&self, available_space: Size<AvailableSpace>, cx: &mut WindowContext) {
let mut rendered_element = (self.initialize)(self, cx);
let layout_id = (self.layout)(self, &mut rendered_element, cx);
cx.window
.layout_engine
.compute_layout(layout_id, available_space);
(self.paint)(self, &mut rendered_element, cx);
}
}
impl Element<()> for AnyView {
type ElementState = AnyBox;
fn id(&self) -> Option<crate::ElementId> {
Some(ElementId::View(self.view.lock().entity_id()))
}
fn initialize(
&mut self,
_: &mut (),
_: Option<Self::ElementState>,
cx: &mut ViewContext<()>,
) -> Self::ElementState {
self.view.lock().initialize(cx)
}
fn layout(
&mut self,
_: &mut (),
element: &mut Self::ElementState,
cx: &mut ViewContext<()>,
) -> LayoutId {
self.view.lock().layout(element, cx)
}
fn paint(
&mut self,
bounds: Bounds<Pixels>,
_: &mut (),
element: &mut AnyBox,
cx: &mut ViewContext<()>,
) {
self.view.lock().paint(bounds, element, cx)
}
}
struct EraseAnyViewState<ParentViewState> {
view: AnyView,
parent_view_state_type: PhantomData<ParentViewState>,
}
unsafe impl<ParentV> Send for EraseAnyViewState<ParentV> {}
unsafe impl<ParentV> Sync for EraseAnyViewState<ParentV> {}
impl<ParentV: 'static> Component<ParentV> for EraseAnyViewState<ParentV> {
fn render(self) -> AnyElement<ParentV> {
impl<V: 'static> Component<V> for AnyView {
fn render(self) -> AnyElement<V> {
AnyElement::new(self)
}
}
impl<ParentV: 'static> Element<ParentV> for EraseAnyViewState<ParentV> {
impl<V: Render> From<View<V>> for AnyView {
fn from(value: View<V>) -> Self {
AnyView {
model: value.model.into_any(),
initialize: |view, cx| {
cx.with_element_id(view.model.entity_id, |_, cx| {
let view = view.clone().downcast::<V>().unwrap();
let element = view.update(cx, |view, cx| {
let mut element = AnyElement::new(view.render(cx));
element.initialize(view, cx);
element
});
Box::new(element)
})
},
layout: |view, element, cx| {
cx.with_element_id(view.model.entity_id, |_, cx| {
let view = view.clone().downcast::<V>().unwrap();
let element = element.downcast_mut::<AnyElement<V>>().unwrap();
view.update(cx, |view, cx| element.layout(view, cx))
})
},
paint: |view, element, cx| {
cx.with_element_id(view.model.entity_id, |_, cx| {
let view = view.clone().downcast::<V>().unwrap();
let element = element.downcast_mut::<AnyElement<V>>().unwrap();
view.update(cx, |view, cx| element.paint(view, cx))
})
},
}
}
}
impl<ParentViewState: 'static> Element<ParentViewState> for AnyView {
type ElementState = AnyBox;
fn id(&self) -> Option<crate::ElementId> {
Element::id(&self.view)
fn id(&self) -> Option<ElementId> {
Some(self.model.entity_id.into())
}
fn initialize(
&mut self,
_: &mut ParentV,
_: Option<Self::ElementState>,
cx: &mut ViewContext<ParentV>,
_view_state: &mut ParentViewState,
_element_state: Option<Self::ElementState>,
cx: &mut ViewContext<ParentViewState>,
) -> Self::ElementState {
self.view.view.lock().initialize(cx)
(self.initialize)(self, cx)
}
fn layout(
&mut self,
_: &mut ParentV,
element: &mut Self::ElementState,
cx: &mut ViewContext<ParentV>,
_view_state: &mut ParentViewState,
rendered_element: &mut Self::ElementState,
cx: &mut ViewContext<ParentViewState>,
) -> LayoutId {
self.view.view.lock().layout(element, cx)
(self.layout)(self, rendered_element, cx)
}
fn paint(
&mut self,
bounds: Bounds<Pixels>,
_: &mut ParentV,
element: &mut Self::ElementState,
cx: &mut ViewContext<ParentV>,
_bounds: Bounds<Pixels>,
_view_state: &mut ParentViewState,
rendered_element: &mut Self::ElementState,
cx: &mut ViewContext<ParentViewState>,
) {
self.view.view.lock().paint(bounds, element, cx)
(self.paint)(self, rendered_element, cx)
}
}
impl Clone for AnyView {
fn clone(&self) -> Self {
Self {
view: self.view.clone(),
}
pub struct AnyWeakView {
model: AnyWeakModel,
initialize: fn(&AnyView, &mut WindowContext) -> AnyBox,
layout: fn(&AnyView, &mut AnyBox, &mut WindowContext) -> LayoutId,
paint: fn(&AnyView, &mut AnyBox, &mut WindowContext),
}
impl AnyWeakView {
pub fn upgrade(&self) -> Option<AnyView> {
let model = self.model.upgrade()?;
Some(AnyView {
model,
initialize: self.initialize,
layout: self.layout,
paint: self.paint,
})
}
}
impl<T, E> Render for T
where
T: 'static + FnMut(&mut WindowContext) -> E,
E: 'static + Send + Element<T>,
{
type Element = E;
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
(self)(cx)
}
}

File diff suppressed because it is too large Load diff