pub struct Element {Show 13 fields
pub layout: LayoutStyle,
pub decoration: BoxStyle,
pub action: Option<ActionId>,
pub focus: Option<FocusId>,
pub drag: Option<DragId>,
pub context: Option<ContextId>,
pub caret: Option<usize>,
pub selection: Option<(usize, usize)>,
pub text_pos: Option<TextPosId>,
pub wrap: bool,
pub scroll: Option<ScrollId>,
pub clip: bool,
pub kind: ElementKind,
}Expand description
A node in the element tree: layout properties, decoration, an optional interaction handle, and a kind.
PartialEq enables the runtime’s no-op repaint skip: if a rebuilt tree
equals the previous one, the cached frame is reused. Handler ids compare by
value and are assigned deterministically per build, so an unchanged view
produces an equal tree.
Fields§
§layout: LayoutStyle§decoration: BoxStyle§action: Option<ActionId>Handler this element routes pointer taps to, if any. Set via
Element::on_tap; resolved against the Cx handler table.
focus: Option<FocusId>Focus + keyboard handle, if this element is focusable. Set via
Element::on_key.
drag: Option<DragId>Drag handle, if this element responds to pointer drags. Set via
Element::on_drag.
context: Option<ContextId>Secondary-click (context) handle: this element opens a context menu on
right-click. Set via Element::on_context.
caret: Option<usize>Caret position as a byte index into this element’s text, for an editable
text leaf. None for non-editable text and non-text elements. Set via
Element::caret; the focus overlay draws the caret bar there.
selection: Option<(usize, usize)>Selected byte range [start, end) into this element’s text, for an
editable text leaf. None when there is no selection. Set via
Element::selection; the focus overlay highlights it.
text_pos: Option<TextPosId>Text-pointer handle: pointer presses/drags on this element resolve to a
byte index in its text. Set via Element::on_text_pos.
wrap: boolWhen true on a text element, the text greedily word-wraps to the
element’s content width instead of laying out on one line. Set via
Element::wrap.
scroll: Option<ScrollId>Scroll-container handle: when set, this element lays its children out at
natural size (overflowing its bounds), clips them, and routes wheel events
to the app’s offset for this id. Set via Element::scrollable.
clip: boolWhen true, children are clipped to this element’s painted bounds. Set
implicitly by Element::scrollable and via Element::clip.
kind: ElementKindImplementations§
Source§impl Element
impl Element
Sourcepub fn text(text: impl Into<String>, size: f64, color: Color) -> Self
pub fn text(text: impl Into<String>, size: f64, color: Color) -> Self
A single line of text in color at size logical pixels.
Sourcepub fn viewport(id: ViewportId) -> Self
pub fn viewport(id: ViewportId) -> Self
An embedded-content viewport leaf for id: a reserved rectangle the app
composites externally-rendered content into. Give it a size via
width/height (or
grow inside a flex parent); add a
border to frame it. The app locates it by id with
collect_viewports to blit content over the
placeholder, and routes pointer/keys landing inside it to that content.
Sourcepub fn stack(axis: Axis, children: Vec<Element>) -> Self
pub fn stack(axis: Axis, children: Vec<Element>) -> Self
An undecorated container laying children along axis.
Sourcepub fn on_tap<S>(
self,
cx: &mut Cx<'_, S>,
handler: impl FnMut(&mut S) + 'static,
) -> Self
pub fn on_tap<S>( self, cx: &mut Cx<'_, S>, handler: impl FnMut(&mut S) + 'static, ) -> Self
Route pointer taps on this element to handler, which runs against the
app state. Registers the handler in cx and stamps its ActionId.
let theme = Theme::light();
let mut cx = Cx::new(&theme);
let button = Element::boxed(BoxStyle::default())
.width(80.0)
.height(32.0)
.on_tap(&mut cx, |count: &mut i32| *count += 1);
assert!(button.action.is_some());Sourcepub fn on_context<S>(
self,
cx: &mut Cx<'_, S>,
handler: impl FnMut(&mut S, Point) + 'static,
) -> Self
pub fn on_context<S>( self, cx: &mut Cx<'_, S>, handler: impl FnMut(&mut S, Point) + 'static, ) -> Self
Route secondary (right) clicks on this element to handler, which runs
against the app state and receives the click position in logical pixels —
typically used to open a context menu there via Cx::overlay. Registers
the handler in cx and stamps its ContextId.
Sourcepub fn on_key<S>(
self,
cx: &mut Cx<'_, S>,
handler: impl FnMut(&mut S, &KeyInput) + 'static,
) -> Self
pub fn on_key<S>( self, cx: &mut Cx<'_, S>, handler: impl FnMut(&mut S, &KeyInput) + 'static, ) -> Self
Sourcepub fn on_drag<S>(
self,
cx: &mut Cx<'_, S>,
handler: impl FnMut(&mut S, f64) + 'static,
) -> Self
pub fn on_drag<S>( self, cx: &mut Cx<'_, S>, handler: impl FnMut(&mut S, f64) + 'static, ) -> Self
Make this element respond to pointer drags. The handler receives the
pointer’s fractional x position (0..=1) across the element on press and
while dragging. Registers in cx and stamps the resulting DragId.
Sourcepub fn on_text_pos<S>(
self,
cx: &mut Cx<'_, S>,
handler: impl FnMut(&mut S, usize, bool) + 'static,
) -> Self
pub fn on_text_pos<S>( self, cx: &mut Cx<'_, S>, handler: impl FnMut(&mut S, usize, bool) + 'static, ) -> Self
Resolve pointer presses/drags on this element to a byte index in its
text. The handler receives the resolved index and an extend flag
(false = initial press / place caret, true = drag / extend
selection). Registers in cx and stamps the resulting TextPosId.
pub fn fill(self, color: Color) -> Self
pub fn radius(self, radius: f64) -> Self
pub fn border(self, color: Color, width: f64) -> Self
pub fn padding(self, insets: Insets) -> Self
pub fn width(self, w: f64) -> Self
pub fn height(self, h: f64) -> Self
pub fn grow(self, grow: f64) -> Self
Sourcepub fn caret(self, byte_index: usize) -> Self
pub fn caret(self, byte_index: usize) -> Self
Mark this text element’s caret position (a byte index into its text), so the focus overlay draws the caret bar there instead of at the end. No effect on non-text elements.
Sourcepub fn selection(self, range: Option<(usize, usize)>) -> Self
pub fn selection(self, range: Option<(usize, usize)>) -> Self
Mark a selected byte range [start, end) into this text element, so the
focus overlay highlights it. An empty or reversed range is ignored.
Sourcepub fn wrap(self) -> Self
pub fn wrap(self) -> Self
Enable word-wrapping for a text element: the text wraps to the element’s content width across as many lines as needed. No effect on non-text elements.
Sourcepub fn clip(self) -> Self
pub fn clip(self) -> Self
Clip this element’s children to its painted bounds (e.g. for an overlay
panel). scrollable sets this implicitly.
Sourcepub fn scrollable(self, id: ScrollId) -> Self
pub fn scrollable(self, id: ScrollId) -> Self
Make this element a scroll container for id: its children lay out at
natural size (overflowing), are clipped to its bounds, and wheel events
over it scroll the app’s offset for id. Give it a fixed height (e.g.
.height(..)) to define the viewport.