Skip to main content

Element

Struct Element 

Source
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: bool

When 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: bool

When true, children are clipped to this element’s painted bounds. Set implicitly by Element::scrollable and via Element::clip.

§kind: ElementKind

Implementations§

Source§

impl Element

Source

pub fn boxed(style: BoxStyle) -> Self

A decorated leaf (background/button/divider).

Source

pub fn text(text: impl Into<String>, size: f64, color: Color) -> Self

A single line of text in color at size logical pixels.

Source

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.

Source

pub fn stack(axis: Axis, children: Vec<Element>) -> Self

An undecorated container laying children along axis.

Source

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());
Source

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.

Source

pub fn on_key<S>( self, cx: &mut Cx<'_, S>, handler: impl FnMut(&mut S, &KeyInput) + 'static, ) -> Self

Make this element focusable and route keyboard input to handler. The handler receives a KeyInput (committed text or an editing key) while this element holds focus. Registers in cx and stamps the resulting FocusId.

Source

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.

Source

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.

Source

pub fn fill(self, color: Color) -> Self

Source

pub fn radius(self, radius: f64) -> Self

Source

pub fn border(self, color: Color, width: f64) -> Self

Source

pub fn padding(self, insets: Insets) -> Self

Source

pub fn width(self, w: f64) -> Self

Source

pub fn height(self, h: f64) -> Self

Source

pub fn grow(self, grow: f64) -> Self

Source

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.

Source

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.

Source

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.

Source

pub fn clip(self) -> Self

Clip this element’s children to its painted bounds (e.g. for an overlay panel). scrollable sets this implicitly.

Source

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.

Source

pub fn gap(self, gap: f64) -> Self

Source

pub fn align(self, main: Align, cross: Align) -> Self

Trait Implementations§

Source§

impl Clone for Element

Source§

fn clone(&self) -> Element

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Element

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Element

Source§

fn eq(&self, other: &Element) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Element

Source§

impl View for Element

An Element is itself a (trivial) view.

Source§

fn build(&self, _theme: &Theme) -> Element

Build this view’s element tree under the given theme.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.