Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Merge branch 'zed2' into zed2-workspace

Antonio Scandurrabd750fbbe2f2
diff --cc crates/gpui2/src/app.rs
index 72b8be2f741,57ea79afbee..d39083c1aa1
--- a/crates/gpui2/src/app.rs
+++ b/crates/gpui2/src/app.rs
@@@ -380,9 -382,13 +394,12 @@@ 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 {
- self.update_window(window_id, |cx| {
+ for window_handle in self.windows() {
+ self.update_window(window_handle, |cx| {
let mut blur_window = false;
let focus = cx.window.focus;
cx.window.focus_handles.write().retain(|handle_id, count| {
@@@ -414,15 -420,11 +431,15 @@@
fn apply_emit_effect(&mut self, emitter: EntityId, event: Box<dyn Any>) {
self.event_listeners
.clone()
- .retain(&emitter, |handler| handler(&event, self));
+ .retain(&emitter, |handler| handler(event.as_ref(), self));
}
- fn apply_focus_changed_effect(&mut self, window_id: WindowId, focused: Option<FocusId>) {
- self.update_window(window_id, |cx| {
+ fn apply_focus_changed_effect(
+ &mut self,
+ window_handle: AnyWindowHandle,
+ focused: Option<FocusId>,
+ ) {
+ self.update_window(window_handle, |cx| {
if cx.window.focus == focused {
let mut listeners = mem::take(&mut cx.window.focus_listeners);
let focused =
@@@ -622,20 -654,6 +669,20 @@@
self.globals_by_type.insert(global_type, lease.global);
}
+ pub fn observe_release<E: 'static>(
+ &mut self,
+ handle: &Handle<E>,
- mut on_release: impl FnMut(&mut E, &mut AppContext) + Send + Sync + 'static,
++ mut on_release: impl FnMut(&mut E, &mut AppContext) + Send + 'static,
+ ) -> Subscription {
+ self.release_listeners.insert(
+ handle.entity_id,
+ Box::new(move |entity, cx| {
+ let entity = entity.downcast_mut().expect("invalid entity type");
+ on_release(entity, cx)
+ }),
+ )
+ }
+
pub(crate) fn push_text_style(&mut self, text_style: TextStyleRefinement) {
self.text_style_stack.push(text_style);
}
@@@ -812,10 -851,10 +880,10 @@@ pub(crate) enum Effect
},
Emit {
emitter: EntityId,
- event: Box<dyn Any + Send + Sync + 'static>,
+ event: Box<dyn Any + Send + 'static>,
},
FocusChanged {
- window_id: WindowId,
+ window_handle: AnyWindowHandle,
focused: Option<FocusId>,
},
Refresh,
diff --cc crates/gpui2/src/app/async_context.rs
index ab42a56cbb2,d49f2ab934e..6615fd535e1
--- a/crates/gpui2/src/app/async_context.rs
+++ b/crates/gpui2/src/app/async_context.rs
@@@ -1,11 -1,11 +1,11 @@@
use crate::{
AnyWindowHandle, AppContext, Context, Executor, Handle, MainThread, ModelContext, Result, Task,
- ViewContext, WindowContext,
+ WindowContext,
};
-use anyhow::anyhow;
+use anyhow::Context as _;
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 {
@@@ -19,12 -19,15 +19,12 @@@ impl Context for AsyncAppContext
fn entity<T: 'static>(
&mut self,
- build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
+ build_entity: impl FnOnce(&mut Self::EntityContext<'_, T>) -> T,
) -> Self::Result<Handle<T>>
where
- T: Any + Send + Sync,
+ T: 'static + Send,
{
- let app = self
- .app
- .upgrade()
- .ok_or_else(|| anyhow!("app was released"))?;
+ let app = self.app.upgrade().context("app was released")?;
let mut lock = app.lock(); // Need this to compile
Ok(lock.entity(build_entity))
}
@@@ -32,9 -35,12 +32,9 @@@
fn update_entity<T: 'static, R>(
&mut self,
handle: &Handle<T>,
- update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R,
+ update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, T>) -> R,
) -> Self::Result<R> {
- let app = self
- .app
- .upgrade()
- .ok_or_else(|| anyhow!("app was released"))?;
+ let app = self.app.upgrade().context("app was released")?;
let mut lock = app.lock(); // Need this to compile
Ok(lock.update_entity(handle, update))
}
diff --cc crates/gpui2/src/app/model_context.rs
index f3d0bf23976,effda37b737..8354bda9a84
--- a/crates/gpui2/src/app/model_context.rs
+++ b/crates/gpui2/src/app/model_context.rs
@@@ -109,17 -108,21 +108,17 @@@ impl<'a, T: 'static> ModelContext<'a, T
pub fn observe_release<E: 'static>(
&mut self,
handle: &Handle<E>,
- mut on_release: impl FnMut(&mut T, &mut E, &mut ModelContext<'_, T>) + Send + Sync + 'static,
+ mut on_release: impl FnMut(&mut T, &mut E, &mut ModelContext<'_, T>) + Send + 'static,
) -> Subscription
where
- T: Any + Send + Sync,
+ T: Any + Send,
{
let this = self.weak_handle();
- self.app.release_listeners.insert(
- handle.entity_id,
- Box::new(move |entity, cx| {
- let entity = entity.downcast_mut().expect("invalid entity type");
- if let Some(this) = this.upgrade() {
- this.update(cx, |this, cx| on_release(this, entity, cx));
- }
- }),
- )
+ self.app.observe_release(handle, move |entity, cx| {
+ if let Some(this) = this.upgrade() {
+ this.update(cx, |this, cx| on_release(this, entity, cx));
+ }
+ })
}
pub fn observe_global<G: 'static>(
diff --cc crates/gpui2/src/view.rs
index 36f1f5d9cd0,4bb9c3d3a82..cf7441249f1
--- a/crates/gpui2/src/view.rs
+++ b/crates/gpui2/src/view.rs
@@@ -148,14 -204,14 +204,14 @@@ trait ViewObject: Send + Sync
}
impl<V: 'static> ViewObject for View<V> {
- fn entity_handle(&self) -> &AnyHandle {
- &self.state
+ fn entity_id(&self) -> EntityId {
- self.state.entity_id
++ self.state.entity_id()
}
- fn initialize(&mut self, cx: &mut WindowContext) -> AnyBox {
- cx.with_element_id(self.state.entity_id, |_global_id, cx| {
- self.state.update(cx, |state, cx| {
- let mut any_element = Box::new((self.render)(state, cx));
+ fn initialize(&self, cx: &mut WindowContext) -> AnyBox {
+ cx.with_element_id(self.entity_id(), |_global_id, cx| {
+ self.update(cx, |state, cx| {
+ let mut any_element = Box::new((self.render.lock())(state, cx));
any_element.initialize(state, cx);
any_element as AnyBox
})
diff --cc crates/gpui2/src/window.rs
index c23f60de0c0,fd5aba6057e..a197b0b6159
--- a/crates/gpui2/src/window.rs
+++ b/crates/gpui2/src/window.rs
@@@ -144,8 -156,9 +156,9 @@@ impl Drop for FocusHandle
}
}
+ // Holds the state for a specific window.
pub struct Window {
- handle: AnyWindowHandle,
+ pub(crate) handle: AnyWindowHandle,
platform_window: MainThreadOnly<Box<dyn PlatformWindow>>,
display_id: DisplayId,
sprite_atlas: Arc<dyn PlatformAtlas>,
@@@ -1081,14 -1147,16 +1145,16 @@@ impl<'a, 'w> WindowContext<'a, 'w>
key_match
}
+ /// Register the given handler to be invoked whenever the global of the given type
+ /// is updated.
pub fn observe_global<G: 'static>(
&mut self,
- f: impl Fn(&mut WindowContext<'_, '_>) + Send + Sync + 'static,
+ f: impl Fn(&mut WindowContext<'_, '_>) + Send + 'static,
) -> Subscription {
- let window_id = self.window.handle.id;
+ let window_handle = self.window.handle;
self.global_observers.insert(
TypeId::of::<G>(),
- Box::new(move |cx| cx.update_window(window_id, |cx| f(cx)).is_ok()),
+ Box::new(move |cx| cx.update_window(window_handle, |cx| f(cx)).is_ok()),
)
}
@@@ -1171,18 -1239,8 +1237,18 @@@
}
}
+impl<'a, 'w> MainThread<WindowContext<'a, 'w>> {
+ fn platform_window(&self) -> &dyn PlatformWindow {
+ self.window.platform_window.borrow_on_main_thread().as_ref()
+ }
+
+ pub fn activate_window(&self) {
+ self.platform_window().activate();
+ }
+}
+
impl Context for WindowContext<'_, '_> {
- type EntityContext<'a, 'w, T> = ViewContext<'a, 'w, T>;
+ type EntityContext<'a, T> = ModelContext<'a, T>;
type Result<T> = T;
fn entity<T>(
@@@ -1476,9 -1589,9 +1597,9 @@@ impl<'a, 'w, V: 'static> ViewContext<'a
self.app.observers.insert(
handle.entity_id,
Box::new(move |cx| {
- cx.update_window(window_handle.id, |cx| {
+ cx.update_window(window_handle, |cx| {
if let Some(handle) = handle.upgrade() {
- this.update(cx, |this, cx| on_notify(this, handle, cx))
+ view.update(cx, |this, cx| on_notify(this, handle, cx))
.is_ok()
} else {
false
@@@ -1503,10 -1615,10 +1623,10 @@@
self.app.event_listeners.insert(
handle.entity_id,
Box::new(move |event, cx| {
- cx.update_window(window_handle.id, |cx| {
+ cx.update_window(window_handle, |cx| {
if let Some(handle) = handle.upgrade() {
let event = event.downcast_ref().expect("invalid event type");
- this.update(cx, |this, cx| on_event(this, handle, event, cx))
+ view.update(cx, |this, cx| on_event(this, handle, event, cx))
.is_ok()
} else {
false
@@@ -1535,15 -1647,22 +1655,22 @@@
pub fn observe_release<T: 'static>(
&mut self,
handle: &Handle<T>,
- mut on_release: impl FnMut(&mut V, &mut T, &mut ViewContext<'_, '_, V>) + Send + Sync + 'static,
- ) -> Subscription {
- let this = self.handle();
+ mut on_release: impl FnMut(&mut V, &mut T, &mut ViewContext<'_, '_, V>) + Send + 'static,
+ ) -> Subscription
+ where
+ V: Any + Send,
+ {
+ let view = self.view();
let window_handle = self.window.handle;
- self.app.observe_release(handle, move |entity, cx| {
- let _ = cx.update_window(window_handle, |cx| {
- this.update(cx, |this, cx| on_release(this, entity, cx))
- });
- })
+ self.app.release_listeners.insert(
+ handle.entity_id,
+ Box::new(move |entity, cx| {
+ let entity = entity.downcast_mut().expect("invalid entity type");
- let _ = cx.update_window(window_handle.id, |cx| {
++ let _ = cx.update_window(window_handle, |cx| {
+ view.update(cx, |this, cx| on_release(this, entity, cx))
+ });
+ }),
+ )
}
pub fn notify(&mut self) {
@@@ -1695,10 -1814,10 +1822,10 @@@
pub fn observe_global<G: 'static>(
&mut self,
- f: impl Fn(&mut V, &mut ViewContext<'_, '_, V>) + Send + Sync + 'static,
+ f: impl Fn(&mut V, &mut ViewContext<'_, '_, V>) + Send + 'static,
) -> Subscription {
- let window_id = self.window.handle.id;
+ let window_handle = self.window.handle;
- let handle = self.handle();
+ let handle = self.view();
self.global_observers.insert(
TypeId::of::<G>(),
Box::new(move |cx| {