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 callback-handles

Conrad Irwin3e2dba36b404
diff --cc crates/ui2/src/components/context_menu.rs
index 240e6a4508c,2c2d45f4f0d..f0424f0cf71
--- a/crates/ui2/src/components/context_menu.rs
+++ b/crates/ui2/src/components/context_menu.rs
@@@ -1,26 -1,30 +1,26 @@@
use std::cell::RefCell;
use std::rc::Rc;
- use crate::{prelude::*, v_stack, List, ListItem};
- use crate::{ListEntry, ListSeparator, ListSubHeader};
+ use crate::{prelude::*, v_stack, List};
+ use crate::{ListItem, ListSeparator, ListSubHeader};
use gpui::{
- overlay, px, Action, AnchorCorner, AnyElement, AppContext, Bounds, DispatchPhase, Div,
- EventEmitter, FocusHandle, FocusableView, LayoutId, ManagedView, Manager, MouseButton,
- MouseDownEvent, Pixels, Point, Render, RenderOnce, View, VisualContext, WeakView,
+ overlay, px, Action, AnchorCorner, AnyElement, AppContext, Bounds, ClickEvent, DispatchPhase,
+ Div, EventEmitter, FocusHandle, FocusableView, LayoutId, ManagedView, Manager, MouseButton,
+ MouseDownEvent, Pixels, Point, Render, RenderOnce, View, VisualContext,
};
-pub enum ContextMenuItem<V: 'static> {
+pub enum ContextMenuItem {
Separator(ListSeparator),
Header(ListSubHeader),
- Entry(ListEntry, Rc<dyn Fn(&ClickEvent, &mut WindowContext)>),
- Entry(
- ListItem<ContextMenu<V>>,
- Rc<dyn Fn(&mut V, &mut ViewContext<V>)>,
- ),
++ Entry(ListItem, Rc<dyn Fn(&ClickEvent, &mut WindowContext)>),
}
-pub struct ContextMenu<V: 'static> {
- items: Vec<ContextMenuItem<V>>,
+pub struct ContextMenu {
+ items: Vec<ContextMenuItem>,
focus_handle: FocusHandle,
- handle: WeakView<V>,
}
-impl<V: 'static> FocusableView for ContextMenu<V> {
+impl FocusableView for ContextMenu {
fn focus_handle(&self, _cx: &AppContext) -> FocusHandle {
self.focus_handle.clone()
}
@@@ -58,15 -63,15 +58,15 @@@ impl ContextMenu
pub fn entry(
mut self,
- view: ListEntry,
- view: ListItem<Self>,
- on_click: impl Fn(&mut V, &mut ViewContext<V>) + 'static,
++ view: ListItem,
+ on_click: impl Fn(&ClickEvent, &mut WindowContext) + 'static,
) -> Self {
self.items
.push(ContextMenuItem::Entry(view, Rc::new(on_click)));
self
}
- pub fn action(self, view: ListEntry, action: Box<dyn Action>) -> Self {
- pub fn action(self, view: ListItem<Self>, action: Box<dyn Action>) -> Self {
++ pub fn action(self, view: ListItem, action: Box<dyn Action>) -> Self {
// todo: add the keybindings to the list entry
self.entry(view, move |_, cx| cx.dispatch_action(action.boxed_clone()))
}
@@@ -102,26 -105,25 +102,26 @@@ impl Render for ContextMenu
// .bg(cx.theme().colors().elevated_surface_background)
// .border()
// .border_color(cx.theme().colors().border)
- .child(List::new(
- self.items
- .iter()
- .map(|item| match item {
- ContextMenuItem::Separator(separator) => {
- ListItem::Separator(separator.clone())
- }
- ContextMenuItem::Header(header) => ListItem::Header(header.clone()),
- ContextMenuItem::Entry(entry, callback) => {
- let callback = callback.clone();
- let dismiss = cx.listener(|_, _, cx| cx.emit(Manager::Dismiss));
-
- ListItem::Entry(entry.clone().on_click(move |event, cx| {
+ .child(
+ List::new().children(self.items.iter().map(|item| match item {
+ ContextMenuItem::Separator(separator) => {
+ separator.clone().render_into_any()
+ }
+ ContextMenuItem::Header(header) => header.clone().render_into_any(),
+ ContextMenuItem::Entry(entry, callback) => {
+ let callback = callback.clone();
- let handle = self.handle.clone();
++ let dismiss = cx.listener(|_, _, cx| cx.emit(Manager::Dismiss));
++
+ entry
+ .clone()
- .on_click(move |this, cx| {
- handle.update(cx, |view, cx| callback(view, cx)).ok();
- cx.emit(Manager::Dismiss);
++ .on_click(move |event, cx| {
+ callback(event, cx);
+ dismiss(event, cx)
- }))
- }
- })
- .collect(),
- )),
+ })
+ .render_into_any()
+ }
+ })),
+ ),
)
}
}
diff --cc crates/ui2/src/components/list.rs
index 4d558a2334b,fb0c730db8b..b1a8d43bd88
--- a/crates/ui2/src/components/list.rs
+++ b/crates/ui2/src/components/list.rs
@@@ -1,4 -1,5 +1,5 @@@
- use gpui::{div, ClickEvent, Div, RenderOnce, Stateful, StatefulInteractiveElement};
-use gpui::{div, AnyElement, Div, RenderOnce, Stateful, StatefulInteractiveElement};
++use gpui::{div, AnyElement, ClickEvent, Div, RenderOnce, Stateful, StatefulInteractiveElement};
+ use smallvec::SmallVec;
use std::rc::Rc;
use crate::settings::user_settings;
@@@ -117,7 -118,7 +118,7 @@@ impl ListHeader
}
// before_ship!("delete")
-- // fn render<V: 'static>(self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Element<V> {
++ // fn render<V: 'static>(self, cx: &mut WindowContext) -> impl Element<V> {
// let disclosure_control = disclosure_control(self.toggle);
// let meta = match self.meta {
@@@ -197,8 -198,12 +198,12 @@@ impl ListSubHeader
self.left_icon = left_icon;
self
}
+ }
+
-impl<V: 'static> Component<V> for ListSubHeader {
- type Rendered = Div<V>;
++impl Component for ListSubHeader {
++ type Rendered = Div;
- fn render(self, cx: &mut WindowContext) -> impl Element {
- fn render(self, view: &mut V, cx: &mut ViewContext<V>) -> Self::Rendered {
++ fn render(self, cx: &mut WindowContext) -> Self::Rendered {
h_stack().flex_1().w_full().relative().py_1().child(
div()
.h_6()
@@@ -232,55 -237,9 +237,9 @@@ pub enum ListEntrySize
Medium,
}
- #[derive(Clone)]
- pub enum ListItem {
- Entry(ListEntry),
- Separator(ListSeparator),
- Header(ListSubHeader),
- }
-
- impl From<ListEntry> for ListItem {
- fn from(entry: ListEntry) -> Self {
- Self::Entry(entry)
- }
- }
-
- impl From<ListSeparator> for ListItem {
- fn from(entry: ListSeparator) -> Self {
- Self::Separator(entry)
- }
- }
-
- impl From<ListSubHeader> for ListItem {
- fn from(entry: ListSubHeader) -> Self {
- Self::Header(entry)
- }
- }
-
- impl ListItem {
- pub fn new(label: Label) -> Self {
- Self::Entry(ListEntry::new(label))
- }
-
- pub fn as_entry(&mut self) -> Option<&mut ListEntry> {
- if let Self::Entry(entry) = self {
- Some(entry)
- } else {
- None
- }
- }
-
- fn render(self, ix: usize, cx: &mut WindowContext) -> Div {
- match self {
- ListItem::Entry(entry) => div().child(entry.render(ix, cx)),
- ListItem::Separator(separator) => div().child(separator.render(cx)),
- ListItem::Header(header) => div().child(header.render(cx)),
- }
- }
- }
-
- // #[derive(RenderOnce)]
- pub struct ListEntry {
+ #[derive(RenderOnce)]
-pub struct ListItem<V: 'static> {
++pub struct ListItem {
+ id: ElementId,
disabled: bool,
// TODO: Reintroduce this
// disclosure_control_style: DisclosureControlVisibility,
@@@ -291,12 -250,13 +250,13 @@@
size: ListEntrySize,
toggle: Toggle,
variant: ListItemVariant,
- on_click: Option<Rc<dyn Fn(&mut V, &mut ViewContext<V>) + 'static>>,
+ on_click: Option<Rc<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
}
- impl Clone for ListEntry {
-impl<V> Clone for ListItem<V> {
++impl Clone for ListItem {
fn clone(&self) -> Self {
Self {
+ id: self.id.clone(),
disabled: self.disabled,
indent_level: self.indent_level,
label: self.label.clone(),
@@@ -310,9 -270,10 +270,10 @@@
}
}
- impl ListEntry {
- pub fn new(label: Label) -> Self {
-impl<V: 'static> ListItem<V> {
++impl ListItem {
+ pub fn new(id: impl Into<ElementId>, label: Label) -> Self {
Self {
+ id: id.into(),
disabled: false,
indent_level: 0,
label,
@@@ -364,8 -325,12 +325,12 @@@
self.size = size;
self
}
+ }
- fn render(self, ix: usize, cx: &mut WindowContext) -> Stateful<Div> {
-impl<V: 'static> Component<V> for ListItem<V> {
- type Rendered = Stateful<V, Div<V>>;
++impl Component for ListItem {
++ type Rendered = Stateful<Div>;
+
- fn render(self, view: &mut V, cx: &mut ViewContext<V>) -> Self::Rendered {
++ fn render(self, cx: &mut WindowContext) -> Self::Rendered {
let settings = user_settings(cx);
let left_content = match self.left_slot.clone() {
@@@ -392,11 -357,13 +357,12 @@@
style.background = Some(cx.theme().colors().editor_background.into());
style
})
- .map(|div| {
- if let Some(on_click) = self.on_click.clone() {
- div.on_click(move |e, cx| on_click(e, cx))
- } else {
- div
+ .on_click({
+ let on_click = self.on_click.clone();
-
- move |view: &mut V, event, cx| {
++ move |event, cx| {
+ if let Some(on_click) = &on_click {
- (on_click)(view, cx)
++ (on_click)(event, cx)
+ }
}
})
.bg(cx.theme().colors().surface_background)
@@@ -451,26 -418,21 +417,21 @@@ impl Component for ListSeparator
}
#[derive(RenderOnce)]
-pub struct List<V: 'static> {
+pub struct List {
- items: Vec<ListItem>,
/// Message to display when the list is empty
/// Defaults to "No items"
empty_message: SharedString,
header: Option<ListHeader>,
toggle: Toggle,
- children: SmallVec<[AnyElement<V>; 2]>,
++ children: SmallVec<[AnyElement; 2]>,
}
-impl<V: 'static> Component<V> for List<V> {
- type Rendered = Div<V>;
+impl Component for List {
+ type Rendered = Div;
- fn render(self, view: &mut V, cx: &mut ViewContext<V>) -> Self::Rendered {
+ fn render(self, cx: &mut WindowContext) -> Self::Rendered {
- let list_content = match (self.items.is_empty(), self.toggle) {
- (false, _) => div().children(
- self.items
- .into_iter()
- .enumerate()
- .map(|(ix, item)| item.render(ix, cx)),
- ),
+ let list_content = match (self.children.is_empty(), self.toggle) {
+ (false, _) => div().children(self.children),
(true, Toggle::Toggled(false)) => div(),
(true, _) => {
div().child(Label::new(self.empty_message.clone()).color(TextColor::Muted))
@@@ -485,10 -447,9 +446,9 @@@
}
}
-impl<V: 'static> List<V> {
+impl List {
- pub fn new(items: Vec<ListItem>) -> Self {
+ pub fn new() -> Self {
Self {
- items,
empty_message: "No items".into(),
header: None,
toggle: Toggle::NotToggleable,
@@@ -509,25 -471,10 +470,10 @@@
self.toggle = toggle;
self
}
+ }
- fn render(self, cx: &mut WindowContext) -> impl Element {
- let list_content = match (self.items.is_empty(), self.toggle) {
- (false, _) => div().children(
- self.items
- .into_iter()
- .enumerate()
- .map(|(ix, item)| item.render(ix, cx)),
- ),
- (true, Toggle::Toggled(false)) => div(),
- (true, _) => {
- div().child(Label::new(self.empty_message.clone()).color(TextColor::Muted))
- }
- };
-
- v_stack()
- .w_full()
- .py_1()
- .children(self.header.map(|header| header))
- .child(list_content)
-impl<V: 'static> ParentElement<V> for List<V> {
- fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
++impl ParentElement for List {
++ fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> {
+ &mut self.children
}
}
diff --cc crates/ui2/src/to_extract/static_data.rs
index 684962651ec,266889a7961..9d752d3fcae
--- a/crates/ui2/src/to_extract/static_data.rs
+++ b/crates/ui2/src/to_extract/static_data.rs
@@@ -478,9 -477,9 +477,9 @@@ pub fn static_new_notification_items_2(
]
}
-pub fn static_project_panel_project_items<V>() -> Vec<ListItem<V>> {
+pub fn static_project_panel_project_items() -> Vec<ListItem> {
vec![
- ListEntry::new(Label::new("zed"))
+ ListItem::new("zed", Label::new("zed"))
.left_icon(Icon::FolderOpen.into())
.indent_level(0)
.toggle(Toggle::Toggled(true)),
@@@ -600,44 -620,36 +620,36 @@@
.left_icon(Icon::FileDoc.into())
.indent_level(1),
]
- .into_iter()
- .map(From::from)
- .collect()
}
-pub fn static_project_panel_single_items<V>() -> Vec<ListItem<V>> {
+pub fn static_project_panel_single_items() -> Vec<ListItem> {
vec![
- ListEntry::new(Label::new("todo.md"))
+ ListItem::new("todo.md", Label::new("todo.md"))
.left_icon(Icon::FileDoc.into())
.indent_level(0),
- ListEntry::new(Label::new("README.md"))
+ ListItem::new("README.md", Label::new("README.md"))
.left_icon(Icon::FileDoc.into())
.indent_level(0),
- ListEntry::new(Label::new("config.json"))
+ ListItem::new("config.json", Label::new("config.json"))
.left_icon(Icon::FileGeneric.into())
.indent_level(0),
]
- .into_iter()
- .map(From::from)
- .collect()
}
-pub fn static_collab_panel_current_call<V>() -> Vec<ListItem<V>> {
+pub fn static_collab_panel_current_call() -> Vec<ListItem> {
vec![
- ListEntry::new(Label::new("as-cii")).left_avatar("http://github.com/as-cii.png?s=50"),
- ListEntry::new(Label::new("nathansobo"))
+ ListItem::new("as-cii", Label::new("as-cii"))
+ .left_avatar("http://github.com/as-cii.png?s=50"),
+ ListItem::new("nathansobo", Label::new("nathansobo"))
.left_avatar("http://github.com/nathansobo.png?s=50"),
- ListEntry::new(Label::new("maxbrunsfeld"))
+ ListItem::new("maxbrunsfeld", Label::new("maxbrunsfeld"))
.left_avatar("http://github.com/maxbrunsfeld.png?s=50"),
]
- .into_iter()
- .map(From::from)
- .collect()
}
-pub fn static_collab_panel_channels<V>() -> Vec<ListItem<V>> {
+pub fn static_collab_panel_channels<V>() -> Vec<ListItem> {
vec![
- ListEntry::new(Label::new("zed"))
+ ListItem::new("zed", Label::new("zed"))
.left_icon(Icon::Hash.into())
.size(ListEntrySize::Medium)
.indent_level(0),