Return impl IntoElement from RenderOnce::render

This makes it a bit more approachable to implement.

This required converting the state of Component to AnyElement, which
costs an allocation. I'm hoping this will be ok performance-wise now
that AnyElements use bump allocation, but I need to benchmark still.
This commit is contained in:
Nathan Sobo
2024-01-02 10:27:09 -07:00
parent 1b5c8b2b4a
commit 68e7d99219
35 changed files with 74 additions and 183 deletions
+2 -4
View File
@@ -1,5 +1,5 @@
use crate::prelude::*;
use gpui::{img, Div, Hsla, ImageSource, Img, IntoElement, Styled};
use gpui::{img, Hsla, ImageSource, Img, IntoElement, Styled};
#[derive(Debug, Default, PartialEq, Clone)]
pub enum Shape {
@@ -16,9 +16,7 @@ pub struct Avatar {
}
impl RenderOnce for Avatar {
type Output = Div;
fn render(mut self, cx: &mut WindowContext) -> Self::Output {
fn render(mut self, cx: &mut WindowContext) -> impl IntoElement {
if self.image.style().corner_radii.top_left.is_none() {
self = self.shape(Shape::Circle);
}
+2 -3
View File
@@ -136,9 +136,8 @@ impl ButtonCommon for Button {
}
impl RenderOnce for Button {
type Output = ButtonLike;
fn render(self, _cx: &mut WindowContext) -> Self::Output {
#[allow(refining_impl_trait)]
fn render(self, _cx: &mut WindowContext) -> ButtonLike {
let is_disabled = self.base.disabled;
let is_selected = self.base.selected;
@@ -63,9 +63,7 @@ impl Selectable for ButtonIcon {
}
impl RenderOnce for ButtonIcon {
type Output = IconElement;
fn render(self, _cx: &mut WindowContext) -> Self::Output {
fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
let icon = self
.selected_icon
.filter(|_| self.selected)
@@ -1,5 +1,5 @@
use gpui::{relative, DefiniteLength, MouseButton};
use gpui::{rems, transparent_black, AnyElement, AnyView, ClickEvent, Div, Hsla, Rems, Stateful};
use gpui::{rems, transparent_black, AnyElement, AnyView, ClickEvent, Hsla, Rems};
use smallvec::SmallVec;
use crate::prelude::*;
@@ -363,9 +363,7 @@ impl ParentElement for ButtonLike {
}
impl RenderOnce for ButtonLike {
type Output = Stateful<Div>;
fn render(self, cx: &mut WindowContext) -> Self::Output {
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
self.base
.h_flex()
.id(self.id.clone())
@@ -106,9 +106,7 @@ impl VisibleOnHover for IconButton {
}
impl RenderOnce for IconButton {
type Output = ButtonLike;
fn render(self, _cx: &mut WindowContext) -> Self::Output {
fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
let is_disabled = self.base.disabled;
let is_selected = self.base.selected;
@@ -99,9 +99,7 @@ impl ButtonCommon for ToggleButton {
}
impl RenderOnce for ToggleButton {
type Output = ButtonLike;
fn render(self, _cx: &mut WindowContext) -> Self::Output {
fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
let is_disabled = self.base.disabled;
let is_selected = self.base.selected;
+2 -4
View File
@@ -1,4 +1,4 @@
use gpui::{div, prelude::*, Div, Element, ElementId, IntoElement, Styled, WindowContext};
use gpui::{div, prelude::*, Element, ElementId, IntoElement, Styled, WindowContext};
use crate::prelude::*;
use crate::{Color, Icon, IconElement, Selection};
@@ -19,9 +19,7 @@ pub struct Checkbox {
}
impl RenderOnce for Checkbox {
type Output = gpui::Stateful<Div>;
fn render(self, cx: &mut WindowContext) -> Self::Output {
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
let group_id = format!("checkbox_group_{:?}", self.id);
let icon = match self.checked {
+1 -3
View File
@@ -28,9 +28,7 @@ impl Disclosure {
}
impl RenderOnce for Disclosure {
type Output = IconButton;
fn render(self, _cx: &mut WindowContext) -> Self::Output {
fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
IconButton::new(
self.id,
match self.is_open {
+2 -4
View File
@@ -1,4 +1,4 @@
use gpui::{Div, Hsla, IntoElement};
use gpui::{Hsla, IntoElement};
use crate::prelude::*;
@@ -31,9 +31,7 @@ pub struct Divider {
}
impl RenderOnce for Divider {
type Output = Div;
fn render(self, cx: &mut WindowContext) -> Self::Output {
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
div()
.map(|this| match self.direction {
DividerDirection::Horizontal => {
+2 -4
View File
@@ -1,4 +1,4 @@
use gpui::{rems, svg, IntoElement, Rems, Svg};
use gpui::{rems, svg, IntoElement, Rems};
use strum::EnumIter;
use crate::prelude::*;
@@ -200,9 +200,7 @@ pub struct IconElement {
}
impl RenderOnce for IconElement {
type Output = Svg;
fn render(self, cx: &mut WindowContext) -> Self::Output {
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
svg()
.size(self.size.rems())
.flex_none()
+2 -4
View File
@@ -1,4 +1,4 @@
use gpui::{Div, Position};
use gpui::Position;
use crate::prelude::*;
@@ -45,9 +45,7 @@ impl Indicator {
}
impl RenderOnce for Indicator {
type Output = Div;
fn render(self, cx: &mut WindowContext) -> Self::Output {
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
div()
.flex_none()
.map(|this| match self.style {
+4 -10
View File
@@ -1,5 +1,5 @@
use crate::{h_stack, prelude::*, Icon, IconElement, IconSize};
use gpui::{relative, rems, Action, Div, FocusHandle, IntoElement, Keystroke};
use gpui::{relative, rems, Action, FocusHandle, IntoElement, Keystroke};
#[derive(IntoElement, Clone)]
pub struct KeyBinding {
@@ -11,9 +11,7 @@ pub struct KeyBinding {
}
impl RenderOnce for KeyBinding {
type Output = Div;
fn render(self, cx: &mut WindowContext) -> Self::Output {
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
h_stack()
.flex_none()
.gap_2()
@@ -87,9 +85,7 @@ pub struct Key {
}
impl RenderOnce for Key {
type Output = Div;
fn render(self, cx: &mut WindowContext) -> Self::Output {
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
let single_char = self.key.len() == 1;
div()
@@ -121,9 +117,7 @@ pub struct KeyIcon {
}
impl RenderOnce for KeyIcon {
type Output = Div;
fn render(self, _cx: &mut WindowContext) -> Self::Output {
fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
div()
.w(rems(14. / 16.))
.child(IconElement::new(self.icon).size(IconSize::Small))
@@ -46,9 +46,7 @@ impl LabelCommon for HighlightedLabel {
}
impl RenderOnce for HighlightedLabel {
type Output = LabelLike;
fn render(self, cx: &mut WindowContext) -> Self::Output {
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
let highlight_color = cx.theme().colors().text_accent;
let mut highlight_indices = self.highlight_indices.iter().copied().peekable();
+1 -3
View File
@@ -40,9 +40,7 @@ impl LabelCommon for Label {
}
impl RenderOnce for Label {
type Output = LabelLike;
fn render(self, _cx: &mut WindowContext) -> Self::Output {
fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
self.base.child(self.label)
}
}
@@ -1,4 +1,4 @@
use gpui::{relative, AnyElement, Div, Styled};
use gpui::{relative, AnyElement, Styled};
use smallvec::SmallVec;
use crate::prelude::*;
@@ -76,9 +76,7 @@ impl ParentElement for LabelLike {
}
impl RenderOnce for LabelLike {
type Output = Div;
fn render(self, cx: &mut WindowContext) -> Self::Output {
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
div()
.when(self.strikethrough, |this| {
this.relative().child(
+2 -4
View File
@@ -1,4 +1,4 @@
use gpui::{AnyElement, Div};
use gpui::AnyElement;
use smallvec::SmallVec;
use crate::{prelude::*, v_stack, Label, ListHeader};
@@ -46,9 +46,7 @@ impl ParentElement for List {
}
impl RenderOnce for List {
type Output = Div;
fn render(self, _cx: &mut WindowContext) -> Self::Output {
fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
v_stack().w_full().py_1().children(self.header).map(|this| {
match (self.children.is_empty(), self.toggle) {
(false, _) => this.children(self.children),
@@ -1,6 +1,5 @@
use gpui::{AnyElement, ClickEvent, Div, Stateful};
use crate::{h_stack, prelude::*, Disclosure, Label};
use gpui::{AnyElement, ClickEvent};
#[derive(IntoElement)]
pub struct ListHeader {
@@ -76,9 +75,7 @@ impl Selectable for ListHeader {
}
impl RenderOnce for ListHeader {
type Output = Stateful<Div>;
fn render(self, cx: &mut WindowContext) -> Self::Output {
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
h_stack()
.id(self.label.clone())
.w_full()
+2 -6
View File
@@ -1,6 +1,4 @@
use gpui::{
px, AnyElement, AnyView, ClickEvent, Div, MouseButton, MouseDownEvent, Pixels, Stateful,
};
use gpui::{px, AnyElement, AnyView, ClickEvent, MouseButton, MouseDownEvent, Pixels};
use smallvec::SmallVec;
use crate::{prelude::*, Disclosure};
@@ -147,9 +145,7 @@ impl ParentElement for ListItem {
}
impl RenderOnce for ListItem {
type Output = Stateful<Div>;
fn render(self, cx: &mut WindowContext) -> Self::Output {
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
h_stack()
.id(self.id)
.w_full()
@@ -1,14 +1,10 @@
use gpui::Div;
use crate::prelude::*;
#[derive(IntoElement)]
pub struct ListSeparator;
impl RenderOnce for ListSeparator {
type Output = Div;
fn render(self, cx: &mut WindowContext) -> Self::Output {
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
div()
.h_px()
.w_full()
@@ -1,5 +1,3 @@
use gpui::Div;
use crate::prelude::*;
use crate::{h_stack, Icon, IconElement, IconSize, Label};
@@ -26,9 +24,7 @@ impl ListSubHeader {
}
impl RenderOnce for ListSubHeader {
type Output = Div;
fn render(self, _cx: &mut WindowContext) -> Self::Output {
fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
h_stack().flex_1().w_full().relative().py_1().child(
div()
.h_6()
+2 -4
View File
@@ -1,5 +1,5 @@
use gpui::{
div, AnyElement, Div, Element, ElementId, IntoElement, ParentElement, RenderOnce, Styled,
div, AnyElement, Element, ElementId, IntoElement, ParentElement, RenderOnce, Styled,
WindowContext,
};
use smallvec::SmallVec;
@@ -41,9 +41,7 @@ pub struct Popover {
}
impl RenderOnce for Popover {
type Output = Div;
fn render(self, cx: &mut WindowContext) -> Self::Output {
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
div()
.flex()
.gap_1()
+2 -2
View File
@@ -153,7 +153,7 @@ impl<M: ManagedView> Element for PopoverMenu<M> {
}
let mut element = overlay.child(menu.clone()).into_any();
menu_layout_id = Some(element.layout(cx));
menu_layout_id = Some(element.request_layout(cx));
element
});
@@ -164,7 +164,7 @@ impl<M: ManagedView> Element for PopoverMenu<M> {
let child_layout_id = child_element
.as_mut()
.map(|child_element| child_element.layout(cx));
.map(|child_element| child_element.request_layout(cx));
let layout_id = cx.request_layout(
&gpui::Style::default(),
@@ -81,7 +81,7 @@ impl<M: ManagedView> Element for RightClickMenu<M> {
overlay = overlay.position(*position.borrow());
let mut element = overlay.child(menu.clone()).into_any();
menu_layout_id = Some(element.layout(cx));
menu_layout_id = Some(element.request_layout(cx));
element
});
@@ -92,7 +92,7 @@ impl<M: ManagedView> Element for RightClickMenu<M> {
let child_layout_id = child_element
.as_mut()
.map(|child_element| child_element.layout(cx));
.map(|child_element| child_element.request_layout(cx));
let layout_id = cx.request_layout(
&gpui::Style::default(),
+2 -3
View File
@@ -93,9 +93,8 @@ impl ParentElement for Tab {
}
impl RenderOnce for Tab {
type Output = Stateful<Div>;
fn render(self, cx: &mut WindowContext) -> Self::Output {
#[allow(refining_impl_trait)]
fn render(self, cx: &mut WindowContext) -> Stateful<Div> {
let (text_color, tab_bg, _tab_hover_bg, _tab_active_bg) = match self.selected {
false => (
cx.theme().colors().text_muted,
+2 -4
View File
@@ -1,4 +1,4 @@
use gpui::{AnyElement, ScrollHandle, Stateful};
use gpui::{AnyElement, ScrollHandle};
use smallvec::SmallVec;
use crate::prelude::*;
@@ -89,9 +89,7 @@ impl ParentElement for TabBar {
}
impl RenderOnce for TabBar {
type Output = Stateful<Div>;
fn render(self, cx: &mut WindowContext) -> Self::Output {
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
const HEIGHT_IN_REMS: f32 = 30. / 16.;
div()