Merge branch 'main' into rename-element-traits

# Conflicts:
#	crates/gpui2/src/elements/uniform_list.rs
#	crates/ui2/src/components/context_menu.rs
#	crates/ui2/src/components/list.rs
This commit is contained in:
Antonio Scandurra
2023-11-23 12:47:46 +01:00
34 changed files with 592 additions and 326 deletions
+17 -16
View File
@@ -1,7 +1,7 @@
use std::cell::RefCell;
use std::rc::Rc;
use crate::{prelude::*, v_stack, List};
use crate::{prelude::*, v_stack, Label, List};
use crate::{ListItem, ListSeparator, ListSubHeader};
use gpui::{
overlay, px, Action, AnchorCorner, AnyElement, AppContext, Bounds, ClickEvent, DispatchPhase,
@@ -10,9 +10,9 @@ use gpui::{
};
pub enum ContextMenuItem {
Separator(ListSeparator),
Header(ListSubHeader),
Entry(ListItem, Rc<dyn Fn(&ClickEvent, &mut WindowContext)>),
Separator,
Header(SharedString),
Entry(SharedString, Rc<dyn Fn(&ClickEvent, &mut WindowContext)>),
}
pub struct ContextMenu {
@@ -46,29 +46,30 @@ impl ContextMenu {
}
pub fn header(mut self, title: impl Into<SharedString>) -> Self {
self.items
.push(ContextMenuItem::Header(ListSubHeader::new(title)));
self.items.push(ContextMenuItem::Header(title.into()));
self
}
pub fn separator(mut self) -> Self {
self.items.push(ContextMenuItem::Separator(ListSeparator));
self.items.push(ContextMenuItem::Separator);
self
}
pub fn entry(
mut self,
view: ListItem,
label: impl Into<SharedString>,
on_click: impl Fn(&ClickEvent, &mut WindowContext) + 'static,
) -> Self {
self.items
.push(ContextMenuItem::Entry(view, Rc::new(on_click)));
.push(ContextMenuItem::Entry(label.into(), Rc::new(on_click)));
self
}
pub fn action(self, view: ListItem, action: Box<dyn Action>) -> Self {
pub fn action(self, label: impl Into<SharedString>, action: Box<dyn Action>) -> Self {
// todo: add the keybindings to the list entry
self.entry(view, move |_, cx| cx.dispatch_action(action.boxed_clone()))
self.entry(label.into(), move |_, cx| {
cx.dispatch_action(action.boxed_clone())
})
}
pub fn confirm(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
@@ -104,16 +105,16 @@ impl Render for ContextMenu {
// .border_color(cx.theme().colors().border)
.child(
List::new().children(self.items.iter().map(|item| match item {
ContextMenuItem::Separator(separator) => {
separator.clone().into_any_element()
ContextMenuItem::Separator => ListSeparator::new().into_any_element(),
ContextMenuItem::Header(header) => {
ListSubHeader::new(header.clone()).into_any_element()
}
ContextMenuItem::Header(header) => header.clone().into_any_element(),
ContextMenuItem::Entry(entry, callback) => {
let callback = callback.clone();
let dismiss = cx.listener(|_, _, cx| cx.emit(Manager::Dismiss));
entry
.clone()
ListItem::new(entry.clone())
.child(Label::new(entry.clone()))
.on_click(move |event, cx| {
callback(event, cx);
dismiss(event, cx)
+10 -21
View File
@@ -245,45 +245,28 @@ pub struct ListItem {
// TODO: Reintroduce this
// disclosure_control_style: DisclosureControlVisibility,
indent_level: u32,
label: Label,
left_slot: Option<GraphicSlot>,
overflow: OverflowStyle,
size: ListEntrySize,
toggle: Toggle,
variant: ListItemVariant,
on_click: Option<Rc<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
}
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(),
left_slot: self.left_slot.clone(),
overflow: self.overflow,
size: self.size,
toggle: self.toggle,
variant: self.variant,
on_click: self.on_click.clone(),
}
}
children: SmallVec<[AnyElement; 2]>,
}
impl ListItem {
pub fn new(id: impl Into<ElementId>, label: Label) -> Self {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
disabled: false,
indent_level: 0,
label,
left_slot: None,
overflow: OverflowStyle::Hidden,
size: ListEntrySize::default(),
toggle: Toggle::NotToggleable,
variant: ListItemVariant::default(),
on_click: Default::default(),
children: SmallVec::new(),
}
}
@@ -394,11 +377,17 @@ impl RenderOnce for ListItem {
.relative()
.child(disclosure_control(self.toggle))
.children(left_content)
.child(self.label),
.children(self.children),
)
}
}
impl ParentElement for ListItem {
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> {
&mut self.children
}
}
#[derive(IntoElement, Clone)]
pub struct ListSeparator;
@@ -2,7 +2,7 @@ use gpui::{actions, Action, AnchorCorner, Div, Render, View};
use story::Story;
use crate::prelude::*;
use crate::{menu_handle, ContextMenu, Label, ListItem};
use crate::{menu_handle, ContextMenu, Label};
actions!(PrintCurrentDate, PrintBestFood);
@@ -10,17 +10,13 @@ fn build_menu(cx: &mut WindowContext, header: impl Into<SharedString>) -> View<C
ContextMenu::build(cx, |menu, _| {
menu.header(header)
.separator()
.entry(
ListItem::new("Print current time", Label::new("Print current time")),
|v, cx| {
println!("dispatching PrintCurrentTime action");
cx.dispatch_action(PrintCurrentDate.boxed_clone())
},
)
.entry(
ListItem::new("Print best food", Label::new("Print best food")),
|v, cx| cx.dispatch_action(PrintBestFood.boxed_clone()),
)
.entry("Print current time", |v, cx| {
println!("dispatching PrintCurrentTime action");
cx.dispatch_action(PrintCurrentDate.boxed_clone())
})
.entry("Print best foot", |v, cx| {
cx.dispatch_action(PrintBestFood.boxed_clone())
})
})
}