Rename Drawable::boxed to into_element and make containers generic

Multi-element are now generic over any drawable child, which can be converted
into an element.

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
This commit is contained in:
Antonio Scandurra 2023-04-21 18:36:21 +02:00 committed by Nathan Sobo
parent 4d433663bd
commit 03619dfa55
80 changed files with 1132 additions and 1434 deletions

View file

@ -144,21 +144,20 @@ impl View for ContextMenu {
fn render(&mut self, cx: &mut ViewContext<Self>) -> Element<Self> {
if !self.visible {
return Empty::new().boxed();
return Empty::new().into_element();
}
// Render the menu once at minimum width.
let mut collapsed_menu = self.render_menu_for_measurement(cx).boxed();
let expanded_menu = self
.render_menu(cx)
.constrained()
.dynamically(move |constraint, view, cx| {
SizeConstraint::strict_along(
Axis::Horizontal,
collapsed_menu.layout(constraint, view, cx).x(),
)
})
.boxed();
let mut collapsed_menu = self.render_menu_for_measurement(cx);
let expanded_menu =
self.render_menu(cx)
.constrained()
.dynamically(move |constraint, view, cx| {
SizeConstraint::strict_along(
Axis::Horizontal,
collapsed_menu.layout(constraint, view, cx).0.x(),
)
});
Overlay::new(expanded_menu)
.with_hoverable(true)
@ -166,7 +165,7 @@ impl View for ContextMenu {
.with_anchor_position(self.anchor_position)
.with_anchor_corner(self.anchor_corner)
.with_position_mode(self.position_mode)
.boxed()
.into_element()
}
fn focus_out(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
@ -335,40 +334,38 @@ impl ContextMenu {
let style = cx.global::<Settings>().theme.context_menu.clone();
Flex::row()
.with_child(
Flex::column()
.with_children(self.items.iter().enumerate().map(|(ix, item)| {
match item {
ContextMenuItem::Item { label, .. } => {
let style = style.item.style_for(
&mut Default::default(),
Some(ix) == self.selected_index,
);
Flex::column().with_children(self.items.iter().enumerate().map(|(ix, item)| {
match item {
ContextMenuItem::Item { label, .. } => {
let style = style.item.style_for(
&mut Default::default(),
Some(ix) == self.selected_index,
);
match label {
ContextMenuItemLabel::String(label) => {
Label::new(label.to_string(), style.label.clone())
.contained()
.with_style(style.container)
.boxed()
}
ContextMenuItemLabel::Element(element) => {
element(&mut Default::default(), style)
}
match label {
ContextMenuItemLabel::String(label) => {
Label::new(label.to_string(), style.label.clone())
.contained()
.with_style(style.container)
.into_element()
}
ContextMenuItemLabel::Element(element) => {
element(&mut Default::default(), style)
}
}
ContextMenuItem::Static(f) => f(cx),
ContextMenuItem::Separator => Empty::new()
.collapsed()
.contained()
.with_style(style.separator)
.constrained()
.with_height(1.)
.boxed(),
}
}))
.boxed(),
ContextMenuItem::Static(f) => f(cx),
ContextMenuItem::Separator => Empty::new()
.collapsed()
.contained()
.with_style(style.separator)
.constrained()
.with_height(1.)
.into_element(),
}
})),
)
.with_child(
Flex::column()
@ -394,10 +391,10 @@ impl ContextMenu {
style.keystroke.container,
style.keystroke.text.clone(),
)
.boxed()
.into_element()
}
ContextMenuItem::Static(_) => Empty::new().boxed(),
ContextMenuItem::Static(_) => Empty::new().into_element(),
ContextMenuItem::Separator => Empty::new()
.collapsed()
@ -405,12 +402,11 @@ impl ContextMenu {
.with_height(1.)
.contained()
.with_style(style.separator)
.boxed(),
.into_element(),
}
}))
.contained()
.with_margin_left(style.keystroke_margin)
.boxed(),
.with_margin_left(style.keystroke_margin),
)
.contained()
.with_style(style.container)
@ -445,7 +441,7 @@ impl ContextMenu {
ContextMenuItemLabel::String(label) => {
Label::new(label.clone(), style.label.clone())
.contained()
.boxed()
.into_element()
}
ContextMenuItemLabel::Element(element) => {
element(state, style)
@ -459,11 +455,9 @@ impl ContextMenu {
style.keystroke.text.clone(),
)
.flex_float()
.boxed()
})
.contained()
.with_style(style.container)
.boxed()
})
.with_cursor_style(CursorStyle::PointingHand)
.on_up(MouseButton::Left, |_, _, _| {}) // Capture these events
@ -474,7 +468,7 @@ impl ContextMenu {
cx.dispatch_any_action_at(window_id, view_id, action.boxed_clone());
})
.on_drag(MouseButton::Left, |_, _, _| {})
.boxed()
.into_element()
}
ContextMenuItem::Static(f) => f(cx),
@ -484,12 +478,11 @@ impl ContextMenu {
.with_height(1.)
.contained()
.with_style(style.separator)
.boxed(),
.into_element(),
}
}))
.contained()
.with_style(style.container)
.boxed()
})
.on_down_out(MouseButton::Left, |_, _, cx| cx.dispatch_action(Cancel))
.on_down_out(MouseButton::Right, |_, _, cx| cx.dispatch_action(Cancel))