pub struct ElementHandle<'a> { /* private fields */ }Expand description
Handle returned by Elements::add for chaining .key() and .width().
This handle has a builder-style API — call methods on it immediately
after adding a component to an Elements list:
ⓘ
let mut els = Elements::new();
els.add(Spinner::new("loading..."))
.key("my-spinner")
.width(WidthConstraint::Fixed(20));Implementations§
Source§impl<'a> ElementHandle<'a>
impl<'a> ElementHandle<'a>
Sourcepub fn key(self, key: impl Into<String>) -> Self
pub fn key(self, key: impl Into<String>) -> Self
Set a key for stable identity across rebuilds.
Keyed elements are matched by key during reconciliation, allowing them to survive position changes. Without a key, elements are matched by position and type.
Examples found in repository?
examples/lifecycle.rs (line 125)
112fn task_view(state: &AppState) -> Elements {
113 let mut els = Elements::new();
114
115 els.add(
116 TextBlock::new().line(
117 format!("Tasks ({})", state.tasks.len()),
118 Style::default()
119 .fg(Color::White)
120 .add_modifier(Modifier::BOLD),
121 ),
122 );
123
124 for task in &state.tasks {
125 els.add(StatusLog::new(task)).key(task.clone());
126 }
127
128 if state.processing {
129 els.add(Spinner::new("Processing...")).key("spinner");
130 }
131
132 els.add(TextBlock::new().line("---", Style::default().fg(Color::DarkGray)));
133
134 els
135}More examples
examples/declarative.rs (line 46)
41fn chat_view(state: &AppState) -> Elements {
42 let mut els = Elements::new();
43
44 // Render all messages with stable keys
45 for (i, msg) in state.messages.iter().enumerate() {
46 els.add(Markdown::new(msg)).key(format!("msg-{i}"));
47 }
48
49 // Show thinking spinner if active (auto-animates via tick registration)
50 if state.thinking {
51 els.add(Spinner::new("Thinking...")).key("thinking");
52 }
53
54 // Show tool call spinner if active (auto-animates via tick registration)
55 if let Some(ref tool) = state.tool_running {
56 els.add(Spinner::new(format!("Running {}...", tool)))
57 .key("tool");
58 }
59
60 // Separator at the bottom
61 if !state.messages.is_empty() || state.thinking || state.tool_running.is_some() {
62 els.add(TextBlock::new().line("---", Style::default().fg(Color::DarkGray)));
63 }
64
65 els
66}examples/chat.rs (line 240)
225fn chat_view(state: &AppState) -> Elements {
226 let mut els = Elements::new();
227
228 for msg in &state.messages {
229 let key = format!("msg-{}", msg.id);
230 match &msg.kind {
231 MessageKind::User(text) => {
232 els.add(
233 TextBlock::new().line(
234 format!("> {}", text),
235 Style::default()
236 .fg(Color::Cyan)
237 .add_modifier(Modifier::BOLD),
238 ),
239 )
240 .key(key);
241 }
242 MessageKind::Assistant { content, done } => {
243 if *done {
244 els.add(Markdown::new(content)).key(key);
245 } else if content.is_empty() {
246 els.add(StreamingDots).key(key);
247 } else {
248 // Show content with a blinking cursor
249 els.add(Markdown::new(format!("{}▌", content))).key(key);
250 }
251 }
252 }
253 }
254
255 // Separator
256 els.add(TextBlock::new());
257
258 // Input box
259 els.add(InputBox {
260 text: state.input.clone(),
261 cursor: state.cursor,
262 prompt: "You".into(),
263 })
264 .key("input");
265
266 els
267}Sourcepub fn width(self, constraint: WidthConstraint) -> Self
pub fn width(self, constraint: WidthConstraint) -> Self
Set the width constraint for this element within a horizontal container.
Only meaningful when the element is a child of an HStack.
Fixed(n) reserves exactly n columns. Fill (default) takes
remaining space, split equally among Fill siblings.
Auto Trait Implementations§
impl<'a> Freeze for ElementHandle<'a>
impl<'a> !RefUnwindSafe for ElementHandle<'a>
impl<'a> Send for ElementHandle<'a>
impl<'a> !Sync for ElementHandle<'a>
impl<'a> Unpin for ElementHandle<'a>
impl<'a> UnsafeUnpin for ElementHandle<'a>
impl<'a> !UnwindSafe for ElementHandle<'a>
Blanket Implementations§
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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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