WIP
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
use crate::{AnyBox, AppContext, Context};
|
use crate::{AnyBox, AppContext, Context, EntityHandle};
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use derive_more::{Deref, DerefMut};
|
use derive_more::{Deref, DerefMut};
|
||||||
use parking_lot::{RwLock, RwLockUpgradableReadGuard};
|
use parking_lot::{RwLock, RwLockUpgradableReadGuard};
|
||||||
@@ -329,7 +329,26 @@ impl<T> Eq for Handle<T> {}
|
|||||||
|
|
||||||
impl<T> PartialEq<WeakHandle<T>> for Handle<T> {
|
impl<T> PartialEq<WeakHandle<T>> for Handle<T> {
|
||||||
fn eq(&self, other: &WeakHandle<T>) -> bool {
|
fn eq(&self, other: &WeakHandle<T>) -> bool {
|
||||||
self.entity_id() == other.entity_id()
|
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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -457,6 +476,6 @@ impl<T> Eq for WeakHandle<T> {}
|
|||||||
|
|
||||||
impl<T> PartialEq<Handle<T>> for WeakHandle<T> {
|
impl<T> PartialEq<Handle<T>> for WeakHandle<T> {
|
||||||
fn eq(&self, other: &Handle<T>) -> bool {
|
fn eq(&self, other: &Handle<T>) -> bool {
|
||||||
self.entity_id() == other.entity_id()
|
self.entity_id == other.entity_id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -106,6 +106,16 @@ pub trait VisualContext: Context {
|
|||||||
) -> Self::Result<R>;
|
) -> Self::Result<R>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait EntityHandle<T> {
|
||||||
|
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 {
|
pub enum GlobalKey {
|
||||||
Numeric(usize),
|
Numeric(usize),
|
||||||
View(EntityId),
|
View(EntityId),
|
||||||
|
|||||||
+24
-12
@@ -1,7 +1,7 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
AnyBox, AnyElement, AvailableSpace, BorrowWindow, Bounds, Component, Element, ElementId,
|
AnyBox, AnyElement, AppContext, AvailableSpace, BorrowWindow, Bounds, Component, Element,
|
||||||
EntityId, Flatten, Handle, LayoutId, Pixels, Size, ViewContext, VisualContext, WeakHandle,
|
ElementId, EntityHandle, EntityId, Flatten, Handle, LayoutId, Pixels, Size, ViewContext,
|
||||||
WindowContext,
|
VisualContext, WeakHandle, WindowContext,
|
||||||
};
|
};
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
@@ -31,9 +31,7 @@ impl<V: 'static> View<V> {
|
|||||||
)),
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl<V: 'static> View<V> {
|
|
||||||
pub fn into_any(self) -> AnyView {
|
pub fn into_any(self) -> AnyView {
|
||||||
AnyView(Arc::new(self))
|
AnyView(Arc::new(self))
|
||||||
}
|
}
|
||||||
@@ -44,9 +42,7 @@ impl<V: 'static> View<V> {
|
|||||||
render: Arc::downgrade(&self.render),
|
render: Arc::downgrade(&self.render),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl<V: 'static> View<V> {
|
|
||||||
pub fn update<C, R>(
|
pub fn update<C, R>(
|
||||||
&self,
|
&self,
|
||||||
cx: &mut C,
|
cx: &mut C,
|
||||||
@@ -58,11 +54,8 @@ impl<V: 'static> View<V> {
|
|||||||
cx.update_view(self, f)
|
cx.update_view(self, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read<C>(&self, cx: &mut C) -> &V
|
pub fn read<'a>(&self, cx: &'a AppContext) -> &'a V {
|
||||||
where
|
cx.entities.read(&self.state)
|
||||||
C: VisualContext,
|
|
||||||
{
|
|
||||||
todo!()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,6 +117,25 @@ impl<V: 'static> Element<()> for View<V> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T: 'static> EntityHandle<T> for View<T> {
|
||||||
|
type Weak = WeakView<T>;
|
||||||
|
|
||||||
|
fn entity_id(&self) -> EntityId {
|
||||||
|
self.state.entity_id
|
||||||
|
}
|
||||||
|
|
||||||
|
fn downgrade(&self) -> Self::Weak {
|
||||||
|
self.downgrade()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn upgrade_from(weak: &Self::Weak) -> Option<Self>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
{
|
||||||
|
weak.upgrade()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct WeakView<V> {
|
pub struct WeakView<V> {
|
||||||
pub(crate) state: WeakHandle<V>,
|
pub(crate) state: WeakHandle<V>,
|
||||||
render: Weak<Mutex<dyn Fn(&mut V, &mut ViewContext<V>) -> AnyElement<V> + Send + 'static>>,
|
render: Weak<Mutex<dyn Fn(&mut V, &mut ViewContext<V>) -> AnyElement<V> + Send + 'static>>,
|
||||||
|
|||||||
+48
-16
@@ -1,14 +1,14 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
px, size, Action, AnyBox, AnyDrag, AnyView, AppContext, AsyncWindowContext, AvailableSpace,
|
px, size, Action, AnyBox, AnyDrag, AnyView, AppContext, AsyncWindowContext, AvailableSpace,
|
||||||
Bounds, BoxShadow, Context, Corners, DevicePixels, DispatchContext, DisplayId, Edges, Effect,
|
Bounds, BoxShadow, Context, Corners, DevicePixels, DispatchContext, DisplayId, Edges, Effect,
|
||||||
EntityId, EventEmitter, ExternalPaths, FileDropEvent, FocusEvent, FontId, GlobalElementId,
|
EntityHandle, EntityId, EventEmitter, ExternalPaths, FileDropEvent, FocusEvent, FontId,
|
||||||
GlyphId, Handle, Hsla, ImageData, InputEvent, IsZero, KeyListener, KeyMatch, KeyMatcher,
|
GlobalElementId, GlyphId, Handle, Hsla, ImageData, InputEvent, IsZero, KeyListener, KeyMatch,
|
||||||
Keystroke, LayoutId, MainThread, MainThreadOnly, ModelContext, Modifiers, MonochromeSprite,
|
KeyMatcher, Keystroke, LayoutId, MainThread, MainThreadOnly, ModelContext, Modifiers,
|
||||||
MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Path, Pixels, PlatformAtlas,
|
MonochromeSprite, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Path, Pixels,
|
||||||
PlatformWindow, Point, PolychromeSprite, Quad, Reference, RenderGlyphParams, RenderImageParams,
|
PlatformAtlas, PlatformWindow, Point, PolychromeSprite, Quad, Reference, RenderGlyphParams,
|
||||||
RenderSvgParams, ScaledPixels, SceneBuilder, Shadow, SharedString, Size, Style, Subscription,
|
RenderImageParams, RenderSvgParams, ScaledPixels, SceneBuilder, Shadow, SharedString, Size,
|
||||||
TaffyLayoutEngine, Task, Underline, UnderlineStyle, View, VisualContext, WeakHandle, WeakView,
|
Style, Subscription, TaffyLayoutEngine, Task, Underline, UnderlineStyle, View, VisualContext,
|
||||||
WindowOptions, SUBPIXEL_VARIANTS,
|
WeakHandle, WeakView, WindowOptions, SUBPIXEL_VARIANTS,
|
||||||
};
|
};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use collections::HashMap;
|
use collections::HashMap;
|
||||||
@@ -376,6 +376,35 @@ impl<'a, 'w> WindowContext<'a, 'w> {
|
|||||||
self.notify();
|
self.notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn subscribe<E, H>(
|
||||||
|
&mut self,
|
||||||
|
handle: &H,
|
||||||
|
mut on_event: impl FnMut(H, &E::Event, &mut WindowContext<'_, '_>) + Send + 'static,
|
||||||
|
) -> Subscription
|
||||||
|
where
|
||||||
|
E: EventEmitter,
|
||||||
|
H: EntityHandle<E>,
|
||||||
|
{
|
||||||
|
let entity_id = handle.entity_id();
|
||||||
|
let handle = handle.downgrade();
|
||||||
|
let window_handle = self.window.handle;
|
||||||
|
self.app.event_listeners.insert(
|
||||||
|
entity_id,
|
||||||
|
Box::new(move |event, cx| {
|
||||||
|
cx.update_window(window_handle, |cx| {
|
||||||
|
if let Some(handle) = H::upgrade_from(&handle) {
|
||||||
|
let event = event.downcast_ref().expect("invalid event type");
|
||||||
|
on_event(handle, event, cx);
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.unwrap_or(false)
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// Schedule the given closure to be run on the main thread. It will be invoked with
|
/// Schedule the given closure to be run on the main thread. It will be invoked with
|
||||||
/// a `MainThread<WindowContext>`, which provides access to platform-specific functionality
|
/// a `MainThread<WindowContext>`, which provides access to platform-specific functionality
|
||||||
/// of the window.
|
/// of the window.
|
||||||
@@ -1600,21 +1629,24 @@ impl<'a, 'w, V: 'static> ViewContext<'a, 'w, V> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn subscribe<E: EventEmitter>(
|
pub fn subscribe<E, H>(
|
||||||
&mut self,
|
&mut self,
|
||||||
handle: &Handle<E>,
|
handle: &H,
|
||||||
mut on_event: impl FnMut(&mut V, Handle<E>, &E::Event, &mut ViewContext<'_, '_, V>)
|
mut on_event: impl FnMut(&mut V, H, &E::Event, &mut ViewContext<'_, '_, V>) + Send + 'static,
|
||||||
+ Send
|
) -> Subscription
|
||||||
+ 'static,
|
where
|
||||||
) -> Subscription {
|
E: EventEmitter,
|
||||||
|
H: EntityHandle<E>,
|
||||||
|
{
|
||||||
let view = self.view();
|
let view = self.view();
|
||||||
|
let entity_id = handle.entity_id();
|
||||||
let handle = handle.downgrade();
|
let handle = handle.downgrade();
|
||||||
let window_handle = self.window.handle;
|
let window_handle = self.window.handle;
|
||||||
self.app.event_listeners.insert(
|
self.app.event_listeners.insert(
|
||||||
handle.entity_id,
|
entity_id,
|
||||||
Box::new(move |event, cx| {
|
Box::new(move |event, cx| {
|
||||||
cx.update_window(window_handle, |cx| {
|
cx.update_window(window_handle, |cx| {
|
||||||
if let Some(handle) = handle.upgrade() {
|
if let Some(handle) = H::upgrade_from(&handle) {
|
||||||
let event = event.downcast_ref().expect("invalid event type");
|
let event = event.downcast_ref().expect("invalid event type");
|
||||||
view.update(cx, |this, cx| on_event(this, handle, event, cx))
|
view.update(cx, |this, cx| on_event(this, handle, event, cx))
|
||||||
.is_ok()
|
.is_ok()
|
||||||
|
|||||||
@@ -104,30 +104,26 @@ pub trait Item: EventEmitter + Sized {
|
|||||||
// fn navigate(&mut self, _: Box<dyn Any>, _: &mut ViewContext<Self>) -> bool {
|
// fn navigate(&mut self, _: Box<dyn Any>, _: &mut ViewContext<Self>) -> bool {
|
||||||
// false
|
// false
|
||||||
// }
|
// }
|
||||||
// fn tab_tooltip_text(&self, _: &AppContext) -> Option<Cow<str>> {
|
fn tab_tooltip_text(&self, _: &AppContext) -> Option<SharedString> {
|
||||||
// None
|
None
|
||||||
// }
|
}
|
||||||
// fn tab_description<'a>(&'a self, _: usize, _: &'a AppContext) -> Option<Cow<str>> {
|
fn tab_description(&self, _: usize, _: &AppContext) -> Option<SharedString> {
|
||||||
// None
|
None
|
||||||
// }
|
}
|
||||||
// fn tab_content<V: 'static>(
|
fn tab_content<V: 'static>(&self, detail: Option<usize>, cx: &AppContext) -> AnyElement<V>;
|
||||||
// &self,
|
|
||||||
// detail: Option<usize>,
|
fn for_each_project_item(&self, _: &AppContext, _: &mut dyn FnMut(usize, &dyn project2::Item)) {
|
||||||
// style: &theme2::Tab,
|
} // (model id, Item)
|
||||||
// cx: &AppContext,
|
|
||||||
// ) -> AnyElement<V>;
|
|
||||||
// fn for_each_project_item(&self, _: &AppContext, _: &mut dyn FnMut(usize, &dyn project2::Item)) {
|
|
||||||
// } // (model id, Item)
|
|
||||||
fn is_singleton(&self, _cx: &AppContext) -> bool {
|
fn is_singleton(&self, _cx: &AppContext) -> bool {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
// fn set_nav_history(&mut self, _: ItemNavHistory, _: &mut ViewContext<Self>) {}
|
// fn set_nav_history(&mut self, _: ItemNavHistory, _: &mut ViewContext<Self>) {}
|
||||||
// fn clone_on_split(&self, _workspace_id: WorkspaceId, _: &mut ViewContext<Self>) -> Option<Self>
|
fn clone_on_split(&self, _workspace_id: WorkspaceId, _: &mut ViewContext<Self>) -> Option<Self>
|
||||||
// where
|
where
|
||||||
// Self: Sized,
|
Self: Sized,
|
||||||
// {
|
{
|
||||||
// None
|
None
|
||||||
// }
|
}
|
||||||
// fn is_dirty(&self, _: &AppContext) -> bool {
|
// fn is_dirty(&self, _: &AppContext) -> bool {
|
||||||
// false
|
// false
|
||||||
// }
|
// }
|
||||||
@@ -221,7 +217,6 @@ pub trait Item: EventEmitter + Sized {
|
|||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
any::Any,
|
any::Any,
|
||||||
borrow::Cow,
|
|
||||||
cell::RefCell,
|
cell::RefCell,
|
||||||
ops::Range,
|
ops::Range,
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
@@ -235,7 +230,7 @@ use std::{
|
|||||||
|
|
||||||
use gpui2::{
|
use gpui2::{
|
||||||
AnyElement, AnyWindowHandle, AppContext, EventEmitter, Handle, HighlightStyle, Pixels, Point,
|
AnyElement, AnyWindowHandle, AppContext, EventEmitter, Handle, HighlightStyle, Pixels, Point,
|
||||||
Task, View, ViewContext, WindowContext,
|
SharedString, Task, View, ViewContext, VisualContext, WindowContext,
|
||||||
};
|
};
|
||||||
use project2::{Project, ProjectEntryId, ProjectPath};
|
use project2::{Project, ProjectEntryId, ProjectPath};
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
@@ -252,10 +247,10 @@ pub trait ItemHandle: 'static + Send {
|
|||||||
fn subscribe_to_item_events(
|
fn subscribe_to_item_events(
|
||||||
&self,
|
&self,
|
||||||
cx: &mut WindowContext,
|
cx: &mut WindowContext,
|
||||||
handler: Box<dyn Fn(ItemEvent, &mut WindowContext)>,
|
handler: Box<dyn Fn(ItemEvent, &mut WindowContext) + Send>,
|
||||||
) -> gpui2::Subscription;
|
) -> gpui2::Subscription;
|
||||||
fn tab_tooltip_text<'a>(&self, cx: &'a AppContext) -> Option<Cow<'a, str>>;
|
fn tab_tooltip_text(&self, cx: &AppContext) -> Option<SharedString>;
|
||||||
fn tab_description<'a>(&'a self, detail: usize, cx: &'a AppContext) -> Option<Cow<'a, str>>;
|
fn tab_description(&self, detail: usize, cx: &AppContext) -> Option<SharedString>;
|
||||||
fn tab_content(&self, detail: Option<usize>, cx: &AppContext) -> AnyElement<Pane>;
|
fn tab_content(&self, detail: Option<usize>, cx: &AppContext) -> AnyElement<Pane>;
|
||||||
fn dragged_tab_content(&self, detail: Option<usize>, cx: &AppContext) -> AnyElement<Workspace>;
|
fn dragged_tab_content(&self, detail: Option<usize>, cx: &AppContext) -> AnyElement<Workspace>;
|
||||||
fn project_path(&self, cx: &AppContext) -> Option<ProjectPath>;
|
fn project_path(&self, cx: &AppContext) -> Option<ProjectPath>;
|
||||||
@@ -329,7 +324,7 @@ impl<T: Item> ItemHandle for View<T> {
|
|||||||
fn subscribe_to_item_events(
|
fn subscribe_to_item_events(
|
||||||
&self,
|
&self,
|
||||||
cx: &mut WindowContext,
|
cx: &mut WindowContext,
|
||||||
handler: Box<dyn Fn(ItemEvent, &mut WindowContext)>,
|
handler: Box<dyn Fn(ItemEvent, &mut WindowContext) + Send>,
|
||||||
) -> gpui2::Subscription {
|
) -> gpui2::Subscription {
|
||||||
cx.subscribe(self, move |_, event, cx| {
|
cx.subscribe(self, move |_, event, cx| {
|
||||||
for item_event in T::to_item_events(event) {
|
for item_event in T::to_item_events(event) {
|
||||||
@@ -338,11 +333,11 @@ impl<T: Item> ItemHandle for View<T> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tab_tooltip_text<'a>(&self, cx: &'a AppContext) -> Option<Cow<'a, str>> {
|
fn tab_tooltip_text(&self, cx: &AppContext) -> Option<SharedString> {
|
||||||
self.read(cx).tab_tooltip_text(cx)
|
self.read(cx).tab_tooltip_text(cx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tab_description<'a>(&'a self, detail: usize, cx: &'a AppContext) -> Option<Cow<'a, str>> {
|
fn tab_description(&self, detail: usize, cx: &AppContext) -> Option<SharedString> {
|
||||||
self.read(cx).tab_description(detail, cx)
|
self.read(cx).tab_description(detail, cx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user