Merge branch 'zed2' into zed2-workspace
This commit is contained in:
+33
-21
@@ -16,7 +16,7 @@ use crate::{
|
||||
current_platform, image_cache::ImageCache, Action, AnyBox, AnyView, AnyWindowHandle,
|
||||
AppMetadata, AssetSource, ClipboardItem, Context, DispatchPhase, DisplayId, Executor,
|
||||
FocusEvent, FocusHandle, FocusId, KeyBinding, Keymap, LayoutId, MainThread, MainThreadOnly,
|
||||
Pixels, Platform, Point, SharedString, SubscriberSet, Subscription, SvgRenderer, Task,
|
||||
Pixels, Platform, Point, Render, SharedString, SubscriberSet, Subscription, SvgRenderer, Task,
|
||||
TextStyle, TextStyleRefinement, TextSystem, View, ViewContext, Window, WindowContext,
|
||||
WindowHandle, WindowId,
|
||||
};
|
||||
@@ -309,10 +309,17 @@ impl AppContext {
|
||||
update: impl FnOnce(&mut V, &mut ViewContext<'_, '_, V>) -> R,
|
||||
) -> Result<R>
|
||||
where
|
||||
V: 'static,
|
||||
V: 'static + Send,
|
||||
{
|
||||
self.update_window(handle.any_handle, |cx| {
|
||||
let root_view = cx.window.root_view.as_ref().unwrap().downcast().unwrap();
|
||||
let root_view = cx
|
||||
.window
|
||||
.root_view
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.clone()
|
||||
.downcast()
|
||||
.unwrap();
|
||||
root_view.update(cx, update)
|
||||
})
|
||||
}
|
||||
@@ -685,7 +692,7 @@ impl AppContext {
|
||||
|
||||
pub fn observe_release<E: 'static>(
|
||||
&mut self,
|
||||
handle: &Handle<E>,
|
||||
handle: &Model<E>,
|
||||
mut on_release: impl FnMut(&mut E, &mut AppContext) + Send + 'static,
|
||||
) -> Subscription {
|
||||
self.release_listeners.insert(
|
||||
@@ -750,35 +757,35 @@ impl AppContext {
|
||||
}
|
||||
|
||||
impl Context for AppContext {
|
||||
type EntityContext<'a, T> = ModelContext<'a, T>;
|
||||
type ModelContext<'a, T> = ModelContext<'a, T>;
|
||||
type Result<T> = T;
|
||||
|
||||
/// 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 `Handle` will be returned
|
||||
/// 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 entity<T: 'static + Send>(
|
||||
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)
|
||||
})
|
||||
}
|
||||
|
||||
/// Update the entity referenced by the given handle. The function is passed a mutable reference to the
|
||||
/// 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_entity<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
|
||||
@@ -861,10 +868,17 @@ impl MainThread<AppContext> {
|
||||
update: impl FnOnce(&mut V, &mut MainThread<ViewContext<'_, '_, V>>) -> R,
|
||||
) -> Result<R>
|
||||
where
|
||||
V: 'static,
|
||||
V: 'static + Send,
|
||||
{
|
||||
self.update_window(handle.any_handle, |cx| {
|
||||
let root_view = cx.window.root_view.as_ref().unwrap().downcast().unwrap();
|
||||
let root_view = cx
|
||||
.window
|
||||
.root_view
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.clone()
|
||||
.downcast()
|
||||
.unwrap();
|
||||
root_view.update(cx, update)
|
||||
})
|
||||
}
|
||||
@@ -872,7 +886,7 @@ impl MainThread<AppContext> {
|
||||
/// 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: 'static>(
|
||||
pub fn open_window<V: Render>(
|
||||
&mut self,
|
||||
options: crate::WindowOptions,
|
||||
build_root_view: impl FnOnce(&mut WindowContext) -> View<V> + Send + 'static,
|
||||
@@ -955,10 +969,8 @@ 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)]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
AnyWindowHandle, AppContext, Component, Context, Executor, Handle, MainThread, ModelContext,
|
||||
Result, Task, View, ViewContext, VisualContext, WindowContext, WindowHandle,
|
||||
AnyWindowHandle, AppContext, Context, Executor, MainThread, Model, ModelContext, Result, Task,
|
||||
View, ViewContext, VisualContext, WindowContext, WindowHandle,
|
||||
};
|
||||
use anyhow::Context as _;
|
||||
use derive_more::{Deref, DerefMut};
|
||||
@@ -14,25 +14,25 @@ pub struct AsyncAppContext {
|
||||
}
|
||||
|
||||
impl Context for AsyncAppContext {
|
||||
type EntityContext<'a, 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: 'static + Send,
|
||||
{
|
||||
let app = self.app.upgrade().context("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>(
|
||||
&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().context("app was released")?;
|
||||
let mut lock = app.lock(); // Need this to compile
|
||||
@@ -84,7 +84,7 @@ impl AsyncAppContext {
|
||||
update: impl FnOnce(&mut V, &mut ViewContext<'_, '_, V>) -> R,
|
||||
) -> Result<R>
|
||||
where
|
||||
V: 'static,
|
||||
V: 'static + Send,
|
||||
{
|
||||
let app = self.app.upgrade().context("app was released")?;
|
||||
let mut app_context = app.lock();
|
||||
@@ -234,24 +234,24 @@ impl AsyncWindowContext {
|
||||
}
|
||||
|
||||
impl Context for AsyncWindowContext {
|
||||
type EntityContext<'a, T> = ModelContext<'a, 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: '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>(
|
||||
&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))
|
||||
@@ -261,17 +261,15 @@ impl Context for AsyncWindowContext {
|
||||
impl VisualContext for AsyncWindowContext {
|
||||
type ViewContext<'a, 'w, V> = ViewContext<'a, 'w, V>;
|
||||
|
||||
fn build_view<E, V>(
|
||||
fn build_view<V>(
|
||||
&mut self,
|
||||
build_entity: impl FnOnce(&mut Self::ViewContext<'_, '_, V>) -> V,
|
||||
render: impl Fn(&mut V, &mut ViewContext<'_, '_, V>) -> E + Send + 'static,
|
||||
build_view_state: impl FnOnce(&mut Self::ViewContext<'_, '_, V>) -> V,
|
||||
) -> Self::Result<View<V>>
|
||||
where
|
||||
E: Component<V>,
|
||||
V: 'static + Send,
|
||||
{
|
||||
self.app
|
||||
.update_window(self.window, |cx| cx.build_view(build_entity, render))
|
||||
.update_window(self.window, |cx| cx.build_view(build_view_state))
|
||||
}
|
||||
|
||||
fn update_view<V: 'static, R>(
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::{AnyBox, AppContext, Context, EntityHandle};
|
||||
use crate::{AnyBox, AppContext, Context};
|
||||
use anyhow::{anyhow, Result};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use parking_lot::{RwLock, RwLockUpgradableReadGuard};
|
||||
@@ -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: '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"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -115,7 +115,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>,
|
||||
}
|
||||
|
||||
@@ -143,15 +143,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,
|
||||
@@ -164,18 +164,18 @@ 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) -> Option<Model<T>> {
|
||||
if TypeId::of::<T>() == self.entity_type {
|
||||
Some(Handle {
|
||||
any_handle: self.clone(),
|
||||
Some(Model {
|
||||
any_model: self.clone(),
|
||||
entity_type: PhantomData,
|
||||
})
|
||||
} else {
|
||||
@@ -184,16 +184,16 @@ impl AnyHandle {
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
@@ -204,16 +204,16 @@ 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();
|
||||
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_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);
|
||||
@@ -223,60 +223,65 @@ 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> {
|
||||
pub struct Model<T> {
|
||||
#[deref]
|
||||
#[deref_mut]
|
||||
any_handle: AnyHandle,
|
||||
entity_type: PhantomData<T>,
|
||||
pub(crate) any_model: AnyModel,
|
||||
pub(crate) entity_type: PhantomData<T>,
|
||||
}
|
||||
|
||||
unsafe impl<T> Send for Handle<T> {}
|
||||
unsafe impl<T> Sync for Handle<T> {}
|
||||
unsafe impl<T> Send for Model<T> {}
|
||||
unsafe impl<T> Sync for Model<T> {}
|
||||
|
||||
impl<T: 'static> Handle<T> {
|
||||
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(),
|
||||
pub fn downgrade(&self) -> WeakModel<T> {
|
||||
WeakModel {
|
||||
any_model: self.any_model.downgrade(),
|
||||
entity_type: self.entity_type,
|
||||
}
|
||||
}
|
||||
|
||||
/// 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`.
|
||||
@@ -284,7 +289,7 @@ 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,
|
||||
@@ -293,73 +298,54 @@ impl<T: 'static> Handle<T> {
|
||||
}
|
||||
}
|
||||
|
||||
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: 'static> EntityHandle<T> for Handle<T> {
|
||||
type Weak = WeakHandle<T>;
|
||||
|
||||
fn entity_id(&self) -> EntityId {
|
||||
self.entity_id
|
||||
}
|
||||
|
||||
fn downgrade(&self) -> Self::Weak {
|
||||
self.downgrade()
|
||||
}
|
||||
|
||||
fn upgrade_from(weak: &Self::Weak) -> Option<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
weak.upgrade()
|
||||
impl<T> PartialEq<WeakModel<T>> for Model<T> {
|
||||
fn eq(&self, other: &WeakModel<T>) -> bool {
|
||||
self.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
|
||||
}
|
||||
@@ -373,14 +359,14 @@ impl AnyWeakHandle {
|
||||
ref_count > 0
|
||||
}
|
||||
|
||||
pub fn upgrade(&self) -> Option<AnyHandle> {
|
||||
pub fn upgrade(&self) -> Option<AnyModel> {
|
||||
let entity_map = self.entity_ref_counts.upgrade()?;
|
||||
entity_map
|
||||
.read()
|
||||
.counts
|
||||
.get(self.entity_id)?
|
||||
.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(),
|
||||
@@ -388,55 +374,55 @@ 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()?,
|
||||
impl<T: 'static> WeakModel<T> {
|
||||
pub fn upgrade(&self) -> Option<Model<T>> {
|
||||
Some(Model {
|
||||
any_model: self.any_model.upgrade()?,
|
||||
entity_type: self.entity_type,
|
||||
})
|
||||
}
|
||||
|
||||
/// 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.
|
||||
///
|
||||
@@ -446,7 +432,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,
|
||||
@@ -460,22 +446,22 @@ impl<T: 'static> WeakHandle<T> {
|
||||
}
|
||||
}
|
||||
|
||||
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.entity_id()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
AppContext, AsyncAppContext, Context, Effect, EntityId, EventEmitter, Handle, MainThread,
|
||||
Reference, Subscription, Task, WeakHandle,
|
||||
AppContext, AsyncAppContext, Context, Effect, 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,20 +30,20 @@ impl<'a, T: 'static> ModelContext<'a, T> {
|
||||
self.model_state.entity_id
|
||||
}
|
||||
|
||||
pub fn handle(&self) -> Handle<T> {
|
||||
pub fn handle(&self) -> Model<T> {
|
||||
self.weak_handle()
|
||||
.upgrade()
|
||||
.expect("The entity must be alive if we have a model context")
|
||||
}
|
||||
|
||||
pub fn weak_handle(&self) -> WeakHandle<T> {
|
||||
pub fn weak_handle(&self) -> WeakModel<T> {
|
||||
self.model_state.clone()
|
||||
}
|
||||
|
||||
pub fn observe<T2: 'static>(
|
||||
&mut self,
|
||||
handle: &Handle<T2>,
|
||||
mut on_notify: impl FnMut(&mut T, Handle<T2>, &mut ModelContext<'_, T>) + Send + 'static,
|
||||
handle: &Model<T2>,
|
||||
mut on_notify: impl FnMut(&mut T, Model<T2>, &mut ModelContext<'_, T>) + Send + 'static,
|
||||
) -> Subscription
|
||||
where
|
||||
T: 'static + Send,
|
||||
@@ -65,10 +65,8 @@ impl<'a, T: 'static> ModelContext<'a, T> {
|
||||
|
||||
pub fn subscribe<E: 'static + EventEmitter>(
|
||||
&mut self,
|
||||
handle: &Handle<E>,
|
||||
mut on_event: impl FnMut(&mut T, Handle<E>, &E::Event, &mut ModelContext<'_, T>)
|
||||
+ Send
|
||||
+ 'static,
|
||||
handle: &Model<E>,
|
||||
mut on_event: impl FnMut(&mut T, Model<E>, &E::Event, &mut ModelContext<'_, T>) + Send + 'static,
|
||||
) -> Subscription
|
||||
where
|
||||
T: 'static + Send,
|
||||
@@ -107,7 +105,7 @@ impl<'a, T: 'static> ModelContext<'a, T> {
|
||||
|
||||
pub fn observe_release<E: 'static>(
|
||||
&mut self,
|
||||
handle: &Handle<E>,
|
||||
handle: &Model<E>,
|
||||
mut on_release: impl FnMut(&mut T, &mut E, &mut ModelContext<'_, T>) + Send + 'static,
|
||||
) -> Subscription
|
||||
where
|
||||
@@ -182,7 +180,7 @@ 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,
|
||||
@@ -195,7 +193,7 @@ impl<'a, T: 'static> ModelContext<'a, T> {
|
||||
|
||||
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,
|
||||
@@ -220,23 +218,23 @@ where
|
||||
}
|
||||
|
||||
impl<'a, T> Context for ModelContext<'a, T> {
|
||||
type EntityContext<'b, 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,
|
||||
{
|
||||
self.app.entity(build_entity)
|
||||
self.app.build_model(build_model)
|
||||
}
|
||||
|
||||
fn update_entity<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)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::{
|
||||
AnyWindowHandle, AppContext, AsyncAppContext, Context, Executor, Handle, MainThread,
|
||||
AnyWindowHandle, AppContext, AsyncAppContext, Context, Executor, MainThread, Model,
|
||||
ModelContext, Result, Task, TestDispatcher, TestPlatform, WindowContext,
|
||||
};
|
||||
use parking_lot::Mutex;
|
||||
@@ -12,24 +12,24 @@ pub struct TestAppContext {
|
||||
}
|
||||
|
||||
impl Context for TestAppContext {
|
||||
type EntityContext<'a, 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: 'static + Send,
|
||||
{
|
||||
let mut lock = self.app.lock();
|
||||
lock.entity(build_entity)
|
||||
lock.build_model(build_model)
|
||||
}
|
||||
|
||||
fn update_entity<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)
|
||||
|
||||
@@ -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>;
|
||||
|
||||
|
||||
+31
-38
@@ -70,33 +70,31 @@ use taffy::TaffyLayoutEngine;
|
||||
type AnyBox = Box<dyn Any + Send>;
|
||||
|
||||
pub trait Context {
|
||||
type EntityContext<'a, 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;
|
||||
|
||||
fn update_entity<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<E, V>(
|
||||
fn build_view<V>(
|
||||
&mut self,
|
||||
build_entity: impl FnOnce(&mut Self::ViewContext<'_, '_, V>) -> V,
|
||||
render: impl Fn(&mut V, &mut ViewContext<'_, '_, V>) -> E + Send + 'static,
|
||||
build_view_state: impl FnOnce(&mut Self::ViewContext<'_, '_, V>) -> V,
|
||||
) -> Self::Result<View<V>>
|
||||
where
|
||||
E: Component<V>,
|
||||
V: 'static + Send;
|
||||
|
||||
fn update_view<V: 'static, R>(
|
||||
@@ -140,37 +138,37 @@ impl<T> DerefMut for MainThread<T> {
|
||||
}
|
||||
|
||||
impl<C: Context> Context for MainThread<C> {
|
||||
type EntityContext<'a, T> = MainThread<C::EntityContext<'a, 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: '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>(
|
||||
&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| {
|
||||
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)
|
||||
@@ -181,27 +179,22 @@ 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<E, V>(
|
||||
fn build_view<V>(
|
||||
&mut self,
|
||||
build_entity: impl FnOnce(&mut Self::ViewContext<'_, '_, V>) -> V,
|
||||
render: impl Fn(&mut V, &mut ViewContext<'_, '_, V>) -> E + Send + 'static,
|
||||
build_view_state: impl FnOnce(&mut Self::ViewContext<'_, '_, V>) -> V,
|
||||
) -> Self::Result<View<V>>
|
||||
where
|
||||
E: Component<V>,
|
||||
V: 'static + Send,
|
||||
{
|
||||
self.0.build_view(
|
||||
|cx| {
|
||||
let cx = unsafe {
|
||||
mem::transmute::<
|
||||
&mut C::ViewContext<'_, '_, V>,
|
||||
&mut MainThread<C::ViewContext<'_, '_, V>>,
|
||||
>(cx)
|
||||
};
|
||||
build_entity(cx)
|
||||
},
|
||||
render,
|
||||
)
|
||||
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>(
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::{
|
||||
point, px, Action, AnyBox, AnyDrag, AppContext, BorrowWindow, Bounds, Component,
|
||||
DispatchContext, DispatchPhase, Element, ElementId, FocusHandle, KeyMatch, Keystroke,
|
||||
Modifiers, Overflow, Pixels, Point, SharedString, Size, Style, StyleRefinement, View,
|
||||
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;
|
||||
@@ -258,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 + '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>(),
|
||||
Box::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
|
||||
@@ -314,36 +314,22 @@ pub trait StatefulInteractive<V: 'static>: StatelessInteractive<V> {
|
||||
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 + 'static,
|
||||
listener: impl Fn(&mut V, &mut ViewContext<V>) -> View<W> + Send + 'static,
|
||||
) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
S: Any + Send,
|
||||
R: Fn(&mut V, &mut ViewContext<V>) -> E,
|
||||
R: 'static + Send,
|
||||
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(Box::new(move |view_state, cursor_offset, cx| {
|
||||
let drag = listener(view_state, cx);
|
||||
let drag_handle_view = Some(
|
||||
View::for_handle(cx.handle().upgrade().unwrap(), 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_any(),
|
||||
cursor_offset,
|
||||
}));
|
||||
self
|
||||
}
|
||||
@@ -412,7 +398,7 @@ pub trait ElementInteraction<V: 'static>: 'static + Send {
|
||||
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);
|
||||
@@ -421,7 +407,8 @@ pub trait ElementInteraction<V: 'static>: 'static + Send {
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -509,7 +496,7 @@ pub trait ElementInteraction<V: 'static>: 'static + Send {
|
||||
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 {
|
||||
@@ -517,7 +504,7 @@ pub trait ElementInteraction<V: 'static>: 'static + Send {
|
||||
.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();
|
||||
}
|
||||
@@ -685,7 +672,7 @@ impl<V> From<ElementId> for StatefulInteraction<V> {
|
||||
}
|
||||
}
|
||||
|
||||
type DropListener<V> = dyn Fn(&mut V, AnyBox, &mut ViewContext<V>) + 'static + Send;
|
||||
type DropListener<V> = dyn Fn(&mut V, AnyView, &mut ViewContext<V>) + 'static + Send;
|
||||
|
||||
pub struct StatelessInteraction<V> {
|
||||
pub dispatch_context: DispatchContext,
|
||||
@@ -866,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,
|
||||
@@ -877,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 {
|
||||
@@ -888,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,
|
||||
@@ -995,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 {
|
||||
|
||||
+71
-53
@@ -1,45 +1,35 @@
|
||||
use crate::{
|
||||
AnyBox, AnyElement, AppContext, AvailableSpace, BorrowWindow, Bounds, Component, Element,
|
||||
ElementId, EntityHandle, EntityId, Flatten, Handle, LayoutId, Pixels, Size, ViewContext,
|
||||
VisualContext, WeakHandle, WindowContext,
|
||||
AnyBox, AnyElement, AnyModel, AppContext, AvailableSpace, BorrowWindow, Bounds, Component,
|
||||
Element, ElementId, EntityHandle, EntityId, Flatten, LayoutId, Model, Pixels, Size,
|
||||
ViewContext, VisualContext, WeakModel, WindowContext,
|
||||
};
|
||||
use anyhow::{Context, Result};
|
||||
use parking_lot::Mutex;
|
||||
use std::{
|
||||
any::Any,
|
||||
any::{Any, TypeId},
|
||||
marker::PhantomData,
|
||||
sync::{Arc, Weak},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
pub struct View<V> {
|
||||
pub(crate) state: Handle<V>,
|
||||
render: Arc<Mutex<dyn Fn(&mut V, &mut ViewContext<V>) -> AnyElement<V> + Send + 'static>>,
|
||||
pub trait Render: 'static + Sized {
|
||||
type Element: Element<Self> + 'static + Send;
|
||||
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element;
|
||||
}
|
||||
|
||||
impl<V: 'static> View<V> {
|
||||
pub fn for_handle<E>(
|
||||
state: Handle<V>,
|
||||
render: impl Fn(&mut V, &mut ViewContext<'_, '_, V>) -> E + Send + 'static,
|
||||
) -> View<V>
|
||||
where
|
||||
E: Component<V>,
|
||||
{
|
||||
View {
|
||||
state,
|
||||
render: Arc::new(Mutex::new(
|
||||
move |state: &mut V, cx: &mut ViewContext<'_, '_, V>| render(state, cx).render(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
pub struct View<V> {
|
||||
pub(crate) model: Model<V>,
|
||||
}
|
||||
|
||||
impl<V: Render> View<V> {
|
||||
pub fn into_any(self) -> AnyView {
|
||||
AnyView(Arc::new(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: 'static> View<V> {
|
||||
pub fn downgrade(&self) -> WeakView<V> {
|
||||
WeakView {
|
||||
state: self.state.downgrade(),
|
||||
render: Arc::downgrade(&self.render),
|
||||
model: self.model.downgrade(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,20 +45,19 @@ impl<V: 'static> View<V> {
|
||||
}
|
||||
|
||||
pub fn read<'a>(&self, cx: &'a AppContext) -> &'a V {
|
||||
cx.entities.read(&self.state)
|
||||
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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
@@ -77,11 +66,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<ElementId> {
|
||||
Some(ElementId::View(self.state.entity_id))
|
||||
fn id(&self) -> Option<crate::ElementId> {
|
||||
Some(ElementId::View(self.model.entity_id))
|
||||
}
|
||||
|
||||
fn initialize(
|
||||
@@ -91,7 +83,7 @@ impl<V: 'static> Element<()> for View<V> {
|
||||
cx: &mut ViewContext<()>,
|
||||
) -> Self::ElementState {
|
||||
self.update(cx, |state, cx| {
|
||||
let mut any_element = (self.render.lock())(state, cx);
|
||||
let mut any_element = AnyElement::new(state.render(cx));
|
||||
any_element.initialize(state, cx);
|
||||
any_element
|
||||
})
|
||||
@@ -121,7 +113,7 @@ impl<T: 'static> EntityHandle<T> for View<T> {
|
||||
type Weak = WeakView<T>;
|
||||
|
||||
fn entity_id(&self) -> EntityId {
|
||||
self.state.entity_id
|
||||
self.model.entity_id
|
||||
}
|
||||
|
||||
fn downgrade(&self) -> Self::Weak {
|
||||
@@ -137,15 +129,13 @@ impl<T: 'static> EntityHandle<T> for View<T> {
|
||||
}
|
||||
|
||||
pub struct WeakView<V> {
|
||||
pub(crate) state: WeakHandle<V>,
|
||||
render: Weak<Mutex<dyn Fn(&mut V, &mut ViewContext<V>) -> AnyElement<V> + Send + 'static>>,
|
||||
pub(crate) model: WeakModel<V>,
|
||||
}
|
||||
|
||||
impl<V: 'static> WeakView<V> {
|
||||
pub fn upgrade(&self) -> Option<View<V>> {
|
||||
let state = self.state.upgrade()?;
|
||||
let render = self.render.upgrade()?;
|
||||
Some(View { state, render })
|
||||
let model = self.model.upgrade()?;
|
||||
Some(View { model })
|
||||
}
|
||||
|
||||
pub fn update<C, R>(
|
||||
@@ -165,8 +155,7 @@ impl<V: 'static> WeakView<V> {
|
||||
impl<V> Clone for WeakView<V> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
state: self.state.clone(),
|
||||
render: self.render.clone(),
|
||||
model: self.model.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -178,13 +167,13 @@ struct EraseViewState<V, ParentV> {
|
||||
|
||||
unsafe impl<V, ParentV> Send 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<ElementId> {
|
||||
@@ -221,30 +210,43 @@ 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 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 as_any(&self) -> &dyn Any;
|
||||
}
|
||||
|
||||
impl<V: 'static> ViewObject for View<V> {
|
||||
impl<V> ViewObject for View<V>
|
||||
where
|
||||
V: Render,
|
||||
{
|
||||
fn entity_type(&self) -> TypeId {
|
||||
TypeId::of::<V>()
|
||||
}
|
||||
|
||||
fn entity_id(&self) -> EntityId {
|
||||
self.state.entity_id
|
||||
self.model.entity_id
|
||||
}
|
||||
|
||||
fn model(&self) -> AnyModel {
|
||||
self.model.clone().into_any()
|
||||
}
|
||||
|
||||
fn initialize(&self, cx: &mut WindowContext) -> AnyBox {
|
||||
cx.with_element_id(self.state.entity_id, |_global_id, cx| {
|
||||
cx.with_element_id(self.model.entity_id, |_global_id, cx| {
|
||||
self.update(cx, |state, cx| {
|
||||
let mut any_element = Box::new((self.render.lock())(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(&self, element: &mut AnyBox, cx: &mut WindowContext) -> LayoutId {
|
||||
cx.with_element_id(self.state.entity_id, |_global_id, cx| {
|
||||
cx.with_element_id(self.model.entity_id, |_global_id, cx| {
|
||||
self.update(cx, |state, cx| {
|
||||
let element = element.downcast_mut::<AnyElement<V>>().unwrap();
|
||||
element.layout(state, cx)
|
||||
@@ -253,7 +255,7 @@ impl<V: 'static> ViewObject for View<V> {
|
||||
}
|
||||
|
||||
fn paint(&self, _: Bounds<Pixels>, element: &mut AnyBox, cx: &mut WindowContext) {
|
||||
cx.with_element_id(self.state.entity_id, |_global_id, cx| {
|
||||
cx.with_element_id(self.model.entity_id, |_global_id, cx| {
|
||||
self.update(cx, |state, cx| {
|
||||
let element = element.downcast_mut::<AnyElement<V>>().unwrap();
|
||||
element.paint(state, cx);
|
||||
@@ -270,8 +272,12 @@ impl<V: 'static> ViewObject for View<V> {
|
||||
pub struct AnyView(Arc<dyn ViewObject>);
|
||||
|
||||
impl AnyView {
|
||||
pub fn downcast<V: 'static>(&self) -> Option<View<V>> {
|
||||
self.0.as_any().downcast_ref().cloned()
|
||||
pub fn downcast<V: 'static + Send>(self) -> Option<View<V>> {
|
||||
self.0.model().downcast().map(|model| View { model })
|
||||
}
|
||||
|
||||
pub(crate) fn entity_type(&self) -> TypeId {
|
||||
self.0.entity_type()
|
||||
}
|
||||
|
||||
pub(crate) fn draw(&self, available_space: Size<AvailableSpace>, cx: &mut WindowContext) {
|
||||
@@ -343,6 +349,18 @@ impl<ParentV: 'static> Component<ParentV> for EraseAnyViewState<ParentV> {
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
impl<ParentV: 'static> Element<ParentV> for EraseAnyViewState<ParentV> {
|
||||
type ElementState = AnyBox;
|
||||
|
||||
|
||||
+51
-62
@@ -1,14 +1,14 @@
|
||||
use crate::{
|
||||
px, size, Action, AnyBox, AnyDrag, AnyView, AppContext, AsyncWindowContext, AvailableSpace,
|
||||
Bounds, BoxShadow, Context, Corners, DevicePixels, DispatchContext, DisplayId, Edges, Effect,
|
||||
EntityHandle, EntityId, EventEmitter, ExternalPaths, FileDropEvent, FocusEvent, FontId,
|
||||
GlobalElementId, GlyphId, Handle, Hsla, ImageData, InputEvent, IsZero, KeyListener, KeyMatch,
|
||||
KeyMatcher, Keystroke, LayoutId, MainThread, MainThreadOnly, ModelContext, Modifiers,
|
||||
MonochromeSprite, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Path, Pixels,
|
||||
PlatformAtlas, PlatformWindow, Point, PolychromeSprite, Quad, Reference, RenderGlyphParams,
|
||||
RenderImageParams, RenderSvgParams, ScaledPixels, SceneBuilder, Shadow, SharedString, Size,
|
||||
Style, Subscription, TaffyLayoutEngine, Task, Underline, UnderlineStyle, View, VisualContext,
|
||||
WeakHandle, WeakView, WindowOptions, SUBPIXEL_VARIANTS,
|
||||
EntityHandle, EntityId, EventEmitter, FileDropEvent, FocusEvent, FontId, GlobalElementId,
|
||||
GlyphId, Hsla, ImageData, InputEvent, IsZero, KeyListener, KeyMatch, KeyMatcher, Keystroke,
|
||||
LayoutId, MainThread, MainThreadOnly, Model, ModelContext, Modifiers, MonochromeSprite,
|
||||
MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Path, Pixels, PlatformAtlas,
|
||||
PlatformWindow, Point, PolychromeSprite, Quad, Reference, RenderGlyphParams, RenderImageParams,
|
||||
RenderSvgParams, ScaledPixels, SceneBuilder, Shadow, SharedString, Size, Style, Subscription,
|
||||
TaffyLayoutEngine, Task, Underline, UnderlineStyle, View, VisualContext, WeakModel, WeakView,
|
||||
WindowOptions, SUBPIXEL_VARIANTS,
|
||||
};
|
||||
use anyhow::Result;
|
||||
use collections::HashMap;
|
||||
@@ -918,15 +918,13 @@ impl<'a, 'w> WindowContext<'a, 'w> {
|
||||
root_view.draw(available_space, cx);
|
||||
});
|
||||
|
||||
if let Some(mut active_drag) = self.app.active_drag.take() {
|
||||
if let Some(active_drag) = self.app.active_drag.take() {
|
||||
self.stack(1, |cx| {
|
||||
let offset = cx.mouse_position() - active_drag.cursor_offset;
|
||||
cx.with_element_offset(Some(offset), |cx| {
|
||||
let available_space =
|
||||
size(AvailableSpace::MinContent, AvailableSpace::MinContent);
|
||||
if let Some(drag_handle_view) = &mut active_drag.drag_handle_view {
|
||||
drag_handle_view.draw(available_space, cx);
|
||||
}
|
||||
active_drag.view.draw(available_space, cx);
|
||||
cx.active_drag = Some(active_drag);
|
||||
});
|
||||
});
|
||||
@@ -994,12 +992,12 @@ impl<'a, 'w> WindowContext<'a, 'w> {
|
||||
InputEvent::FileDrop(file_drop) => match file_drop {
|
||||
FileDropEvent::Entered { position, files } => {
|
||||
self.window.mouse_position = position;
|
||||
self.active_drag.get_or_insert_with(|| AnyDrag {
|
||||
drag_handle_view: None,
|
||||
cursor_offset: position,
|
||||
state: Box::new(files),
|
||||
state_type: TypeId::of::<ExternalPaths>(),
|
||||
});
|
||||
if self.active_drag.is_none() {
|
||||
self.active_drag = Some(AnyDrag {
|
||||
view: self.build_view(|_| files).into_any(),
|
||||
cursor_offset: position,
|
||||
});
|
||||
}
|
||||
InputEvent::MouseDown(MouseDownEvent {
|
||||
position,
|
||||
button: MouseButton::Left,
|
||||
@@ -1267,30 +1265,30 @@ impl<'a, 'w> WindowContext<'a, 'w> {
|
||||
}
|
||||
|
||||
impl Context for WindowContext<'_, '_> {
|
||||
type EntityContext<'a, T> = ModelContext<'a, T>;
|
||||
type ModelContext<'a, T> = ModelContext<'a, T>;
|
||||
type Result<T> = T;
|
||||
|
||||
fn entity<T>(
|
||||
fn build_model<T>(
|
||||
&mut self,
|
||||
build_entity: impl FnOnce(&mut Self::EntityContext<'_, T>) -> T,
|
||||
) -> Handle<T>
|
||||
build_model: impl FnOnce(&mut Self::ModelContext<'_, T>) -> T,
|
||||
) -> Model<T>
|
||||
where
|
||||
T: 'static + Send,
|
||||
{
|
||||
let slot = self.app.entities.reserve();
|
||||
let entity = build_entity(&mut ModelContext::mutable(&mut *self.app, slot.downgrade()));
|
||||
self.entities.insert(slot, entity)
|
||||
let model = build_model(&mut ModelContext::mutable(&mut *self.app, slot.downgrade()));
|
||||
self.entities.insert(slot, model)
|
||||
}
|
||||
|
||||
fn update_entity<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 {
|
||||
let mut entity = self.entities.lease(handle);
|
||||
let mut entity = self.entities.lease(model);
|
||||
let result = update(
|
||||
&mut *entity,
|
||||
&mut ModelContext::mutable(&mut *self.app, handle.downgrade()),
|
||||
&mut ModelContext::mutable(&mut *self.app, model.downgrade()),
|
||||
);
|
||||
self.entities.end_lease(entity);
|
||||
result
|
||||
@@ -1300,21 +1298,17 @@ impl Context for WindowContext<'_, '_> {
|
||||
impl VisualContext for WindowContext<'_, '_> {
|
||||
type ViewContext<'a, 'w, V> = ViewContext<'a, 'w, V>;
|
||||
|
||||
/// Builds a new view in the current window. The first argument is a function that builds
|
||||
/// an entity representing the view's state. It is invoked with a `ViewContext` that provides
|
||||
/// entity-specific access to the window and application state during construction. The second
|
||||
/// argument is a render function that returns a component based on the view's state.
|
||||
fn build_view<E, V>(
|
||||
fn build_view<V>(
|
||||
&mut self,
|
||||
build_view_state: impl FnOnce(&mut Self::ViewContext<'_, '_, V>) -> V,
|
||||
render: impl Fn(&mut V, &mut ViewContext<'_, '_, V>) -> E + Send + 'static,
|
||||
) -> Self::Result<View<V>>
|
||||
where
|
||||
E: crate::Component<V>,
|
||||
V: 'static + Send,
|
||||
{
|
||||
let slot = self.app.entities.reserve();
|
||||
let view = View::for_handle(slot.clone(), render);
|
||||
let view = View {
|
||||
model: slot.clone(),
|
||||
};
|
||||
let mut cx = ViewContext::mutable(&mut *self.app, &mut *self.window, view.downgrade());
|
||||
let entity = build_view_state(&mut cx);
|
||||
self.entities.insert(slot, entity);
|
||||
@@ -1327,7 +1321,7 @@ impl VisualContext for WindowContext<'_, '_> {
|
||||
view: &View<T>,
|
||||
update: impl FnOnce(&mut T, &mut Self::ViewContext<'_, '_, T>) -> R,
|
||||
) -> Self::Result<R> {
|
||||
let mut lease = self.app.entities.lease(&view.state);
|
||||
let mut lease = self.app.entities.lease(&view.model);
|
||||
let mut cx = ViewContext::mutable(&mut *self.app, &mut *self.window, view.downgrade());
|
||||
let result = update(&mut *lease, &mut cx);
|
||||
cx.app.entities.end_lease(lease);
|
||||
@@ -1582,8 +1576,8 @@ impl<'a, 'w, V: 'static> ViewContext<'a, 'w, V> {
|
||||
self.view.clone()
|
||||
}
|
||||
|
||||
pub fn handle(&self) -> WeakHandle<V> {
|
||||
self.view.state.clone()
|
||||
pub fn model(&self) -> WeakModel<V> {
|
||||
self.view.model.clone()
|
||||
}
|
||||
|
||||
pub fn stack<R>(&mut self, order: u32, f: impl FnOnce(&mut Self) -> R) -> R {
|
||||
@@ -1603,8 +1597,8 @@ impl<'a, 'w, V: 'static> ViewContext<'a, 'w, V> {
|
||||
|
||||
pub fn observe<E>(
|
||||
&mut self,
|
||||
handle: &Handle<E>,
|
||||
mut on_notify: impl FnMut(&mut V, Handle<E>, &mut ViewContext<'_, '_, V>) + Send + 'static,
|
||||
handle: &Model<E>,
|
||||
mut on_notify: impl FnMut(&mut V, Model<E>, &mut ViewContext<'_, '_, V>) + Send + 'static,
|
||||
) -> Subscription
|
||||
where
|
||||
E: 'static,
|
||||
@@ -1665,7 +1659,7 @@ impl<'a, 'w, V: 'static> ViewContext<'a, 'w, V> {
|
||||
) -> Subscription {
|
||||
let window_handle = self.window.handle;
|
||||
self.app.release_listeners.insert(
|
||||
self.view.state.entity_id,
|
||||
self.view.model.entity_id,
|
||||
Box::new(move |this, cx| {
|
||||
let this = this.downcast_mut().expect("invalid entity type");
|
||||
// todo!("are we okay with silently swallowing the error?")
|
||||
@@ -1676,7 +1670,7 @@ impl<'a, 'w, V: 'static> ViewContext<'a, 'w, V> {
|
||||
|
||||
pub fn observe_release<T: 'static>(
|
||||
&mut self,
|
||||
handle: &Handle<T>,
|
||||
handle: &Model<T>,
|
||||
mut on_release: impl FnMut(&mut V, &mut T, &mut ViewContext<'_, '_, V>) + Send + 'static,
|
||||
) -> Subscription
|
||||
where
|
||||
@@ -1698,7 +1692,7 @@ impl<'a, 'w, V: 'static> ViewContext<'a, 'w, V> {
|
||||
pub fn notify(&mut self) {
|
||||
self.window_cx.notify();
|
||||
self.window_cx.app.push_effect(Effect::Notify {
|
||||
emitter: self.view.state.entity_id,
|
||||
emitter: self.view.model.entity_id,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1878,7 +1872,7 @@ where
|
||||
V::Event: Any + Send,
|
||||
{
|
||||
pub fn emit(&mut self, event: V::Event) {
|
||||
let emitter = self.view.state.entity_id;
|
||||
let emitter = self.view.model.entity_id;
|
||||
self.app.push_effect(Effect::Emit {
|
||||
emitter,
|
||||
event: Box::new(event),
|
||||
@@ -1897,41 +1891,36 @@ impl<'a, 'w, V: 'static> MainThread<ViewContext<'a, 'w, V>> {
|
||||
}
|
||||
|
||||
impl<'a, 'w, V> Context for ViewContext<'a, 'w, V> {
|
||||
type EntityContext<'b, U> = ModelContext<'b, U>;
|
||||
type ModelContext<'b, U> = ModelContext<'b, U>;
|
||||
type Result<U> = U;
|
||||
|
||||
fn entity<T>(
|
||||
fn build_model<T>(
|
||||
&mut self,
|
||||
build_entity: impl FnOnce(&mut Self::EntityContext<'_, T>) -> T,
|
||||
) -> Handle<T>
|
||||
build_model: impl FnOnce(&mut Self::ModelContext<'_, T>) -> T,
|
||||
) -> Model<T>
|
||||
where
|
||||
T: 'static + Send,
|
||||
{
|
||||
self.window_cx.entity(build_entity)
|
||||
self.window_cx.build_model(build_model)
|
||||
}
|
||||
|
||||
fn update_entity<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.window_cx.update_entity(handle, update)
|
||||
self.window_cx.update_entity(model, update)
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: 'static> VisualContext for ViewContext<'_, '_, V> {
|
||||
type ViewContext<'a, 'w, V2> = ViewContext<'a, 'w, V2>;
|
||||
|
||||
fn build_view<E, V2>(
|
||||
fn build_view<W: 'static + Send>(
|
||||
&mut self,
|
||||
build_entity: impl FnOnce(&mut Self::ViewContext<'_, '_, V2>) -> V2,
|
||||
render: impl Fn(&mut V2, &mut ViewContext<'_, '_, V2>) -> E + Send + 'static,
|
||||
) -> Self::Result<View<V2>>
|
||||
where
|
||||
E: crate::Component<V2>,
|
||||
V2: 'static + Send,
|
||||
{
|
||||
self.window_cx.build_view(build_entity, render)
|
||||
build_view: impl FnOnce(&mut Self::ViewContext<'_, '_, W>) -> W,
|
||||
) -> Self::Result<View<W>> {
|
||||
self.window_cx.build_view(build_view)
|
||||
}
|
||||
|
||||
fn update_view<V2: 'static, R>(
|
||||
|
||||
Reference in New Issue
Block a user