History

WIP

Antonio Scandurra committed 3 years ago

Commit 96889371 parent b9ce1865 files · +127 −59Browse files

Unified · Split

5 files
  1. crates/gpui2/src/app/entity_map.rs +22 −3
  2. crates/gpui2/src/gpui2.rs +10 −0
  3. crates/gpui2/src/view.rs +24 −12
  4. crates/gpui2/src/window.rs +48 −16
  5. crates/workspace2/src/item.rs +23 −28
crates/gpui2/src/app/entity_map.rs+22−3
@@ -1,4 +1,4 @@
1−use crate::{AnyBox, AppContext, Context};
1+use crate::{AnyBox, AppContext, Context, EntityHandle};
22 use anyhow::{anyhow, Result};
33 use derive_more::{Deref, DerefMut};
44 use parking_lot::{RwLock, RwLockUpgradableReadGuard};
@@ -329,7 +329,26 @@ impl<T> Eq for Handle<T> {} · 324 unmodified lines
329329
330330 impl<T> PartialEq<WeakHandle<T>> for Handle<T> {
331331 fn eq(&self, other: &WeakHandle<T>) -> bool {
332− self.entity_id() == other.entity_id()
332+ self.entity_id == other.entity_id
333+ }
334+}
335+
336+impl<T: 'static> EntityHandle<T> for Handle<T> {
337+ type Weak = WeakHandle<T>;
338+
339+ fn entity_id(&self) -> EntityId {
340+ self.entity_id
341+ }
342+
343+ fn downgrade(&self) -> Self::Weak {
344+ self.downgrade()
345+ }
346+
347+ fn upgrade_from(weak: &Self::Weak) -> Option<Self>
348+ where
349+ Self: Sized,
350+ {
351+ weak.upgrade()
333352 }
334353 }
335354
@@ -457,6 +476,6 @@ impl<T> Eq for WeakHandle<T> {} · 121 unmodified lines
457476
458477 impl<T> PartialEq<Handle<T>> for WeakHandle<T> {
459478 fn eq(&self, other: &Handle<T>) -> bool {
460− self.entity_id() == other.entity_id()
479+ self.entity_id == other.entity_id
461480 }
462481 }
crates/gpui2/src/gpui2.rs+10−0
@@ -106,6 +106,16 @@ pub trait VisualContext: Context { · 105 unmodified lines
106106 ) -> Self::Result<R>;
107107 }
108108
109+pub trait EntityHandle<T> {
110+ type Weak: 'static + Send;
111+
112+ fn entity_id(&self) -> EntityId;
113+ fn downgrade(&self) -> Self::Weak;
114+ fn upgrade_from(weak: &Self::Weak) -> Option<Self>
115+ where
116+ Self: Sized;
117+}
118+
109119 pub enum GlobalKey {
110120 Numeric(usize),
111121 View(EntityId),
crates/gpui2/src/view.rs+24−12
@@ -1,7 +1,7 @@
11 use crate::{
2− AnyBox, AnyElement, AvailableSpace, BorrowWindow, Bounds, Component, Element, ElementId,
3− EntityId, Flatten, Handle, LayoutId, Pixels, Size, ViewContext, VisualContext, WeakHandle,
4− WindowContext,
2+ AnyBox, AnyElement, AppContext, AvailableSpace, BorrowWindow, Bounds, Component, Element,
3+ ElementId, EntityHandle, EntityId, Flatten, Handle, LayoutId, Pixels, Size, ViewContext,
4+ VisualContext, WeakHandle, WindowContext,
55 };
66 use anyhow::{Context, Result};
77 use parking_lot::Mutex;
@@ -31,9 +31,7 @@ impl<V: 'static> View<V> { · 23 unmodified lines
3131 )),
3232 }
3333 }
34−}
3534
36−impl<V: 'static> View<V> {
3735 pub fn into_any(self) -> AnyView {
3836 AnyView(Arc::new(self))
3937 }
@@ -44,9 +42,7 @@ impl<V: 'static> View<V> { · 4 unmodified lines
4442 render: Arc::downgrade(&self.render),
4543 }
4644 }
47−}
4845
49−impl<V: 'static> View<V> {
5046 pub fn update<C, R>(
5147 &self,
5248 cx: &mut C,
@@ -58,11 +54,8 @@ impl<V: 'static> View<V> { · 5 unmodified lines
5854 cx.update_view(self, f)
5955 }
6056
61− pub fn read<C>(&self, cx: &mut C) -> &V
62− where
63− C: VisualContext,
64− {
65− todo!()
57+ pub fn read<'a>(&self, cx: &'a AppContext) -> &'a V {
58+ cx.entities.read(&self.state)
6659 }
6760 }
6861
@@ -124,6 +117,25 @@ impl<V: 'static> Element<()> for View<V> { · 55 unmodified lines
124117 }
125118 }
126119
120+impl<T: 'static> EntityHandle<T> for View<T> {
121+ type Weak = WeakView<T>;
122+
123+ fn entity_id(&self) -> EntityId {
124+ self.state.entity_id
125+ }
126+
127+ fn downgrade(&self) -> Self::Weak {
128+ self.downgrade()
129+ }
130+
131+ fn upgrade_from(weak: &Self::Weak) -> Option<Self>
132+ where
133+ Self: Sized,
134+ {
135+ weak.upgrade()
136+ }
137+}
138+
127139 pub struct WeakView<V> {
128140 pub(crate) state: WeakHandle<V>,
129141 render: Weak<Mutex<dyn Fn(&mut V, &mut ViewContext<V>) -> AnyElement<V> + Send + 'static>>,
crates/gpui2/src/window.rs+48−16
@@ -1,14 +1,14 @@
11 use crate::{
22 px, size, Action, AnyBox, AnyDrag, AnyView, AppContext, AsyncWindowContext, AvailableSpace,
33 Bounds, BoxShadow, Context, Corners, DevicePixels, DispatchContext, DisplayId, Edges, Effect,
4− EntityId, EventEmitter, ExternalPaths, FileDropEvent, FocusEvent, FontId, GlobalElementId,
5− GlyphId, Handle, Hsla, ImageData, InputEvent, IsZero, KeyListener, KeyMatch, KeyMatcher,
6− Keystroke, LayoutId, MainThread, MainThreadOnly, ModelContext, Modifiers, MonochromeSprite,
7− MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Path, Pixels, PlatformAtlas,
8− PlatformWindow, Point, PolychromeSprite, Quad, Reference, RenderGlyphParams, RenderImageParams,
9− RenderSvgParams, ScaledPixels, SceneBuilder, Shadow, SharedString, Size, Style, Subscription,
10− TaffyLayoutEngine, Task, Underline, UnderlineStyle, View, VisualContext, WeakHandle, WeakView,
11− WindowOptions, SUBPIXEL_VARIANTS,
4+ EntityHandle, EntityId, EventEmitter, ExternalPaths, FileDropEvent, FocusEvent, FontId,
5+ GlobalElementId, GlyphId, Handle, Hsla, ImageData, InputEvent, IsZero, KeyListener, KeyMatch,
6+ KeyMatcher, Keystroke, LayoutId, MainThread, MainThreadOnly, ModelContext, Modifiers,
7+ MonochromeSprite, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Path, Pixels,
8+ PlatformAtlas, PlatformWindow, Point, PolychromeSprite, Quad, Reference, RenderGlyphParams,
9+ RenderImageParams, RenderSvgParams, ScaledPixels, SceneBuilder, Shadow, SharedString, Size,
10+ Style, Subscription, TaffyLayoutEngine, Task, Underline, UnderlineStyle, View, VisualContext,
11+ WeakHandle, WeakView, WindowOptions, SUBPIXEL_VARIANTS,
1212 };
1313 use anyhow::Result;
1414 use collections::HashMap;
@@ -376,6 +376,35 @@ impl<'a, 'w> WindowContext<'a, 'w> { · 361 unmodified lines
376376 self.notify();
377377 }
378378
379+ pub fn subscribe<E, H>(
380+ &mut self,
381+ handle: &H,
382+ mut on_event: impl FnMut(H, &E::Event, &mut WindowContext<'_, '_>) + Send + 'static,
383+ ) -> Subscription
384+ where
385+ E: EventEmitter,
386+ H: EntityHandle<E>,
387+ {
388+ let entity_id = handle.entity_id();
389+ let handle = handle.downgrade();
390+ let window_handle = self.window.handle;
391+ self.app.event_listeners.insert(
392+ entity_id,
393+ Box::new(move |event, cx| {
394+ cx.update_window(window_handle, |cx| {
395+ if let Some(handle) = H::upgrade_from(&handle) {
396+ let event = event.downcast_ref().expect("invalid event type");
397+ on_event(handle, event, cx);
398+ true
399+ } else {
400+ false
401+ }
402+ })
403+ .unwrap_or(false)
404+ }),
405+ )
406+ }
407+
379408 /// Schedule the given closure to be run on the main thread. It will be invoked with
380409 /// a `MainThread<WindowContext>`, which provides access to platform-specific functionality
381410 /// of the window.
@@ -1600,21 +1629,24 @@ impl<'a, 'w, V: 'static> ViewContext<'a, 'w, V> { · 1218 unmodified lines
16001629 )
16011630 }
16021631
1603− pub fn subscribe<E: EventEmitter>(
1632+ pub fn subscribe<E, H>(
16041633 &mut self,
1605− handle: &Handle<E>,
1606− mut on_event: impl FnMut(&mut V, Handle<E>, &E::Event, &mut ViewContext<'_, '_, V>)
1607− + Send
1608− + 'static,
1609− ) -> Subscription {
1634+ handle: &H,
1635+ mut on_event: impl FnMut(&mut V, H, &E::Event, &mut ViewContext<'_, '_, V>) + Send + 'static,
1636+ ) -> Subscription
1637+ where
1638+ E: EventEmitter,
1639+ H: EntityHandle<E>,
1640+ {
16101641 let view = self.view();
1642+ let entity_id = handle.entity_id();
16111643 let handle = handle.downgrade();
16121644 let window_handle = self.window.handle;
16131645 self.app.event_listeners.insert(
1614− handle.entity_id,
1646+ entity_id,
16151647 Box::new(move |event, cx| {
16161648 cx.update_window(window_handle, |cx| {
1617− if let Some(handle) = handle.upgrade() {
1649+ if let Some(handle) = H::upgrade_from(&handle) {
16181650 let event = event.downcast_ref().expect("invalid event type");
16191651 view.update(cx, |this, cx| on_event(this, handle, event, cx))
16201652 .is_ok()
crates/workspace2/src/item.rs+23−28
@@ -104,30 +104,26 @@ pub trait Item: EventEmitter + Sized { · 103 unmodified lines
104104 // fn navigate(&mut self, _: Box<dyn Any>, _: &mut ViewContext<Self>) -> bool {
105105 // false
106106 // }
107− // fn tab_tooltip_text(&self, _: &AppContext) -> Option<Cow<str>> {
108− // None
109− // }
110− // fn tab_description<'a>(&'a self, _: usize, _: &'a AppContext) -> Option<Cow<str>> {
111− // None
112− // }
113− // fn tab_content<V: 'static>(
114− // &self,
115− // detail: Option<usize>,
116− // style: &theme2::Tab,
117− // cx: &AppContext,
118− // ) -> AnyElement<V>;
119− // fn for_each_project_item(&self, _: &AppContext, _: &mut dyn FnMut(usize, &dyn project2::Item)) {
120− // } // (model id, Item)
107+ fn tab_tooltip_text(&self, _: &AppContext) -> Option<SharedString> {
108+ None
109+ }
110+ fn tab_description(&self, _: usize, _: &AppContext) -> Option<SharedString> {
111+ None
112+ }
113+ fn tab_content<V: 'static>(&self, detail: Option<usize>, cx: &AppContext) -> AnyElement<V>;
114+
115+ fn for_each_project_item(&self, _: &AppContext, _: &mut dyn FnMut(usize, &dyn project2::Item)) {
116+ } // (model id, Item)
121117 fn is_singleton(&self, _cx: &AppContext) -> bool {
122118 false
123119 }
124120 // fn set_nav_history(&mut self, _: ItemNavHistory, _: &mut ViewContext<Self>) {}
125− // fn clone_on_split(&self, _workspace_id: WorkspaceId, _: &mut ViewContext<Self>) -> Option<Self>
126− // where
127− // Self: Sized,
128− // {
129− // None
130− // }
121+ fn clone_on_split(&self, _workspace_id: WorkspaceId, _: &mut ViewContext<Self>) -> Option<Self>
122+ where
123+ Self: Sized,
124+ {
125+ None
126+ }
131127 // fn is_dirty(&self, _: &AppContext) -> bool {
132128 // false
133129 // }
@@ -221,7 +217,6 @@ pub trait Item: EventEmitter + Sized { · 87 unmodified lines
221217
222218 use std::{
223219 any::Any,
224− borrow::Cow,
225220 cell::RefCell,
226221 ops::Range,
227222 path::PathBuf,
@@ -235,7 +230,7 @@ use std::{ · 7 unmodified lines
235230
236231 use gpui2::{
237232 AnyElement, AnyWindowHandle, AppContext, EventEmitter, Handle, HighlightStyle, Pixels, Point,
238− Task, View, ViewContext, WindowContext,
233+ SharedString, Task, View, ViewContext, VisualContext, WindowContext,
239234 };
240235 use project2::{Project, ProjectEntryId, ProjectPath};
241236 use smallvec::SmallVec;
@@ -252,10 +247,10 @@ pub trait ItemHandle: 'static + Send { · 10 unmodified lines
252247 fn subscribe_to_item_events(
253248 &self,
254249 cx: &mut WindowContext,
255− handler: Box<dyn Fn(ItemEvent, &mut WindowContext)>,
250+ handler: Box<dyn Fn(ItemEvent, &mut WindowContext) + Send>,
256251 ) -> gpui2::Subscription;
257− fn tab_tooltip_text<'a>(&self, cx: &'a AppContext) -> Option<Cow<'a, str>>;
258− fn tab_description<'a>(&'a self, detail: usize, cx: &'a AppContext) -> Option<Cow<'a, str>>;
252+ fn tab_tooltip_text(&self, cx: &AppContext) -> Option<SharedString>;
253+ fn tab_description(&self, detail: usize, cx: &AppContext) -> Option<SharedString>;
259254 fn tab_content(&self, detail: Option<usize>, cx: &AppContext) -> AnyElement<Pane>;
260255 fn dragged_tab_content(&self, detail: Option<usize>, cx: &AppContext) -> AnyElement<Workspace>;
261256 fn project_path(&self, cx: &AppContext) -> Option<ProjectPath>;
@@ -329,7 +324,7 @@ impl<T: Item> ItemHandle for View<T> { · 67 unmodified lines
329324 fn subscribe_to_item_events(
330325 &self,
331326 cx: &mut WindowContext,
332− handler: Box<dyn Fn(ItemEvent, &mut WindowContext)>,
327+ handler: Box<dyn Fn(ItemEvent, &mut WindowContext) + Send>,
333328 ) -> gpui2::Subscription {
334329 cx.subscribe(self, move |_, event, cx| {
335330 for item_event in T::to_item_events(event) {
@@ -338,11 +333,11 @@ impl<T: Item> ItemHandle for View<T> { · 2 unmodified lines
338333 })
339334 }
340335
341− fn tab_tooltip_text<'a>(&self, cx: &'a AppContext) -> Option<Cow<'a, str>> {
336+ fn tab_tooltip_text(&self, cx: &AppContext) -> Option<SharedString> {
342337 self.read(cx).tab_tooltip_text(cx)
343338 }
344339
345− fn tab_description<'a>(&'a self, detail: usize, cx: &'a AppContext) -> Option<Cow<'a, str>> {
340+ fn tab_description(&self, detail: usize, cx: &AppContext) -> Option<SharedString> {
346341 self.read(cx).tab_description(detail, cx)
347342 }
348343

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t