pub struct Elements { /* private fields */ }Expand description
A list of component descriptions for declarative tree building.
Elements is what view functions return and what the framework
reconciles against the existing component tree. Build one with the
element! macro or the imperative API:
// With the element! macro (preferred):
fn my_view(state: &MyState) -> Elements {
element! {
"Hello"
#(if state.loading {
Spinner(key: "spinner", label: "Loading...")
})
}
}
// Imperative API:
fn my_view(state: &MyState) -> Elements {
let mut els = Elements::new();
els.add(Text::unstyled("Hello"));
if state.loading {
els.add(Spinner::new("Loading...")).key("spinner");
}
els
}Implementations§
Source§impl Elements
impl Elements
Sourcepub fn new() -> Self
pub fn new() -> Self
Create an empty element list.
Examples found in repository?
108fn task_view(state: &AppState) -> Elements {
109 let mut els = Elements::new();
110
111 els.add(Text::styled(
112 format!("Tasks ({})", state.tasks.len()),
113 Style::default()
114 .fg(Color::White)
115 .add_modifier(Modifier::BOLD),
116 ));
117
118 for task in &state.tasks {
119 els.add(StatusLog::new(task)).key(task.clone());
120 }
121
122 if state.processing {
123 els.add(Spinner::new("Processing...")).key("spinner");
124 }
125
126 els.add(Text::styled("---", Style::default().fg(Color::DarkGray)));
127
128 els
129}Sourcepub fn add<C: Component>(&mut self, component: C) -> ElementHandle<'_>
pub fn add<C: Component>(&mut self, component: C) -> ElementHandle<'_>
Add a component to the list.
Returns an ElementHandle that can be used to set a key
for stable identity across rebuilds.
Examples found in repository?
108fn task_view(state: &AppState) -> Elements {
109 let mut els = Elements::new();
110
111 els.add(Text::styled(
112 format!("Tasks ({})", state.tasks.len()),
113 Style::default()
114 .fg(Color::White)
115 .add_modifier(Modifier::BOLD),
116 ));
117
118 for task in &state.tasks {
119 els.add(StatusLog::new(task)).key(task.clone());
120 }
121
122 if state.processing {
123 els.add(Spinner::new("Processing...")).key("spinner");
124 }
125
126 els.add(Text::styled("---", Style::default().fg(Color::DarkGray)));
127
128 els
129}Sourcepub fn add_with_children<C: Component>(
&mut self,
component: C,
children: Elements,
) -> ElementHandle<'_>
pub fn add_with_children<C: Component>( &mut self, component: C, children: Elements, ) -> ElementHandle<'_>
Add a component with nested children.
The component is created first, then children are built as its
descendants. The component’s children() method receives
these as the slot parameter.
Sourcepub fn group(&mut self, children: Elements) -> ElementHandle<'_>
pub fn group(&mut self, children: Elements) -> ElementHandle<'_>
Add a VStack wrapper around the given children.
Equivalent to add_with_children(VStack, children).
Sourcepub fn hstack(&mut self, children: Elements) -> ElementHandle<'_>
pub fn hstack(&mut self, children: Elements) -> ElementHandle<'_>
Add an HStack wrapper around the given children.
Children default to WidthConstraint::Fill.
Use .width(WidthConstraint::Fixed(n)) for fixed-width children.
Trait Implementations§
Source§impl<C: Component> AddTo<Elements> for ComponentWithSlot<C>
impl<C: Component> AddTo<Elements> for ComponentWithSlot<C>
Source§type Handle<'a> = ElementHandle<'a>
type Handle<'a> = ElementHandle<'a>
.key() / .width() chaining.Source§fn add_to(self, els: &mut Elements) -> ElementHandle<'_>
fn add_to(self, els: &mut Elements) -> ElementHandle<'_>
.key() and .width().Source§impl AddTo<Elements> for String
String → Elements: creates a Text component with the string as content.
impl AddTo<Elements> for String
String → Elements: creates a Text component with the string as content.
This powers the element! string literal sugar — "hello" becomes a
Text component. The same AddTo dispatch also works
inside data children contexts (e.g., Text { "hello" }) via the
Into<TextChild> blanket impl.
Source§type Handle<'a> = ElementHandle<'a>
type Handle<'a> = ElementHandle<'a>
.key() / .width() chaining.Source§fn add_to(self, els: &mut Elements) -> ElementHandle<'_>
fn add_to(self, els: &mut Elements) -> ElementHandle<'_>
.key() and .width().Source§impl SpliceInto<Elements> for Elements
impl SpliceInto<Elements> for Elements
Source§fn splice_into(self, collector: &mut Elements)
fn splice_into(self, collector: &mut Elements)
Auto Trait Implementations§
impl Freeze for Elements
impl !RefUnwindSafe for Elements
impl Send for Elements
impl !Sync for Elements
impl Unpin for Elements
impl UnsafeUnpin for Elements
impl !UnwindSafe for Elements
Blanket Implementations§
Source§impl<T, V> AddTo<DataChildren<T>> for Vwhere
V: Into<T>,
impl<T, V> AddTo<DataChildren<T>> for Vwhere
V: Into<T>,
Source§type Handle<'a> = DataHandle
where
T: 'a
type Handle<'a> = DataHandle where T: 'a
.key() / .width() chaining.Source§fn add_to(self, collector: &mut DataChildren<T>) -> DataHandle
fn add_to(self, collector: &mut DataChildren<T>) -> DataHandle
.key() and .width().Source§impl<C> AddTo<Elements> for Cwhere
C: Component,
impl<C> AddTo<Elements> for Cwhere
C: Component,
Source§type Handle<'a> = ElementHandle<'a>
type Handle<'a> = ElementHandle<'a>
.key() / .width() chaining.Source§fn add_to(self, els: &mut Elements) -> ElementHandle<'_>
fn add_to(self, els: &mut Elements) -> ElementHandle<'_>
.key() and .width().Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more