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

@ -25,7 +25,7 @@ where
impl<V: View, F> Drawable<V> for Canvas<V, F>
where
F: FnMut(&mut SceneBuilder, RectF, RectF, &mut V, &mut ViewContext<V>),
F: 'static + FnMut(&mut SceneBuilder, RectF, RectF, &mut V, &mut ViewContext<V>),
{
type LayoutState = ();
type PaintState = ();

View file

@ -28,9 +28,9 @@ impl<V: View> ToJson for Constraint<V> {
}
impl<V: View> ConstrainedBox<V> {
pub fn new(child: Element<V>) -> Self {
pub fn new(child: impl Drawable<V>) -> Self {
Self {
child,
child: child.into_element(),
constraint: Constraint::Static(Default::default()),
}
}

View file

@ -13,9 +13,9 @@ pub struct Expanded<V: View> {
}
impl<V: View> Expanded<V> {
pub fn new(child: Element<V>) -> Self {
pub fn new(child: impl Drawable<V>) -> Self {
Self {
child,
child: child.into_element(),
full_width: true,
full_height: true,
}

View file

@ -403,13 +403,13 @@ pub struct FlexItem<V: View> {
}
impl<V: View> FlexItem<V> {
pub fn new(child: Element<V>) -> Self {
pub fn new(child: impl Drawable<V>) -> Self {
FlexItem {
metadata: FlexParentData {
flex: None,
float: false,
},
child,
child: child.into_element(),
}
}

View file

@ -12,9 +12,9 @@ pub struct Hook<V: View> {
}
impl<V: View> Hook<V> {
pub fn new(child: Element<V>) -> Self {
pub fn new(child: impl Drawable<V>) -> Self {
Self {
child,
child: child.into_element(),
after_layout: None,
}
}

View file

@ -49,11 +49,10 @@ impl<V: View> Drawable<V> for KeystrokeLabel {
Label::new(keystroke.to_string(), self.text_style.clone())
.contained()
.with_style(self.container_style)
.boxed()
}))
.boxed()
.into_element()
} else {
Empty::new().collapsed().boxed()
Empty::new().collapsed().into_element()
};
let size = element.layout(constraint, view, cx);

View file

@ -347,21 +347,21 @@ impl<V: View> Drawable<V> for List<V> {
}
impl<V: View> ListState<V> {
pub fn new<F>(
pub fn new<D, F>(
element_count: usize,
orientation: Orientation,
overdraw: f32,
render_item: F,
mut render_item: F,
) -> Self
where
V: View,
F: 'static + FnMut(&mut V, usize, &mut ViewContext<V>) -> Element<V>,
D: Drawable<V>,
F: 'static + FnMut(&mut V, usize, &mut ViewContext<V>) -> D,
{
let mut items = SumTree::new();
items.extend((0..element_count).map(|_| ListItem::Unrendered), &());
Self(Rc::new(RefCell::new(StateInner {
last_layout_width: None,
render_item: Box::new(render_item),
render_item: Box::new(move |view, ix, cx| render_item(view, ix, cx).into_element()),
rendered_range: 0..0,
items,
logical_scroll_top: None,
@ -660,7 +660,7 @@ mod tests {
let elements = elements.clone();
move |_, ix, _| {
let (id, height) = elements.borrow()[ix];
TestElement::new(id, height).boxed()
TestElement::new(id, height).into_element()
}
});
@ -765,7 +765,7 @@ mod tests {
let elements = elements.clone();
move |_, ix, _| {
let (id, height) = elements.borrow()[ix];
TestElement::new(id, height).boxed()
TestElement::new(id, height).into_element()
}
});
@ -921,7 +921,7 @@ mod tests {
}
fn render(&mut self, _: &mut ViewContext<Self>) -> Element<Self> {
Empty::new().boxed()
Empty::new().into_element()
}
}

View file

@ -32,13 +32,13 @@ pub struct MouseEventHandler<Tag: 'static, V: View> {
/// 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: View> MouseEventHandler<Tag, V> {
pub fn new<F>(region_id: usize, cx: &mut ViewContext<V>, render_child: F) -> Self
pub fn new<D, F>(region_id: usize, cx: &mut ViewContext<V>, render_child: F) -> Self
where
V: View,
F: FnOnce(&mut MouseState, &mut ViewContext<V>) -> Element<V>,
D: Drawable<V>,
F: FnOnce(&mut MouseState, &mut ViewContext<V>) -> D,
{
let mut mouse_state = cx.mouse_state::<Tag>(region_id);
let child = render_child(&mut mouse_state, cx);
let child = render_child(&mut mouse_state, cx).into_element();
let notify_on_hover = mouse_state.accessed_hovered();
let notify_on_click = mouse_state.accessed_clicked();
Self {
@ -58,10 +58,10 @@ impl<Tag, V: View> MouseEventHandler<Tag, V> {
/// Modifies the MouseEventHandler to render the MouseRegion above the child element. Useful
/// for drag and drop handling and similar events which should be captured before the child
/// gets the opportunity
pub fn above<F>(region_id: usize, cx: &mut ViewContext<V>, render_child: F) -> Self
pub fn above<D, F>(region_id: usize, cx: &mut ViewContext<V>, render_child: F) -> Self
where
V: View,
F: FnOnce(&mut MouseState, &mut ViewContext<V>) -> Element<V>,
D: Drawable<V>,
F: FnOnce(&mut MouseState, &mut ViewContext<V>) -> D,
{
let mut handler = Self::new(region_id, cx, render_child);
handler.above = true;

View file

@ -73,9 +73,9 @@ impl AnchorCorner {
}
impl<V: View> Overlay<V> {
pub fn new(child: Element<V>) -> Self {
pub fn new(child: impl Drawable<V>) -> Self {
Self {
child,
child: child.into_element(),
anchor_position: None,
anchor_corner: AnchorCorner::TopLeft,
fit_mode: OverlayFitMode::None,

View file

@ -108,7 +108,6 @@ impl<V: View> Resizable<V> {
Axis::Horizontal => constrained.with_max_width(state.custom_dimension.get()),
Axis::Vertical => constrained.with_max_height(state.custom_dimension.get()),
}
.boxed()
})
.on_after_layout({
let state = state.clone();
@ -116,7 +115,7 @@ impl<V: View> Resizable<V> {
state.actual_dimension.set(side.relevant_component(size));
}
})
.boxed();
.into_element();
Self {
side,

View file

@ -308,7 +308,7 @@ mod tests {
}
fn render(&mut self, _: &mut ViewContext<Self>) -> Element<Self> {
Empty::new().boxed()
Empty::new().into_element()
}
}
}

View file

@ -71,8 +71,7 @@ impl<V: View> Tooltip<V> {
style.clone(),
action.as_ref().map(|a| a.boxed_clone()),
true,
)
.boxed();
);
Some(
Overlay::new(
Self::render_tooltip(focused_view_id, text, style, action, false)
@ -80,14 +79,13 @@ impl<V: View> Tooltip<V> {
.dynamically(move |constraint, view, cx| {
SizeConstraint::strict_along(
Axis::Vertical,
collapsed_tooltip.layout(constraint, view, cx).y(),
collapsed_tooltip.layout(constraint, view, cx).0.y(),
)
})
.boxed(),
}),
)
.with_fit_mode(OverlayFitMode::SwitchAnchor)
.with_anchor_position(state.position.get())
.boxed(),
.into_element(),
)
} else {
None
@ -119,7 +117,7 @@ impl<V: View> Tooltip<V> {
cx.notify();
}
})
.boxed();
.into_element();
Self {
child,
tooltip,
@ -145,9 +143,9 @@ impl<V: View> Tooltip<V> {
};
if measure {
text.flex(1., false).boxed()
text.flex(1., false).into_element()
} else {
text.flex(1., false).aligned().boxed()
text.flex(1., false).aligned().into_element()
}
})
.with_children(action.and_then(|action| {
@ -158,9 +156,9 @@ impl<V: View> Tooltip<V> {
style.keystroke.text,
);
if measure {
Some(keystroke_label.boxed())
Some(keystroke_label.into_element())
} else {
Some(keystroke_label.aligned().boxed())
Some(keystroke_label.aligned().into_element())
}
}))
.contained()