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 'main' into divs

Nathan Sobod375f7992d4c
diff --cc Cargo.lock
index 84fd279fa73,3edf9acab39..d6490337dae
--- a/Cargo.lock
+++ b/Cargo.lock
@@@ -3183,8 -3090,6 +3101,7 @@@ dependencies =
name = "gpui_macros"
version = "0.1.0"
dependencies = [
- "gpui",
+ "lazy_static",
"proc-macro2",
"quote",
"syn 1.0.109",
diff --cc crates/drag_and_drop/src/drag_and_drop.rs
index 28b876504c5,59b0bc89e2f..197c9918f54
--- a/crates/drag_and_drop/src/drag_and_drop.rs
+++ b/crates/drag_and_drop/src/drag_and_drop.rs
@@@ -301,7 -301,7 +301,7 @@@ pub trait Draggable<V>
Self: Sized;
}
- impl<Tag, V: 'static> Draggable<V> for MouseEventHandler<Tag, V> {
-impl<V: View> Draggable<V> for MouseEventHandler<V> {
++impl<V: 'static> Draggable<V> for MouseEventHandler<V> {
fn as_draggable<D: View, P: Any>(
self,
payload: P,
diff --cc crates/gpui/src/app.rs
index d6a5367e409,ca5c2fb8b50..076a3fdde32
--- a/crates/gpui/src/app.rs
+++ b/crates/gpui/src/app.rs
@@@ -3324,22 -3341,53 +3345,68 @@@ impl<'a, 'b, V: 'static> ViewContext<'a
self.element_state::<Tag, T>(element_id, T::default())
}
+ pub fn rem_pixels(&self) -> f32 {
+ 16.
+ }
++
+ pub fn default_element_state_dynamic<T: 'static + Default>(
+ &mut self,
+ tag: TypeTag,
+ element_id: usize,
+ ) -> ElementStateHandle<T> {
+ self.element_state_dynamic::<T>(tag, element_id, T::default())
+ }
}
+impl<V: View> ViewContext<'_, '_, V> {
- pub fn emit(&mut self, payload: V::Event) {
++ pub fn emit(&mut self, event: V::Event) {
+ self.window_context
+ .pending_effects
+ .push_back(Effect::Event {
+ entity_id: self.view_id,
- payload: Box::new(payload),
++ payload: Box::new(event),
+ });
+ }
+}
+
+ #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
+ pub struct TypeTag {
+ tag: TypeId,
+ composed: Option<TypeId>,
+ #[cfg(debug_assertions)]
+ tag_type_name: &'static str,
+ }
+
+ impl TypeTag {
+ pub fn new<Tag: 'static>() -> Self {
+ Self {
+ tag: TypeId::of::<Tag>(),
+ composed: None,
+ #[cfg(debug_assertions)]
+ tag_type_name: std::any::type_name::<Tag>(),
+ }
+ }
+
+ pub fn dynamic(tag: TypeId, #[cfg(debug_assertions)] type_name: &'static str) -> Self {
+ Self {
+ tag,
+ composed: None,
+ #[cfg(debug_assertions)]
+ tag_type_name: type_name,
+ }
+ }
+
+ pub fn compose(mut self, other: TypeTag) -> Self {
+ self.composed = Some(other.tag);
+ self
+ }
+
+ #[cfg(debug_assertions)]
+ pub(crate) fn type_name(&self) -> &'static str {
+ self.tag_type_name
+ }
+ }
+
impl<V> BorrowAppContext for ViewContext<'_, '_, V> {
fn read_with<T, F: FnOnce(&AppContext) -> T>(&self, f: F) -> T {
BorrowAppContext::read_with(&*self.window_context, f)
diff --cc crates/gpui/src/app/window.rs
index 5b815acc899,12a9adafcba..5638699565f
--- a/crates/gpui/src/app/window.rs
+++ b/crates/gpui/src/app/window.rs
@@@ -507,8 -475,6 +507,8 @@@ impl<'a> WindowContext<'a>
}
pub(crate) fn dispatch_event(&mut self, event: Event, event_reused: bool) -> bool {
- self.dispatch_to_interactive_regions(&event);
++ self.dispatch_to_new_event_handlers(&event);
+
let mut mouse_events = SmallVec::<[_; 2]>::new();
let mut notified_views: HashSet<usize> = Default::default();
let handle = self.window_handle;
@@@ -886,19 -852,6 +886,18 @@@
any_event_handled
}
- fn dispatch_to_interactive_regions(&mut self, event: &Event) {
++ fn dispatch_to_new_event_handlers(&mut self, event: &Event) {
+ if let Some(mouse_event) = event.mouse_event() {
- let mouse_position = event.position().expect("mouse events must have a position");
+ let event_handlers = self.window.take_event_handlers();
+ for event_handler in event_handlers.iter().rev() {
+ if event_handler.event_type == mouse_event.type_id() {
+ (event_handler.handler)(mouse_event, self);
+ }
+ }
+ self.window.event_handlers = event_handlers;
+ }
+ }
+
pub(crate) fn dispatch_key_down(&mut self, event: &KeyDownEvent) -> bool {
let handle = self.window_handle;
if let Some(focused_view_id) = self.window.focused_view_id {
diff --cc crates/gpui/src/elements.rs
index 025bb7cb698,03caae8dd9b..bde15068ef0
--- a/crates/gpui/src/elements.rs
+++ b/crates/gpui/src/elements.rs
@@@ -33,7 -34,7 +34,7 @@@ use crate::
rect::RectF,
vector::{vec2f, Vector2F},
},
- json, Action, Entity, LayoutContext, PaintContext, SceneBuilder, SizeConstraint, View,
- json, Action, LayoutContext, PaintContext, SceneBuilder, SizeConstraint, TypeTag, View,
++ json, Action, Entity, LayoutContext, PaintContext, SceneBuilder, SizeConstraint, TypeTag, View,
ViewContext, WeakViewHandle, WindowContext,
};
use anyhow::{anyhow, Result};
@@@ -41,12 -42,16 +42,21 @@@ use collections::HashMap
use core::panic;
use json::ToJson;
use smallvec::SmallVec;
--use std::{any::Any, borrow::Cow, mem, ops::Range};
++use std::{
++ any::{type_name, Any},
++ borrow::Cow,
++ mem,
++ ops::Range,
++};
-pub trait Element<V: View>: 'static {
+pub trait Element<V: 'static>: 'static {
type LayoutState;
type PaintState;
+ fn view_name(&self) -> &'static str {
- V::ui_name()
++ type_name::<V>()
+ }
+
fn layout(
&mut self,
constraint: SizeConstraint,
diff --cc crates/gpui/src/elements/container.rs
index 52954db78ee,bb1366b4e76..03c2f1d0464
--- a/crates/gpui/src/elements/container.rs
+++ b/crates/gpui/src/elements/container.rs
@@@ -37,7 -38,16 +37,16 @@@ pub struct ContainerStyle
pub cursor: Option<CursorStyle>,
}
+ impl ContainerStyle {
+ pub fn fill(color: Color) -> Self {
+ Self {
+ background_color: Some(color),
+ ..Default::default()
+ }
+ }
+ }
+
-pub struct Container<V: View> {
+pub struct Container<V> {
child: AnyElement<V>,
style: ContainerStyle,
}
diff --cc crates/gpui/src/elements/mouse_event_handler.rs
index be773fd4b13,2ed0f1720f9..b1140a69604
--- a/crates/gpui/src/elements/mouse_event_handler.rs
+++ b/crates/gpui/src/elements/mouse_event_handler.rs
@@@ -11,12 -11,12 +11,12 @@@ use crate::
MouseHover, MouseMove, MouseMoveOut, MouseScrollWheel, MouseUp, MouseUpOut,
},
AnyElement, Element, EventContext, LayoutContext, MouseRegion, MouseState, PaintContext,
- SceneBuilder, SizeConstraint, ViewContext,
- SceneBuilder, SizeConstraint, TypeTag, View, ViewContext,
++ SceneBuilder, SizeConstraint, TypeTag, ViewContext,
};
use serde_json::json;
- use std::{marker::PhantomData, ops::Range};
+ use std::ops::Range;
- pub struct MouseEventHandler<Tag: 'static, V> {
-pub struct MouseEventHandler<V: View> {
++pub struct MouseEventHandler<V: 'static> {
child: AnyElement<V>,
region_id: usize,
cursor_style: Option<CursorStyle>,
@@@ -31,8 -31,8 +31,8 @@@
/// Element which provides a render_child callback with a MouseState and paints a mouse
/// region under (or above) it for easy mouse event handling.
- impl<Tag, V: 'static> MouseEventHandler<Tag, V> {
- pub fn for_child(child: impl Element<V>, region_id: usize) -> Self {
-impl<V: View> MouseEventHandler<V> {
++impl<V: 'static> MouseEventHandler<V> {
+ pub fn for_child<Tag: 'static>(child: impl Element<V>, region_id: usize) -> Self {
Self {
child: child.into_any(),
region_id,
@@@ -236,7 -267,7 +267,7 @@@
}
}
- impl<Tag, V: 'static> Element<V> for MouseEventHandler<Tag, V> {
-impl<V: View> Element<V> for MouseEventHandler<V> {
++impl<V: 'static> Element<V> for MouseEventHandler<V> {
type LayoutState = ();
type PaintState = ();
diff --cc crates/gpui/src/elements/resizable.rs
index 00b0bb1e69d,37e40d6584d..3c2febcce34
--- a/crates/gpui/src/elements/resizable.rs
+++ b/crates/gpui/src/elements/resizable.rs
@@@ -69,17 -52,25 +52,25 @@@ impl HandleSide
}
}
- pub struct Resizable<V> {
+ fn get_bounds(tag: TypeTag, cx: &AppContext) -> Option<&(RectF, RectF)>
+ where
+ {
+ cx.optional_global::<ProviderMap>()
+ .and_then(|map| map.0.get(&tag))
+ }
+
-pub struct Resizable<V: View> {
++pub struct Resizable<V: 'static> {
child: AnyElement<V>,
+ tag: TypeTag,
handle_side: HandleSide,
handle_size: f32,
- on_resize: Rc<RefCell<dyn FnMut(&mut V, f32, &mut ViewContext<V>)>>,
+ on_resize: Rc<RefCell<dyn FnMut(&mut V, Option<f32>, &mut ViewContext<V>)>>,
}
const DEFAULT_HANDLE_SIZE: f32 = 4.0;
-impl<V: View> Resizable<V> {
+impl<V: 'static> Resizable<V> {
- pub fn new(
+ pub fn new<Tag: 'static>(
child: AnyElement<V>,
handle_side: HandleSide,
size: f32,
@@@ -201,3 -215,80 +215,80 @@@ impl<V: 'static> Element<V> for Resizab
})
}
}
+
+ #[derive(Debug, Default)]
+ struct ProviderMap(HashMap<TypeTag, (RectF, RectF)>);
+
-pub struct BoundsProvider<V: View, P> {
++pub struct BoundsProvider<V: 'static, P> {
+ child: AnyElement<V>,
+ phantom: std::marker::PhantomData<P>,
+ }
+
-impl<V: View, P: 'static> BoundsProvider<V, P> {
++impl<V: 'static, P: 'static> BoundsProvider<V, P> {
+ pub fn new(child: AnyElement<V>) -> Self {
+ Self {
+ child,
+ phantom: std::marker::PhantomData,
+ }
+ }
+ }
+
+ impl<V: View, P: 'static> Element<V> for BoundsProvider<V, P> {
+ type LayoutState = ();
+
+ type PaintState = ();
+
+ fn layout(
+ &mut self,
+ constraint: crate::SizeConstraint,
+ view: &mut V,
+ cx: &mut crate::LayoutContext<V>,
+ ) -> (pathfinder_geometry::vector::Vector2F, Self::LayoutState) {
+ (self.child.layout(constraint, view, cx), ())
+ }
+
+ fn paint(
+ &mut self,
+ scene: &mut crate::SceneBuilder,
+ bounds: pathfinder_geometry::rect::RectF,
+ visible_bounds: pathfinder_geometry::rect::RectF,
+ _: &mut Self::LayoutState,
+ view: &mut V,
+ cx: &mut crate::PaintContext<V>,
+ ) -> Self::PaintState {
+ cx.update_default_global::<ProviderMap, _, _>(|map, _| {
+ map.0.insert(TypeTag::new::<P>(), (bounds, visible_bounds));
+ });
+
+ self.child
+ .paint(scene, bounds.origin(), visible_bounds, view, cx)
+ }
+
+ fn rect_for_text_range(
+ &self,
+ range_utf16: std::ops::Range<usize>,
+ _: pathfinder_geometry::rect::RectF,
+ _: pathfinder_geometry::rect::RectF,
+ _: &Self::LayoutState,
+ _: &Self::PaintState,
+ view: &V,
+ cx: &crate::ViewContext<V>,
+ ) -> Option<pathfinder_geometry::rect::RectF> {
+ self.child.rect_for_text_range(range_utf16, view, cx)
+ }
+
+ fn debug(
+ &self,
+ _: pathfinder_geometry::rect::RectF,
+ _: &Self::LayoutState,
+ _: &Self::PaintState,
+ view: &V,
+ cx: &crate::ViewContext<V>,
+ ) -> serde_json::Value {
+ serde_json::json!({
+ "type": "Provider",
+ "providing": format!("{:?}", TypeTag::new::<P>()),
+ "child": self.child.debug(view, cx),
+ })
+ }
+ }
diff --cc crates/gpui/src/elements/tooltip.rs
index 58efb9b5e37,0ce34fcc143..d3a1db713e6
--- a/crates/gpui/src/elements/tooltip.rs
+++ b/crates/gpui/src/elements/tooltip.rs
@@@ -7,7 -7,7 +7,7 @@@ use crate::
geometry::{rect::RectF, vector::Vector2F},
json::json,
Action, Axis, ElementStateHandle, LayoutContext, PaintContext, SceneBuilder, SizeConstraint,
- Task, ViewContext,
- Task, TypeTag, View, ViewContext,
++ Task, TypeTag, ViewContext,
};
use schemars::JsonSchema;
use serde::Deserialize;
diff --cc crates/gpui/src/platform.rs
index 4dda941fd59,c994b655736..824e6b61e53
--- a/crates/gpui/src/platform.rs
+++ b/crates/gpui/src/platform.rs
@@@ -180,7 -182,17 +182,17 @@@ pub struct WindowOptions<'a>
pub screen: Option<Rc<dyn Screen>>,
}
+ impl<'a> WindowOptions<'a> {
+ pub fn with_bounds(bounds: Vector2F) -> Self {
+ Self {
+ bounds: WindowBounds::Fixed(RectF::new(vec2f(0., 0.), bounds)),
+ center: true,
+ ..Default::default()
+ }
+ }
+ }
+
-#[derive(Debug)]
+#[derive(Debug, Default)]
pub struct TitlebarOptions<'a> {
pub title: Option<&'a str>,
pub appears_transparent: bool,
diff --cc crates/gpui/src/scene/mouse_region.rs
index a0277a9432b,1632b494a35..d19ffdcb871
--- a/crates/gpui/src/scene/mouse_region.rs
+++ b/crates/gpui/src/scene/mouse_region.rs
@@@ -1,4 -1,6 +1,4 @@@
- use crate::{platform::MouseButton, window::WindowContext, EventContext, ViewContext};
-use crate::{
- platform::MouseButton, window::WindowContext, EventContext, TypeTag, View, ViewContext,
-};
++use crate::{platform::MouseButton, window::WindowContext, EventContext, TypeTag, ViewContext};
use collections::HashMap;
use pathfinder_geometry::rect::RectF;
use smallvec::SmallVec;
diff --cc crates/gpui_macros/Cargo.toml
index 84b0ac55c5e,9d1d232c95d..c11a4af33df
--- a/crates/gpui_macros/Cargo.toml
+++ b/crates/gpui_macros/Cargo.toml
@@@ -10,10 -10,6 +10,7 @@@ proc-macro = tru
doctest = false
[dependencies]
+lazy_static.workspace = true
+proc-macro2 = "1.0"
syn = "1.0"
quote = "1.0"
-
- [dev-dependencies]
- gpui = { path = "../gpui" }
-proc-macro2 = "1.0"
diff --cc crates/theme/src/ui.rs
index bd9af52d0e9,7f0b05731eb..bb67aaebd5b
--- a/crates/theme/src/ui.rs
+++ b/crates/theme/src/ui.rs
@@@ -34,10 -34,10 +34,10 @@@ pub fn checkbox<Tag, V, F>
id: usize,
cx: &mut ViewContext<V>,
change: F,
- ) -> MouseEventHandler<Tag, V>
+ ) -> MouseEventHandler<V>
where
Tag: 'static,
- V: View,
+ V: 'static,
F: 'static + Fn(&mut V, bool, &mut EventContext<V>),
{
let label = Label::new(label, style.label.text.clone())
@@@ -57,10 -57,10 +57,10 @@@ pub fn checkbox_with_label<Tag, D, V, F
where
Tag: 'static,
D: Element<V>,
- V: View,
+ V: 'static,
F: 'static + Fn(&mut V, bool, &mut EventContext<V>),
{
- MouseEventHandler::new(id, cx, |state, _| {
+ MouseEventHandler::new::<Tag, _>(id, cx, |state, _| {
let indicator = if checked {
svg(&style.icon)
} else {
@@@ -107,7 -107,17 +107,17 @@@ pub struct IconStyle
pub container: ContainerStyle,
}
+ impl IconStyle {
+ pub fn width(&self) -> f32 {
+ self.icon.dimensions.width
+ + self.container.padding.left
+ + self.container.padding.right
+ + self.container.margin.left
+ + self.container.margin.right
+ }
+ }
+
-pub fn icon<V: View>(style: &IconStyle) -> Container<V> {
+pub fn icon<V: 'static>(style: &IconStyle) -> Container<V> {
svg(&style.icon).contained().with_style(style.container)
}
@@@ -147,10 -157,10 +157,10 @@@ pub fn cta_button<Tag, L, V, F>
where
Tag: 'static,
L: Into<Cow<'static, str>>,
- V: View,
+ V: 'static,
F: Fn(MouseClick, &mut V, &mut EventContext<V>) + 'static,
{
- MouseEventHandler::<Tag, V>::new(0, cx, |state, _| {
+ MouseEventHandler::new::<Tag, _>(0, cx, |state, _| {
let style = style.style_for(state);
Label::new(label, style.text.to_owned())
.aligned()